CEF Graphics
CEF (Chromium Embedded Framework) supplies the web browser used inside an X-Plane plugin window. The page is ordinary HTML, CSS, and JavaScript. X-Plane owns and renders the browser; Lua creates and controls its window.
## Minimal Code for CEF:
--[[ XLua 2.0 ]]
local myBrowserWindow = nil
--======================================================================================================================
-- URL Resolution Utilities. These make it easy to specify a relative URL from your Lua to HTML assets,
-- e.g. XPLMWindowSetURL(myBrowserWindow, relURL("html/index.html"))
-- Without these, you'd have to provide nasty, absolute file paths, which differ in format on Mac and windows,
-- UGGGHH. Just use the one function "relURL(rel_path)", and call it a day!
--======================================================================================================================
local function encode_url_path(path)
-- Encode every byte except URL-safe path characters.
return path:gsub("([^%w%-%._~/:])", function(character)
return string.format("%%%02X", string.byte(character))
end)
end
------------------------------------------------------------------------------------------------------------------------
local function path_to_file_url(path)
-- File URLs always use forward slashes, including on Windows.
path = path:gsub("\\", "/")
path = encode_url_path(path)
if path:match("^%a:/") then
-- Windows drive path: C:/X-Plane/...
return "file:///" .. path
elseif path:sub(1, 2) == "//" then
-- Windows UNC path: //server/share/...
return "file:" .. path
elseif path:sub(1, 1) == "/" then
-- macOS/Linux absolute path. "file://" + "/..." gives file:///...
return "file://" .. path
end
error("Cannot create file URL from a relative path: " .. path)
end
------------------------------------------------------------------------------------------------------------------------
local aircraft = XPLMGetNthAircraftModel(0)
local aircraft_path = aircraft.outPath
local aircraft_directory = aircraft_path:match("^(.*[/\\])")
assert(aircraft_directory, "Could not determine aircraft directory")
local project_directory = aircraft_directory .. "plugins/xlua2/scripts/tomcef/"
------------------------------------------------------------------------------------------------------------------------
-- USE THE FUNCTION BELOW, NOT the ones above, they're helpers to this one. Pass a relative path FROM this Lua file to
-- your HTML files.
local function relURL(relative_path)
return path_to_file_url(project_directory .. relative_path)
end
--======================================================================================================================
-- LIFECYCLE CALLBACKS
--======================================================================================================================
function XPluginStart()
myBrowserWindow = XPLMCreateWindowEx({
left = 100,
top = 600,
right = 700,
bottom = 200,
visible = true,
decorateAsFloatingWindow = XPLMWindowDecoration.xplm_WindowDecorationRoundRectangle,
windowContentType = XPLMWindowContentType.xplm_WindowContentTypeBrowser
})
XPLMSetWindowTitle (myBrowserWindow, "hello world test")
-- XPLMWindowSetURL (myBrowserWindow, relURL("site/hello_world.html"))
return myBrowserWindow ~= nil
end
function XPluginEnable()
return true
end
-- An aircraft XLua module is executed directly; XPluginStart is not dispatched.
function XPluginDisable()
end
function XPluginStop()
if myBrowserWindow then XPLMDestroyWindow(myBrowserWindow); myBrowserWindow = nil end
end
function aircraft_unload()
end
function XPluginReceiveMessage(from, message, parameter)
end