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 (void)ctx;
9 if (on_click) {
10 on_click();
11 }
12}
13
15{
16 if (!enabled) {
17 return texture_disabled != nullptr ? texture_disabled : texture;
18 }
19 if (_pressed && texture_pressed != nullptr) {
20 return texture_pressed;
21 }
22 if (is_highlighted(ctx) && texture_hover != nullptr) {
23 return texture_hover;
24 }
25 return texture;
26}
27
28void asw::ui::Button::set_texture(const asw::Texture& tex, bool auto_size)
29{
30 texture = tex;
31 if (auto_size && texture != nullptr) {
32 const auto tex_size = asw::util::get_texture_size(texture);
33 transform.size = tex_size + asw::Vec2<float>(padding * 2.0f, padding * 2.0f);
34 }
35}
36
37void asw::ui::Button::set_images(const asw::Texture& normal, const asw::Texture& hover,
38 const asw::Texture& pressed, const asw::Texture& disabled, bool auto_size)
39{
40 texture_hover = hover;
41 texture_pressed = pressed;
42 texture_disabled = disabled;
43 draw_background = false;
44 set_texture(normal, auto_size);
45}
46
47void asw::ui::Button::set_text(const std::string& t, bool auto_size)
48{
49 text = t;
50 _fit_text = false;
51 if (!auto_size || text.empty()) {
52 return;
53 }
54
55 if (font != nullptr) {
56 fit_text(font);
57 } else {
58 // The theme font is only known at layout
59 _fit_text = true;
60 }
61}
62
64{
65 const auto size = asw::util::get_text_size(f, text);
66 transform.size = asw::Vec2<float>(
67 size.x + padding * 2.0f,
68 size.y + padding * 2.0f);
69}
70
72{
73 if (_fit_text) {
74 if (const auto& f = pick_font(font, ctx.theme); f != nullptr) {
75 fit_text(f);
76 _fit_text = false;
77 }
78 }
79}
80
82{
83 // A parent that places children without measuring them still gets a
84 // button sized to its text, one layout late
85 measure(ctx);
86 Widget::layout(ctx);
87}
88
90{
91 const auto& s = get_style(ctx);
92 const bool highlighted = enabled && is_highlighted(ctx);
93
94 if (draw_background) {
95 asw::Color bg = s.bg;
96 if (!enabled) {
97 bg = s.bg_disabled;
98 } else if (_pressed) {
99 bg = s.bg_pressed;
100 } else if (highlighted) {
101 bg = s.bg_hover;
102 }
103 asw::draw::rect_fill(transform, bg);
104 }
105
106 if (s.border_width > 0.0F) {
107 asw::draw::rect(transform, s.border, s.border_width);
108 }
109
110 const asw::Quad<float> inner {
111 { transform.position.x + padding, transform.position.y + padding },
112 { transform.size.x - padding * 2.0f, transform.size.y - padding * 2.0f }
113 };
114
115 if (const auto& tex = current_texture(ctx); tex != nullptr) {
116 asw::draw::stretch_sprite(tex, inner);
117 }
118
119 if (const auto& f = pick_font(font, ctx.theme); !text.empty() && f != nullptr) {
120 asw::Color color = s.text;
121 if (!enabled) {
122 color = s.text_disabled;
123 } else if (highlighted) {
124 color = s.text_hover;
125 }
126
127 const auto text_size = asw::util::get_text_size(f, text);
128 const float text_y
129 = inner.position.y + ((inner.size.y - static_cast<float>(text_size.y)) / 2.0F);
130
131 float text_x = inner.get_center().x - (text_size.x / 2.0F);
132 if (s.text_align == asw::TextJustify::Left) {
133 text_x = inner.position.x;
134 } else if (s.text_align == asw::TextJustify::Right) {
135 text_x = inner.position.x + inner.size.x - text_size.x;
136 }
137
138 asw::draw::text(f, text, { text_x, text_y }, color, asw::TextJustify::Left);
139 }
140
141 Widget::draw(ctx);
142}
Button widget for the ASW UI module.
A 2D rectangle in space.
Definition geometry.h:504
A 2D vector in space.
Definition geometry.h:27
void layout(Context &ctx) override
Measure, in case the parent did not, then lay out the children.
Definition button.cpp:81
std::function< void()> on_click
Callback invoked when the button is clicked.
Definition button.h:34
void draw(Context &ctx) override
Draw the button.
Definition button.cpp:89
void activate(Context &ctx) override
Calls on_click.
Definition button.cpp:6
void set_images(const asw::Texture &normal, const asw::Texture &hover=nullptr, const asw::Texture &pressed=nullptr, const asw::Texture &disabled=nullptr, bool auto_size=true)
Make an image button: set the state textures, turn off the background, and optionally size the button...
Definition button.cpp:37
void set_text(const std::string &t, bool auto_size=false)
Set the text, optionally resizing the button to match.
Definition button.cpp:47
void fit_text(const asw::Font &f)
Definition button.cpp:63
void measure(Context &ctx) override
Size to the text if set_text asked for it.
Definition button.cpp:71
const asw::Texture & current_texture(const Context &ctx) const
Get the texture for the current state.
Definition button.cpp:14
void set_texture(const asw::Texture &tex, bool auto_size=false)
Set the texture, optionally resizing the button to match.
Definition button.cpp:28
Shared state for the UI system.
Definition context.h:97
Theme theme
The current UI theme.
Definition context.h:104
virtual void draw(Context &ctx)
Draw this widget and its children.
Definition widget.cpp:163
virtual void layout(Context &ctx)
Lay out this widget and its children.
Definition widget.cpp:73
Routines for drawing sprites and primitives to the screen.
void rect(const asw::Quad< float > &position, asw::Color color, float thickness=1.0F)
Draw a rectangle.
Definition draw.cpp:504
void stretch_sprite(const asw::Texture &tex, const asw::Quad< float > &position)
Draw a sprite with the option to stretch it.
Definition draw.cpp:232
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. The position is rounded to whole pixels after justification so glyphs stay sharp....
Definition draw.cpp:357
void rect_fill(const asw::Quad< float > &position, asw::Color color)
Draw a filled rectangle.
Definition draw.cpp:536
const asw::Font & pick_font(const asw::Font &own, const Theme &theme)
The font a widget draws with: its own, or the theme font.
Definition theme.h:304
asw::Vec2< int > get_text_size(const asw::Font &font, std::string_view text)
Get text size.
Definition util.cpp:81
asw::Vec2< float > get_texture_size(const asw::Texture &tex)
Get texture size.
Definition util.cpp:74
std::shared_ptr< TTF_Font > Font
Alias for a shared pointer to an TTF_Font.
Definition types.h:79
std::shared_ptr< SDL_Texture > Texture
Alias for a shared pointer to an SDL_Texture.
Definition types.h:76
RGBA color struct with 8-bit channels.
Definition color.h:18
General utility functions.