Skip to content

Commit

Permalink
setOrientation in Slider and SpinButton will no longer flip width and…
Browse files Browse the repository at this point in the history
… height + setSize in Slider and SpinButton no longer affects orientation once setOrientation is called
  • Loading branch information
texus committed Sep 28, 2024
1 parent 99a3acc commit 7753db0
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 52 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ TGUI 1.6 (TBD)
- Added CloseBehavior property to ChildWindow
- Added addMultipleItems to ListBox and ComboBox
- Added getItemByIndex, getIndexById and getIdByIndex to ComboBox
- 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
- Fixed wrong arrow sizes for horizontal spin button
- Fixed view not being usable in CanvasSFML
Expand Down
8 changes: 7 additions & 1 deletion gui-builder/include/WidgetProperties/SliderProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ struct SliderProperties : WidgetProperties
else if (property == "InvertedDirection")
slider->setInvertedDirection(parseBoolean(value, false));
else if (property == "Orientation")
slider->setOrientation(deserializeOrientation(value));
{
const tgui::Orientation oldOrientation = slider->getOrientation();
const tgui::Orientation newOrientation = deserializeOrientation(value);
slider->setOrientation(newOrientation);
if (oldOrientation != newOrientation)
slider->setSize({slider->getSize().y, slider->getSize().x});
}
else
WidgetProperties::updateProperty(widget, property, value);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,13 @@ struct SpinButtonProperties : WidgetProperties
else if (property == "Step")
spinButton->setStep(value.toFloat());
else if (property == "Orientation")
spinButton->setOrientation(deserializeOrientation(value));
{
const tgui::Orientation oldOrientation = spinButton->getOrientation();
const tgui::Orientation newOrientation = deserializeOrientation(value);
spinButton->setOrientation(newOrientation);
if (oldOrientation != newOrientation)
spinButton->setSize({spinButton->getSize().y, spinButton->getSize().x});
}
else
WidgetProperties::updateProperty(widget, property, value);
}
Expand Down
3 changes: 2 additions & 1 deletion include/TGUI/Widgets/Scrollbar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ TGUI_MODULE_EXPORT namespace tgui
///
/// @param size The new size of the scrollbar
///
/// Note that the Orientation propery is changed by this function based on the given width and height.
/// Note that the Orientation propery is changed by this function based on the given width and height,
/// unless the setOrientation function was previously called to explicitly select the orientation.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setSize(const Layout2d& size) override;
using Widget::setSize;
Expand Down
6 changes: 3 additions & 3 deletions include/TGUI/Widgets/Slider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ TGUI_MODULE_EXPORT namespace tgui
///
/// @param size The new size of the slider
///
/// Note that the Orientation propery is changed by this function based on the given width and height.
/// Note that the Orientation propery is changed by this function based on the given width and height,
/// unless the setOrientation function was previously called to explicitly select the orientation.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setSize(const Layout2d& size) override;
using Widget::setSize;
Expand Down Expand Up @@ -211,8 +212,6 @@ TGUI_MODULE_EXPORT namespace tgui
/// @brief Changes whether the slider lies horizontally or vertically
/// @param orientation Orientation of the slider
///
/// This function will swap the width and height of the slider if it didn't lie in the wanted direction.
///
/// @since TGUI 1.4
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setOrientation(Orientation orientation);
Expand Down Expand Up @@ -362,6 +361,7 @@ TGUI_MODULE_EXPORT namespace tgui

Orientation m_orientation = Orientation::Horizontal; // Is the slider drawn horizontally or vertically?
Orientation m_imageOrientation = Orientation::Horizontal; // Does the loaded image lie horizontally or vertically?
bool m_orientationLocked = false; // Will setSize change the orientation or not?

Sprite m_spriteTrack;
Sprite m_spriteTrackHover;
Expand Down
6 changes: 3 additions & 3 deletions include/TGUI/Widgets/SpinButton.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ TGUI_MODULE_EXPORT namespace tgui
///
/// @param size The new size of the spin button
///
/// Note that the Orientation property is changed by this function based on the given width and height.
/// Note that the Orientation propery is changed by this function based on the given width and height,
/// unless the setOrientation function was previously called to explicitly select the orientation.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setSize(const Layout2d& size) override;
using Widget::setSize;
Expand Down Expand Up @@ -188,8 +189,6 @@ TGUI_MODULE_EXPORT namespace tgui
/// @brief Changes whether the spin button lies horizontally or vertically
/// @param orientation Orientation of the spin button
///
/// This function will swap the width and height of the spin button if it didn't lie in the wanted direction.
///
/// @since TGUI 1.4
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void setOrientation(Orientation orientation);
Expand Down Expand Up @@ -282,6 +281,7 @@ TGUI_MODULE_EXPORT namespace tgui
protected:

Orientation m_orientation = Orientation::Vertical; // Is the spin button draw horizontally (arrows next to each other) or vertically (arrows on top of each other)?
bool m_orientationLocked = false; // Will setSize change the orientation or not?
std::chrono::time_point<std::chrono::steady_clock> m_PressedAt;

float m_minimum = 0;
Expand Down
3 changes: 0 additions & 3 deletions src/Widgets/Scrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,9 +289,6 @@ namespace tgui
void Scrollbar::setOrientation(Orientation orientation)
{
m_orientationLocked = true;
if (m_orientation == orientation)
return;

m_orientation = orientation;
}

Expand Down
24 changes: 15 additions & 9 deletions src/Widgets/Slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,13 @@ namespace tgui

m_bordersCached.updateParentSize(getSize());

if (getSize().x < getSize().y)
m_orientation = Orientation::Vertical;
else if (getSize().x > getSize().y)
m_orientation = Orientation::Horizontal;
if (!m_orientationLocked)
{
if (getSize().x < getSize().y)
m_orientation = Orientation::Vertical;
else if (getSize().x > getSize().y)
m_orientation = Orientation::Horizontal;
}

if (m_spriteTrack.isSet())
{
Expand Down Expand Up @@ -323,7 +326,13 @@ namespace tgui
#ifndef TGUI_REMOVE_DEPRECATED_CODE
void Slider::setVerticalScroll(bool vertical)
{
setOrientation(vertical ? Orientation::Vertical : Orientation::Horizontal);
m_orientationLocked = false;
const Orientation orientation = vertical ? Orientation::Vertical : Orientation::Horizontal;
if (m_orientation == orientation)
return;

m_orientation = orientation;
setSize(getSize().y, getSize().x);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -337,11 +346,8 @@ namespace tgui

void Slider::setOrientation(Orientation orientation)
{
if (m_orientation == orientation)
return;

m_orientationLocked = true;
m_orientation = orientation;
setSize(getSize().y, getSize().x);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
26 changes: 16 additions & 10 deletions src/Widgets/SpinButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ namespace tgui
setRenderer(Theme::getDefault()->getRendererNoThrow(m_type));
}

setSize(20, 42);
setSize(21, 42);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -101,10 +101,13 @@ namespace tgui

m_bordersCached.updateParentSize(getSize());

if (getSize().x < getSize().y)
m_orientation = Orientation::Vertical;
else if (getSize().x > getSize().y)
m_orientation = Orientation::Horizontal;
if (!m_orientationLocked)
{
if (getSize().x < getSize().y)
m_orientation = Orientation::Vertical;
else if (getSize().x > getSize().y)
m_orientation = Orientation::Horizontal;
}

if (m_orientation == Orientation::Vertical)
{
Expand Down Expand Up @@ -219,7 +222,13 @@ namespace tgui
#ifndef TGUI_REMOVE_DEPRECATED_CODE
void SpinButton::setVerticalScroll(bool vertical)
{
setOrientation(vertical ? Orientation::Vertical : Orientation::Horizontal);
m_orientationLocked = false;
const Orientation orientation = vertical ? Orientation::Vertical : Orientation::Horizontal;
if (m_orientation == orientation)
return;

m_orientation = orientation;
setSize(getSize().y, getSize().x);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -233,11 +242,8 @@ namespace tgui

void SpinButton::setOrientation(Orientation orientation)
{
if (m_orientation == orientation)
return;

m_orientationLocked = true;
m_orientation = orientation;
setSize(getSize().y, getSize().x);
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
60 changes: 50 additions & 10 deletions tests/Widgets/Slider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,19 +158,59 @@ TEST_CASE("[Slider]")

SECTION("Orientation")
{
slider->setSize(100, 20);
REQUIRE(slider->getOrientation() == tgui::Orientation::Horizontal);

slider->setSize(20, 100);
REQUIRE(slider->getOrientation() == tgui::Orientation::Vertical);
SECTION("Deprecated setVerticalScroll method")
{
REQUIRE(!slider->getVerticalScroll());

slider->setSize(100, 20);
REQUIRE(!slider->getVerticalScroll());
REQUIRE(slider->getOrientation() == tgui::Orientation::Horizontal);

slider->setSize(20, 100);
REQUIRE(slider->getVerticalScroll());
REQUIRE(slider->getOrientation() == tgui::Orientation::Vertical);

slider->setSize(10, 40);
slider->setVerticalScroll(false);
REQUIRE(!slider->getVerticalScroll());
REQUIRE(slider->getOrientation() == tgui::Orientation::Horizontal);
REQUIRE(slider->getSize() == tgui::Vector2f(40, 10)); // setVerticalScroll flips size
slider->setVerticalScroll(true);
REQUIRE(slider->getVerticalScroll());
REQUIRE(slider->getOrientation() == tgui::Orientation::Vertical);
REQUIRE(slider->getSize() == tgui::Vector2f(10, 40)); // setVerticalScroll flips size

// Orientation isn't locked, calling setSize can still alter the orientation
slider->setSize(100, 20);
REQUIRE(!slider->getVerticalScroll());
REQUIRE(slider->getOrientation() == tgui::Orientation::Horizontal);
}

slider->setSize(10, 40);
slider->setOrientation(tgui::Orientation::Horizontal);
REQUIRE(slider->getOrientation() == tgui::Orientation::Horizontal);
REQUIRE(slider->getSize() == tgui::Vector2f(40, 10));
slider->setOrientation(tgui::Orientation::Vertical);
REQUIRE(slider->getOrientation() == tgui::Orientation::Vertical);
REQUIRE(slider->getSize() == tgui::Vector2f(10, 40));
SECTION("New setOrientation method")
{
slider->setSize(100, 20);
REQUIRE(slider->getOrientation() == tgui::Orientation::Horizontal);

slider->setSize(20, 100);
REQUIRE(slider->getOrientation() == tgui::Orientation::Vertical);

slider->setSize(10, 40);
slider->setOrientation(tgui::Orientation::Horizontal);
REQUIRE(slider->getOrientation() == tgui::Orientation::Horizontal);
REQUIRE(slider->getSize() == tgui::Vector2f(10, 40)); // setOrientation does not alter size
slider->setOrientation(tgui::Orientation::Vertical);
REQUIRE(slider->getOrientation() == tgui::Orientation::Vertical);
REQUIRE(slider->getSize() == tgui::Vector2f(10, 40)); // setOrientation does not alter size

slider->setSize(100, 20);
slider->setOrientation(tgui::Orientation::Horizontal);

// Orientation is locked, setSize no longer alters the orientation
slider->setSize(20, 100);
REQUIRE(slider->getOrientation() == tgui::Orientation::Horizontal);
}
}

SECTION("InvertedDirection")
Expand Down
62 changes: 51 additions & 11 deletions tests/Widgets/SpinButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,59 @@ TEST_CASE("[SpinButton]")

SECTION("Orientation")
{
spinButton->setSize(100, 20);
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Horizontal);

spinButton->setSize(20, 100);
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Vertical);

spinButton->setSize(10, 40);
spinButton->setOrientation(tgui::Orientation::Horizontal);
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Horizontal);
REQUIRE(spinButton->getSize() == tgui::Vector2f(40, 10));
spinButton->setOrientation(tgui::Orientation::Vertical);
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Vertical);
REQUIRE(spinButton->getSize() == tgui::Vector2f(10, 40));
SECTION("Deprecated setVerticalScroll method")
{
REQUIRE(spinButton->getVerticalScroll());

spinButton->setSize(100, 20);
REQUIRE(!spinButton->getVerticalScroll());
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Horizontal);

spinButton->setSize(20, 100);
REQUIRE(spinButton->getVerticalScroll());
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Vertical);

spinButton->setSize(10, 40);
spinButton->setVerticalScroll(false);
REQUIRE(!spinButton->getVerticalScroll());
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Horizontal);
REQUIRE(spinButton->getSize() == tgui::Vector2f(40, 10)); // setVerticalScroll flips size
spinButton->setVerticalScroll(true);
REQUIRE(spinButton->getVerticalScroll());
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Vertical);
REQUIRE(spinButton->getSize() == tgui::Vector2f(10, 40)); // setVerticalScroll flips size

// Orientation isn't locked, calling setSize can still alter the orientation
spinButton->setSize(100, 20);
REQUIRE(!spinButton->getVerticalScroll());
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Horizontal);
}

SECTION("New setOrientation method")
{
spinButton->setSize(100, 20);
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Horizontal);

spinButton->setSize(20, 100);
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Vertical);

spinButton->setSize(10, 40);
spinButton->setOrientation(tgui::Orientation::Horizontal);
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Horizontal);
REQUIRE(spinButton->getSize() == tgui::Vector2f(10, 40)); // setOrientation does not alter size
spinButton->setOrientation(tgui::Orientation::Vertical);
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Vertical);
REQUIRE(spinButton->getSize() == tgui::Vector2f(10, 40)); // setOrientation does not alter size

spinButton->setSize(100, 20);
spinButton->setOrientation(tgui::Orientation::Horizontal);

// Orientation is locked, setSize no longer alters the orientation
spinButton->setSize(20, 100);
REQUIRE(spinButton->getOrientation() == tgui::Orientation::Horizontal);
}
}

SECTION("Events / Signals")
Expand Down

0 comments on commit 7753db0

Please sign in to comment.