aboutsummaryrefslogtreecommitdiff
path: root/mwe/events/src/inputSystem.cpp
blob: ffab84be5934bb63903aa8859b45afdbf443b105 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "inputSystem.h"

InputSystem::InputSystem() {}

void InputSystem::registerButton(Button* button) {
    buttons.push_back(button);
}

void InputSystem::processInput() {
	
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        switch (event.type) {
            case SDL_QUIT:
                triggerEvent(ShutDownEvent());
                break;
            case SDL_KEYDOWN:
                triggerEvent(KeyPressedEvent(getCustomKey(event.key.keysym.sym)));
                break;
            case SDL_MOUSEBUTTONDOWN: {
                int mouseX, mouseY;
                SDL_GetMouseState(&mouseX, &mouseY);
                processMouseClick(mouseX, mouseY);
                triggerEvent(MousePressedEvent(mouseX, mouseY));
                break;
            }
        }
    }
}

void InputSystem::processMouseClick(int mouseX, int mouseY) {
    for (auto* button : buttons) {
        if (mouseX >= button->x && mouseX <= (button->x + button->width) &&
            mouseY >= button->y && mouseY <= (button->y + button->height)) {
            button->onClick();
        }
    }
}