Skip to content

Commit

Permalink
Layout with ratio can now also be created with RelativeValue object a…
Browse files Browse the repository at this point in the history
…s alternative to a string containing a percentage
  • Loading branch information
texus committed Sep 22, 2024
1 parent 99c13d4 commit 1885e14
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/TGUI/AbsoluteOrRelativeValue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ TGUI_MODULE_EXPORT namespace tgui
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
struct TGUI_API RelativeValue : AbsoluteOrRelativeValue
{
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Default constructor that sets the given ratio
/// @param ratio Ratio of the value relative to its parent size
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
explicit constexpr RelativeValue(float ratio)
{
m_constant = false;
Expand Down
10 changes: 10 additions & 0 deletions include/TGUI/Layout.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

#include <TGUI/Config.hpp>
#include <TGUI/Vector2.hpp>
#include <TGUI/AbsoluteOrRelativeValue.hpp>

#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
#include <type_traits>
Expand Down Expand Up @@ -141,6 +142,15 @@ TGUI_MODULE_EXPORT namespace tgui
{
}

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Constructs the layout from a ratio relative to the parent size
///
/// @param ratio Ratio to the size of the parent widget
///
/// Layout(RelativeValue(0.3f)) is equivalent to Layout("30%")
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Layout(RelativeValue ratio);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Constructs the layout based on a string which will be parsed to determine the value of the layout
///
Expand Down
9 changes: 9 additions & 0 deletions src/Layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ namespace tgui

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

Layout::Layout(RelativeValue ratio) :
Layout{Layout::Operation::Multiplies,
std::make_unique<Layout>(ratio.getRatio()),
std::make_unique<Layout>(U"&.innersize")}
{
}

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

Layout::Layout(String expression)
{
// Empty strings have value 0 (although this might indicate a mistake in the expression, it is valid for unary minus,
Expand Down
34 changes: 34 additions & 0 deletions tests/Layouts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,40 @@ TEST_CASE("[Layouts]")

SECTION("without strings")
{
SECTION("relative values")
{
Layout layout{tgui::RelativeValue(1)};
REQUIRE(layout.toString() == "100%");

Layout layout2{tgui::RelativeValue(0.5f)};
REQUIRE(layout2.toString() == "50%");

REQUIRE(!layout.isConstant());

auto button = std::make_shared<tgui::Button>();
button->setPosition({tgui::RelativeValue(0.4f), tgui::RelativeValue(0.3f)});
button->setSize({tgui::RelativeValue(0.2f), tgui::RelativeValue(0.1f)});

REQUIRE(button->getSize() == tgui::Vector2f(0, 0));
REQUIRE(button->getPosition() == tgui::Vector2f(0, 0));

auto panel = std::make_shared<tgui::Panel>();
panel->setSize(400, 300);
panel->add(button);

REQUIRE(button->getSize() == tgui::Vector2f(80, 30));
REQUIRE(button->getPosition() == tgui::Vector2f(160, 90));

SECTION("Inner size is used")
{
panel->getRenderer()->setBorders({5, 10, 15, 20});
panel->getRenderer()->setPadding({25, 30, 35, 40});

REQUIRE(button->getSize() == tgui::Vector2f(0.2f * (400 - 5 - 15 - 25 - 35), 0.1f * (300 - 10 - 20 - 30 - 40)));
REQUIRE(button->getPosition() == tgui::Vector2f(0.4f * (400 - 5 - 15 - 25 - 35), 0.3f * (300 - 10 - 20 - 30 - 40)));
}
}

SECTION("operators")
{
Layout l1{2};
Expand Down

0 comments on commit 1885e14

Please sign in to comment.