DDraceNetwork Docs
input.h
Go to the documentation of this file.
1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef ENGINE_CLIENT_INPUT_H
4#define ENGINE_CLIENT_INPUT_H
5
6#include <SDL_events.h>
7#include <SDL_joystick.h>
8#include <engine/console.h>
9
10#include <engine/input.h>
11#include <engine/keys.h>
12
13#include <string>
14#include <vector>
15
16class IEngineGraphics;
17class IConfigManager;
18
19class CInput : public IEngineInput
20{
21public:
22 class CJoystick : public IJoystick
23 {
24 friend class CInput;
25
28 char m_aName[64];
29 char m_aGUID[34];
30 SDL_JoystickID m_InstanceId;
35 SDL_Joystick *m_pDelegate;
36
37 CInput *Input() { return m_pInput; }
38
39 public:
41 CJoystick(CInput *pInput, int Index, SDL_Joystick *pDelegate);
42 virtual ~CJoystick() = default;
43
44 int GetIndex() const override { return m_Index; }
45 const char *GetName() const override { return m_aName; }
46 const char *GetGUID() const { return m_aGUID; }
47 SDL_JoystickID GetInstanceId() const { return m_InstanceId; }
48 int GetNumAxes() const override { return m_NumAxes; }
49 int GetNumButtons() const override { return m_NumButtons; }
50 int GetNumBalls() const override { return m_NumBalls; }
51 int GetNumHats() const override { return m_NumHats; }
52 float GetAxisValue(int Axis) override;
53 void GetHatValue(int Hat, int (&HatKeys)[2]) override;
54 bool Relative(float *pX, float *pY) override;
55 bool Absolute(float *pX, float *pY) override;
56
57 static void GetJoystickHatKeys(int Hat, int HatValue, int (&HatKeys)[2]);
58 };
59
60private:
64
66 IConsole *Console() const { return m_pConsole; }
67
68 // joystick
69 std::vector<CJoystick> m_vJoysticks;
71 void InitJoysticks();
72 bool OpenJoystick(int JoystickIndex);
73 void CloseJoysticks();
75 static void ConchainJoystickGuidChanged(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
76 float GetJoystickDeadzone();
77
79
81
82 // IME support
85 std::vector<std::string> m_vCandidates;
87
88 // events
89 std::vector<CEvent> m_vInputEvents;
90 int64_t m_LastUpdate;
92 void AddKeyEvent(int Key, int Flags);
93 void AddTextEvent(const char *pText);
94
95 // quick access to input
97 unsigned char m_aInputState[g_MaxKeys];
99 std::vector<CTouchFingerState> m_vTouchFingerStates;
100
101 void UpdateMouseState();
102 void UpdateJoystickState();
103 void HandleJoystickAxisMotionEvent(const SDL_JoyAxisEvent &Event);
104 void HandleJoystickButtonEvent(const SDL_JoyButtonEvent &Event);
105 void HandleJoystickHatMotionEvent(const SDL_JoyHatEvent &Event);
106 void HandleJoystickAddedEvent(const SDL_JoyDeviceEvent &Event);
107 void HandleJoystickRemovedEvent(const SDL_JoyDeviceEvent &Event);
108 void HandleTouchDownEvent(const SDL_TouchFingerEvent &Event);
109 void HandleTouchUpEvent(const SDL_TouchFingerEvent &Event);
110 void HandleTouchMotionEvent(const SDL_TouchFingerEvent &Event);
111 void HandleTextEditingEvent(const char *pText, int Start, int Length);
112
114
115 bool KeyState(int Key) const;
116
117 void ProcessSystemMessage(SDL_SysWMmsg *pMsg);
118
119public:
120 CInput();
121
122 void Init() override;
123 int Update() override;
124 void Shutdown() override;
125
126 void ConsumeEvents(std::function<void(const CEvent &Event)> Consumer) const override;
127 void Clear() override;
128 float GetUpdateTime() const override;
129
131 bool ShiftIsPressed() const override { return KeyState(KEY_LSHIFT) || KeyState(KEY_RSHIFT); }
132 bool AltIsPressed() const override { return KeyState(KEY_LALT) || KeyState(KEY_RALT); }
133 bool KeyIsPressed(int Key) const override { return KeyState(Key); }
134 bool KeyPress(int Key, bool CheckCounter) const override { return CheckCounter ? (m_aInputCount[Key] == m_InputCounter) : m_aInputCount[Key]; }
135 int FindKeyByName(const char *pKeyName) const override;
136
137 size_t NumJoysticks() const override { return m_vJoysticks.size(); }
138 CJoystick *GetJoystick(size_t Index) override { return &m_vJoysticks[Index]; }
140 void SetActiveJoystick(size_t Index) override;
141
142 bool MouseRelative(float *pX, float *pY) override;
143 void MouseModeAbsolute() override;
144 void MouseModeRelative() override;
145 vec2 NativeMousePos() const override;
146 bool NativeMousePressed(int Index) const override;
147
148 const std::vector<CTouchFingerState> &TouchFingerStates() const override;
149
150 std::string GetClipboardText() override;
151 void SetClipboardText(const char *pText) override;
152
153 void StartTextInput() override;
154 void StopTextInput() override;
155 const char *GetComposition() const override { return m_CompositionString.c_str(); }
156 bool HasComposition() const override { return !m_CompositionString.empty(); }
157 int GetCompositionCursor() const override { return m_CompositionCursor; }
158 int GetCompositionLength() const override { return m_CompositionString.length(); }
159 const char *GetCandidate(int Index) const override { return m_vCandidates[Index].c_str(); }
160 int GetCandidateCount() const override { return m_vCandidates.size(); }
162 void SetCompositionWindowPosition(float X, float Y, float H) override;
163
164 bool GetDropFile(char *aBuf, int Len) override;
165};
166
167#endif
Definition: input.h:23
int m_NumButtons
Definition: input.h:32
int m_NumBalls
Definition: input.h:33
int GetNumButtons() const override
Definition: input.h:49
const char * GetName() const override
Definition: input.h:45
CJoystick()
Definition: input.h:40
int GetNumHats() const override
Definition: input.h:51
char m_aGUID[34]
Definition: input.h:29
virtual ~CJoystick()=default
float GetAxisValue(int Axis) override
Definition: input.cpp:196
SDL_JoystickID GetInstanceId() const
Definition: input.h:47
int m_Index
Definition: input.h:27
CInput * Input()
Definition: input.h:37
CInput * m_pInput
Definition: input.h:26
bool Relative(float *pX, float *pY) override
Definition: input.cpp:223
bool Absolute(float *pX, float *pY) override
Definition: input.cpp:241
const char * GetGUID() const
Definition: input.h:46
static void GetJoystickHatKeys(int Hat, int HatValue, int(&HatKeys)[2])
Definition: input.cpp:201
int m_NumHats
Definition: input.h:34
char m_aName[64]
Definition: input.h:28
int GetNumBalls() const override
Definition: input.h:50
SDL_Joystick * m_pDelegate
Definition: input.h:35
int GetNumAxes() const override
Definition: input.h:48
int GetIndex() const override
Definition: input.h:44
SDL_JoystickID m_InstanceId
Definition: input.h:30
void GetHatValue(int Hat, int(&HatKeys)[2]) override
Definition: input.cpp:218
int m_NumAxes
Definition: input.h:31
Definition: input.h:20
void HandleTouchMotionEvent(const SDL_TouchFingerEvent &Event)
Definition: input.cpp:582
void SetCompositionWindowPosition(float X, float Y, float H) override
Definition: input.cpp:614
void ProcessSystemMessage(SDL_SysWMmsg *pMsg)
Definition: input.cpp:870
std::vector< std::string > m_vCandidates
Definition: input.h:85
void HandleJoystickAxisMotionEvent(const SDL_JoyAxisEvent &Event)
Definition: input.cpp:439
std::vector< CEvent > m_vInputEvents
Definition: input.h:89
void StartTextInput() override
Definition: input.cpp:317
char m_aDropFile[IO_MAX_PATH_LENGTH]
Definition: input.h:113
IConsole * Console() const
Definition: input.h:66
void HandleJoystickRemovedEvent(const SDL_JoyDeviceEvent &Event)
Definition: input.cpp:544
CJoystick * GetJoystick(size_t Index) override
Definition: input.h:138
bool HasComposition() const override
Definition: input.h:156
void AddKeyEvent(int Key, int Flags)
Definition: input.cpp:36
void CloseJoysticks()
Definition: input.cpp:181
bool KeyState(int Key) const
Definition: input.cpp:362
bool KeyIsPressed(int Key) const override
Definition: input.h:133
bool OpenJoystick(int JoystickIndex)
Definition: input.cpp:115
void ConsumeEvents(std::function< void(const CEvent &Event)> Consumer) const override
Definition: input.cpp:334
bool KeyPress(int Key, bool CheckCounter) const override
Definition: input.h:134
uint32_t m_InputCounter
Definition: input.h:98
void UpdateMouseState()
Definition: input.cpp:389
bool ShiftIsPressed() const override
Definition: input.h:131
void HandleJoystickButtonEvent(const SDL_JoyButtonEvent &Event)
Definition: input.cpp:478
CJoystick * GetActiveJoystick() override
Definition: input.h:139
const char * GetComposition() const override
Definition: input.h:155
int m_CandidateSelectedIndex
Definition: input.h:86
void HandleTextEditingEvent(const char *pText, int Start, int Length)
Definition: input.cpp:594
void InitJoysticks()
Definition: input.cpp:95
size_t NumJoysticks() const override
Definition: input.h:137
int GetCandidateSelectedIndex() const override
Definition: input.h:161
bool AltIsPressed() const override
Definition: input.h:132
bool GetDropFile(char *aBuf, int Len) override
Definition: input.cpp:917
CJoystick * m_pActiveJoystick
Definition: input.h:70
void HandleJoystickHatMotionEvent(const SDL_JoyHatEvent &Event)
Definition: input.cpp:503
void StopTextInput() override
Definition: input.cpp:324
void HandleJoystickAddedEvent(const SDL_JoyDeviceEvent &Event)
Definition: input.cpp:536
const char * GetCandidate(int Index) const override
Definition: input.h:159
int64_t m_LastUpdate
Definition: input.h:90
void MouseModeRelative() override
Definition: input.cpp:277
const std::vector< CTouchFingerState > & TouchFingerStates() const override
Definition: input.cpp:299
void SetActiveJoystick(size_t Index) override
Definition: input.cpp:190
bool m_InputGrabbed
Definition: input.h:78
std::string m_CompositionString
Definition: input.h:83
float m_UpdateTime
Definition: input.h:91
void SetClipboardText(const char *pText) override
Definition: input.cpp:312
int FindKeyByName(const char *pKeyName) const override
Definition: input.cpp:369
bool ModifierIsPressed() const override
Definition: input.h:130
vec2 NativeMousePos() const override
Definition: input.cpp:286
float GetJoystickDeadzone()
Definition: input.cpp:162
uint32_t m_aInputCount[g_MaxKeys]
Definition: input.h:96
float GetUpdateTime() const override
Definition: input.cpp:357
int GetCandidateCount() const override
Definition: input.h:160
void Init() override
Definition: input.cpp:77
IConfigManager * m_pConfigManager
Definition: input.h:63
std::string GetClipboardText() override
Definition: input.cpp:304
void Clear() override
Definition: input.cpp:346
void Shutdown() override
Definition: input.cpp:90
void HandleTouchUpEvent(const SDL_TouchFingerEvent &Event)
Definition: input.cpp:571
unsigned char m_aInputState[g_MaxKeys]
Definition: input.h:97
void UpdateJoystickState()
Definition: input.cpp:412
IEngineGraphics * m_pGraphics
Definition: input.h:61
void UpdateActiveJoystick()
Definition: input.cpp:135
int GetCompositionLength() const override
Definition: input.h:158
bool NativeMousePressed(int Index) const override
Definition: input.cpp:293
int m_CompositionCursor
Definition: input.h:84
IEngineGraphics * Graphics() const
Definition: input.h:65
bool MouseRelative(float *pX, float *pY) override
Definition: input.cpp:257
std::vector< CJoystick > m_vJoysticks
Definition: input.h:69
bool m_MouseFocus
Definition: input.h:80
int Update() override
Definition: input.cpp:658
void MouseModeAbsolute() override
Definition: input.cpp:270
static void ConchainJoystickGuidChanged(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
Definition: input.cpp:153
void HandleTouchDownEvent(const SDL_TouchFingerEvent &Event)
Definition: input.cpp:561
IConsole * m_pConsole
Definition: input.h:62
std::vector< CTouchFingerState > m_vTouchFingerStates
Definition: input.h:99
void AddTextEvent(const char *pText)
Definition: input.cpp:47
CInput()
Definition: input.cpp:57
int GetCompositionCursor() const override
Definition: input.h:157
Definition: config.h:9
Definition: console.h:45
Definition: console.h:18
void(* FCommandCallback)(IResult *pResult, void *pUserData)
Definition: console.h:92
Definition: graphics.h:501
Definition: input.h:171
Definition: input.h:24
const int g_MaxKeys
Definition: input.h:16
@ KEY_LALT
Definition: keys.h:217
@ KEY_LCTRL
Definition: keys.h:215
@ KEY_RALT
Definition: keys.h:221
@ KEY_LSHIFT
Definition: keys.h:216
@ KEY_RCTRL
Definition: keys.h:219
@ KEY_RGUI
Definition: keys.h:222
@ KEY_RSHIFT
Definition: keys.h:220
@ KEY_LGUI
Definition: keys.h:218
@ IO_MAX_PATH_LENGTH
Definition: types.h:43