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