KarmaEngine
Game Engine for practical learning and research purposes
Loading...
Searching...
No Matches
Input.h
Go to the documentation of this file.
1
10#pragma once
11
12#include "krpch.h"
13
14#include "Karma/Log.h"
15#include "Karma/Events/Event.h"
16#include "Karma/Window.h"
18
19/*
20 Input system need complete revamp in the sense to provide enough context for input maping
21 and being able to work with specialized controller devices with minimal latency.
22
23 Partial progress has been made towards that direction.
24*/
25
26
27namespace Karma
28{
35 {
39 None = 0,
44 };
45
51 struct InputData
52 {
53 };
54
55 class KARMA_API GameAction
56 {
57 public:
58 GameAction();
59
60 // Getters
61 const std::string& GetGAName() const { return m_ActionName; }
62 const std::shared_ptr<void>& GetClassAction() const { return m_ClassAction; }
63 private:
64 // The class/object pointer, imparting the action
65 std::shared_ptr<void> m_ClassAction;
66 std::string m_ActionName;
67 };
68
69 class KARMA_API Button
70 {
71 public:
72 Button();
73
74 // Getters
75 const int& GetButtonID() const { return m_ButtonID; }
76 private:
77 int m_ButtonID;
78
79 };
80
81 class KARMA_API ControllerDevice
82 {
83 public:
84 ControllerDevice(int cID, const std::string& dName);
85
86 void AddInputMapping();
87
88 // Getters
89 const int& GetControllerID() const { return m_ControllerID; }
90 const std::string& GetDeviceName() const { return m_DeviceName; }
91
92 private:
93 std::unordered_map<std::shared_ptr<Button>, std::shared_ptr<GameAction>> m_InputMapping;
94 int m_ControllerID;
95 std::string m_DeviceName;
96 bool m_IsGamePad;
97 };
98
105 {
106 public:
130 Input(InputData& inputDatRef);
131
137 ~Input();
138
148 inline static bool IsKeyPressed(int keycode)
149 {
150 return s_Instance->IsKeyPressedImpl(keycode);
151 }
152
162 inline static bool IsMouseButtonPressed(int button)
163 {
164 return s_Instance->IsMouseButtonPressedImpl(button);
165 }
166
176 inline static bool IsMouseButtonReleased(int button)
177 {
178 return s_Instance->IsMouseButtonReleasedImpl(button);
179 }
180
191 inline static bool IsControllerButtonPressed(int button, int cID)
192 {
193 return s_Instance->IsControllerButtonPressedImpl(button, cID);
194 }
195
208 inline static float ControllerAxisPivotVal(int axis, int cID)
209 {
210 return s_Instance->ControllerAxisPivotValImpl(axis, cID);
211 }
212
219 inline static std::pair<float, float>GetMousePosition()
220 {
221 return s_Instance->GetMousePositionImpl();
222 }
223
230 inline static float GetMouseX()
231 {
232 return s_Instance->GetMouseXImpl();
233 }
234
241 inline static float GetMouseY()
242 {
243 return s_Instance->GetMouseYImpl();
244 }
245
252 inline static InputRegisteringAPI GetAPI() { return s_InputAPI; }
253
259 inline static void DeInit()
260 {
261 KR_CORE_INFO("Shutting down input system");
262 }
263
273 static void Init();
274
280 static std::shared_ptr<Input> GetInputInstance() { return s_Instance; }
281
282 using EventCallbackFn = std::function<void(Event&)>;
283
293 virtual void SetEventCallback(const EventCallbackFn& callback, Window* window) = 0;
294
317 void SetGamepadMapping();
318
319 // Debug purposes
325 static void DisplayControllerDevices();
326
327 protected:
335 virtual bool IsKeyPressedImpl(int keycode) = 0;
336
344 virtual bool IsMouseButtonPressedImpl(int button) = 0;
345
353 virtual bool IsMouseButtonReleasedImpl(int button) = 0;
354
364 virtual bool IsControllerButtonPressedImpl(int button, int cID) = 0;
365
375 virtual float ControllerAxisPivotValImpl(int axis, int cID) = 0;
376
382 virtual std::pair<float, float> GetMousePositionImpl() = 0;
383
389 virtual float GetMouseXImpl() = 0;
390
396 virtual float GetMouseYImpl() = 0;
397
398 // GLFW specific
404 static void SetConnectedJoySticks();
405
406 // Getters
412 const std::list<std::shared_ptr<ControllerDevice>>& GetControllerDevices() const { return m_ControllerDevices; }
413
420 void AddControllerDevice(std::shared_ptr<ControllerDevice> device);
421
422 private:
423 static InputRegisteringAPI s_InputAPI;
424 static std::shared_ptr<Input> s_Instance;
425
426 protected:
427 static std::list<std::shared_ptr<ControllerDevice>> m_ControllerDevices;
428 };
429}
#define KARMA_API
Defining Karma's API macro for storage class information.
Definition Core.h:41
This file contains the base class Event for Karma's events.
@ None
Events of the unknown category.
Definition Event.h:37
InputRegisteringAPI
The input API being used by Karma.
Definition Input.h:35
@ GlfwInput
Using the GLFW library.
Definition Input.h:43
This file contains various classes and functions for various Karma's utilities.
This file contains the Log class. I want to imagine the Log pronounciation match with that of that ht...
#define KR_CORE_INFO(...)
A macro for logging information in the Core part.
Definition Log.h:85
This file contains the Window class.
The base class of all the events for Karma.
Definition Event.h:90
static InputRegisteringAPI GetAPI()
Getter for API (enum) in use for input.
Definition Input.h:252
static void DisplayControllerDevices()
For debugging purposes, a function to print out the number of connected devices.
Definition Input.cpp:37
static std::pair< float, float > GetMousePosition()
Polling function for the inquiry of mouse position (abscissa, ordinate)
Definition Input.h:219
virtual float ControllerAxisPivotValImpl(int axis, int cID)=0
Declaration of ControllerAxisPivotVal function to be implemented.
virtual bool IsControllerButtonPressedImpl(int button, int cID)=0
Declaration of IsControllerButtonPressed function to be implemented.
static bool IsKeyPressed(int keycode)
Polling function for inquiry of key press.
Definition Input.h:148
static bool IsControllerButtonPressed(int button, int cID)
Polling function for the inquiry of game controller button pressed action.
Definition Input.h:191
virtual float GetMouseXImpl()=0
Declaration of GetMouseX function to be implemented.
virtual bool IsKeyPressedImpl(int keycode)=0
Declaration of IsKeyPressed function to be implemented.
static float GetMouseX()
Polling function for the inquiry of abscissa.
Definition Input.h:230
static float GetMouseY()
Polling function for the inquiry of ordinate.
Definition Input.h:241
const std::list< std::shared_ptr< ControllerDevice > > & GetControllerDevices() const
Getter for connected controller devices.
Definition Input.h:412
virtual bool IsMouseButtonReleasedImpl(int button)=0
Declaration of IsMouseButtonReleased function to be implemented.
static std::shared_ptr< Input > GetInputInstance()
Getter for the Input instance.
Definition Input.h:280
virtual float GetMouseYImpl()=0
Declaration of GetMouseY function to be implemented.
static bool IsMouseButtonReleased(int button)
Polling function for the inquiry of mouse button release.
Definition Input.h:176
static void SetConnectedJoySticks()
Create a list of connected joysticks (gamepads)
Definition Input.cpp:95
static void DeInit()
Deinitialize the Input system.
Definition Input.h:259
virtual void SetEventCallback(const EventCallbackFn &callback, Window *window)=0
Set the event callback for the Input.
static bool IsMouseButtonPressed(int button)
Polling function for inquiry of mouse button press.
Definition Input.h:162
Input(InputData &inputDatRef)
A constructor for initializing the Input class.
Definition Input.cpp:57
virtual bool IsMouseButtonPressedImpl(int button)=0
Declaration of IsMouseButtonPressed function to be implemeted.
static float ControllerAxisPivotVal(int axis, int cID)
Polling function for the inquiry of the Axis pivot value (analog stick deflection for instance)
Definition Input.h:208
virtual std::pair< float, float > GetMousePositionImpl()=0
Declaration of GetMousePosition function to be implemented.
void SetGamepadMapping()
Set the gamepad mapping based on a database (../Resources/Misc/GameControllerDB.txt) The mapping is d...
Definition Input.cpp:83
The abstract base class of Karma's window (for platform specific purposes)
Definition Window.h:63
Base class for Input relevant data for the variety of platforms Karma supports.
Definition Input.h:52