|
5 | 5 |
|
6 | 6 | #include <cstdint> |
7 | 7 |
|
| 8 | +#include <variant> |
| 9 | + |
8 | 10 | #include "CoreApi.h" |
9 | 11 | #include "GamepadTypes.h" |
10 | 12 | #include "Keycode.h" |
@@ -71,76 +73,94 @@ namespace gf { |
71 | 73 | } |
72 | 74 |
|
73 | 75 | enum class ControlType : uint8_t { |
| 76 | + None, |
74 | 77 | Keycode, |
75 | 78 | Scancode, |
76 | 79 | MouseButton, |
77 | 80 | GamepadButton, |
78 | 81 | GamepadAxis, |
79 | 82 | }; |
80 | 83 |
|
81 | | - struct GF_CORE_API ControlSettings { |
| 84 | + class GF_CORE_API ControlSettings { |
| 85 | + public: |
82 | 86 | ControlSettings() = default; |
83 | 87 |
|
84 | 88 | constexpr ControlSettings(Keycode keycode, Flags<Modifier> modifiers = None) |
85 | | - : type(ControlType::Keycode) |
86 | | - , keycode({ keycode, modifiers }) |
| 89 | + : m_data(KeycodeControlSettings{ keycode, modifiers }) |
87 | 90 | { |
88 | 91 | } |
89 | 92 |
|
90 | 93 | constexpr ControlSettings(Scancode scancode, Flags<Modifier> modifiers = None) |
91 | | - : type(ControlType::Scancode) |
92 | | - , scancode({ scancode, modifiers }) |
| 94 | + : m_data(ScancodeControlSettings{ scancode, modifiers }) |
93 | 95 | { |
94 | 96 | } |
95 | 97 |
|
96 | 98 | constexpr ControlSettings(MouseButton button) |
97 | | - : type(ControlType::MouseButton) |
98 | | - , mouse_button({ button }) |
| 99 | + : m_data(MouseButtonControlSettings{ button }) |
99 | 100 | { |
100 | 101 | } |
101 | 102 |
|
102 | 103 | constexpr ControlSettings(GamepadId gamepad_id, GamepadButton button) |
103 | | - : type(ControlType::GamepadButton) |
104 | | - , gamepad_button({ gamepad_id, button }) |
| 104 | + : m_data(GamepadButtonControlSettings{ gamepad_id, button }) |
105 | 105 | { |
106 | 106 | } |
107 | 107 |
|
108 | 108 | constexpr ControlSettings(GamepadId gamepad_id, GamepadAxis axis, GamepadAxisDirection direction) |
109 | | - : type(ControlType::GamepadAxis) |
110 | | - , gamepad_axis({ gamepad_id, axis, direction }) |
| 109 | + : m_data(GamepadAxisControlSettings{ gamepad_id, axis, direction }) |
| 110 | + { |
| 111 | + } |
| 112 | + |
| 113 | + constexpr ControlType type() const |
111 | 114 | { |
| 115 | + return static_cast<ControlType>(m_data.index()); |
112 | 116 | } |
113 | 117 |
|
114 | | - ControlType type; |
115 | | - union { |
116 | | - KeycodeControlSettings keycode; |
117 | | - ScancodeControlSettings scancode; |
118 | | - MouseButtonControlSettings mouse_button; |
119 | | - GamepadButtonControlSettings gamepad_button; |
120 | | - GamepadAxisControlSettings gamepad_axis; |
121 | | - }; |
| 118 | + template<typename T> |
| 119 | + constexpr T& as() |
| 120 | + { |
| 121 | + return std::get<T>(m_data); |
| 122 | + } |
| 123 | + |
| 124 | + template<typename T> |
| 125 | + constexpr const T& as() const |
| 126 | + { |
| 127 | + return std::get<T>(m_data); |
| 128 | + } |
| 129 | + |
| 130 | + private: |
| 131 | + std::variant< |
| 132 | + std::monostate, |
| 133 | + KeycodeControlSettings, |
| 134 | + ScancodeControlSettings, |
| 135 | + MouseButtonControlSettings, |
| 136 | + GamepadButtonControlSettings, |
| 137 | + GamepadAxisControlSettings |
| 138 | + > m_data; |
122 | 139 | }; |
123 | 140 |
|
124 | 141 | template<typename Archive> |
125 | 142 | inline Archive& operator|(Archive& ar, MaybeConst<ControlSettings, Archive>& settings) |
126 | 143 | { |
127 | | - ar | settings.type; |
| 144 | + ControlType type = settings.type(); |
| 145 | + ar | type; |
128 | 146 |
|
129 | | - switch (settings.type) { |
| 147 | + switch (type) { |
| 148 | + case ControlType::None: |
| 149 | + break; |
130 | 150 | case ControlType::Keycode: |
131 | | - ar | settings.keycode; |
| 151 | + ar | settings.template as<KeycodeControlSettings>(); |
132 | 152 | break; |
133 | 153 | case ControlType::Scancode: |
134 | | - ar | settings.scancode; |
| 154 | + ar | settings.template as<ScancodeControlSettings>(); |
135 | 155 | break; |
136 | 156 | case ControlType::MouseButton: |
137 | | - ar | settings.mouse_button; |
| 157 | + ar | settings.template as<MouseButtonControlSettings>(); |
138 | 158 | break; |
139 | 159 | case ControlType::GamepadButton: |
140 | | - ar | settings.gamepad_button; |
| 160 | + ar | settings.template as<GamepadButtonControlSettings>(); |
141 | 161 | break; |
142 | 162 | case ControlType::GamepadAxis: |
143 | | - ar | settings.gamepad_axis; |
| 163 | + ar | settings.template as<GamepadAxisControlSettings>(); |
144 | 164 | break; |
145 | 165 | } |
146 | 166 |
|
|
0 commit comments