DDraceNetwork Docs
lineinput.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 GAME_CLIENT_LINEINPUT_H
4#define GAME_CLIENT_LINEINPUT_H
5
6#include <base/vmath.h>
7
8#include <engine/client.h>
9#include <engine/graphics.h>
10#include <engine/input.h>
11#include <engine/textrender.h>
12
13#include <game/client/ui_rect.h>
14
16{
17 NONE = 0,
18 UI,
19 CHAT,
20 CONSOLE,
21};
22
23// line input helper
25{
26public:
28 {
29 bool m_Selecting = false;
30 vec2 m_PressMouse = vec2(0.0f, 0.0f);
31 vec2 m_ReleaseMouse = vec2(0.0f, 0.0f);
32 vec2 m_Offset = vec2(0.0f, 0.0f);
33 };
34
35 typedef std::function<void(const char *pLine)> FClipboardLineCallback;
36 typedef std::function<const char *(char *pCurrentText, size_t NumChars)> FDisplayTextCallback;
37 typedef std::function<bool()> FCalculateOffsetCallback;
38
39private:
44
45 static IClient *Client() { return ms_pClient; }
46 static IGraphics *Graphics() { return ms_pGraphics; }
47 static IInput *Input() { return ms_pInput; }
49
52
55
56 static char ms_aStars[128];
57
58 char *m_pStr = nullptr; // explicitly set to nullptr outside of constructor, so SetBuffer works in this case
59 size_t m_MaxSize;
60 size_t m_MaxChars;
61 size_t m_Len;
62 size_t m_NumChars;
63
67
73
75 const char *m_pEmptyText;
82
84
85 void UpdateStrData();
87 {
89 REWIND
90 };
91 static void MoveCursor(EMoveDirection Direction, bool MoveWord, const char *pStr, size_t MaxSize, size_t *pCursorPos);
92 static void SetCompositionWindowPosition(vec2 Anchor, float LineHeight);
93
94 void OnActivate();
95 void OnDeactivate();
96
97public:
98 static void Init(IClient *pClient, IGraphics *pGraphics, IInput *pInput, ITextRender *pTextRender)
99 {
100 ms_pClient = pClient;
101 ms_pGraphics = pGraphics;
102 ms_pInput = pInput;
103 ms_pTextRender = pTextRender;
104 }
105 static void RenderCandidates();
106
108
110 {
111 SetBuffer(nullptr, 0, 0);
112 }
113
114 CLineInput(char *pStr, size_t MaxSize)
115 {
116 SetBuffer(pStr, MaxSize);
117 }
118
119 CLineInput(char *pStr, size_t MaxSize, size_t MaxChars)
120 {
121 SetBuffer(pStr, MaxSize, MaxChars);
122 }
123
124 void SetBuffer(char *pStr, size_t MaxSize) { SetBuffer(pStr, MaxSize, MaxSize); }
125 void SetBuffer(char *pStr, size_t MaxSize, size_t MaxChars);
126
127 void Clear();
128 void Set(const char *pString);
129 void SetRange(const char *pString, size_t Begin, size_t End);
130 void Insert(const char *pString, size_t Begin);
131 void Append(const char *pString);
132
133 const char *GetString() const { return m_pStr; }
134 const char *GetDisplayedString();
135 size_t GetMaxSize() const { return m_MaxSize; }
136 size_t GetMaxChars() const { return m_MaxChars; }
137 size_t GetLength() const { return m_Len; }
138 size_t GetNumChars() const { return m_NumChars; }
139 bool IsEmpty() const { return GetLength() == 0; }
140
141 size_t GetCursorOffset() const { return m_CursorPos; }
142 void SetCursorOffset(size_t Offset);
143 size_t GetSelectionStart() const { return m_SelectionStart; }
144 size_t GetSelectionEnd() const { return m_SelectionEnd; }
146 bool HasSelection() const { return GetSelectionLength() > 0; }
147 void SetSelection(size_t Start, size_t End);
150 {
153 }
154
155 size_t OffsetFromActualToDisplay(size_t ActualOffset);
156 size_t OffsetFromDisplayToActual(size_t DisplayOffset);
157
158 // used either for vertical or horizontal scrolling
159 float GetScrollOffset() const { return m_ScrollOffset; }
160 void SetScrollOffset(float ScrollOffset) { m_ScrollOffset = ScrollOffset; }
162 void SetScrollOffsetChange(float ScrollOffsetChange) { m_ScrollOffsetChange = ScrollOffsetChange; }
163
164 vec2 GetCaretPosition() const { return m_CaretPosition; } // only updated while the input is active
165
166 bool IsHidden() const { return m_Hidden; }
167 void SetHidden(bool Hidden) { m_Hidden = Hidden; }
168
169 const char *GetEmptyText() const { return m_pEmptyText; }
170 void SetEmptyText(const char *pText) { m_pEmptyText = pText; }
171
172 void SetClipboardLineCallback(FClipboardLineCallback pfnClipboardLineCallback) { m_pfnClipboardLineCallback = pfnClipboardLineCallback; }
173 void SetDisplayTextCallback(FDisplayTextCallback pfnDisplayTextCallback) { m_pfnDisplayTextCallback = pfnDisplayTextCallback; }
174 void SetCalculateOffsetCallback(FCalculateOffsetCallback pfnCalculateOffsetCallback) { m_pfnCalculateOffsetCallback = pfnCalculateOffsetCallback; }
175
176 bool ProcessInput(const IInput::CEvent &Event);
178 {
179 const bool Changed = m_WasChanged;
180 m_WasChanged = false;
181 return Changed;
182 }
184 {
185 const bool Changed = m_WasCursorChanged;
186 m_WasCursorChanged = false;
187 return Changed;
188 }
189
190 STextBoundingBox Render(const CUIRect *pRect, float FontSize, int Align, bool Changed, float LineWidth, float LineSpacing, const std::vector<STextColorSplit> &vColorSplits = {});
192
193 const void *GetClearButtonId() const { return &m_ClearButtonId; }
194
195 bool IsActive() const { return GetActiveInput() == this; }
196 void Activate(EInputPriority Priority);
197 void Deactivate() const;
198};
199
200template<size_t MaxSize, size_t MaxChars = MaxSize>
202{
203 char m_aBuffer[MaxSize];
204
205public:
207 CLineInput()
208 {
209 m_aBuffer[0] = '\0';
210 SetBuffer(m_aBuffer, MaxSize, MaxChars);
211 }
212};
213
215{
216public:
219 {
220 }
221
222 CLineInputNumber(int Number) :
224 {
225 SetInteger(Number);
226 }
227
228 CLineInputNumber(float Number) :
230 {
231 SetFloat(Number);
232 }
233
234 void SetInteger(int Number, int Base = 10, int HexPrefix = 6);
235 int GetInteger(int Base = 10) const;
236
237 void SetInteger64(int64_t Number, int Base = 10, int HexPrefix = 6);
238 int64_t GetInteger64(int Base = 10) const;
239
240 void SetFloat(float Number);
241 float GetFloat() const;
242};
243
244#endif
Definition: lineinput.h:202
CLineInputBuffered()
Definition: lineinput.h:206
char m_aBuffer[MaxSize]
Definition: lineinput.h:203
Definition: lineinput.h:215
CLineInputNumber(int Number)
Definition: lineinput.h:222
void SetFloat(float Number)
Definition: lineinput.cpp:717
int64_t GetInteger64(int Base=10) const
Definition: lineinput.cpp:712
CLineInputNumber(float Number)
Definition: lineinput.h:228
int GetInteger(int Base=10) const
Definition: lineinput.cpp:688
CLineInputNumber()
Definition: lineinput.h:217
void SetInteger(int Number, int Base=10, int HexPrefix=6)
Definition: lineinput.cpp:669
void SetInteger64(int64_t Number, int Base=10, int HexPrefix=6)
Definition: lineinput.cpp:693
float GetFloat() const
Definition: lineinput.cpp:725
Definition: lineinput.h:25
std::function< const char *(char *pCurrentText, size_t NumChars)> FDisplayTextCallback
Definition: lineinput.h:36
static ITextRender * ms_pTextRender
Definition: lineinput.h:43
void Activate(EInputPriority Priority)
Definition: lineinput.cpp:636
char * m_pStr
Definition: lineinput.h:58
bool m_WasRendered
Definition: lineinput.h:81
void Set(const char *pString)
Definition: lineinput.cpp:51
size_t GetSelectionEnd() const
Definition: lineinput.h:144
FClipboardLineCallback m_pfnClipboardLineCallback
Definition: lineinput.h:76
bool m_WasChanged
Definition: lineinput.h:79
const char * GetDisplayedString()
Definition: lineinput.cpp:114
static IInput * Input()
Definition: lineinput.h:47
CLineInput(char *pStr, size_t MaxSize, size_t MaxChars)
Definition: lineinput.h:119
bool ProcessInput(const IInput::CEvent &Event)
Definition: lineinput.cpp:189
bool IsActive() const
Definition: lineinput.h:195
void SetHidden(bool Hidden)
Definition: lineinput.h:167
size_t GetSelectionLength() const
Definition: lineinput.h:145
static ITextRender * TextRender()
Definition: lineinput.h:48
size_t GetSelectionStart() const
Definition: lineinput.h:143
size_t m_MaxSize
Definition: lineinput.h:59
size_t m_CursorPos
Definition: lineinput.h:64
size_t m_MaxChars
Definition: lineinput.h:60
size_t m_NumChars
Definition: lineinput.h:62
static IClient * Client()
Definition: lineinput.h:45
vec2 m_CaretPosition
Definition: lineinput.h:70
void SetCalculateOffsetCallback(FCalculateOffsetCallback pfnCalculateOffsetCallback)
Definition: lineinput.h:174
static CLineInput * ms_pActiveInput
Definition: lineinput.h:50
size_t GetCursorOffset() const
Definition: lineinput.h:141
void SetEmptyText(const char *pText)
Definition: lineinput.h:170
size_t OffsetFromActualToDisplay(size_t ActualOffset)
Definition: lineinput.cpp:175
float GetScrollOffset() const
Definition: lineinput.h:159
void Insert(const char *pString, size_t Begin)
Definition: lineinput.cpp:95
size_t m_SelectionStart
Definition: lineinput.h:65
SMouseSelection m_MouseSelection
Definition: lineinput.h:71
const void * GetClearButtonId() const
Definition: lineinput.h:193
bool WasCursorChanged()
Definition: lineinput.h:183
static CLineInput * GetActiveInput()
Definition: lineinput.h:107
void SetScrollOffsetChange(float ScrollOffsetChange)
Definition: lineinput.h:162
void Clear()
Definition: lineinput.cpp:45
static EInputPriority ms_ActiveInputPriority
Definition: lineinput.h:51
void Deactivate() const
Definition: lineinput.cpp:649
static IGraphics * ms_pGraphics
Definition: lineinput.h:41
bool m_Hidden
Definition: lineinput.h:74
bool IsEmpty() const
Definition: lineinput.h:139
EMoveDirection
Definition: lineinput.h:87
@ FORWARD
Definition: lineinput.h:88
@ REWIND
Definition: lineinput.h:89
static IGraphics * Graphics()
Definition: lineinput.h:46
CLineInput()
Definition: lineinput.h:109
void SetDisplayTextCallback(FDisplayTextCallback pfnDisplayTextCallback)
Definition: lineinput.h:173
static float ms_CompositionLineHeight
Definition: lineinput.h:54
void UpdateStrData()
Definition: lineinput.cpp:105
void OnActivate()
Definition: lineinput.cpp:658
void Append(const char *pString)
Definition: lineinput.cpp:100
void SetCursorOffset(size_t Offset)
Definition: lineinput.cpp:159
size_t GetMaxChars() const
Definition: lineinput.h:136
bool WasChanged()
Definition: lineinput.h:177
void SetScrollOffset(float ScrollOffset)
Definition: lineinput.h:160
size_t GetNumChars() const
Definition: lineinput.h:138
STextBoundingBox Render(const CUIRect *pRect, float FontSize, int Align, bool Changed, float LineWidth, float LineSpacing, const std::vector< STextColorSplit > &vColorSplits={})
Definition: lineinput.cpp:403
static void SetCompositionWindowPosition(vec2 Anchor, float LineHeight)
Definition: lineinput.cpp:623
void SetClipboardLineCallback(FClipboardLineCallback pfnClipboardLineCallback)
Definition: lineinput.h:172
static void RenderCandidates()
Definition: lineinput.cpp:536
std::function< bool()> FCalculateOffsetCallback
Definition: lineinput.h:37
size_t m_SelectionEnd
Definition: lineinput.h:66
bool m_WasCursorChanged
Definition: lineinput.h:80
const char * GetString() const
Definition: lineinput.h:133
float m_ScrollOffsetChange
Definition: lineinput.h:69
size_t GetLength() const
Definition: lineinput.h:137
const char * m_pEmptyText
Definition: lineinput.h:75
const char * GetEmptyText() const
Definition: lineinput.h:169
static char ms_aStars[128]
Definition: lineinput.h:56
bool HasSelection() const
Definition: lineinput.h:146
void SetRange(const char *pString, size_t Begin, size_t End)
Definition: lineinput.cpp:58
void SetSelection(size_t Start, size_t End)
Definition: lineinput.cpp:165
SMouseSelection * GetMouseSelection()
Definition: lineinput.h:191
static void Init(IClient *pClient, IGraphics *pGraphics, IInput *pInput, ITextRender *pTextRender)
Definition: lineinput.h:98
bool IsHidden() const
Definition: lineinput.h:166
static void MoveCursor(EMoveDirection Direction, bool MoveWord, const char *pStr, size_t MaxSize, size_t *pCursorPos)
Definition: lineinput.cpp:129
void OnDeactivate()
Definition: lineinput.cpp:663
size_t m_LastCompositionCursorPos
Definition: lineinput.h:72
static vec2 ms_CompositionWindowPosition
Definition: lineinput.h:53
CLineInput(char *pStr, size_t MaxSize)
Definition: lineinput.h:114
float m_ScrollOffset
Definition: lineinput.h:68
size_t OffsetFromDisplayToActual(size_t DisplayOffset)
Definition: lineinput.cpp:182
static IInput * ms_pInput
Definition: lineinput.h:42
static IClient * ms_pClient
Definition: lineinput.h:40
char m_ClearButtonId
Definition: lineinput.h:83
void SelectAll()
Definition: lineinput.h:149
std::function< void(const char *pLine)> FClipboardLineCallback
Definition: lineinput.h:35
size_t m_Len
Definition: lineinput.h:61
float GetScrollOffsetChange() const
Definition: lineinput.h:161
void SetBuffer(char *pStr, size_t MaxSize)
Definition: lineinput.h:124
FDisplayTextCallback m_pfnDisplayTextCallback
Definition: lineinput.h:77
vec2 GetCaretPosition() const
Definition: lineinput.h:164
FCalculateOffsetCallback m_pfnCalculateOffsetCallback
Definition: lineinput.h:78
size_t GetMaxSize() const
Definition: lineinput.h:135
void SelectNothing()
Definition: lineinput.h:148
Definition: ui_rect.h:11
Definition: client.h:36
Definition: graphics.h:183
Definition: input.h:24
Definition: input.h:20
Definition: textrender.h:312
EInputPriority
Definition: lineinput.h:16
Definition: lineinput.h:28
vec2 m_PressMouse
Definition: lineinput.h:30
bool m_Selecting
Definition: lineinput.h:29
vec2 m_Offset
Definition: lineinput.h:32
vec2 m_ReleaseMouse
Definition: lineinput.h:31
Definition: textrender.h:175
vector2_base< float > vec2
Definition: vmath.h:158