TGUI  0.7.4
Layout.hpp
1 //
3 // TGUI - Texus's 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 <SFML/System/Vector2.hpp>
30 #include <TGUI/Config.hpp>
31 #include <functional>
32 #include <memory>
33 #include <vector>
34 #include <set>
35 
37 
38 namespace tgui
39 {
40  class Gui;
41  class Widget;
42  class Layout;
43 
48  class TGUI_API LayoutImpl : public std::enable_shared_from_this<LayoutImpl>
49  {
50  public:
51 
53  enum class Operation
54  {
55  Value,
56  String,
57  Plus,
58  Minus,
59  Multiplies,
60  Divides,
61  Modulus,
62  And,
63  Or,
64  LessThan,
65  LessOrEqual,
66  GreaterThan,
67  GreaterOrEqual,
68  Equal,
69  NotEqual,
70  Minimum,
71  Maximum,
72  Conditional
73  };
74 
75 
77  public:
78 
80  ~LayoutImpl();
81 
83  void recalculate();
84 
85 
87  private:
88 
89  // Calculate the value of a layout defined by a string
90  float parseLayoutString(std::string expression);
91 
92  // Parse references to widgets from the layout strings
93  float parseWidgetName(const std::string& expression, Widget* widget, const std::string& alreadyParsedPart = "");
94 
95 
97  public:
98  float value = 0;
99  std::set<Layout*> attachedLayouts;
100  std::set<LayoutImpl*> parents;
101 
102  Operation operation = Operation::Value;
103  std::vector<std::shared_ptr<LayoutImpl>> operands;
104 
105  // These members are only used when operation == Operation::String
106  std::string stringExpression;
107  Widget* parentWidget = nullptr;
108 
109  private:
110  std::set<std::string> boundCallbacks;
111  };
112 
113 
120  class TGUI_API Layout
121  {
122  public:
123 
128  Layout();
129 
130 
137  template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
138  Layout(T constant)
139  {
140  m_impl->value = static_cast<float>(constant);
141  m_impl->attachedLayouts.insert(this);
142  }
143 
144 
151  Layout(const char* expression);
152 
153 
160  Layout(const std::string& expression);
161 
162 
169  Layout(const Layout& copy);
170 
171 
176  ~Layout();
177 
178 
187  Layout& operator=(const Layout& right);
188 
189 
196  float getValue() const;
197 
198 
206  void connectUpdateCallback(const std::function<void()>& callbackFunction);
207 
208 
214  void update() const;
215 
216 
224  std::shared_ptr<LayoutImpl> getImpl() const;
225 
226 
228  Layout operator+();
229 
231  Layout operator-();
232 
234  Layout operator+=(Layout right);
235 
237  Layout operator-=(Layout right);
238 
240  Layout operator*=(Layout right);
241 
243  Layout operator/=(Layout right);
244 
246  Layout operator%=(Layout right);
247 
248 
250  private:
251  std::shared_ptr<LayoutImpl> m_impl = std::make_shared<LayoutImpl>();
252  std::function<void()> m_callbackFunction;
253  };
254 
255 
262  class TGUI_API Layout2d
263  {
264  public:
265 
272  Layout2d(sf::Vector2f constant = {0, 0});
273 
274 
282  Layout2d(Layout layoutX, Layout layoutY);
283 
284 
291  Layout2d(const char* expression);
292 
293 
300  Layout2d(const std::string& expression);
301 
302 
309  sf::Vector2f getValue() const;
310 
311 
313  Layout2d operator+();
314 
316  Layout2d operator-();
317 
319  Layout2d operator+=(Layout2d right);
320 
322  Layout2d operator-=(Layout2d right);
323 
325  Layout2d operator*=(Layout right);
326 
328  Layout2d operator/=(Layout right);
329 
331  Layout2d operator%=(Layout right);
332 
333  public:
336  };
337 
339 
341  TGUI_API Layout operator<(Layout left, Layout right);
342 
344  TGUI_API Layout operator<=(Layout left, Layout right);
345 
347  TGUI_API Layout operator>(Layout left, Layout right);
348 
350  TGUI_API Layout operator>=(Layout left, Layout right);
351 
353  TGUI_API Layout operator==(Layout left, Layout right);
354 
356  TGUI_API Layout operator!=(Layout left, Layout right);
357 
359  TGUI_API Layout operator+(Layout left, Layout right);
360 
362  TGUI_API Layout operator-(Layout left, Layout right);
363 
365  TGUI_API Layout operator*(Layout left, Layout right);
366 
368  TGUI_API Layout operator/(Layout left, Layout right);
369 
371  TGUI_API Layout operator%(Layout left, Layout right);
372 
374  TGUI_API Layout operator&&(Layout left, Layout right);
375 
377  TGUI_API Layout operator||(Layout left, Layout right);
378 
380  TGUI_API Layout operator==(Layout2d left, Layout2d right);
381 
383  TGUI_API Layout operator!=(Layout2d left, Layout2d right);
384 
386  TGUI_API Layout2d operator+(Layout2d left, Layout2d right);
387 
389  TGUI_API Layout2d operator-(Layout2d left, Layout2d right);
390 
392  TGUI_API Layout2d operator*(Layout2d left, Layout right);
393 
395  TGUI_API Layout2d operator/(Layout2d left, Layout right);
396 
398  TGUI_API Layout2d operator%(Layout2d left, Layout right);
399 
401  TGUI_API Layout2d operator*(Layout left, Layout2d right);
402 
404  TGUI_API Layout bindLeft(std::shared_ptr<Widget> widget);
405 
407  TGUI_API Layout bindTop(std::shared_ptr<Widget> widget);
408 
410  TGUI_API Layout bindWidth(std::shared_ptr<Widget> widget);
411 
413  TGUI_API Layout bindHeight(std::shared_ptr<Widget> widget);
414 
416  TGUI_API Layout bindRight(std::shared_ptr<Widget> widget);
417 
419  TGUI_API Layout bindBottom(std::shared_ptr<Widget> widget);
420 
422  TGUI_API Layout2d bindPosition(std::shared_ptr<Widget> widget);
423 
425  TGUI_API Layout2d bindSize(std::shared_ptr<Widget> widget);
426 
428  TGUI_API Layout bindLeft(Gui& gui);
429 
431  TGUI_API Layout bindTop(Gui& gui);
432 
434  TGUI_API Layout bindWidth(Gui& gui);
435 
437  TGUI_API Layout bindHeight(Gui& gui);
438 
440  TGUI_API Layout bindRight(Gui& gui);
441 
443  TGUI_API Layout bindBottom(Gui& gui);
444 
446  TGUI_API Layout2d bindPosition(Gui& gui);
447 
449  TGUI_API Layout2d bindSize(Gui& gui);
450 
452  TGUI_API Layout bindMin(Layout value1, Layout value2);
453 
455  TGUI_API Layout bindMax(Layout value1, Layout value2);
456 
458  TGUI_API Layout bindRange(Layout minimum, Layout maximum, Layout value);
459 
461  TGUI_API Layout bindIf(Layout condition, Layout trueExpr, Layout falseExpr);
462 
464  TGUI_API Layout2d bindIf(Layout condition, Layout2d trueExpr, Layout2d falseExpr);
465 
467  TGUI_API Layout bindStr(const std::string& expression);
468 
470  TGUI_API Layout bindStr(const char* expression);
471 
473  TGUI_API Layout2d bindStr2d(const std::string& expression);
474 
476  TGUI_API Layout2d bindStr2d(const char* expression);
477 
478  // Put TGUI_IMPORT_LAYOUT_BIND_FUNCTIONS somewhere in your code to no longer have to put "tgui::" in front of the bind functions
479  // without having to import everything from tgui in your namespace or writing all these using statements yourself.
480  #define TGUI_IMPORT_LAYOUT_BIND_FUNCTIONS \
481  using tgui::bindLeft; \
482  using tgui::bindTop; \
483  using tgui::bindWidth; \
484  using tgui::bindHeight; \
485  using tgui::bindRight; \
486  using tgui::bindBottom; \
487  using tgui::bindPosition; \
488  using tgui::bindSize; \
489  using tgui::bindMin; \
490  using tgui::bindMax; \
491  using tgui::bindRange; \
492  using tgui::bindIf; \
493  using tgui::bindStr; \
494  using tgui::bindStr2d;
495 
497 }
498 
500 
501 #endif // TGUI_LAYOUT_HPP
Namespace that contains all TGUI functions and classes.
Definition: Animation.hpp:33
TGUI_API Layout bindTop(std::shared_ptr< Widget > widget)
Bind to the y position of the widget.
TGUI_API Layout2d bindStr2d(const std::string &expression)
Bind a string for a layout (you can also just create the layout directly with the string) ...
TGUI_API Layout bindIf(Layout condition, Layout trueExpr, Layout falseExpr)
Bind conditionally to one of the two layouts.
Class to store the position or size of a widget.
Definition: Layout.hpp:262
Gui class.
Definition: Gui.hpp:42
Layout(T constant)
Constructor to implicitly construct from numeric constant.
Definition: Layout.hpp:138
TGUI_API Layout operator>=(Layout left, Layout right)
>= operator for the Layout class
TGUI_API Layout operator<=(Layout left, Layout right)
<= operator for the Layout class
TGUI_API Layout2d bindSize(std::shared_ptr< Widget > widget)
Bind to the size of the widget.
TGUI_API Layout operator==(Layout left, Layout right)
== operator for the Layout class
TGUI_API Layout bindMin(Layout value1, Layout value2)
Bind to the minimum of two values.
TGUI_API Layout operator<(Layout left, Layout right)
< operator for the Layout class
Class to store the left, top, width or height of a widget.
Definition: Layout.hpp:120
TGUI_API Layout bindRight(std::shared_ptr< Widget > widget)
Bind to the right position of the widget.
TGUI_API Layout bindLeft(std::shared_ptr< Widget > widget)
Bind to the x position of the widget.
TGUI_API Layout operator%(Layout left, Layout right)
% operator for the Layout class
TGUI_API Layout operator||(Layout left, Layout right)
|| operator for the Layout class
TGUI_API Layout operator!=(Layout left, Layout right)
!= operator for the Layout class
TGUI_API Layout bindRange(Layout minimum, Layout maximum, Layout value)
Bind to a value that remains between the minimum and maximum.
TGUI_API Layout operator*(Layout left, Layout right)
operator for the Layout class
TGUI_API Layout bindMax(Layout value1, Layout value2)
Bind to the maximum of two values.
TGUI_API Layout bindStr(const std::string &expression)
Bind a string for a layout (you can also just create the layout directly with the string) ...
TGUI_API Layout operator-(Layout left, Layout right)
operator for the Layout class
TGUI_API Layout operator &&(Layout left, Layout right)
&& operator for the Layout class
TGUI_API Layout operator+(Layout left, Layout right)
operator for the Layout class
TGUI_API Layout operator>(Layout left, Layout right)
> operator for the Layout class
TGUI_API Layout operator/(Layout left, Layout right)
/ operator for the Layout class
TGUI_API Layout2d bindPosition(std::shared_ptr< Widget > widget)
Bind to the position of the widget.
TGUI_API Layout bindHeight(std::shared_ptr< Widget > widget)
Bind to the height of the widget.
Layout x
Layout to store the x component.
Definition: Layout.hpp:334
TGUI_API Layout bindWidth(std::shared_ptr< Widget > widget)
Bind to the width of the widget.
TGUI_API Layout bindBottom(std::shared_ptr< Widget > widget)
Bind to the bottom of the widget.
Layout y
Layout to store the y component.
Definition: Layout.hpp:335