TGUI
=
=
Download
Tutorials
1.x
0.9
0.8
Documentation
1.x
0.9
0.8
Examples
1.x
0.9
0.8
Community
TGUI
1.x-dev
Toggle main menu visibility
Loading...
Searching...
No Matches
include
TGUI
AbsoluteOrRelativeValue.hpp
1
2
//
3
// TGUI - Texus' Graphical User Interface
4
// Copyright (C) 2012-2026 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
30
#include <TGUI/String.hpp>
31
32
#include <type_traits>
33
35
36
namespace
tgui
37
{
43
class
TGUI_API
AbsoluteOrRelativeValue
44
{
45
public
:
49
constexpr
AbsoluteOrRelativeValue
() =
default
;
50
56
template
<
typename
T,
typename
=
typename
std::enable_if_t<std::is_arithmetic_v<T>, 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) :
69
AbsoluteOrRelativeValue
{
String
{expression}}
70
{
71
}
72
78
AbsoluteOrRelativeValue
(
const
String
& expression)
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
[[nodiscard]]
constexpr
float
getValue
()
const
98
{
99
return
m_value;
100
}
101
107
[[nodiscard]]
constexpr
float
getRatio
()
const
108
{
109
return
m_ratio;
110
}
111
117
[[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
[[nodiscard]] String toString()
const
144
{
145
if
(m_constant)
146
return
String::fromNumber(m_value);
147
return
String::fromNumber(m_ratio * 100) +
'%'
;
148
}
149
151
152
protected
:
153
bool
m_constant =
true
;
154
float
m_value = 0;
155
float
m_ratio = 0;
156
float
m_parentValue = 0;
157
};
158
162
struct
TGUI_API
RelativeValue
:
AbsoluteOrRelativeValue
163
{
168
explicit
constexpr
RelativeValue
(
float
ratio)
169
{
170
m_constant =
false
;
171
m_ratio = ratio;
172
}
173
};
174
176
177
// Allow writing 50_percent instead of RelativeValue(0.5) after adding "using namespace tgui::literals::percent;"
178
// This can also be used in layouts, where you can replace e.g. the "40%" string with 40_percent to skip string parsing
179
inline
namespace
literals
180
{
181
inline
namespace
percent
182
{
183
constexpr
RelativeValue
operator
""
_percent(
long
double
n)
184
{
185
return
RelativeValue{
static_cast<
float
>
(n / 100.0L)};
186
}
187
188
constexpr
RelativeValue
operator
""
_percent(
unsigned
long
long
n)
189
{
190
return
RelativeValue{
static_cast<
float
>
(n) / 100.f};
191
}
192
}
// namespace percent
193
}
// namespace literals
194
}
// namespace tgui
195
196
#endif
// TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
tgui::AbsoluteOrRelativeValue::getRatio
constexpr float getRatio() const
Returns the stored ratio.
Definition
AbsoluteOrRelativeValue.hpp:107
tgui::AbsoluteOrRelativeValue::isConstant
constexpr bool isConstant() const
Returns whether the value is constant or a ratio.
Definition
AbsoluteOrRelativeValue.hpp:117
tgui::AbsoluteOrRelativeValue::AbsoluteOrRelativeValue
AbsoluteOrRelativeValue(const char *expression)
Construct the value from a string that either contains a value or a percentage.
Definition
AbsoluteOrRelativeValue.hpp:68
tgui::AbsoluteOrRelativeValue::AbsoluteOrRelativeValue
constexpr AbsoluteOrRelativeValue(T constant)
Constructor to set constant.
Definition
AbsoluteOrRelativeValue.hpp:57
tgui::AbsoluteOrRelativeValue::AbsoluteOrRelativeValue
AbsoluteOrRelativeValue(const String &expression)
Construct the value from a string that either contains a value or a percentage.
Definition
AbsoluteOrRelativeValue.hpp:78
tgui::AbsoluteOrRelativeValue::getValue
constexpr float getValue() const
Returns the value.
Definition
AbsoluteOrRelativeValue.hpp:97
tgui::AbsoluteOrRelativeValue::AbsoluteOrRelativeValue
constexpr AbsoluteOrRelativeValue()=default
Default constructor.
tgui::String
Wrapper class to store strings.
Definition
String.hpp:94
tgui::String::toFloat
float toFloat(float defaultValue=0) const
Converts the string to a float.
tgui
Namespace that contains all TGUI functions and classes.
Definition
AbsoluteOrRelativeValue.hpp:37
tgui::RelativeValue::RelativeValue
constexpr RelativeValue(float ratio)
Default constructor that sets the given ratio.
Definition
AbsoluteOrRelativeValue.hpp:168
Generated on
for TGUI by
1.18.0