TGUI  0.7.8
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  }
142 
143 
150  Layout(const char* expression);
151 
152 
159  Layout(const std::string& expression);
160 
161 
168  Layout(const Layout& copy);
169 
170 
179  Layout& operator=(const Layout& right);
180 
181 
188  float getValue() const;
189 
190 
198  void connectUpdateCallback(const std::function<void()>& callbackFunction);
199 
200 
206  void update() const;
207 
208 
216  std::shared_ptr<LayoutImpl> getImpl() const;
217 
218 
220  Layout operator+();
221 
223  Layout operator-();
224 
226  Layout operator+=(Layout right);
227 
229  Layout operator-=(Layout right);
230 
232  Layout operator*=(Layout right);
233 
235  Layout operator/=(Layout right);
236 
238  Layout operator%=(Layout right);
239 
240 
242  private:
243  std::shared_ptr<LayoutImpl> m_impl = std::make_shared<LayoutImpl>();
244  std::function<void()> m_callbackFunction;
245  };
246 
247 
254  class TGUI_API Layout2d
255  {
256  public:
257 
264  Layout2d(sf::Vector2f constant = {0, 0});
265 
266 
274  Layout2d(Layout layoutX, Layout layoutY);
275 
276 
283  Layout2d(const char* expression);
284 
285 
292  Layout2d(const std::string& expression);
293 
294 
301  sf::Vector2f getValue() const;
302 
303 
306 
309 
311  Layout2d operator+=(Layout2d right);
312 
314  Layout2d operator-=(Layout2d right);
315 
317  Layout2d operator*=(Layout right);
318 
320  Layout2d operator/=(Layout right);
321 
323  Layout2d operator%=(Layout right);
324 
325  public:
328  };
329 
331 
333  TGUI_API Layout operator<(Layout left, Layout right);
334 
336  TGUI_API Layout operator<=(Layout left, Layout right);
337 
339  TGUI_API Layout operator>(Layout left, Layout right);
340 
342  TGUI_API Layout operator>=(Layout left, Layout right);
343 
345  TGUI_API Layout operator==(Layout left, Layout right);
346 
348  TGUI_API Layout operator!=(Layout left, Layout right);
349 
351  TGUI_API Layout operator+(Layout left, Layout right);
352 
354  TGUI_API Layout operator-(Layout left, Layout right);
355 
357  TGUI_API Layout operator*(Layout left, Layout right);
358 
360  TGUI_API Layout operator/(Layout left, Layout right);
361 
363  TGUI_API Layout operator%(Layout left, Layout right);
364 
366  TGUI_API Layout operator&&(Layout left, Layout right);
367 
369  TGUI_API Layout operator||(Layout left, Layout right);
370 
372  TGUI_API Layout operator==(Layout2d left, Layout2d right);
373 
375  TGUI_API Layout operator!=(Layout2d left, Layout2d right);
376 
378  TGUI_API Layout2d operator+(Layout2d left, Layout2d right);
379 
381  TGUI_API Layout2d operator-(Layout2d left, Layout2d right);
382 
384  TGUI_API Layout2d operator*(Layout2d left, Layout right);
385 
387  TGUI_API Layout2d operator/(Layout2d left, Layout right);
388 
390  TGUI_API Layout2d operator%(Layout2d left, Layout right);
391 
393  TGUI_API Layout2d operator*(Layout left, Layout2d right);
394 
396  TGUI_API Layout bindLeft(std::shared_ptr<Widget> widget);
397 
399  TGUI_API Layout bindTop(std::shared_ptr<Widget> widget);
400 
402  TGUI_API Layout bindWidth(std::shared_ptr<Widget> widget);
403 
405  TGUI_API Layout bindHeight(std::shared_ptr<Widget> widget);
406 
408  TGUI_API Layout bindRight(std::shared_ptr<Widget> widget);
409 
411  TGUI_API Layout bindBottom(std::shared_ptr<Widget> widget);
412 
414  TGUI_API Layout2d bindPosition(std::shared_ptr<Widget> widget);
415 
417  TGUI_API Layout2d bindSize(std::shared_ptr<Widget> widget);
418 
420  TGUI_API Layout bindLeft(Gui& gui);
421 
423  TGUI_API Layout bindTop(Gui& gui);
424 
426  TGUI_API Layout bindWidth(Gui& gui);
427 
429  TGUI_API Layout bindHeight(Gui& gui);
430 
432  TGUI_API Layout bindRight(Gui& gui);
433 
435  TGUI_API Layout bindBottom(Gui& gui);
436 
438  TGUI_API Layout2d bindPosition(Gui& gui);
439 
441  TGUI_API Layout2d bindSize(Gui& gui);
442 
444  TGUI_API Layout bindMin(Layout value1, Layout value2);
445 
447  TGUI_API Layout bindMax(Layout value1, Layout value2);
448 
450  TGUI_API Layout bindRange(Layout minimum, Layout maximum, Layout value);
451 
453  TGUI_API Layout bindIf(Layout condition, Layout trueExpr, Layout falseExpr);
454 
456  TGUI_API Layout2d bindIf(Layout condition, Layout2d trueExpr, Layout2d falseExpr);
457 
459  TGUI_API Layout bindStr(const std::string& expression);
460 
462  TGUI_API Layout bindStr(const char* expression);
463 
465  TGUI_API Layout2d bindStr2d(const std::string& expression);
466 
468  TGUI_API Layout2d bindStr2d(const char* expression);
469 
470  // Put TGUI_IMPORT_LAYOUT_BIND_FUNCTIONS somewhere in your code to no longer have to put "tgui::" in front of the bind functions
471  // without having to import everything from tgui in your namespace or writing all these using statements yourself.
472  #define TGUI_IMPORT_LAYOUT_BIND_FUNCTIONS \
473  using tgui::bindLeft; \
474  using tgui::bindTop; \
475  using tgui::bindWidth; \
476  using tgui::bindHeight; \
477  using tgui::bindRight; \
478  using tgui::bindBottom; \
479  using tgui::bindPosition; \
480  using tgui::bindSize; \
481  using tgui::bindMin; \
482  using tgui::bindMax; \
483  using tgui::bindRange; \
484  using tgui::bindIf; \
485  using tgui::bindStr; \
486  using tgui::bindStr2d;
487 
489 }
490 
492 
493 #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:254
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:326
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:327