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_INPUT_H
4#define ENGINE_INPUT_H
5
6#include "kernel.h"
7
8#include <base/types.h>
9#include <base/vmath.h>
10
11#include <chrono>
12#include <cstdint>
13#include <functional>
14#include <string>
15#include <vector>
16
17class IInput : public IInterface
18{
19 MACRO_INTERFACE("input")
20public:
21 class CEvent
22 {
23 public:
24 int m_Flags;
25 int m_Key;
26 uint32_t m_InputCount;
27 char m_aText[32]; // SDL_TEXTINPUTEVENT_TEXT_SIZE
28 };
29
30 enum
31 {
32 FLAG_PRESS = 1 << 0,
33 FLAG_RELEASE = 1 << 1,
34 FLAG_TEXT = 1 << 2,
35 /**
36 * Combined with @link FLAG_PRESS @endlink for key press events that
37 * are being repeated when a key is held down.
38 */
39 FLAG_REPEAT = 1 << 3,
40 };
41 enum ECursorType
42 {
43 CURSOR_NONE,
44 CURSOR_MOUSE,
45 CURSOR_JOYSTICK,
46 };
47
48 // events
49 virtual void ConsumeEvents(std::function<void(const CEvent &Event)> Consumer) const = 0;
50 /**
51 * Clears the events and @link KeyPress @endlink state for this frame. Must be called at the end of each frame.
52 */
53 virtual void Clear() = 0;
54
55 /**
56 * @return Rolling average of the time in seconds between
57 * calls of the Update function.
58 */
59 virtual float GetUpdateTime() const = 0;
60
61 // keys
62 virtual bool ModifierIsPressed() const = 0;
63 virtual bool ShiftIsPressed() const = 0;
64 virtual bool AltIsPressed() const = 0;
65 /**
66 * Returns whether the given key is currently pressed down. This directly represents the state of pressed keys
67 * based on all handled input events.
68 *
69 * This function should be used to trigger behavior continuously while a specific key is held down, e.g. for
70 * showing a list of all keys that are currently being pressed.
71 *
72 * @param Key The key code (see `keys.h`).
73 *
74 * @return `true` if key is currently pressed down, `false` otherwise.
75 */
76 virtual bool KeyIsPressed(int Key) const = 0;
77 /**
78 * Returns whether the given key was pressed down during input updates for current frame. This state is
79 * cleared at the end of each frame by calling the @link Clear @endlink function.
80 *
81 * This function should be used to trigger behavior only once per key press event per frame, e.g. for menu
82 * hotkeys that should activate behavior once per key press.
83 *
84 * @param Key The key code (see `keys.h`).
85 *
86 * @return `true` if key was pressed down during input updates for the current frame, `false` otherwise.
87 */
88 virtual bool KeyPress(int Key) const = 0;
89 virtual int FindKeyByName(const char *pKeyName) const = 0;
90
91 // joystick
92 class IJoystick
93 {
94 public:
95 virtual ~IJoystick() = default;
96 virtual int GetIndex() const = 0;
97 virtual const char *GetName() const = 0;
98 virtual int GetNumAxes() const = 0;
99 virtual int GetNumButtons() const = 0;
100 virtual int GetNumBalls() const = 0;
101 virtual int GetNumHats() const = 0;
102 virtual float GetAxisValue(int Axis) = 0;
103 virtual void GetHatValue(int Hat, int (&aHatKeys)[2]) = 0;
104 virtual bool Relative(float *pX, float *pY) = 0;
105 virtual bool Absolute(float *pX, float *pY) = 0;
106 };
107 virtual size_t NumJoysticks() const = 0;
108 virtual IJoystick *GetJoystick(size_t Index) = 0;
109 virtual IJoystick *GetActiveJoystick() = 0;
110 virtual void SetActiveJoystick(size_t Index) = 0;
111
112 // mouse
113 virtual vec2 NativeMousePos() const = 0;
114 virtual bool NativeMousePressed(int Index) const = 0;
115 virtual void MouseModeRelative() = 0;
116 virtual void MouseModeAbsolute() = 0;
117 virtual bool MouseRelative(float *pX, float *pY) = 0;
118
119 // touch
120 /**
121 * Represents a unique finger for a current touch event. If there are multiple touch input devices, they
122 * are handled transparently like different fingers. The concrete values of the member variables of this
123 * class are arbitrary based on the touch device driver and should only be used to uniquely identify touch
124 * fingers. Note that once a finger has been released, the same finger value may also be reused again.
125 */
126 class CTouchFinger
127 {
128 friend class CInput;
129
130 int64_t m_DeviceId;
131 int64_t m_FingerId;
132
133 public:
134 bool operator==(const CTouchFinger &Other) const { return m_DeviceId == Other.m_DeviceId && m_FingerId == Other.m_FingerId; }
135 bool operator!=(const CTouchFinger &Other) const { return !(*this == Other); }
136 };
137 /**
138 * Represents the state of a particular touch finger currently being pressed down on a touch device.
139 */
140 class CTouchFingerState
141 {
142 public:
143 /**
144 * The unique finger which this state is associated with.
145 */
146 CTouchFinger m_Finger;
147 /**
148 * The current position of the finger. The x- and y-components of the position are normalized to the
149 * range `0.0f`-`1.0f` representing the absolute position of the finger on the current touch device.
150 */
151 vec2 m_Position;
152 /**
153 * The current delta of the finger. The x- and y-components of the delta are normalized to the
154 * range `-1.0f`-`1.0f` representing the absolute delta of the finger on the current touch device.
155 *
156 * @remark This is reset to zero at the end of each frame.
157 */
158 vec2 m_Delta;
159 /**
160 * The time when this finger was first pressed down.
161 */
162 std::chrono::nanoseconds m_PressTime;
163 };
164 /**
165 * Returns a vector of the states of all touch fingers currently being pressed down on touch devices.
166 * Note that this only contains fingers which are pressed down, i.e. released fingers are never stored.
167 * The order of the fingers in this vector is based on the order in which the fingers where pressed.
168 *
169 * @return vector of all touch finger states
170 */
171 virtual const std::vector<CTouchFingerState> &TouchFingerStates() const = 0;
172 /**
173 * Must be called after the touch finger states have been used during the client update to ensure that
174 * touch deltas are only accumulated until the next update. If the touch states are only used during
175 * rendering, i.e. for user interfaces, then this is called automatically by calling @link Clear @endlink.
176 */
177 virtual void ClearTouchDeltas() = 0;
178
179 // clipboard
180 virtual std::string GetClipboardText() = 0;
181 virtual void SetClipboardText(const char *pText) = 0;
182
183 // text editing
184 virtual void StartTextInput() = 0;
185 virtual void StopTextInput() = 0;
186 virtual void EnsureScreenKeyboardShown() = 0;
187 virtual const char *GetComposition() const = 0;
188 virtual bool HasComposition() const = 0;
189 virtual int GetCompositionCursor() const = 0;
190 virtual int GetCompositionLength() const = 0;
191 virtual const char *GetCandidate(int Index) const = 0;
192 virtual int GetCandidateCount() const = 0;
193 virtual int GetCandidateSelectedIndex() const = 0;
194 virtual void SetCompositionWindowPosition(float X, float Y, float H) = 0;
195
196 virtual bool GetDropFile(char *aBuf, int Len) = 0;
197
198 ECursorType CursorRelative(float *pX, float *pY)
199 {
200 if(MouseRelative(pX, pY))
201 return CURSOR_MOUSE;
202 IJoystick *pJoystick = GetActiveJoystick();
203 if(pJoystick && pJoystick->Relative(pX, pY))
204 return CURSOR_JOYSTICK;
205 return CURSOR_NONE;
206 }
207};
208
209class IEngineInput : public IInput
210{
211 MACRO_INTERFACE("engineinput")
212public:
213 virtual void Init() = 0;
214 void Shutdown() override = 0;
215 virtual int Update() = 0;
216};
217
218extern IEngineInput *CreateEngineInput();
219
220#endif
221