TGUI  0.8.2
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  BindingInnerWidth,
65  BindingInnerHeight,
66  BindingString
67  };
68 
69 
71  public:
72 
76  Layout() = default;
77 
78 
84  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
85  Layout(T constant) :
86  m_value{static_cast<float>(constant)}
87  {
88  }
89 
90 
96  Layout(const char* expression) :
97  Layout{std::string{expression}}
98  {
99  }
100 
101 
107  Layout(std::string expression);
108 
109 
114  explicit Layout(Operation operation, Widget* boundWidget);
115 
116 
121  explicit Layout(Operation operation, std::unique_ptr<Layout> leftOperand, std::unique_ptr<Layout> rightOperand);
122 
123 
127  Layout(const Layout& other);
128 
132  Layout(Layout&& other);
133 
137  Layout& operator=(const Layout& other);
138 
142  Layout& operator=(Layout&& other);
143 
147  ~Layout();
148 
149 
155  float getValue() const
156  {
157  return m_value;
158  }
159 
160 
167  std::string toString() const;
168 
169 
177  void connectWidget(Widget* widget, bool xAxis, std::function<void()> valueChangedCallbackHandler);
178 
179 
184  void unbindWidget();
185 
186 
192  void recalculateValue();
193 
194 
196  private:
197 
198 
200  // If a widget is bound, inform it that the layout no longer binds it
202  void unbindLayout();
203 
204 
206  // Resets the parent pointers of the left and right operands if they exist and tell the bound widget that this layout
207  // requires information about changes to its position or size when the operation requires a widget to be bound.
209  void resetPointers();
210 
211 
213  // Check whether sublayouts contain a string that refers to a widget which should be bound.
215  void parseBindingStringRecursive(Widget* widget, bool xAxis);
216 
217 
219  // Find the widget corresponding to the given name and bind it if found
221  void parseBindingString(const std::string& expression, Widget* widget, bool xAxis);
222 
223 
225  private:
226 
227  float m_value = 0;
228  Layout* m_parent = nullptr;
229  Operation m_operation = Operation::Value;
230  std::unique_ptr<Layout> m_leftOperand = nullptr; // The left operand of the operation in case the operation is a math operation
231  std::unique_ptr<Layout> m_rightOperand = nullptr; // The left operand of the operation in case the operation is a math operation
232  Widget* m_boundWidget = nullptr; // The widget on which this layout depends in case the operation is a binding
233  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
234  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
235 
237  };
238 
239 
245  class TGUI_API Layout2d
246  {
247  public:
248 
254  Layout2d(Vector2f constant = {0, 0}) :
255  x{constant.x},
256  y{constant.y}
257  {
258  }
259 
260 
266  Layout2d(sf::Vector2f constant) :
267  x{constant.x},
268  y{constant.y}
269  {
270  }
271 
272 
279  Layout2d(Layout layoutX, Layout layoutY) :
280  x{std::move(layoutX)},
281  y{std::move(layoutY)}
282  {
283  }
284 
285 
293  Layout2d(const char* expression) :
294  x{expression},
295  y{expression}
296  {
297  }
298 
299 
307  Layout2d(const std::string& expression) :
308  x{expression},
309  y{expression}
310  {
311  }
312 
313 
320  {
321  return {x.getValue(), y.getValue()};
322  }
323 
324 
331  std::string toString() const
332  {
333  return "(" + x.toString() + ", " + y.toString() + ")";
334  }
335 
337  public:
338 
339  Layout x;
340  Layout y;
341  };
342 
343 
347  TGUI_API Layout operator-(Layout right);
348 
352  TGUI_API Layout operator+(Layout left, Layout right);
353 
357  TGUI_API Layout operator-(Layout left, Layout right);
358 
362  TGUI_API Layout operator*(Layout left, Layout right);
363 
367  TGUI_API Layout operator/(Layout left, Layout right);
368 
372  TGUI_API Layout2d operator-(Layout2d right);
373 
377  TGUI_API Layout2d operator+(Layout2d left, Layout2d right);
378 
382  TGUI_API Layout2d operator-(Layout2d left, Layout2d right);
383 
387  TGUI_API Layout2d operator*(Layout2d left, const Layout& right);
388 
392  TGUI_API Layout2d operator*(const Layout& left, Layout2d right);
393 
397  TGUI_API Layout2d operator/(Layout2d left, const Layout& right);
398 
399 
401 
402  inline namespace bind_functions
403  {
405  TGUI_API Layout bindLeft(std::shared_ptr<Widget> widget);
406 
408  TGUI_API Layout bindTop(std::shared_ptr<Widget> widget);
409 
411  TGUI_API Layout bindWidth(std::shared_ptr<Widget> widget);
412 
414  TGUI_API Layout bindHeight(std::shared_ptr<Widget> widget);
415 
417  TGUI_API Layout bindRight(std::shared_ptr<Widget> widget);
418 
420  TGUI_API Layout bindBottom(std::shared_ptr<Widget> widget);
421 
423  TGUI_API Layout2d bindPosition(std::shared_ptr<Widget> widget);
424 
426  TGUI_API Layout2d bindSize(std::shared_ptr<Widget> widget);
427 
429  TGUI_API Layout bindWidth(Gui& gui);
430 
432  TGUI_API Layout bindHeight(Gui& gui);
433 
435  TGUI_API Layout2d bindSize(Gui& gui);
436  }
437 
439 }
440 
442 
443 #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:279
Class to store the position or size of a widget.
Definition: Layout.hpp:245
The parent class for every widget.
Definition: Widget.hpp:72
Layout(T constant)
Constructor to implicitly construct from numeric constant.
Definition: Layout.hpp:85
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:96
Layout2d(sf::Vector2f constant)
Default constructor to implicitly construct from a sf::Vector2f.
Definition: Layout.hpp:266
Layout2d(Vector2f constant={0, 0})
Default constructor to implicitly construct from a tgui::Vector2f.
Definition: Layout.hpp:254
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:293
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:155
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:319
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:307