libgui 2.01
libgui is a GUI library for the GameCube, Wii, and Wii U
 
Loading...
Searching...
No Matches
InputController.cpp
1/****************************************************************************
2 * Platform Abstraction Layer
3 * Daryl Borth 2026
4 * InputController.cpp
5 ***************************************************************************/
6
7#include <cmath>
8#include "InputController.h"
9#include "Platform.h"
10
11InputController* controller[4] = {nullptr, nullptr, nullptr, nullptr};
12
13void InitUserInputControllers()
14{
15 for(int i = 0; i < 4; i++)
16 {
17 if(!controller[i])
18 controller[i] = new InputController(i);
19 }
20}
21
23 channel(ch),
24 sideways(false),
25 scrollTimer(0.0f),
26 internalScrollTimer(0.0f)
27{}
28
29void InputController::update(const InputPadData& data, float deltaTime) {
30 currentData = data;
31
32 // Advance the scroll timer
33 internalScrollTimer += deltaTime;
34
35 // If no directional inputs are held, reset the scroll timer completely
36 if (currentData.buttons_h == 0 &&
37 std::abs(currentData.stickX) < STICK_DEADZONE &&
38 std::abs(currentData.stickY) < STICK_DEADZONE) {
39 internalScrollTimer = 0.0f;
40 }
41}
42
43bool InputController::processDirection(uint32_t logicalButtonMask, float stickAxis, bool isPositiveAxis) const {
44 bool isPressedDown = (currentData.buttons_d & logicalButtonMask);
45 bool isHeld = (currentData.buttons_h & logicalButtonMask);
46 bool isStickActive = isPositiveAxis ? (stickAxis > STICK_DEADZONE) : (stickAxis < -STICK_DEADZONE);
47
48 // Initial press fires immediately
49 if (isPressedDown) {
50 internalScrollTimer = 0.0f; // Reset timer on fresh press
51 return true;
52 }
53
54 // If it's held down (or stick pushed), evaluate the repeat delay
55 if (isHeld || isStickActive) {
56 if (internalScrollTimer >= SCROLL_DELAY_INITIAL) {
57 // Re-trigger and step back the timer by the loop amount so it triggers again soon
58 internalScrollTimer -= SCROLL_DELAY_LOOP;
59 return true;
60 }
61 }
62
63 return false;
64}
65
67 uint32_t targetBtn = sideways ? INPUT_BTN_2 : INPUT_BTN_A;
68 return (currentData.buttons_d & targetBtn);
69}
70
72 uint32_t targetBtn = sideways ? INPUT_BTN_1 : INPUT_BTN_B;
73 return (currentData.buttons_d & targetBtn);
74}
75
76bool InputController::isPressed(uint32_t logicalButtonMask) const {
77 return (currentData.buttons_d & logicalButtonMask);
78}
79
80bool InputController::isHeld(uint32_t logicalButtonMask) const {
81 return (currentData.buttons_h & logicalButtonMask);
82}
83
84bool InputController::up() const {
85 uint32_t targetBtn = sideways ? INPUT_BTN_RIGHT : INPUT_BTN_UP;
86 return processDirection(targetBtn, currentData.stickY, true);
87}
88
90 uint32_t targetBtn = sideways ? INPUT_BTN_LEFT : INPUT_BTN_DOWN;
91 return processDirection(targetBtn, currentData.stickY, false);
92}
93
95 uint32_t targetBtn = sideways ? INPUT_BTN_UP : INPUT_BTN_LEFT;
96 return processDirection(targetBtn, currentData.stickX, false);
97}
98
100 uint32_t targetBtn = sideways ? INPUT_BTN_DOWN : INPUT_BTN_RIGHT;
101 return processDirection(targetBtn, currentData.stickX, true);
102}
103
105 platform->getInput()->setContinuousRumble(channel, continuous);
106}
Logical controller for one input channel.
void setContinuousRumble(bool continuous)
Holds this controller's motor on or off (see InputDriver::setContinuousRumble()).
bool isPressed(uint32_t logicalButtonMask) const
bool isSecondaryPressed() const
void update(const InputPadData &data, float deltaTime)
Updates the controller state.
bool isHeld(uint32_t logicalButtonMask) const
bool isPrimaryPressed() const
InputController(int channel)
The universal input payload for UI elements.
Definition InputData.h:57
uint32_t buttons_d
Pressed this frame (down)
Definition InputData.h:61
uint32_t buttons_h
Held this frame.
Definition InputData.h:62