Skip to main content

GamepadState.ixx File

Representation of current gamepad input state. More...

Included Headers

#include <algorithm> #include <cassert> #include <helios-engine-config.h> #include <helios.engine.input.types.Gamepad> #include <helios.engine.util.log.LogManager> #include <helios.engine.util.log.Logger> #include <helios.math.types>

Namespaces Index

namespacehelios
namespaceengine
namespaceinput
namespacegamepad

Classes Index

classGamepadState

A lightweight class for transferring the state of a Gamepad. More...

Macro Definitions Index

#defineHELIOS_LOG_SCOPE   "helios::engine::input::gamepad::GamepadState"

Description

Representation of current gamepad input state.

Macro Definitions

HELIOS_LOG_SCOPE

#define HELIOS_LOG_SCOPE   "helios::engine::input::gamepad::GamepadState"

Definition at line 20 of file GamepadState.ixx.

20#define HELIOS_LOG_SCOPE "helios::engine::input::gamepad::GamepadState"

File Listing

The file content with the documentation metadata removed is:

1
5module;
6
7#include <algorithm>
8#include <cassert>
10
11export module helios.engine.input.gamepad.GamepadState;
12
13import helios.math.types;
14import helios.engine.util.log.Logger;
15import helios.engine.util.log.LogManager;
16
17import helios.engine.input.types.Gamepad;
18
19
20#define HELIOS_LOG_SCOPE "helios::engine::input::gamepad::GamepadState"
21
22using namespace helios::engine::input::types;
23
24export namespace helios::engine::input::gamepad {
25
61 private:
62
63 uint32_t currInput_ = 0;
64 uint32_t prevInput_ = 0;
65
70
71
75 bool buttonA_ = false;
76 bool prevButtonA_ = false;
77
81 bool buttonB_ = false;
82 bool prevButtonB_ = false;
83
87 bool buttonX_ = false;
88
92 bool buttonY_ = false;
93
97 bool buttonStart_ = false;
98
99 bool prevButtonStart_ = false;
100
104 bool buttonBack_ = false;
105
109 bool buttonGuide_ = false;
110
114 bool buttonLeftBumper_ = false;
115
119 bool buttonRightBumper_ = false;
120
124 bool buttonLeftThumb_ = false;
125
129 bool buttonRightThumb_ = false;
130
134 bool buttonDpadUp_ = false;
135 bool prevButtonDpadUp_ = false;
136
140 bool buttonDpadRight_ = false;
141
145 bool buttonDpadDown_ = false;
146 bool prevButtonDpadDown_ = false;
147
151 bool buttonDpadLeft_ = false;
152
156 mutable bool needsUpdate_ = true;
157
161 float axisLeftX_{};
162
166 float axisLeftY_{};
167
171 float axisRightX_{};
172
176 float axisRightY_{};
177
181 float triggerLeft_{};
182
186 float triggerRight_{};
187
191 mutable helios::math::vec2f left_;
192
196 mutable helios::math::vec2f right_;
197
201 mutable helios::math::vec2f trigger_;
202
208 void update() const noexcept {
209 left_ = helios::math::vec2f(axisLeftX_, axisLeftY_);
210 right_ = helios::math::vec2f(axisRightX_, axisRightY_);
211 trigger_ = helios::math::vec2f(triggerLeft_, triggerRight_);
212
213 needsUpdate_ = false;
214 }
215
216 public:
217 ~GamepadState() = default;
218
224 GamepadState() = default;
225
233 explicit GamepadState(
234 float axisLeftX, float axisLeftY, float axisRightX,
235 float axisRightY, float triggerLeft, float triggerRight,
236
237 bool buttonA, bool buttonB, bool buttonX, bool buttonY,
238 bool buttonStart, bool buttonBack, bool buttonGuide,
242
243 ) noexcept {
246
250 }
251
252
289 float axisLeftX, float axisLeftY, float axisRightX, float axisRightY,
290 float triggerLeft, float triggerRight,
291
292 bool buttonA, bool buttonB, bool buttonX, bool buttonY,
293 bool buttonStart, bool buttonBack, bool buttonGuide,
297 ) noexcept {
298
299#ifdef HELIOS_DEBUG
301 logger_.warn("axisLeftX is out of bounds.");
302 }
304 logger_.warn("axisLeftY is out of bounds.");
305 }
307 logger_.warn("axisRightX is out of bounds.");
308 }
310 logger_.warn("axisRightY is out of bounds.");
311 }
313 logger_.warn("triggerLeft is out of bounds.");
314 }
316 logger_.warn("triggerRight is out of bounds.");
317 }
318#endif
319
320 axisLeftX_ = std::clamp(axisLeftX, -1.0f, 1.0f);
321 axisLeftY_ = std::clamp(axisLeftY, -1.0f, 1.0f);
322 axisRightX_ = std::clamp(axisRightX, -1.0f, 1.0f);
323 axisRightY_ = std::clamp(axisRightY, -1.0f, 1.0f);
324 triggerLeft_ = std::clamp(triggerLeft, 0.0f, 1.0f);
325 triggerRight_ = std::clamp(triggerRight, 0.0f, 1.0f);
326;
327
328 prevButtonA_ = buttonA_;
329 buttonA_ = buttonA;
330
331 prevButtonB_ = buttonB_;
332 buttonB_ = buttonB;
333
334 buttonX_ = buttonX;
335
336 buttonY_ = buttonY;
337
338 prevButtonStart_ = buttonStart_;
339 buttonStart_ = buttonStart;
340
341 buttonBack_ = buttonBack;
342
343 buttonGuide_ = buttonGuide;
344
345 buttonLeftBumper_ = buttonLeftBumper;
346
347 buttonRightBumper_ = buttonRightBumper;
348
349 buttonLeftThumb_ = buttonLeftThumb;
350
351 buttonRightThumb_ = buttonRightThumb;
352
353 prevButtonDpadUp_ = buttonDpadUp_;
354 buttonDpadUp_ = buttonDpadUp;
355
356 buttonDpadRight_ = buttonDpadRight;
357
358 prevButtonDpadDown_ = buttonDpadDown_;
359 buttonDpadDown_ = buttonDpadDown;
360
361 buttonDpadLeft_ = buttonDpadLeft;
362
363 needsUpdate_ = true;
364 }
365
372 return axisLeftX_;
373 }
374
375
382 return axisLeftY_;
383 }
384
385
392 return axisRightX_;
393 }
394
395
402 return axisRightY_;
403 }
404
405
412 return triggerLeft_;
413 }
414
415
422 return triggerRight_;
423 }
424
425
431 [[nodiscard]] helios::math::vec2f left() const noexcept {
432 if (needsUpdate_) {
433 update();
434 }
435 return left_;
436 }
437
438
444 [[nodiscard]] helios::math::vec2f right() const noexcept {
445 if (needsUpdate_) {
446 update();
447 }
448 return right_;
449 }
450
451
458 [[nodiscard]] helios::math::vec2f trigger() const noexcept {
459 if (needsUpdate_) {
460 update();
461 }
462 return trigger_;
463 }
464
465
471 return buttonA_;
472 };
473
474
480 return buttonB_;
481 };
482
483
489 return buttonX_;
490 };
491
492
498 return buttonY_;
499 };
500
501
509 [[nodiscard]] bool isButtonDown(const GamepadInput input) const {
510 switch (input) {
511 case GamepadInput::Start:
512 return buttonStart_;
513 case GamepadInput::Down:
514 return buttonDpadDown_;
515 case GamepadInput::Up:
516 return buttonDpadUp_;
517 case GamepadInput::A:
518 return buttonA_;
519 case GamepadInput::B:
520 return buttonB_;
521
522 default:
523 assert(false && "Unexpected input");
524 return false;
525 }
526 }
527
535 [[nodiscard]] bool isButtonHeld(const GamepadInput input) const {
536 switch (input) {
537 case GamepadInput::Start:
538 return prevButtonStart_ && buttonStart_;
539 case GamepadInput::Down:
540 return prevButtonDpadDown_ && buttonDpadDown_;
541 case GamepadInput::Up:
542 return prevButtonDpadUp_ && buttonDpadUp_;
543 case GamepadInput::A:
544 return prevButtonA_ && buttonA_;
545 case GamepadInput::B:
546 return prevButtonB_ && buttonB_;
547
548 default:
549 assert(false && "Unexpected input");
550 return false;
551 }
552 }
553
561 [[nodiscard]] bool isButtonUp(const GamepadInput input) const {
562 switch (input) {
563 case GamepadInput::Start:
564 return prevButtonStart_ && !buttonStart_;
565 case GamepadInput::Down:
566 return prevButtonDpadDown_ && !buttonDpadDown_;
567 case GamepadInput::Up:
568 return prevButtonDpadUp_ && !buttonDpadUp_;
569 case GamepadInput::A:
570 return prevButtonA_ && !buttonA_;
571 case GamepadInput::B:
572 return prevButtonB_ && !buttonB_;
573
574 default:
575 assert(false && "Unexpected input");
576 return false;
577 }
578 }
579
580
588 [[nodiscard]] bool isButtonPressed(const GamepadInput input) const {
589 switch (input) {
590 case GamepadInput::Start:
591 return !prevButtonStart_ && buttonStart_;
592 case GamepadInput::Down:
593 return !prevButtonDpadDown_ && buttonDpadDown_;
594 case GamepadInput::Up:
595 return !prevButtonDpadUp_ && buttonDpadUp_;
596 case GamepadInput::A:
597 return !prevButtonA_ && buttonA_;
598 case GamepadInput::B:
599 return !prevButtonB_ && buttonB_;
600
601 default:
602 assert(false && "Unexpected input");
603 return false;
604 }
605 }
606
607
608
614 return prevButtonStart_ && buttonStart_;
615 };
616
623 return !prevButtonStart_ && buttonStart_;
624 };
625
626
632 return buttonBack_;
633 };
634
635
641 return buttonGuide_;
642 };
643
644
650 return buttonLeftBumper_;
651 };
652
653
659 return buttonRightBumper_;
660 };
661
662
668 return buttonLeftThumb_;
669 };
670
671
677 return buttonRightThumb_;
678 };
679
680
686 return buttonDpadUp_;
687 };
688
689
695 return buttonDpadRight_;
696 };
697
698
704 return buttonDpadDown_;
705 };
706
707
713 return buttonDpadLeft_;
714 };
715 };
716
717
718} // namespace helios::engine::input

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.