ImagoEngine
A C++ 2D game engine built on a sparse-set ECS, with scenes, scheduled systems, events, and action-based input
Loading...
Searching...
No Matches
InputSource.hpp
Go to the documentation of this file.
1
6
7#pragma once
8
9#include <SFML/Window/Keyboard.hpp>
10#include <SFML/Window/Mouse.hpp>
11#include <variant>
12#include <cstdint> // for uint8_t for the enums
13
14namespace Imago::Engine
15{
16 // --- Imago Input Enum definitions ---
17
18 enum class GamepadButton : uint8_t
19 {
20 FaceLeft, // X in SDL2
21 FaceRight, // B in SDL2
22 FaceUp, // Y in SDL2
23 FaceDown, // A in SDL2
24 Back, Guide, Start,
25 LeftStick, RightStick, // the presses not movement
26 LeftShoulder, RightShoulder,
27 DPadLeft, DPadRight, DPadUp, DPadDown,
28 Unknown
29 };
30
31 enum class GamepadAxis : uint8_t
32 {
33 LeftStickLeft,
34 LeftStickRight,
35 LeftStickUp,
36 LeftStickDown,
37 RightStickLeft,
38 RightStickRight,
39 RightStickUp,
40 RightStickDown,
41 LeftTrigger,
42 RightTrigger,
43 Unknown
44 };
45
46 // --- input sources and their variant overloads ---
47
49 struct KeySource { sf::Keyboard::Scancode scancode; };
50
52 struct MouseButtonSource { sf::Mouse::Button button; };
53
55 struct GamepadButtonSource { GamepadButton button; };
56
63 struct GamepadAxisSource { GamepadAxis axis; };
64
65 // NOTE: std::variant's == operator is used by the InputHandler to find matching inputs. It only exists if every alternative
66 // of the variant has its own == operator. The variant compares the held type first, so a KeySource never equals a MouseButtonSource.
67
68 constexpr bool operator==(const KeySource& a, const KeySource& b) { return a.scancode == b.scancode; }
69 constexpr bool operator!=(const KeySource& a, const KeySource& b) { return !(a==b); }
70
71 constexpr bool operator==(const MouseButtonSource& a, const MouseButtonSource& b) { return a.button == b.button; }
72 constexpr bool operator!=(const MouseButtonSource& a, const MouseButtonSource& b) { return !(a==b); }
73
74 constexpr bool operator==(const GamepadButtonSource& a, const GamepadButtonSource& b) { return a.button == b.button; }
75 constexpr bool operator!=(const GamepadButtonSource& a, const GamepadButtonSource& b) { return !(a==b); }
76
77 constexpr bool operator==(const GamepadAxisSource& a, const GamepadAxisSource& b) { return a.axis == b.axis; }
78 constexpr bool operator!=(const GamepadAxisSource& a, const GamepadAxisSource& b) { return !(a==b); }
79
80 // NOTE: we use a standard variant here so that all InputSources can be stoed in the same container in the InputHandler.
81 // we use std::variant instead of other type-erasure or polymorphic methods for simplicity and because the set of kinds
82 // is closed and know to the engine at compile time.
83
85 using InputSource = std::variant<KeySource, MouseButtonSource, GamepadButtonSource, GamepadAxisSource>;
86}
std::variant< KeySource, MouseButtonSource, GamepadButtonSource, GamepadAxisSource > InputSource
Any single raw input that can drive an action. Is a std::variant of all the InputSource types.
Definition InputSource.hpp:85
A gamepad axis motion that drives an action. Reads as a strength rescaled to a dead zone and scaled t...
Definition InputSource.hpp:63
A gamepad button that drives an action. Reads as 0 or 1. (pressed or not pressed).
Definition InputSource.hpp:55
A keybaord key that drives an action. Reads as 0 or 1. (pressed or not pressed).
Definition InputSource.hpp:49
A mouse button that drives an action. Reads as 0 or 1. (pressed or not pressed).
Definition InputSource.hpp:52