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.h
Go to the documentation of this file.
1
8
9#ifndef ASW_MODULES_UI_BUTTON_H
10#define ASW_MODULES_UI_BUTTON_H
11
12#include <functional>
13#include <optional>
14#include <string>
15
16#include "../types.h"
17#include "context.h"
18#include "widget.h"
19
20namespace asw::ui {
21
24class Button : public Widget {
25public:
29 {
30 focusable = true;
31 }
32
34 std::function<void()> on_click;
35
40 void activate(Context& ctx) override;
41
46 void draw(Context& ctx) override;
47
49 float padding = 0.0f;
50
53
55 std::string text;
56
59
62
65
68
71 bool draw_background = true;
72
74 std::optional<ButtonStyle> style;
75
81 const ButtonStyle& get_style(const Context& ctx) const
82 {
83 return style ? *style : ctx.theme.button;
84 }
85
91 void set_texture(const asw::Texture& tex, bool auto_size = false);
92
102 void set_images(const asw::Texture& normal, const asw::Texture& hover = nullptr,
103 const asw::Texture& pressed = nullptr, const asw::Texture& disabled = nullptr,
104 bool auto_size = true);
105
114 void set_text(const std::string& t, bool auto_size = false);
115
120 void measure(Context& ctx) override;
121
126 void layout(Context& ctx) override;
127
128protected:
134 const asw::Texture& current_texture(const Context& ctx) const;
135
136private:
137 // set_text asked to fit the text, waiting for the theme font at layout
138 bool _fit_text = false;
139
140 void fit_text(const asw::Font& f);
141};
142
143} // namespace asw::ui
144
145#endif // ASW_MODULES_UI_BUTTON_H
An interactive button widget.
Definition button.h:24
const ButtonStyle & get_style(const Context &ctx) const
Get the style this button draws with.
Definition button.h:81
void layout(Context &ctx) override
Measure, in case the parent did not, then lay out the children.
Definition button.cpp:81
std::optional< ButtonStyle > style
Style for this button only. Uses the theme button style when empty.
Definition button.h:74
asw::Texture texture_pressed
Texture shown while pressed. Falls back to texture_hover, then texture.
Definition button.h:64
asw::Texture texture_hover
Texture shown while hovered or focused. Falls back to texture.
Definition button.h:61
std::function< void()> on_click
Callback invoked when the button is clicked.
Definition button.h:34
asw::Texture texture_disabled
Texture shown while disabled. Falls back to texture.
Definition button.h:67
bool draw_background
Fill the button with the theme background. Turn off for buttons that are only an image.
Definition button.h:71
asw::Texture texture
The texture to display on the button.
Definition button.h:58
void draw(Context &ctx) override
Draw the button.
Definition button.cpp:89
void activate(Context &ctx) override
Calls on_click.
Definition button.cpp:6
std::string text
The button text.
Definition button.h:55
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
asw::Font font
The font for the text. Uses the theme font when empty.
Definition button.h:52
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
float padding
Padding applied inside the button on all sides.
Definition button.h:49
Button()
Default constructor.
Definition button.h:28
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
Base class for all UI widgets.
Definition widget.h:43
bool focusable
Whether the widget can receive focus. Focusable widgets are also the ones the pointer can press.
Definition widget.h:88
UI context and focus management for the ASW UI module.
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
How a button looks. Set on Theme::button for every button, or on Button::style for one button.
Definition theme.h:24
ButtonStyle button
Default button style.
Definition theme.h:264
Types used throughout the ASW library.
Base widget class for the ASW UI module.