TGUI  0.10-beta
Layout.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2022 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/Vector2.hpp>
31#include <type_traits>
32#include <functional>
33#include <memory>
34#include <string>
35
37
38namespace tgui
39{
40 class BackendGui;
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 BindingPosX, // X position, same as BindingLeft if widget origin isn't changed
64 BindingPosY, // Y position, same as BindingTop if widget origin isn't changed
65 BindingLeft,
66 BindingTop,
67 BindingWidth,
68 BindingHeight,
69 BindingInnerWidth,
70 BindingInnerHeight,
71 BindingString
72 };
73
74
76 public:
77
81 Layout() = default;
82
83
89 template <typename T, typename = typename std::enable_if<std::is_arithmetic<T>::value, T>::type>
90 Layout(T constant) :
91 m_value{static_cast<float>(constant)}
92 {
93 }
94
95
101 Layout(const char* expression) :
102 Layout{String{expression}}
103 {
104 }
105
106
112 Layout(String expression);
113
114
119 explicit Layout(Operation operation, Widget* boundWidget);
120
121
126 explicit Layout(Operation operation, std::unique_ptr<Layout> leftOperand, std::unique_ptr<Layout> rightOperand);
127
128
132 Layout(const Layout& other);
133
137 Layout(Layout&& other);
138
142 Layout& operator=(const Layout& other);
143
148
153
154
160 void replaceValue(const Layout& value);
161
162
168 float getValue() const
169 {
170 return m_value;
171 }
172
173
179 bool isConstant() const
180 {
181 return m_operation == Operation::Value;
182 }
183
184
191 String toString() const;
192
193
201 void connectWidget(Widget* widget, bool xAxis, std::function<void()> valueChangedCallbackHandler);
202
203
208 void unbindWidget();
209
210
216 void recalculateValue();
217
218
223 Layout* getLeftOperand() const;
224
225
230 Layout* getRightOperand() const;
231
232
234 private:
235
236
238 // If a widget is bound, inform it that the layout no longer binds it
240 void unbindLayout();
241
242
244 // Resets the parent pointers of the left and right operands if they exist and tell the bound widget that this layout
245 // requires information about changes to its position or size when the operation requires a widget to be bound.
247 void resetPointers();
248
249
251 // Check whether sublayouts contain a string that refers to a widget which should be bound.
253 void parseBindingStringRecursive(Widget* widget, bool xAxis);
254
255
257 // Find the widget corresponding to the given name and bind it if found
259 void parseBindingString(const String& expression, Widget* widget, bool xAxis);
260
261
263 private:
264
265 float m_value = 0;
266 Layout* m_parent = nullptr;
267 Operation m_operation = Operation::Value;
268 std::unique_ptr<Layout> m_leftOperand = nullptr; // The left operand of the operation in case the operation is a math operation
269 std::unique_ptr<Layout> m_rightOperand = nullptr; // The left operand of the operation in case the operation is a math operation
270 Widget* m_boundWidget = nullptr; // The widget on which this layout depends in case the operation is a binding
271 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
272 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
273
275 };
276
277
283 class TGUI_API Layout2d
284 {
285 public:
286
292 Layout2d(Vector2f constant = {0, 0}) :
293 x{constant.x},
294 y{constant.y}
295 {
296 }
297
298
305 Layout2d(Layout layoutX, Layout layoutY) :
306 x{std::move(layoutX)},
307 y{std::move(layoutY)}
308 {
309 }
310
311
319 Layout2d(const char* expression) :
320 x{expression},
321 y{expression}
322 {
323 }
324
325
333 Layout2d(const String& expression) :
334 x{expression},
335 y{expression}
336 {
337 }
338
339
346 {
347 return {x.getValue(), y.getValue()};
348 }
349
350
357 String toString() const
358 {
359 return "(" + x.toString() + ", " + y.toString() + ")";
360 }
361
363 public:
364
365 Layout x;
366 Layout y;
367 };
368
369
373 TGUI_API Layout operator-(Layout right);
374
378 TGUI_API Layout operator+(Layout left, Layout right);
379
383 TGUI_API Layout operator-(Layout left, Layout right);
384
388 TGUI_API Layout operator*(Layout left, Layout right);
389
393 TGUI_API Layout operator/(Layout left, Layout right);
394
398 TGUI_API Layout2d operator-(Layout2d right);
399
403 TGUI_API Layout2d operator+(Layout2d left, Layout2d right);
404
408 TGUI_API Layout2d operator-(Layout2d left, Layout2d right);
409
413 TGUI_API Layout2d operator*(Layout2d left, const Layout& right);
414
418 TGUI_API Layout2d operator*(const Layout& left, Layout2d right);
419
423 TGUI_API Layout2d operator/(Layout2d left, const Layout& right);
424
425
427
428 inline namespace bind_functions
429 {
431 TGUI_API Layout bindPosX(std::shared_ptr<Widget> widget);
432
434 TGUI_API Layout bindPosY(std::shared_ptr<Widget> widget);
435
437 TGUI_API Layout bindLeft(std::shared_ptr<Widget> widget);
438
440 TGUI_API Layout bindTop(std::shared_ptr<Widget> widget);
441
443 TGUI_API Layout bindWidth(std::shared_ptr<Widget> widget);
444
446 TGUI_API Layout bindHeight(std::shared_ptr<Widget> widget);
447
449 TGUI_API Layout bindInnerWidth(std::shared_ptr<Container> container);
450
452 TGUI_API Layout bindInnerHeight(std::shared_ptr<Container> container);
453
455 TGUI_API Layout bindRight(std::shared_ptr<Widget> widget);
456
458 TGUI_API Layout bindBottom(std::shared_ptr<Widget> widget);
459
461 TGUI_API Layout2d bindPosition(std::shared_ptr<Widget> widget);
462
464 TGUI_API Layout2d bindSize(std::shared_ptr<Widget> widget);
465
467 TGUI_API Layout2d bindInnerSize(std::shared_ptr<Container> container);
468
471
474
477
479 TGUI_API Layout bindMin(const Layout& value1, const Layout& value2);
480
482 TGUI_API Layout bindMax(const Layout& value1, const Layout& value2);
483 }
484
486}
487
489
490#endif // TGUI_LAYOUT_HPP
Base class for the Gui.
Definition: BackendGui.hpp:45
Class to store the position or size of a widget.
Definition: Layout.hpp:284
Layout2d(Vector2f constant={0, 0})
Default constructor to implicitly construct from a tgui::Vector2f.
Definition: Layout.hpp:292
Layout2d(Layout layoutX, Layout layoutY)
Constructor to create the Layout2d from two Layout classes.
Definition: Layout.hpp:305
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:319
Vector2f getValue() const
Returns the cached value of the layout.
Definition: Layout.hpp:345
Layout2d(const String &expression)
Constructs the Layout2d based on a string which will be parsed to determine the value of the layouts.
Definition: Layout.hpp:333
Class to store the left, top, width or height of a widget.
Definition: Layout.hpp:50
Layout(String expression)
Constructs the layout based on a string which will be parsed to determine the value of the layout.
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:168
Layout & operator=(Layout &&other)
Move assignment operator.
Layout(Layout &&other)
Move constructor.
Layout(T constant)
Constructor to implicitly construct from numeric constant.
Definition: Layout.hpp:90
Layout(const Layout &other)
Copy constructor.
~Layout()
Destructor.
void replaceValue(const Layout &value)
Replaces the value of the layout without overwriting its parent.
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:101
bool isConstant() const
Return whether the layout stores a constant value.
Definition: Layout.hpp:179
Layout & operator=(const Layout &other)
Copy assignment operator.
Wrapper class to store strings.
Definition: String.hpp:79
The parent class for every widget.
Definition: Widget.hpp:70
Namespace that contains all TGUI functions and classes.
Definition: AbsoluteOrRelativeValue.hpp:36
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 top position of the widget.
TGUI_API Layout bindPosX(std::shared_ptr< Widget > widget)
Bind to the x position of the widget (same as bindLeft unless widget origin is changed)
TGUI_API Layout2d bindInnerSize(std::shared_ptr< Container > container)
Bind to the inner size of the container widget.
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 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 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 left position of the widget.
TGUI_API Layout bindPosY(std::shared_ptr< Widget > widget)
Bind to the y position of the widget (same as bindTop unless widget origin is changed)
TGUI_API Layout2d bindPosition(std::shared_ptr< Widget > widget)
Bind to the position of the widget.