TGUI  0.8-dev
Layout.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_LAYOUT_HPP
27 #define TGUI_LAYOUT_HPP
28 
29 #include <TGUI/Config.hpp>
30 #include <TGUI/to_string.hpp>
31 #include <SFML/System/Vector2.hpp>
32 #include <type_traits>
33 #include <string>
34 
36 
37 namespace tgui
38 {
45  class TGUI_API Layout
46  {
47  public:
48 
53  TGUI_CONSTEXPR Layout() = default;
54 
55 
62  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
63  TGUI_CONSTEXPR Layout(T constant) :
64  m_constant{true},
65  m_value {static_cast<float>(constant)}
66  {
67  }
68 
69 
76  Layout(const char* expression) :
77  Layout{std::string{expression}}
78  {
79  }
80 
81 
88  Layout(const std::string& expression)
89  {
90  if (!expression.empty())
91  {
92  if (expression.back() == '%')
93  {
94  m_constant = false;
95  m_ratio = std::stof(expression.substr(0, expression.length()-1)) / 100.f;
96  }
97  else
98  m_value = std::stof(expression.substr(0, expression.length()));
99  }
100  }
101 
102 
109  TGUI_CONSTEXPR float getValue() const
110  {
111  return m_value;
112  }
113 
114 
122  TGUI_CONSTEXPR bool isConstant() const
123  {
124  return m_constant;
125  }
126 
127 
137  TGUI_CONSTEXPR float getRatio() const
138  {
139  return m_ratio;
140  }
141 
142 
150  TGUI_CONSTEXPR void updateParentSize(float newParentSize)
151  {
152  if (!m_constant)
153  {
154  m_parentValue = newParentSize;
155  m_value = m_ratio * newParentSize;
156  }
157  }
158 
159 
167  std::string toString() const
168  {
169  if (m_constant)
170  return to_string(m_value);
171  else
172  return to_string(m_ratio * 100) + '%';
173  }
174 
176  protected:
177 
178  bool m_constant = true;
179  float m_value = 0;
180  float m_ratio = 0;
181  float m_parentValue = 0;
182  };
183 
184 
189  struct TGUI_API RelLayout : Layout
190  {
191  explicit TGUI_CONSTEXPR RelLayout(float constant)
192  {
193  m_constant = false;
194  m_ratio = constant;
195  }
196  };
197 
198 
205  class TGUI_API Layout2d
206  {
207  public:
208 
215  Layout2d(sf::Vector2f constant = {0, 0}) :
216  x{constant.x},
217  y{constant.y}
218  {
219  }
220 
221 
229  TGUI_CONSTEXPR Layout2d(Layout layoutX, Layout layoutY) :
230  x{std::move(layoutX)},
231  y{std::move(layoutY)}
232  {
233  }
234 
235 
242  sf::Vector2f getValue() const
243  {
244  return {x.getValue(), y.getValue()};
245  }
246 
247 
255  void updateParentSize(sf::Vector2f newParentSize)
256  {
257  x.updateParentSize(newParentSize.x);
258  y.updateParentSize(newParentSize.y);
259  }
260 
261 
269  std::string toString() const
270  {
271  return "(" + x.toString() + ", " + y.toString() + ")";
272  }
273 
274 
276  public:
277  Layout x;
278  Layout y;
279  };
280 
282 }
283 
285 
286 #endif // TGUI_LAYOUT_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
TGUI_CONSTEXPR Layout2d(Layout layoutX, Layout layoutY)
Constructor to create the Layout2d from two Layout classes.
Definition: Layout.hpp:229
TGUI_CONSTEXPR Layout(T constant)
Constructor to implicitly construct from numeric constant.
Definition: Layout.hpp:63
Class to store the position or size of a widget.
Definition: Layout.hpp:205
sf::Vector2f getValue() const
Returns the cached value of the layout.
Definition: Layout.hpp:242
Layout2d(sf::Vector2f constant={0, 0})
Default constructor to implicitly construct from an sf::Vector2f.
Definition: Layout.hpp:215
Class to store the left, top, width or height of something.
Definition: Layout.hpp:45
Layout(const char *expression)
Construct the layout based on a string which will be parsed to determine the value of the layout...
Definition: Layout.hpp:76
TGUI_CONSTEXPR float getValue() const
Returns the cached value of the layout.
Definition: Layout.hpp:109
Layout(const std::string &expression)
Construct the layout based on a string which will be parsed to determine the value of the layout...
Definition: Layout.hpp:88
Class to create a relative layout without using a string.
Definition: Layout.hpp:189