2025-06-16 03:14:07 +01:00

114 lines
3.1 KiB
C++

#include "input.hpp"
Input::Input() {
}
Input::~Input() {
}
void Input::handleEvents() {
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_EVENT_QUIT:
quitRequestedFlag = true;
break;
case SDL_EVENT_KEY_DOWN:
if (keyStates[event.key.scancode] == false) {
keyFallingEdgeStates[event.key.scancode] = true;
}
keyStates[event.key.scancode] = true;
break;
case SDL_EVENT_KEY_UP:
keyStates[event.key.scancode] = false;
keyFallingEdgeStates[event.key.scancode] = false;
break;
case SDL_EVENT_MOUSE_BUTTON_DOWN:
if (mouseButtonStates[event.button.button] == false) {
mouseButtonFallingEdgeStates[event.button.button] = true;
}
mouseButtonStates[event.button.button] = true;
break;
case SDL_EVENT_MOUSE_BUTTON_UP:
mouseButtonStates[event.button.button] = false;
mouseButtonFallingEdgeStates[event.button.button] = false;
break;
case SDL_EVENT_MOUSE_MOTION:
mousePosition.x = static_cast<int>(event.motion.x);
mousePosition.y = static_cast<int>(event.motion.y);
break;
}
}
}
bool Input::quitRequested() const {
return quitRequestedFlag;
}
bool Input::isKeyPressed(SDL_Scancode key) {
auto it = keyStates.find(key);
return it != keyStates.end() && it->second;
}
bool Input::isKeyPressed(std::vector<SDL_Scancode> keys) {
for (const auto& key : keys) {
if (isKeyPressed(key)) {
return true;
}
}
return false;
}
bool Input::isMouseButtonPressed(Uint8 button) {
auto it = mouseButtonStates.find(button);
return it != mouseButtonStates.end() && it->second;
}
bool Input::isMouseButtonPressed(std::vector<Uint8> buttons) {
for (const auto& button : buttons) {
if (isMouseButtonPressed(button)) {
return true;
}
}
return false;
}
Vector2 Input::getMousePosition() const {
return mousePosition;
}
bool Input::hasKeyBeenPressed(SDL_Scancode key) {
auto it = keyFallingEdgeStates.find(key);
if (it != keyFallingEdgeStates.end() && it->second) {
keyFallingEdgeStates[key] = false;
return true;
}
return false;
}
bool Input::hasKeyBeenPressed(std::vector<SDL_Scancode> keys) {
for (const auto& key : keys) {
if (hasKeyBeenPressed(key)) {
return true;
}
}
return false;
}
bool Input::hasMouseButtonBeenPressed(Uint8 button) {
auto it = mouseButtonFallingEdgeStates.find(button);
if (it != mouseButtonFallingEdgeStates.end() && it->second) {
mouseButtonFallingEdgeStates[button] = false;
return true;
}
return false;
}
bool Input::hasMouseButtonBeenPressed(std::vector<Uint8> buttons) {
for (const auto& button : buttons) {
if (hasMouseButtonBeenPressed(button)) {
return true;
}
}
return false;
}