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 "Karma/Log.h"
13#include "Karma/Events/Event.h"
14#include "Karma/Window.h"
16
17/*
18 Input system need complete revamp in the sense to provide enough context for input maping
19 and being able to work with specialized controller devices with minimal latency.
20
21 Partial progress has been made towards that direction.
22*/
23
24
25namespace Karma
26{
33 {
37 None = 0,
42 };
43
49 struct InputData
50 {
51 };
52
53 class KARMA_API GameAction
54 {
55 public:
56 GameAction();
57
58 // Getters
59 const std::string& GetGAName() const { return m_ActionName; }
60 const std::shared_ptr<void>& GetClassAction() const { return m_ClassAction; }
61 private:
62 // The class/object pointer, imparting the action
63 std::shared_ptr<void> m_ClassAction;
64 std::string m_ActionName;
65 };
66
67 class KARMA_API Button
68 {
69 public:
70 Button();
71
72 // Getters
73 const int& GetButtonID() const { return m_ButtonID; }
74 private:
75 int m_ButtonID;
76
77 };
78
79 class KARMA_API ControllerDevice
80 {
81 public:
82 ControllerDevice(int cID, const std::string& dName);
83
84 void AddInputMapping();
85
86 // Getters
87 const int& GetControllerID() const { return m_ControllerID; }
88 const std::string& GetDeviceName() const { return m_DeviceName; }
89
90 private:
91 std::unordered_map<std::shared_ptr<Button>, std::shared_ptr<GameAction>> m_InputMapping;
92 int m_ControllerID;
93 std::string m_DeviceName;
94 bool m_IsGamePad;
95 };
96
103 {
104 public:
128 Input(InputData& inputDatRef);
129
135 ~Input();
136
146 inline static bool IsKeyPressed(int keycode)
147 {
148 return s_Instance->IsKeyPressedImpl(keycode);
149 }
150
160 inline static bool IsMouseButtonPressed(int button)
161 {
162 return s_Instance->IsMouseButtonPressedImpl(button);
163 }
164
174 inline static bool IsMouseButtonReleased(int button)
175 {
176 return s_Instance->IsMouseButtonReleasedImpl(button);
177 }
178
189 inline static bool IsControllerButtonPressed(int button, int cID)
190 {
191 return s_Instance->IsControllerButtonPressedImpl(button, cID);
192 }
193
206 inline static float ControllerAxisPivotVal(int axis, int cID)
207 {
208 return s_Instance->ControllerAxisPivotValImpl(axis, cID);
209 }
210
217 inline static std::pair<float, float>GetMousePosition()
218 {
219 return s_Instance->GetMousePositionImpl();
220 }
221
228 inline static float GetMouseX()
229 {
230 return s_Instance->GetMouseXImpl();
231 }
232
239 inline static float GetMouseY()
240 {
241 return s_Instance->GetMouseYImpl();
242 }
243
250 inline static InputRegisteringAPI GetAPI() { return s_InputAPI; }
251
257 inline static void DeInit()
258 {
259 KR_CORE_INFO("Shutting down input system");
260 }
261
271 static void Init();
272
278 static std::shared_ptr<Input> GetInputInstance() { return s_Instance; }
279
280 using EventCallbackFn = std::function<void(Event&)>;
281
291 virtual void SetEventCallback(const EventCallbackFn& callback, Window* window) = 0;
292
315 void SetGamepadMapping();
316
317 // Debug purposes
323 static void DisplayControllerDevices();
324
325 protected:
333 virtual bool IsKeyPressedImpl(int keycode) = 0;
334
342 virtual bool IsMouseButtonPressedImpl(int button) = 0;
343
351 virtual bool IsMouseButtonReleasedImpl(int button) = 0;
352
362 virtual bool IsControllerButtonPressedImpl(int button, int cID) = 0;
363
373 virtual float ControllerAxisPivotValImpl(int axis, int cID) = 0;
374
380 virtual std::pair<float, float> GetMousePositionImpl() = 0;
381
387 virtual float GetMouseXImpl() = 0;
388
394 virtual float GetMouseYImpl() = 0;
395
396 // GLFW specific
402 static void SetConnectedJoySticks();
403
404 // Getters
410 const std::list<std::shared_ptr<ControllerDevice>>& GetControllerDevices() const { return m_ControllerDevices; }
411
418 void AddControllerDevice(std::shared_ptr<ControllerDevice> device);
419
420 private:
421 static InputRegisteringAPI s_InputAPI;
422 static std::shared_ptr<Input> s_Instance;
423
424 protected:
425 static std::list<std::shared_ptr<ControllerDevice>> m_ControllerDevices;
426 };
427}
#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:35
InputRegisteringAPI
The input API being used by Karma.
Definition Input.h:33
@ GlfwInput
Using the GLFW library.
Definition Input.h:41
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:88
static InputRegisteringAPI GetAPI()
Getter for API (enum) in use for input.
Definition Input.h:250
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:217
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:146
static bool IsControllerButtonPressed(int button, int cID)
Polling function for the inquiry of game controller button pressed action.
Definition Input.h:189
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:228
static float GetMouseY()
Polling function for the inquiry of ordinate.
Definition Input.h:239
const std::list< std::shared_ptr< ControllerDevice > > & GetControllerDevices() const
Getter for connected controller devices.
Definition Input.h:410
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:278
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:174
static void SetConnectedJoySticks()
Create a list of connected joysticks (gamepads).
Definition Input.cpp:95
static void DeInit()
Deinitialize the Input system.
Definition Input.h:257
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:160
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:206
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:61
Base class for Input relevant data for the variety of platforms Karma supports.
Definition Input.h:50