TGUI  0.8.9
AbsoluteOrRelativeValue.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2020 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/to_string.hpp>
31#include <type_traits>
32#include <string>
33
35
36namespace tgui
37{
44 {
45 public:
46
50 TGUI_CONSTEXPR AbsoluteOrRelativeValue()
51 {
52 // Constructor isn't defined as "= default" because this leads to an IntelliSense error
53 }
54
55
61 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
62 TGUI_CONSTEXPR AbsoluteOrRelativeValue(T constant) :
63 m_constant {true},
64 m_value {static_cast<float>(constant)}
65 {
66 }
67
68
74 AbsoluteOrRelativeValue(const char* expression) :
75 AbsoluteOrRelativeValue{std::string{expression}}
76 {
77 }
78
79
85 AbsoluteOrRelativeValue(const std::string& expression)
86 {
87 if (!expression.empty() && (expression.back() == '%'))
88 {
89 m_constant = false;
90 m_ratio = strToFloat(expression.substr(0, expression.length()-1)) / 100.f;
91 }
92 else
93 {
94 m_constant = true;
95 m_value = strToFloat(expression.substr(0, expression.length()));
96 }
97 }
98
99
105 TGUI_CONSTEXPR float getValue() const
106 {
107 return m_value;
108 }
109
110
116 TGUI_CONSTEXPR float getRatio() const
117 {
118 return m_ratio;
119 }
120
121
127 TGUI_CONSTEXPR bool isConstant() const
128 {
129 return m_constant;
130 }
131
132
139 void updateParentSize(float newParentSize)
140 {
141 if (!m_constant)
142 {
143 m_parentValue = newParentSize;
144 m_value = m_ratio * newParentSize;
145 }
146 }
147
148
155 std::string toString() const
156 {
157 if (m_constant)
158 return to_string(m_value);
159 else
160 return to_string(m_ratio * 100) + '%';
161 }
162
163
165 protected:
166
167 bool m_constant = true;
168 float m_value = 0;
169 float m_ratio = 0;
170 float m_parentValue = 0;
171 };
172
173
178 {
179 explicit TGUI_CONSTEXPR RelativeValue(float ratio)
180 {
181 m_constant = false;
182 m_ratio = ratio;
183 }
184 };
185
187}
188
190
191#endif // TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
Class to store the a value that is either a constant or a ratio.
Definition: AbsoluteOrRelativeValue.hpp:44
TGUI_CONSTEXPR float getValue() const
Returns the value.
Definition: AbsoluteOrRelativeValue.hpp:105
AbsoluteOrRelativeValue(const char *expression)
Construct the value from a string that either contains a value or a percentage.
Definition: AbsoluteOrRelativeValue.hpp:74
TGUI_CONSTEXPR float getRatio() const
Returns the stored ratio.
Definition: AbsoluteOrRelativeValue.hpp:116
TGUI_CONSTEXPR bool isConstant() const
Returns whether the value is constant or a ratio.
Definition: AbsoluteOrRelativeValue.hpp:127
TGUI_CONSTEXPR AbsoluteOrRelativeValue()
Default constructor.
Definition: AbsoluteOrRelativeValue.hpp:50
AbsoluteOrRelativeValue(const std::string &expression)
Construct the value from a string that either contains a value or a percentage.
Definition: AbsoluteOrRelativeValue.hpp:85
TGUI_CONSTEXPR AbsoluteOrRelativeValue(T constant)
Constructor to set constant.
Definition: AbsoluteOrRelativeValue.hpp:62
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
TGUI_API float strToFloat(const std::string &str, float defaultValue=0)
Converts a string to a float.
Helper class to create an AbsoluteOrRelativeValue object containing a relative value without using a ...
Definition: AbsoluteOrRelativeValue.hpp:178