forked from nbcraft-org/nbcraft
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTextInputBox.cpp
More file actions
498 lines (456 loc) · 10.9 KB
/
TextInputBox.cpp
File metadata and controls
498 lines (456 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/********************************************************************
Minecraft: Pocket Edition - Decompilation Project
Copyright (C) 2023 iProgramInCpp
The following code is licensed under the BSD 1 clause license.
SPDX-License-Identifier: BSD-1-Clause
********************************************************************/
#include "TextInputBox.hpp"
#include "client/app/Minecraft.hpp"
#ifndef ORIGINAL_CODE
#ifdef USE_NATIVE_ANDROID
#define HANDLE_CHARS_SEPARATELY // faked though, see platforms/android/minecraftcpp/minecraftcpp.NativeActivity/main.cpp
#endif
TextInputBox::TextInputBox(Screen* parent, int id, int x, int y, int width, int height, const std::string& placeholder, const std::string& text)
{
m_ID = id;
m_xPos = x;
m_yPos = y;
m_width = width;
m_height = height;
m_placeholder = placeholder;
m_text = text;
m_bFocused = false;
m_bEnabled = true;
m_bCursorOn = true;
m_insertHead = 0;
m_lastFlashed = 0;
m_pFont = nullptr;
m_pParent = parent;
m_maxLength = -1;
m_scrollPos = 0;
}
TextInputBox::~TextInputBox()
{
m_pParent->m_pMinecraft->platform()->hideKeyboard();
}
void TextInputBox::init(Font* pFont)
{
m_pFont = pFont;
}
void TextInputBox::setEnabled(bool bEnabled)
{
m_bEnabled = true;
}
#ifdef USE_SDL
// See https://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlkey.html
#define AKEYCODE_FORWARD_DEL SDLVK_DELETE
#define AKEYCODE_ARROW_LEFT SDLVK_LEFT
#define AKEYCODE_ARROW_RIGHT SDLVK_RIGHT
#define AKEYCODE_DEL SDLVK_BACKSPACE
#define AKEYCODE_ENTER SDLVK_RETURN
#define AKEYCODE_A SDLVK_a
#define AKEYCODE_Z SDLVK_z
#define AKEYCODE_0 SDLVK_0
#define AKEYCODE_9 SDLVK_9
#define AKEYCODE_SPACE SDLVK_SPACE
#define AKEYCODE_COMMA SDLVK_COMMA
#define AKEYCODE_PERIOD SDLVK_PERIOD
#define AKEYCODE_PLUS SDLVK_PLUS
#define AKEYCODE_MINUS SDLVK_MINUS
#define AKEYCODE_SEMICOLON SDLVK_SEMICOLON
#define AKEYCODE_SLASH SDLVK_SLASH
#define AKEYCODE_GRAVE SDLVK_BACKQUOTE
#define AKEYCODE_BACKSLASH SDLVK_BACKSLASH
#define AKEYCODE_APOSTROPHE SDLVK_QUOTE
#define AKEYCODE_LEFT_BRACKET SDLVK_LEFTBRACKET
#define AKEYCODE_RIGHT_BRACKET SDLVK_RIGHTBRACKET
#elif defined(_WIN32)
// See https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
#define AKEYCODE_FORWARD_DEL VK_DELETE
#define AKEYCODE_ARROW_LEFT VK_LEFT
#define AKEYCODE_ARROW_RIGHT VK_RIGHT
#define AKEYCODE_DEL VK_BACK
#define AKEYCODE_ENTER VK_RETURN
#define AKEYCODE_A 'A'
#define AKEYCODE_Z 'Z'
#define AKEYCODE_0 '0'
#define AKEYCODE_9 '9'
#define AKEYCODE_SPACE VK_SPACE
#define AKEYCODE_COMMA VK_OEM_COMMA
#define AKEYCODE_PERIOD VK_OEM_PERIOD
#define AKEYCODE_PLUS VK_OEM_PLUS
#define AKEYCODE_MINUS VK_OEM_MINUS
#define AKEYCODE_SEMICOLON VK_OEM_1
#define AKEYCODE_SLASH VK_OEM_2
#define AKEYCODE_GRAVE VK_OEM_3
#define AKEYCODE_BACKSLASH VK_OEM_5
#define AKEYCODE_APOSTROPHE VK_OEM_7
#define AKEYCODE_LEFT_BRACKET VK_OEM_4
#define AKEYCODE_RIGHT_BRACKET VK_OEM_6
#endif
#ifndef HANDLE_CHARS_SEPARATELY
char TextInputBox::guessCharFromKey(int key) {
bool bShiftPressed = m_pParent->m_pMinecraft->platform()->shiftPressed();
char chr = '\0';
if (key >= AKEYCODE_A && key <= AKEYCODE_Z)
{
chr = char((key - AKEYCODE_A) + (bShiftPressed ? 'A' : 'a'));
}
if (key >= AKEYCODE_0 && key <= AKEYCODE_9)
{
static const char shiftmap[] = { ')', '!', '@', '#', '$', '%', '^', '&', '*', '(' };
chr = char(bShiftPressed ? shiftmap[key - AKEYCODE_0] : (key - AKEYCODE_0 + '0'));
}
switch (key)
{
case AKEYCODE_SPACE:
chr = ' ';
break;
case AKEYCODE_COMMA:
chr = bShiftPressed ? '<' : ',';
break;
case AKEYCODE_PERIOD:
chr = bShiftPressed ? '>' : '.';
break;
case AKEYCODE_PLUS:
chr = bShiftPressed ? '+' : '=';
break;
case AKEYCODE_MINUS:
chr = bShiftPressed ? '_' : '-';
break;
case AKEYCODE_SEMICOLON:
chr = bShiftPressed ? ':' : ';';
break;
case AKEYCODE_SLASH:
chr = bShiftPressed ? '?' : '/';
break;
case AKEYCODE_GRAVE:
chr = bShiftPressed ? '~' : '`';
break;
case AKEYCODE_BACKSLASH:
chr = bShiftPressed ? '|' : '\\';
break;
case AKEYCODE_APOSTROPHE:
chr = bShiftPressed ? '"' : '\'';
break;
case AKEYCODE_LEFT_BRACKET:
chr = bShiftPressed ? '{' : '[';
break;
case AKEYCODE_RIGHT_BRACKET:
chr = bShiftPressed ? '}' : ']';
break;
}
return chr;
}
#endif
void TextInputBox::keyPressed(int key)
{
if (!m_bFocused)
{
return;
}
#ifndef HANDLE_CHARS_SEPARATELY
char guess = guessCharFromKey(key);
if (guess != '\0') {
charPressed(guess);
return;
}
#endif
switch (key) {
case AKEYCODE_DEL:
{
charPressed('\b');
}
case AKEYCODE_FORWARD_DEL:
{
charPressed(AKEYCODE_FORWARD_DEL);
}
case AKEYCODE_ARROW_LEFT:
{
// Left
m_insertHead--;
if (m_insertHead < 0)
{
m_insertHead = 0;
}
recalculateScroll();
break;
}
case AKEYCODE_ARROW_RIGHT:
{
// Right
m_insertHead++;
if (!m_text.empty())
{
if (m_insertHead > int(m_text.size()))
{
m_insertHead = int(m_text.size());
}
}
else
{
m_insertHead = 0;
}
recalculateScroll();
break;
}
case AKEYCODE_ENTER:
{
// Enter
m_bFocused = false;
break;
}
}
}
void TextInputBox::tick()
{
if (!m_lastFlashed)
m_lastFlashed = getTimeMs();
if (m_bFocused)
{
if (getTimeMs() > m_lastFlashed + 500)
{
m_lastFlashed += 500;
m_bCursorOn ^= 1;
}
}
else
{
m_bCursorOn = false;
}
}
void TextInputBox::setFocused(bool b)
{
if (m_bFocused == b)
return;
if (b)
{
int x = (int) (((float) m_xPos) / Gui::InvGuiScale);
int y = (int) (((float) m_yPos) / Gui::InvGuiScale);
int w = (int) (((float) m_width) / Gui::InvGuiScale);
int h = (int) (((float) m_height) / Gui::InvGuiScale);
m_pParent->m_pMinecraft->platform()->showKeyboard(x, y, w, h);
}
else
{
m_pParent->m_pMinecraft->platform()->hideKeyboard();
}
m_bFocused = b;
if (b)
{
m_lastFlashed = getTimeMs();
m_bCursorOn = true;
m_insertHead = int(m_text.size());
recalculateScroll();
}
// don't actually hide the keyboard when unfocusing
// - we may be undoing the work of another text box
}
void TextInputBox::onClick(int x, int y)
{
setFocused(clicked(x, y));
}
void TextInputBox::charPressed(int k)
{
if (!m_bFocused)
return;
switch (k) {
case '\b':
{
// Backspace
if (m_text.empty())
{
return;
}
if (m_insertHead <= 0)
{
return;
}
if (m_insertHead > int(m_text.size()))
{
m_insertHead = int(m_text.size());
}
m_text.erase(m_text.begin() + m_insertHead - 1, m_text.begin() + m_insertHead);
m_insertHead--;
recalculateScroll();
break;
}
case AKEYCODE_FORWARD_DEL:
{
// Delete
if (m_text.empty())
{
return;
}
if (m_insertHead < 0)
{
return;
}
if (m_insertHead >= int(m_text.size()))
{
return;
}
m_text.erase(m_text.begin() + m_insertHead, m_text.begin() + m_insertHead + 1);
break;
}
default:
{
// Ignore Unprintable Characters
if (k == '\n' || k < ' ' || k > '~')
return;
// Check Max Length
if (m_maxLength != -1 && int(m_text.length()) >= m_maxLength)
{
return;
}
// Insert
m_text.insert(m_text.begin() + m_insertHead, k);
m_insertHead++;
recalculateScroll();
break;
}
}
}
constexpr int PADDING = 5;
std::string TextInputBox::getRenderedText(int scroll_pos, std::string text)
{
// Not the most efficient code.
// But it does not run often enough to matter.
std::string rendered_text = text.substr(scroll_pos);
int max_width = m_width - (PADDING * 2);
while (m_pFont->width(rendered_text) > max_width)
{
//rendered_text.pop_back(); // breaks C++03 compatibility
rendered_text.erase(rendered_text.length()-2, 1);
}
return rendered_text;
}
constexpr char CURSOR_CHAR = '_';
void TextInputBox::render()
{
fill(m_xPos, m_yPos, m_xPos + m_width, m_yPos + m_height, 0xFFAAAAAA);
fill(m_xPos + 1, m_yPos + 1, m_xPos + m_width - 1, m_yPos + m_height - 1, 0xFF000000);
int text_color;
int scroll_pos;
std::string rendered_text;
if (m_text.empty())
{
rendered_text = m_placeholder;
text_color = 0x404040;
scroll_pos = 0;
}
else
{
rendered_text = m_text;
text_color = 0xffffff;
scroll_pos = m_scrollPos;
}
rendered_text = getRenderedText(scroll_pos, rendered_text);
int textYPos = (m_height - 8) / 2;
drawString(m_pFont, rendered_text, m_xPos + PADDING, m_yPos + textYPos, text_color);
if (m_bCursorOn)
{
int cursor_pos = m_insertHead - m_scrollPos;
if (cursor_pos >= 0 && cursor_pos <= int(rendered_text.length()))
{
std::string substr = rendered_text.substr(0, cursor_pos);
int xPos = PADDING + m_pFont->width(substr);
std::string str;
str += CURSOR_CHAR;
drawString(m_pFont, str, m_xPos + xPos, m_yPos + textYPos + 2, 0xffffff);
}
}
}
bool TextInputBox::clicked(int xPos, int yPos)
{
if (!m_bEnabled) return false;
if (xPos < m_xPos) return false;
if (yPos < m_yPos) return false;
if (xPos >= m_xPos + m_width) return false;
if (yPos >= m_yPos + m_height) return false;
return true;
}
void TextInputBox::recalculateScroll()
{
// Skip If Size Unset
if (m_width == 0)
{
return;
}
// Ensure Cursor Is Visible
bool is_cursor_at_end = m_insertHead == int(m_text.length());
if (m_scrollPos >= m_insertHead && m_scrollPos > 0)
{
// Cursor Is At Scroll Position
// Move Back Scroll As Far As Possible
while (true)
{
int test_scroll_pos = m_scrollPos - 1;
std::string rendered_text = m_text;
if (is_cursor_at_end)
{
rendered_text += CURSOR_CHAR;
}
rendered_text = getRenderedText(test_scroll_pos, rendered_text);
int cursor_pos = m_insertHead - test_scroll_pos;
if (cursor_pos >= int(rendered_text.length()))
{
break;
}
else
{
m_scrollPos = test_scroll_pos;
if (m_scrollPos == 0)
{
break;
}
}
}
}
else
{
// Cursor After Scroll Area
// Increase Scroll So Cursor Is Visible
while (true)
{
std::string rendered_text = m_text;
if (is_cursor_at_end)
{
rendered_text += CURSOR_CHAR;
}
rendered_text = getRenderedText(m_scrollPos, rendered_text);
int cursor_pos = m_insertHead - m_scrollPos;
if (cursor_pos < int(rendered_text.length()))
{
break;
}
else
{
if (m_scrollPos == int(m_text.length())) {
LOG_W("Text Box Is Too Small");
break;
} else {
m_scrollPos++;
}
}
}
}
}
std::string TextInputBox::getText()
{
return m_text;
}
void TextInputBox::setText(const std::string& text)
{
m_text = text;
m_insertHead = int(m_text.size());
}
bool TextInputBox::isFocused()
{
return m_bFocused;
}
void TextInputBox::setMaxLength(int max_length)
{
m_maxLength = max_length;
}
#endif