TGUI  0.8.0
Layout.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2018 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/Vector2f.hpp>
31 #include <type_traits>
32 #include <functional>
33 #include <memory>
34 #include <string>
35 
37 
38 namespace tgui
39 {
40  class Gui;
41  class Widget;
42 
48  class TGUI_API Layout
49  {
50  public:
51 
53  enum class Operation
54  {
55  Value,
56  Plus,
57  Minus,
58  Multiplies,
59  Divides,
60  BindingLeft,
61  BindingTop,
62  BindingWidth,
63  BindingHeight,
64  BindingString
65  };
66 
67 
69  public:
70 
74  Layout() = default;
75 
76 
82  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
83  Layout(T constant) :
84  m_value{static_cast<float>(constant)}
85  {
86  }
87 
88 
94  Layout(const char* expression) :
95  Layout{std::string{expression}}
96  {
97  }
98 
99 
105  Layout(std::string expression);
106 
107 
112  explicit Layout(Operation operation, Widget* boundWidget);
113 
114 
119  explicit Layout(Operation operation, std::unique_ptr<Layout> leftOperand, std::unique_ptr<Layout> rightOperand);
120 
121 
125  Layout(const Layout& other);
126 
130  Layout(Layout&& other);
131 
135  Layout& operator=(const Layout& other);
136 
140  Layout& operator=(Layout&& other);
141 
145  ~Layout();
146 
147 
153  float getValue() const
154  {
155  return m_value;
156  }
157 
158 
165  std::string toString() const;
166 
167 
175  void connectWidget(Widget* widget, bool xAxis, std::function<void()> valueChangedCallbackHandler);
176 
177 
182  void unbindWidget();
183 
184 
190  void recalculateValue();
191 
192 
194  private:
195 
196 
198  // If a widget is bound, inform it that the layout no longer binds it
200  void unbindLayout();
201 
202 
204  // Resets the parent pointers of the left and right operands if they exist and tell the bound widget that this layout
205  // requires information about changes to its position or size when the operation requires a widget to be bound.
207  void resetPointers();
208 
209 
211  // Check whether sublayouts contain a string that refers to a widget which should be bound.
213  void parseBindingStringRecursive(Widget* widget, bool xAxis);
214 
215 
217  // Find the widget corresponding to the given name and bind it if found
219  void parseBindingString(const std::string& expression, Widget* widget, bool xAxis);
220 
221 
223  private:
224 
225  float m_value = 0;
226  Layout* m_parent = nullptr;
227  Operation m_operation = Operation::Value;
228  std::unique_ptr<Layout> m_leftOperand = nullptr; // The left operand of the operation in case the operation is a math operation
229  std::unique_ptr<Layout> m_rightOperand = nullptr; // The left operand of the operation in case the operation is a math operation
230  Widget* m_boundWidget = nullptr; // The widget on which this layout depends in case the operation is a binding
231  std::string m_boundString; // String referring to a widget on which this layout depends in case the layout was created from a string and contains a binding operation
232  std::function<void()> m_connectedWidgetCallback = nullptr; // Function to call when the value of the layout changes in case the layout and sublayouts are not all constants
233 
235  };
236 
237 
243  class TGUI_API Layout2d
244  {
245  public:
246 
252  Layout2d(Vector2f constant = {0, 0}) :
253  x{constant.x},
254  y{constant.y}
255  {
256  }
257 
258 
264  Layout2d(sf::Vector2f constant) :
265  x{constant.x},
266  y{constant.y}
267  {
268  }
269 
270 
277  Layout2d(Layout layoutX, Layout layoutY) :
278  x{std::move(layoutX)},
279  y{std::move(layoutY)}
280  {
281  }
282 
283 
291  Layout2d(const char* expression) :
292  x{expression},
293  y{expression}
294  {
295  }
296 
297 
305  Layout2d(const std::string& expression) :
306  x{expression},
307  y{expression}
308  {
309  }
310 
311 
318  {
319  return {x.getValue(), y.getValue()};
320  }
321 
322 
329  std::string toString() const
330  {
331  return "(" + x.toString() + ", " + y.toString() + ")";
332  }
333 
335  public:
336 
337  Layout x;
338  Layout y;
339  };
340 
341 
345  TGUI_API Layout operator-(Layout right);
346 
350  TGUI_API Layout operator+(Layout left, Layout right);
351 
355  TGUI_API Layout operator-(Layout left, Layout right);
356 
360  TGUI_API Layout operator*(Layout left, Layout right);
361 
365  TGUI_API Layout operator/(Layout left, Layout right);
366 
370  TGUI_API Layout2d operator-(Layout2d right);
371 
375  TGUI_API Layout2d operator+(Layout2d left, Layout2d right);
376 
380  TGUI_API Layout2d operator-(Layout2d left, Layout2d right);
381 
385  TGUI_API Layout2d operator*(Layout2d left, const Layout& right);
386 
390  TGUI_API Layout2d operator*(const Layout& left, Layout2d right);
391 
395  TGUI_API Layout2d operator/(Layout2d left, const Layout& right);
396 
397 
399 
400  inline namespace bind_functions
401  {
403  TGUI_API Layout bindLeft(std::shared_ptr<Widget> widget);
404 
406  TGUI_API Layout bindTop(std::shared_ptr<Widget> widget);
407 
409  TGUI_API Layout bindWidth(std::shared_ptr<Widget> widget);
410 
412  TGUI_API Layout bindHeight(std::shared_ptr<Widget> widget);
413 
415  TGUI_API Layout bindRight(std::shared_ptr<Widget> widget);
416 
418  TGUI_API Layout bindBottom(std::shared_ptr<Widget> widget);
419 
421  TGUI_API Layout2d bindPosition(std::shared_ptr<Widget> widget);
422 
424  TGUI_API Layout2d bindSize(std::shared_ptr<Widget> widget);
425 
427  TGUI_API Layout bindWidth(Gui& gui);
428 
430  TGUI_API Layout bindHeight(Gui& gui);
431 
433  TGUI_API Layout2d bindSize(Gui& gui);
434  }
435 
437 }
438 
440 
441 #endif // TGUI_LAYOUT_HPP
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
Layout2d(Layout layoutX, Layout layoutY)
Constructor to create the Layout2d from two Layout classes.
Definition: Layout.hpp:277
Class to store the position or size of a widget.
Definition: Layout.hpp:243
The parent class for every widget.
Definition: Widget.hpp:72
Layout(T constant)
Constructor to implicitly construct from numeric constant.
Definition: Layout.hpp:83
Definition: Vector2f.hpp:37
Class to store the left, top, width or height of a widget.
Definition: Layout.hpp:48
Layout(const char *expression)
Constructs the layout based on a string which will be parsed to determine the value of the layout...
Definition: Layout.hpp:94
Layout2d(sf::Vector2f constant)
Default constructor to implicitly construct from a sf::Vector2f.
Definition: Layout.hpp:264
Layout2d(Vector2f constant={0, 0})
Default constructor to implicitly construct from a tgui::Vector2f.
Definition: Layout.hpp:252
TGUI_API Layout operator*(Layout left, Layout right)
operator for the Layout class
Layout2d(const char *expression)
Constructs the Layout2d based on a string which will be parsed to determine the value of the layouts...
Definition: Layout.hpp:291
TGUI_API Layout operator+(Layout left, Layout right)
operator for the Layout class
float getValue() const
Return the cached value of the layout.
Definition: Layout.hpp:153
TGUI_API Layout operator/(Layout left, Layout right)
/ operator for the Layout class
Operation
The operation which the layout has to perform to find its value.
Definition: Layout.hpp:53
TGUI_API Layout operator-(Layout right)
Unary minus operator for the Layout class.
Vector2f getValue() const
Returns the cached value of the layout.
Definition: Layout.hpp:317
Layout2d(const std::string &expression)
Constructs the Layout2d based on a string which will be parsed to determine the value of the layouts...
Definition: Layout.hpp:305