TGUI  1.3-dev
Loading...
Searching...
No Matches
AbsoluteOrRelativeValue.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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
26#ifndef TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
27#define TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
28
29#include <TGUI/Global.hpp>
30#include <TGUI/String.hpp>
31
32#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
33 #include <type_traits>
34#endif
35
37
38TGUI_MODULE_EXPORT namespace tgui
39{
46 {
47 public:
48
52 constexpr AbsoluteOrRelativeValue() = default;
53
54
60 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
61 constexpr AbsoluteOrRelativeValue(T constant) :
62 m_constant {true},
63 m_value {static_cast<float>(constant)}
64 {
65 }
66
67
73 AbsoluteOrRelativeValue(const char* expression) :
75 {
76 }
77
78
85 {
86 if (!expression.empty() && (expression.back() == '%'))
87 {
88 m_constant = false;
89 m_ratio = expression.substr(0, expression.length()-1).toFloat() / 100.f;
90 }
91 else
92 {
93 m_constant = true;
94 m_value = expression.substr(0, expression.length()).toFloat();
95 }
96 }
97
98
104 TGUI_NODISCARD constexpr float getValue() const
105 {
106 return m_value;
107 }
108
109
115 TGUI_NODISCARD constexpr float getRatio() const
116 {
117 return m_ratio;
118 }
119
120
126 TGUI_NODISCARD constexpr bool isConstant() const
127 {
128 return m_constant;
129 }
130
131
138 constexpr void updateParentSize(float newParentSize)
139 {
140 if (!m_constant)
141 {
142 m_parentValue = newParentSize;
143 m_value = m_ratio * newParentSize;
144 }
145 }
146
147
154 TGUI_NODISCARD String toString() const
155 {
156 if (m_constant)
157 return String::fromNumber(m_value);
158 else
159 return String::fromNumber(m_ratio * 100) + '%';
160 }
161
162
164 protected:
165
166 bool m_constant = true;
167 float m_value = 0;
168 float m_ratio = 0;
169 float m_parentValue = 0;
170 };
171
172
177 {
178 explicit constexpr RelativeValue(float ratio)
179 {
180 m_constant = false;
181 m_ratio = ratio;
182 }
183 };
184
186}
187
189
190#endif // TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
Class to store the a value that is either a constant or a ratio.
Definition AbsoluteOrRelativeValue.hpp:46
TGUI_NODISCARD constexpr float getValue() const
Returns the value.
Definition AbsoluteOrRelativeValue.hpp:104
TGUI_NODISCARD constexpr float getRatio() const
Returns the stored ratio.
Definition AbsoluteOrRelativeValue.hpp:115
AbsoluteOrRelativeValue(const char *expression)
Construct the value from a string that either contains a value or a percentage.
Definition AbsoluteOrRelativeValue.hpp:73
constexpr AbsoluteOrRelativeValue(T constant)
Constructor to set constant.
Definition AbsoluteOrRelativeValue.hpp:61
TGUI_NODISCARD constexpr bool isConstant() const
Returns whether the value is constant or a ratio.
Definition AbsoluteOrRelativeValue.hpp:126
AbsoluteOrRelativeValue(const String &expression)
Construct the value from a string that either contains a value or a percentage.
Definition AbsoluteOrRelativeValue.hpp:84
constexpr AbsoluteOrRelativeValue()=default
Default constructor.
Wrapper class to store strings.
Definition String.hpp:101
TGUI_NODISCARD float toFloat(float defaultValue=0) const
Converts the string to a float.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:39
Helper class to create an AbsoluteOrRelativeValue object containing a relative value without using a ...
Definition AbsoluteOrRelativeValue.hpp:177