MGL(Win32)
読み取り中…
検索中…
一致する文字列を見つけられません
mgl_gamepad_state.h
[詳解]
1// SPDX-License-Identifier: Zlib
2/* ------------------------------------------------------------------------- */
9/* ------------------------------------------------------------------------- */
10
11#ifndef INCGUARD_MGL_GAMEPAD_STATE_H_1609654412
12#define INCGUARD_MGL_GAMEPAD_STATE_H_1609654412
13
14#include <array>
15#include <atomic>
16
20#include <mgl/mgl_environment.h>
21
22namespace MGL::Input
23{
26{
27public:
28 PadState() noexcept;
29
30 void PreUpdate() noexcept;
31 void PostUpdate(const RepeatSetting &repeatSetting) noexcept;
32
33 /* ------------------------------------------------------------------------- */
38 /* ------------------------------------------------------------------------- */
39 constexpr void SetUsing(bool isUsing) noexcept
40 {
41 _isUsing = isUsing;
42 }
43
44 /* ------------------------------------------------------------------------- */
50 /* ------------------------------------------------------------------------- */
51 [[nodiscard]] constexpr bool IsUsing() const noexcept
52 {
53 return _isUsing || IsEnabled();
54 }
55
56 /* ------------------------------------------------------------------------- */
63 /* ------------------------------------------------------------------------- */
64 void RequestsActivate(PadType type, PadPriority priority, const PadDeviceInfo &deviceInfo) noexcept
65 {
66 _type = type;
67 _priority = priority;
68 _deviceInfo = deviceInfo;
69 _shouldActivate = true;
70 }
71
72 /* ------------------------------------------------------------------------- */
76 /* ------------------------------------------------------------------------- */
77 void RequestsDeactivate() noexcept
78 {
79 _shouldDeactivate = true;
80 }
81
82 /* ------------------------------------------------------------------------- */
88 /* ------------------------------------------------------------------------- */
89 bool operator==(const PadState &rhs) const noexcept
90 {
91 return _identifier == rhs._identifier;
92 }
93
94 /* ------------------------------------------------------------------------- */
100 /* ------------------------------------------------------------------------- */
101 bool operator!=(const PadState &rhs) const noexcept
102 {
103 return _identifier != rhs._identifier;
104 }
105
106 /* ------------------------------------------------------------------------- */
112 /* ------------------------------------------------------------------------- */
113 [[nodiscard]] constexpr bool IsEnabled() const noexcept
114 {
115 return _identifier != PadID::Invalid;
116 }
117
118 /* ------------------------------------------------------------------------- */
123 /* ------------------------------------------------------------------------- */
124 constexpr void SetPlayerIndex(int32_t playerIndex) noexcept
125 {
126 _playerIndex = playerIndex;
127 }
128
129 /* ------------------------------------------------------------------------- */
134 /* ------------------------------------------------------------------------- */
135 [[nodiscard]] constexpr int32_t GetPlayerIndex() const noexcept
136 {
137 return _playerIndex;
138 }
139
140 /* ------------------------------------------------------------------------- */
145 /* ------------------------------------------------------------------------- */
146 [[nodiscard]] constexpr PadType GetType() const noexcept
147 {
148 return _type;
149 }
150
151 /* ------------------------------------------------------------------------- */
156 /* ------------------------------------------------------------------------- */
157 constexpr void SetEntry(PadEntry entry) noexcept
158 {
159 if (entry < PadEntry::Reserve_Start)
160 {
161 _entry = entry;
162 }
163 }
164
165 /* ------------------------------------------------------------------------- */
170 /* ------------------------------------------------------------------------- */
171 [[nodiscard]] constexpr PadEntry GetEntry() const noexcept
172 {
173 return _entry;
174 }
175
176 /* ------------------------------------------------------------------------- */
182 /* ------------------------------------------------------------------------- */
183 [[nodiscard]] constexpr bool HasEntry() const noexcept
184 {
185 return IsEnabled() && (_entry < PadEntry::Reserve_Start);
186 }
187
188 /* ------------------------------------------------------------------------- */
193 /* ------------------------------------------------------------------------- */
194 constexpr void SetButton(PadButton button) noexcept
195 {
196 _buttonFlags.Set(button);
197 }
198
199 /* ------------------------------------------------------------------------- */
204 /* ------------------------------------------------------------------------- */
205 constexpr void SetButton(PadButtonFlags buttonFlags) noexcept
206 {
207 _buttonFlags |= buttonFlags;
208 }
209
210 /* ------------------------------------------------------------------------- */
215 /* ------------------------------------------------------------------------- */
216 [[nodiscard]] constexpr PadButtonFlags GetButtonFlags() const noexcept
217 {
218 return _buttonFlags;
219 }
220
221 /* ------------------------------------------------------------------------- */
226 /* ------------------------------------------------------------------------- */
227 constexpr void SetLeftStick(const Vector2 &axisValue) noexcept
228 {
229 _leftStick = axisValue;
230 }
231
232 /* ------------------------------------------------------------------------- */
237 /* ------------------------------------------------------------------------- */
238 constexpr void SetRightStick(const Vector2 &axisValue) noexcept
239 {
240 _rightStick = axisValue;
241 }
242
243 /* ------------------------------------------------------------------------- */
248 /* ------------------------------------------------------------------------- */
249 [[nodiscard]] constexpr const Vector2 &GetLeftStick() const noexcept
250 {
251 return _leftStick;
252 }
253
254 /* ------------------------------------------------------------------------- */
259 /* ------------------------------------------------------------------------- */
260 [[nodiscard]] constexpr Vector2 &GetLeftStick() noexcept
261 {
262 return _leftStick;
263 }
264
265 /* ------------------------------------------------------------------------- */
270 /* ------------------------------------------------------------------------- */
271 [[nodiscard]] constexpr const Vector2 &GetRightStick() const noexcept
272 {
273 return _rightStick;
274 }
275
276 /* ------------------------------------------------------------------------- */
281 /* ------------------------------------------------------------------------- */
282 [[nodiscard]] constexpr Vector2 &GetRightStick() noexcept
283 {
284 return _rightStick;
285 }
286
287 /* ------------------------------------------------------------------------- */
294 /* ------------------------------------------------------------------------- */
295 [[nodiscard]] constexpr bool IsPressing(PadButton button) const noexcept
296 {
297 return IsPressingAny(button);
298 }
299
300 /* ------------------------------------------------------------------------- */
307 /* ------------------------------------------------------------------------- */
308 [[nodiscard]] constexpr bool IsPressing(PadButtonFlags buttonFlags) const noexcept
309 {
310 return (_buttonFlags & buttonFlags) == buttonFlags;
311 }
312
313 /* ------------------------------------------------------------------------- */
320 /* ------------------------------------------------------------------------- */
321 [[nodiscard]] constexpr bool IsPressingAny(PadButtonFlags buttonFlags = kGamepadButtonAll) const noexcept
322 {
323 return (_buttonFlags & buttonFlags).HasAny();
324 }
325
326 /* ------------------------------------------------------------------------- */
333 /* ------------------------------------------------------------------------- */
334 [[nodiscard]] constexpr bool IsTriggered(PadButton button) const noexcept
335 {
336 return IsPressing(button) && !_prevButtonFlags.Has(button);
337 }
338
339 /* ------------------------------------------------------------------------- */
346 /* ------------------------------------------------------------------------- */
347 [[nodiscard]] constexpr bool IsReleased(PadButton button) const noexcept
348 {
349 return !IsPressing(button) && _prevButtonFlags.Has(button);
350 }
351
352 /* ------------------------------------------------------------------------- */
358 /* ------------------------------------------------------------------------- */
359 [[nodiscard]] constexpr bool HasInputButton() const noexcept
360 {
361 return IsEnabled() && (_buttonFlags.HasAny() || _prevButtonFlags.HasAny());
362 }
363
364 /* ------------------------------------------------------------------------- */
371 /* ------------------------------------------------------------------------- */
372 [[nodiscard]] constexpr bool IsARepeat(PadButton button) const noexcept
373 {
374 return _repeatFlags.Has(button);
375 }
376
377 /* ------------------------------------------------------------------------- */
382 /* ------------------------------------------------------------------------- */
383 constexpr void SetPortraitMode(bool isEnabled) noexcept
384 {
385 _isPortraitMode = isEnabled;
386 }
387
388 /* ------------------------------------------------------------------------- */
394 /* ------------------------------------------------------------------------- */
395 [[nodiscard]] constexpr bool IsPortraitMode() const noexcept
396 {
397 return _isPortraitMode;
398 }
399
400 /* ------------------------------------------------------------------------- */
405 /* ------------------------------------------------------------------------- */
406 [[nodiscard]] constexpr PadPriority GetPriority() const noexcept
407 {
408 return _priority;
409 }
410
411 /* ------------------------------------------------------------------------- */
416 /* ------------------------------------------------------------------------- */
417 constexpr void SetDecideButton(PadButton button) noexcept
418 {
419 _decideButton = button;
420 }
421
422 /* ------------------------------------------------------------------------- */
427 /* ------------------------------------------------------------------------- */
428 [[nodiscard]] constexpr PadButton GetDecideButton() const noexcept
429 {
430 return _decideButton;
431 }
432
433 /* ------------------------------------------------------------------------- */
438 /* ------------------------------------------------------------------------- */
439 constexpr void SetCancelButton(PadButton button) noexcept
440 {
441 _cancelButton = button;
442 }
443
444 /* ------------------------------------------------------------------------- */
449 /* ------------------------------------------------------------------------- */
450 [[nodiscard]] constexpr PadButton GetCancelButton() const noexcept
451 {
452 return _cancelButton;
453 }
454
455 /* ------------------------------------------------------------------------- */
460 /* ------------------------------------------------------------------------- */
461 [[nodiscard]] constexpr const PadDeviceInfo &GetDeviceInfo() const noexcept
462 {
463 return _deviceInfo;
464 }
465
466 void ApplyLeftStickToDPad() noexcept;
467
468 // コピーとムーブは禁止
469 PadState(const PadState &) = delete;
470 PadState &operator=(const PadState &) = delete;
471 PadState(PadState &&) = delete;
472 PadState &operator=(PadState &&) = delete;
473
474private:
475 void Activate() noexcept;
476 void Deactivate() noexcept;
477
478 void ApplyDecideCancelButton() noexcept;
479 void UpdateRepeatFlags(const RepeatSetting &repeatSetting) noexcept;
480
481 PadType _type{PadType::Disable};
482 PadID _identifier{PadID::Invalid};
483 PadDeviceInfo _deviceInfo;
484 bool _isUsing{false};
485
486 PadButtonFlags _buttonFlags;
487 PadButtonFlags _prevButtonFlags;
488 PadButtonFlags _repeatFlags;
489
490 Vector2 _leftStick;
491 Vector2 _rightStick;
492
493 int32_t _playerIndex{-1};
494 PadEntry _entry{PadEntry::NoEntry};
495
496 PadPriority _priority{PadPriority::Low};
497
498 PadButton _decideButton{PadButton::None};
499 PadButton _cancelButton{PadButton::None};
500
501 bool _isPortraitMode{false};
502
503 std::array<float, PadButtonFlags::kSize> _repeatTimerArray;
504
505 std::atomic<bool> _shouldActivate{false};
506 std::atomic<bool> _shouldDeactivate{false};
507};
508} // namespace MGL::Input
509
510#endif // INCGUARD_MGL_GAMEPAD_STATE_H_1609654412
511
512// vim: et ts=4 sw=4 sts=4
constexpr bool HasAny() const noexcept
いずれかの値を保持しているかを取得
Definition mgl_bit.h:114
constexpr EnumBitFlags & Set(EnumType value) noexcept
指定した値をセットする
Definition mgl_bit.h:126
constexpr bool Has(EnumType value) const noexcept
指定した値を保持しているかを取得
Definition mgl_bit.h:102
ゲームパッドステートクラス
Definition mgl_gamepad_state.h:26
constexpr void SetEntry(PadEntry entry) noexcept
エントリー番号の設定
Definition mgl_gamepad_state.h:157
void ApplyLeftStickToDPad() noexcept
アナログスティックからD-Padを適用する
Definition mgl_gamepad_state.cc:185
constexpr void SetPortraitMode(bool isEnabled) noexcept
ポートレートモード(縦持ち)の設定
Definition mgl_gamepad_state.h:383
constexpr PadButtonFlags GetButtonFlags() const noexcept
設定済みのボタンフラグの取得
Definition mgl_gamepad_state.h:216
constexpr void SetRightStick(const Vector2 &axisValue) noexcept
右スティックの値を設定
Definition mgl_gamepad_state.h:238
constexpr void SetPlayerIndex(int32_t playerIndex) noexcept
プレイヤーインデックスの設定
Definition mgl_gamepad_state.h:124
constexpr Vector2 & GetLeftStick() noexcept
左スティックの値を設定(const版)
Definition mgl_gamepad_state.h:260
constexpr PadButton GetDecideButton() const noexcept
決定ボタンに割り当てるボタンを取得
Definition mgl_gamepad_state.h:428
constexpr bool HasInputButton() const noexcept
いずれかのボタンの入力があるかを取得
Definition mgl_gamepad_state.h:359
constexpr const Vector2 & GetRightStick() const noexcept
右スティックの値を設定
Definition mgl_gamepad_state.h:271
constexpr PadPriority GetPriority() const noexcept
プライオリティを取得
Definition mgl_gamepad_state.h:406
constexpr void SetButton(PadButton button) noexcept
ボタンの設定
Definition mgl_gamepad_state.h:194
constexpr bool IsPortraitMode() const noexcept
ポートレートモード(縦持ち)の取得
Definition mgl_gamepad_state.h:395
constexpr bool IsEnabled() const noexcept
このパッドステートが有効かどうかを取得
Definition mgl_gamepad_state.h:113
constexpr void SetDecideButton(PadButton button) noexcept
決定ボタンに割り当てるボタンを設定
Definition mgl_gamepad_state.h:417
bool operator==(const PadState &rhs) const noexcept
等価演算子のオーバーロード
Definition mgl_gamepad_state.h:89
constexpr bool IsTriggered(PadButton button) const noexcept
ボタンが押された瞬間を取得
Definition mgl_gamepad_state.h:334
void RequestsActivate(PadType type, PadPriority priority, const PadDeviceInfo &deviceInfo) noexcept
アクティベート要求
Definition mgl_gamepad_state.h:64
constexpr void SetButton(PadButtonFlags buttonFlags) noexcept
ボタンの設定
Definition mgl_gamepad_state.h:205
constexpr bool IsPressing(PadButtonFlags buttonFlags) const noexcept
指定したボタンが全て押されているかを取得
Definition mgl_gamepad_state.h:308
constexpr PadEntry GetEntry() const noexcept
エントリー番号の取得
Definition mgl_gamepad_state.h:171
void RequestsDeactivate() noexcept
アクティベート解除要求
Definition mgl_gamepad_state.h:77
constexpr const Vector2 & GetLeftStick() const noexcept
左スティックの値を設定
Definition mgl_gamepad_state.h:249
constexpr void SetUsing(bool isUsing) noexcept
使用中フラグを設定
Definition mgl_gamepad_state.h:39
constexpr PadType GetType() const noexcept
ゲームパッドの種類を取得
Definition mgl_gamepad_state.h:146
void PreUpdate() noexcept
更新前の処理
Definition mgl_gamepad_state.cc:64
constexpr bool HasEntry() const noexcept
エントリー済みかを取得
Definition mgl_gamepad_state.h:183
constexpr bool IsUsing() const noexcept
使用中フラグを取得
Definition mgl_gamepad_state.h:51
constexpr void SetCancelButton(PadButton button) noexcept
キャンセルボタンに割り当てるボタンを設定
Definition mgl_gamepad_state.h:439
bool operator!=(const PadState &rhs) const noexcept
不等価演算子のオーバーロード
Definition mgl_gamepad_state.h:101
constexpr bool IsPressing(PadButton button) const noexcept
ボタンが押されているかを取得
Definition mgl_gamepad_state.h:295
constexpr Vector2 & GetRightStick() noexcept
右スティックの値を設定(const版)
Definition mgl_gamepad_state.h:282
void PostUpdate(const RepeatSetting &repeatSetting) noexcept
更新後の処理
Definition mgl_gamepad_state.cc:100
constexpr const PadDeviceInfo & GetDeviceInfo() const noexcept
デバイス情報を取得
Definition mgl_gamepad_state.h:461
constexpr int32_t GetPlayerIndex() const noexcept
プレイヤーインデックスの設定
Definition mgl_gamepad_state.h:135
PadState() noexcept
コンストラクタ
Definition mgl_gamepad_state.cc:52
constexpr bool IsARepeat(PadButton button) const noexcept
リピート入力を取得
Definition mgl_gamepad_state.h:372
constexpr void SetLeftStick(const Vector2 &axisValue) noexcept
左スティックの値を設定
Definition mgl_gamepad_state.h:227
constexpr bool IsReleased(PadButton button) const noexcept
ボタンが離された瞬間を取得
Definition mgl_gamepad_state.h:347
constexpr bool IsPressingAny(PadButtonFlags buttonFlags=kGamepadButtonAll) const noexcept
指定したボタンのいずれかが押されているかを取得
Definition mgl_gamepad_state.h:321
constexpr PadButton GetCancelButton() const noexcept
キャンセルボタンに割り当てるボタンを取得
Definition mgl_gamepad_state.h:450
MGL 環境定義
ゲームパッド関連各種定義
PadEntry
パッドのエントリータイプ
Definition mgl_gamepad_defs.h:164
PadType
ゲームパッドの種類
Definition mgl_gamepad_defs.h:148
PadPriority
パッドのプライオリティ
Definition mgl_gamepad_defs.h:192
PadButton
ゲームパッドのボタンの定義
Definition mgl_gamepad_defs.h:33
EnumBitFlags< PadButton > PadButtonFlags
ゲームパッドボタンの入力状態のビットフラグ型
Definition mgl_gamepad_defs.h:132
MGL リピート入力関連定義
MGL 2Dベクトル
ゲームパッドのデバイス情報
Definition mgl_gamepad_defs.h:199
リピート設定
Definition mgl_input_repeat.h:25
2Dベクトル
Definition mgl_vector2.h:23