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 "function_ref.hh"
26
27#include <SDL.h>
28
29#include <cstdint>
30#include <optional>
31#include <string>
32#include <string_view>
33#include <variant>
34
35namespace openmsx {
36
38{
39public:
40 explicit BooleanKeyboard(SDL_Keycode code)
41 : keyCode(code) {}
42
43 [[nodiscard]] auto getKeyCode() const { return keyCode; }
44
45private:
46 SDL_Keycode keyCode;
47};
48
50{
51public:
52 explicit BooleanMouseButton(uint8_t button_)
53 : button(button_) {}
54
55 [[nodiscard]] auto getButton() const { return button; }
56
57private:
58 uint8_t button;
59};
60
62{
63public:
64 explicit BooleanJoystickButton(JoystickId joystick_, uint8_t button_)
65 : joystick(joystick_), button(button_) {}
66
67 [[nodiscard]] auto getJoystick() const { return joystick; }
68 [[nodiscard]] auto getButton() const { return button; }
69
70private:
71 JoystickId joystick;
72 uint8_t button;
73};
74
76{
77public:
78 enum Value : uint8_t {
79 UP = SDL_HAT_UP,
80 RIGHT = SDL_HAT_RIGHT,
81 DOWN = SDL_HAT_DOWN,
82 LEFT = SDL_HAT_LEFT,
83 };
84
85 explicit BooleanJoystickHat(JoystickId joystick_, uint8_t hat_, Value value_)
86 : joystick(joystick_), hat(hat_), value(value_) {}
87
88 [[nodiscard]] auto getJoystick() const { return joystick; }
89 [[nodiscard]] auto getHat() const { return hat; }
90 [[nodiscard]] auto getValue() const { return value; }
91
92private:
93 JoystickId joystick;
94 uint8_t hat;
95 Value value;
96};
97
99{
100public:
101 enum Direction : uint8_t { POS = 0, NEG = 1 };
102
103 explicit BooleanJoystickAxis(JoystickId joystick_, uint8_t axis_, Direction direction_)
104 : joystick(joystick_), axis(axis_), direction(direction_) {}
105
106 [[nodiscard]] auto getJoystick() const { return joystick; }
107 [[nodiscard]] auto getAxis() const { return axis; }
108 [[nodiscard]] auto getDirection() const { return direction; }
109
110private:
111 JoystickId joystick;
112 uint8_t axis;
113 Direction direction;
114};
115
116
117using BooleanInput = std::variant<
123
124
125[[nodiscard]] std::string toString(const BooleanInput& input);
126[[nodiscard]] std::optional<BooleanInput> parseBooleanInput(std::string_view text);
127[[nodiscard]] std::optional<BooleanInput> captureBooleanInput(const Event& event, function_ref<int(JoystickId)> getJoyDeadZone);
128
129[[nodiscard]] bool operator==(const BooleanInput& x, const BooleanInput& y);
130
131[[nodiscard]] std::optional<bool> match(const BooleanInput& binding, const Event& event,
132 function_ref<int(JoystickId)> getJoyDeadZone);
133
134} // namespace openmsx
135
136#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, function_ref< int(JoystickId)> getJoyDeadZone)
bool operator==(const BooleanInput &x, const BooleanInput &y)
std::optional< bool > match(const BooleanInput &binding, const Event &event, function_ref< 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)
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, Rs232NetEvent, ImGuiDelayedActionEvent, ImGuiActiveEvent > Event
Definition Event.hh:444