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_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 <cstdint>
12#include <functional>
13#include <string>
14#include <vector>
15
16const int g_MaxKeys = 512;
17extern const char g_aaKeyStrings[g_MaxKeys][20];
18
19class IInput : public IInterface
20{
22public:
23 class CEvent
24 {
25 public:
27 int m_Key;
28 uint32_t m_InputCount;
29 char m_aText[32]; // SDL_TEXTINPUTEVENT_TEXT_SIZE
30 };
31
32 enum
33 {
34 FLAG_PRESS = 1 << 0,
35 FLAG_RELEASE = 1 << 1,
36 FLAG_TEXT = 1 << 2,
37 };
39 {
43 };
44
45 // events
46 virtual void ConsumeEvents(std::function<void(const CEvent &Event)> Consumer) const = 0;
47 virtual void Clear() = 0;
48
53 virtual float GetUpdateTime() const = 0;
54
55 // keys
56 virtual bool ModifierIsPressed() const = 0;
57 virtual bool ShiftIsPressed() const = 0;
58 virtual bool AltIsPressed() const = 0;
59 virtual bool KeyIsPressed(int Key) const = 0;
60 virtual bool KeyPress(int Key, bool CheckCounter = false) const = 0;
61 const char *KeyName(int Key) const { return (Key >= 0 && Key < g_MaxKeys) ? g_aaKeyStrings[Key] : g_aaKeyStrings[0]; }
62 virtual int FindKeyByName(const char *pKeyName) const = 0;
63
64 // joystick
66 {
67 public:
68 virtual int GetIndex() const = 0;
69 virtual const char *GetName() const = 0;
70 virtual int GetNumAxes() const = 0;
71 virtual int GetNumButtons() const = 0;
72 virtual int GetNumBalls() const = 0;
73 virtual int GetNumHats() const = 0;
74 virtual float GetAxisValue(int Axis) = 0;
75 virtual void GetHatValue(int Hat, int (&HatKeys)[2]) = 0;
76 virtual bool Relative(float *pX, float *pY) = 0;
77 virtual bool Absolute(float *pX, float *pY) = 0;
78 };
79 virtual size_t NumJoysticks() const = 0;
80 virtual IJoystick *GetJoystick(size_t Index) = 0;
82 virtual void SetActiveJoystick(size_t Index) = 0;
83
84 // mouse
85 virtual vec2 NativeMousePos() const = 0;
86 virtual bool NativeMousePressed(int Index) const = 0;
87 virtual void MouseModeRelative() = 0;
88 virtual void MouseModeAbsolute() = 0;
89 virtual bool MouseRelative(float *pX, float *pY) = 0;
90
91 // touch
99 {
100 friend class CInput;
101
102 int64_t m_DeviceId;
103 int64_t m_FingerId;
104
105 public:
106 bool operator==(const CTouchFinger &Other) const { return m_DeviceId == Other.m_DeviceId && m_FingerId == Other.m_FingerId; }
107 bool operator!=(const CTouchFinger &Other) const { return !(*this == Other); }
108 };
113 {
114 public:
131 };
139 virtual const std::vector<CTouchFingerState> &TouchFingerStates() const = 0;
140
141 // clipboard
142 virtual std::string GetClipboardText() = 0;
143 virtual void SetClipboardText(const char *pText) = 0;
144
145 // text editing
146 virtual void StartTextInput() = 0;
147 virtual void StopTextInput() = 0;
148 virtual const char *GetComposition() const = 0;
149 virtual bool HasComposition() const = 0;
150 virtual int GetCompositionCursor() const = 0;
151 virtual int GetCompositionLength() const = 0;
152 virtual const char *GetCandidate(int Index) const = 0;
153 virtual int GetCandidateCount() const = 0;
154 virtual int GetCandidateSelectedIndex() const = 0;
155 virtual void SetCompositionWindowPosition(float X, float Y, float H) = 0;
156
157 virtual bool GetDropFile(char *aBuf, int Len) = 0;
158
159 ECursorType CursorRelative(float *pX, float *pY)
160 {
161 if(MouseRelative(pX, pY))
162 return CURSOR_MOUSE;
163 IJoystick *pJoystick = GetActiveJoystick();
164 if(pJoystick && pJoystick->Relative(pX, pY))
165 return CURSOR_JOYSTICK;
166 return CURSOR_NONE;
167 }
168};
169
170class IEngineInput : public IInput
171{
172 MACRO_INTERFACE("engineinput")
173public:
174 virtual void Init() = 0;
175 virtual void Shutdown() override = 0;
176 virtual int Update() = 0;
177};
178
180
181#endif
Definition: input.h:20
Definition: input.h:171
virtual void Shutdown() override=0
virtual int Update()=0
virtual void Init()=0
Definition: input.h:24
int m_Key
Definition: input.h:27
int m_Flags
Definition: input.h:26
uint32_t m_InputCount
Definition: input.h:28
Definition: input.h:113
vec2 m_Delta
Definition: input.h:130
CTouchFinger m_Finger
Definition: input.h:118
vec2 m_Position
Definition: input.h:123
Definition: input.h:99
int64_t m_DeviceId
Definition: input.h:102
bool operator==(const CTouchFinger &Other) const
Definition: input.h:106
int64_t m_FingerId
Definition: input.h:103
bool operator!=(const CTouchFinger &Other) const
Definition: input.h:107
Definition: input.h:66
virtual int GetNumBalls() const =0
virtual const char * GetName() const =0
virtual int GetNumButtons() const =0
virtual bool Absolute(float *pX, float *pY)=0
virtual int GetNumAxes() const =0
virtual bool Relative(float *pX, float *pY)=0
virtual int GetIndex() const =0
virtual int GetNumHats() const =0
virtual void GetHatValue(int Hat, int(&HatKeys)[2])=0
virtual float GetAxisValue(int Axis)=0
Definition: input.h:20
virtual bool KeyPress(int Key, bool CheckCounter=false) const =0
ECursorType
Definition: input.h:39
@ CURSOR_MOUSE
Definition: input.h:41
@ CURSOR_NONE
Definition: input.h:40
@ CURSOR_JOYSTICK
Definition: input.h:42
virtual int GetCompositionLength() const =0
virtual void MouseModeAbsolute()=0
virtual void StopTextInput()=0
virtual int FindKeyByName(const char *pKeyName) const =0
virtual int GetCandidateSelectedIndex() const =0
virtual IJoystick * GetJoystick(size_t Index)=0
virtual int GetCompositionCursor() const =0
virtual int GetCandidateCount() const =0
virtual const char * GetCandidate(int Index) const =0
virtual std::string GetClipboardText()=0
virtual bool NativeMousePressed(int Index) const =0
virtual vec2 NativeMousePos() const =0
virtual bool AltIsPressed() const =0
virtual bool ShiftIsPressed() const =0
virtual void SetActiveJoystick(size_t Index)=0
virtual void SetClipboardText(const char *pText)=0
@ FLAG_TEXT
Definition: input.h:36
@ FLAG_RELEASE
Definition: input.h:35
@ FLAG_PRESS
Definition: input.h:34
virtual size_t NumJoysticks() const =0
virtual void MouseModeRelative()=0
virtual void SetCompositionWindowPosition(float X, float Y, float H)=0
virtual void StartTextInput()=0
virtual void ConsumeEvents(std::function< void(const CEvent &Event)> Consumer) const =0
virtual const std::vector< CTouchFingerState > & TouchFingerStates() const =0
ECursorType CursorRelative(float *pX, float *pY)
Definition: input.h:159
virtual bool KeyIsPressed(int Key) const =0
virtual void Clear()=0
virtual bool MouseRelative(float *pX, float *pY)=0
virtual bool ModifierIsPressed() const =0
virtual const char * GetComposition() const =0
virtual IJoystick * GetActiveJoystick()=0
const char * KeyName(int Key) const
Definition: input.h:61
virtual bool HasComposition() const =0
virtual bool GetDropFile(char *aBuf, int Len)=0
virtual float GetUpdateTime() const =0
Definition: kernel.h:10
IEngineInput * CreateEngineInput()
Definition: input.cpp:928
const char g_aaKeyStrings[g_MaxKeys][20]
Definition: keynames.h:7
const int g_MaxKeys
Definition: input.h:16
#define MACRO_INTERFACE(Name)
Definition: kernel.h:25