Skip to content

Commit

Permalink
Added setSpinButtonWidth function to SpinControl
Browse files Browse the repository at this point in the history
  • Loading branch information
texus committed Sep 28, 2024
1 parent 7753db0 commit f933e2f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 36 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 3 additions & 7 deletions gui-builder/include/WidgetProperties/SpinControlProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -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())};
Expand Down Expand Up @@ -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())};
Expand Down
25 changes: 22 additions & 3 deletions include/TGUI/Widgets/SpinControl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
77 changes: 51 additions & 26 deletions src/Widgets/SpinControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -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<SpinButton>(U"SpinButton")},
m_spinText {m_container->get<EditBox>(U"SpinText")}
{
Expand All @@ -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)}
{
Expand All @@ -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<SpinButton>(U"SpinButton");
m_spinText = m_container->get<EditBox>(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<SpinButton>(U"SpinButton");
m_spinText = m_container->get<EditBox>(U"SpinText");

init();
}
Expand All @@ -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();
Expand Down Expand Up @@ -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});
}
}
Expand Down Expand Up @@ -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));
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -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<DataIO::Node> SpinControl::save(SavingRenderersMap& renderers) const
{
auto node = SubwidgetContainer::save(renderers);
node->propertyValuePairs[U"DecimalPlaces"] = std::make_unique<DataIO::ValueNode>(String::fromNumber(m_decimalPlaces));
node->propertyValuePairs[U"UseWideArrows"] = std::make_unique<DataIO::ValueNode>(Serializer::serialize(m_useWideArrows));
node->propertyValuePairs[U"SpinButtonWidth"] = std::make_unique<DataIO::ValueNode>(m_spinButtonWidth.toString());
return node;
}

Expand All @@ -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<EditBox>("SpinText");
m_spinButton = m_container->get<SpinButton>("SpinButton");
Expand Down
1 change: 1 addition & 0 deletions tests/Widgets/SpinControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ TEST_CASE("[spinControl]")
spinControl->setMaximum(50);
spinControl->setValue(20);
spinControl->setStep(5);
spinControl->setSpinButtonWidth("80%");

testSavingWidget("SpinControl", spinControl);
}
Expand Down

0 comments on commit f933e2f

Please sign in to comment.