PocketPutr/Desktop_Test/input_manager.cpp
2025-12-15 19:44:36 +01:00

104 lines
2.2 KiB
C++

#include "event_manager.h"
#include "GLOBALS.h"
#include "tft_handler.h"
#include "HardwareSerial.h"
#include "input_manager.h"
void InputManager::init(DISPLAY_STATE* display_state, TFT_Handler* tf) {
_display_state = display_state;
_tf = tf;
mi = {
.x = 5,
.y = 5,
.size_x = 4,
.size_y = 4,
.color = 0x0000,
};
for (int i = 0; i < NUM_BUTTONS; i++) {
pinMode(BUTTON_PINS[i], INPUT_PULLUP);
}
}
void InputManager::update() {
for (int i = 0; i < NUM_BUTTONS; i++) {
int reading = digitalRead(BUTTON_PINS[i]);
if (reading != lastButtonState[i]) {
lastDebounceTime[i] = millis();
}
if ((millis() - lastDebounceTime[i]) > DEBOUNCE_DELAY) {
if (reading != buttonState[i]) {
buttonState[i] = reading;
if (buttonState[i] == LOW) {
handle_button_press(i);
}
}
}
lastButtonState[i] = reading;
}
}
void InputManager::handle_button_press(int buttonIndex) {
bool needs_redraw = false;
switch(buttonIndex) {
case 0:
mi.x = (mi.x + 5 > 476) ? 476 : mi.x + 5;
needs_redraw = true;
break;
case 1:
mi.y = (mi.y + 5 > 316) ? 316 : mi.y + 5;
needs_redraw = true;
break;
case 2:
mi.y = (mi.y - 5 < 0) ? 0 : mi.y - 5;
needs_redraw = true;
break;
case 3:
mi.x = (mi.x - 5 < 0) ? 0 : mi.x - 5;
needs_redraw = true;
break;
case 4: // Click
{
CLICK_EVENT event = {
.x = mi.x,
.y = mi.y,
.event = CLICK_EVENTS::LEFT_CLICK
};
EventManager::getInstance().publish(event);
}
break;
case 5: // Right click
{
CLICK_EVENT event = {
.x = mi.x,
.y = mi.y,
.event = CLICK_EVENTS::RIGHT_CLICK
};
EventManager::getInstance().publish(event);
}
break;
}
if (needs_redraw) {
_display_state->update_display.store(true);
}
}
void InputManager::draw_button() {
_tf->draw_box(mi.x, mi.y, mi.size_x, mi.size_y, mi.color);
}
bool InputManager::are_buttons_pressed(int btn1, int btn2) {
return (buttonState[btn1] == LOW && buttonState[btn2] == LOW);
}