TGUI  1.6.1
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#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#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
32 #include <type_traits>
33#endif
34
36
37TGUI_MODULE_EXPORT namespace tgui
38{
45 {
46 public:
47
51 constexpr AbsoluteOrRelativeValue() = default;
52
58 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
59 constexpr AbsoluteOrRelativeValue(T constant) :
60 m_constant {true},
61 m_value {static_cast<float>(constant)}
62 {
63 }
64
70 AbsoluteOrRelativeValue(const char* expression) :
72 {
73 }
74
81 {
82 if (!expression.empty() && (expression.back() == '%'))
83 {
84 m_constant = false;
85 m_ratio = expression.substr(0, expression.length()-1).toFloat() / 100.f;
86 }
87 else
88 {
89 m_constant = true;
90 m_value = expression.substr(0, expression.length()).toFloat();
91 }
92 }
93
99 TGUI_NODISCARD constexpr float getValue() const
100 {
101 return m_value;
102 }
103
109 TGUI_NODISCARD constexpr float getRatio() const
110 {
111 return m_ratio;
112 }
113
119 TGUI_NODISCARD constexpr bool isConstant() const
120 {
121 return m_constant;
122 }
123
130 constexpr void updateParentSize(float newParentSize)
131 {
132 if (!m_constant)
133 {
134 m_parentValue = newParentSize;
135 m_value = m_ratio * newParentSize;
136 }
137 }
138
145 TGUI_NODISCARD String toString() const
146 {
147 if (m_constant)
148 return String::fromNumber(m_value);
149 else
150 return String::fromNumber(m_ratio * 100) + '%';
151 }
152
154 protected:
155
156 bool m_constant = true;
157 float m_value = 0;
158 float m_ratio = 0;
159 float m_parentValue = 0;
160 };
161
166 {
171 explicit constexpr RelativeValue(float ratio)
172 {
173 m_constant = false;
174 m_ratio = ratio;
175 }
176 };
177
179}
180
182
183#endif // TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
Class to store the a value that is either a constant or a ratio.
Definition AbsoluteOrRelativeValue.hpp:45
constexpr float getRatio() const
Returns the stored ratio.
Definition AbsoluteOrRelativeValue.hpp:109
constexpr bool isConstant() const
Returns whether the value is constant or a ratio.
Definition AbsoluteOrRelativeValue.hpp:119
AbsoluteOrRelativeValue(const char *expression)
Construct the value from a string that either contains a value or a percentage.
Definition AbsoluteOrRelativeValue.hpp:70
constexpr AbsoluteOrRelativeValue(T constant)
Constructor to set constant.
Definition AbsoluteOrRelativeValue.hpp:59
AbsoluteOrRelativeValue(const String &expression)
Construct the value from a string that either contains a value or a percentage.
Definition AbsoluteOrRelativeValue.hpp:80
constexpr float getValue() const
Returns the value.
Definition AbsoluteOrRelativeValue.hpp:99
constexpr AbsoluteOrRelativeValue()=default
Default constructor.
Wrapper class to store strings.
Definition String.hpp:96
float toFloat(float defaultValue=0) const
Converts the string to a float.
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
Helper class to create an AbsoluteOrRelativeValue object containing a relative value without using a ...
Definition AbsoluteOrRelativeValue.hpp:166
constexpr RelativeValue(float ratio)
Default constructor that sets the given ratio.
Definition AbsoluteOrRelativeValue.hpp:171