TGUI  0.10-dev
Layout.hpp
1
2//
3// TGUI - Texus' Graphical User Interface
4// Copyright (C) 2012-2021 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 float getValue() const
161 {
162 return m_value;
163 }
164
165
171 bool isConstant() const
172 {
173 return m_operation == Operation::Value;
174 }
175
176
183 String toString() const;
184
185
193 void connectWidget(Widget* widget, bool xAxis, std::function<void()> valueChangedCallbackHandler);
194
195
200 void unbindWidget();
201
202
208 void recalculateValue();
209
210
212 private:
213
214
216 // If a widget is bound, inform it that the layout no longer binds it
218 void unbindLayout();
219
220
222 // Resets the parent pointers of the left and right operands if they exist and tell the bound widget that this layout
223 // requires information about changes to its position or size when the operation requires a widget to be bound.
225 void resetPointers();
226
227
229 // Check whether sublayouts contain a string that refers to a widget which should be bound.
231 void parseBindingStringRecursive(Widget* widget, bool xAxis);
232
233
235 // Find the widget corresponding to the given name and bind it if found
237 void parseBindingString(const String& expression, Widget* widget, bool xAxis);
238
239
241 private:
242
243 float m_value = 0;
244 Layout* m_parent = nullptr;
245 Operation m_operation = Operation::Value;
246 std::unique_ptr<Layout> m_leftOperand = nullptr; // The left operand of the operation in case the operation is a math operation
247 std::unique_ptr<Layout> m_rightOperand = nullptr; // The left operand of the operation in case the operation is a math operation
248 Widget* m_boundWidget = nullptr; // The widget on which this layout depends in case the operation is a binding
249 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
250 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
251
253 };
254
255
261 class TGUI_API Layout2d
262 {
263 public:
264
270 Layout2d(Vector2f constant = {0, 0}) :
271 x{constant.x},
272 y{constant.y}
273 {
274 }
275
276
283 Layout2d(Layout layoutX, Layout layoutY) :
284 x{std::move(layoutX)},
285 y{std::move(layoutY)}
286 {
287 }
288
289
297 Layout2d(const char* expression) :
298 x{expression},
299 y{expression}
300 {
301 }
302
303
311 Layout2d(const String& expression) :
312 x{expression},
313 y{expression}
314 {
315 }
316
317
324 {
325 return {x.getValue(), y.getValue()};
326 }
327
328
335 String toString() const
336 {
337 return "(" + x.toString() + ", " + y.toString() + ")";
338 }
339
341 public:
342
343 Layout x;
344 Layout y;
345 };
346
347
351 TGUI_API Layout operator-(Layout right);
352
356 TGUI_API Layout operator+(Layout left, Layout right);
357
361 TGUI_API Layout operator-(Layout left, 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 Layout2d operator-(Layout2d right);
377
381 TGUI_API Layout2d operator+(Layout2d left, Layout2d right);
382
386 TGUI_API Layout2d operator-(Layout2d left, Layout2d right);
387
391 TGUI_API Layout2d operator*(Layout2d left, const Layout& right);
392
396 TGUI_API Layout2d operator*(const Layout& left, Layout2d right);
397
401 TGUI_API Layout2d operator/(Layout2d left, const Layout& right);
402
403
405
406 inline namespace bind_functions
407 {
409 TGUI_API Layout bindPosX(std::shared_ptr<Widget> widget);
410
412 TGUI_API Layout bindPosY(std::shared_ptr<Widget> widget);
413
415 TGUI_API Layout bindLeft(std::shared_ptr<Widget> widget);
416
418 TGUI_API Layout bindTop(std::shared_ptr<Widget> widget);
419
421 TGUI_API Layout bindWidth(std::shared_ptr<Widget> widget);
422
424 TGUI_API Layout bindHeight(std::shared_ptr<Widget> widget);
425
427 TGUI_API Layout bindInnerWidth(std::shared_ptr<Container> container);
428
430 TGUI_API Layout bindInnerHeight(std::shared_ptr<Container> container);
431
433 TGUI_API Layout bindRight(std::shared_ptr<Widget> widget);
434
436 TGUI_API Layout bindBottom(std::shared_ptr<Widget> widget);
437
439 TGUI_API Layout2d bindPosition(std::shared_ptr<Widget> widget);
440
442 TGUI_API Layout2d bindSize(std::shared_ptr<Widget> widget);
443
445 TGUI_API Layout2d bindInnerSize(std::shared_ptr<Container> container);
446
449
452
455
457 TGUI_API Layout bindMin(const Layout& value1, const Layout& value2);
458
460 TGUI_API Layout bindMax(const Layout& value1, const Layout& value2);
461 }
462
464}
465
467
468#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:262
Layout2d(Vector2f constant={0, 0})
Default constructor to implicitly construct from a tgui::Vector2f.
Definition: Layout.hpp:270
Layout2d(Layout layoutX, Layout layoutY)
Constructor to create the Layout2d from two Layout classes.
Definition: Layout.hpp:283
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:297
Vector2f getValue() const
Returns the cached value of the layout.
Definition: Layout.hpp:323
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:311
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:160
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.
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:171
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.