fix document global for external Lua scripts in designer and Android
This commit is contained in:
@@ -16,6 +16,7 @@
|
||||
#include "testing/ui_inspector.h"
|
||||
#include "testing/visual_capture.h"
|
||||
#include <RmlUi/Lua/Interpreter.h>
|
||||
#include <RmlUi/Lua/LuaType.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
#include <filesystem>
|
||||
@@ -77,6 +78,18 @@ bool LoadDocument(const std::string& path);
|
||||
void ReloadDocument();
|
||||
void PopulateSimulatorApps();
|
||||
|
||||
// Helper to set the 'document' global in Lua to the current document
|
||||
static void SetLuaDocumentGlobal(Rml::ElementDocument* doc) {
|
||||
if (!doc) return;
|
||||
lua_State* L = Rml::Lua::Interpreter::GetLuaState();
|
||||
if (!L) return;
|
||||
|
||||
// Push the document using RmlUi's Lua type system
|
||||
Rml::Lua::LuaType<Rml::ElementDocument>::push(L, doc, false);
|
||||
lua_setglobal(L, "document");
|
||||
std::cout << "Set Lua 'document' global to: " << doc->GetTitle() << std::endl;
|
||||
}
|
||||
|
||||
// GLFW callbacks
|
||||
static void ErrorCallback(int error, const char* description) {
|
||||
std::cerr << "GLFW Error " << error << ": " << description << std::endl;
|
||||
@@ -788,6 +801,7 @@ bool InitializeRmlUi(const std::string& assets_path, int fb_width, int fb_height
|
||||
auto* document = g_context->LoadDocument(path);
|
||||
if (document) {
|
||||
document->Show();
|
||||
SetLuaDocumentGlobal(document); // Set 'document' global for Lua scripts
|
||||
g_current_document_path = path;
|
||||
g_current_screen_url = path; // Track current screen for hierarchy dump
|
||||
// Log using RmlUi logging so it appears in log file
|
||||
@@ -840,6 +854,7 @@ bool InitializeRmlUi(const std::string& assets_path, int fb_width, int fb_height
|
||||
auto* document = g_context->LoadDocument(home_path);
|
||||
if (document) {
|
||||
document->Show();
|
||||
SetLuaDocumentGlobal(document); // Set 'document' global for Lua scripts
|
||||
g_current_document_path = home_path;
|
||||
g_current_screen_url = home_path;
|
||||
g_current_app_id = ""; // Clear current app
|
||||
@@ -929,6 +944,7 @@ bool InitializeRmlUi(const std::string& assets_path, int fb_width, int fb_height
|
||||
auto* document = g_context->LoadDocument(entry);
|
||||
if (document) {
|
||||
document->Show();
|
||||
SetLuaDocumentGlobal(document); // Set 'document' global for Lua scripts
|
||||
g_current_document_path = entry;
|
||||
g_current_screen_url = entry;
|
||||
std::cout << "Simulator: App launched successfully" << std::endl;
|
||||
@@ -973,6 +989,7 @@ bool InitializeRmlUi(const std::string& assets_path, int fb_width, int fb_height
|
||||
auto* document = g_context->LoadDocument(g_simulator_home_path);
|
||||
if (document) {
|
||||
document->Show();
|
||||
SetLuaDocumentGlobal(document); // Set 'document' global for Lua scripts
|
||||
g_current_document_path = g_simulator_home_path;
|
||||
g_current_screen_url = g_simulator_home_path;
|
||||
|
||||
@@ -1207,8 +1224,9 @@ bool LoadDocument(const std::string& path) {
|
||||
std::cerr << "Failed to load: " << path << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
document->Show();
|
||||
SetLuaDocumentGlobal(document); // Set 'document' global for Lua scripts
|
||||
g_current_document_path = path;
|
||||
g_current_screen_url = path; // Track current screen for hierarchy dump
|
||||
std::cout << "Loaded: " << path << std::endl;
|
||||
|
||||
Reference in New Issue
Block a user