TGUI  1.6.1
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#include <TGUI/AbsoluteOrRelativeValue.hpp>
31
32#if !TGUI_EXPERIMENTAL_USE_STD_MODULE
33 #include <type_traits>
34 #include <functional>
35 #include <memory>
36 #include <string>
37#endif
38
40
41TGUI_MODULE_EXPORT namespace tgui
42{
43 class BackendGui;
44 class Widget;
45 class Container;
46
51 enum class Orientation
52 {
53 Vertical,
55 };
56
62 {
63 Left,
64 Center,
65 Right
66 };
67
73 {
74 Top ,
75 Center,
76 Bottom
77 };
78
84 enum class AutoLayout
85 {
86 Manual,
87 Top,
88 Left,
89 Right,
90 Bottom,
91 Leftmost,
92 Rightmost,
93 Fill
94 };
95
101 class TGUI_API Layout
102 {
103 public:
104
106 enum class Operation
107 {
108 Value,
109 Plus,
110 Minus,
111 Multiplies,
112 Divides,
113 Minimum,
114 Maximum,
115 BindingPosX, // X position, same as BindingLeft if widget origin isn't changed
116 BindingPosY, // Y position, same as BindingTop if widget origin isn't changed
117 BindingLeft,
118 BindingTop,
119 BindingWidth,
120 BindingHeight,
121 BindingInnerWidth,
122 BindingInnerHeight,
123 BindingString
124 };
125
127 public:
128
132 Layout() = default;
133
139 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic<T>::value, T>>
140 Layout(T constant) :
141 m_value{static_cast<float>(constant)}
142 {
143 }
144
153
159 Layout(const char* expression) :
160 Layout{String{expression}}
161 {
162 }
163
169 Layout(String expression);
170
175 explicit Layout(Operation operation, Widget* boundWidget);
176
181 explicit Layout(Operation operation, std::unique_ptr<Layout> leftOperand, std::unique_ptr<Layout> rightOperand);
182
186 Layout(const Layout& other);
187
191 Layout(Layout&& other) noexcept;
192
196 Layout& operator=(const Layout& other);
197
201 Layout& operator=(Layout&& other) noexcept;
202
207
213 void replaceValue(const Layout& value);
214
220 TGUI_NODISCARD float getValue() const
221 {
222 return m_value;
223 }
224
230 TGUI_NODISCARD bool isConstant() const
231 {
232 return m_operation == Operation::Value;
233 }
234
241 TGUI_NODISCARD String toString() const;
242
250 void connectWidget(Widget* widget, bool xAxis, std::function<void()> valueChangedCallbackHandler);
251
256 void unbindWidget();
257
263 void recalculateValue();
264
269 TGUI_NODISCARD Layout* getLeftOperand() const;
270
275 TGUI_NODISCARD Layout* getRightOperand() const;
276
278 private:
279
281 // If a widget is bound, inform it that the layout no longer binds it
283 void unbindLayout();
284
286 // Resets the parent pointers of the left and right operands if they exist and tell the bound widget that this layout
287 // requires information about changes to its position or size when the operation requires a widget to be bound.
289 void resetPointers();
290
292 // Check whether sublayouts contain a string that refers to a widget which should be bound.
294 void parseBindingStringRecursive(Widget* widget, bool xAxis);
295
297 // Find the widget corresponding to the given name and bind it if found
299 void parseBindingString(const String& expression, Widget* widget, bool xAxis);
300
302 private:
303
304 float m_value = 0;
305 Layout* m_parent = nullptr;
306 Operation m_operation = Operation::Value;
307 std::unique_ptr<Layout> m_leftOperand = nullptr; // The left operand of the operation in case the operation is a math operation
308 std::unique_ptr<Layout> m_rightOperand = nullptr; // The left operand of the operation in case the operation is a math operation
309 Widget* m_boundWidget = nullptr; // The widget on which this layout depends in case the operation is a binding
310 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
311 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
312 int m_callingCallbackCount = 0; // Used to detect that connectWidget is called in an infinity loop if certain layouts depend on each other
313
315 };
316
322 class TGUI_API Layout2d
323 {
324 public:
325
331 Layout2d(Vector2f constant = {0, 0}) :
332 x{constant.x},
333 y{constant.y}
334 {
335 }
336
343 Layout2d(Layout layoutX, Layout layoutY) :
344 x{std::move(layoutX)},
345 y{std::move(layoutY)}
346 {
347 }
348
356 Layout2d(const char* expression) :
357 x{expression},
358 y{expression}
359 {
360 }
361
369 Layout2d(const String& expression) :
370 x{expression},
371 y{expression}
372 {
373 }
374
380 TGUI_NODISCARD Vector2f getValue() const
381 {
382 return {x.getValue(), y.getValue()};
383 }
384
391 TGUI_NODISCARD String toString() const
392 {
393 return U"(" + x.toString() + U", " + y.toString() + U")";
394 }
395
397 public:
398
399 Layout x;
400 Layout y;
401 };
402
406 TGUI_NODISCARD TGUI_API Layout operator-(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 Layout operator*(Layout left, Layout right);
422
426 TGUI_NODISCARD TGUI_API Layout operator/(Layout left, Layout right);
427
431 TGUI_NODISCARD TGUI_API Layout2d operator-(Layout2d right);
432
436 TGUI_NODISCARD TGUI_API Layout2d operator+(Layout2d left, Layout2d right);
437
441 TGUI_NODISCARD TGUI_API Layout2d operator-(Layout2d left, Layout2d right);
442
446 TGUI_NODISCARD TGUI_API Layout2d operator*(Layout2d left, const Layout& right);
447
451 TGUI_NODISCARD TGUI_API Layout2d operator*(const Layout& left, Layout2d right);
452
456 TGUI_NODISCARD TGUI_API Layout2d operator/(Layout2d left, const Layout& right);
457
459
460 inline namespace bind_functions
461 {
463 TGUI_NODISCARD TGUI_API Layout bindPosX(const std::shared_ptr<Widget>& widget);
464
466 TGUI_NODISCARD TGUI_API Layout bindPosY(const std::shared_ptr<Widget>& widget);
467
469 TGUI_NODISCARD TGUI_API Layout bindLeft(const std::shared_ptr<Widget>& widget);
470
472 TGUI_NODISCARD TGUI_API Layout bindTop(const std::shared_ptr<Widget>& widget);
473
475 TGUI_NODISCARD TGUI_API Layout bindWidth(const std::shared_ptr<Widget>& widget);
476
478 TGUI_NODISCARD TGUI_API Layout bindHeight(const std::shared_ptr<Widget>& widget);
479
481 TGUI_NODISCARD TGUI_API Layout bindInnerWidth(const std::shared_ptr<Container>& container);
482
484 TGUI_NODISCARD TGUI_API Layout bindInnerHeight(const std::shared_ptr<Container>& container);
485
487 TGUI_NODISCARD TGUI_API Layout bindRight(const std::shared_ptr<Widget>& widget);
488
490 TGUI_NODISCARD TGUI_API Layout bindBottom(const std::shared_ptr<Widget>& widget);
491
493 TGUI_NODISCARD TGUI_API Layout2d bindPosition(const std::shared_ptr<Widget>& widget);
494
496 TGUI_NODISCARD TGUI_API Layout2d bindSize(const std::shared_ptr<Widget>& widget);
497
499 TGUI_NODISCARD TGUI_API Layout2d bindInnerSize(const std::shared_ptr<Container>& container);
500
502 TGUI_NODISCARD TGUI_API Layout bindWidth(const BackendGui& gui);
503
505 TGUI_NODISCARD TGUI_API Layout bindHeight(const BackendGui& gui);
506
508 TGUI_NODISCARD TGUI_API Layout2d bindSize(const BackendGui& gui);
509
511 TGUI_NODISCARD TGUI_API Layout bindMin(const Layout& value1, const Layout& value2);
512
514 TGUI_NODISCARD TGUI_API Layout bindMax(const Layout& value1, const Layout& value2);
515 }
516
518}
519
521
522#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:323
Layout2d(Vector2f constant={0, 0})
Default constructor to implicitly construct from a tgui::Vector2f.
Definition Layout.hpp:331
Layout2d(Layout layoutX, Layout layoutY)
Constructor to create the Layout2d from two Layout classes.
Definition Layout.hpp:343
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:356
Vector2f getValue() const
Returns the cached value of the layout.
Definition Layout.hpp:380
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:369
Class to store the left, top, width or height of a widget.
Definition Layout.hpp:102
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:107
Layout()=default
Default constructor.
float getValue() const
Return the cached value of the layout.
Definition Layout.hpp:220
Layout & operator=(Layout &&other) noexcept
Move assignment operator.
Layout(T constant)
Constructor to implicitly construct from numeric constant.
Definition Layout.hpp:140
Layout(const Layout &other)
Copy constructor.
~Layout()
Destructor.
void replaceValue(const Layout &value)
Replaces the value of the layout without overwriting its parent.
Layout(RelativeValue ratio)
Constructs the layout from a ratio relative to the parent size.
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:159
bool isConstant() const
Return whether the layout stores a constant value.
Definition Layout.hpp:230
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_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_API Layout2d bindInnerSize(const std::shared_ptr< Container > &container)
Bind to the inner size of the container widget.
TGUI_API Layout bindMax(const Layout &value1, const Layout &value2)
Bind to the maximum value of two layouts.
TGUI_API Layout bindInnerHeight(const std::shared_ptr< Container > &container)
Bind to the inner height of the container widget.
HorizontalAlignment
The horizontal alignment.
Definition Layout.hpp:62
@ Center
Center the object horizontally.
@ Right
Align to the right side.
@ Left
Align to the left side.
TGUI_API Layout2d bindPosition(const std::shared_ptr< Widget > &widget)
Bind to the position of the widget.
TGUI_API Layout bindInnerWidth(const std::shared_ptr< Container > &container)
Bind to the inner width of the container widget.
TGUI_API Layout bindLeft(const std::shared_ptr< Widget > &widget)
Bind to the left position of the widget.
Orientation
Orientation of the object.
Definition Layout.hpp:52
@ Vertical
Vertical orientation.
@ Horizontal
Horizontal orientation.
AutoLayout
Alignments for how to position a widget in its parent.
Definition Layout.hpp:85
@ 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_API Layout bindWidth(const std::shared_ptr< Widget > &widget)
Bind to the width of the widget.
TGUI_API Layout bindMin(const Layout &value1, const Layout &value2)
Bind to the minimum value of two layouts.
TGUI_API Layout bindHeight(const std::shared_ptr< Widget > &widget)
Bind to the height of the widget.
TGUI_API Layout bindBottom(const std::shared_ptr< Widget > &widget)
Bind to the bottom of the widget.
VerticalAlignment
The vertical alignment.
Definition Layout.hpp:73
@ Bottom
Align to the bottom.
@ Top
Align to the top.
TGUI_API Layout bindRight(const std::shared_ptr< Widget > &widget)
Bind to the right position of the widget.
TGUI_API Layout bindTop(const std::shared_ptr< Widget > &widget)
Bind to the top position of the widget.
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_API Layout2d bindSize(const std::shared_ptr< Widget > &widget)
Bind to the size of the widget.
Helper class to create an AbsoluteOrRelativeValue object containing a relative value without using a ...
Definition AbsoluteOrRelativeValue.hpp:166