TGUI  0.8.5
Layout.hpp
1 //
3 // TGUI - Texus' Graphical User Interface
4 // Copyright (C) 2012-2019 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  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{std::string{expression}}
100  {
101  }
102 
103 
109  Layout(std::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  std::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 std::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  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
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 
279  Layout2d(sf::Vector2f constant) :
280  x{constant.x},
281  y{constant.y}
282  {
283  }
284 
285 
292  Layout2d(Layout layoutX, Layout layoutY) :
293  x{std::move(layoutX)},
294  y{std::move(layoutY)}
295  {
296  }
297 
298 
306  Layout2d(const char* expression) :
307  x{expression},
308  y{expression}
309  {
310  }
311 
312 
320  Layout2d(const std::string& expression) :
321  x{expression},
322  y{expression}
323  {
324  }
325 
326 
333  {
334  return {x.getValue(), y.getValue()};
335  }
336 
337 
344  std::string toString() const
345  {
346  return "(" + x.toString() + ", " + y.toString() + ")";
347  }
348 
350  public:
351 
352  Layout x;
353  Layout y;
354  };
355 
356 
360  TGUI_API Layout operator-(Layout right);
361 
365  TGUI_API Layout operator+(Layout left, Layout right);
366 
370  TGUI_API Layout operator-(Layout left, Layout right);
371 
375  TGUI_API Layout operator*(Layout left, Layout right);
376 
380  TGUI_API Layout operator/(Layout left, Layout right);
381 
385  TGUI_API Layout2d operator-(Layout2d right);
386 
390  TGUI_API Layout2d operator+(Layout2d left, Layout2d right);
391 
395  TGUI_API Layout2d operator-(Layout2d left, Layout2d right);
396 
400  TGUI_API Layout2d operator*(Layout2d left, const Layout& right);
401 
405  TGUI_API Layout2d operator*(const Layout& left, Layout2d right);
406 
410  TGUI_API Layout2d operator/(Layout2d left, const Layout& right);
411 
412 
414 
415  inline namespace bind_functions
416  {
418  TGUI_API Layout bindLeft(std::shared_ptr<Widget> widget);
419 
421  TGUI_API Layout bindTop(std::shared_ptr<Widget> widget);
422 
424  TGUI_API Layout bindWidth(std::shared_ptr<Widget> widget);
425 
427  TGUI_API Layout bindHeight(std::shared_ptr<Widget> widget);
428 
430  TGUI_API Layout bindRight(std::shared_ptr<Widget> widget);
431 
433  TGUI_API Layout bindBottom(std::shared_ptr<Widget> widget);
434 
436  TGUI_API Layout2d bindPosition(std::shared_ptr<Widget> widget);
437 
439  TGUI_API Layout2d bindSize(std::shared_ptr<Widget> widget);
440 
442  TGUI_API Layout bindWidth(Gui& gui);
443 
445  TGUI_API Layout bindHeight(Gui& gui);
446 
448  TGUI_API Layout2d bindSize(Gui& gui);
449 
451  TGUI_API Layout bindMin(const Layout& value1, const Layout& value2);
452 
454  TGUI_API Layout bindMax(const Layout& value1, const Layout& value2);
455  }
456 
458 }
459 
461 
462 #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:292
Class to store the position or size of a widget.
Definition: Layout.hpp:258
The parent class for every widget.
Definition: Widget.hpp:72
Layout(T constant)
Constructor to implicitly construct from numeric constant.
Definition: Layout.hpp:87
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:98
Layout2d(sf::Vector2f constant)
Default constructor to implicitly construct from a sf::Vector2f.
Definition: Layout.hpp:279
Layout2d(Vector2f constant={0, 0})
Default constructor to implicitly construct from a tgui::Vector2f.
Definition: Layout.hpp:267
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:306
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:157
TGUI_API Layout operator/(Layout left, Layout right)
/ operator for the Layout class
bool isConstant() const
Return whether the layout stores a constant value.
Definition: Layout.hpp:168
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:332
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:320