@@ -24,11 +24,13 @@ namespace TextInput {
24
24
SDL_Point cursor_select_pos;
25
25
int cursor_x_tallest;
26
26
27
- void init (void ) {
27
+ void init (void )
28
+ {
28
29
taking_input = false ;
29
30
}
30
31
31
- void attach_input (std::vector<std::string>* text) {
32
+ void attach_input (std::vector<std::string>* text)
33
+ {
32
34
taking_input = true ;
33
35
current_text = text;
34
36
selecting = false ;
@@ -41,7 +43,8 @@ namespace TextInput {
41
43
send_cursor_to_end ();
42
44
}
43
45
44
- void attach_input (std::string* text) {
46
+ void attach_input (std::string* text)
47
+ {
45
48
taking_input = true ;
46
49
current_text_line = text;
47
50
selecting = false ;
@@ -58,7 +61,8 @@ namespace TextInput {
58
61
cursor_x_tallest = cursor_pos.x ;
59
62
}
60
63
61
- void detach_input (void ) {
64
+ void detach_input (void )
65
+ {
62
66
taking_input = false ;
63
67
SDL_StopTextInput ();
64
68
}
@@ -70,13 +74,16 @@ namespace TextInput {
70
74
cursor_x_tallest = cursor_pos.x ;
71
75
}
72
76
73
- void insert_text (std::string text) {
77
+ void insert_text (std::string text)
78
+ {
74
79
// Insert text at cursor position, respecting newlines
75
80
// Don't bother deleting selection, already done
76
81
77
82
std::string::iterator it;
78
- for (it = text.begin (); it != text.end (); it++) {
79
- if (*it == ' \n ' ) {
83
+ for (it = text.begin (); it != text.end (); it++)
84
+ {
85
+ if (*it == ' \n ' )
86
+ {
80
87
insert_newline ();
81
88
}
82
89
else if (*it != ' \r ' && *it != ' \0 ' )
@@ -193,7 +200,8 @@ namespace TextInput {
193
200
194
201
SelectionRect rect = reorder_selection_positions ();
195
202
196
- if (rect.y == rect.y2 ) {
203
+ if (rect.y == rect.y2 )
204
+ {
197
205
return UTF8_substr (get_line (rect.y ), rect.x , rect.x2 );
198
206
}
199
207
@@ -202,14 +210,16 @@ namespace TextInput {
202
210
203
211
// Loop through the lines in between
204
212
int total_length = SDL_strlen (select_part_first_line) + SDL_strlen (select_part_last_line) + 1 ;
205
- for (int i = rect.y + 1 ; i < rect.y2 ; i++) {
213
+ for (int i = rect.y + 1 ; i < rect.y2 ; i++)
214
+ {
206
215
total_length += SDL_strlen (get_line (i)) + 1 ;
207
216
}
208
217
209
218
char * select_part = (char *)SDL_malloc (total_length);
210
219
strcpy (select_part, select_part_first_line);
211
220
strcat (select_part, " \n " );
212
- for (int i = rect.y + 1 ; i < rect.y2 ; i++) {
221
+ for (int i = rect.y + 1 ; i < rect.y2 ; i++)
222
+ {
213
223
strcat (select_part, get_line (i));
214
224
strcat (select_part, " \n " );
215
225
}
@@ -221,15 +231,19 @@ namespace TextInput {
221
231
return select_part;
222
232
}
223
233
224
- SelectionRect reorder_selection_positions (void ) {
234
+ SelectionRect reorder_selection_positions (void )
235
+ {
225
236
SelectionRect positions;
226
237
bool in_front = false ;
227
238
228
- if (cursor_pos.y > cursor_select_pos.y ) {
239
+ if (cursor_pos.y > cursor_select_pos.y )
240
+ {
229
241
in_front = true ;
230
242
}
231
- else if (cursor_pos.y == cursor_select_pos.y ) {
232
- if (cursor_pos.x >= cursor_select_pos.x ) {
243
+ else if (cursor_pos.y == cursor_select_pos.y )
244
+ {
245
+ if (cursor_pos.x >= cursor_select_pos.x )
246
+ {
233
247
in_front = true ;
234
248
}
235
249
}
@@ -254,7 +268,8 @@ namespace TextInput {
254
268
255
269
void remove_characters (int x, int y, int x2, int y2)
256
270
{
257
- if (x == x2 && y == y2) {
271
+ if (x == x2 && y == y2)
272
+ {
258
273
return ;
259
274
}
260
275
// Get the rest of the last line
@@ -295,14 +310,17 @@ namespace TextInput {
295
310
void move_cursor_up (void )
296
311
{
297
312
bool reset = process_selection (); // Only returns true if you don't hold shift
298
- if (reset && (cursor_pos.y > cursor_select_pos.y )) {
313
+ if (reset && (cursor_pos.y > cursor_select_pos.y ))
314
+ {
299
315
cursor_pos.y = cursor_select_pos.y ;
300
316
}
301
317
302
- if (cursor_pos.y > 0 ) {
318
+ if (cursor_pos.y > 0 )
319
+ {
303
320
cursor_pos.y --;
304
321
cursor_pos.x = cursor_x_tallest;
305
- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
322
+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
323
+ {
306
324
cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
307
325
}
308
326
}
@@ -311,14 +329,17 @@ namespace TextInput {
311
329
void move_cursor_down (void )
312
330
{
313
331
bool reset = process_selection (); // Only returns true if you don't hold shift
314
- if (reset && (cursor_pos.y < cursor_select_pos.y )) {
332
+ if (reset && (cursor_pos.y < cursor_select_pos.y ))
333
+ {
315
334
cursor_pos.y = cursor_select_pos.y ;
316
335
}
317
336
318
- if (cursor_pos.y < get_lines () - 1 ) {
337
+ if (cursor_pos.y < get_lines () - 1 )
338
+ {
319
339
cursor_pos.y ++;
320
340
cursor_pos.x = cursor_x_tallest;
321
- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
341
+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
342
+ {
322
343
cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
323
344
}
324
345
}
@@ -330,17 +351,19 @@ namespace TextInput {
330
351
331
352
bool is_word_part (char character)
332
353
{
333
- return character > 0x7F || isalpha (character) || isdigit (character) || character == ' _' || character == ' -' ;
354
+ return character < 0 || isalpha (character) || isdigit (character) || character == ' _' || character == ' -' ;
334
355
}
335
356
336
357
void move_cursor_left (void )
337
358
{
338
359
bool reset = process_selection (); // Only returns true if you don't hold shift
339
- if (reset) {
360
+ if (reset)
361
+ {
340
362
cursor_pos.x = cursor_select_pos.x ;
341
363
}
342
364
343
- if (cursor_pos.x == 0 && cursor_pos.y == 0 ) {
365
+ if (cursor_pos.x == 0 && cursor_pos.y == 0 )
366
+ {
344
367
cursor_x_tallest = 0 ;
345
368
return ;
346
369
}
@@ -372,7 +395,8 @@ namespace TextInput {
372
395
}
373
396
else
374
397
{
375
- if (cursor_pos.x > 0 ) {
398
+ if (cursor_pos.x > 0 )
399
+ {
376
400
cursor_pos.x --;
377
401
}
378
402
else if (cursor_pos.y > 0 )
@@ -388,11 +412,13 @@ namespace TextInput {
388
412
void move_cursor_right (void )
389
413
{
390
414
bool reset = process_selection (); // Only returns true if you don't hold shift
391
- if (reset) {
415
+ if (reset)
416
+ {
392
417
cursor_pos.x = cursor_select_pos.x ;
393
418
}
394
419
395
- if (cursor_pos.x >= UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y == get_lines () - 1 ) {
420
+ if (cursor_pos.x >= (int ) UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y == get_lines () - 1 )
421
+ {
396
422
cursor_x_tallest = cursor_pos.x ;
397
423
return ;
398
424
}
@@ -414,7 +440,7 @@ namespace TextInput {
414
440
}
415
441
SDL_free (character);
416
442
cursor_pos.x ++;
417
- if (cursor_pos.x >= UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y < get_lines () - 1 )
443
+ if (cursor_pos.x >= ( int ) UTF8_total_codepoints (get_line (cursor_pos.y )) && cursor_pos.y < get_lines () - 1 )
418
444
{
419
445
cursor_pos.y ++;
420
446
cursor_pos.x = 0 ;
@@ -425,7 +451,8 @@ namespace TextInput {
425
451
else
426
452
{
427
453
428
- if (cursor_pos.x < UTF8_total_codepoints (get_line (cursor_pos.y ))) {
454
+ if (cursor_pos.x < (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
455
+ {
429
456
cursor_pos.x ++;
430
457
}
431
458
else if (cursor_pos.y < get_lines () - 1 )
@@ -441,7 +468,8 @@ namespace TextInput {
441
468
void backspace (void )
442
469
{
443
470
// The user pressed backspace.
444
- if (selecting) {
471
+ if (selecting)
472
+ {
445
473
remove_selection ();
446
474
return ;
447
475
}
@@ -537,12 +565,13 @@ namespace TextInput {
537
565
void delete_key (void )
538
566
{
539
567
// The user pressed delete.
540
- if (selecting) {
568
+ if (selecting)
569
+ {
541
570
remove_selection ();
542
571
return ;
543
572
}
544
573
545
- if (cursor_pos.x == UTF8_total_codepoints (get_line (cursor_pos.y )))
574
+ if (cursor_pos.x == ( int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
546
575
{
547
576
if (cursor_pos.y < get_lines () - 1 )
548
577
{
@@ -598,10 +627,12 @@ namespace TextInput {
598
627
}
599
628
}
600
629
601
- void handle_events (SDL_Event e) {
630
+ void handle_events (SDL_Event e)
631
+ {
602
632
if (!taking_input) return ;
603
633
604
- if (e.type == SDL_KEYDOWN) {
634
+ if (e.type == SDL_KEYDOWN)
635
+ {
605
636
// Show cursor!!
606
637
flash_timer = 0 ;
607
638
@@ -613,7 +644,8 @@ namespace TextInput {
613
644
}
614
645
else if (e.key .keysym .sym == SDLK_v && SDL_GetModState () & KMOD_CTRL)
615
646
{
616
- if (selecting) {
647
+ if (selecting)
648
+ {
617
649
remove_selection ();
618
650
}
619
651
char * clipboard_text = SDL_GetClipboardText ();
@@ -695,20 +727,24 @@ namespace TextInput {
695
727
else if (e.key .keysym .sym == SDLK_PAGEUP && can_move_cursor)
696
728
{
697
729
cursor_pos.y -= 10 ;
698
- if (cursor_pos.y < 0 ) {
730
+ if (cursor_pos.y < 0 )
731
+ {
699
732
cursor_pos.y = 0 ;
700
733
}
701
- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
734
+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
735
+ {
702
736
cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
703
737
}
704
738
}
705
739
else if (e.key .keysym .sym == SDLK_PAGEDOWN && can_move_cursor)
706
740
{
707
741
cursor_pos.y += 10 ;
708
- if (cursor_pos.y >= get_lines ()) {
742
+ if (cursor_pos.y >= get_lines ())
743
+ {
709
744
cursor_pos.y = get_lines () - 1 ;
710
745
}
711
- if (cursor_pos.x > UTF8_total_codepoints (get_line (cursor_pos.y ))) {
746
+ if (cursor_pos.x > (int ) UTF8_total_codepoints (get_line (cursor_pos.y )))
747
+ {
712
748
cursor_pos.x = UTF8_total_codepoints (get_line (cursor_pos.y ));
713
749
}
714
750
}
@@ -720,7 +756,8 @@ namespace TextInput {
720
756
flash_timer = 0 ;
721
757
722
758
// Append character(s)
723
- if (selecting) {
759
+ if (selecting)
760
+ {
724
761
remove_selection ();
725
762
}
726
763
insert_text (e.text .text );
0 commit comments