TGUI  0.9-dev
AbsoluteOrRelativeValue.hpp
1 //
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/String.hpp>
31 #include <type_traits>
32 
34 
35 namespace tgui
36 {
42  class TGUI_API AbsoluteOrRelativeValue
43  {
44  public:
45 
49  TGUI_CONSTEXPR AbsoluteOrRelativeValue()
50  {
51  // Constructor isn't defined as "= default" because this leads to an IntelliSense error
52  }
53 
54 
60  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
61  TGUI_CONSTEXPR AbsoluteOrRelativeValue(T constant) :
62  m_constant {true},
63  m_value {static_cast<float>(constant)}
64  {
65  }
66 
67 
73  AbsoluteOrRelativeValue(const char* expression) :
74  AbsoluteOrRelativeValue{String{expression}}
75  {
76  }
77 
78 
84  AbsoluteOrRelativeValue(const String& expression)
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_CONSTEXPR float getValue() const
105  {
106  return m_value;
107  }
108 
109 
115  TGUI_CONSTEXPR float getRatio() const
116  {
117  return m_ratio;
118  }
119 
120 
126  TGUI_CONSTEXPR bool isConstant() const
127  {
128  return m_constant;
129  }
130 
131 
138  TGUI_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  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 TGUI_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
TGUI_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
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
Helper class to create an AbsoluteOrRelativeValue object containing a relative value without using a ...
Definition: AbsoluteOrRelativeValue.hpp:176
TGUI_CONSTEXPR bool isConstant() const
Returns whether the value is constant or a ratio.
Definition: AbsoluteOrRelativeValue.hpp:126
TGUI_CONSTEXPR AbsoluteOrRelativeValue(T constant)
Constructor to set constant.
Definition: AbsoluteOrRelativeValue.hpp:61
float toFloat(float defaultValue=0) const
Converts the string to a float.
Class to store the a value that is either a constant or a ratio.
Definition: AbsoluteOrRelativeValue.hpp:42
Wrapper class to store strings.
Definition: String.hpp:70
AbsoluteOrRelativeValue(const String &expression)
Construct the value from a string that either contains a value or a percentage.
Definition: AbsoluteOrRelativeValue.hpp:84
TGUI_CONSTEXPR float getValue() const
Returns the value.
Definition: AbsoluteOrRelativeValue.hpp:104
TGUI_CONSTEXPR AbsoluteOrRelativeValue()
Default constructor.
Definition: AbsoluteOrRelativeValue.hpp:49
static String fromNumber(T value)
Construct the string from a number.
Definition: String.hpp:174