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
99 std::vector<CTouchFingerState> m_vTouchFingerStates;
100
101 void HandleJoystickAxisMotionEvent(const SDL_JoyAxisEvent &Event);
102 void HandleJoystickButtonEvent(const SDL_JoyButtonEvent &Event);
103 void HandleJoystickHatMotionEvent(const SDL_JoyHatEvent &Event);
104 void HandleJoystickAddedEvent(const SDL_JoyDeviceEvent &Event);
105 void HandleJoystickRemovedEvent(const SDL_JoyDeviceEvent &Event);
106 void HandleTouchDownEvent(const SDL_TouchFingerEvent &Event);
107 void HandleTouchUpEvent(const SDL_TouchFingerEvent &Event);
108 void HandleTouchMotionEvent(const SDL_TouchFingerEvent &Event);
109 void HandleTextEditingEvent(const char *pText, int Start, int Length);
110
112
113 void ProcessSystemMessage(SDL_SysWMmsg *pMsg);
114
115public:
116 CInput();
117
118 void Init() override;
119 int Update() override;
120 void Shutdown() override;
121
122 void ConsumeEvents(std::function<void(const CEvent &Event)> Consumer) const override;
123 void Clear() override;
124 float GetUpdateTime() const override;
125
127 bool ShiftIsPressed() const override { return KeyIsPressed(KEY_LSHIFT) || KeyIsPressed(KEY_RSHIFT); }
128 bool AltIsPressed() const override { return KeyIsPressed(KEY_LALT) || KeyIsPressed(KEY_RALT); }
129 bool KeyIsPressed(int Key) const override;
130 bool KeyPress(int Key) const override;
131 const char *KeyName(int Key) const override;
132 int FindKeyByName(const char *pKeyName) const override;
133
134 size_t NumJoysticks() const override { return m_vJoysticks.size(); }
135 CJoystick *GetJoystick(size_t Index) override { return &m_vJoysticks[Index]; }
137 void SetActiveJoystick(size_t Index) override;
138
139 bool MouseRelative(float *pX, float *pY) override;
140 void MouseModeAbsolute() override;
141 void MouseModeRelative() override;
142 vec2 NativeMousePos() const override;
143 bool NativeMousePressed(int Index) const override;
144
145 const std::vector<CTouchFingerState> &TouchFingerStates() const override;
146 void ClearTouchDeltas() override;
147
148 std::string GetClipboardText() override;
149 void SetClipboardText(const char *pText) override;
150
151 void StartTextInput() override;
152 void StopTextInput() override;
153 const char *GetComposition() const override { return m_CompositionString.c_str(); }
154 bool HasComposition() const override { return !m_CompositionString.empty(); }
155 int GetCompositionCursor() const override { return m_CompositionCursor; }
156 int GetCompositionLength() const override { return m_CompositionString.length(); }
157 const char *GetCandidate(int Index) const override { return m_vCandidates[Index].c_str(); }
158 int GetCandidateCount() const override { return m_vCandidates.size(); }
160 void SetCompositionWindowPosition(float X, float Y, float H) override;
161
162 bool GetDropFile(char *aBuf, int Len) override;
163};
164
165#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:221
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:248
bool Absolute(float *pX, float *pY) override
Definition: input.cpp:266
const char * GetGUID() const
Definition: input.h:46
static void GetJoystickHatKeys(int Hat, int HatValue, int(&HatKeys)[2])
Definition: input.cpp:226
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:243
int m_NumAxes
Definition: input.h:31
Definition: input.h:20
void HandleTouchMotionEvent(const SDL_TouchFingerEvent &Event)
Definition: input.cpp:561
void SetCompositionWindowPosition(float X, float Y, float H) override
Definition: input.cpp:593
void ProcessSystemMessage(SDL_SysWMmsg *pMsg)
Definition: input.cpp:857
std::vector< std::string > m_vCandidates
Definition: input.h:85
void HandleJoystickAxisMotionEvent(const SDL_JoyAxisEvent &Event)
Definition: input.cpp:429
std::vector< CEvent > m_vInputEvents
Definition: input.h:89
void StartTextInput() override
Definition: input.cpp:350
char m_aDropFile[IO_MAX_PATH_LENGTH]
Definition: input.h:111
IConsole * Console() const
Definition: input.h:66
const char * KeyName(int Key) const override
Definition: input.cpp:403
void HandleJoystickRemovedEvent(const SDL_JoyDeviceEvent &Event)
Definition: input.cpp:522
CJoystick * GetJoystick(size_t Index) override
Definition: input.h:135
bool HasComposition() const override
Definition: input.h:154
void AddKeyEvent(int Key, int Flags)
Definition: input.cpp:49
void CloseJoysticks()
Definition: input.cpp:206
bool KeyIsPressed(int Key) const override
Definition: input.cpp:391
bool OpenJoystick(int JoystickIndex)
Definition: input.cpp:140
void ConsumeEvents(std::function< void(const CEvent &Event)> Consumer) const override
Definition: input.cpp:367
uint32_t m_InputCounter
Definition: input.h:98
bool KeyPress(int Key) const override
Definition: input.cpp:397
bool ShiftIsPressed() const override
Definition: input.h:127
void HandleJoystickButtonEvent(const SDL_JoyButtonEvent &Event)
Definition: input.cpp:462
bool m_aFrameKeyStates[KEY_LAST]
Definition: input.h:97
CJoystick * GetActiveJoystick() override
Definition: input.h:136
const char * GetComposition() const override
Definition: input.h:153
bool m_aCurrentKeyStates[KEY_LAST]
Definition: input.h:96
int m_CandidateSelectedIndex
Definition: input.h:86
void HandleTextEditingEvent(const char *pText, int Start, int Length)
Definition: input.cpp:573
void InitJoysticks()
Definition: input.cpp:120
size_t NumJoysticks() const override
Definition: input.h:134
int GetCandidateSelectedIndex() const override
Definition: input.h:159
bool AltIsPressed() const override
Definition: input.h:128
bool GetDropFile(char *aBuf, int Len) override
Definition: input.cpp:904
CJoystick * m_pActiveJoystick
Definition: input.h:70
void HandleJoystickHatMotionEvent(const SDL_JoyHatEvent &Event)
Definition: input.cpp:484
void StopTextInput() override
Definition: input.cpp:357
void HandleJoystickAddedEvent(const SDL_JoyDeviceEvent &Event)
Definition: input.cpp:514
const char * GetCandidate(int Index) const override
Definition: input.h:157
int64_t m_LastUpdate
Definition: input.h:90
void MouseModeRelative() override
Definition: input.cpp:302
const std::vector< CTouchFingerState > & TouchFingerStates() const override
Definition: input.cpp:324
void SetActiveJoystick(size_t Index) override
Definition: input.cpp:215
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:345
int FindKeyByName(const char *pKeyName) const override
Definition: input.cpp:409
bool ModifierIsPressed() const override
Definition: input.h:126
vec2 NativeMousePos() const override
Definition: input.cpp:311
float GetJoystickDeadzone()
Definition: input.cpp:187
float GetUpdateTime() const override
Definition: input.cpp:386
int GetCandidateCount() const override
Definition: input.h:158
void Init() override
Definition: input.cpp:102
IConfigManager * m_pConfigManager
Definition: input.h:63
std::string GetClipboardText() override
Definition: input.cpp:337
void Clear() override
Definition: input.cpp:379
void Shutdown() override
Definition: input.cpp:115
void HandleTouchUpEvent(const SDL_TouchFingerEvent &Event)
Definition: input.cpp:550
IEngineGraphics * m_pGraphics
Definition: input.h:61
void UpdateActiveJoystick()
Definition: input.cpp:160
int GetCompositionLength() const override
Definition: input.h:156
bool NativeMousePressed(int Index) const override
Definition: input.cpp:318
int m_CompositionCursor
Definition: input.h:84
IEngineGraphics * Graphics() const
Definition: input.h:65
bool MouseRelative(float *pX, float *pY) override
Definition: input.cpp:282
std::vector< CJoystick > m_vJoysticks
Definition: input.h:69
bool m_MouseFocus
Definition: input.h:80
void ClearTouchDeltas() override
Definition: input.cpp:329
int Update() override
Definition: input.cpp:688
void MouseModeAbsolute() override
Definition: input.cpp:295
static void ConchainJoystickGuidChanged(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData)
Definition: input.cpp:178
void HandleTouchDownEvent(const SDL_TouchFingerEvent &Event)
Definition: input.cpp:539
IConsole * m_pConsole
Definition: input.h:62
std::vector< CTouchFingerState > m_vTouchFingerStates
Definition: input.h:99
void AddTextEvent(const char *pText)
Definition: input.cpp:72
CInput()
Definition: input.cpp:82
int GetCompositionCursor() const override
Definition: input.h:155
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:487
Definition: input.h:204
Definition: input.h:22
@ KEY_LALT
Definition: keys.h:217
@ KEY_LCTRL
Definition: keys.h:215
@ KEY_LAST
Definition: keys.h:319
@ 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