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 <engine/console.h>
7#include <engine/input.h>
8#include <engine/keys.h>
9
10#include <SDL_events.h>
11#include <SDL_joystick.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
26 CInput *m_pInput;
27 int m_Index;
28 char m_aName[64];
29 char m_aGUID[34];
30 SDL_JoystickID m_InstanceId;
31 int m_NumAxes;
32 int m_NumButtons;
33 int m_NumBalls;
34 int m_NumHats;
35 SDL_Joystick *m_pDelegate;
36
37 CInput *Input() { return m_pInput; }
38
39 public:
40 CJoystick(CInput *pInput, int Index, SDL_Joystick *pDelegate);
41 ~CJoystick() override = default;
42
43 int GetIndex() const override { return m_Index; }
44 const char *GetName() const override { return m_aName; }
45 const char *GetGUID() const { return m_aGUID; }
46 SDL_JoystickID GetInstanceId() const { return m_InstanceId; }
47 int GetNumAxes() const override { return m_NumAxes; }
48 int GetNumButtons() const override { return m_NumButtons; }
49 int GetNumBalls() const override { return m_NumBalls; }
50 int GetNumHats() const override { return m_NumHats; }
51 float GetAxisValue(int Axis) override;
52 void GetHatValue(int Hat, int (&aHatKeys)[2]) override;
53 bool Relative(float *pX, float *pY) override;
54 bool Absolute(float *pX, float *pY) override;
55
56 static void GetJoystickHatKeys(int Hat, int HatValue, int (&aHatKeys)[2]);
57 };
58
59private:
60 IEngineGraphics *m_pGraphics;
61 IConsole *m_pConsole;
62 IConfigManager *m_pConfigManager;
63
64 IEngineGraphics *Graphics() const { return m_pGraphics; }
65 IConsole *Console() const { return m_pConsole; }
66
67 // joystick
68 std::vector<CJoystick> m_vJoysticks;
69 CJoystick *m_pActiveJoystick = nullptr;
70 void InitJoysticks();
71 bool OpenJoystick(int JoystickIndex);
72 void CloseJoysticks();
73 void UpdateActiveJoystick();
74 static void ConchainJoystickGuidChanged(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
75 float GetJoystickDeadzone();
76
77 bool m_InputGrabbed;
78
79 bool m_MouseFocus;
80
81 // IME support
82 std::string m_CompositionString;
83 int m_CompositionCursor;
84 std::vector<std::string> m_vCandidates;
85 int m_CandidateSelectedIndex;
86
87 // events
88 std::vector<CEvent> m_vInputEvents;
89 int64_t m_LastUpdate;
90 float m_UpdateTime;
91 void AddKeyEvent(int Key, int Flags);
92 void AddTextEvent(const char *pText);
93
94 // quick access to input
95 bool m_aCurrentKeyStates[KEY_LAST];
96 bool m_aFrameKeyStates[KEY_LAST];
97 uint32_t m_InputCounter;
98 std::vector<CTouchFingerState> m_vTouchFingerStates;
99
100 void HandleJoystickAxisMotionEvent(const SDL_JoyAxisEvent &Event);
101 void HandleJoystickButtonEvent(const SDL_JoyButtonEvent &Event);
102 void HandleJoystickHatMotionEvent(const SDL_JoyHatEvent &Event);
103 void HandleJoystickAddedEvent(const SDL_JoyDeviceEvent &Event);
104 void HandleJoystickRemovedEvent(const SDL_JoyDeviceEvent &Event);
105 void HandleTouchDownEvent(const SDL_TouchFingerEvent &Event);
106 void HandleTouchUpEvent(const SDL_TouchFingerEvent &Event);
107 void HandleTouchMotionEvent(const SDL_TouchFingerEvent &Event);
108 void HandleTextEditingEvent(const char *pText, int Start, int Length);
109
110 char m_aDropFile[IO_MAX_PATH_LENGTH];
111
112 void ProcessSystemMessage(SDL_SysWMmsg *pMsg);
113
114public:
115 CInput();
116
117 void Init() override;
118 int Update() override;
119 void Shutdown() override;
120
121 void ConsumeEvents(std::function<void(const CEvent &Event)> Consumer) const override;
122 void Clear() override;
123 float GetUpdateTime() const override;
124
125 bool ModifierIsPressed() const override { return KeyIsPressed(Key: KEY_LCTRL) || KeyIsPressed(Key: KEY_RCTRL) || KeyIsPressed(Key: KEY_LGUI) || KeyIsPressed(Key: KEY_RGUI); }
126 bool ShiftIsPressed() const override { return KeyIsPressed(Key: KEY_LSHIFT) || KeyIsPressed(Key: KEY_RSHIFT); }
127 bool AltIsPressed() const override { return KeyIsPressed(Key: KEY_LALT) || KeyIsPressed(Key: KEY_RALT); }
128 bool KeyIsPressed(int Key) const override;
129 bool KeyPress(int Key) const override;
130 const char *KeyName(int Key) const override;
131 int FindKeyByName(const char *pKeyName) const override;
132
133 size_t NumJoysticks() const override { return m_vJoysticks.size(); }
134 CJoystick *GetJoystick(size_t Index) override { return &m_vJoysticks[Index]; }
135 CJoystick *GetActiveJoystick() override { return m_pActiveJoystick; }
136 void SetActiveJoystick(size_t Index) override;
137
138 bool MouseRelative(float *pX, float *pY) override;
139 void MouseModeAbsolute() override;
140 void MouseModeRelative() override;
141 vec2 NativeMousePos() const override;
142 bool NativeMousePressed(int Index) const override;
143
144 const std::vector<CTouchFingerState> &TouchFingerStates() const override;
145 void ClearTouchDeltas() override;
146
147 std::string GetClipboardText() override;
148 void SetClipboardText(const char *pText) override;
149
150 void StartTextInput() override;
151 void StopTextInput() override;
152 void EnsureScreenKeyboardShown() 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(); }
159 int GetCandidateSelectedIndex() const override { return m_CandidateSelectedIndex; }
160 void SetCompositionWindowPosition(float X, float Y, float H) override;
161
162 bool GetDropFile(char *aBuf, int Len) override;
163};
164
165#endif
166