lua: uidocuments sub-environment
This commit is contained in:
parent
4ce46f34bf
commit
e4f7315cc3
@ -5,6 +5,7 @@
|
||||
#include "../graphics/Atlas.h"
|
||||
#include "../graphics/Font.h"
|
||||
#include "../frontend/UiDocument.h"
|
||||
#include "../logic/scripting/scripting.h"
|
||||
|
||||
Assets::~Assets() {
|
||||
}
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
#include "../graphics/Font.h"
|
||||
#include "../graphics/TextureAnimation.h"
|
||||
#include "../frontend/UiDocument.h"
|
||||
#include "../logic/scripting/scripting.h"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
|
||||
@ -131,12 +131,12 @@ void Engine::mainloop() {
|
||||
Engine::~Engine() {
|
||||
screen = nullptr;
|
||||
content.reset();
|
||||
scripting::close();
|
||||
|
||||
Audio::finalize();
|
||||
|
||||
std::cout << "-- shutting down" << std::endl;
|
||||
assets.reset();
|
||||
scripting::close();
|
||||
Window::terminate();
|
||||
std::cout << "-- engine finished" << std::endl;
|
||||
}
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
#include "UiDocument.h"
|
||||
|
||||
#include <iostream>
|
||||
#include "gui/UINode.h"
|
||||
#include "gui/panels.h"
|
||||
#include "InventoryView.h"
|
||||
@ -11,8 +12,8 @@ UiDocument::UiDocument(
|
||||
std::string id,
|
||||
uidocscript script,
|
||||
std::shared_ptr<gui::UINode> root,
|
||||
int env
|
||||
) : id(id), script(script), root(root), env(env) {
|
||||
std::unique_ptr<scripting::Environment> env
|
||||
) : id(id), script(script), root(root), env(std::move(env)) {
|
||||
collect(map, root);
|
||||
}
|
||||
|
||||
@ -34,7 +35,7 @@ const uidocscript& UiDocument::getScript() const {
|
||||
}
|
||||
|
||||
int UiDocument::getEnvironment() const {
|
||||
return env;
|
||||
return env->getId();
|
||||
}
|
||||
|
||||
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);
|
||||
auto xmldoc = xml::parse(file.u8string(), text);
|
||||
gui::UiXmlReader reader(env);
|
||||
|
||||
auto env = scripting::create_environment(penv);
|
||||
gui::UiXmlReader reader(*env);
|
||||
InventoryView::createReaders(reader);
|
||||
auto view = reader.readXML(
|
||||
file.u8string(), xmldoc->getRoot()
|
||||
@ -61,7 +64,7 @@ std::unique_ptr<UiDocument> UiDocument::read(int env, std::string namesp, fs::pa
|
||||
uidocscript script {};
|
||||
auto scriptFile = fs::path(file.u8string()+".lua");
|
||||
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));
|
||||
}
|
||||
|
||||
@ -12,6 +12,10 @@ namespace gui {
|
||||
class UINode;
|
||||
}
|
||||
|
||||
namespace scripting {
|
||||
class Environment;
|
||||
}
|
||||
|
||||
struct uidocscript {
|
||||
int environment;
|
||||
bool onopen : 1;
|
||||
@ -25,13 +29,13 @@ class UiDocument {
|
||||
uidocscript script;
|
||||
uinodes_map map;
|
||||
std::shared_ptr<gui::UINode> root;
|
||||
int env;
|
||||
std::unique_ptr<scripting::Environment> env;
|
||||
public:
|
||||
UiDocument(
|
||||
std::string id,
|
||||
uidocscript script,
|
||||
std::shared_ptr<gui::UINode> root,
|
||||
int env
|
||||
std::unique_ptr<scripting::Environment> env
|
||||
);
|
||||
|
||||
const std::string& getId() const;
|
||||
|
||||
@ -228,7 +228,7 @@ const std::string lua::LuaState::storeAnonymous() {
|
||||
return funcName;
|
||||
}
|
||||
|
||||
int lua::LuaState::createEnvironment() {
|
||||
int lua::LuaState::createEnvironment(int parent) {
|
||||
int id = nextEnvironment++;
|
||||
|
||||
// local env = {}
|
||||
@ -236,7 +236,13 @@ int lua::LuaState::createEnvironment() {
|
||||
|
||||
// setmetatable(env, {__index=_G})
|
||||
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_setmetatable(L, -2);
|
||||
|
||||
|
||||
@ -51,7 +51,7 @@ namespace lua {
|
||||
bool rename(const std::string& from, const std::string& to);
|
||||
void remove(const std::string& name);;
|
||||
void createFuncs();
|
||||
int createEnvironment();
|
||||
int createEnvironment(int parent);
|
||||
void removeEnvironment(int id);
|
||||
const std::string storeAnonymous();
|
||||
};
|
||||
|
||||
@ -91,8 +91,8 @@ wstringconsumer scripting::create_wstring_consumer(
|
||||
};
|
||||
}
|
||||
|
||||
std::unique_ptr<Environment> scripting::create_environment() {
|
||||
return std::make_unique<Environment>(state->createEnvironment());
|
||||
std::unique_ptr<Environment> scripting::create_environment(int parent) {
|
||||
return std::make_unique<Environment>(state->createEnvironment(parent));
|
||||
}
|
||||
|
||||
void scripting::on_world_load(Level* level, BlocksController* blocks) {
|
||||
|
||||
@ -51,7 +51,7 @@ namespace scripting {
|
||||
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_save();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user