uinode:add method fix + gui.reindex(document_name)

This commit is contained in:
MihailRis 2024-04-03 18:59:58 +03:00
parent e456b77143
commit e57b0eca15
9 changed files with 81 additions and 44 deletions

View File

@ -1,23 +1,6 @@
<panel size='400' color='0' interval='1'>
<label id='l_master'>-</label>
<trackbar min='0' max='1' value='1' step='0.01' track-width='5'
consumer='on_master_change'
supplier='core.get_setting("audio.volume-master")'/>
<label id='l_regular'>-</label>
<trackbar min='0' max='1' value='1' step='0.01' track-width='5'
consumer='on_regular_change'
supplier='core.get_setting("audio.volume-regular")'/>
<label id='l_ui'>-</label>
<trackbar min='0' max='1' value='1' step='0.01' track-width='5'
consumer='on_ui_change'
supplier='core.get_setting("audio.volume-ui")'/>
<label id='l_ambient'>-</label>
<trackbar min='0' max='1' value='1' step='0.01' track-width='5'
consumer='on_ambient_change'
supplier='core.get_setting("audio.volume-ambient")'/>
<label id='l_music'>-</label>
<trackbar min='0' max='1' value='1' step='0.01' track-width='5'
consumer='on_music_change'
supplier='core.get_setting("audio.volume-music")'/>
<panel id='tracks_panel' size='400' color='0' interval='1'>
<!-- content is generated in script -->
</panel>
<button padding='10' onclick='menu:back()'>@Back</button>
</panel>

View File

@ -1,4 +1,12 @@
function on_open()
new_volume_control("audio.volume-master", "master")
new_volume_control("audio.volume-regular", "regular")
new_volume_control("audio.volume-ui", "ui")
new_volume_control("audio.volume-ambient", "ambient")
new_volume_control("audio.volume-music", "music")
gui.reindex("core:pages/settings-audio")
on_master_change()
on_regular_change()
on_ui_change()
@ -6,32 +14,45 @@ function on_open()
on_music_change()
end
function on_volume_change(setting, label, name, val)
function new_volume_control(setting, id, name)
-- value text label
document.tracks_panel:add("<label id='l_"..id.."'>-</label>")
-- value track-bar
document.tracks_panel:add(string.format(
"<trackbar id='t_%s' min='0' max='1' value='1' step='0.01' track-width='5' "..
" consumer='on_%s_change'/>"
, id, id))
end
function on_volume_change(setting, id, name, val)
if val ~= nil then
core.set_setting(setting, val)
else
document["t_"..id].value = core.get_setting(setting, val)
end
label.text = (
document["l_"..id].text = (
gui.str(name, "settings")..": "..
core.str_setting(setting)
)
end
function on_master_change(val)
on_volume_change("audio.volume-master", document.l_master, "Master Volume", val)
on_volume_change("audio.volume-master", "master", "Master Volume", val)
end
function on_regular_change(val)
on_volume_change("audio.volume-regular", document.l_regular, "Regular Sounds", val)
on_volume_change("audio.volume-regular", "regular", "Regular Sounds", val)
end
function on_ui_change(val)
on_volume_change("audio.volume-ui", document.l_ui, "UI Sounds", val)
on_volume_change("audio.volume-ui", "ui", "UI Sounds", val)
end
function on_ambient_change(val)
on_volume_change("audio.volume-ambient", document.l_ambient, "Ambient", val)
on_volume_change("audio.volume-ambient", "ambient", "Ambient", val)
end
function on_music_change(val)
on_volume_change("audio.volume-music", document.l_music, "Music", val)
on_volume_change("audio.volume-music", "music", "Music", val)
end

View File

@ -13,9 +13,12 @@ UiDocument::UiDocument(
std::shared_ptr<gui::UINode> root,
std::unique_ptr<scripting::Environment> env
) : id(id), script(script), root(root), env(std::move(env)) {
collect(map, root);
buildIndices(map, root);
}
void UiDocument::rebuildIndices() {
buildIndices(map, root);
}
const uinodes_map& UiDocument::getMap() const {
return map;
@ -45,7 +48,7 @@ int UiDocument::getEnvironment() const {
return env->getId();
}
void UiDocument::collect(uinodes_map& map, std::shared_ptr<gui::UINode> node) {
void UiDocument::buildIndices(uinodes_map& map, std::shared_ptr<gui::UINode> node) {
const std::string& id = node->getId();
if (!id.empty()) {
map[id] = node;
@ -53,7 +56,7 @@ void UiDocument::collect(uinodes_map& map, std::shared_ptr<gui::UINode> node) {
auto container = std::dynamic_pointer_cast<gui::Container>(node);
if (container) {
for (auto subnode : container->getNodes()) {
collect(map, subnode);
buildIndices(map, subnode);
}
}
}

View File

@ -38,14 +38,16 @@ public:
std::unique_ptr<scripting::Environment> env
);
void rebuildIndices();
const std::string& getId() const;
const uinodes_map& getMap() const;
const std::shared_ptr<gui::UINode> getRoot() const;
const std::shared_ptr<gui::UINode> get(const std::string& id) const;
const uidocscript& getScript() const;
int getEnvironment() const;
/* Collect map of all uinodes having identifiers */
static void collect(uinodes_map& map, std::shared_ptr<gui::UINode> node);
// @brief Collect map of all uinodes having identifiers
static void buildIndices(uinodes_map& map, std::shared_ptr<gui::UINode> node);
static std::unique_ptr<UiDocument> read(int env, std::string name, fs::path file);
static std::shared_ptr<gui::UINode> readElement(fs::path file);

View File

@ -31,10 +31,12 @@ std::shared_ptr<Button> guiutil::gotoButton(
));
}
std::shared_ptr<gui::UINode> guiutil::create(const std::string& source) {
scripting::Environment env(0);
std::shared_ptr<gui::UINode> guiutil::create(const std::string& source, int envid) {
scripting::Environment env(envid);
UiXmlReader reader(env);
return reader.readXML("<string>", source);
auto node = reader.readXML("<string>", source);
env.release();
return node;
}
void guiutil::alert(GUI* gui, const std::wstring& text, runnable on_hidden) {

View File

@ -23,7 +23,7 @@ namespace guiutil {
/// @brief Create element from XML
/// @param source XML
std::shared_ptr<gui::UINode> create(const std::string& source);
std::shared_ptr<gui::UINode> create(const std::string& source, int env=0);
void alert(
gui::GUI* gui,

View File

@ -15,7 +15,12 @@
#include "../../../frontend/locale/langs.h"
#include "../../../util/stringutil.h"
static gui::UINode* getDocumentNode(lua_State* L, const std::string& name, const std::string& nodeName) {
struct DocumentNode {
UiDocument* document;
gui::UINode* node;
};
static DocumentNode getDocumentNode(lua_State* L, const std::string& name, const std::string& nodeName) {
auto doc = scripting::engine->getAssets()->getLayout(name);
if (doc == nullptr) {
luaL_error(L, "document '%s' not found", name.c_str());
@ -24,7 +29,7 @@ static gui::UINode* getDocumentNode(lua_State* L, const std::string& name, const
if (node == nullptr) {
luaL_error(L, "document '%s' has no element with id '%s'", name.c_str(), nodeName.c_str());
}
return node.get();
return {doc, node.get()};
}
static bool getattr(lua_State* L, gui::TrackBar* bar, const std::string& attr) {
@ -119,7 +124,7 @@ static bool getattr(lua_State* L, gui::TextBox* box, const std::string& attr) {
return false;
}
static gui::UINode* getDocumentNode(lua_State* L) {
static DocumentNode getDocumentNode(lua_State* L) {
lua_getfield(L, 1, "docname");
lua_getfield(L, 1, "name");
auto docname = lua_tostring(L, -2);
@ -131,7 +136,7 @@ static gui::UINode* getDocumentNode(lua_State* L) {
static int menu_back(lua_State* L) {
auto node = getDocumentNode(L);
auto menu = dynamic_cast<gui::Menu*>(node);
auto menu = dynamic_cast<gui::Menu*>(node.node);
menu->back();
return 0;
}
@ -210,10 +215,11 @@ static bool setattr(lua_State* L, gui::Menu* menu, const std::string& attr) {
}
static int container_add(lua_State* L) {
auto node = dynamic_cast<gui::Container*>(getDocumentNode(L));
auto docnode = getDocumentNode(L);
auto node = dynamic_cast<gui::Container*>(docnode.node);
auto xmlsrc = lua_tostring(L, 2);
try {
node->add(guiutil::create(xmlsrc));
node->add(guiutil::create(xmlsrc, docnode.document->getEnvironment()));
} catch (const std::exception& err) {
luaL_error(L, err.what());
}
@ -235,7 +241,8 @@ static int l_gui_getattr(lua_State* L) {
auto docname = lua_tostring(L, 1);
auto element = lua_tostring(L, 2);
const std::string attr = lua_tostring(L, 3);
auto node = getDocumentNode(L, docname, element);
auto docnode = getDocumentNode(L, docname, element);
auto node = docnode.node;
if (attr == "color") {
return lua::pushcolor_arr(L, node->getColor());
@ -281,7 +288,8 @@ static int l_gui_setattr(lua_State* L) {
auto element = lua_tostring(L, 2);
const std::string attr = lua_tostring(L, 3);
auto node = getDocumentNode(L, docname, element);
auto docnode = getDocumentNode(L, docname, element);
auto node = docnode.node;
if (attr == "pos") {
node->setPos(lua::tovec2(L, 4));
} else if (attr == "size") {
@ -332,11 +340,22 @@ static int l_gui_str(lua_State* L) {
return 1;
}
static int l_gui_reindex(lua_State* L) {
auto name = lua_tostring(L, 1);
auto doc = scripting::engine->getAssets()->getLayout(name);
if (doc == nullptr) {
luaL_error(L, "document '%s' not found", name);
}
doc->rebuildIndices();
return 0;
}
const luaL_Reg guilib [] = {
{"get_viewport", lua_wrap_errors<l_gui_getviewport>},
{"getattr", lua_wrap_errors<l_gui_getattr>},
{"setattr", lua_wrap_errors<l_gui_setattr>},
{"get_env", lua_wrap_errors<l_gui_get_env>},
{"str", lua_wrap_errors<l_gui_str>},
{"reindex", lua_wrap_errors<l_gui_reindex>},
{NULL, NULL}
};

View File

@ -47,6 +47,10 @@ int Environment::getId() const {
return env;
}
void Environment::release() {
env = 0;
}
void load_script(fs::path name) {
auto paths = scripting::engine->getPaths();
fs::path file = paths->getResources()/fs::path("scripts")/name;

View File

@ -41,6 +41,9 @@ namespace scripting {
~Environment();
int getId() const;
// @brief Release namespace control
void release();
};
void initialize(Engine* engine);