QuasarEngine
Cross-platform 3D engine and editor
Loading...
Searching...
No Matches
GamepadEvent.h
Go to the documentation of this file.
1#pragma once
2
7
10
11#include <spdlog/fmt/fmt.h>
12
13#include <string>
14#include <utility>
15
16namespace Quasar
17{
19 class GamepadConnectedEvent final : public EventOf<EventType::GamepadConnected, EventCategory::Gamepad>
20 {
21 public:
25 GamepadConnectedEvent(GamepadId gamepad, std::string name) : m_Gamepad(gamepad), m_Name(std::move(name)) {}
26
28 GamepadId GetGamepad() const { return m_Gamepad; }
29
31 const std::string& GetGamepadName() const { return m_Name; }
32
34 std::string ToString() const override { return fmt::format("{}: {} ({})", GetName(), static_cast<unsigned>(m_Gamepad), m_Name); }
35
36 private:
37 GamepadId m_Gamepad{};
38 std::string m_Name;
39 };
40
42 class GamepadDisconnectedEvent final : public EventOf<EventType::GamepadDisconnected, EventCategory::Gamepad>
43 {
44 public:
46 explicit GamepadDisconnectedEvent(GamepadId gamepad) : m_Gamepad(gamepad) {}
47
49 GamepadId GetGamepad() const { return m_Gamepad; }
50
52 std::string ToString() const override { return fmt::format("{}: {}", GetName(), static_cast<unsigned>(m_Gamepad)); }
53
54 private:
55 GamepadId m_Gamepad{};
56 };
57
59 class GamepadButtonPressedEvent final : public EventOf<EventType::GamepadButtonPressed, EventCategory::Input | EventCategory::Gamepad>
60 {
61 public:
63 GamepadButtonPressedEvent(GamepadId gamepad, GamepadButton button) : m_Gamepad(gamepad), m_Button(button) {}
64
66 GamepadId GetGamepad() const { return m_Gamepad; }
67
69 GamepadButton GetButton() const { return m_Button; }
70
72 std::string ToString() const override
73 {
74 return fmt::format("{}: {}, {}", GetName(), static_cast<unsigned>(m_Gamepad), static_cast<unsigned>(m_Button));
75 }
76
77 private:
78 GamepadId m_Gamepad{};
79 GamepadButton m_Button{};
80 };
81
83 class GamepadButtonReleasedEvent final : public EventOf<EventType::GamepadButtonReleased, EventCategory::Input | EventCategory::Gamepad>
84 {
85 public:
87 GamepadButtonReleasedEvent(GamepadId gamepad, GamepadButton button) : m_Gamepad(gamepad), m_Button(button) {}
88
90 GamepadId GetGamepad() const { return m_Gamepad; }
91
93 GamepadButton GetButton() const { return m_Button; }
94
96 std::string ToString() const override
97 {
98 return fmt::format("{}: {}, {}", GetName(), static_cast<unsigned>(m_Gamepad), static_cast<unsigned>(m_Button));
99 }
100
101 private:
102 GamepadId m_Gamepad{};
103 GamepadButton m_Button{};
104 };
105
107 class GamepadAxisMovedEvent final : public EventOf<EventType::GamepadAxisMoved, EventCategory::Input | EventCategory::Gamepad>
108 {
109 public:
114 GamepadAxisMovedEvent(GamepadId gamepad, GamepadAxis axis, float value) : m_Gamepad(gamepad), m_Axis(axis), m_Value(value) {}
115
117 GamepadId GetGamepad() const { return m_Gamepad; }
118
120 GamepadAxis GetAxis() const { return m_Axis; }
121
123 float GetValue() const { return m_Value; }
124
126 std::string ToString() const override
127 {
128 return fmt::format("{}: {}, {}, {}", GetName(), static_cast<unsigned>(m_Gamepad), static_cast<unsigned>(m_Axis), m_Value);
129 }
130
131 private:
132 GamepadId m_Gamepad{};
133 GamepadAxis m_Axis{};
134 float m_Value = 0.0f;
135 };
136}
Base class of all events, with their types and categories.
Codes of the gamepads, of their buttons and of their axes, independent from GLFW.
Base class of each event class, which gives the event its type and its categories.
Definition Event.h:164
const char * GetName() const
Returns the name of the type of the event, for example "KeyPressed".
Definition Event.h:128
float GetValue() const
Returns the new value of the axis: from -1 to 1 for a stick axis, from 0 to 1 for a trigger.
Definition GamepadEvent.h:123
std::string ToString() const override
Returns the event with the id, the axis code and the value, for example "GamepadAxisMoved: 0,...
Definition GamepadEvent.h:126
GamepadAxisMovedEvent(GamepadId gamepad, GamepadAxis axis, float value)
Creates the event with the new value of the axis.
Definition GamepadEvent.h:114
GamepadId GetGamepad() const
Returns the id of the gamepad.
Definition GamepadEvent.h:117
GamepadAxis GetAxis() const
Returns the axis that moved.
Definition GamepadEvent.h:120
std::string ToString() const override
Returns the event with the id and the button code, for example "GamepadButtonPressed: 0,...
Definition GamepadEvent.h:72
GamepadButtonPressedEvent(GamepadId gamepad, GamepadButton button)
Creates the event for the pressed button.
Definition GamepadEvent.h:63
GamepadId GetGamepad() const
Returns the id of the gamepad.
Definition GamepadEvent.h:66
GamepadButton GetButton() const
Returns the pressed button.
Definition GamepadEvent.h:69
GamepadButton GetButton() const
Returns the released button.
Definition GamepadEvent.h:93
GamepadButtonReleasedEvent(GamepadId gamepad, GamepadButton button)
Creates the event for the released button.
Definition GamepadEvent.h:87
std::string ToString() const override
Returns the event with the id and the button code, for example "GamepadButtonReleased: 0,...
Definition GamepadEvent.h:96
GamepadId GetGamepad() const
Returns the id of the gamepad.
Definition GamepadEvent.h:90
std::string ToString() const override
Returns the event with the id and the name, for example "GamepadConnected: 0 (Xbox Controller)".
Definition GamepadEvent.h:34
GamepadConnectedEvent(GamepadId gamepad, std::string name)
Creates the event for the connected gamepad.
Definition GamepadEvent.h:25
GamepadId GetGamepad() const
Returns the id of the gamepad.
Definition GamepadEvent.h:28
const std::string & GetGamepadName() const
Returns the name of the gamepad.
Definition GamepadEvent.h:31
std::string ToString() const override
Returns the event with the id, for example "GamepadDisconnected: 0".
Definition GamepadEvent.h:52
GamepadDisconnectedEvent(GamepadId gamepad)
Creates the event for the disconnected gamepad.
Definition GamepadEvent.h:46
GamepadId GetGamepad() const
Returns the id the gamepad had. A gamepad connected later can get the same id.
Definition GamepadEvent.h:49
Contains all the engine code.
GamepadButton
Button of a gamepad, at the position of the buttons of an Xbox controller.
Definition GamepadCodes.h:34
GamepadId
Identifies a connected gamepad, from 0 to MaxGamepads - 1.
Definition GamepadCodes.h:26
GamepadAxis
Axis of a gamepad.
Definition GamepadCodes.h:57