Skip to content

Commit

Permalink
Scrollbar::setOrientation will no longer flip width and height + Scro…
Browse files Browse the repository at this point in the history
…llbar::setSize no longer affects orientation once setOrientation is called
  • Loading branch information
texus committed Aug 16, 2024
1 parent 64ff862 commit efa878d
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 44 deletions.
2 changes: 2 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ TGUI 1.5 (TBD)
- Added getter for hovered item to ListBox, ListView, PanelListBox, Tabs and TreeView
- Added option to buttons to only repond to clicks and ignore space/return key presses
- Added setItemIndexInParent and getItemIndexInParent functions to TreeView
- Scrollbar::setOrientation will no longer flip width and height
- Scrollbar::setSize no longer affects orientation once setOrientation is called
- Opacity of ScrollablePanel wasn't applied to its scrollbars
- Setting opacity of SeparatorLine had no effect

Expand Down
8 changes: 7 additions & 1 deletion gui-builder/include/WidgetProperties/ScrollbarProperties.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,13 @@ struct ScrollbarProperties : WidgetProperties
else if (property == "Policy")
scrollbar->setPolicy(deserializeScrollbarPolicy(value));
else if (property == "Orientation")
scrollbar->setOrientation(deserializeOrientation(value));
{
const tgui::Orientation oldOrientation = scrollbar->getOrientation();
const tgui::Orientation newOrientation = deserializeOrientation(value);
scrollbar->setOrientation(newOrientation);
if (oldOrientation != newOrientation)
scrollbar->setSize({scrollbar->getSize().y, scrollbar->getSize().x});
}
else
WidgetProperties::updateProperty(widget, property, value);
}
Expand Down
13 changes: 9 additions & 4 deletions include/TGUI/Widgets/Scrollbar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ TGUI_MODULE_EXPORT namespace tgui

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Creates a new scrollbar widget
///
/// @param orientation Whether the scrollbar lies horizontally or vertically
/// @return The new scrollbar
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD static Scrollbar::Ptr create();
TGUI_NODISCARD static Scrollbar::Ptr create(Orientation orientation = Orientation::Vertical);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Makes a copy of another scrollbar
Expand Down Expand Up @@ -235,7 +235,6 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD bool isShown() const;

#ifndef TGUI_REMOVE_DEPRECATED_CODE
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes whether the scrollbar lies horizontally or vertically
/// @param vertical Should the scrollbar lie vertically?
Expand All @@ -249,7 +248,6 @@ TGUI_MODULE_EXPORT namespace tgui
/// @return Does the scrollbar lie vertically?
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_DEPRECATED("Use getOrientation instead") TGUI_NODISCARD bool getVerticalScroll() const;
#endif

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Changes whether the scrollbar lies horizontally or vertically
Expand Down Expand Up @@ -401,6 +399,7 @@ TGUI_MODULE_EXPORT namespace tgui

Orientation m_orientation = Orientation::Vertical; // Is the scrollbar drawn horizontally or vertically?
Orientation m_imageOrientation = Orientation::Vertical; // Does the loaded image lie horizontally or vertically?
bool m_orientationLocked = false; // TGUI_NEXT: Remove property and make locked the default

// How far should the value change when pressing one of the arrows?
unsigned int m_scrollAmount = 1;
Expand Down Expand Up @@ -451,6 +450,12 @@ TGUI_MODULE_EXPORT namespace tgui
{
public:

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Default constructor
/// @param orientation Whether the scrollbar lies horizontally or vertically
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
ScrollbarChildWidget(Orientation orientation = Orientation::Vertical); // TGUI_NEXT: No more default option

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns whether the left mouse button has been pressed on top of the thumb of the scrollbar
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 0 additions & 2 deletions src/Widgets/ListView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ namespace tgui
ListView::ListView(const char* typeName, bool initRenderer) :
Widget{typeName, false}
{
m_horizontalScrollbar->setSize(m_horizontalScrollbar->getSize().y, m_horizontalScrollbar->getSize().x);

if (initRenderer)
{
m_renderer = aurora::makeCopied<ListViewRenderer>();
Expand Down
19 changes: 7 additions & 12 deletions src/Widgets/ScrollablePanel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,6 @@ namespace tgui
ScrollablePanel::ScrollablePanel(const char* typeName, bool initRenderer) :
Panel{typeName, false}
{
// Rotate the horizontal scrollbar
m_horizontalScrollbar->setSize(m_horizontalScrollbar->getSize().y, m_horizontalScrollbar->getSize().x);

if (initRenderer)
{
m_renderer = aurora::makeCopied<ScrollablePanelRenderer>();
Expand Down Expand Up @@ -810,37 +807,35 @@ namespace tgui
}

const bool horizontalScrollbarVisible = m_horizontalScrollbar->isShown();
const float minVerticalScrollbarHeight = m_verticalScrollbar->getSize().x;
const float minHorizontalScrollbarWidth = m_horizontalScrollbar->getSize().y;
if (horizontalScrollbarVisible)
{
m_verticalScrollbar->setSize(m_verticalScrollbar->getSize().x, std::max(minVerticalScrollbarHeight, scrollbarSpace.y - m_horizontalScrollbar->getSize().y));
m_verticalScrollbar->setSize(m_verticalScrollbar->getSize().x, scrollbarSpace.y - m_horizontalScrollbar->getSize().y);
m_verticalScrollbar->setViewportSize(static_cast<unsigned int>(m_verticalScrollbar->getViewportSize() - m_horizontalScrollbar->getSize().y));

if (m_verticalScrollbar->isShown())
{
m_horizontalScrollbar->setViewportSize(static_cast<unsigned int>(m_horizontalScrollbar->getViewportSize() - m_verticalScrollbar->getSize().x));
m_horizontalScrollbar->setSize(std::max(minHorizontalScrollbarWidth, scrollbarSpace.x - m_verticalScrollbar->getSize().x), m_horizontalScrollbar->getSize().y);
m_horizontalScrollbar->setSize(scrollbarSpace.x - m_verticalScrollbar->getSize().x, m_horizontalScrollbar->getSize().y);
}
else
m_horizontalScrollbar->setSize(std::max(minHorizontalScrollbarWidth, scrollbarSpace.x), m_horizontalScrollbar->getSize().y);
m_horizontalScrollbar->setSize(scrollbarSpace.x, m_horizontalScrollbar->getSize().y);
}
else
{
m_verticalScrollbar->setSize(m_verticalScrollbar->getSize().x, std::max(minVerticalScrollbarHeight, scrollbarSpace.y));
m_verticalScrollbar->setSize(m_verticalScrollbar->getSize().x, scrollbarSpace.y);
if (m_verticalScrollbar->isShown())
{
m_horizontalScrollbar->setSize(std::max(minHorizontalScrollbarWidth, scrollbarSpace.x - m_verticalScrollbar->getSize().x), m_horizontalScrollbar->getSize().y);
m_horizontalScrollbar->setSize(scrollbarSpace.x - m_verticalScrollbar->getSize().x, m_horizontalScrollbar->getSize().y);
m_horizontalScrollbar->setViewportSize(static_cast<unsigned int>(m_horizontalScrollbar->getViewportSize() - m_verticalScrollbar->getSize().x));

if (m_horizontalScrollbar->isShown())
{
m_verticalScrollbar->setSize(m_verticalScrollbar->getSize().x, std::max(minVerticalScrollbarHeight, scrollbarSpace.y - m_horizontalScrollbar->getSize().y));
m_verticalScrollbar->setSize(m_verticalScrollbar->getSize().x, scrollbarSpace.y - m_horizontalScrollbar->getSize().y);
m_verticalScrollbar->setViewportSize(static_cast<unsigned int>(m_verticalScrollbar->getViewportSize() - m_horizontalScrollbar->getSize().y));
}
}
else
m_horizontalScrollbar->setSize(std::max(minHorizontalScrollbarWidth, scrollbarSpace.x), m_horizontalScrollbar->getSize().y);
m_horizontalScrollbar->setSize(scrollbarSpace.x, m_horizontalScrollbar->getSize().y);
}

m_verticalScrollbar->setPosition(m_bordersCached.getLeft() + scrollbarSpace.x - m_verticalScrollbar->getSize().x, m_bordersCached.getTop());
Expand Down
49 changes: 39 additions & 10 deletions src/Widgets/Scrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,15 @@ namespace tgui

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

Scrollbar::Ptr Scrollbar::create()
Scrollbar::Ptr Scrollbar::create(Orientation orientation)
{
return std::make_shared<Scrollbar>();
auto scrollbar = std::make_shared<Scrollbar>();
if (orientation == Orientation::Horizontal)
{
scrollbar->setOrientation(orientation);
scrollbar->setSize({scrollbar->getSize().y, scrollbar->getSize().x});
}
return scrollbar;
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -259,10 +265,16 @@ namespace tgui
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef TGUI_REMOVE_DEPRECATED_CODE

void Scrollbar::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 @@ -271,16 +283,16 @@ namespace tgui
{
return m_orientation == Orientation::Vertical;
}
#endif

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

void Scrollbar::setOrientation(Orientation orientation)
{
m_orientationLocked = true;
if (m_orientation == orientation)
return;

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

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

void Scrollbar::updateSize()
{
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 @@ -1179,6 +1194,17 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ScrollbarChildWidget::ScrollbarChildWidget(Orientation orientation)
{
if (orientation == Orientation::Horizontal)
{
setOrientation(orientation);
setSize({getSize().y, getSize().x});
}
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

bool ScrollbarChildWidget::isMouseDownOnThumb() const
{
return m_mouseDownOnThumb;
Expand Down Expand Up @@ -1293,6 +1319,7 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

ScrollbarChildInterface::ScrollbarChildInterface() :
m_scrollbar(Orientation::Vertical),
m_scrollbarAccessor{*m_scrollbar, [this]{ scrollbarValueChanged(); }, [this]{ scrollbarPolicyChanged(); }, [this]{ scrollbarScrollAmountChanged(); }}
{
}
Expand Down Expand Up @@ -1406,6 +1433,8 @@ namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

DualScrollbarChildInterface::DualScrollbarChildInterface() :
m_verticalScrollbar(Orientation::Vertical),
m_horizontalScrollbar(Orientation::Horizontal),
m_verticalScrollbarAccessor{*m_verticalScrollbar, [this]{ scrollbarValueChanged(Orientation::Vertical); }, [this]{ scrollbarPolicyChanged(Orientation::Vertical); }, [this]{ scrollbarScrollAmountChanged(Orientation::Vertical); }},
m_horizontalScrollbarAccessor{*m_horizontalScrollbar, [this]{ scrollbarValueChanged(Orientation::Horizontal); }, [this]{ scrollbarPolicyChanged(Orientation::Horizontal); }, [this]{ scrollbarScrollAmountChanged(Orientation::Horizontal); }}
{
Expand Down
1 change: 0 additions & 1 deletion src/Widgets/TextArea.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ namespace tgui
m_textAfterSelection2.setFont(m_fontCached);
m_defaultText.setFont(m_fontCached);

m_horizontalScrollbar->setSize(m_horizontalScrollbar->getSize().y, m_horizontalScrollbar->getSize().x);
m_horizontalScrollbar->setPolicy(Scrollbar::Policy::Never);

if (initRenderer)
Expand Down
3 changes: 0 additions & 3 deletions src/Widgets/TreeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,6 @@ namespace tgui
TreeView::TreeView(const char* typeName, bool initRenderer) :
Widget{typeName, false}
{
// Rotate the horizontal scrollbar
m_horizontalScrollbar->setSize(m_horizontalScrollbar->getSize().y, m_horizontalScrollbar->getSize().x);

if (initRenderer)
{
m_renderer = aurora::makeCopied<TreeViewRenderer>();
Expand Down
62 changes: 51 additions & 11 deletions tests/Widgets/Scrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,59 @@ TEST_CASE("[Scrollbar]")

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

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

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

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

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

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

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

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

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

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

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

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

SECTION("Events / Signals")
Expand Down

0 comments on commit efa878d

Please sign in to comment.