diff --git a/.idea/.gitignore b/.idea/.gitignore index c0f9e196..ece251b3 100644 --- a/.idea/.gitignore +++ b/.idea/.gitignore @@ -1,5 +1,6 @@ # Default ignored files /workspace.xml +/shelf/ # Datasource local storage ignored files /dataSources.local.xml \ No newline at end of file diff --git a/src/game/hud/GuiBuilder.cpp b/src/game/hud/GuiBuilder.cpp index 389074ab..f4cabe79 100644 --- a/src/game/hud/GuiBuilder.cpp +++ b/src/game/hud/GuiBuilder.cpp @@ -13,8 +13,8 @@ GuiBuilder::GuiBuilder(TextureAtlas& textures, ModelStore& models, std::shared_ptr root) : textures(textures), models(models), root(root) {} -void GuiBuilder::setGuiRoot(LuaGuiElement& menu) { - elements = &menu; +void GuiBuilder::setGuiRoot(std::shared_ptr menu) { + elements = menu; } void GuiBuilder::build(glm::ivec2 winBounds) { @@ -34,17 +34,12 @@ void GuiBuilder::create(LuaGuiElement& element, std::shared_ptr pa if (!component) throw std::runtime_error("GuiBuilder failed to create component: " + element.key); parent->add(component); - for (auto& child : element.children) create(child, component, component->getScale()); + for (auto& child : element.children) create(*child, component, component->getScale()); } std::shared_ptr GuiBuilder::createComponent(LuaGuiElement& elem, glm::ivec2 bounds) { std::shared_ptr c = nullptr; - if (elem.key == "wee") { - auto a = elem.getAsAny("position"); - std::cout << a.get().x << ", " << a.get().y << std::endl; - } - switch (Util::hash(elem.type.c_str())) { default: break; case Util::hash("Body"): { @@ -92,7 +87,6 @@ void GuiBuilder::clearCallbacks(std::shared_ptr component) { } void GuiBuilder::elementUpdated() { - std::cout << "rebuilding ui " << std::endl; build(); } diff --git a/src/game/hud/GuiBuilder.h b/src/game/hud/GuiBuilder.h index 73f0eabb..0c064e83 100644 --- a/src/game/hud/GuiBuilder.h +++ b/src/game/hud/GuiBuilder.h @@ -16,7 +16,7 @@ public: struct ComponentCallbacks { GuiComponent::callback left {}, right {}, hover {}; }; GuiBuilder(TextureAtlas& textures, ModelStore& models, std::shared_ptr root); - void setGuiRoot(LuaGuiElement& menu); + void setGuiRoot(std::shared_ptr menu); void build(glm::ivec2 winBounds = {}); void clear(bool deleteRoot = true); @@ -33,7 +33,7 @@ protected: std::shared_ptr root = nullptr; - LuaGuiElement* elements = nullptr; + std::shared_ptr elements = nullptr; unsigned int keyInd = 0; glm::ivec2 winBounds {}; diff --git a/src/lua/api/class/LuaGuiElement.cpp b/src/lua/api/class/LuaGuiElement.cpp index 63528521..58ec8342 100644 --- a/src/lua/api/class/LuaGuiElement.cpp +++ b/src/lua/api/class/LuaGuiElement.cpp @@ -8,16 +8,31 @@ #include "../../../game/hud/SerialGui.h" -LuaGuiElement::LuaGuiElement(const std::string& type, sol::table data) : - type(type) { +//LuaGuiElement::LuaGuiElement(const std::string& type, sol::table data) : +// type(type) { +// +// for (const auto& pair : data) { +// if (pair.first.is()) { +// if (!pair.second.is>()) throw std::runtime_error("Child is not a GuiElement."); +// children.push_back(pair.second.as>()); +// } +// else if (pair.first.is()) set_trait(pair.first.as(), pair.second); +// } +//} + +std::shared_ptr LuaGuiElement::create(const std::string& type, sol::table data) { + auto elem = std::make_shared(); + elem->type = type; for (const auto& pair : data) { if (pair.first.is()) { - if (!pair.second.is()) throw std::runtime_error("Child is not a GuiElement."); - children.push_back(pair.second.as()); + if (!pair.second.is>()) throw std::runtime_error("Child is not a GuiElement."); + elem->children.push_back(pair.second.as>()); } - else if (pair.first.is()) set_trait(pair.first.as(), pair.second); + else if (pair.first.is()) elem->set_trait(pair.first.as(), pair.second); } + + return elem; } sol::object LuaGuiElement::get_trait(sol::this_state s, const std::string& key) { @@ -37,15 +52,8 @@ sol::object LuaGuiElement::set_trait(const std::string& key, sol::object val) { this->key = val.as(); } else { - if (val.is()) { - std::cout << key << "t: " << val.as().get(1) << ", " << val.as().get(2) << std::endl; - } traits.erase(key); traits.emplace(key, val); - if (val.is()) { - sol::table v = traits.at(key).as(); - std::cout << key << "tv: " << v.get(1) << ", " << v.get(2) << std::endl; - } } if (updateFunction) updateFunction(); @@ -60,34 +68,42 @@ sol::object LuaGuiElement::call(sol::this_state s, sol::function fun) { return fun(this); } -sol::object LuaGuiElement::find(sol::this_state s, const std::string& key) { - for (auto& child : children) { - if (child.key == key) return sol::make_object(s, child); +sol::object LuaGuiElement::get_child(sol::this_state s, sol::object key) { + if (key.is() && key.as() <= children.size()) { + auto begin = children.begin(); + std::advance(begin, key.as() - 1); + return sol::make_object>(s, *begin); + } + else if (key.is()) { + for (auto &child : children) { + if (child->key == key.as()) return sol::make_object>(s, child); + } + + for (auto &child : children) { + auto recurse = child->get_child(s, key); + if (recurse) return recurse; + } } - for (auto& child : children) { - auto recurse = child.find(s, key); - if (recurse) return recurse; - } return sol::nil; } void LuaGuiElement::append(sol::this_state s, sol::object elem) { - if (elem.is()) children.push_back(elem.as()); - else if (elem.is()) children.push_back(call(s, elem.as()).as()); + if (elem.is()) children.push_back(elem.as>()); + else if (elem.is()) children.push_back(call(s, elem.as()).as>()); else throw std::runtime_error("Append arg is not an element or a function to generate one."); if (updateFunction) updateFunction(); - children.back().updateFunction = updateFunction; + children.back()->updateFunction = updateFunction; } void LuaGuiElement::prepend(sol::this_state s, sol::object elem) { - if (elem.is()) children.insert(children.begin(), elem.as()); - else if (elem.is()) children.insert(children.begin(), call(s, elem.as()).as()); + if (elem.is()) children.insert(children.begin(), elem.as>()); + else if (elem.is()) children.insert(children.begin(), call(s, elem.as()).as>()); else throw std::runtime_error("Append arg is not an element or a function to generate one."); if (updateFunction) updateFunction(); - children.front().updateFunction = updateFunction; + children.front()->updateFunction = updateFunction; } void LuaGuiElement::remove(sol::optional elem) { @@ -97,7 +113,7 @@ void LuaGuiElement::remove(sol::optional elem) { } else { for (const auto it = children.cbegin(); it != children.cend();) { - if (it->key == elem->key) { + if ((*it)->key == elem->key) { children.erase(it); if (updateFunction) updateFunction(); return; diff --git a/src/lua/api/class/LuaGuiElement.h b/src/lua/api/class/LuaGuiElement.h index 78eeba75..59bd9384 100644 --- a/src/lua/api/class/LuaGuiElement.h +++ b/src/lua/api/class/LuaGuiElement.h @@ -11,14 +11,16 @@ class LuaGuiElement { public: - LuaGuiElement(const std::string& type, sol::table data); + LuaGuiElement() = default; // Lua Functions and Properties + static std::shared_ptr create(const std::string& type, sol::table data); + sol::object get_trait(sol::this_state s, const std::string& key); sol::object set_trait(const std::string& key, sol::object val); sol::object call(sol::this_state s, sol::function fun); - sol::object find(sol::this_state s, const std::string& key); + sol::object get_child(sol::this_state s, sol::object key); void append(sol::this_state s, sol::object elem); void prepend(sol::this_state s, sol::object elem); @@ -27,7 +29,7 @@ public: std::string type {}, key {}; LuaGuiElement* parent = nullptr; - std::list children {}; + std::list> children {}; std::unordered_map callbacks {}; std::unordered_map traits {}; @@ -54,14 +56,14 @@ public: namespace ClientApi { static void gui_element(sol::state& lua) { lua.new_usertype("GuiElement", - sol::constructors(), + sol::meta_function::construct, sol::factories(&LuaGuiElement::create), sol::meta_function::index, &LuaGuiElement::get_trait, sol::meta_function::new_index, &LuaGuiElement::set_trait, sol::meta_function::call, &LuaGuiElement::call, - "find", &LuaGuiElement::find, + "get", &LuaGuiElement::get_child, "append", &LuaGuiElement::append, "prepend", &LuaGuiElement::prepend, "remove", &LuaGuiElement::remove diff --git a/src/lua/api/menu/mSetGui.h b/src/lua/api/menu/mSetGui.h index 06b1abbf..498ee749 100644 --- a/src/lua/api/menu/mSetGui.h +++ b/src/lua/api/menu/mSetGui.h @@ -10,7 +10,7 @@ namespace MenuApi { void set_gui(GuiBuilder& builder, glm::ivec2& win, sol::state& lua, sol::table& core) { - core.set_function("set_gui", [&](LuaGuiElement& gui) { + core.set_function("set_gui", [&](std::shared_ptr gui) { builder.setGuiRoot(gui); builder.build(win); }); diff --git a/subgames/minimal/menu/script/main.lua b/subgames/minimal/menu/script/main.lua index b4ca21d1..5d6d0e20 100644 --- a/subgames/minimal/menu/script/main.lua +++ b/subgames/minimal/menu/script/main.lua @@ -5,7 +5,7 @@ local gui = zepha.build_gui(function() Gui.Text { position = { 4, 4 }, - content = "Minimal Subgame" + content = "Minimalminimalmmnal" }, Gui.Rect { @@ -22,25 +22,10 @@ local gui = zepha.build_gui(function() background = "zeus_background", Gui.Text { - content = "What's the fuck is going on?" + content = "What's the fuck it'd going on?" } - }, - - Gui.Rect { - position = { 0, 0 }, - size = { 32, 32 }, - background = "#f00", - key = "wee" } } end) -zepha.set_gui(gui) - -local menu = gui:find("wee") - -zepha.delay(function() - print("updating position") - menu.position = {menu.position[1] + 16, menu.position[2] + 16} - return true -end, 1) \ No newline at end of file +zepha.set_gui(gui) \ No newline at end of file diff --git a/subgames/zeus/menu/textures/zeus_button_extex.png b/subgames/zeus/menu/textures/zeus_button_extex.png new file mode 100644 index 00000000..c585cb0e Binary files /dev/null and b/subgames/zeus/menu/textures/zeus_button_extex.png differ