TGUI 1.11
Loading...
Searching...
No Matches
AbsoluteOrRelativeValue.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2025 Bruno Van de Velde (vdv_b@tgui.eu)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
26#define TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
27
28#include <TGUI/Global.hpp>
29#include <TGUI/String.hpp>
30
31#include <type_traits>
32
34
35namespace tgui
36{
43 {
44 public:
45
49 constexpr AbsoluteOrRelativeValue() = default;
50
56 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
57 constexpr AbsoluteOrRelativeValue(T constant) :
58 m_constant {true},
59 m_value {static_cast<float>(constant)}
60 {
61 }
62
68 AbsoluteOrRelativeValue(const char* expression) :
70 {
71 }
72
79 {
80 if (!expression.empty() && (expression.back() == '%'))
81 {
82 m_constant = false;
83 m_ratio = expression.substr(0, expression.length()-1).toFloat() / 100.f;
84 }
85 else
86 {
87 m_constant = true;
88 m_value = expression.substr(0, expression.length()).toFloat();
89 }
90 }
91
97 TGUI_NODISCARD constexpr float getValue() const
98 {
99 return m_value;
100 }
101
107 TGUI_NODISCARD constexpr float getRatio() const
108 {
109 return m_ratio;
110 }
111
117 TGUI_NODISCARD constexpr bool isConstant() const
118 {
119 return m_constant;
120 }
121
128 constexpr void updateParentSize(float newParentSize)
129 {
130 if (!m_constant)
131 {
132 m_parentValue = newParentSize;
133 m_value = m_ratio * newParentSize;
134 }
135 }
136
143 TGUI_NODISCARD String toString() const
144 {
145 if (m_constant)
146 return String::fromNumber(m_value);
147 else
148 return String::fromNumber(m_ratio * 100) + '%';
149 }
150
152 protected:
153
154 bool m_constant = true;
155 float m_value = 0;
156 float m_ratio = 0;
157 float m_parentValue = 0;
158 };
159
164 {
169 explicit constexpr RelativeValue(float ratio)
170 {
171 m_constant = false;
172 m_ratio = ratio;
173 }
174 };
175
177
178 // Allow writing 50_percent instead of RelativeValue(0.5) after adding "using namespace tgui::literals::percent;"
179 // This can also be used in layouts, where you can replace e.g. the "40%" string with 40_percent to skip string parsing
180 inline namespace literals
181 {
182 inline namespace percent
183 {
184 constexpr RelativeValue operator""_percent(long double n)
185 {
186 return RelativeValue{static_cast<float>(n / 100.0L)};
187 }
188
189 constexpr RelativeValue operator""_percent(unsigned long long n)
190 {
191 return RelativeValue{static_cast<float>(n) / 100.f};
192 }
193 }
194 }
195
197}
198
200
201#endif // TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
constexpr float getRatio() const
Returns the stored ratio.
Definition AbsoluteOrRelativeValue.hpp:107
constexpr bool isConstant() const
Returns whether the value is constant or a ratio.
Definition AbsoluteOrRelativeValue.hpp:117
AbsoluteOrRelativeValue(const char *expression)
Construct the value from a string that either contains a value or a percentage.
Definition AbsoluteOrRelativeValue.hpp:68
constexpr AbsoluteOrRelativeValue(T constant)
Constructor to set constant.
Definition AbsoluteOrRelativeValue.hpp:57
AbsoluteOrRelativeValue(const String &expression)
Construct the value from a string that either contains a value or a percentage.
Definition AbsoluteOrRelativeValue.hpp:78
constexpr float getValue() const
Returns the value.
Definition AbsoluteOrRelativeValue.hpp:97
constexpr AbsoluteOrRelativeValue()=default
Default constructor.
Wrapper class to store strings.
Definition String.hpp:94
float toFloat(float defaultValue=0) const
Converts the string to a float.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:36
constexpr RelativeValue(float ratio)
Default constructor that sets the given ratio.
Definition AbsoluteOrRelativeValue.hpp:169