diff --git a/changelog.md b/changelog.md index d1e1a4143..7e4afabb1 100644 --- a/changelog.md +++ b/changelog.md @@ -4,6 +4,7 @@ TGUI 1.6 (TBD) - Added CloseBehavior property to ChildWindow - Added addMultipleItems to ListBox and ComboBox - Added getItemByIndex, getIndexById and getIdByIndex to ComboBox +- Added setSpinButtonWidth function to SpinControl - setOrientation in Slider or SpinButton will no longer flip width and height - setSize in Slider or SpinButton no longer affects orientation once setOrientation is called - Fixed crash on exit when tool tip was visible diff --git a/gui-builder/include/WidgetProperties/SpinControlProperties.hpp b/gui-builder/include/WidgetProperties/SpinControlProperties.hpp index d05d231cb..3dce47be8 100644 --- a/gui-builder/include/WidgetProperties/SpinControlProperties.hpp +++ b/gui-builder/include/WidgetProperties/SpinControlProperties.hpp @@ -43,8 +43,8 @@ struct SpinControlProperties : WidgetProperties spinControl->setStep(value.toFloat()); else if (property == "DecimalPlaces") spinControl->setDecimalPlaces(value.toUInt()); - else if (property == "UseWideArrows") - spinControl->setUseWideArrows(parseBoolean(value, false)); + else if (property == "SpinButtonWidth") + spinControl->setSpinButtonWidth(value.toFloat()); else WidgetProperties::updateProperty(widget, property, value); } @@ -57,9 +57,8 @@ struct SpinControlProperties : WidgetProperties pair.first["Maximum"] = {"Float", tgui::String::fromNumber(spinControl->getMaximum())}; pair.first["Value"] = {"Float", tgui::String::fromNumber(spinControl->getValue())}; pair.first["Step"] = {"Float", tgui::String::fromNumber(spinControl->getStep())}; - pair.first["Step"] = {"Float", tgui::String::fromNumber(spinControl->getStep())}; pair.first["DecimalPlaces"] = {"UInt", tgui::String::fromNumber(spinControl->getDecimalPlaces())}; - pair.first["UseWideArrows"] = {"Bool", tgui::Serializer::serialize(spinControl->getUseWideArrows())}; + pair.first["SpinButtonWidth"] = {"Float", tgui::String::fromNumber(spinControl->getSpinButtonWidth())}; const auto buttonRenderer = spinControl->getSpinButtonSharedRenderer(); pair.second["SpinButton.ButtonsBorders"] = {"Outline", tgui::Serializer::serialize(buttonRenderer->getBorders())}; @@ -89,9 +88,6 @@ struct SpinControlProperties : WidgetProperties pair.second["SpinText.BackgroundColorFocused"] = {"Color", tgui::Serializer::serialize(textRenderer->getBackgroundColorFocused())}; pair.second["SpinText.CaretColor"] = {"Color", tgui::Serializer::serialize(textRenderer->getCaretColor())}; pair.second["SpinText.CaretColorHover"] = {"Color", tgui::Serializer::serialize(textRenderer->getCaretColorHover())}; -TGUI_IGNORE_DEPRECATED_WARNINGS_START - pair.second["SpinText.CaretColorFocused"] = {"Color", tgui::Serializer::serialize(textRenderer->getCaretColorFocused())}; -TGUI_IGNORE_DEPRECATED_WARNINGS_END pair.second["SpinText.BorderColor"] = {"Color", tgui::Serializer::serialize(textRenderer->getBorderColor())}; pair.second["SpinText.BorderColorHover"] = {"Color", tgui::Serializer::serialize(textRenderer->getBorderColorHover())}; pair.second["SpinText.BorderColorDisabled"] = {"Color", tgui::Serializer::serialize(textRenderer->getBorderColorDisabled())}; diff --git a/include/TGUI/Widgets/SpinControl.hpp b/include/TGUI/Widgets/SpinControl.hpp index e0c919148..2e7c8b1fa 100644 --- a/include/TGUI/Widgets/SpinControl.hpp +++ b/include/TGUI/Widgets/SpinControl.hpp @@ -227,13 +227,31 @@ TGUI_MODULE_EXPORT namespace tgui /// @brief Changes whether the spin button width equals the heigh or the widget or only half of the height (default) /// @param useWideArrows Should the width of the spin button equal its height instead of half the height? ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - void setUseWideArrows(bool useWideArrows); + TGUI_DEPRECATED("Use setSpinButtonWidth(\"100%\") or setSpinButtonWidth(\"50%\") instead") void setUseWideArrows(bool useWideArrows); ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// @brief Returns whether the spin button width equals the heigh or the widget or only half of the height (default) /// @return Does the width of the spin button equal its height instead of half the height? ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - TGUI_NODISCARD bool getUseWideArrows() const; + TGUI_DEPRECATED("Use getSpinButtonWidth() instead") TGUI_NODISCARD bool getUseWideArrows() const; + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Changes the width of the spin button (i.e. width of the arrows that are shown next to the edit box) + /// + /// @param width Width of the spin button + /// + /// When the width is a relative value, then it is relative to the height of the spin control. + /// + /// @since TGUI 1.6 + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void setSpinButtonWidth(AbsoluteOrRelativeValue width); + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + /// @brief Returns the width of the spin button (i.e. width of the arrows that are shown next to the edit box) + /// @return Width of the spin button + /// @since TGUI 1.6 + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + float getSpinButtonWidth() const; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// protected: @@ -291,7 +309,8 @@ TGUI_MODULE_EXPORT namespace tgui protected: unsigned int m_decimalPlaces = 0; - bool m_useWideArrows = false; + bool m_useWideArrows = false; // TGUI_NEXT: Remove + AbsoluteOrRelativeValue m_spinButtonWidth; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private: diff --git a/src/Widgets/SpinControl.cpp b/src/Widgets/SpinControl.cpp index a3030dca3..1efe18660 100644 --- a/src/Widgets/SpinControl.cpp +++ b/src/Widgets/SpinControl.cpp @@ -44,6 +44,11 @@ namespace tgui m_container->add(m_spinButton, "SpinButton"); init(); + + if (m_spinButton->getSize().x > 0 && m_spinButton->getSize().y > 0) + setSpinButtonWidth(RelativeValue(m_spinButton->getSize().x / m_spinButton->getSize().y)); + else + setSpinButtonWidth(RelativeValue(0.5f)); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -53,6 +58,7 @@ namespace tgui onValueChange {other.onValueChange}, m_decimalPlaces {other.m_decimalPlaces}, m_useWideArrows {other.m_useWideArrows}, + m_spinButtonWidth {other.m_spinButtonWidth}, m_spinButton {m_container->get(U"SpinButton")}, m_spinText {m_container->get(U"SpinText")} { @@ -66,6 +72,7 @@ namespace tgui onValueChange {std::move(other.onValueChange)}, m_decimalPlaces {std::move(other.m_decimalPlaces)}, m_useWideArrows {std::move(other.m_useWideArrows)}, + m_spinButtonWidth {std::move(other.m_spinButtonWidth)}, m_spinButton {std::move(other.m_spinButton)}, m_spinText {std::move(other.m_spinText)} { @@ -79,11 +86,12 @@ namespace tgui if (this != &other) { SubwidgetContainer::operator=(other); - onValueChange = other.onValueChange; - m_decimalPlaces = other.m_decimalPlaces; - m_useWideArrows = other.m_useWideArrows; - m_spinButton = m_container->get(U"SpinButton"); - m_spinText = m_container->get(U"SpinText"); + onValueChange = other.onValueChange; + m_decimalPlaces = other.m_decimalPlaces; + m_useWideArrows = other.m_useWideArrows; + m_spinButtonWidth = other.m_spinButtonWidth; + m_spinButton = m_container->get(U"SpinButton"); + m_spinText = m_container->get(U"SpinText"); init(); } @@ -97,11 +105,12 @@ namespace tgui { if (this != &other) { - onValueChange = std::move(other.onValueChange); - m_decimalPlaces = std::move(other.m_decimalPlaces); - m_useWideArrows = std::move(other.m_useWideArrows); - m_spinButton = std::move(other.m_spinButton); - m_spinText = std::move(other.m_spinText); + onValueChange = std::move(other.onValueChange); + m_decimalPlaces = std::move(other.m_decimalPlaces); + m_useWideArrows = std::move(other.m_useWideArrows); + m_spinButtonWidth = std::move(other.m_spinButtonWidth); + m_spinButton = std::move(other.m_spinButton); + m_spinText = std::move(other.m_spinText); SubwidgetContainer::operator=(std::move(other)); init(); @@ -182,22 +191,14 @@ namespace tgui { SubwidgetContainer::setSize(size); - if (getSize().x > getSize().y) - { - if (m_useWideArrows) - { - m_spinButton->setSize({getSize().y, getSize().y}); - m_spinText->setSize({getSize().x - getSize().y, getSize().y}); - } - else - { - m_spinButton->setSize({getSize().y / 2, getSize().y}); - m_spinText->setSize({getSize().x - getSize().y / 2, getSize().y}); - } - } + m_spinButtonWidth.updateParentSize(getSize().y); + m_spinButton->setSize({m_spinButtonWidth.getValue(), getSize().y}); + + if (getSize().x > m_spinButton->getSize().x) + m_spinText->setSize({getSize().x - m_spinButton->getSize().x, getSize().y}); else { - m_spinButton->setSize({getSize().x, getSize().y}); + m_spinButton->setSize(getSize()); m_spinText->setSize({0, 0}); } } @@ -284,7 +285,10 @@ namespace tgui void SpinControl::setUseWideArrows(bool useWideArrows) { m_useWideArrows = useWideArrows; - setSize(m_size); + if (useWideArrows) + setSpinButtonWidth(RelativeValue(1.0f)); + else + setSpinButtonWidth(RelativeValue(0.5f)); } ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -296,11 +300,26 @@ namespace tgui ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + void SpinControl::setSpinButtonWidth(AbsoluteOrRelativeValue width) + { + m_spinButtonWidth = width; + setSize(m_size); + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + + float SpinControl::getSpinButtonWidth() const + { + return m_spinButtonWidth.getValue(); + } + + ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// + std::unique_ptr SpinControl::save(SavingRenderersMap& renderers) const { auto node = SubwidgetContainer::save(renderers); node->propertyValuePairs[U"DecimalPlaces"] = std::make_unique(String::fromNumber(m_decimalPlaces)); - node->propertyValuePairs[U"UseWideArrows"] = std::make_unique(Serializer::serialize(m_useWideArrows)); + node->propertyValuePairs[U"SpinButtonWidth"] = std::make_unique(m_spinButtonWidth.toString()); return node; } @@ -312,8 +331,14 @@ namespace tgui if (node->propertyValuePairs[U"DecimalPlaces"]) setDecimalPlaces(node->propertyValuePairs[U"DecimalPlaces"]->value.toUInt()); + + TGUI_IGNORE_DEPRECATED_WARNINGS_START if (node->propertyValuePairs[U"UseWideArrows"]) setUseWideArrows(Deserializer::deserialize(ObjectConverter::Type::Bool, node->propertyValuePairs[U"UseWideArrows"]->value).getBool()); + TGUI_IGNORE_DEPRECATED_WARNINGS_END + + if (node->propertyValuePairs[U"SpinButtonWidth"]) + setSpinButtonWidth(node->propertyValuePairs[U"SpinButtonWidth"]->value); m_spinText = m_container->get("SpinText"); m_spinButton = m_container->get("SpinButton"); diff --git a/tests/Widgets/SpinControl.cpp b/tests/Widgets/SpinControl.cpp index f6215d1b8..b8670fe71 100644 --- a/tests/Widgets/SpinControl.cpp +++ b/tests/Widgets/SpinControl.cpp @@ -180,6 +180,7 @@ TEST_CASE("[spinControl]") spinControl->setMaximum(50); spinControl->setValue(20); spinControl->setStep(5); + spinControl->setSpinButtonWidth("80%"); testSavingWidget("SpinControl", spinControl); }