TGUI 1.13
Loading...
Searching...
No Matches
Layout.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2026 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
30#include <TGUI/AbsoluteOrRelativeValue.hpp>
31#include <TGUI/Vector2.hpp>
32
33#include <functional>
34#include <memory>
35#include <type_traits>
36
38
39namespace tgui
40{
41 class BackendGui;
42 class Widget;
43 class Container;
44
49 enum class Orientation
50 {
53 };
54
65
76
93
99 class TGUI_API Layout
100 {
101 public:
103 enum class Operation
104 {
105 Value,
106 Plus,
107 Minus,
108 Multiplies,
109 Divides,
110 Minimum,
111 Maximum,
112 BindingPosX, // X position, same as BindingLeft if widget origin isn't changed
113 BindingPosY, // Y position, same as BindingTop if widget origin isn't changed
114 BindingLeft,
115 BindingTop,
116 BindingWidth,
117 BindingHeight,
118 BindingInnerWidth,
119 BindingInnerHeight,
120 BindingString
121 };
122
124
125 public:
129 Layout() = default;
130
136 template <typename T, typename = typename std::enable_if_t<std::is_arithmetic_v<T>, T>>
137 Layout(T constant) :
138 m_value{static_cast<float>(constant)}
139 {
140 }
141
150
156 Layout(const char* expression) :
157 Layout{String{expression}}
158 {
159 }
160
166 Layout(String expression);
167
172 explicit Layout(Operation operation, Widget* boundWidget);
173
178 explicit Layout(Operation operation, std::unique_ptr<Layout> leftOperand, std::unique_ptr<Layout> rightOperand);
179
183 Layout(const Layout& other);
184
188 Layout(Layout&& other) noexcept;
189
193 Layout& operator=(const Layout& other);
194
198 Layout& operator=(Layout&& other) noexcept;
199
204
210 void replaceValue(const Layout& value);
211
217 [[nodiscard]] float getValue() const
218 {
219 return m_value;
220 }
221
227 [[nodiscard]] bool isConstant() const
228 {
229 return m_operation == Operation::Value;
230 }
231
238 [[nodiscard]] String toString() const;
239
247 void connectWidget(Widget* widget, bool xAxis, std::function<void()> valueChangedCallbackHandler);
248
253 void unbindWidget();
254
260 void recalculateValue();
261
266 [[nodiscard]] Layout* getLeftOperand() const;
267
272 [[nodiscard]] Layout* getRightOperand() const;
273
275
276 private:
278 // If a widget is bound, inform it that the layout no longer binds it
280 void unbindLayout();
281
283 // Resets the parent pointers of the left and right operands if they exist and tell the bound widget that this layout
284 // requires information about changes to its position or size when the operation requires a widget to be bound.
286 void resetPointers();
287
289 // Check whether sublayouts contain a string that refers to a widget which should be bound.
291 void parseBindingStringRecursive(Widget* widget, bool xAxis);
292
294 // Find the widget corresponding to the given name and bind it if found
296 void parseBindingString(const String& expression, Widget* widget, bool xAxis);
297
299
300 private:
301 float m_value = 0;
302 Layout* m_parent = nullptr;
303 Operation m_operation = Operation::Value;
304 std::unique_ptr<Layout> m_leftOperand = nullptr; // The left operand of the operation in case the operation is a math operation
305 std::unique_ptr<Layout> m_rightOperand = nullptr; // The left operand of the operation in case the operation is a math operation
306 Widget* m_boundWidget = nullptr; // The widget on which this layout depends in case the operation is a binding
307 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
308 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
309 int m_callingCallbackCount = 0; // Used to detect that connectWidget is called in an infinity loop if certain layouts depend on each other
310
312 };
313
319 class TGUI_API Layout2d
320 {
321 public:
327 Layout2d(Vector2f constant = {0, 0}) :
328 x{constant.x},
329 y{constant.y}
330 {
331 }
332
339 Layout2d(Layout layoutX, Layout layoutY) :
340 x{std::move(layoutX)},
341 y{std::move(layoutY)}
342 {
343 }
344
352 Layout2d(const char* expression) :
353 x{expression},
354 y{expression}
355 {
356 }
357
365 Layout2d(const String& expression) :
366 x{expression},
367 y{expression}
368 {
369 }
370
376 [[nodiscard]] Vector2f getValue() const
377 {
378 return {x.getValue(), y.getValue()};
379 }
380
387 [[nodiscard]] String toString() const
388 {
389 return U"(" + x.toString() + U", " + y.toString() + U")";
390 }
391
393
394 public:
395 Layout x;
396 Layout y;
397 };
398
402 [[nodiscard]] TGUI_API Layout operator-(Layout right);
403
407 [[nodiscard]] TGUI_API Layout operator+(Layout left, Layout right);
408
412 [[nodiscard]] TGUI_API Layout operator-(Layout left, Layout right);
413
417 [[nodiscard]] TGUI_API Layout operator*(Layout left, Layout right);
418
422 [[nodiscard]] TGUI_API Layout operator/(Layout left, Layout right);
423
427 [[nodiscard]] TGUI_API Layout2d operator-(Layout2d right);
428
432 [[nodiscard]] TGUI_API Layout2d operator+(Layout2d left, Layout2d right);
433
437 [[nodiscard]] TGUI_API Layout2d operator-(Layout2d left, Layout2d right);
438
442 [[nodiscard]] TGUI_API Layout2d operator*(Layout2d left, const Layout& right);
443
447 [[nodiscard]] TGUI_API Layout2d operator*(const Layout& left, Layout2d right);
448
452 [[nodiscard]] TGUI_API Layout2d operator/(Layout2d left, const Layout& right);
453
455
456 inline namespace bind_functions
457 {
459 [[nodiscard]] TGUI_API Layout bindPosX(const std::shared_ptr<Widget>& widget);
460
462 [[nodiscard]] TGUI_API Layout bindPosY(const std::shared_ptr<Widget>& widget);
463
465 [[nodiscard]] TGUI_API Layout bindLeft(const std::shared_ptr<Widget>& widget);
466
468 [[nodiscard]] TGUI_API Layout bindTop(const std::shared_ptr<Widget>& widget);
469
471 [[nodiscard]] TGUI_API Layout bindWidth(const std::shared_ptr<Widget>& widget);
472
474 [[nodiscard]] TGUI_API Layout bindHeight(const std::shared_ptr<Widget>& widget);
475
477 [[nodiscard]] TGUI_API Layout bindInnerWidth(const std::shared_ptr<Container>& container);
478
480 [[nodiscard]] TGUI_API Layout bindInnerHeight(const std::shared_ptr<Container>& container);
481
483 [[nodiscard]] TGUI_API Layout bindRight(const std::shared_ptr<Widget>& widget);
484
486 [[nodiscard]] TGUI_API Layout bindBottom(const std::shared_ptr<Widget>& widget);
487
489 [[nodiscard]] TGUI_API Layout2d bindPosition(const std::shared_ptr<Widget>& widget);
490
492 [[nodiscard]] TGUI_API Layout2d bindSize(const std::shared_ptr<Widget>& widget);
493
495 [[nodiscard]] TGUI_API Layout2d bindInnerSize(const std::shared_ptr<Container>& container);
496
498 [[nodiscard]] TGUI_API Layout bindWidth(const BackendGui& gui);
499
501 [[nodiscard]] TGUI_API Layout bindHeight(const BackendGui& gui);
502
504 [[nodiscard]] TGUI_API Layout2d bindSize(const BackendGui& gui);
505
507 [[nodiscard]] TGUI_API Layout bindMin(const Layout& value1, const Layout& value2);
508
510 [[nodiscard]] TGUI_API Layout bindMax(const Layout& value1, const Layout& value2);
511 } // namespace bind_functions
512} // namespace tgui
513
514#endif // TGUI_LAYOUT_HPP
Base class for the Gui.
Definition BackendGui.hpp:45
Container widget.
Definition Container.hpp:45
Class to store the position or size of a widget.
Definition Layout.hpp:320
Layout2d(Vector2f constant={0, 0})
Default constructor to implicitly construct from a tgui::Vector2f.
Definition Layout.hpp:327
Layout2d(Layout layoutX, Layout layoutY)
Constructor to create the Layout2d from two Layout classes.
Definition Layout.hpp:339
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:352
Vector2f getValue() const
Returns the cached value of the layout.
Definition Layout.hpp:376
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:365
Class to store the left, top, width or height of a widget.
Definition Layout.hpp:100
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:104
Layout()=default
Default constructor.
float getValue() const
Return the cached value of the layout.
Definition Layout.hpp:217
Layout & operator=(Layout &&other) noexcept
Move assignment operator.
Layout(T constant)
Constructor to implicitly construct from numeric constant.
Definition Layout.hpp:137
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:156
bool isConstant() const
Return whether the layout stores a constant value.
Definition Layout.hpp:227
Layout & operator=(const Layout &other)
Copy assignment operator.
Wrapper class to store strings.
Definition String.hpp:94
The parent class for every widget.
Definition Widget.hpp:83
Namespace that contains all TGUI functions and classes.
Definition AbsoluteOrRelativeValue.hpp:37
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:60
@ Center
Center the object horizontally.
Definition Layout.hpp:62
@ Right
Align to the right side.
Definition Layout.hpp:63
@ Left
Align to the left side.
Definition Layout.hpp:61
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:50
@ Vertical
Vertical orientation.
Definition Layout.hpp:51
@ Horizontal
Horizontal orientation.
Definition Layout.hpp:52
AutoLayout
Alignments for how to position a widget in its parent.
Definition Layout.hpp:83
@ Leftmost
Places the widget on the left side and sets height to 100%. Width needs to be manually set....
Definition Layout.hpp:89
@ Rightmost
Places the widget on the right side and sets height to 100%. Width needs to be manually set....
Definition Layout.hpp:90
@ Fill
Sets the position and size to fill the entire area that isn't already taken by components with the ot...
Definition Layout.hpp:91
@ Manual
Position and size need to be manually set. This is the default.
Definition Layout.hpp:84
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:71
@ Bottom
Align to the bottom.
Definition Layout.hpp:74
@ Top
Align to the top.
Definition Layout.hpp:72
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:163