-
Notifications
You must be signed in to change notification settings - Fork 1
/
TextField.asc
942 lines (842 loc) · 27.8 KB
/
TextField.asc
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* TEXT FIELD MODULE - Script *
* by Gunnar Harboe (Snarky), v1.3.0 *
* *
* Copyright (c) 2018, 2019 Gunnar Harboe *
* *
* *
* This code is offered under multiple licenses. Choose whichever one you like. *
* *
* You may use it under the MIT license: *
* https://opensource.org/licenses/MIT *
* *
* You may also use it under the Creative Commons Attribution 4.0 International License. *
* https://creativecommons.org/licenses/by/4.0/ *
* *
* You may also use it under the Artistic License 2.0 *
* https://opensource.org/licenses/Artistic-2.0 *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#ifdef ENABLE_TEXTFIELD
TextField* _focusedTextField; // The TextField that has focus
int _textFieldCount; // Number of TextField created
int _textFieldSize; // Current size of TextField arrays
bool _handlesReturn; // Whether the TextField handles return
int _blinkTimer; // To keep track of caret blinking
int _blinkDelay=20; // Number of loops between caret blink
bool _wasShiftPressed; // Whether Shift was pressed last game loop
// Variables used to handle mouse drag-to-select gesture
bool _wasButtonDown; // Whether the left mouse button was down last game loop
TextField* _draggingTextField; // The TextField that a drag gesture started in
int _dragLastX; // The mouse coordinates last game loop (to check if we need to update)
int _dragLastY;
// TextField Arrays
TextField* _textFields[];
String _textFieldTexts[];
Button* _textDisplayButtons[];
DynamicSprite* _textSprites[];
DynamicSprite* _borderSprites[];
DynamicSprite* _focusedTextSpriteCaret; // (We just need this for the currently focused TextField)
#region General helper functions
int _max(int a, int b)
{
if(a>b) return a;
return b;
}
int _min(int a, int b)
{
if(a<b) return a;
return b;
}
bool IsShiftPressed()
{
return IsKeyPressed(403) || IsKeyPressed(404);
}
#endregion
#region String manipulation functions
String DeleteChar(this String*, int index)
{
if(index == this.Length)
return this.Truncate(this.Length-1);
String s1 = this.Truncate(index-1);
String s2 = this.Substring(index, this.Length - index);
return s1.Append(s2);
}
String DeleteSubstring(this String*, int startIndex, int endIndex)
{
/*
// Watching out for edge cases:
// The string functions don't like returning empty strings
String s1 = ""; String s2 = "";
if(startIndex>0)
s1 = this.Truncate(startIndex);
if(endIndex<this.Length)
s2 = this.Substring(endIndex, this.Length-endIndex);
return s1.Append(s2);
*/
String s1 = this.Truncate(startIndex);
String s2 = this.Substring(endIndex, this.Length-endIndex);
return s1.Append(s2);
}
String InsertChar(this String*, char c, int index)
{
if(index == this.Length)
return this.AppendChar(c);
String s1 = this.Truncate(index);
s1 = s1.AppendChar(c);
String s2 = this.Substring(index, this.Length - index);
return s1.Append(s2);
}
String InsertString(this String*, String s, int index)
{
if(index == this.Length)
return this.Append(s);
String s1 = this.Truncate(index);
s1 = s1.Append(s);
String s2 = this.Substring(index, this.Length - index);
return s1.Append(s2);
}
#endregion
#region Attribute getters
//readonly attribute int ID;
int get_ID(this TextField*)
{
// This is because the field is initialized to 0 by default, and we want that to be an illegal value
// in order to require users to use TextField.Create()
return this._id-1;
}
bool _isValid(this TextField*)
{
return this._id > 0;
}
// attribute int BorderTransparency;
int get_BorderTransparency(this TextField*)
{
return this._borderTransparency;
}
//attribute FontType Font;
FontType get_Font(this TextField*)
{
if(!this._isValid())
return 0;
return _textDisplayButtons[this.get_ID()].Font;
}
//readonly attribute bool HasFocus;
bool get_HasFocus(this TextField*)
{
return (this == _focusedTextField);
}
//static attribute bool HandlesReturn;
bool get_HandlesReturn(static TextField)
{
return _handlesReturn;
}
//static attribute int BlinkDelay
int get_BlinkDelay(static TextField)
{
return _blinkDelay;
}
//readonly attribute Button* TextDisplayButton;
Button* get_TextDisplayButton(this TextField*)
{
if(!this._isValid())
return null;
return _textDisplayButtons[this.get_ID()];
}
// attribute bool Enabled;
bool get_Enabled(this TextField*)
{
return this._isValid() && _textDisplayButtons[this.get_ID()].Enabled;
}
//static attribute TextField* Focused;
TextField* get_Focused(static TextField)
{
return _focusedTextField;
}
//attribute int TextColor;
int get_TextColor(this TextField*)
{
if(!this._isValid())
return 0;
return _textDisplayButtons[this.get_ID()].TextColor;
}
//attribute int SelectionColor;
int get_SelectionColor(this TextField*)
{
if(!this._isValid())
return 0;
return this._selectionColor;
}
//attribute int MaxLength;
int get_MaxLength(this TextField*)
{
return this._maxLength;
}
//attribute String Text;
String get_Text(this TextField*)
{
if(this._isValid())
return _textFieldTexts[this.get_ID()];
else return null;
}
//attribute int CaretIndex;
int get_CaretIndex(this TextField*)
{
return this._caretIndex;
}
#endregion
#region Module functions
int calculateStringWidth(this TextField*, int startIndex, int endIndex)
{
String subString = _textFieldTexts[this.get_ID()].Substring(startIndex, endIndex-startIndex);
return GetTextWidth(subString, this.get_Font());
}
// Draw a box around the borders of the TextField
void RenderBorder(this TextField*)
{
int id = this.get_ID();
DynamicSprite* sprite = _borderSprites[id];
Button* displayButton = _textDisplayButtons[id];
DrawingSurface* surface = sprite.GetDrawingSurface();
surface.Clear();
surface.DrawingColor = displayButton.TextColor;
// Draw border
int hbwd = TEXTFIELD_BORDER_WIDTH/2; // Half border width, rounded down
int hbwu = (TEXTFIELD_BORDER_WIDTH+1)/2; // Half border width, rounded up
surface.DrawLine(hbwd, hbwd, surface.Width, hbwd, TEXTFIELD_BORDER_WIDTH); // Top
surface.DrawLine(hbwd, hbwd, hbwd, surface.Height, TEXTFIELD_BORDER_WIDTH); // Left
surface.DrawLine(hbwd, surface.Height-hbwu, surface.Width, surface.Height-hbwu, TEXTFIELD_BORDER_WIDTH); // Bottom
surface.DrawLine(surface.Width-hbwu, hbwd, surface.Width-hbwu, surface.Height, TEXTFIELD_BORDER_WIDTH); // Right
surface.Release();
}
void RenderText(this TextField*)
{
int id = this.get_ID();
DynamicSprite* sprite = _textSprites[id];
Button* displayButton = _textDisplayButtons[id];
DrawingSurface* surface = sprite.GetDrawingSurface();
surface.Clear();
surface.DrawingColor = displayButton.TextColor;
String fieldText = _textFieldTexts[id];
if(!String.IsNullOrEmpty(fieldText))
{
if(this._selectionStartIndex == this._caretIndex) // Just draw the text
surface.DrawString(this._paddingLeft, this._paddingTop, displayButton.Font, fieldText);
else // Part of the text is selected
{
int i1 = _min(this._selectionStartIndex, this._caretIndex);
int i2 = _max(this._selectionStartIndex, this._caretIndex);
int w1 = _min(this._selectionStartX, this._caretX);
int w2 = _max(this._selectionStartX, this._caretX);
// 1. Draw the part of the text before the selection
if(i1>0)
{
String s1 = fieldText.Truncate(i1);
surface.DrawString(this._paddingLeft, this._paddingTop, displayButton.Font, s1);
}
// 2. Draw the text selection
// Draw selection rectangle
surface.DrawingColor = this._selectionColor;
surface.DrawRectangle(w1, this._paddingTop, w2, this._paddingTop+GetFontHeight(displayButton.Font));
// Draw selected text
String s2 = fieldText.Substring(i1, i2-i1);
// TODO: Reverse text color
surface.DrawingColor = Game.GetColorFromRGB(128, 128, 128);
surface.DrawString(w1, this._paddingTop, displayButton.Font, s2);
// 3. Draw the part of the text after the selection
surface.DrawingColor = displayButton.TextColor;
if(i2<fieldText.Length)
{
String s3 = fieldText.Substring(i2, fieldText.Length-i2);
surface.DrawString(w2, this._paddingTop, displayButton.Font, s3);
}
}
}
// Draw border
surface.DrawImage(0, 0, _borderSprites[id].Graphic, this._borderTransparency);
surface.Release();
}
// Draw a version of the TextField with the caret showing (we flip to this sprite to blink caret)
void RenderFocusSprite(this TextField*)
{
int id = this.get_ID();
DynamicSprite* sprite = _textSprites[id];
if(_focusedTextSpriteCaret != null)
_focusedTextSpriteCaret.Delete();
// Copy the sprite without the caret
_focusedTextSpriteCaret = DynamicSprite.CreateFromExistingSprite(sprite.Graphic, true);
DrawingSurface* surface = _focusedTextSpriteCaret.GetDrawingSurface();
// Draw caret
surface.DrawingColor = _textDisplayButtons[id].TextColor;
surface.DrawLine(this._caretX - TEXTFIELD_CARET_WIDTH/2 + (TEXTFIELD_CARET_OFFSET_X),
this._caretY,
this._caretX - TEXTFIELD_CARET_WIDTH/2 + (TEXTFIELD_CARET_OFFSET_X),
this._caretY + GetFontHeight(_textDisplayButtons[id].Font),
TEXTFIELD_CARET_WIDTH);
surface.Release();
_textDisplayButtons[id].NormalGraphic = _focusedTextSpriteCaret.Graphic;
_textDisplayButtons[id].PushedGraphic = _focusedTextSpriteCaret.Graphic;
}
bool SetFocus(this TextField*, bool giveFocus)
{
if(!this.get_Enabled())
return false;
if(giveFocus)
{
if(_focusedTextField != this)
{
// Unfocus any other field that has focus
if(_focusedTextField != null)
_textDisplayButtons[_focusedTextField.get_ID()].NormalGraphic = _textSprites[_focusedTextField.get_ID()].Graphic;
_focusedTextField = this;
this.RenderFocusSprite();
_textDisplayButtons[this.get_ID()].NormalGraphic = _focusedTextSpriteCaret.Graphic;
_textDisplayButtons[this.get_ID()].PushedGraphic = _focusedTextSpriteCaret.Graphic;
}
return true;
}
else if(_focusedTextField == this)
{
_focusedTextField = null;
int id = this.get_ID();
_textDisplayButtons[id].NormalGraphic = _textSprites[id].Graphic;
_textDisplayButtons[id].PushedGraphic = _textSprites[id].Graphic;
}
return false;
}
bool Activated(this TextField*)
{
bool a = this._activated;
this._activated = false;
return a;
}
void calculateCaretPos(this TextField*)
{
String caretString = _textFieldTexts[this.get_ID()].Truncate(this._caretIndex);
this._caretX = GetTextWidth(caretString, this.get_Font()) + this._paddingLeft;
this._caretY = this._paddingTop;
}
#endregion
#region Attribute setters
void _setId(this TextField*, int value)
{
this._id = value;
}
void _setPaddingLeft(this TextField*, int value)
{
this._paddingLeft = value;
}
void _setPaddingTop(this TextField*, int value)
{
this._paddingTop = value;
}
void set_Font(this TextField*, FontType value)
{
if(this._isValid())
{
int id = this.get_ID();
_textDisplayButtons[id].Font = value;
this.RenderText();
if(this.get_HasFocus())
this.RenderFocusSprite();
}
}
void set_BorderTransparency(this TextField*, int value)
{
if(value<0) value=0;
if(value>100) value=100;
if(value != this._borderTransparency)
{
this._borderTransparency = value;
this.RenderText();
this.RenderFocusSprite();
}
}
void set_HandlesReturn(static TextField, bool value)
{
_handlesReturn = value;
}
void set_BlinkDelay(static TextField, int value)
{
_blinkDelay = value;
}
void set_Enabled(this TextField*, bool value)
{
if(!this._isValid())
return;
int id = this.get_ID();
_textDisplayButtons[id].Enabled = value;
if(!value)
{
if(_focusedTextField == this)
_focusedTextField = null;
_textDisplayButtons[id].NormalGraphic = _textSprites[id].Graphic;
}
}
void set_Focused(static TextField, TextField* value)
{
value.SetFocus(true);
}
void set_TextColor(this TextField*, int value)
{
if(this._isValid())
{
int id = this.get_ID();
_textDisplayButtons[id].TextColor = value;
this.RenderText();
if(this.get_HasFocus())
this.RenderFocusSprite();
}
}
void set_SelectionColor(this TextField*, int value)
{
this._selectionColor = value;
if(this._isValid())
{
this.RenderText();
if(this.get_HasFocus())
this.RenderFocusSprite();
}
}
void set_MaxLength(this TextField*, int value)
{
if(value<0)
value=0;
this._maxLength = value;
int id = this.get_ID();
if(value > 0 && _textFieldTexts[id] != null && _textFieldTexts[id].Length > value)
{
_textFieldTexts[id] = _textFieldTexts[id].Truncate(value);
}
}
void set_CaretIndex(this TextField*, int value, bool doSelect)
{
String txt = this.get_Text();
if(txt == null)
{
this._caretIndex = 0;
this._selectionStartIndex = 0;
return;
}
bool wasSelection = (this._caretIndex != this._selectionStartIndex);
if(value < 0)
value = 0;
if(value > txt.Length)
value = txt.Length;
if(value != this._caretIndex)
{
this._caretIndex = value;
this.calculateCaretPos();
this.RenderFocusSprite();
}
// If making a selection, re-render the selection region
if(doSelect)
{
this.RenderText();
this.RenderFocusSprite();
}
else
{
// Unset the selection, re-render if necessary
this._selectionStartIndex = this._caretIndex;
this._selectionStartX = this._caretX;
if(wasSelection) this.RenderText();
this.RenderFocusSprite();
}
}
void set_Text(this TextField*, String value)
{
if(this._isValid())
{
if(value == null)
value = "";
if(this.get_MaxLength() > 0 && value.Length > this.get_MaxLength())
value = value.Truncate(this.get_MaxLength());
int id = this.get_ID();
if(value != _textFieldTexts[id])
{
_textFieldTexts[id] = value;
if(this._caretIndex > value.Length)
this.set_CaretIndex(value.Length, false); // This also unsets selection
this.RenderText();
if(this.get_HasFocus())
this.RenderFocusSprite();
}
}
}
#endregion
bool PositionCaret(this TextField*, int x, int y, bool doSelect)
{
if(!this.get_Enabled())
return false;
// Get x,y relative to the string position
int id = this.get_ID();
Button* displayButton = _textDisplayButtons[id];
int xOffset = displayButton.X + displayButton.OwningGUI.X + this._paddingLeft;
int yOffset = displayButton.Y + displayButton.OwningGUI.Y + this._paddingTop;
x = x - xOffset;
if(x <= 0)
this.set_CaretIndex(0, doSelect);
else for(int caretIndex = _textFieldTexts[id].Length; caretIndex>0; caretIndex--)
{
String caretString = _textFieldTexts[id].Truncate(caretIndex);
int textWidth = GetTextWidth(caretString, _textDisplayButtons[id].Font);
if(x >= textWidth)
{
this.set_CaretIndex(caretIndex, doSelect);
return true;
}
}
this.set_CaretIndex(0, doSelect);
return true;
}
#region Module setup
void resizeArrays(static TextField, int newSize)
{
TextField* fields[] = new TextField[newSize];
String texts[] = new String[newSize];
Button* buttons[] = new Button[newSize];
DynamicSprite* txtSprites[] = new DynamicSprite[newSize];
DynamicSprite* brdSprites[] = new DynamicSprite[newSize];
for(int i=0; i<_textFieldSize; i++)
{
fields[i] = _textFields[i];
texts[i] = _textFieldTexts[i];
buttons[i] = _textDisplayButtons[i];
txtSprites[i] = _textSprites[i];
brdSprites[i] = _borderSprites[i];
}
_textFields = fields;
_textFieldTexts = texts;
_textDisplayButtons = buttons;
_textSprites = txtSprites;
_borderSprites = brdSprites;
_textFieldSize = newSize;
}
// Init
function game_start()
{
TextField.resizeArrays(TEXTFIELD_DEFAULT_COUNT);
}
TextField* Create(static TextField, Button* textDisplay, String text, int paddingLeft, int paddingTop) //, Button* caretDisplay)
{
// Must provide a button
if(textDisplay == null)
return null;
// Increase array sizes if we go over
if(_textFieldCount >= _textFieldSize)
{
TextField.resizeArrays(_textFieldSize*2);
}
if(text == null)
text = textDisplay.Text;
_textFieldCount++;
TextField* newField = new TextField;
newField._setId(_textFieldCount);
newField._setPaddingLeft(paddingLeft);
newField._setPaddingTop(paddingTop);
int id = newField.get_ID();
_textFields[id] = newField;
_textDisplayButtons[id] = textDisplay;
_textFieldTexts[id] = text;
_borderSprites[id] = DynamicSprite.Create(textDisplay.Width, textDisplay.Height, true);
newField.RenderBorder();
_textSprites[id] = DynamicSprite.Create(textDisplay.Width, textDisplay.Height, true);
newField.RenderText();
textDisplay.NormalGraphic = _textSprites[id].Graphic;
textDisplay.MouseOverGraphic = 0;
textDisplay.PushedGraphic = 0;
textDisplay.Text = "";
newField.set_CaretIndex(text.Length, false);
if(text.Length == 0)
newField.calculateCaretPos();
newField.set_Enabled(textDisplay.Enabled);
return newField;
}
#endregion
TextField* FindByDisplayButton(static TextField, Button* textDisplayButton)
{
for(int i=0; i<_textFieldCount;i++)
{
if(_textDisplayButtons[i] == textDisplayButton)
return _textFields[i];
}
return null;
}
TextField* FindByID(static TextField, int id)
{
if(id>=0 && id<_textFieldCount)
return _textFields[id];
return null;
}
void UpdateDisplay(this TextField*, bool cancelSelection)
{
this.calculateCaretPos();
if(cancelSelection)
this._selectionStartX = this._caretX;
this.RenderText();
this.RenderFocusSprite();
}
bool HandleKeyPress(this TextField*, eKeyCode keycode)
{
if(!this._isValid())
return false;
int id = this.get_ID();
switch(keycode)
{
case eKeyCtrlC:
{
#ifdef CLIPBOARD_PLUGIN
if(this._selectionStartIndex != this._caretIndex)
{
int i1 = _min(this._selectionStartIndex, this._caretIndex);
int i2 = _max(this._selectionStartIndex, this._caretIndex);
String s = this.get_Text();
s = s.Substring(i1, i2-i1);
Clipboard.CopyText(s);
}
return true;
#endif
#ifndef CLIPBOARD_PLUGIN
return false;
#endif
}
case eKeyCtrlX:
{
#ifdef CLIPBOARD_PLUGIN
if(this._selectionStartIndex != this._caretIndex)
{
// Copy...
int i1 = _min(this._selectionStartIndex, this._caretIndex);
int i2 = _max(this._selectionStartIndex, this._caretIndex);
String s = this.get_Text();
s = s.Substring(i1, i2-i1);
if(Clipboard.CopyText(s))
{
// ... and delete
_textFieldTexts[id] = _textFieldTexts[id].DeleteSubstring(i1, i2);
this._caretIndex = i1;
this._selectionStartIndex = i1;
this.UpdateDisplay(true);
}
}
return true;
#endif
#ifndef CLIPBOARD_PLUGIN
return false;
#endif
}
case eKeyCtrlV:
{
#ifdef CLIPBOARD_PLUGIN
String s = Clipboard.PasteText();
if(!String.IsNullOrEmpty(s))
{
String newString = _textFieldTexts[id];
if(this._selectionStartIndex != this._caretIndex)
{
int i1 = _min(this._selectionStartIndex, this._caretIndex);
int i2 = _max(this._selectionStartIndex, this._caretIndex);
newString = newString.DeleteSubstring(i1, i2);
this._caretIndex = i1;
}
newString = newString.InsertString(s, this._caretIndex);
if(this.get_MaxLength() > 0 && newString.Length > this.get_MaxLength())
newString = newString.Truncate(this.get_MaxLength());
_textFieldTexts[id] = newString;
this._caretIndex += s.Length;
if(this._caretIndex > newString.Length)
this._caretIndex = newString.Length;
this._selectionStartIndex = this._caretIndex;
this.UpdateDisplay(true);
}
return true;
#endif
#ifndef CLIPBOARD_PLUGIN
return false;
#endif
}
case eKeyReturn:
if(_handlesReturn)
{
this._activated = true;
return true;
}
return false;
case eKeyLeftArrow:
this.set_CaretIndex(this._caretIndex-1, IsShiftPressed());
return true;
case eKeyRightArrow:
this.set_CaretIndex(this._caretIndex+1, IsShiftPressed());
return true;
case eKeyBackspace:
// Delete selection
if(this._selectionStartIndex != this._caretIndex)
{
int i1 = _min(this._selectionStartIndex, this._caretIndex);
int i2 = _max(this._selectionStartIndex, this._caretIndex);
_textFieldTexts[id] = _textFieldTexts[id].DeleteSubstring(i1, i2);
this._caretIndex = i1;
this._selectionStartIndex = i1;
this.UpdateDisplay(true);
}
// Delete character
else if(this._caretIndex>0)
{
_textFieldTexts[id] = _textFieldTexts[id].DeleteChar(this._caretIndex);
this._caretIndex--;
this._selectionStartIndex = this._caretIndex;
this.UpdateDisplay(true);
}
return true;
case eKeyDelete:
// Delete selection
if(this._selectionStartIndex != this._caretIndex)
{
int i1 = _min(this._selectionStartIndex, this._caretIndex);
int i2 = _max(this._selectionStartIndex, this._caretIndex);
_textFieldTexts[id] = _textFieldTexts[id].DeleteSubstring(i1, i2);
this._caretIndex = i1;
this._selectionStartIndex = i1;
this.UpdateDisplay(true);
}
// Delete character
else if(this._caretIndex < _textFieldTexts[id].Length)
{
_textFieldTexts[id] = _textFieldTexts[id].DeleteChar(this._caretIndex+1);
this.RenderText();
this.RenderFocusSprite();
}
return true;
default:
if(keycode >= 32 && keycode<256)
{
// Delete (overwrite) selection
if(this._selectionStartIndex != this._caretIndex)
{
int i1 = _min(this._selectionStartIndex, this._caretIndex);
int i2 = _max(this._selectionStartIndex, this._caretIndex);
_textFieldTexts[id] = _textFieldTexts[id].DeleteSubstring(i1, i2);
this._caretIndex = i1;
this._selectionStartIndex = i1;
}
if(this.get_MaxLength() <= 0 || _textFieldTexts[id].Length < this.get_MaxLength())
{
// AGS always reports A-Z buttons as uppercase, so we check if (Shift XOR CapsLock) is off, and if so shift to the lowercase range
if(keycode >= 'A' && keycode <= 'Z' && ((IsShiftPressed() || _wasShiftPressed ) == System.CapsLock))
keycode += 32; // 32 = 'a' - 'A'
_textFieldTexts[id] = _textFieldTexts[id].InsertChar(keycode, this._caretIndex);
this._caretIndex++;
this._selectionStartIndex = this._caretIndex;
this.UpdateDisplay(true);
}
return true;
}
}
return false;
}
function on_key_press(eKeyCode keycode)
{
if(_focusedTextField != null && _focusedTextField.get_Enabled())
{
if(_focusedTextField.HandleKeyPress(keycode))
ClaimEvent();
}
}
bool HandleMouseClick(this TextField*, MouseButton button)
{
if(!this.get_Enabled())
return false;
switch(button)
{
case eMouseWheelNorth:
// do nothing
return true;
case eMouseWheelSouth:
// do nothing
return true;
case eMouseLeft:
default:
// If this was the end of a mouse drag, don't count as a click
if(_draggingTextField == this)
return true;
this.SetFocus();
this.PositionCaret(mouse.x, mouse.y);
return true;
}
}
bool HandleMouseClickAny(static TextField, GUIControl* control, MouseButton button)
{
Button* b = control.AsButton;
if(b == null)
return false;
TextField* tf = TextField.FindByDisplayButton(b);
if(tf == null)
return false;
return tf.HandleMouseClick(button);
}
function late_repeatedly_execute_always()
{
// Handle drag-to-select
if(Mouse.IsButtonDown(eMouseLeft))
{
if(!_wasButtonDown) // Mouse down
{
// Check whether we pressed down over an enabled Text Field
GUIControl* activeControl = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
if(activeControl != null)
{
Button* activeButton = activeControl.AsButton;
if(activeButton != null)
{
_draggingTextField = TextField.FindByDisplayButton(activeButton);
if(_draggingTextField != null)
{
if(_draggingTextField.get_Enabled()) // ... finally, yes!
{
_draggingTextField.SetFocus();
_draggingTextField.PositionCaret(mouse.x, mouse.y);
_dragLastX = mouse.x;
_dragLastY = mouse.y;
}
else
_draggingTextField = null;
}
}
}
} // !_wasButtonDown
else if(_draggingTextField != null) // Mouse dragging
{
if(mouse.x != _dragLastX)
{
// Recalculate selection
_draggingTextField.PositionCaret(mouse.x, mouse.y, true);
}
_dragLastX = mouse.x;
_dragLastY = mouse.y;
}
_wasButtonDown = true;
} // Mouse.IsButtonDown()
else // Mouse up
{
if(!_wasButtonDown)
_draggingTextField = null; // Keep _draggingTextField for another loop to potentially intercept mouseclick
_wasButtonDown = false;
}
// Because AGS only updates state every 1/40 seconds, the SHIFT key may already be released by the time we poll it.
// By also including the previous cycle, we get better results
_wasShiftPressed = IsShiftPressed();
if(_blinkTimer == 0 && _focusedTextField != null && _focusedTextField.get_Enabled())
{
int id = _focusedTextField.get_ID();
Button* displayButton = _textDisplayButtons[id];
if(displayButton.NormalGraphic == _focusedTextSpriteCaret.Graphic)
displayButton.NormalGraphic = _textSprites[id].Graphic;
else
displayButton.NormalGraphic = _focusedTextSpriteCaret.Graphic;
displayButton.PushedGraphic = displayButton.NormalGraphic;
}
_blinkTimer++;
_blinkTimer = _blinkTimer % _blinkDelay;
}
#endif // ENABLE_TEXTFIELD