Brunot
Loading...
Searching...
No Matches
Controllers.h
Go to the documentation of this file.
1
12// ____ __ __ __
13// /\__ _\/\ \ /\ \/\ \
14// \/_/\ \/\ \ \___ __ \ \ \_\ \ ___ __ __ ____ __
15// \ \ \ \ \ _ `\ /'__`\ \ \ _ \ / __`\/\ \/\ \ /',__\ /'__`\
16// \ \ \ \ \ \ \ \/\ __/ \ \ \ \ \/\ \L\ \ \ \_\ \/\__, `\/\ __/
17// \ \_\ \ \_\ \_\ \____\ \ \_\ \_\ \____/\ \____/\/\____/\ \____\
18// \/_/ \/_/\/_/\/____/ \/_/\/_/\/___/ \/___/ \/___/ \/____/
19
20
21
22#pragma once
23
24#include <array>
25#include <memory>
26#include <string>
27#include <unordered_map>
29
30class ControllerStick;
32
38{
39
40public:
42
43 auto update(float dt) -> void;
44 auto showMenu() -> void;
45
47 auto getHeldButtonsMap() -> std::unordered_map<int, std::string>&;
49 auto getTapButtonsMap() -> std::unordered_map<int, std::string>&;
51 auto getSticksMap() -> std::unordered_map<int, std::pair<std::string, std::string>>;
52
54 auto getEnabled() const -> bool;
56 auto setEnabled(bool newState) -> void;
57
58
63 auto removeControllerID(int id) -> void;
64
66 auto connectController(int controllerID) -> void;
67
69 static auto getControllerNameFromID(int id) -> std::string;
70
72 static auto controllerCallback(int joystickID, int event) -> void;
73
74private:
75
77 bool enabled;
78
80 std::vector<int> connectedControllers;
81
83 std::unordered_map<int, std::string> holdButtonMap;
84
86 std::unordered_map<int, std::string> tapButtonMap;
87
89 std::unordered_map<int, std::pair<std::string, std::string>> stickMap;
90
92 std::array<controllerInputTracker, 16> lastFrameControllerState;
93
96
97 auto getButtonStatesOfController(int controllerID) -> std::array<std::unique_ptr<ControllerButton>,15>&;
98 auto getStickStatesOfController(int controllerID) -> std::array<ControllerStick,6>&;
99
100
101};
102
Helper class used by Controllers to handle the logic of knowing when a button is pressed down or not.
Definition ControllerButton.h:28
Helper class that assists Controllers by managing when a stick should be considered pressed or not.
Definition ControllerStick.h:30
static auto controllerCallback(int joystickID, int event) -> void
Callback used by GLFW whenever a controller is connected/disconnected.
Definition Controllers.cpp:224
auto getEnabled() const -> bool
Returns if controller input is currently enabled or not.
Definition Controllers.cpp:208
static auto getControllerNameFromID(int id) -> std::string
Public function that gets the name for a controller based on its ID. Primarily used for debugging.
Definition Controllers.cpp:259
auto getStickStatesOfController(int controllerID) -> std::array< ControllerStick, 6 > &
Definition Controllers.cpp:270
auto getButtonStatesOfController(int controllerID) -> std::array< std::unique_ptr< ControllerButton >, 15 > &
Definition Controllers.cpp:264
auto getSticksMap() -> std::unordered_map< int, std::pair< std::string, std::string > >
Returns the map of all the sticks and their corresponding events.
Definition Controllers.cpp:203
auto setEnabled(bool newState) -> void
Sets whether input from controllers are accepted or not.
Definition Controllers.cpp:213
Controllers()
Definition Controllers.cpp:36
bool enabled
Bool used to see if we should be receiving input from controllers.
Definition Controllers.h:77
auto connectController(int controllerID) -> void
Helper function that handles all the necessary protocols whenever a new controller is detected.
Definition Controllers.cpp:239
auto removeControllerID(int id) -> void
Removes a controller ID from the updating list, meaning its inputs will no longer be received.
Definition Controllers.cpp:218
auto getTapButtonsMap() -> std::unordered_map< int, std::string > &
Returns the map of all buttons that have ControllerButton behavior and their corresponding event.
Definition Controllers.cpp:198
std::unordered_map< int, std::pair< std::string, std::string > > stickMap
Map that takes GLFW gamepad sticks and maps them to the two events per stick (sticks go from -1 to 1,...
Definition Controllers.h:89
auto update(float dt) -> void
Definition Controllers.cpp:95
std::array< controllerInputTracker, 16 > lastFrameControllerState
An array that tracks the previous frame state of the buttons and joysticks for controllers.
Definition Controllers.h:92
std::vector< int > connectedControllers
Tracks every single controller that is currently plugged in.
Definition Controllers.h:80
int maxConnectedControllers
Tracker used to see if we need to create another controllerInputTracker (so we're not allocating a lo...
Definition Controllers.h:95
auto showMenu() -> void
Definition Controllers.cpp:159
auto getHeldButtonsMap() -> std::unordered_map< int, std::string > &
Returns the map of all buttons that have ControllerHoldButton behavior and their corresponding event.
Definition Controllers.cpp:193
std::unordered_map< int, std::string > holdButtonMap
Map that takes GLFW gamepad buttons and maps them to input event names. Should only store buttons tha...
Definition Controllers.h:83
std::unordered_map< int, std::string > tapButtonMap
Map that takes GLFW gamepad buttons and maps them to input event names. Should only store buttons tha...
Definition Controllers.h:86
Helper struct used by Controllers to handle the trouble of storing all the buttons and sticks.