Brunot
Loading...
Searching...
No Matches
Input.h
Go to the documentation of this file.
1// File: Input.h
2// Description: The universal input system
3// Author(s): Bryley Elder (bryley.elder@digipen.edu) Base Implementation
4// Ori Balashov (ori.balashov@digipen.edu) Refactoring and additions
5// 2025 / 09 / 14
6// (C) Digipen 2025
7// ____ __ __ __
8// /\__ _\/\ \ /\ \/\ \
9// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
10// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
11// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
12// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
13// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
14#pragma once
15
16
17#include "System/opengl.h"
18#include <unordered_map>
19#include "Logging.h"
21
22namespace sys
23{
24class Editor;
25class Input;
26}
27
29
31{
32
33public:
35 Controllers(int playerNumber); // Will be standard initializer when there are multiple players
37
38 auto testController() -> void; // Temporary test function
39
40 auto showMenu() -> void;
41
42 auto isPressed(const std::string& keyName) -> bool;
43
44 auto Update() -> void;
45
46private:
47 static std::unordered_map<std::string, unsigned int> bind;
48
49 static int playerNum;
50 static int active;
51 static int NUMBER_OF_AXES;
52 const unsigned char* hat;
53};
54
55
56class Keys
57{
58public:
59 friend sys::Input;
60 friend sys::Editor;
61 Keys();
62
63 auto update(float dt) -> void;
64
65 auto getInputMap() const -> const std::unordered_map<int, std::string>&;
66
67 auto showMenu() -> void;
68
69 auto getEnabled() const -> bool;
70 auto setEnabled(bool newState) -> void;
71
72 // Gets the mouse position in GLFW space (top left is 0,0)
73 auto getMousePosition() const -> std::pair<float,float>;
74
75 // Gets the mouse position in the world of the gameplay viewport (center is 0,0)
76 auto getMouseWorldCoords() const -> const Vector4D&;
77
79 static auto keyCallback(GLFWwindow* window, int pressedKey, int scancode, int action, int mods) -> void;
80
82 static auto mouseButtonCallback(GLFWwindow* window, int pressedButton, int action, int mods) -> void;
83
85 static auto sendKeyPressedMessage(unsigned int GLFWKey) -> void;
86
87
88
89private:
91 std::unordered_map<int, std::string> inputMap;
92
94 bool enabled;
95
96 Vector4D mouseWorldCoords = {0.f, 0.f, 0.f, 1.f};
97
98 auto setMouseWorldCoords(const Vector4D& coords) -> void;
100};
101
102namespace sys
103{
104class Input : public System
105{
106
107public:
109 : System("Input", Type::Input)
110 {
111 }
112
113 ~Input() override = default;
114
118 static auto getEnum() -> Type
119 {
120 return Type::Input;
121 }
122
123 auto clone() const -> std::unique_ptr<GameObject> override;
124
125protected:
126 auto systemShowMenu() -> void override;
127
128public:
129 auto onUpdate(float dt) -> void override;
130
131 auto endWindow() -> void override
132 {
133 } // for debug ImGUI
134 auto onRender() -> void override;
135 auto load(Stream& stream) -> void override;
136 auto save(Stream& stream) const -> void override;
137
138 auto setEnabled(bool newState) -> void;
139 auto getEnabled() const -> bool;
140
145 [[nodiscard]] auto keys() -> Keys&
146 {
147 return k;
148 }
149
154 [[nodiscard]] auto controller() -> Controllers&
155 {
156 return c;
157 }
158
163 auto getMousePosition() const -> std::pair<float,float>;
164
169 auto getWorldMousePosition() const -> const Vector4D&;
170
171private:
174
175};
176}
sys::Json Stream
Definition AudioObject.h:20
static int pos[2]
Definition Editor.cpp:69
Definition Input.h:31
static int playerNum
Definition Input.h:49
static int NUMBER_OF_AXES
Definition Input.h:51
auto isPressed(const std::string &keyName) -> bool
Definition Input.cpp:263
Controllers()
Definition Input.cpp:218
auto testController() -> void
Definition Input.cpp:246
const unsigned char * hat
Definition Input.h:52
static std::unordered_map< std::string, unsigned int > bind
Definition Input.h:47
~Controllers()
Definition Input.cpp:241
auto Update() -> void
Definition Input.cpp:258
static int active
Definition Input.h:50
auto showMenu() -> void
Definition Input.cpp:254
the base class for the engine, most things inherit from this.
Definition GameObject.h:77
Definition Input.h:57
auto getInputMap() const -> const std::unordered_map< int, std::string > &
Definition Input.cpp:148
auto setImGuiMouseWorldCoords(Vector4D &pos) -> void
Definition Input.cpp:187
static auto sendKeyPressedMessage(unsigned int GLFWKey) -> void
Helper function that creates and broadcasts a message of a key being pressed.
Definition Input.cpp:196
std::unordered_map< int, std::string > inputMap
A map that takes GLFW key codes and maps them to input event names.
Definition Input.h:91
auto getMouseWorldCoords() const -> const Vector4D &
Definition Input.cpp:182
bool enabled
If this is off, we don't do anything on key press.
Definition Input.h:94
Vector4D mouseWorldCoords
Definition Input.h:96
auto showMenu() -> void
Definition Input.cpp:153
auto setEnabled(bool newState) -> void
Definition Input.cpp:169
auto getEnabled() const -> bool
Definition Input.cpp:164
auto getMousePosition() const -> std::pair< float, float >
Definition Input.cpp:174
Keys()
Definition Input.cpp:73
auto update(float dt) -> void
Definition Input.cpp:118
static auto mouseButtonCallback(GLFWwindow *window, int pressedButton, int action, int mods) -> void
Registers which mouse buttons are pressed.
Definition Input.cpp:64
static auto keyCallback(GLFWwindow *window, int pressedKey, int scancode, int action, int mods) -> void
Registers which keys are pressed.
Definition Input.cpp:46
auto setMouseWorldCoords(const Vector4D &coords) -> void
Definition Input.cpp:206
Type
Definition System.h:20
@ Input
Definition System.h:24
System(const std::string &typeName, Type systemType)
Definition System.cpp:75
Definition Editor.h:37
Definition Input.h:105
auto controller() -> Controllers &
Definition Input.h:154
auto getMousePosition() const -> std::pair< float, float >
Gets the current mouse position in screen coordinates.
Definition Input.cpp:333
Controllers c
Definition Input.h:173
auto endWindow() -> void override
currently unused, but intended to but was assumed to be necessary for a proper ImGui editor
Definition Input.h:131
auto getEnabled() const -> bool
Definition Input.cpp:328
static auto getEnum() -> Type
function required by all systems
Definition Input.h:118
Keys k
Definition Input.h:172
auto onRender() -> void override
called every frame after update has been called for every object.
Definition Input.cpp:307
~Input() override=default
Input()
Definition Input.h:108
auto onUpdate(float dt) -> void override
called once every frame.
Definition Input.cpp:295
auto setEnabled(bool newState) -> void
Definition Input.cpp:323
auto systemShowMenu() -> void override
Specific systems should override this function to show their specific menu for the system.
Definition Input.cpp:285
auto save(Stream &stream) const -> void override
Implementations will load the state of a GameObject to a th::Json object.
Definition Input.cpp:318
auto load(Stream &stream) -> void override
Definition Input.cpp:313
auto keys() -> Keys &
Definition Input.h:145
auto getWorldMousePosition() const -> const Vector4D &
Gets the current mouse position in world coordinates.
Definition Input.cpp:338
auto clone() const -> std::unique_ptr< GameObject > override
makes a copy a GameObject even if it's held polymorphically
Definition Input.cpp:280
the type of elements in a basic_json container
Definition GameObject.h:32
Definition KeyPressedMessage.h:25
Definition Vector4D.h:23