From 7792c1a6f40598460fb029d62580a10a308d46ce Mon Sep 17 00:00:00 2001 From: MihailRis Date: Mon, 19 Feb 2024 14:13:06 +0300 Subject: [PATCH] a huge pile of boilerplate code --- src/frontend/gui/controls.cpp | 58 ++++++++++++++++++++++++ src/frontend/gui/controls.h | 18 +++++++- src/frontend/gui/gui_xml.cpp | 17 ++++++- src/logic/scripting/lua/libgui.cpp | 71 ++++++++++++++++++++++++++++++ src/logic/scripting/lua/lua_util.h | 17 +++++++ 5 files changed, 179 insertions(+), 2 deletions(-) diff --git a/src/frontend/gui/controls.cpp b/src/frontend/gui/controls.cpp index 9979b6f1..3e68f392 100644 --- a/src/frontend/gui/controls.cpp +++ b/src/frontend/gui/controls.cpp @@ -150,6 +150,14 @@ std::wstring Button::getText() const { return L""; } +glm::vec4 Button::getPressedColor() const { + return pressedColor; +} + +void Button::setPressedColor(glm::vec4 color) { + pressedColor = color; +} + Button* Button::textSupplier(wstringsupplier supplier) { if (label) { label->textSupplier(supplier); @@ -402,6 +410,7 @@ TrackBar::TrackBar(double min, step(step), trackWidth(trackWidth) { setColor(glm::vec4(0.f, 0.f, 0.f, 0.4f)); + setHoverColor(glm::vec4(0.01f, 0.02f, 0.03f, 0.5f)); } void TrackBar::draw(const GfxContext* pctx, Assets* assets) { @@ -444,6 +453,55 @@ void TrackBar::mouseMove(GUI*, int x, int y) { } } + +double TrackBar::getValue() const { + return value; +} + +double TrackBar::getMin() const { + return min; +} + +double TrackBar::getMax() const { + return max; +} + +double TrackBar::getStep() const { + return step; +} + +int TrackBar::getTrackWidth() const { + return trackWidth; +} + +glm::vec4 TrackBar::getTrackColor() const { + return trackColor; +} + +void TrackBar::setValue(double x) { + value = x; +} + +void TrackBar::setMin(double x) { + min = x; +} + +void TrackBar::setMax(double x) { + max = x; +} + +void TrackBar::setStep(double x) { + step = x; +} + +void TrackBar::setTrackWidth(int width) { + trackWidth = width; +} + +void TrackBar::setTrackColor(glm::vec4 color) { + trackColor = color; +} + // ================================ CheckBox ================================== CheckBox::CheckBox(bool checked) : UINode(glm::vec2(), glm::vec2(32.0f)), checked(checked) { setColor(glm::vec4(0.0f, 0.0f, 0.0f, 0.5f)); diff --git a/src/frontend/gui/controls.h b/src/frontend/gui/controls.h index 72aeb5cd..fd62f0db 100644 --- a/src/frontend/gui/controls.h +++ b/src/frontend/gui/controls.h @@ -72,6 +72,9 @@ namespace gui { virtual void setText(std::wstring text); virtual std::wstring getText() const; + virtual glm::vec4 getPressedColor() const; + virtual void setPressedColor(glm::vec4 color); + virtual Button* textSupplier(wstringsupplier supplier); virtual void refresh() override; @@ -144,7 +147,6 @@ namespace gui { class TrackBar : public UINode { protected: - glm::vec4 hoverColor {0.01f, 0.02f, 0.03f, 0.5f}; glm::vec4 trackColor {1.0f, 1.0f, 1.0f, 0.4f}; doublesupplier supplier = nullptr; doubleconsumer consumer = nullptr; @@ -165,6 +167,20 @@ namespace gui { virtual void setConsumer(doubleconsumer consumer); virtual void mouseMove(GUI*, int x, int y) override; + + virtual double getValue() const; + virtual double getMin() const; + virtual double getMax() const; + virtual double getStep() const; + virtual int getTrackWidth() const; + virtual glm::vec4 getTrackColor() const; + + virtual void setValue(double); + virtual void setMin(double); + virtual void setMax(double); + virtual void setStep(double); + virtual void setTrackWidth(int); + virtual void setTrackColor(glm::vec4); }; class CheckBox : public UINode { diff --git a/src/frontend/gui/gui_xml.cpp b/src/frontend/gui/gui_xml.cpp index bb1923da..bb218568 100644 --- a/src/frontend/gui/gui_xml.cpp +++ b/src/frontend/gui/gui_xml.cpp @@ -32,7 +32,13 @@ static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& no node.setSize(element->attr("size").asVec2()); } if (element->has("color")) { - node.setColor(element->attr("color").asColor()); + glm::vec4 color = element->attr("color").asColor(); + glm::vec4 hoverColor = color; + if (element->has("hover-color")) { + hoverColor = node.getHoverColor(); + } + node.setColor(color); + node.setHoverColor(hoverColor); } if (element->has("margin")) { node.setMargin(element->attr("margin").asVec4()); @@ -54,6 +60,9 @@ static void _readUINode(UiXmlReader& reader, xml::xmlelement element, UINode& no ); node.setPositionFunc(supplier); } + if (element->has("hover-color")) { + node.setHoverColor(element->attr("hover-color").asColor()); + } std::string alignName = element->attr("align", "").getText(); node.setAlign(align_from_string(alignName, node.getAlign())); } @@ -163,6 +172,9 @@ static std::shared_ptr readButton(UiXmlReader& reader, xml::xmlelement e if (element->has("text-align")) { button->setTextAlign(align_from_string(element->attr("text-align").getText(), button->getTextAlign())); } + if (element->has("pressed-color")) { + button->setPressedColor(element->attr("pressed-color").asColor()); + } return button; } @@ -216,6 +228,9 @@ static std::shared_ptr readTrackBar(UiXmlReader& reader, xml::xmlelement ); bar->setSupplier(supplier); } + if (element->has("track-color")) { + bar->setTrackColor(element->attr("track-color").asColor()); + } return bar; } diff --git a/src/logic/scripting/lua/libgui.cpp b/src/logic/scripting/lua/libgui.cpp index 6819ee4f..321e518f 100644 --- a/src/logic/scripting/lua/libgui.cpp +++ b/src/logic/scripting/lua/libgui.cpp @@ -24,12 +24,61 @@ static gui::UINode* getDocumentNode(lua_State* L, const std::string& name, const return node.get(); } +static bool getattr(lua_State* L, gui::TrackBar* bar, const std::string& attr) { + if (bar == nullptr) + return false; + if (attr == "value") { + lua_pushnumber(L, bar->getValue()); return true; + } else if (attr == "min") { + lua_pushnumber(L, bar->getMin()); return true; + } else if (attr == "max") { + lua_pushnumber(L, bar->getMax()); + return true; + } else if (attr == "step") { + lua_pushnumber(L, bar->getStep()); + return true; + } else if (attr == "trackWidth") { + lua_pushnumber(L, bar->getTrackWidth()); + return true; + } else if (attr == "trackColor") { + return lua::pushcolor_arr(L, bar->getTrackColor()); + } + return false; +} + +static bool setattr(lua_State* L, gui::TrackBar* bar, const std::string& attr) { + if (bar == nullptr) + return false; + if (attr == "value") { + bar->setValue(lua_tonumber(L, 4)); + return true; + } else if (attr == "min") { + bar->setMin(lua_tonumber(L, 4)); + return true; + } else if (attr == "max") { + bar->setMax(lua_tonumber(L, 4)); + return true; + } else if (attr == "step") { + bar->setStep(lua_tonumber(L, 4)); + return true; + } else if (attr == "trackWidth") { + bar->setTrackWidth(lua_tonumber(L, 4)); + return true; + } else if (attr == "trackColor") { + bar->setTrackColor(lua::tocolor(L, 4)); + return true; + } + return false; +} + static bool getattr(lua_State* L, gui::Button* button, const std::string& attr) { if (button == nullptr) return false; if (attr == "text") { lua_pushstring(L, util::wstr2str_utf8(button->getText()).c_str()); return true; + } else if (attr == "pressedColor") { + return lua::pushcolor_arr(L, button->getPressedColor()); } return false; } @@ -50,6 +99,8 @@ static bool setattr(lua_State* L, gui::Button* button, const std::string& attr) if (attr == "text") { button->setText(util::str2wstr_utf8(lua_tostring(L, 4))); return true; + } else if (attr == "pressedColor") { + button->setPressedColor(lua::tocolor(L, 4)); } return false; } @@ -76,12 +127,22 @@ int l_gui_getattr(lua_State* L) { return lua::pushvec2_arr(L, node->getCoord()); } else if (attr == "size") { return lua::pushvec2_arr(L, node->getSize()); + } else if (attr == "hoverColor") { + return lua::pushcolor_arr(L, node->getHoverColor()); + } else if (attr == "interactive") { + lua_pushboolean(L, node->isInteractive()); + return 1; + } else if (attr == "visible") { + lua_pushboolean(L, node->isVisible()); + return 1; } if (getattr(L, dynamic_cast(node), attr)) return 1; if (getattr(L, dynamic_cast(node), attr)) return 1; + if (getattr(L, dynamic_cast(node), attr)) + return 1; return 0; } @@ -101,11 +162,21 @@ int l_gui_setattr(lua_State* L) { node->setCoord(lua::tovec2(L, 4)); } else if (attr == "size") { node->setSize(lua::tovec2(L, 4)); + } else if (attr == "color") { + node->setColor(lua::tocolor(L, 4)); + } else if (attr == "hoverColor") { + node->setHoverColor(lua::tocolor(L, 4)); + } else if (attr == "interactive") { + node->setInteractive(lua_toboolean(L, 4)); + } else if (attr == "visible") { + node->setVisible(lua_toboolean(L, 4)); } else { if (setattr(L, dynamic_cast(node), attr)) return 0; if (setattr(L, dynamic_cast(node), attr)) return 0; + if (setattr(L, dynamic_cast(node), attr)) + return 0; } return 0; } diff --git a/src/logic/scripting/lua/lua_util.h b/src/logic/scripting/lua/lua_util.h index 55fa43fe..caa66d30 100644 --- a/src/logic/scripting/lua/lua_util.h +++ b/src/logic/scripting/lua/lua_util.h @@ -95,6 +95,23 @@ namespace lua { lua_pop(L, -1); return glm::vec2(x, y); } + + inline glm::vec4 tocolor(lua_State* L, int idx) { + lua_pushvalue(L, idx); + if (!lua_istable(L, -1)) { + luaL_error(L, "RGBA array required"); + } + lua_rawgeti(L, -1, 1); + lua::luanumber r = lua_tonumber(L, -1); lua_pop(L, -1); + lua_rawgeti(L, -2, 2); + lua::luanumber g = lua_tonumber(L, -1); lua_pop(L, -1); + lua_rawgeti(L, -3, 3); + lua::luanumber b = lua_tonumber(L, -1); lua_pop(L, -1); + lua_rawgeti(L, -4, 4); + lua::luanumber a = lua_tonumber(L, -1); lua_pop(L, -1); + lua_pop(L, -1); + return glm::vec4(r/255, g/255, b/255, a/255); + } } #endif // LOGIC_SCRIPTING_LUA_UTIL_H_