uinode:add method fix + gui.reindex(document_name)
This commit is contained in:
parent
e456b77143
commit
e57b0eca15
@ -1,23 +1,6 @@
|
|||||||
<panel size='400' color='0' interval='1'>
|
<panel size='400' color='0' interval='1'>
|
||||||
<label id='l_master'>-</label>
|
<panel id='tracks_panel' size='400' color='0' interval='1'>
|
||||||
<trackbar min='0' max='1' value='1' step='0.01' track-width='5'
|
<!-- content is generated in script -->
|
||||||
consumer='on_master_change'
|
</panel>
|
||||||
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")'/>
|
|
||||||
<button padding='10' onclick='menu:back()'>@Back</button>
|
<button padding='10' onclick='menu:back()'>@Back</button>
|
||||||
</panel>
|
</panel>
|
||||||
|
|||||||
@ -1,4 +1,12 @@
|
|||||||
function on_open()
|
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_master_change()
|
||||||
on_regular_change()
|
on_regular_change()
|
||||||
on_ui_change()
|
on_ui_change()
|
||||||
@ -6,32 +14,45 @@ function on_open()
|
|||||||
on_music_change()
|
on_music_change()
|
||||||
end
|
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
|
if val ~= nil then
|
||||||
core.set_setting(setting, val)
|
core.set_setting(setting, val)
|
||||||
|
else
|
||||||
|
document["t_"..id].value = core.get_setting(setting, val)
|
||||||
end
|
end
|
||||||
label.text = (
|
document["l_"..id].text = (
|
||||||
gui.str(name, "settings")..": "..
|
gui.str(name, "settings")..": "..
|
||||||
core.str_setting(setting)
|
core.str_setting(setting)
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
function on_master_change(val)
|
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
|
end
|
||||||
|
|
||||||
function on_regular_change(val)
|
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
|
end
|
||||||
|
|
||||||
function on_ui_change(val)
|
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
|
end
|
||||||
|
|
||||||
function on_ambient_change(val)
|
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
|
end
|
||||||
|
|
||||||
function on_music_change(val)
|
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
|
end
|
||||||
|
|||||||
@ -13,9 +13,12 @@ UiDocument::UiDocument(
|
|||||||
std::shared_ptr<gui::UINode> root,
|
std::shared_ptr<gui::UINode> root,
|
||||||
std::unique_ptr<scripting::Environment> env
|
std::unique_ptr<scripting::Environment> env
|
||||||
) : id(id), script(script), root(root), env(std::move(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 {
|
const uinodes_map& UiDocument::getMap() const {
|
||||||
return map;
|
return map;
|
||||||
@ -45,7 +48,7 @@ int UiDocument::getEnvironment() const {
|
|||||||
return env->getId();
|
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();
|
const std::string& id = node->getId();
|
||||||
if (!id.empty()) {
|
if (!id.empty()) {
|
||||||
map[id] = node;
|
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);
|
auto container = std::dynamic_pointer_cast<gui::Container>(node);
|
||||||
if (container) {
|
if (container) {
|
||||||
for (auto subnode : container->getNodes()) {
|
for (auto subnode : container->getNodes()) {
|
||||||
collect(map, subnode);
|
buildIndices(map, subnode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -38,14 +38,16 @@ public:
|
|||||||
std::unique_ptr<scripting::Environment> env
|
std::unique_ptr<scripting::Environment> env
|
||||||
);
|
);
|
||||||
|
|
||||||
|
void rebuildIndices();
|
||||||
|
|
||||||
const std::string& getId() const;
|
const std::string& getId() const;
|
||||||
const uinodes_map& getMap() const;
|
const uinodes_map& getMap() const;
|
||||||
const std::shared_ptr<gui::UINode> getRoot() const;
|
const std::shared_ptr<gui::UINode> getRoot() const;
|
||||||
const std::shared_ptr<gui::UINode> get(const std::string& id) const;
|
const std::shared_ptr<gui::UINode> get(const std::string& id) const;
|
||||||
const uidocscript& getScript() const;
|
const uidocscript& getScript() const;
|
||||||
int getEnvironment() const;
|
int getEnvironment() const;
|
||||||
/* Collect map of all uinodes having identifiers */
|
// @brief Collect map of all uinodes having identifiers
|
||||||
static void collect(uinodes_map& map, std::shared_ptr<gui::UINode> node);
|
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::unique_ptr<UiDocument> read(int env, std::string name, fs::path file);
|
||||||
static std::shared_ptr<gui::UINode> readElement(fs::path file);
|
static std::shared_ptr<gui::UINode> readElement(fs::path file);
|
||||||
|
|||||||
@ -31,10 +31,12 @@ std::shared_ptr<Button> guiutil::gotoButton(
|
|||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::shared_ptr<gui::UINode> guiutil::create(const std::string& source) {
|
std::shared_ptr<gui::UINode> guiutil::create(const std::string& source, int envid) {
|
||||||
scripting::Environment env(0);
|
scripting::Environment env(envid);
|
||||||
UiXmlReader reader(env);
|
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) {
|
void guiutil::alert(GUI* gui, const std::wstring& text, runnable on_hidden) {
|
||||||
|
|||||||
@ -23,7 +23,7 @@ namespace guiutil {
|
|||||||
|
|
||||||
/// @brief Create element from XML
|
/// @brief Create element from XML
|
||||||
/// @param source 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(
|
void alert(
|
||||||
gui::GUI* gui,
|
gui::GUI* gui,
|
||||||
|
|||||||
@ -15,7 +15,12 @@
|
|||||||
#include "../../../frontend/locale/langs.h"
|
#include "../../../frontend/locale/langs.h"
|
||||||
#include "../../../util/stringutil.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);
|
auto doc = scripting::engine->getAssets()->getLayout(name);
|
||||||
if (doc == nullptr) {
|
if (doc == nullptr) {
|
||||||
luaL_error(L, "document '%s' not found", name.c_str());
|
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) {
|
if (node == nullptr) {
|
||||||
luaL_error(L, "document '%s' has no element with id '%s'", name.c_str(), nodeName.c_str());
|
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) {
|
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;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static gui::UINode* getDocumentNode(lua_State* L) {
|
static DocumentNode getDocumentNode(lua_State* L) {
|
||||||
lua_getfield(L, 1, "docname");
|
lua_getfield(L, 1, "docname");
|
||||||
lua_getfield(L, 1, "name");
|
lua_getfield(L, 1, "name");
|
||||||
auto docname = lua_tostring(L, -2);
|
auto docname = lua_tostring(L, -2);
|
||||||
@ -131,7 +136,7 @@ static gui::UINode* getDocumentNode(lua_State* L) {
|
|||||||
|
|
||||||
static int menu_back(lua_State* L) {
|
static int menu_back(lua_State* L) {
|
||||||
auto node = getDocumentNode(L);
|
auto node = getDocumentNode(L);
|
||||||
auto menu = dynamic_cast<gui::Menu*>(node);
|
auto menu = dynamic_cast<gui::Menu*>(node.node);
|
||||||
menu->back();
|
menu->back();
|
||||||
return 0;
|
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) {
|
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);
|
auto xmlsrc = lua_tostring(L, 2);
|
||||||
try {
|
try {
|
||||||
node->add(guiutil::create(xmlsrc));
|
node->add(guiutil::create(xmlsrc, docnode.document->getEnvironment()));
|
||||||
} catch (const std::exception& err) {
|
} catch (const std::exception& err) {
|
||||||
luaL_error(L, err.what());
|
luaL_error(L, err.what());
|
||||||
}
|
}
|
||||||
@ -235,7 +241,8 @@ static int l_gui_getattr(lua_State* L) {
|
|||||||
auto docname = lua_tostring(L, 1);
|
auto docname = lua_tostring(L, 1);
|
||||||
auto element = lua_tostring(L, 2);
|
auto element = lua_tostring(L, 2);
|
||||||
const std::string attr = lua_tostring(L, 3);
|
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") {
|
if (attr == "color") {
|
||||||
return lua::pushcolor_arr(L, node->getColor());
|
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);
|
auto element = lua_tostring(L, 2);
|
||||||
const std::string attr = lua_tostring(L, 3);
|
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") {
|
if (attr == "pos") {
|
||||||
node->setPos(lua::tovec2(L, 4));
|
node->setPos(lua::tovec2(L, 4));
|
||||||
} else if (attr == "size") {
|
} else if (attr == "size") {
|
||||||
@ -332,11 +340,22 @@ static int l_gui_str(lua_State* L) {
|
|||||||
return 1;
|
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 [] = {
|
const luaL_Reg guilib [] = {
|
||||||
{"get_viewport", lua_wrap_errors<l_gui_getviewport>},
|
{"get_viewport", lua_wrap_errors<l_gui_getviewport>},
|
||||||
{"getattr", lua_wrap_errors<l_gui_getattr>},
|
{"getattr", lua_wrap_errors<l_gui_getattr>},
|
||||||
{"setattr", lua_wrap_errors<l_gui_setattr>},
|
{"setattr", lua_wrap_errors<l_gui_setattr>},
|
||||||
{"get_env", lua_wrap_errors<l_gui_get_env>},
|
{"get_env", lua_wrap_errors<l_gui_get_env>},
|
||||||
{"str", lua_wrap_errors<l_gui_str>},
|
{"str", lua_wrap_errors<l_gui_str>},
|
||||||
|
{"reindex", lua_wrap_errors<l_gui_reindex>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -47,6 +47,10 @@ int Environment::getId() const {
|
|||||||
return env;
|
return env;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Environment::release() {
|
||||||
|
env = 0;
|
||||||
|
}
|
||||||
|
|
||||||
void load_script(fs::path name) {
|
void load_script(fs::path name) {
|
||||||
auto paths = scripting::engine->getPaths();
|
auto paths = scripting::engine->getPaths();
|
||||||
fs::path file = paths->getResources()/fs::path("scripts")/name;
|
fs::path file = paths->getResources()/fs::path("scripts")/name;
|
||||||
|
|||||||
@ -41,6 +41,9 @@ namespace scripting {
|
|||||||
~Environment();
|
~Environment();
|
||||||
|
|
||||||
int getId() const;
|
int getId() const;
|
||||||
|
|
||||||
|
// @brief Release namespace control
|
||||||
|
void release();
|
||||||
};
|
};
|
||||||
|
|
||||||
void initialize(Engine* engine);
|
void initialize(Engine* engine);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user