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
navigation.cpp
Go to the documentation of this file.
2
3#include <initializer_list>
4
6
7namespace {
8using namespace asw::input;
9
10std::string bind(
11 std::string_view prefix, std::string_view name, std::initializer_list<ActionBinding> bindings)
12{
13 std::string action { prefix };
14 action += name;
15
16 unbind_action(action);
17 for (const auto& binding : bindings) {
18 bind_action(action, binding);
19 }
20 return action;
21}
22
24{
25 return { .button = button, .controller_index = ANY_CONTROLLER };
26}
27
29{
30 return { .axis = axis,
31 .controller_index = ANY_CONTROLLER,
32 .threshold = 0.5F,
33 .positive_direction = positive };
34}
35} // namespace
36
38{
39 Navigation nav;
40
41 nav.up = bind(prefix, "up",
42 { KeyBinding { Key::Up }, pad(ControllerButton::DPadUp),
43 stick(ControllerAxis::LeftY, false) });
44 nav.down = bind(prefix, "down",
45 { KeyBinding { Key::Down }, pad(ControllerButton::DPadDown),
46 stick(ControllerAxis::LeftY, true) });
47 nav.left = bind(prefix, "left",
48 { KeyBinding { Key::Left }, pad(ControllerButton::DPadLeft),
49 stick(ControllerAxis::LeftX, false) });
50 nav.right = bind(prefix, "right",
51 { KeyBinding { Key::Right }, pad(ControllerButton::DPadRight),
52 stick(ControllerAxis::LeftX, true) });
53 nav.next
54 = bind(prefix, "next", { KeyBinding { Key::Tab }, pad(ControllerButton::RightShoulder) });
55 nav.prev = bind(prefix, "prev", { pad(ControllerButton::LeftShoulder) });
56 nav.activate = bind(prefix, "activate",
57 { KeyBinding { Key::Return }, KeyBinding { Key::Space }, pad(ControllerButton::A) });
58 nav.back = bind(prefix, "back", { KeyBinding { Key::Escape }, pad(ControllerButton::B) });
59
60 return nav;
61}
Action binding system for the ASW library.
std::string bind(std::string_view prefix, std::string_view name, std::initializer_list< ActionBinding > bindings)
ControllerAxisBinding stick(ControllerAxis axis, bool positive)
ControllerButtonBinding pad(ControllerButton button)
void bind_action(std::string_view name, ActionBinding binding)
Register a binding for a named action.
Definition action.cpp:151
constexpr uint32_t ANY_CONTROLLER
Pass as a controller index to read every connected controller.
Definition input.h:503
ControllerButton
Mappings from SDL game controller buttons to ASW buttons.
Definition input.h:459
void unbind_action(std::string_view name)
Remove all bindings for a named action.
Definition action.cpp:156
ControllerAxis
Mappings from SDL game controller axes to ASW axes.
Definition input.h:488
Navigation bind_default_navigation(std::string_view prefix="ui_")
Bind keyboard and controller actions for UI navigation and return their names. Calling it again repla...
Action based navigation for the ASW UI module.
Binding to a controller axis, activated when the axis exceeds a threshold.
Definition action.h:57
Binding to a controller (gamepad) button. Set controller_index to ANY_CONTROLLER to accept every conn...
Definition action.h:44
Binding to a keyboard key.
Definition action.h:33
Action names the UI reads for focus navigation, see asw/modules/action.h.
Definition navigation.h:33
std::string down
Move focus down.
Definition navigation.h:38
std::string back
Go back, sent to the focused widget and then Root::on_back.
Definition navigation.h:57
std::string right
Move focus right.
Definition navigation.h:44
std::string prev
Move focus to the previous widget.
Definition navigation.h:51
std::string up
Move focus up.
Definition navigation.h:35
std::string activate
Press the focused widget.
Definition navigation.h:54
std::string next
Move focus to the next widget. Holding Shift makes it go back, so Shift+Tab still works when Tab is b...
Definition navigation.h:48
std::string left
Move focus left.
Definition navigation.h:41