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.h
Go to the documentation of this file.
1
8
9#ifndef ASW_MODULES_UI_CHOICE_H
10#define ASW_MODULES_UI_CHOICE_H
11
12#include <cstddef>
13#include <functional>
14#include <optional>
15#include <string>
16#include <vector>
17
18#include "button.h"
19
20namespace asw::ui {
21
32class Choice : public Button {
33public:
35 std::vector<std::string> options;
36
38 std::vector<asw::Texture> images;
39
41 std::size_t index = 0;
42
44 bool wrap = true;
45
48 std::optional<bool> adjust_on_left_right;
49
54 bool adjusts_left_right() const;
55
58 bool show_arrows = true;
59
61 std::function<void(std::size_t)> on_change;
62
67 std::size_t count() const;
68
73 void select(std::size_t i);
74
77 void next();
78
81 void prev();
82
87 void activate(Context& ctx) override;
88
95 bool on_event(Context& ctx, const UIEvent& e) override;
96
101 void draw(Context& ctx) override;
102
103private:
104 void step(int dir);
105};
106
107} // namespace asw::ui
108
109#endif // ASW_MODULES_UI_CHOICE_H
Button widget for the ASW UI module.
An interactive button widget.
Definition button.h:24
A button that cycles through a list of options, e.g. difficulty or window mode.
Definition choice.h:32
std::size_t index
Index of the selected option.
Definition choice.h:41
void select(std::size_t i)
Select an option by index without calling on_change.
Definition choice.cpp:19
std::function< void(std::size_t)> on_change
Callback invoked with the new index when the user changes it.
Definition choice.h:61
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
std::optional< bool > adjust_on_left_right
Whether left and right change the option. Empty to decide from the layout: off in a row with other fo...
Definition choice.h:48
void activate(Context &ctx) override
Move to the next option, then call on_click.
Definition choice.cpp:68
bool show_arrows
Show < and > at the sides while highlighted, when there is text and left and right change the option.
Definition choice.h:58
bool wrap
Wrap around at either end.
Definition choice.h:44
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
Event structure for UI interactions.
Definition event.h:21