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
67void asw::ui::Button::set_texture(const asw::Texture& tex, bool auto_size)
68{
69 texture = tex;
70 if (auto_size && texture != nullptr) {
71 const auto tex_size = asw::util::get_texture_size(texture);
72 transform.size = tex_size + asw::Vec2<float>(padding * 2.0f, padding * 2.0f);
73 }
74}
75
76void asw::ui::Button::set_text(const std::string& t, bool auto_size)
77{
78 text = t;
79 if (auto_size && font != nullptr && !text.empty()) {
80 const auto size = asw::util::get_text_size(font, text);
81 transform.size = asw::Vec2<float>(
82 size.x + padding * 2.0f,
83 size.y + padding * 2.0f);
84 }
85}
86
88{
89 asw::Color bg = ctx.theme.btn_bg;
90 if (!enabled) {
91 bg = ctx.theme.panel_bg;
92 } else if (_pressed) {
93 bg = ctx.theme.btn_pressed;
94 } else if (_hovered) {
95 bg = ctx.theme.btn_hover;
96 }
97
98 asw::draw::rect_fill(transform, bg);
99
100 const asw::Quad<float> inner {
101 { transform.position.x + padding, transform.position.y + padding },
102 { transform.size.x - padding * 2.0f, transform.size.y - padding * 2.0f }
103 };
104
105 if (texture != nullptr) {
106 asw::draw::stretch_sprite(texture, inner);
107 }
108
109 if (!text.empty() && font != nullptr) {
110 const auto text_size = asw::util::get_text_size(font, text);
111 const auto text_pos
112 = inner.get_center() - asw::Vec2<float>(text_size.x / 2.0f, text_size.y / 2.0f);
113
114 asw::draw::text(font, text, text_pos, ctx.theme.text, asw::TextJustify::Left);
115 }
116
117 if (_focused && ctx.theme.show_focus) {
118 // Focus ring
119 auto ring = asw::Quad<float>(transform);
120 ring.position.x -= 2;
121 ring.position.y -= 2;
122 ring.size.x += 4;
123 ring.size.y += 4;
125 }
126
127 Widget::draw(ctx);
128}
Button widget for the ASW UI module.
A 2D rectangle in space.
Definition geometry.h:433
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:87
void set_text(const std::string &t, bool auto_size=false)
Set the text, optionally resizing the button to match.
Definition button.cpp:76
void set_texture(const asw::Texture &tex, bool auto_size=false)
Set the texture, optionally resizing the button to match.
Definition button.cpp:67
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 stretch_sprite(const asw::Texture &tex, const asw::Quad< float > &position)
Draw a sprite with the option to stretch it.
Definition draw.cpp:70
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:156
void rect(const asw::Quad< float > &position, asw::Color color)
Draw a rectangle.
Definition draw.cpp:212
void rect_fill(const asw::Quad< float > &position, asw::Color color)
Draw a filled rectangle.
Definition draw.cpp:229
asw::Vec2< float > get_texture_size(const asw::Texture &tex)
Get texture size.
Definition util.cpp:15
asw::Vec2< int > get_text_size(const asw::Font &font, const std::string &text)
Get text size.
Definition util.cpp:22
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:38
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:46
Type type
The type of the event.
Definition event.h:37
General utility functions.