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
choice.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4
8
9std::size_t asw::ui::Choice::count() const
10{
11 return std::max(options.size(), images.size());
12}
13
15{
16 return adjust_on_left_right.value_or(!in_row_with_focusables(*this));
17}
18
19void asw::ui::Choice::select(std::size_t i)
20{
21 const auto n = count();
22 index = n == 0 ? 0 : std::min(i, n - 1);
23}
24
26{
27 const auto n = count();
28 if (n == 0) {
29 return;
30 }
31
32 // options may have shrunk since the last change, step from a valid index
33 select(index);
34
35 std::size_t next_index = index;
36 if (dir > 0) {
37 if (index + 1 < n) {
38 next_index = index + 1;
39 } else if (wrap) {
40 next_index = 0;
41 }
42 } else {
43 if (index > 0) {
44 next_index = index - 1;
45 } else if (wrap) {
46 next_index = n - 1;
47 }
48 }
49
50 if (next_index != index) {
51 index = next_index;
52 if (on_change) {
53 on_change(index);
54 }
55 }
56}
57
59{
60 step(1);
61}
62
64{
65 step(-1);
66}
67
69{
70 next();
72}
73
75{
76 (void)ctx;
77 if (!enabled || e.type != UIEvent::Type::KeyDown || !adjusts_left_right()) {
78 return false;
79 }
80 if (e.key == asw::input::Key::Left) {
81 prev();
82 return true;
83 }
84 if (e.key == asw::input::Key::Right) {
85 next();
86 return true;
87 }
88 return false;
89}
90
92{
93 select(index);
94
95 // Show the selected option through the Button fields
96 text = index < options.size() ? options[index] : std::string {};
97 texture = index < images.size() ? images[index] : asw::Texture {};
98
99 Button::draw(ctx);
100
101 const auto& f = pick_font(font, ctx.theme);
102 if (!show_arrows || !adjusts_left_right() || text.empty() || f == nullptr || !enabled
103 || !is_highlighted(ctx)) {
104 return;
105 }
106
107 const auto& s = get_style(ctx);
108 const auto arrow_size = asw::util::get_text_size(f, "<");
109 const float y = transform.position.y
110 + ((transform.size.y - static_cast<float>(arrow_size.y)) / 2.0F);
111 const float inset = padding + 4.0F;
112
113 asw::draw::text(f, "<", { transform.position.x + inset, y }, s.text_hover);
114 asw::draw::text(f, ">",
115 { transform.position.x + transform.size.x - inset - static_cast<float>(arrow_size.x), y },
116 s.text_hover);
117}
Choice widget for the ASW UI module.
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 select(std::size_t i)
Select an option by index without calling on_change.
Definition choice.cpp:19
bool on_event(Context &ctx, const UIEvent &e) override
Handle left and right while focused.
Definition choice.cpp:74
std::vector< asw::Texture > images
Option textures, one per option. Optional.
Definition choice.h:38
void prev()
Move to the previous option, calls on_change.
Definition choice.cpp:63
void activate(Context &ctx) override
Move to the next option, then call on_click.
Definition choice.cpp:68
void draw(Context &ctx) override
Draw the selected option.
Definition choice.cpp:91
bool adjusts_left_right() const
Whether left and right change the option right now.
Definition choice.cpp:14
void next()
Move to the next option, calls on_change.
Definition choice.cpp:58
std::size_t count() const
Number of options, the larger of options and images.
Definition choice.cpp:9
std::vector< std::string > options
Option labels.
Definition choice.h:35
void step(int dir)
Definition choice.cpp:25
Shared state for the UI system.
Definition context.h:97
Theme theme
The current UI theme.
Definition context.h:104
Routines for drawing sprites and primitives to the screen.
Layout widgets for the ASW UI module.
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
bool in_row_with_focusables(const Widget &w)
Whether a widget sits in a row with other widgets that can take focus: its parent is a horizontal Sta...
Definition layout.cpp:138
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
Event structure for UI interactions.
Definition event.h:21
asw::input::Key key
The key associated with the event.
Definition event.h:39
Type type
The type of the event.
Definition event.h:36
General utility functions.