Do something, not working
parent
49d9ad73ae
commit
fdd5ae307f
|
@ -8,7 +8,7 @@
|
|||
|
||||
#include "components/compound/GuiInventoryList.h"
|
||||
|
||||
std::shared_ptr<GuiComponent> GameGuiBuilder::createComponent(const LuaGuiElement& elem, glm::ivec2 bounds) {
|
||||
std::shared_ptr<GuiComponent> GameGuiBuilder::createComponent(LuaGuiElement& elem, glm::ivec2 bounds) {
|
||||
auto c = GuiBuilder::createComponent(elem, bounds);
|
||||
if (c != nullptr) return c;
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ public:
|
|||
GameGuiBuilder(LocalInventoryRefs& refs, ClientGame& defs, std::shared_ptr<GuiContainer> root) :
|
||||
defs(defs), refs(refs), GuiBuilder(defs.textures, defs.models, root) {};
|
||||
|
||||
std::shared_ptr<GuiComponent> createComponent(const LuaGuiElement& elem, glm::ivec2 bounds) override;
|
||||
std::shared_ptr<GuiComponent> createComponent(LuaGuiElement& elem, glm::ivec2 bounds) override;
|
||||
private:
|
||||
LocalInventoryRefs& refs;
|
||||
ClientGame& defs;
|
||||
|
|
|
@ -11,44 +11,40 @@
|
|||
#include "components/compound/GuiImageButton.h"
|
||||
|
||||
GuiBuilder::GuiBuilder(TextureAtlas& textures, ModelStore& models, std::shared_ptr<GuiContainer> root) :
|
||||
textures(textures), models(models), root(root) {}
|
||||
textures(textures), models(models), root(root) {}
|
||||
|
||||
void GuiBuilder::setGuiRoot(sol::state_view state, LuaGuiElement& menu) {
|
||||
void GuiBuilder::setGuiRoot(LuaGuiElement& menu) {
|
||||
elements = &menu;
|
||||
}
|
||||
|
||||
void GuiBuilder::build(glm::ivec2 winBounds) {
|
||||
clear(false);
|
||||
if (elements) recursivelyCreate(*elements, root, winBounds);
|
||||
if (winBounds != glm::ivec2 {}) this->winBounds = winBounds;
|
||||
if (elements) create(*elements, root, this->winBounds);
|
||||
}
|
||||
|
||||
void GuiBuilder::clear(bool deleteRoot) {
|
||||
recursivelyClearCallbacks(root);
|
||||
clearCallbacks(root);
|
||||
root->empty();
|
||||
if (deleteRoot) elements = nullptr;
|
||||
}
|
||||
|
||||
void GuiBuilder::recursivelyCreate(const LuaGuiElement& element, std::shared_ptr<GuiComponent> parent, glm::ivec2 bounds) {
|
||||
void GuiBuilder::create(LuaGuiElement& element, std::shared_ptr<GuiComponent> parent, glm::ivec2 bounds) {
|
||||
auto component = createComponent(element, bounds);
|
||||
if (!component) throw std::runtime_error("GuiBuilder failed to create component: " + element.key);
|
||||
parent->add(component);
|
||||
|
||||
for (auto& child : element.children) recursivelyCreate(child, component, component->getScale());
|
||||
for (auto& child : element.children) create(child, component, component->getScale());
|
||||
}
|
||||
|
||||
void GuiBuilder::recursivelyClearCallbacks(std::shared_ptr<GuiComponent> component) {
|
||||
component->setCallback(GuiComponent::CallbackType::PRIMARY, nullptr);
|
||||
component->setCallback(GuiComponent::CallbackType::SECONDARY, nullptr);
|
||||
component->setCallback(GuiComponent::CallbackType::HOVER, nullptr);
|
||||
|
||||
for (auto& child : component->getChildren()) {
|
||||
recursivelyClearCallbacks(child);
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<GuiComponent> GuiBuilder::createComponent(const LuaGuiElement& elem, glm::ivec2 bounds) {
|
||||
std::shared_ptr<GuiComponent> GuiBuilder::createComponent(LuaGuiElement& elem, glm::ivec2 bounds) {
|
||||
std::shared_ptr<GuiComponent> c = nullptr;
|
||||
|
||||
if (elem.key == "wee") {
|
||||
auto a = elem.getAsAny("position");
|
||||
std::cout << a.get<glm::vec2>().x << ", " << a.get<glm::vec2>().y << std::endl;
|
||||
}
|
||||
|
||||
switch (Util::hash(elem.type.c_str())) {
|
||||
default: break;
|
||||
case Util::hash("Body"): {
|
||||
|
@ -73,6 +69,8 @@ std::shared_ptr<GuiComponent> GuiBuilder::createComponent(const LuaGuiElement& e
|
|||
|
||||
if (!c) return nullptr;
|
||||
|
||||
elem.updateFunction = std::bind(&GuiBuilder::elementUpdated, this);
|
||||
|
||||
if (elem.callbacks.count("primary")) c->setCallback(GuiComponent::CallbackType::PRIMARY, [=](bool b, glm::vec2 v) {
|
||||
elem.callbacks.at("primary")(b, LuaParser::luaVec(elem.callbacks.at("primary").lua_state(), {v.x, v.y, 0})); });
|
||||
|
||||
|
@ -85,6 +83,19 @@ std::shared_ptr<GuiComponent> GuiBuilder::createComponent(const LuaGuiElement& e
|
|||
return c;
|
||||
}
|
||||
|
||||
void GuiBuilder::clearCallbacks(std::shared_ptr<GuiComponent> component) {
|
||||
component->setCallback(GuiComponent::CallbackType::PRIMARY, nullptr);
|
||||
component->setCallback(GuiComponent::CallbackType::SECONDARY, nullptr);
|
||||
component->setCallback(GuiComponent::CallbackType::HOVER, nullptr);
|
||||
|
||||
for (auto& child : component->getChildren()) clearCallbacks(child);
|
||||
}
|
||||
|
||||
void GuiBuilder::elementUpdated() {
|
||||
std::cout << "rebuilding ui " << std::endl;
|
||||
build();
|
||||
}
|
||||
|
||||
GuiBuilder::~GuiBuilder() {
|
||||
clear();
|
||||
}
|
||||
}
|
|
@ -16,15 +16,17 @@ public:
|
|||
struct ComponentCallbacks { GuiComponent::callback left {}, right {}, hover {}; };
|
||||
|
||||
GuiBuilder(TextureAtlas& textures, ModelStore& models, std::shared_ptr<GuiContainer> root);
|
||||
void setGuiRoot(sol::state_view state, LuaGuiElement& menu);
|
||||
void build(glm::ivec2 winBounds);
|
||||
void setGuiRoot(LuaGuiElement& menu);
|
||||
void build(glm::ivec2 winBounds = {});
|
||||
void clear(bool deleteRoot = true);
|
||||
|
||||
~GuiBuilder();
|
||||
protected:
|
||||
void recursivelyCreate(const LuaGuiElement& element, std::shared_ptr<GuiComponent> parent, glm::ivec2 bounds);
|
||||
static void recursivelyClearCallbacks(std::shared_ptr<GuiComponent> component);
|
||||
virtual std::shared_ptr<GuiComponent> createComponent(const LuaGuiElement& elem, glm::ivec2 bounds);
|
||||
void create(LuaGuiElement& element, std::shared_ptr<GuiComponent> parent, glm::ivec2 bounds);
|
||||
virtual std::shared_ptr<GuiComponent> createComponent(LuaGuiElement& elem, glm::ivec2 bounds);
|
||||
static void clearCallbacks(std::shared_ptr<GuiComponent> component);
|
||||
|
||||
void elementUpdated();
|
||||
|
||||
TextureAtlas& textures;
|
||||
ModelStore& models;
|
||||
|
@ -33,4 +35,6 @@ protected:
|
|||
|
||||
LuaGuiElement* elements = nullptr;
|
||||
unsigned int keyInd = 0;
|
||||
|
||||
glm::ivec2 winBounds {};
|
||||
};
|
||||
|
|
|
@ -6,7 +6,6 @@
|
|||
|
||||
#include "../../SerialGui.h"
|
||||
#include "../../../../util/Util.h"
|
||||
#include "../../../../def/ClientGame.h"
|
||||
|
||||
GuiRect::GuiRect(const std::string &key) : GuiComponent(key) {}
|
||||
|
||||
|
|
|
@ -24,7 +24,7 @@ sol::object LuaGuiElement::get_trait(sol::this_state s, const std::string& key)
|
|||
if (key == "key") return sol::make_object<std::string>(s, this->key);
|
||||
if (key == "type") return sol::make_object<std::string>(s, this->type);
|
||||
|
||||
if (traits.count(key)) return traits[key];
|
||||
if (traits.count(key)) return traits.at(key);
|
||||
return sol::nil;
|
||||
}
|
||||
|
||||
|
@ -32,12 +32,23 @@ sol::object LuaGuiElement::set_trait(const std::string& key, sol::object val) {
|
|||
if (key == "callbacks") {
|
||||
callbacks.clear();
|
||||
for (auto pair : val.as<sol::table>()) callbacks[pair.first.as<std::string>()] = pair.second.as<sol::function>();
|
||||
return val;
|
||||
}
|
||||
else if (key == "key") {
|
||||
this->key = val.as<std::string>();
|
||||
}
|
||||
else {
|
||||
if (val.is<sol::table>()) {
|
||||
std::cout << key << "t: " << val.as<sol::table>().get<float>(1) << ", " << val.as<sol::table>().get<float>(2) << std::endl;
|
||||
}
|
||||
traits.erase(key);
|
||||
traits.emplace(key, val);
|
||||
if (val.is<sol::table>()) {
|
||||
sol::table v = traits.at(key).as<sol::table>();
|
||||
std::cout << key << "tv: " << v.get<float>(1) << ", " << v.get<float>(2) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
// if ((!traits.count(key) || traits.at(key) != val) && updateFunction) updateFunction(id);
|
||||
|
||||
traits[key] = val;
|
||||
if (updateFunction) updateFunction();
|
||||
return val;
|
||||
}
|
||||
|
||||
|
@ -50,7 +61,10 @@ sol::object LuaGuiElement::call(sol::this_state s, sol::function fun) {
|
|||
}
|
||||
|
||||
sol::object LuaGuiElement::find(sol::this_state s, const std::string& key) {
|
||||
for (auto& child : children) if (child.key == key) return sol::make_object<LuaGuiElement>(s, child);
|
||||
for (auto& child : children) {
|
||||
if (child.key == key) return sol::make_object<LuaGuiElement>(s, child);
|
||||
}
|
||||
|
||||
for (auto& child : children) {
|
||||
auto recurse = child.find(s, key);
|
||||
if (recurse) return recurse;
|
||||
|
@ -63,7 +77,8 @@ void LuaGuiElement::append(sol::this_state s, sol::object elem) {
|
|||
else if (elem.is<sol::function>()) children.push_back(call(s, elem.as<sol::function>()).as<LuaGuiElement>());
|
||||
else throw std::runtime_error("Append arg is not an element or a function to generate one.");
|
||||
|
||||
// if (updateFunction) updateFunction(id);
|
||||
if (updateFunction) updateFunction();
|
||||
children.back().updateFunction = updateFunction;
|
||||
}
|
||||
|
||||
void LuaGuiElement::prepend(sol::this_state s, sol::object elem) {
|
||||
|
@ -71,7 +86,8 @@ void LuaGuiElement::prepend(sol::this_state s, sol::object elem) {
|
|||
else if (elem.is<sol::function>()) children.insert(children.begin(), call(s, elem.as<sol::function>()).as<LuaGuiElement>());
|
||||
else throw std::runtime_error("Append arg is not an element or a function to generate one.");
|
||||
|
||||
// if (updateFunction) updateFunction(id);
|
||||
if (updateFunction) updateFunction();
|
||||
children.front().updateFunction = updateFunction;
|
||||
}
|
||||
|
||||
void LuaGuiElement::remove(sol::optional<LuaGuiElement> elem) {
|
||||
|
@ -83,7 +99,7 @@ void LuaGuiElement::remove(sol::optional<LuaGuiElement> elem) {
|
|||
for (const auto it = children.cbegin(); it != children.cend();) {
|
||||
if (it->key == elem->key) {
|
||||
children.erase(it);
|
||||
// if (updateFunction) updateFunction(id);
|
||||
if (updateFunction) updateFunction();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -47,6 +47,8 @@ public:
|
|||
Any a = getAsAny(key);
|
||||
return !a.empty() && a.is<T>();
|
||||
}
|
||||
|
||||
std::function<void()> updateFunction = nullptr;
|
||||
};
|
||||
|
||||
namespace ClientApi {
|
||||
|
|
|
@ -10,8 +10,8 @@
|
|||
|
||||
namespace MenuApi {
|
||||
void set_gui(GuiBuilder& builder, glm::ivec2& win, sol::state& lua, sol::table& core) {
|
||||
core.set_function("set_gui", [&](sol::this_state s, LuaGuiElement& gui) {
|
||||
builder.setGuiRoot(sol::state_view(s), gui);
|
||||
core.set_function("set_gui", [&](LuaGuiElement& gui) {
|
||||
builder.setGuiRoot(gui);
|
||||
builder.build(win);
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
local menu = zepha.build_gui(function()
|
||||
local gui = zepha.build_gui(function()
|
||||
return Gui.Body {
|
||||
-- background = "#214a21",
|
||||
background = "#334",
|
||||
|
@ -9,10 +9,10 @@ local menu = zepha.build_gui(function()
|
|||
},
|
||||
|
||||
Gui.Rect {
|
||||
position = { 64, 64 },
|
||||
size = { 128 * (16/9), 128 },
|
||||
position = { 64, 64 },
|
||||
size = { 128 * (16/9), 128 },
|
||||
|
||||
background = "zeus_background"
|
||||
background = "zeus_background"
|
||||
},
|
||||
|
||||
Gui.Rect {
|
||||
|
@ -24,34 +24,23 @@ local menu = zepha.build_gui(function()
|
|||
Gui.Text {
|
||||
content = "What's the fuck is going on?"
|
||||
}
|
||||
},
|
||||
|
||||
Gui.Rect {
|
||||
position = { 0, 0 },
|
||||
size = { 32, 32 },
|
||||
background = "#f00",
|
||||
key = "wee"
|
||||
}
|
||||
}
|
||||
end)
|
||||
|
||||
menu(function(e)
|
||||
e:append(Gui.Text {
|
||||
position = { 300, 16 },
|
||||
color = "#0fc",
|
||||
content = "Magic phones"
|
||||
})
|
||||
end)
|
||||
zepha.set_gui(gui)
|
||||
|
||||
menu:append(function()
|
||||
return Gui.Text {
|
||||
position = { 300, 32 },
|
||||
color = "#f39",
|
||||
content = "AAAAAA"
|
||||
}
|
||||
end)
|
||||
|
||||
zepha.set_gui(menu)
|
||||
local menu = gui:find("wee")
|
||||
|
||||
zepha.delay(function()
|
||||
menu(function()
|
||||
menu:append(Gui.Rect {
|
||||
position = {0, 0},
|
||||
size = {32, 32},
|
||||
background = "#f00"
|
||||
})
|
||||
end)
|
||||
end, 5)
|
||||
print("updating position")
|
||||
menu.position = {menu.position[1] + 16, menu.position[2] + 16}
|
||||
return true
|
||||
end, 1)
|
Loading…
Reference in New Issue