aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine/input.cpp
blob: ffa8076311cc9539b84b336670434c73586257e2 (plain) (blame)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "engine/input.hpp"
#include "imgui_impl_sdl3.h"

void Input::begin_frame() {
    // Save previous state
    for (int i = 0; i < SDL_SCANCODE_COUNT; i++) {
        key_prev_[i] = key_state_[i];
        key_just_pressed_[i]  = false;
        key_just_released_[i] = false;
    }
    mouse_prev_ = mouse_pos_;
    mouse_delta_ = glm::vec2(0.0f);
    scroll_ = 0.0f;
    for (int i = 0; i < 8; i++) {
        mouse_prev_state_[i] = mouse_state_[i];
        mouse_pressed_[i]  = false;
        mouse_released_[i] = false;
    }

    // Pump SDL events
    SDL_Event event;
    while (SDL_PollEvent(&event)) {
        ImGui_ImplSDL3_ProcessEvent(&event);   // let the dev UI see input too
        switch (event.type) {
        case SDL_EVENT_QUIT:
            quit_ = true;
            break;

        case SDL_EVENT_KEY_DOWN: {
            auto sc = event.key.scancode;
            if (sc < SDL_SCANCODE_COUNT) {
                if (!key_state_[sc]) key_just_pressed_[sc] = true;
                key_state_[sc] = true;
            }
            break;
        }
        case SDL_EVENT_KEY_UP: {
            auto sc = event.key.scancode;
            if (sc < SDL_SCANCODE_COUNT) {
                key_just_released_[sc] = true;
                key_state_[sc] = false;
            }
            break;
        }

        case SDL_EVENT_MOUSE_MOTION:
            mouse_pos_ = glm::vec2(event.motion.x, event.motion.y);
            mouse_delta_ = glm::vec2(event.motion.xrel, event.motion.yrel);
            break;

        case SDL_EVENT_MOUSE_BUTTON_DOWN:
            if (event.button.button < 8) {
                if (!mouse_state_[event.button.button])
                    mouse_pressed_[event.button.button] = true;
                mouse_state_[event.button.button] = true;
            }
            break;
        case SDL_EVENT_MOUSE_BUTTON_UP:
            if (event.button.button < 8) {
                mouse_released_[event.button.button] = true;
                mouse_state_[event.button.button] = false;
            }
            break;

        case SDL_EVENT_MOUSE_WHEEL:
            scroll_ = event.wheel.y;
            break;

        default:
            break;
        }
    }
}

bool Input::key_down(SDL_Keycode key) const {
    return key_state_[SDL_GetScancodeFromKey(key, nullptr)];
}

bool Input::key_pressed(SDL_Keycode key) const {
    return key_just_pressed_[SDL_GetScancodeFromKey(key, nullptr)];
}

bool Input::key_released(SDL_Keycode key) const {
    return key_just_released_[SDL_GetScancodeFromKey(key, nullptr)];
}

glm::vec2 Input::mouse_pos() const   { return mouse_pos_; }
glm::vec2 Input::mouse_delta() const { return mouse_delta_; }
float Input::scroll_delta() const    { return scroll_; }

bool Input::mouse_down(int button) const {
    return button < 8 && mouse_state_[button];
}

bool Input::mouse_pressed(int button) const {
    return button < 8 && mouse_pressed_[button];
}

bool Input::mouse_released(int button) const {
    return button < 8 && mouse_released_[button];
}