lua: uidocuments sub-environment

This commit is contained in:
MihailRis
2024-02-11 23:56:24 +03:00
parent 4ce46f34bf
commit e4f7315cc3
9 changed files with 31 additions and 16 deletions
+1
View File
@@ -5,6 +5,7 @@
#include "../graphics/Atlas.h" #include "../graphics/Atlas.h"
#include "../graphics/Font.h" #include "../graphics/Font.h"
#include "../frontend/UiDocument.h" #include "../frontend/UiDocument.h"
#include "../logic/scripting/scripting.h"
Assets::~Assets() { Assets::~Assets() {
} }
+1
View File
@@ -14,6 +14,7 @@
#include "../graphics/Font.h" #include "../graphics/Font.h"
#include "../graphics/TextureAnimation.h" #include "../graphics/TextureAnimation.h"
#include "../frontend/UiDocument.h" #include "../frontend/UiDocument.h"
#include "../logic/scripting/scripting.h"
namespace fs = std::filesystem; namespace fs = std::filesystem;
+1 -1
View File
@@ -131,12 +131,12 @@ void Engine::mainloop() {
Engine::~Engine() { Engine::~Engine() {
screen = nullptr; screen = nullptr;
content.reset(); content.reset();
scripting::close();
Audio::finalize(); Audio::finalize();
std::cout << "-- shutting down" << std::endl; std::cout << "-- shutting down" << std::endl;
assets.reset(); assets.reset();
scripting::close();
Window::terminate(); Window::terminate();
std::cout << "-- engine finished" << std::endl; std::cout << "-- engine finished" << std::endl;
} }
+10 -7
View File
@@ -1,5 +1,6 @@
#include "UiDocument.h" #include "UiDocument.h"
#include <iostream>
#include "gui/UINode.h" #include "gui/UINode.h"
#include "gui/panels.h" #include "gui/panels.h"
#include "InventoryView.h" #include "InventoryView.h"
@@ -11,8 +12,8 @@ UiDocument::UiDocument(
std::string id, std::string id,
uidocscript script, uidocscript script,
std::shared_ptr<gui::UINode> root, std::shared_ptr<gui::UINode> root,
int env std::unique_ptr<scripting::Environment> env
) : id(id), script(script), root(root), env(env) { ) : id(id), script(script), root(root), env(std::move(env)) {
collect(map, root); collect(map, root);
} }
@@ -34,7 +35,7 @@ const uidocscript& UiDocument::getScript() const {
} }
int UiDocument::getEnvironment() const { int UiDocument::getEnvironment() const {
return env; return env->getId();
} }
void UiDocument::collect(uinodes_map& map, std::shared_ptr<gui::UINode> node) { void UiDocument::collect(uinodes_map& map, std::shared_ptr<gui::UINode> node) {
@@ -50,10 +51,12 @@ void UiDocument::collect(uinodes_map& map, std::shared_ptr<gui::UINode> node) {
} }
} }
std::unique_ptr<UiDocument> UiDocument::read(int env, std::string namesp, fs::path file) { std::unique_ptr<UiDocument> UiDocument::read(int penv, std::string namesp, fs::path file) {
const std::string text = files::read_string(file); const std::string text = files::read_string(file);
auto xmldoc = xml::parse(file.u8string(), text); auto xmldoc = xml::parse(file.u8string(), text);
gui::UiXmlReader reader(env);
auto env = scripting::create_environment(penv);
gui::UiXmlReader reader(*env);
InventoryView::createReaders(reader); InventoryView::createReaders(reader);
auto view = reader.readXML( auto view = reader.readXML(
file.u8string(), xmldoc->getRoot() file.u8string(), xmldoc->getRoot()
@@ -61,7 +64,7 @@ std::unique_ptr<UiDocument> UiDocument::read(int env, std::string namesp, fs::pa
uidocscript script {}; uidocscript script {};
auto scriptFile = fs::path(file.u8string()+".lua"); auto scriptFile = fs::path(file.u8string()+".lua");
if (fs::is_regular_file(scriptFile)) { if (fs::is_regular_file(scriptFile)) {
scripting::load_layout_script(env, namesp, scriptFile, script); scripting::load_layout_script(env->getId(), namesp, scriptFile, script);
} }
return std::make_unique<UiDocument>(namesp, script, view, env); return std::make_unique<UiDocument>(namesp, script, view, std::move(env));
} }
+6 -2
View File
@@ -12,6 +12,10 @@ namespace gui {
class UINode; class UINode;
} }
namespace scripting {
class Environment;
}
struct uidocscript { struct uidocscript {
int environment; int environment;
bool onopen : 1; bool onopen : 1;
@@ -25,13 +29,13 @@ class UiDocument {
uidocscript script; uidocscript script;
uinodes_map map; uinodes_map map;
std::shared_ptr<gui::UINode> root; std::shared_ptr<gui::UINode> root;
int env; std::unique_ptr<scripting::Environment> env;
public: public:
UiDocument( UiDocument(
std::string id, std::string id,
uidocscript script, uidocscript script,
std::shared_ptr<gui::UINode> root, std::shared_ptr<gui::UINode> root,
int env std::unique_ptr<scripting::Environment> env
); );
const std::string& getId() const; const std::string& getId() const;
+8 -2
View File
@@ -228,7 +228,7 @@ const std::string lua::LuaState::storeAnonymous() {
return funcName; return funcName;
} }
int lua::LuaState::createEnvironment() { int lua::LuaState::createEnvironment(int parent) {
int id = nextEnvironment++; int id = nextEnvironment++;
// local env = {} // local env = {}
@@ -236,7 +236,13 @@ int lua::LuaState::createEnvironment() {
// setmetatable(env, {__index=_G}) // setmetatable(env, {__index=_G})
lua_createtable(L, 0, 1); lua_createtable(L, 0, 1);
lua_pushvalue(L, LUA_GLOBALSINDEX); if (parent == 0 || true) {
lua_pushvalue(L, LUA_GLOBALSINDEX);
} else {
if (pushenv(parent) == 0) {
lua_pushvalue(L, LUA_GLOBALSINDEX);
}
}
lua_setfield(L, -2, "__index"); lua_setfield(L, -2, "__index");
lua_setmetatable(L, -2); lua_setmetatable(L, -2);
+1 -1
View File
@@ -51,7 +51,7 @@ namespace lua {
bool rename(const std::string& from, const std::string& to); bool rename(const std::string& from, const std::string& to);
void remove(const std::string& name);; void remove(const std::string& name);;
void createFuncs(); void createFuncs();
int createEnvironment(); int createEnvironment(int parent);
void removeEnvironment(int id); void removeEnvironment(int id);
const std::string storeAnonymous(); const std::string storeAnonymous();
}; };
+2 -2
View File
@@ -91,8 +91,8 @@ wstringconsumer scripting::create_wstring_consumer(
}; };
} }
std::unique_ptr<Environment> scripting::create_environment() { std::unique_ptr<Environment> scripting::create_environment(int parent) {
return std::make_unique<Environment>(state->createEnvironment()); return std::make_unique<Environment>(state->createEnvironment(parent));
} }
void scripting::on_world_load(Level* level, BlocksController* blocks) { void scripting::on_world_load(Level* level, BlocksController* blocks) {
+1 -1
View File
@@ -51,7 +51,7 @@ namespace scripting {
const std::string& file="<string>" const std::string& file="<string>"
); );
std::unique_ptr<Environment> create_environment(); std::unique_ptr<Environment> create_environment(int parent=0);
void on_world_load(Level* level, BlocksController* blocks); void on_world_load(Level* level, BlocksController* blocks);
void on_world_save(); void on_world_save();