TGUI  0.9-dev
Layout.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_LAYOUT_HPP
27 #define TGUI_LAYOUT_HPP
28 
29 #include <TGUI/Config.hpp>
30 #include <TGUI/Vector2.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  Minimum,
61  Maximum,
62  BindingLeft,
63  BindingTop,
64  BindingWidth,
65  BindingHeight,
66  BindingInnerWidth,
67  BindingInnerHeight,
68  BindingString
69  };
70 
71 
73  public:
74 
78  Layout() = default;
79 
80 
86  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
87  Layout(T constant) :
88  m_value{static_cast<float>(constant)}
89  {
90  }
91 
92 
98  Layout(const char* expression) :
99  Layout{String{expression}}
100  {
101  }
102 
103 
109  Layout(String expression);
110 
111 
116  explicit Layout(Operation operation, Widget* boundWidget);
117 
118 
123  explicit Layout(Operation operation, std::unique_ptr<Layout> leftOperand, std::unique_ptr<Layout> rightOperand);
124 
125 
129  Layout(const Layout& other);
130 
134  Layout(Layout&& other);
135 
139  Layout& operator=(const Layout& other);
140 
144  Layout& operator=(Layout&& other);
145 
149  ~Layout();
150 
151 
157  float getValue() const
158  {
159  return m_value;
160  }
161 
162 
168  bool isConstant() const
169  {
170  return m_operation == Operation::Value;
171  }
172 
173 
180  String toString() const;
181 
182 
190  void connectWidget(Widget* widget, bool xAxis, std::function<void()> valueChangedCallbackHandler);
191 
192 
197  void unbindWidget();
198 
199 
205  void recalculateValue();
206 
207 
209  private:
210 
211 
213  // If a widget is bound, inform it that the layout no longer binds it
215  void unbindLayout();
216 
217 
219  // Resets the parent pointers of the left and right operands if they exist and tell the bound widget that this layout
220  // requires information about changes to its position or size when the operation requires a widget to be bound.
222  void resetPointers();
223 
224 
226  // Check whether sublayouts contain a string that refers to a widget which should be bound.
228  void parseBindingStringRecursive(Widget* widget, bool xAxis);
229 
230 
232  // Find the widget corresponding to the given name and bind it if found
234  void parseBindingString(const String& expression, Widget* widget, bool xAxis);
235 
236 
238  private:
239 
240  float m_value = 0;
241  Layout* m_parent = nullptr;
242  Operation m_operation = Operation::Value;
243  std::unique_ptr<Layout> m_leftOperand = nullptr; // The left operand of the operation in case the operation is a math operation
244  std::unique_ptr<Layout> m_rightOperand = nullptr; // The left operand of the operation in case the operation is a math operation
245  Widget* m_boundWidget = nullptr; // The widget on which this layout depends in case the operation is a binding
246  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
247  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
248 
250  };
251 
252 
258  class TGUI_API Layout2d
259  {
260  public:
261 
267  Layout2d(Vector2f constant = {0, 0}) :
268  x{constant.x},
269  y{constant.y}
270  {
271  }
272 
273 
280  Layout2d(Layout layoutX, Layout layoutY) :
281  x{std::move(layoutX)},
282  y{std::move(layoutY)}
283  {
284  }
285 
286 
294  Layout2d(const char* expression) :
295  x{expression},
296  y{expression}
297  {
298  }
299 
300 
308  Layout2d(const String& expression) :
309  x{expression},
310  y{expression}
311  {
312  }
313 
314 
321  {
322  return {x.getValue(), y.getValue()};
323  }
324 
325 
332  String toString() const
333  {
334  return "(" + x.toString() + ", " + y.toString() + ")";
335  }
336 
338  public:
339 
340  Layout x;
341  Layout y;
342  };
343 
344 
348  TGUI_API Layout operator-(Layout right);
349 
353  TGUI_API Layout operator+(Layout left, Layout right);
354 
358  TGUI_API Layout operator-(Layout left, Layout right);
359 
363  TGUI_API Layout operator*(Layout left, Layout right);
364 
368  TGUI_API Layout operator/(Layout left, Layout right);
369 
373  TGUI_API Layout2d operator-(Layout2d right);
374 
378  TGUI_API Layout2d operator+(Layout2d left, Layout2d right);
379 
383  TGUI_API Layout2d operator-(Layout2d left, Layout2d right);
384 
388  TGUI_API Layout2d operator*(Layout2d left, const Layout& right);
389 
393  TGUI_API Layout2d operator*(const Layout& left, Layout2d right);
394 
398  TGUI_API Layout2d operator/(Layout2d left, const Layout& right);
399 
400 
402 
403  inline namespace bind_functions
404  {
406  TGUI_API Layout bindLeft(std::shared_ptr<Widget> widget);
407 
409  TGUI_API Layout bindTop(std::shared_ptr<Widget> widget);
410 
412  TGUI_API Layout bindWidth(std::shared_ptr<Widget> widget);
413 
415  TGUI_API Layout bindHeight(std::shared_ptr<Widget> widget);
416 
418  TGUI_API Layout bindRight(std::shared_ptr<Widget> widget);
419 
421  TGUI_API Layout bindBottom(std::shared_ptr<Widget> widget);
422 
424  TGUI_API Layout2d bindPosition(std::shared_ptr<Widget> widget);
425 
427  TGUI_API Layout2d bindSize(std::shared_ptr<Widget> widget);
428 
430  TGUI_API Layout bindWidth(Gui& gui);
431 
433  TGUI_API Layout bindHeight(Gui& gui);
434 
436  TGUI_API Layout2d bindSize(Gui& gui);
437 
439  TGUI_API Layout bindMin(const Layout& value1, const Layout& value2);
440 
442  TGUI_API Layout bindMax(const Layout& value1, const Layout& value2);
443  }
444 
446 }
447 
449 
450 #endif // TGUI_LAYOUT_HPP
TGUI_API Layout bindMin(const Layout &value1, const Layout &value2)
Bind to the minimum value of two layouts.
TGUI_API Layout bindWidth(std::shared_ptr< Widget > widget)
Bind to the width of the widget.
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:35
Vector2f getValue() const
Returns the cached value of the layout.
Definition: Layout.hpp:320
Class to store the position or size of a widget.
Definition: Layout.hpp:258
Operation
The operation which the layout has to perform to find its value.
Definition: Layout.hpp:53
Layout2d(Layout layoutX, Layout layoutY)
Constructor to create the Layout2d from two Layout classes.
Definition: Layout.hpp:280
TGUI_API Layout2d bindPosition(std::shared_ptr< Widget > widget)
Bind to the position of the widget.
Wrapper class to store strings.
Definition: String.hpp:70
float getValue() const
Return the cached value of the layout.
Definition: Layout.hpp:157
TGUI_API Layout bindLeft(std::shared_ptr< Widget > widget)
Bind to the x position of the widget.
bool isConstant() const
Return whether the layout stores a constant value.
Definition: Layout.hpp:168
TGUI_API Layout bindBottom(std::shared_ptr< Widget > widget)
Bind to the bottom of the widget.
The parent class for every widget.
Definition: Widget.hpp:68
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:98
Layout(T constant)
Constructor to implicitly construct from numeric constant.
Definition: Layout.hpp:87
TGUI_API Layout bindRight(std::shared_ptr< Widget > widget)
Bind to the right position of the widget.
TGUI_API Layout bindMax(const Layout &value1, const Layout &value2)
Bind to the maximum value of two layouts.
Class to store the left, top, width or height of a widget.
Definition: Layout.hpp:48
Layout2d(const String &expression)
Constructs the Layout2d based on a string which will be parsed to determine the value of the layouts.
Definition: Layout.hpp:308
TGUI_API Layout bindHeight(std::shared_ptr< Widget > widget)
Bind to the height of the widget.
TGUI_API Layout2d bindSize(std::shared_ptr< Widget > widget)
Bind to the size of the widget.
TGUI_API Layout bindTop(std::shared_ptr< Widget > widget)
Bind to the y position of the widget.
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:294
Layout2d(Vector2f constant={0, 0})
Default constructor to implicitly construct from a tgui::Vector2f.
Definition: Layout.hpp:267