VisuTwin Canvas
C++ 3D Engine — Metal Backend
Loading...
Searching...
No Matches
input.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2025-2026 Arnis Lektauers
3//
4// Created by Arnis Lektauers on 24.07.2025.
5//
6
7#pragma once
8#include <unordered_map>
9
10#include "core/math/vector2.h"
11#include "SDL3/SDL_events.h"
12
13namespace visutwin::canvas
14{
15 enum class Key {
16 W, A, S, D, Q, E
17 };
18
19 enum class MouseButton {
23 };
24
25 class Input {
26 public:
27 Input(const int windowWidth, const int windowHeight): windowSize(windowWidth, windowHeight) {}
28
29 void handleEvent(const SDL_Event& event);
30 void newFrame(); // Call at the start of each frame
31
32 [[nodiscard]] bool isKeyDown(Key key) const;
33 [[nodiscard]] bool isKeyPressed(Key key) const; // Key just pressed this frame
34 [[nodiscard]] bool isMouseButtonDown(MouseButton button) const;
35
36 [[nodiscard]] const Vector2& getMouseDelta() const {
37 return mouseDelta;
38 }
39
40 [[nodiscard]] bool mouseMoved() const {
41 return mouseDelta.length() > 0.0f;
42 }
43
45 {
46 return windowSize;
47 }
48
49 private:
50 Vector2 mousePosition{0.0f};
51 Vector2 prevMousePosition{0.0f};
52 Vector2 mouseDelta{0.0f};
53
54 Vector2u windowSize{0};
55
56 std::unordered_map<SDL_Scancode, bool> currentKeys;
57 std::unordered_map<SDL_Scancode, bool> previousKeys;
58
59 uint32_t mouseButtonMask = 0;
60 };
61}
62
bool isMouseButtonDown(MouseButton button) const
Definition input.cpp:83
Input(const int windowWidth, const int windowHeight)
Definition input.h:27
void handleEvent(const SDL_Event &event)
Definition input.cpp:37
bool mouseMoved() const
Definition input.h:40
Vector2u getWindowSize() const
Definition input.h:44
bool isKeyDown(Key key) const
Definition input.cpp:71
bool isKeyPressed(Key key) const
Definition input.cpp:77
const Vector2 & getMouseDelta() const
Definition input.h:36
Vector2T< uint32_t > Vector2u
Definition vector2.h:127
2D vector for UV coordinates, screen positions, and 2D math.
Definition vector2.h:18