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
modal.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4
7
13
15{
16 return style ? *style : ctx.theme.modal;
17}
18
19asw::ui::Label& asw::ui::Modal::add_text(const std::string& text, const asw::Font& font)
20{
21 auto& label = add_child<Label>();
22 label.font = font;
23 label.justify = asw::TextJustify::Center;
24 label.set_text(text, true);
25 return label;
26}
27
28asw::ui::Button& asw::ui::Modal::add_button(const std::string& text, std::function<void()> on_click)
29{
30 auto& button = add_child<Button>();
31 button.padding = 8.0F;
32 button.set_text(text, true);
33 button.on_click = std::move(on_click);
34 return button;
35}
36
38{
39 if (_closed) {
40 return;
41 }
42 _closed = true;
43
44 if (on_close) {
45 on_close();
46 }
47
48 // Kept alive by the parent until Root frees it
49 if (parent != nullptr) {
50 parent->remove_child(*this);
51 }
52}
53
55{
56 const auto& s = get_style(ctx);
57 padding = s.padding;
58 gap = s.gap;
59
60 float width = 0.0F;
61 float height = 0.0F;
62 int shown = 0;
63 for (auto const& c : children()) {
64 if (!c->visible) {
65 continue;
66 }
67 c->measure(ctx);
68 width = std::max(width, c->transform.size.x);
69 height += c->transform.size.y;
70 shown += 1;
71 }
72 if (shown > 1) {
73 height += gap * static_cast<float>(shown - 1);
74 }
75
76 transform.size = { std::max(s.min_width, width + (padding * 2.0F)), height + (padding * 2.0F) };
77}
78
80{
81 if (e.type == UIEvent::Type::Back && close_on_back) {
82 close();
83 return true;
84 }
85 return Stack::on_event(ctx, e);
86}
87
89{
90 const auto& s = get_style(ctx);
91 if (s.bg.a > 0) {
92 asw::draw::rect_fill(transform, s.bg);
93 }
94 if (s.border_width > 0.0F && s.border.a > 0) {
95 asw::draw::rect(transform, s.border, s.border_width);
96 }
97
98 Stack::draw(ctx);
99}
An interactive button widget.
Definition button.h:24
Shared state for the UI system.
Definition context.h:97
Theme theme
The current UI theme.
Definition context.h:104
A text display widget.
Definition label.h:23
void draw(Context &ctx) override
Draw the box, then the children.
Definition modal.cpp:88
bool on_event(Context &ctx, const UIEvent &e) override
Close on back.
Definition modal.cpp:79
Button & add_button(const std::string &text, std::function< void()> on_click)
Add a button, sized to its text.
Definition modal.cpp:28
void measure(Context &ctx) override
Size to the children, at least the style's min_width.
Definition modal.cpp:54
Label & add_text(const std::string &text, const asw::Font &font=nullptr)
Add a line of text, sized to fit.
Definition modal.cpp:19
void close()
Close the modal. Calls on_close, then takes it off the screen. Safe to call from its own buttons or f...
Definition modal.cpp:37
Modal()
Default constructor.
Definition modal.cpp:8
const ModalStyle & get_style(const Context &ctx) const
Get the style this modal draws with.
Definition modal.cpp:14
Align align
Where children sit across the stack.
Definition layout.h:106
Direction direction
Direction children are placed in.
Definition layout.h:103
virtual bool on_event(Context &ctx, const UIEvent &e)
Handle a UI event.
Definition widget.cpp:140
virtual void draw(Context &ctx)
Draw this widget and its children.
Definition widget.cpp:163
UI context and focus management for the ASW UI module.
Routines for drawing sprites and primitives to the screen.
Modal widget for the ASW UI module.
void rect(const asw::Quad< float > &position, asw::Color color, float thickness=1.0F)
Draw a rectangle.
Definition draw.cpp:504
void rect_fill(const asw::Quad< float > &position, asw::Color color)
Draw a filled rectangle.
Definition draw.cpp:536
std::shared_ptr< TTF_Font > Font
Alias for a shared pointer to an TTF_Font.
Definition types.h:79
Look of a Modal and the dimmed screen behind it.
Definition theme.h:214
ModalStyle modal
Modals from Root::open_modal.
Definition theme.h:282
Event structure for UI interactions.
Definition event.h:21
Type type
The type of the event.
Definition event.h:36