From 90f3835dae164b48ee63eaf4e1d586ca34961536 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Mon, 15 Apr 2024 17:14:03 +1000 Subject: [PATCH] widget.getText didn't work for labels and buttons?? --- source/windowing/StarButtonWidget.cpp | 4 +++ source/windowing/StarButtonWidget.hpp | 1 + source/windowing/StarTextBoxWidget.cpp | 2 +- source/windowing/StarTextBoxWidget.hpp | 2 +- source/windowing/StarWidgetLuaBindings.cpp | 38 ++++++++++++++-------- 5 files changed, 31 insertions(+), 16 deletions(-) diff --git a/source/windowing/StarButtonWidget.cpp b/source/windowing/StarButtonWidget.cpp index c19fb4b..a8c8a85 100644 --- a/source/windowing/StarButtonWidget.cpp +++ b/source/windowing/StarButtonWidget.cpp @@ -314,6 +314,10 @@ void ButtonWidget::setPressedOffset(Vec2I const& offset) { m_pressedOffset = offset; } +String const& ButtonWidget::getText() const { + return m_text; +} + void ButtonWidget::setText(String const& text) { m_text = text; } diff --git a/source/windowing/StarButtonWidget.hpp b/source/windowing/StarButtonWidget.hpp index d2616fe..69335d8 100644 --- a/source/windowing/StarButtonWidget.hpp +++ b/source/windowing/StarButtonWidget.hpp @@ -66,6 +66,7 @@ public: Vec2I const& pressedOffset() const; void setPressedOffset(Vec2I const& offset); + virtual String const& getText() const; virtual void setText(String const& text); virtual void setFontSize(int size); virtual void setFontDirectives(String directives); diff --git a/source/windowing/StarTextBoxWidget.cpp b/source/windowing/StarTextBoxWidget.cpp index 28d8f8b..984021d 100644 --- a/source/windowing/StarTextBoxWidget.cpp +++ b/source/windowing/StarTextBoxWidget.cpp @@ -154,7 +154,7 @@ void TextBoxWidget::update(float dt) { } } -String TextBoxWidget::getText() const { +String const& TextBoxWidget::getText() const { return m_text; } diff --git a/source/windowing/StarTextBoxWidget.hpp b/source/windowing/StarTextBoxWidget.hpp index 11a0527..12294d5 100644 --- a/source/windowing/StarTextBoxWidget.hpp +++ b/source/windowing/StarTextBoxWidget.hpp @@ -13,7 +13,7 @@ public: virtual void update(float dt) override; - String getText() const; + String const& getText() const; bool setText(String const& text, bool callback = true); bool getHidden() const; diff --git a/source/windowing/StarWidgetLuaBindings.cpp b/source/windowing/StarWidgetLuaBindings.cpp index 5b59d48..16ef279 100644 --- a/source/windowing/StarWidgetLuaBindings.cpp +++ b/source/windowing/StarWidgetLuaBindings.cpp @@ -206,27 +206,37 @@ LuaCallbacks LuaBindings::makeWidgetCallbacks(Widget* parentWidget, GuiReaderPtr // callbacks only valid for specific widget types callbacks.registerCallback("getText", [parentWidget](String const& widgetName) -> Maybe { - if (auto textBox = parentWidget->fetchChild(widgetName)) - return textBox->getText(); + if (auto widget = parentWidget->fetchChild(widgetName)) { + if (auto label = as(widget)) + return label->text(); + else if (auto button = as(widget)) + return button->getText(); + else if (auto textBox = as(widget)) + return textBox->getText(); + } return {}; }); callbacks.registerCallback("setText", [parentWidget](String const& widgetName, String const& text) { - if (auto label = parentWidget->fetchChild(widgetName)) - label->setText(text); - else if (auto button = parentWidget->fetchChild(widgetName)) - button->setText(text); - else if (auto textBox = parentWidget->fetchChild(widgetName)) - textBox->setText(text); + if (auto widget = parentWidget->fetchChild(widgetName)) { + if (auto label = as(widget)) + label->setText(text); + else if (auto button = as(widget)) + button->setText(text); + else if (auto textBox = as(widget)) + textBox->setText(text); + } }); callbacks.registerCallback("setFontColor", [parentWidget](String const& widgetName, Color const& color) { - if (auto label = parentWidget->fetchChild(widgetName)) - label->setColor(color); - else if (auto button = parentWidget->fetchChild(widgetName)) - button->setFontColor(color); - else if (auto textBox = parentWidget->fetchChild(widgetName)) - textBox->setColor(color); + if (auto widget = parentWidget->fetchChild(widgetName)) { + if (auto label = as(widget)) + label->setColor(color); + else if (auto button = as(widget)) + button->setFontColor(color); + else if (auto textBox = as(widget)) + textBox->setColor(color); + } }); callbacks.registerCallback("setImage", [parentWidget](String const& widgetName, String const& imagePath) {