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
checkbox.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4
7
9{
11 if (on_change) {
13 }
15}
16
18{
19 const auto& s = get_checkbox_style(ctx);
20 const bool highlighted = enabled && is_highlighted(ctx);
21
22 // Row background and border, behind images too
23 if (draw_background) {
24 asw::draw::rect_fill(transform, highlighted ? s.bg_hover : s.bg);
25 }
26 if (s.border_width > 0.0F) {
27 asw::draw::rect(transform, s.border, s.border_width);
28 }
29
30 const asw::Quad<float> inner {
31 { transform.position.x + padding, transform.position.y + padding },
32 { transform.size.x - (padding * 2.0F), transform.size.y - (padding * 2.0F) }
33 };
34
35 // Image checkbox
36 const auto& unchecked_tex = current_texture(ctx);
37 if (unchecked_tex != nullptr) {
38 const asw::Texture* tex = &unchecked_tex;
39 if (checked && enabled) {
40 if (highlighted && texture_checked_hover != nullptr) {
41 tex = &texture_checked_hover;
42 } else if (texture_checked != nullptr) {
43 tex = &texture_checked;
44 }
45 } else if (checked && texture_checked != nullptr) {
46 tex = &texture_checked;
47 }
48
49 asw::draw::stretch_sprite(*tex, inner);
50 } else {
51 // Box as tall as the widget, text on the other side
52 const float box_size = std::min(inner.size.x, inner.size.y);
53 const bool box_right = s.box_side == BoxSide::Right;
54 const float box_x = box_right ? inner.position.x + inner.size.x - box_size : inner.position.x;
55 const asw::Quad<float> box { { box_x, inner.position.y }, { box_size, box_size } };
56
57 asw::Color box_color = s.box;
58 if (!enabled) {
59 box_color = s.box_disabled;
60 } else if (_pressed) {
61 box_color = s.box_pressed;
62 } else if (highlighted) {
63 box_color = s.box_hover;
64 }
65 asw::draw::rect_fill(box, box_color);
66
67 if (s.box_border_width > 0.0F) {
68 asw::draw::rect(box, s.box_border, s.box_border_width);
69 }
70
71 if (checked) {
72 const float inset = std::max(3.0F, box_size / 4.0F);
73 const asw::Quad<float> mark { { box.position.x + inset, box.position.y + inset },
74 { box_size - (inset * 2.0F), box_size - (inset * 2.0F) } };
75 asw::draw::rect_fill(mark, enabled ? s.mark : s.mark_disabled);
76 }
77
78 if (const auto& f = pick_font(font, ctx.theme); !text.empty() && f != nullptr) {
79 const auto text_size = asw::util::get_text_size(f, text);
80 const float text_y
81 = inner.position.y + ((inner.size.y - static_cast<float>(text_size.y)) / 2.0F);
82 const float text_x
83 = box_right ? inner.position.x : inner.position.x + box_size + ctx.theme.gap;
84 asw::draw::text(f, text, { text_x, text_y }, enabled ? s.text : s.text_disabled,
86 }
87 }
88
89 // Children, skipping Button::draw which would draw the button again
90 Widget::draw(ctx);
91}
Checkbox widget for the ASW UI module.
A 2D rectangle in space.
Definition geometry.h:504
void activate(Context &ctx) override
Calls on_click.
Definition button.cpp:6
void draw(Context &ctx) override
Draw the checkbox.
Definition checkbox.cpp:17
std::function< void(bool)> on_change
Callback invoked when checked changes by user input.
Definition checkbox.h:45
bool checked
Whether the box is checked.
Definition checkbox.h:42
void activate(Context &ctx) override
Toggle checked, then call on_change and on_click.
Definition checkbox.cpp:8
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
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
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
float gap
Default gap between elements.
Definition theme.h:288
General utility functions.