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
10#include <cstdint>
11#include <functional>
12
13const int g_MaxKeys = 512;
14extern const char g_aaKeyStrings[g_MaxKeys][20];
15
16class IInput : public IInterface
17{
18 MACRO_INTERFACE("input")
19public:
20 enum
21 {
22 INPUT_TEXT_SIZE = 32 * UTF8_BYTE_LENGTH + 1,
23 };
24
25 class CEvent
26 {
27 public:
28 int m_Flags;
29 int m_Key;
30 uint32_t m_InputCount;
31 char m_aText[INPUT_TEXT_SIZE];
32 };
33
34 enum
35 {
36 FLAG_PRESS = 1 << 0,
37 FLAG_RELEASE = 1 << 1,
38 FLAG_TEXT = 1 << 2,
39 };
40 enum ECursorType
41 {
42 CURSOR_NONE,
43 CURSOR_MOUSE,
44 CURSOR_JOYSTICK,
45 };
46 enum
47 {
48 MAX_COMPOSITION_ARRAY_SIZE = 32, // SDL2 limitation
49
50 COMP_LENGTH_INACTIVE = -1,
51 };
52
53 // events
54 virtual void ConsumeEvents(std::function<void(const CEvent &Event)> Consumer) const = 0;
55 virtual void Clear() = 0;
56
57 /**
58 * @return Rolling average of the time in seconds between
59 * calls of the Update function.
60 */
61 virtual float GetUpdateTime() const = 0;
62
63 // keys
64 virtual bool ModifierIsPressed() const = 0;
65 virtual bool ShiftIsPressed() const = 0;
66 virtual bool AltIsPressed() const = 0;
67 virtual bool KeyIsPressed(int Key) const = 0;
68 virtual bool KeyPress(int Key, bool CheckCounter = false) const = 0;
69 const char *KeyName(int Key) const { return (Key >= 0 && Key < g_MaxKeys) ? g_aaKeyStrings[Key] : g_aaKeyStrings[0]; }
70
71 // joystick
72 class IJoystick
73 {
74 public:
75 virtual int GetIndex() const = 0;
76 virtual const char *GetName() const = 0;
77 virtual int GetNumAxes() const = 0;
78 virtual int GetNumButtons() const = 0;
79 virtual int GetNumBalls() const = 0;
80 virtual int GetNumHats() const = 0;
81 virtual float GetAxisValue(int Axis) = 0;
82 virtual void GetHatValue(int Hat, int (&HatKeys)[2]) = 0;
83 virtual bool Relative(float *pX, float *pY) = 0;
84 virtual bool Absolute(float *pX, float *pY) = 0;
85 };
86 virtual size_t NumJoysticks() const = 0;
87 virtual IJoystick *GetJoystick(size_t Index) = 0;
88 virtual IJoystick *GetActiveJoystick() = 0;
89 virtual void SetActiveJoystick(size_t Index) = 0;
90
91 // mouse
92 virtual void NativeMousePos(int *pX, int *pY) const = 0;
93 virtual bool NativeMousePressed(int Index) = 0;
94 virtual void MouseModeRelative() = 0;
95 virtual void MouseModeAbsolute() = 0;
96 virtual bool MouseRelative(float *pX, float *pY) = 0;
97
98 // clipboard
99 virtual const char *GetClipboardText() = 0;
100 virtual void SetClipboardText(const char *pText) = 0;
101
102 // text editing
103 virtual void StartTextInput() = 0;
104 virtual void StopTextInput() = 0;
105 virtual const char *GetComposition() const = 0;
106 virtual bool HasComposition() const = 0;
107 virtual int GetCompositionCursor() const = 0;
108 virtual int GetCompositionLength() const = 0;
109 virtual const char *GetCandidate(int Index) const = 0;
110 virtual int GetCandidateCount() const = 0;
111 virtual int GetCandidateSelectedIndex() const = 0;
112 virtual void SetCompositionWindowPosition(float X, float Y, float H) = 0;
113
114 virtual bool GetDropFile(char *aBuf, int Len) = 0;
115
116 ECursorType CursorRelative(float *pX, float *pY)
117 {
118 if(MouseRelative(pX, pY))
119 return CURSOR_MOUSE;
120 IJoystick *pJoystick = GetActiveJoystick();
121 if(pJoystick && pJoystick->Relative(pX, pY))
122 return CURSOR_JOYSTICK;
123 return CURSOR_NONE;
124 }
125};
126
127class IEngineInput : public IInput
128{
129 MACRO_INTERFACE("engineinput")
130public:
131 virtual void Init() = 0;
132 virtual void Shutdown() override = 0;
133 virtual int Update() = 0;
134};
135
136extern IEngineInput *CreateEngineInput();
137
138#endif
139