openMSX
BooleanInput.hh
Go to the documentation of this file.
1#ifndef BOOLEAN_INPUT_HH
2#define BOOLEAN_INPUT_HH
3
4// A 'BooleanInput' represents an input which has a well defined start and stop.
5//
6// Some examples:
7// * Press a key on the keyboard, later release that key.
8// * Same for joystick and mouse buttons.
9// * The 4 directions on a joystick hat (d-pad): active when e.g. pressing the
10// 'up' direction, it remains active when the 'up-left' or 'up-right'
11// combination is pressed, but becomes deactive for any other combination
12// (including 'center').
13// * Joystick axis: becomes active when moving the axis in e.g. the positive
14// direction (the negative direction is a different BooleanInput) past the
15// dead-zone in the center. Becomes deactive when moving the axis back to the
16// center or in the opposite direction.
17//
18// Counter-examples:
19// * Mouse-wheel.
20// * Mouse motion.
21
22#include "Event.hh"
23#include "JoystickId.hh"
24
25#include <SDL.h>
26
27#include <cstdint>
28#include <functional>
29#include <optional>
30#include <string>
31#include <string_view>
32#include <variant>
33
34namespace openmsx {
35
37{
38public:
39 explicit BooleanKeyboard(SDL_Keycode code)
40 : keyCode(code) {}
41
42 [[nodiscard]] auto getKeyCode() const { return keyCode; }
43
44private:
45 SDL_Keycode keyCode;
46};
47
49{
50public:
51 explicit BooleanMouseButton(uint8_t button_)
52 : button(button_) {}
53
54 [[nodiscard]] auto getButton() const { return button; }
55
56private:
57 uint8_t button;
58};
59
61{
62public:
63 explicit BooleanJoystickButton(JoystickId joystick_, uint8_t button_)
64 : joystick(joystick_), button(button_) {}
65
66 [[nodiscard]] auto getJoystick() const { return joystick; }
67 [[nodiscard]] auto getButton() const { return button; }
68
69private:
70 JoystickId joystick;
71 uint8_t button;
72};
73
75{
76public:
77 enum Value : uint8_t {
78 UP = SDL_HAT_UP,
79 RIGHT = SDL_HAT_RIGHT,
80 DOWN = SDL_HAT_DOWN,
81 LEFT = SDL_HAT_LEFT,
82 };
83
84 explicit BooleanJoystickHat(JoystickId joystick_, uint8_t hat_, Value value_)
85 : joystick(joystick_), hat(hat_), value(value_) {}
86
87 [[nodiscard]] auto getJoystick() const { return joystick; }
88 [[nodiscard]] auto getHat() const { return hat; }
89 [[nodiscard]] auto getValue() const { return value; }
90
91private:
92 JoystickId joystick;
93 uint8_t hat;
94 Value value;
95};
96
98{
99public:
100 enum Direction : uint8_t { POS = 0, NEG = 1 };
101
102 explicit BooleanJoystickAxis(JoystickId joystick_, uint8_t axis_, Direction direction_)
103 : joystick(joystick_), axis(axis_), direction(direction_) {}
104
105 [[nodiscard]] auto getJoystick() const { return joystick; }
106 [[nodiscard]] auto getAxis() const { return axis; }
107 [[nodiscard]] auto getDirection() const { return direction; }
108
109private:
110 JoystickId joystick;
111 uint8_t axis;
112 Direction direction;
113};
114
115
116using BooleanInput = std::variant<
122
123
124[[nodiscard]] std::string toString(const BooleanInput& input);
125[[nodiscard]] std::optional<BooleanInput> parseBooleanInput(std::string_view text);
126[[nodiscard]] std::optional<BooleanInput> captureBooleanInput(const Event& event, std::function<int(JoystickId)> getJoyDeadZone);
127
128[[nodiscard]] bool operator==(const BooleanInput& x, const BooleanInput& y);
129
130[[nodiscard]] std::optional<bool> match(const BooleanInput& binding, const Event& event,
131 std::function<int(JoystickId)> getJoyDeadZone);
132
133} // namespace openmsx
134
135#endif
BooleanJoystickAxis(JoystickId joystick_, uint8_t axis_, Direction direction_)
BooleanJoystickButton(JoystickId joystick_, uint8_t button_)
BooleanJoystickHat(JoystickId joystick_, uint8_t hat_, Value value_)
BooleanKeyboard(SDL_Keycode code)
BooleanMouseButton(uint8_t button_)
This file implemented 3 utility functions:
Definition Autofire.cc:9
std::optional< BooleanInput > captureBooleanInput(const Event &event, std::function< int(JoystickId)> getJoyDeadZone)
bool operator==(const BooleanInput &x, const BooleanInput &y)
std::variant< KeyUpEvent, KeyDownEvent, MouseMotionEvent, MouseButtonUpEvent, MouseButtonDownEvent, MouseWheelEvent, JoystickAxisMotionEvent, JoystickHatEvent, JoystickButtonUpEvent, JoystickButtonDownEvent, OsdControlReleaseEvent, OsdControlPressEvent, WindowEvent, TextEvent, FileDropEvent, QuitEvent, FinishFrameEvent, CliCommandEvent, GroupEvent, BootEvent, FrameDrawnEvent, BreakEvent, SwitchRendererEvent, TakeReverseSnapshotEvent, AfterTimedEvent, MachineLoadedEvent, MachineActivatedEvent, MachineDeactivatedEvent, MidiInReaderEvent, MidiInWindowsEvent, MidiInCoreMidiEvent, MidiInCoreMidiVirtualEvent, MidiInALSAEvent, Rs232TesterEvent, ImGuiDelayedActionEvent, ImGuiActiveEvent > Event
Definition Event.hh:452
std::optional< bool > match(const BooleanInput &binding, const Event &event, std::function< int(JoystickId)> getJoyDeadZone)
std::optional< BooleanInput > parseBooleanInput(std::string_view text)
std::variant< BooleanKeyboard, BooleanMouseButton, BooleanJoystickButton, BooleanJoystickHat, BooleanJoystickAxis > BooleanInput
std::string toString(const BooleanInput &input)