OXYGENHUB
◆ main openmsx / src / events / AnalogInput.hh
1.6 KiB · C++ · last changed chore(): initial commit 1 month ago source blame history raw
1
#ifndef ANALOG_INPUT_HH
2
#define ANALOG_INPUT_HH
3
4
// A 'AnalogInput' represents an input with an associated analog value
5
//
6
// Some examples:
7
// * Joystick axis motion, the corresponding analog value can vary between -32768..32767
8
// * Relative mouse motion, separate for X and Y axis.
9
10
#include "Event.hh"
11
#include "JoystickId.hh"
12
13
#include "function_ref.hh"
14
15
#include <SDL.h>
16
17
#include <cstdint>
18
#include <optional>
19
#include <string>
20
#include <string_view>
21
#include <variant>
22
23
namespace openmsx {
24
25
class AnalogMouseAxis
26
{
27
public:
28
	// 0 = X-axis, 1 = Y-axis
29
	explicit AnalogMouseAxis(uint8_t axis_)
30
		: axis(axis_) {}
31
32
	[[nodiscard]] auto getAxis() const { return axis; }
33
34
private:
35
	uint8_t axis;
36
};
37
38
class AnalogJoystickAxis
39
{
40
public:
41
	explicit AnalogJoystickAxis(JoystickId joystick_, uint8_t axis_)
42
		: joystick(joystick_), axis(axis_) {}
43
44
	[[nodiscard]] auto getJoystick() const { return joystick; }
45
	[[nodiscard]] auto getAxis() const { return axis; }
46
47
private:
48
	JoystickId joystick;
49
	uint8_t axis;
50
};
51
52
53
using AnalogInput = std::variant<
54
	AnalogMouseAxis,
55
	AnalogJoystickAxis>;
56
57
58
[[nodiscard]] std::string toString(const AnalogInput& input);
59
[[nodiscard]] std::optional<AnalogInput> parseAnalogInput(std::string_view text);
60
[[nodiscard]] std::optional<AnalogInput> captureAnalogInput(const Event& event, function_ref<int(JoystickId)> getJoyDeadThreshold);
61
62
[[nodiscard]] bool operator==(const AnalogInput& x, const AnalogInput& y);
63
64
[[nodiscard]] std::optional<float> match(
65
	const AnalogInput& binding, const Event& event,
66
	function_ref<float(JoystickId, const JoystickAxisMotionEvent&)> getJoyValue);
67
68
} // namespace openmsx
69
70
#endif