TGUI  0.8-dev
AbsoluteOrRelativeValue.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2017 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 
36 namespace tgui
37 {
43  class TGUI_API AbsoluteOrRelativeValue
44  {
45  public:
46 
50  TGUI_CONSTEXPR AbsoluteOrRelativeValue() = default;
51 
52 
58  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
59  TGUI_CONSTEXPR AbsoluteOrRelativeValue(T constant) :
60  m_constant {true},
61  m_value {static_cast<float>(constant)}
62  {
63  }
64 
65 
71  AbsoluteOrRelativeValue(const char* expression) :
72  AbsoluteOrRelativeValue{std::string{expression}}
73  {
74  }
75 
76 
82  AbsoluteOrRelativeValue(const std::string& expression)
83  {
84  if (!expression.empty() && (expression.back() == '%'))
85  {
86  m_constant = false;
87  m_ratio = tgui::stof(expression.substr(0, expression.length()-1)) / 100.f;
88  }
89  else
90  {
91  m_constant = true;
92  m_value = tgui::stof(expression.substr(0, expression.length()));
93  }
94  }
95 
96 
102  TGUI_CONSTEXPR float getValue() const
103  {
104  return m_value;
105  }
106 
107 
113  TGUI_CONSTEXPR float getRatio() const
114  {
115  return m_ratio;
116  }
117 
118 
124  TGUI_CONSTEXPR bool isConstant() const
125  {
126  return m_constant;
127  }
128 
129 
136  void updateParentSize(float newParentSize)
137  {
138  if (!m_constant)
139  {
140  m_parentValue = newParentSize;
141  m_value = m_ratio * newParentSize;
142  }
143  }
144 
145 
152  std::string toString() const
153  {
154  if (m_constant)
155  return to_string(m_value);
156  else
157  return to_string(m_ratio * 100) + '%';
158  }
159 
160 
162  protected:
163 
164  bool m_constant = true;
165  float m_value = 0;
166  float m_ratio = 0;
167  float m_parentValue = 0;
168  };
169 
170 
175  {
176  explicit TGUI_CONSTEXPR RelativeValue(float ratio)
177  {
178  m_constant = false;
179  m_ratio = ratio;
180  }
181  };
182 
184 }
185 
187 
188 #endif // TGUI_ABSOLUTE_OR_RELATIVE_VALUE_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
AbsoluteOrRelativeValue(const std::string &expression)
Construct the value from a string that either contains a value or a percentage.
Definition: AbsoluteOrRelativeValue.hpp:82
TGUI_CONSTEXPR float getRatio() const
Returns the stored ratio.
Definition: AbsoluteOrRelativeValue.hpp:113
Helper class to create an AbsoluteOrRelativeValue object containing a relative value without using a ...
Definition: AbsoluteOrRelativeValue.hpp:174
Class to store the a value that is either a constant or a ratio.
Definition: AbsoluteOrRelativeValue.hpp:43
AbsoluteOrRelativeValue(const char *expression)
Construct the value from a string that either contains a value or a percentage.
Definition: AbsoluteOrRelativeValue.hpp:71
TGUI_CONSTEXPR float getValue() const
Returns the value.
Definition: AbsoluteOrRelativeValue.hpp:102
TGUI_CONSTEXPR bool isConstant() const
Returns whether the value is constant or a ratio.
Definition: AbsoluteOrRelativeValue.hpp:124
TGUI_CONSTEXPR AbsoluteOrRelativeValue(T constant)
Constructor to set constant.
Definition: AbsoluteOrRelativeValue.hpp:59