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
button.cpp
Go to the documentation of this file.
2
5
7{
8 focused_ = focused;
9 (void)ctx;
10}
11
13{
14 if (!enabled) {
15 return false;
16 }
17
18 switch (e.type) {
20 hovered_ = true;
21 return false;
22 }
24 hovered_ = false;
25 pressed_ = false;
26 return false;
27 }
29 return false;
30 }
32 if (transform.contains(e.pointer_pos)) {
33 pressed_ = true;
34 ctx.pointer_capture = this;
35 ctx.focus.set_focus(ctx, this);
36 return true;
37 }
38 return false;
39 }
41 const bool in = transform.contains(e.pointer_pos);
42 const bool wasPressed = pressed_;
43 pressed_ = false;
44 if (ctx.pointer_capture == this) {
45 ctx.pointer_capture = nullptr;
46 }
47 if (wasPressed && in) {
48 if (on_click) {
49 on_click();
50 }
51 return true;
52 }
53 return false;
54 }
56 if (on_click) {
57 on_click();
58 }
59 return true;
60 }
61 default:
62 break;
63 }
64 return false;
65}
66
68{
69 asw::Color bg = ctx.theme.btn_bg;
70 if (!enabled) {
71 bg = ctx.theme.panel_bg;
72 } else if (pressed_) {
73 bg = ctx.theme.btn_pressed;
74 } else if (hovered_) {
75 bg = ctx.theme.btn_hover;
76 }
77
78 asw::draw::rect_fill(transform, bg);
79
80 if (!text.empty() && font != nullptr) {
81 const auto text_size = asw::util::get_text_size(font, text);
82 const auto text_pos
83 = transform.get_center() - asw::Vec2<float>(text_size.x / 2.0f, text_size.y / 2.0f);
84
85 asw::draw::text(font, text, text_pos, ctx.theme.text, asw::TextJustify::Left);
86 }
87
88 if (focused_ && ctx.theme.show_focus) {
89 // Focus ring
90 auto ring = asw::Quad<float>(transform);
91 ring.position.x -= 2;
92 ring.position.y -= 2;
93 ring.size.x += 4;
94 ring.size.y += 4;
96 }
97
98 Widget::draw(ctx);
99}
Button widget for the ASW UI module.
A 2D rectangle in space.
Definition geometry.h:388
A 2D vector in space.
Definition geometry.h:21
void on_focus_changed(Context &ctx, bool focused) override
Called when focus state changes.
Definition button.cpp:6
bool on_event(Context &ctx, const UIEvent &e) override
Handle a UI event.
Definition button.cpp:12
void draw(Context &ctx) override
Draw the button.
Definition button.cpp:67
bool focused_
Definition button.h:65
Shared state for the UI system.
Definition context.h:75
Widget * pointer_capture
The widget that has captured pointer input.
Definition context.h:88
FocusManager focus
The focus manager.
Definition context.h:85
Theme theme
The current UI theme.
Definition context.h:82
void set_focus(Context &ctx, Widget *w)
Set focus to a specific widget.
Definition context.cpp:22
virtual void draw(Context &ctx)
Draw this widget and its children.
Definition widget.cpp:26
Routines for drawing sprites and primitives to the screen.
void text(const asw::Font &font, const std::string &text, const asw::Vec2< float > &position, asw::Color color, asw::TextJustify justify=asw::TextJustify::Left)
Draw text.
Definition draw.cpp:151
void rect(const asw::Quad< float > &position, asw::Color color)
Draw a rectangle.
Definition draw.cpp:204
void rect_fill(const asw::Quad< float > &position, asw::Color color)
Draw a filled rectangle.
Definition draw.cpp:220
asw::Vec2< int > get_text_size(const asw::Font &font, const std::string &text)
Get text size.
Definition util.cpp:22
RGBA color struct with 8-bit channels.
Definition color.h:11
asw::Color text
Default text color.
Definition theme.h:20
bool show_focus
Show focus rings around focused widgets. Enabled by default when using keyboard navigation.
Definition theme.h:51
asw::Color panel_bg
Panel background color.
Definition theme.h:26
asw::Color btn_pressed
Button pressed color.
Definition theme.h:35
asw::Color btn_focus_ring
Button focus ring color.
Definition theme.h:38
asw::Color btn_hover
Button hover color.
Definition theme.h:32
asw::Color btn_bg
Button background color.
Definition theme.h:29
Event structure for UI interactions.
Definition event.h:21
asw::Vec2< float > pointer_pos
The pointer position.
Definition event.h:49
Type type
The type of the event.
Definition event.h:37
General utility functions.