ui scripting related fixes

This commit is contained in:
MihailRis 2024-02-13 14:36:10 +03:00
parent 7f17face73
commit 302b649634
8 changed files with 21 additions and 9 deletions

View File

@ -6,6 +6,7 @@
using runnable = std::function<void()>;
using stringconsumer = std::function<void(const std::string&)>;
using wstringsupplier = std::function<std::wstring()>;
using wstringconsumer = std::function<void(const std::wstring&)>;
#endif // DELEGATES_H_

View File

@ -55,7 +55,7 @@ std::unique_ptr<UiDocument> UiDocument::read(AssetsLoader& loader, int penv, std
const std::string text = files::read_string(file);
auto xmldoc = xml::parse(file.u8string(), text);
auto env = scripting::create_environment(penv);
auto env = scripting::create_doc_environment(penv, namesp);
gui::UiXmlReader reader(*env, loader);
InventoryView::createReaders(reader);
auto view = reader.readXML(

View File

@ -17,9 +17,6 @@ class Batch2D;
class Assets;
namespace gui {
using wstringsupplier = std::function<std::wstring()>;
using wstringconsumer = std::function<void(std::wstring)>;
using doublesupplier = std::function<double()>;
using doubleconsumer = std::function<void(double)>;

View File

@ -133,7 +133,7 @@ static std::shared_ptr<UINode> readButton(UiXmlReader& reader, xml::xmlelement e
auto callback = scripting::create_runnable(
reader.getEnvironment().getId(),
element->attr("onclick").getText(),
"<onclick>"
reader.getFilename()+".lua"
);
button->listenAction([callback](GUI*) {
callback();
@ -156,7 +156,7 @@ static std::shared_ptr<UINode> readTextBox(UiXmlReader& reader, xml::xmlelement
auto consumer = scripting::create_wstring_consumer(
reader.getEnvironment().getId(),
element->attr("consumer").getText(),
reader.getFilename()
reader.getFilename()+"lua"
);
textbox->textConsumer(consumer);
}

View File

@ -43,6 +43,7 @@
#include "LevelFrontend.h"
#include "UiDocument.h"
#include "../engine.h"
#include "../delegates.h"
#include "../core_defs.h"
#include "../items/ItemDef.h"
#include "../items/Inventory.h"
@ -53,7 +54,7 @@ using glm::vec3;
using glm::vec4;
using namespace gui;
static std::shared_ptr<Label> create_label(gui::wstringsupplier supplier) {
static std::shared_ptr<Label> create_label(wstringsupplier supplier) {
auto label = std::make_shared<Label>(L"-");
label->textSupplier(supplier);
return label;

View File

@ -24,6 +24,7 @@
#include "../window/Window.h"
#include "../engine.h"
#include "../settings.h"
#include "../delegates.h"
#include "../content/Content.h"
#include "../content/ContentLUT.h"
#include "../content/ContentPack.h"
@ -46,7 +47,7 @@ inline uint64_t randU64() {
((uint64_t)rand() << 56);
}
static std::shared_ptr<Label> create_label(gui::wstringsupplier supplier) {
static std::shared_ptr<Label> create_label(wstringsupplier supplier) {
auto label = std::make_shared<Label>(L"-");
label->textSupplier(supplier);
return label;

View File

@ -74,7 +74,6 @@ wstringconsumer scripting::create_wstring_consumer(
const std::string& src,
const std::string& file
) {
auto funcName = state->storeAnonymous();
return [=](const std::wstring& x){
try {
if (state->eval(env, src, file) == 0)
@ -119,6 +118,18 @@ std::unique_ptr<Environment> scripting::create_pack_environment(const ContentPac
state->setfield("PACK_ENV");
state->pushstring(pack.id);
state->setfield("PACK_ID");
state->pop();
return std::make_unique<Environment>(id);
}
std::unique_ptr<Environment> scripting::create_doc_environment(int parent, const std::string& name) {
int id = state->createEnvironment(parent);
state->pushenv(id);
state->pushvalue(-1);
state->setfield("DOC_ENV");
state->pushstring(name.c_str());
state->setfield("DOC_NAME");
state->pop();
return std::make_unique<Environment>(id);
}

View File

@ -63,6 +63,7 @@ namespace scripting {
std::unique_ptr<Environment> create_environment(int parent=0);
std::unique_ptr<Environment> create_pack_environment(const ContentPack& pack);
std::unique_ptr<Environment> create_doc_environment(int parent, const std::string& name);
void on_world_load(Level* level, BlocksController* blocks);
void on_world_save();