TGUI  0.8.9
Layout.hpp
1
2//
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/Vector2f.hpp>
31#include <type_traits>
32#include <functional>
33#include <memory>
34#include <string>
35
37
38namespace tgui
39{
40 class Gui;
41 class Widget;
42 class Container;
43
49 class TGUI_API Layout
50 {
51 public:
52
54 enum class Operation
55 {
56 Value,
57 Plus,
58 Minus,
59 Multiplies,
60 Divides,
61 Minimum,
62 Maximum,
63 BindingLeft,
64 BindingTop,
65 BindingWidth,
66 BindingHeight,
67 BindingInnerWidth,
68 BindingInnerHeight,
69 BindingString
70 };
71
72
74 public:
75
79 Layout() = default;
80
81
87 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
88 Layout(T constant) :
89 m_value{static_cast<float>(constant)}
90 {
91 }
92
93
99 Layout(const char* expression) :
100 Layout{std::string{expression}}
101 {
102 }
103
104
110 Layout(std::string expression);
111
112
117 explicit Layout(Operation operation, Widget* boundWidget);
118
119
124 explicit Layout(Operation operation, std::unique_ptr<Layout> leftOperand, std::unique_ptr<Layout> rightOperand);
125
126
130 Layout(const Layout& other);
131
135 Layout(Layout&& other);
136
140 Layout& operator=(const Layout& other);
141
146
151
152
158 float getValue() const
159 {
160 return m_value;
161 }
162
163
169 bool isConstant() const
170 {
171 return m_operation == Operation::Value;
172 }
173
174
181 std::string toString() const;
182
183
191 void connectWidget(Widget* widget, bool xAxis, std::function<void()> valueChangedCallbackHandler);
192
193
198 void unbindWidget();
199
200
206 void recalculateValue();
207
208
210 private:
211
212
214 // If a widget is bound, inform it that the layout no longer binds it
216 void unbindLayout();
217
218
220 // Resets the parent pointers of the left and right operands if they exist and tell the bound widget that this layout
221 // requires information about changes to its position or size when the operation requires a widget to be bound.
223 void resetPointers();
224
225
227 // Check whether sublayouts contain a string that refers to a widget which should be bound.
229 void parseBindingStringRecursive(Widget* widget, bool xAxis);
230
231
233 // Find the widget corresponding to the given name and bind it if found
235 void parseBindingString(const std::string& expression, Widget* widget, bool xAxis);
236
237
239 private:
240
241 float m_value = 0;
242 Layout* m_parent = nullptr;
243 Operation m_operation = Operation::Value;
244 std::unique_ptr<Layout> m_leftOperand = nullptr; // The left operand of the operation in case the operation is a math operation
245 std::unique_ptr<Layout> m_rightOperand = nullptr; // The left operand of the operation in case the operation is a math operation
246 Widget* m_boundWidget = nullptr; // The widget on which this layout depends in case the operation is a binding
247 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
248 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
249
251 };
252
253
259 class TGUI_API Layout2d
260 {
261 public:
262
268 Layout2d(Vector2f constant = {0, 0}) :
269 x{constant.x},
270 y{constant.y}
271 {
272 }
273
274
280 Layout2d(sf::Vector2f constant) :
281 x{constant.x},
282 y{constant.y}
283 {
284 }
285
286
293 Layout2d(Layout layoutX, Layout layoutY) :
294 x{std::move(layoutX)},
295 y{std::move(layoutY)}
296 {
297 }
298
299
307 Layout2d(const char* expression) :
308 x{expression},
309 y{expression}
310 {
311 }
312
313
321 Layout2d(const std::string& expression) :
322 x{expression},
323 y{expression}
324 {
325 }
326
327
334 {
335 return {x.getValue(), y.getValue()};
336 }
337
338
345 std::string toString() const
346 {
347 return "(" + x.toString() + ", " + y.toString() + ")";
348 }
349
351 public:
352
353 Layout x;
354 Layout y;
355 };
356
357
361 TGUI_API Layout operator-(Layout right);
362
366 TGUI_API Layout operator+(Layout left, Layout right);
367
371 TGUI_API Layout operator-(Layout left, Layout right);
372
376 TGUI_API Layout operator*(Layout left, Layout right);
377
381 TGUI_API Layout operator/(Layout left, Layout right);
382
386 TGUI_API Layout2d operator-(Layout2d right);
387
391 TGUI_API Layout2d operator+(Layout2d left, Layout2d right);
392
396 TGUI_API Layout2d operator-(Layout2d left, Layout2d right);
397
401 TGUI_API Layout2d operator*(Layout2d left, const Layout& right);
402
406 TGUI_API Layout2d operator*(const Layout& left, Layout2d right);
407
411 TGUI_API Layout2d operator/(Layout2d left, const Layout& right);
412
413
415
416 inline namespace bind_functions
417 {
419 TGUI_API Layout bindLeft(std::shared_ptr<Widget> widget);
420
422 TGUI_API Layout bindTop(std::shared_ptr<Widget> widget);
423
425 TGUI_API Layout bindWidth(std::shared_ptr<Widget> widget);
426
428 TGUI_API Layout bindHeight(std::shared_ptr<Widget> widget);
429
431 TGUI_API Layout bindInnerWidth(std::shared_ptr<Container> container);
432
434 TGUI_API Layout bindInnerHeight(std::shared_ptr<Container> container);
435
437 TGUI_API Layout bindRight(std::shared_ptr<Widget> widget);
438
440 TGUI_API Layout bindBottom(std::shared_ptr<Widget> widget);
441
443 TGUI_API Layout2d bindPosition(std::shared_ptr<Widget> widget);
444
446 TGUI_API Layout2d bindSize(std::shared_ptr<Widget> widget);
447
449 TGUI_API Layout2d bindInnerSize(std::shared_ptr<Container> container);
450
452 TGUI_API Layout bindWidth(Gui& gui);
453
455 TGUI_API Layout bindHeight(Gui& gui);
456
458 TGUI_API Layout2d bindSize(Gui& gui);
459
461 TGUI_API Layout bindMin(const Layout& value1, const Layout& value2);
462
464 TGUI_API Layout bindMax(const Layout& value1, const Layout& value2);
465 }
466
468}
469
471
472#endif // TGUI_LAYOUT_HPP
Gui class.
Definition: Gui.hpp:43
Class to store the position or size of a widget.
Definition: Layout.hpp:260
Layout2d(Vector2f constant={0, 0})
Default constructor to implicitly construct from a tgui::Vector2f.
Definition: Layout.hpp:268
Layout2d(Layout layoutX, Layout layoutY)
Constructor to create the Layout2d from two Layout classes.
Definition: Layout.hpp:293
Layout2d(sf::Vector2f constant)
Default constructor to implicitly construct from a sf::Vector2f.
Definition: Layout.hpp:280
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:307
Vector2f getValue() const
Returns the cached value of the layout.
Definition: Layout.hpp:333
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:321
Class to store the left, top, width or height of a widget.
Definition: Layout.hpp:50
Operation
The operation which the layout has to perform to find its value.
Definition: Layout.hpp:55
Layout()=default
Default constructor.
float getValue() const
Return the cached value of the layout.
Definition: Layout.hpp:158
Layout & operator=(Layout &&other)
Move assignment operator.
Layout(Layout &&other)
Move constructor.
Layout(std::string expression)
Constructs the layout based on a string which will be parsed to determine the value of the layout.
Layout(T constant)
Constructor to implicitly construct from numeric constant.
Definition: Layout.hpp:88
Layout(const Layout &other)
Copy constructor.
~Layout()
Destructor.
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:99
bool isConstant() const
Return whether the layout stores a constant value.
Definition: Layout.hpp:169
Layout & operator=(const Layout &other)
Copy assignment operator.
Definition: Vector2f.hpp:39
The parent class for every widget.
Definition: Widget.hpp:74
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:37
TGUI_API Layout bindRight(std::shared_ptr< Widget > widget)
Bind to the right position of the widget.
TGUI_API Layout bindHeight(std::shared_ptr< Widget > widget)
Bind to the height of the widget.
TGUI_API Layout bindTop(std::shared_ptr< Widget > widget)
Bind to the y position of the widget.
TGUI_API Layout2d bindInnerSize(std::shared_ptr< Container > container)
Bind to the inner size of the container widget.
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 bindMax(const Layout &value1, const Layout &value2)
Bind to the maximum value of two layouts.
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.
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 bindInnerHeight(std::shared_ptr< Container > container)
Bind to the inner height of the container widget.
TGUI_API Layout bindInnerWidth(std::shared_ptr< Container > container)
Bind to the inner width of the container widget.
TGUI_API Layout operator-(Layout right)
Unary minus operator for the Layout class.
TGUI_API Layout bindMin(const Layout &value1, const Layout &value2)
Bind to the minimum value of two layouts.
TGUI_API Layout bindLeft(std::shared_ptr< Widget > widget)
Bind to the x position of the widget.
TGUI_API Layout2d bindPosition(std::shared_ptr< Widget > widget)
Bind to the position of the widget.