TGUI  1.5
Loading...
Searching...
No Matches
Layout.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2024 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#ifndef TGUI_LAYOUT_HPP
26#define TGUI_LAYOUT_HPP
27
28#include <TGUI/Config.hpp>
29#include <TGUI/Vector2.hpp>
30
31#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
32 #include <type_traits>
33 #include <functional>
34 #include <memory>
35 #include <string>
36#endif
37
39
40TGUI_MODULE_EXPORT namespace tgui
41{
42 class BackendGui;
43 class Widget;
44 class Container;
45
50 enum class Orientation
51 {
52 Vertical,
54 };
55
61 {
62 Left,
63 Center,
64 Right
65 };
66
72 {
73 Top ,
74 Center,
75 Bottom
76 };
77
83 enum class AutoLayout
84 {
85 Manual,
86 Top,
87 Left,
88 Right,
89 Bottom,
90 Leftmost,
91 Rightmost,
92 Fill
93 };
94
100 class TGUI_API Layout
101 {
102 public:
103
105 enum class Operation
106 {
107 Value,
108 Plus,
109 Minus,
110 Multiplies,
111 Divides,
112 Minimum,
113 Maximum,
114 BindingPosX, // X position, same as BindingLeft if widget origin isn't changed
115 BindingPosY, // Y position, same as BindingTop if widget origin isn't changed
116 BindingLeft,
117 BindingTop,
118 BindingWidth,
119 BindingHeight,
120 BindingInnerWidth,
121 BindingInnerHeight,
122 BindingString
123 };
124
126 public:
127
131 Layout() = default;
132
138 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
139 Layout(T constant) :
140 m_value{static_cast<float>(constant)}
141 {
142 }
143
149 Layout(const char* expression) :
150 Layout{String{expression}}
151 {
152 }
153
159 Layout(String expression);
160
165 explicit Layout(Operation operation, Widget* boundWidget);
166
171 explicit Layout(Operation operation, std::unique_ptr<Layout> leftOperand, std::unique_ptr<Layout> rightOperand);
172
176 Layout(const Layout& other);
177
181 Layout(Layout&& other) noexcept;
182
186 Layout& operator=(const Layout& other);
187
191 Layout& operator=(Layout&& other) noexcept;
192
197
203 void replaceValue(const Layout& value);
204
210 TGUI_NODISCARD float getValue() const
211 {
212 return m_value;
213 }
214
220 TGUI_NODISCARD bool isConstant() const
221 {
222 return m_operation == Operation::Value;
223 }
224
231 TGUI_NODISCARD String toString() const;
232
240 void connectWidget(Widget* widget, bool xAxis, std::function<void()> valueChangedCallbackHandler);
241
246 void unbindWidget();
247
253 void recalculateValue();
254
259 TGUI_NODISCARD Layout* getLeftOperand() const;
260
265 TGUI_NODISCARD Layout* getRightOperand() const;
266
268 private:
269
271 // If a widget is bound, inform it that the layout no longer binds it
273 void unbindLayout();
274
276 // Resets the parent pointers of the left and right operands if they exist and tell the bound widget that this layout
277 // requires information about changes to its position or size when the operation requires a widget to be bound.
279 void resetPointers();
280
282 // Check whether sublayouts contain a string that refers to a widget which should be bound.
284 void parseBindingStringRecursive(Widget* widget, bool xAxis);
285
287 // Find the widget corresponding to the given name and bind it if found
289 void parseBindingString(const String& expression, Widget* widget, bool xAxis);
290
292 private:
293
294 float m_value = 0;
295 Layout* m_parent = nullptr;
296 Operation m_operation = Operation::Value;
297 std::unique_ptr<Layout> m_leftOperand = nullptr; // The left operand of the operation in case the operation is a math operation
298 std::unique_ptr<Layout> m_rightOperand = nullptr; // The left operand of the operation in case the operation is a math operation
299 Widget* m_boundWidget = nullptr; // The widget on which this layout depends in case the operation is a binding
300 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
301 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
302 int m_callingCallbackCount = 0; // Used to detect that connectWidget is called in an infinity loop if certain layouts depend on each other
303
305 };
306
312 class TGUI_API Layout2d
313 {
314 public:
315
321 Layout2d(Vector2f constant = {0, 0}) :
322 x{constant.x},
323 y{constant.y}
324 {
325 }
326
333 Layout2d(Layout layoutX, Layout layoutY) :
334 x{std::move(layoutX)},
335 y{std::move(layoutY)}
336 {
337 }
338
346 Layout2d(const char* expression) :
347 x{expression},
348 y{expression}
349 {
350 }
351
359 Layout2d(const String& expression) :
360 x{expression},
361 y{expression}
362 {
363 }
364
370 TGUI_NODISCARD Vector2f getValue() const
371 {
372 return {x.getValue(), y.getValue()};
373 }
374
381 TGUI_NODISCARD String toString() const
382 {
383 return U"(" + x.toString() + U", " + y.toString() + U")";
384 }
385
387 public:
388
389 Layout x;
390 Layout y;
391 };
392
396 TGUI_NODISCARD TGUI_API Layout operator-(Layout right);
397
401 TGUI_NODISCARD TGUI_API Layout operator+(Layout left, Layout right);
402
406 TGUI_NODISCARD TGUI_API Layout operator-(Layout left, Layout right);
407
411 TGUI_NODISCARD TGUI_API Layout operator*(Layout left, Layout right);
412
416 TGUI_NODISCARD TGUI_API Layout operator/(Layout left, Layout right);
417
421 TGUI_NODISCARD TGUI_API Layout2d operator-(Layout2d right);
422
426 TGUI_NODISCARD TGUI_API Layout2d operator+(Layout2d left, Layout2d right);
427
431 TGUI_NODISCARD TGUI_API Layout2d operator-(Layout2d left, Layout2d right);
432
436 TGUI_NODISCARD TGUI_API Layout2d operator*(Layout2d left, const Layout& right);
437
441 TGUI_NODISCARD TGUI_API Layout2d operator*(const Layout& left, Layout2d right);
442
446 TGUI_NODISCARD TGUI_API Layout2d operator/(Layout2d left, const Layout& right);
447
449
450 inline namespace bind_functions
451 {
453 TGUI_NODISCARD TGUI_API Layout bindPosX(const std::shared_ptr<Widget>& widget);
454
456 TGUI_NODISCARD TGUI_API Layout bindPosY(const std::shared_ptr<Widget>& widget);
457
459 TGUI_NODISCARD TGUI_API Layout bindLeft(const std::shared_ptr<Widget>& widget);
460
462 TGUI_NODISCARD TGUI_API Layout bindTop(const std::shared_ptr<Widget>& widget);
463
465 TGUI_NODISCARD TGUI_API Layout bindWidth(const std::shared_ptr<Widget>& widget);
466
468 TGUI_NODISCARD TGUI_API Layout bindHeight(const std::shared_ptr<Widget>& widget);
469
471 TGUI_NODISCARD TGUI_API Layout bindInnerWidth(const std::shared_ptr<Container>& container);
472
474 TGUI_NODISCARD TGUI_API Layout bindInnerHeight(const std::shared_ptr<Container>& container);
475
477 TGUI_NODISCARD TGUI_API Layout bindRight(const std::shared_ptr<Widget>& widget);
478
480 TGUI_NODISCARD TGUI_API Layout bindBottom(const std::shared_ptr<Widget>& widget);
481
483 TGUI_NODISCARD TGUI_API Layout2d bindPosition(const std::shared_ptr<Widget>& widget);
484
486 TGUI_NODISCARD TGUI_API Layout2d bindSize(const std::shared_ptr<Widget>& widget);
487
489 TGUI_NODISCARD TGUI_API Layout2d bindInnerSize(const std::shared_ptr<Container>& container);
490
492 TGUI_NODISCARD TGUI_API Layout bindWidth(const BackendGui& gui);
493
495 TGUI_NODISCARD TGUI_API Layout bindHeight(const BackendGui& gui);
496
498 TGUI_NODISCARD TGUI_API Layout2d bindSize(const BackendGui& gui);
499
501 TGUI_NODISCARD TGUI_API Layout bindMin(const Layout& value1, const Layout& value2);
502
504 TGUI_NODISCARD TGUI_API Layout bindMax(const Layout& value1, const Layout& value2);
505 }
506
508}
509
511
512#endif // TGUI_LAYOUT_HPP
Base class for the Gui.
Definition BackendGui.hpp:47
Class to store the position or size of a widget.
Definition Layout.hpp:313
Layout2d(Vector2f constant={0, 0})
Default constructor to implicitly construct from a tgui::Vector2f.
Definition Layout.hpp:321
Layout2d(Layout layoutX, Layout layoutY)
Constructor to create the Layout2d from two Layout classes.
Definition Layout.hpp:333
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:346
TGUI_NODISCARD Vector2f getValue() const
Returns the cached value of the layout.
Definition Layout.hpp:370
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:359
Class to store the left, top, width or height of a widget.
Definition Layout.hpp:101
Layout(Layout &&other) noexcept
Move constructor.
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:106
TGUI_NODISCARD float getValue() const
Return the cached value of the layout.
Definition Layout.hpp:210
Layout()=default
Default constructor.
Layout & operator=(Layout &&other) noexcept
Move assignment operator.
Layout(T constant)
Constructor to implicitly construct from numeric constant.
Definition Layout.hpp:139
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:149
TGUI_NODISCARD bool isConstant() const
Return whether the layout stores a constant value.
Definition Layout.hpp:220
Layout & operator=(const Layout &other)
Copy assignment operator.
Wrapper class to store strings.
Definition String.hpp:96
The parent class for every widget.
Definition Widget.hpp:83
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:38
TGUI_NODISCARD TGUI_API Layout bindLeft(const std::shared_ptr< Widget > &widget)
Bind to the left position of the widget.
TGUI_NODISCARD TGUI_API Layout bindRight(const std::shared_ptr< Widget > &widget)
Bind to the right position of the widget.
HorizontalAlignment
The horizontal alignment.
Definition Layout.hpp:61
@ Center
Center the object horizontally.
@ Right
Align to the right side.
@ Left
Align to the left side.
TGUI_NODISCARD TGUI_API Layout bindWidth(const std::shared_ptr< Widget > &widget)
Bind to the width of the widget.
TGUI_NODISCARD TGUI_API Layout bindMax(const Layout &value1, const Layout &value2)
Bind to the maximum value of two layouts.
TGUI_NODISCARD TGUI_API Layout bindHeight(const std::shared_ptr< Widget > &widget)
Bind to the height of the widget.
TGUI_NODISCARD TGUI_API Layout2d bindSize(const std::shared_ptr< Widget > &widget)
Bind to the size of the widget.
TGUI_NODISCARD TGUI_API Layout bindTop(const std::shared_ptr< Widget > &widget)
Bind to the top position of the widget.
TGUI_NODISCARD TGUI_API Layout bindInnerWidth(const std::shared_ptr< Container > &container)
Bind to the inner width of the container widget.
Orientation
Orientation of the object.
Definition Layout.hpp:51
@ Vertical
Vertical orientation.
@ Horizontal
Horizontal orientation.
TGUI_NODISCARD TGUI_API Layout2d bindPosition(const std::shared_ptr< Widget > &widget)
Bind to the position of the widget.
AutoLayout
Alignments for how to position a widget in its parent.
Definition Layout.hpp:84
@ Leftmost
Places the widget on the left side and sets height to 100%. Width needs to be manually set....
@ Rightmost
Places the widget on the right side and sets height to 100%. Width needs to be manually set....
@ Fill
Sets the position and size to fill the entire area that isn't already taken by components with the ot...
@ Manual
Position and size need to be manually set. This is the default.
TGUI_NODISCARD TGUI_API Layout bindPosX(const std::shared_ptr< Widget > &widget)
Bind to the x position of the widget (same as bindLeft unless widget origin is changed)
TGUI_NODISCARD TGUI_API Layout bindBottom(const std::shared_ptr< Widget > &widget)
Bind to the bottom of the widget.
TGUI_NODISCARD TGUI_API Layout bindMin(const Layout &value1, const Layout &value2)
Bind to the minimum value of two layouts.
TGUI_NODISCARD TGUI_API Layout bindPosY(const std::shared_ptr< Widget > &widget)
Bind to the y position of the widget (same as bindTop unless widget origin is changed)
TGUI_NODISCARD TGUI_API Layout2d bindInnerSize(const std::shared_ptr< Container > &container)
Bind to the inner size of the container widget.
VerticalAlignment
The vertical alignment.
Definition Layout.hpp:72
@ Bottom
Align to the bottom.
@ Top
Align to the top.
TGUI_NODISCARD TGUI_API Layout bindInnerHeight(const std::shared_ptr< Container > &container)
Bind to the inner height of the container widget.