ASW Lib
A.D.S. Games SDL Wrapper Library. A library targeted at Allegro4 users who want to switch to SDL3 and use modern c++.
Loading...
Searching...
No Matches
layout.h
Go to the documentation of this file.
1
8
9#ifndef ASW_MODULES_UI_LAYOUT_H
10#define ASW_MODULES_UI_LAYOUT_H
11
12#include <cstddef>
13#include <vector>
14
15#include "context.h"
16#include "widget.h"
17
18namespace asw::ui {
19
22enum class Direction {
25};
26
30enum class Align {
31 Start,
32 Center,
33 End,
35 Stretch,
36};
37
38namespace detail {
39
45public:
53 float natural(const Widget& w, float current) const;
54
61 void record(const Widget& w, float natural, float forced);
62
68 void keep(const Widget& w);
69
72 void finish();
73
74private:
75 struct Entry {
77 float natural;
78 float forced;
79 };
80
81 // Find a child's record from last layout. Children come in the same
82 // order each layout, so the search starts where the last one ended.
83 const Entry* find(WidgetId id) const;
84
85 // Vectors, not maps, and kept between layouts, so a layout does not
86 // allocate
87 std::vector<Entry> _last;
88 std::vector<Entry> _next;
89 mutable std::size_t _cursor = 0;
90};
91
92} // namespace detail
93
100class Stack : public Widget {
101public:
104
107
109 float gap = 8.0F;
110
112 float padding = 0.0F;
113
118 void layout(Context& ctx) override;
119
120private:
121 // Sizes across the stack forced by Align::Stretch
123};
124
127class VBox : public Stack {
128public:
132 {
133 padding = 10.0F;
134 }
135};
136
144class Grid : public Widget {
145public:
147 std::size_t columns = 2;
148
150 float gap = 8.0F;
151
153 float padding = 0.0F;
154
156 float row_height = 0.0F;
157
162 void layout(Context& ctx) override;
163
164private:
165 // Heights forced by the row height
167
168 // Kept between layouts so a layout does not allocate
169 std::vector<Widget*> _shown;
170 std::vector<float> _natural_h;
171};
172
183bool in_row_with_focusables(const Widget& w);
184
185} // namespace asw::ui
186
187#endif // ASW_MODULES_UI_LAYOUT_H
Shared state for the UI system.
Definition context.h:97
Places children in a grid, filling each row left to right, e.g. a level select or a 2 by 2 menu.
Definition layout.h:144
float gap
Space between cells, both ways.
Definition layout.h:150
std::vector< float > _natural_h
Definition layout.h:170
float row_height
Height of every row, 0 for the tallest child in the row.
Definition layout.h:156
std::vector< Widget * > _shown
Definition layout.h:169
float padding
Space inside the grid on every side.
Definition layout.h:153
void layout(Context &ctx) override
Place the children.
Definition layout.cpp:90
detail::ForcedSizes _heights
Definition layout.h:166
std::size_t columns
Number of columns.
Definition layout.h:147
Places children one after another, top to bottom or left to right.
Definition layout.h:100
Align align
Where children sit across the stack.
Definition layout.h:106
float padding
Space inside the stack on every side.
Definition layout.h:112
detail::ForcedSizes _across
Definition layout.h:122
Direction direction
Direction children are placed in.
Definition layout.h:103
float gap
Space between children.
Definition layout.h:109
void layout(Context &ctx) override
Place the children.
Definition layout.cpp:47
A vertical stack with padding, for a column of rows.
Definition layout.h:127
VBox()
Default constructor.
Definition layout.h:131
Base class for all UI widgets.
Definition widget.h:43
Sizes a layout forced on its children, so a child gets its own size back when the layout stops forcin...
Definition layout.h:44
std::vector< Entry > _last
Definition layout.h:87
void keep(const Widget &w)
Keep last layout's record for a child skipped this layout, e.g. a hidden one.
Definition layout.cpp:33
void finish()
End the layout. Records not made or kept this layout are dropped.
Definition layout.cpp:40
const Entry * find(WidgetId id) const
Definition layout.cpp:6
float natural(const Widget &w, float current) const
The child's own size: its current size, unless that is still the size this layout forced on it last t...
Definition layout.cpp:19
void record(const Widget &w, float natural, float forced)
Record the size forced on a child this layout.
Definition layout.cpp:28
std::vector< Entry > _next
Definition layout.h:88
UI context and focus management for the ASW UI module.
bool in_row_with_focusables(const Widget &w)
Whether a widget sits in a row with other widgets that can take focus: its parent is a horizontal Sta...
Definition layout.cpp:138
uint32_t WidgetId
Unique identifier type for widgets.
Definition widget.h:39
Align
Where children sit across the stack: left to right for a vertical stack, top to bottom for a horizont...
Definition layout.h:30
@ Stretch
Children fill the stack across.
Direction
Stack direction.
Definition layout.h:22
Base widget class for the ASW UI module.