-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Expand file tree
/
Copy pathEditControl.lua
More file actions
944 lines (909 loc) · 29.8 KB
/
Copy pathEditControl.lua
File metadata and controls
944 lines (909 loc) · 29.8 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
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
943
944
-- Path of Building
--
-- Class: Edit Control
-- Basic edit control.
--
local m_max = math.max
local m_min = math.min
local m_floor = math.floor
local protected_replace = "*"
local utf8 = require('lua-utf8')
local function lastLine(str)
local lastLineIndex = 1
while true do
local nextLine = str:find("\n", lastLineIndex, true)
if nextLine then
lastLineIndex = nextLine + 1
else
break
end
end
return str:sub(lastLineIndex, -1)
end
local function newlineCount(str)
local count = 0
local lastLineIndex = 1
while true do
local nextLine = str:find("\n", lastLineIndex, true)
if nextLine then
count = count + 1
lastLineIndex = nextLine + 1
else
return count
end
end
end
local function getColorCodeLength(str, index)
if str:sub(index, index) ~= "^" then
return 0
end
local nextChar = str:sub(index + 1, index + 1)
if nextChar == "x" and str:sub(index + 2, index + 7):match("^%x%x%x%x%x%x$") then
return 8
elseif nextChar:match("^%d$") then
return 2
end
return 0
end
local function buildVisibleLineMap(rawLine)
local visible = ""
local rawStarts = {}
local rawEnds = {}
local rawIndex = 1
while rawIndex <= #rawLine do
local colorCodeLength = getColorCodeLength(rawLine, rawIndex)
if colorCodeLength > 0 then
rawIndex = rawIndex + colorCodeLength
else
local rawEnd = utf8.next(rawLine, rawIndex, 1)
if not rawEnd or rawEnd <= rawIndex then
rawEnd = rawIndex + 1
end
local char = rawLine:sub(rawIndex, rawEnd - 1)
local visibleStart = #visible + 1
visible = visible .. char
for offset = 0, #char - 1 do
rawStarts[visibleStart + offset] = rawIndex
rawEnds[visibleStart + offset] = rawEnd
end
rawIndex = rawEnd
end
end
return visible, rawStarts, rawEnds
end
local EditClass = newClass("EditControl", "ControlHost", "Control", "UndoHandler", "TooltipHost", function(self, anchor, rect, init, prompt, filter, limit, changeFunc, lineHeight, allowZoom, clearable)
self.ControlHost()
self.Control(anchor, rect)
self.UndoHandler()
self.TooltipHost()
self:SetText(init or "")
self.prompt = prompt
self.filter = filter or (main.unicode and "%c" or "^%w%p ")
self.filterPattern = "["..self.filter.."]"
self.limit = limit
self.changeFunc = changeFunc
self.lineHeight = lineHeight
self.defaultLineHeight = lineHeight
self.font = "VAR"
self.textCol = "^7"
self.inactiveCol = "^8"
self.disableCol = "^9"
self.selCol = "^0"
self.selBGCol = "^xBBBBBB"
self.searchBGFillCol = { 0.03, 0.03, 0.04, 0.78 }
self.searchFocusFillCol = { 0.03, 0.03, 0.04, 0.88 }
self.searchBGCol = { 0.58, 0.60, 0.64, 0.98 }
self.searchFocusBGCol = { 0.96, 0.97, 0.99, 1.00 }
self.searchQuery = ""
self.searchMatches = {}
self.searchMatchesByLine = {}
self.searchFocusIndex = nil
self.blinkStart = GetTime()
self.allowZoom = allowZoom
local function buttonSize()
local _, height = self:GetSize()
return height - 4
end
if self.filter == "%D" or self.filter == "^%-%d" then
-- Add +/- buttons for integer number edits
self.isNumeric = true
self.controls.buttonDown = new("ButtonControl", {"RIGHT",self,"RIGHT"}, {-2, 0, buttonSize, buttonSize}, "-", function()
self:OnKeyUp("DOWN")
end)
self.controls.buttonUp = new("ButtonControl", {"RIGHT",self.controls.buttonDown,"LEFT"}, {-1, 0, buttonSize, buttonSize}, "+", function()
self:OnKeyUp("UP")
end)
elseif clearable then
self.controls.buttonClear = new("ButtonControl", {"RIGHT",self,"RIGHT"}, {-2, 0, buttonSize, buttonSize}, "x", function()
self:SetText("", true)
end)
self.controls.buttonClear.shown = function() return #self.buf > 0 and self:IsMouseInBounds() end
end
self.controls.scrollBarH = new("ScrollBarControl", {"BOTTOMLEFT",self,"BOTTOMLEFT"}, {1, -1, 0, 14}, 60, "HORIZONTAL", true)
self.controls.scrollBarH.width = function()
local width, height = self:GetSize()
return width - (self.controls.scrollBarV.enabled and 16 or 2)
end
self.controls.scrollBarV = new("ScrollBarControl", {"TOPRIGHT",self,"TOPRIGHT"}, {-1, 1, 14, 0}, (lineHeight or 0) * 3, "VERTICAL", true)
self.controls.scrollBarV.height = function()
local width, height = self:GetSize()
return height - (self.controls.scrollBarH.enabled and 16 or 2)
end
if not lineHeight then
self.controls.scrollBarH.shown = false
self.controls.scrollBarV.shown = false
end
self.protected = false
end)
function EditClass:SetText(text, notify)
self.buf = tostring(text)
self.caret = #self.buf + 1
self.sel = nil
if notify and self.changeFunc then
self.changeFunc(self.buf)
end
self:ResetUndo()
end
function EditClass:SetPlaceholder(text, notify)
self.placeholder = tostring(text)
if notify and self.changeFunc then
self.changeFunc(self.placeholder, true)
end
end
function EditClass:SetProtected(bool)
self.protected = bool or true
-- set the font to be fixed to prevent strange
-- spacing
self.font = "FIXED"
end
function EditClass:IsMouseOver()
if not self:IsShown() then
return false
end
return self:IsMouseInBounds() or self:GetMouseOverControl()
end
function EditClass:SelectAll()
self.caret = #self.buf + 1
self.sel = 1
self:ScrollCaretIntoView()
end
function EditClass:GetSelText()
local left = m_min(self.caret, self.sel)
local right = m_max(self.caret, self.sel)
local newBuf = self.buf:sub(left, right - 1)
return newBuf
end
function EditClass:ReplaceSel(text)
text = text:gsub("\r","")
if text:match(self.filterPattern) then
return
end
local left = m_min(self.caret, self.sel)
local right = m_max(self.caret, self.sel)
local newBuf = self.buf:sub(1, left - 1) .. text .. self.buf:sub(right)
if self.limit and #newBuf > self.limit then
return
end
self.buf = newBuf
self.caret = left + #text
self.sel = nil
self:ScrollCaretIntoView()
self.blinkStart = GetTime()
if self.changeFunc then
self.changeFunc(self.buf)
end
self:AddUndoState()
end
function EditClass:Insert(text)
text = text:gsub("\r","")
-- Remove any illegal chars from the "text" variable, to stop resulting in no text when an illegal character is found.
text = text:gsub(self.filterPattern,"")
if text == "" then
return
end
local newBuf = self.buf:sub(1, self.caret - 1) .. text .. self.buf:sub(self.caret)
if self.limit and #newBuf > self.limit then
return
end
self.buf = newBuf
self.caret = self.caret + #text
self.sel = nil
self:ScrollCaretIntoView()
self.blinkStart = GetTime()
if self.changeFunc then
self.changeFunc(self.buf)
end
self:AddUndoState()
end
function EditClass:ZoomText(zoom)
if not self.allowZoom or not self.lineHeight then
return
end
local textHeight = self.lineHeight
if zoom == "+" then
textHeight = textHeight + 1
elseif zoom == "-" then
textHeight = textHeight - 1
elseif zoom == "0" then
textHeight = self.defaultLineHeight
end
if textHeight < 10 then
textHeight = 10
elseif textHeight > 100 then
textHeight = 100
end
self.lineHeight = textHeight
end
function EditClass:UpdateScrollBars()
local width, height = self:GetSize()
local textHeight = self.lineHeight or (height - 4)
if self.lineHeight then
self.controls.scrollBarH:SetContentDimension(DrawStringWidth(textHeight, self.font, self.buf) + 2, width - 18)
self.controls.scrollBarV:SetContentDimension(newlineCount(self.buf.."\n") * textHeight, height - (self.controls.scrollBarH.enabled and 18 or 4))
else
self.controls.scrollBarH:SetContentDimension(DrawStringWidth(textHeight, self.font, self.buf) + 2, width - 4 - (self.prompt and DrawStringWidth(textHeight, self.font, self.prompt) + textHeight/2 or 0))
end
end
function EditClass:ScrollCaretIntoView()
local width, height = self:GetSize()
local textHeight = self.lineHeight or (height - 4)
local pre = self.buf:sub(1, self.caret - 1)
local caretX = DrawStringWidth(textHeight, self.font, lastLine(pre))
self:UpdateScrollBars()
self.controls.scrollBarH:ScrollIntoView(caretX - textHeight, textHeight * 2)
if self.lineHeight then
local caretY = newlineCount(pre) * textHeight
self.controls.scrollBarV:ScrollIntoView(caretY, textHeight)
end
end
function EditClass:MoveCaretVertically(offset)
local pre = self.buf:sub(1, self.caret - 1)
local caretX = DrawStringWidth(self.lineHeight, self.font, lastLine(pre))
local caretY = newlineCount(pre) * self.lineHeight
self.caret = DrawStringCursorIndex(self.lineHeight, self.font, self.buf, caretX + 1, caretY + self.lineHeight/2 + offset)
self.lastUndoState.caret = self.caret
self:ScrollCaretIntoView()
self.blinkStart = GetTime()
end
function EditClass:SetSearchQuery(query, centerFocused)
query = tostring(query or "")
local resetFocus = query ~= self.searchQuery
self.searchQuery = query
self:RefreshSearch(centerFocused, resetFocus)
end
function EditClass:AdvanceSearchMatch(direction)
local matchCount = #self.searchMatches
if matchCount == 0 then
return false
end
if direction and direction < 0 then
if not self.searchFocusIndex or self.searchFocusIndex <= 1 then
self.searchFocusIndex = matchCount
else
self.searchFocusIndex = self.searchFocusIndex - 1
end
else
if not self.searchFocusIndex or self.searchFocusIndex >= matchCount then
self.searchFocusIndex = 1
else
self.searchFocusIndex = self.searchFocusIndex + 1
end
end
self:CenterOnSearchMatch(self.searchFocusIndex)
return true
end
function EditClass:RefreshSearch(centerFocused, resetFocus)
local query = self.searchQuery or ""
local lowerQuery = query:lower()
local previousFocus = self.searchFocusIndex
self.searchMatches = {}
self.searchMatchesByLine = {}
self.searchFocusIndex = nil
if query == "" then
return
end
local lineIndex = 0
for s, line in (self.buf.."\n"):gmatch("()([^\n]*)\n") do
lineIndex = lineIndex + 1
local visibleLine, rawStarts, rawEnds = buildVisibleLineMap(line)
local searchLine = visibleLine:lower()
local searchStart = 1
while true do
local visibleStart, visibleEnd = searchLine:find(lowerQuery, searchStart, true)
if not visibleStart then
break
end
local rawStart = rawStarts[visibleStart]
local rawEnd = rawEnds[visibleEnd]
if rawStart and rawEnd then
local matchIndex = #self.searchMatches + 1
local match = {
index = matchIndex,
lineIndex = lineIndex,
line = line,
rawStart = rawStart,
rawEnd = rawEnd,
}
self.searchMatches[matchIndex] = match
self.searchMatchesByLine[lineIndex] = self.searchMatchesByLine[lineIndex] or {}
table.insert(self.searchMatchesByLine[lineIndex], match)
end
searchStart = visibleStart + 1
end
end
if #self.searchMatches > 0 then
if not resetFocus and previousFocus then
self.searchFocusIndex = m_min(previousFocus, #self.searchMatches)
else
self.searchFocusIndex = 1
end
if centerFocused then
self:CenterOnSearchMatch(self.searchFocusIndex)
end
end
end
function EditClass:CenterOnSearchMatch(matchIndex)
if not self.lineHeight then
return
end
local match = self.searchMatches[matchIndex]
if not match then
return
end
self:UpdateScrollBars()
if self.controls.scrollBarV.enabled then
local targetY = (match.lineIndex - 1) * self.lineHeight
self.controls.scrollBarV:SetOffset(targetY - (self.controls.scrollBarV.viewDim - self.lineHeight) / 2)
end
if self.controls.scrollBarH.enabled then
local matchStartX = DrawStringWidth(self.lineHeight, self.font, match.line:sub(1, match.rawStart - 1))
local matchWidth = DrawStringWidth(self.lineHeight, self.font, match.line:sub(match.rawStart, match.rawEnd - 1))
self.controls.scrollBarH:SetOffset(matchStartX + matchWidth / 2 - self.controls.scrollBarH.viewDim / 2)
end
end
function EditClass:DrawSearchHighlightsForLine(lineIndex, line, textX, textY, textHeight)
local matches = self.searchMatchesByLine[lineIndex]
if not matches then
return
end
for _, match in ipairs(matches) do
local matchStartX = DrawStringWidth(textHeight, self.font, line:sub(1, match.rawStart - 1))
local matchWidth = DrawStringWidth(textHeight, self.font, line:sub(match.rawStart, match.rawEnd - 1))
if matchWidth > 0 then
local isFocused = match.index == self.searchFocusIndex
local fillColor = isFocused and self.searchFocusFillCol or self.searchBGFillCol
local borderColor = isFocused and self.searchFocusBGCol or self.searchBGCol
local drawX = textX + matchStartX - 2
local drawWidth = matchWidth + 4
local borderX = drawX - 1
local borderY = textY - 1
local borderWidth = drawWidth + 2
local borderHeight = textHeight + 2
SetDrawColor(fillColor[1], fillColor[2], fillColor[3], fillColor[4])
DrawImage(nil, drawX, textY, drawWidth, textHeight)
SetDrawColor(borderColor[1], borderColor[2], borderColor[3], borderColor[4])
DrawImage(nil, borderX, borderY, borderWidth, 2)
DrawImage(nil, borderX, borderY + borderHeight - 2, borderWidth, 2)
DrawImage(nil, borderX, borderY, 2, borderHeight)
DrawImage(nil, borderX + borderWidth - 2, borderY, 2, borderHeight)
end
end
end
function EditClass:Draw(viewPort, noTooltip)
local x, y = self:GetPos()
local width, height = self:GetSize()
local enabled = self:IsEnabled()
local mOver = self:IsMouseOver()
if not enabled then
SetDrawColor(0.33, 0.33, 0.33)
elseif mOver then
SetDrawColor(1, 1, 1)
elseif self.borderFunc then
local r, g, b = self.borderFunc()
SetDrawColor(r, g, b)
else
SetDrawColor(0.5, 0.5, 0.5)
end
DrawImage(nil, x, y, width, height)
if not enabled then
SetDrawColor(0, 0, 0)
elseif self.hasFocus or mOver then
if self.lineHeight then
SetDrawColor(0.1, 0.1, 0.1)
else
SetDrawColor(0.15, 0.15, 0.15)
end
else
SetDrawColor(0, 0, 0)
end
DrawImage(nil, x + 1, y + 1, width - 2, height - 2)
local textX = x + 2
local textY = y + 2
local textHeight = self.lineHeight or (height - 4)
if self.prompt then
if not enabled then
DrawString(textX, textY, "LEFT", textHeight, self.font, self.disableCol..self.prompt)
else
DrawString(textX, textY, "LEFT", textHeight, self.font, self.textCol..self.prompt..":")
end
textX = textX + DrawStringWidth(textHeight, self.font, self.prompt) + textHeight/2
end
if not enabled then
return
end
if mOver and not noTooltip then
SetDrawLayer(nil, 100)
self:DrawTooltip(x, y, width, height, viewPort)
SetDrawLayer(nil, 0)
end
self:UpdateScrollBars()
local marginL = textX - x - 2
local marginR = self.controls.scrollBarV:IsShown() and 14 or 0
local marginB = self.controls.scrollBarH:IsShown() and 14 or 0
SetViewport(textX, textY, width - 4 - marginL - marginR, height - 4 - marginB)
if not self.hasFocus then
if self.buf == '' and self.placeholder then
SetDrawColor(self.disableCol)
DrawString(-self.controls.scrollBarH.offset, -self.controls.scrollBarV.offset, "LEFT", textHeight, self.font, self.placeholder)
else
SetDrawColor(self.inactiveCol)
if self.inactiveText then
local inactiveText = type(inactiveText) == "string" and self.inactiveText or self.inactiveText(self.buf)
DrawString(-self.controls.scrollBarH.offset, -self.controls.scrollBarV.offset, "LEFT", textHeight, self.font, inactiveText)
elseif self.lineHeight and #self.searchMatches > 0 then
local lineIndex = 0
local drawY = -self.controls.scrollBarV.offset
for line in (self.buf.."\n"):gmatch("([^\n]*)\n") do
lineIndex = lineIndex + 1
self:DrawSearchHighlightsForLine(lineIndex, line, -self.controls.scrollBarH.offset, drawY, textHeight)
SetDrawColor(self.inactiveCol)
DrawString(-self.controls.scrollBarH.offset, drawY, "LEFT", textHeight, self.font, line)
drawY = drawY + textHeight
end
elseif self.protected then
DrawString(-self.controls.scrollBarH.offset, -self.controls.scrollBarV.offset, "LEFT", textHeight, self.font, string.rep(protected_replace, #self.buf))
else
DrawString(-self.controls.scrollBarH.offset, -self.controls.scrollBarV.offset, "LEFT", textHeight, self.font, self.buf)
end
end
SetViewport()
self:DrawControls(viewPort, noTooltip and self)
return
end
if not IsKeyDown("LEFTBUTTON") then
self.drag = false
end
if self.drag then
local cursorX, cursorY = GetCursorPos()
self.caret = DrawStringCursorIndex(textHeight, self.font, self.buf, cursorX - textX + self.controls.scrollBarH.offset, cursorY - textY + self.controls.scrollBarV.offset)
self.lastUndoState.caret = self.caret
self:ScrollCaretIntoView()
end
textX = -self.controls.scrollBarH.offset
textY = -self.controls.scrollBarV.offset
if self.lineHeight then
local left = m_min(self.caret, self.sel or self.caret)
local right = m_max(self.caret, self.sel or self.caret)
local caretX
local lineIndex = 0
SetDrawColor(self.textCol)
for s, line, e in (self.buf.."\n"):gmatch("()([^\n]*)\n()") do
lineIndex = lineIndex + 1
textX = -self.controls.scrollBarH.offset
self:DrawSearchHighlightsForLine(lineIndex, line, textX, textY, textHeight)
SetDrawColor(self.textCol)
if left >= e or right <= s then
DrawString(textX, textY, "LEFT", textHeight, self.font, line)
end
if left < e then
if left > s then
local pre = line:sub(1, left - s)
DrawString(textX, textY, "LEFT", textHeight, self.font, pre)
textX = textX + DrawStringWidth(textHeight, self.font, pre)
end
if left >= s and left == self.caret then
caretX, caretY = textX, textY
end
end
if left ~= right and left < e and right > s then
local sel = self.selCol .. StripEscapes(line:sub(m_max(1, left - s + 1), m_min(#line, right - s)))
if right >= e then
sel = sel .. " "
end
local selWidth = DrawStringWidth(textHeight, self.font, sel)
SetDrawColor(self.selBGCol)
DrawImage(nil, textX, textY, selWidth, textHeight)
DrawString(textX, textY, "LEFT", textHeight, self.font, sel)
SetDrawColor(self.textCol)
textX = textX + selWidth
end
if right >= s and right < e and right == self.caret then
caretX, caretY = textX, textY
end
if right > s then
if right < e then
local post = line:sub(right - s + 1)
DrawString(textX, textY, "LEFT", textHeight, self.font, post)
textX = textX + DrawStringWidth(textHeight, self.font, post)
end
end
textY = textY + textHeight
end
if caretX then
if (GetTime() - self.blinkStart) % 1000 < 500 then
SetDrawColor(self.textCol)
DrawImage(nil, caretX, caretY, 1, textHeight)
end
end
elseif self.sel and self.sel ~= self.caret then
local left = m_min(self.caret, self.sel)
local right = m_max(self.caret, self.sel)
local pre = self.textCol .. self.buf:sub(1, left - 1)
local sel = self.selCol .. StripEscapes(self.buf:sub(left, right - 1))
local post = self.textCol .. self.buf:sub(right)
if self.protected then
DrawString(textX, textY, "LEFT", textHeight, self.font, self.textCol .. string.rep(protected_replace, #pre-#self.textCol))
else
DrawString(textX, textY, "LEFT", textHeight, self.font, pre)
end
textX = textX + DrawStringWidth(textHeight, self.font, pre)
local selWidth = DrawStringWidth(textHeight, self.font, sel)
SetDrawColor(self.selBGCol)
DrawImage(nil, textX, textY, selWidth, textHeight)
if self.protected then
DrawString(textX, textY, "LEFT", textHeight, self.font, self.selCol .. string.rep(protected_replace, #sel-#self.selCol))
else
DrawString(textX, textY, "LEFT", textHeight, self.font, sel)
end
if self.protected and #post > 0 then
DrawString(textX + selWidth, textY, "LEFT", textHeight, self.font, self.textCol .. string.rep(protected_replace, #post-#self.textCol))
else
DrawString(textX + selWidth, textY, "LEFT", textHeight, self.font, post)
end
if (GetTime() - self.blinkStart) % 1000 < 500 then
local caretX = (self.caret > self.sel) and textX + selWidth or textX
SetDrawColor(self.textCol)
DrawImage(nil, caretX, textY, 1, textHeight)
end
else
local pre = self.textCol .. self.buf:sub(1, self.caret - 1)
local post = self.buf:sub(self.caret)
if self.protected then
DrawString(textX, textY, "LEFT", textHeight, self.font, self.textCol .. string.rep(protected_replace, #pre-#self.textCol))
else
DrawString(textX, textY, "LEFT", textHeight, self.font, pre)
end
textX = textX + DrawStringWidth(textHeight, self.font, pre)
if self.protected and #post > 0 then
DrawString(textX, textY, "LEFT", textHeight, self.font, string.rep(protected_replace, #post))
else
DrawString(textX, textY, "LEFT", textHeight, self.font, post)
end
if (GetTime() - self.blinkStart) % 1000 < 500 then
SetDrawColor(self.textCol)
DrawImage(nil, textX, textY, 1, textHeight)
end
end
SetViewport()
self:DrawControls(viewPort, noTooltip and self)
end
function EditClass:OnFocusGained()
self.blinkStart = GetTime()
if not self.drag and not self.selControl and not self.lineHeight then
self:SelectAll()
end
end
function EditClass:OnKeyDown(key, doubleClick)
if not self:IsShown() or not self:IsEnabled() then
return
end
local mOverControl = self:GetMouseOverControl()
if mOverControl and mOverControl.OnKeyDown then
self.selControl = mOverControl
return mOverControl:OnKeyDown(key) and self
else
self.selControl = nil
end
local shift = IsKeyDown("SHIFT")
local ctrl = IsKeyDown("CTRL")
if key == "LEFTBUTTON" then
if not self.Object:IsMouseOver() then
return
end
if doubleClick then
if self.lineHeight then
if self.buf:sub(self.caret - 1, self.caret):match("^%C\n$") then
self.caret = self.caret - 1
end
while self.buf:sub(self.caret - 1, self.caret):match("[^\n][ \t]") do
self.caret = self.caret - 1
end
local caretChar = self.buf:sub(self.caret, self.caret)
if caretChar:match("%w") then
self.sel = self.caret
while self.buf:sub(self.sel - 1, self.sel - 1):match("%w") do
self.sel = self.sel - 1
end
while self.buf:sub(self.caret, self.caret):match("%w") do
self.caret = self.caret + 1
end
elseif caretChar:match("%S") then
self.sel = self.caret
while self.buf:sub(self.sel - 1, self.sel - 1) == caretChar do
self.sel = self.sel - 1
end
while self.buf:sub(self.caret, self.caret) == caretChar do
self.caret = self.caret + 1
end
end
else
self.sel = 1
self.caret = #self.buf + 1
end
self.lastUndoState.caret = self.caret
self:ScrollCaretIntoView()
elseif ctrl and string.match(self.buf, '[a-z]*://[^ >,;]*') then
OpenURL(self.buf)
else
self.drag = true
local x, y = self:GetPos()
local width, height = self:GetSize()
local textX = x + 2
local textY = y + 2
local textHeight = self.lineHeight or (height - 4)
if self.prompt then
textX = textX + DrawStringWidth(textHeight, self.font, self.prompt) + textHeight/2
end
local cursorX, cursorY = GetCursorPos()
self.caret = DrawStringCursorIndex(textHeight, self.font, self.buf, cursorX - textX + self.controls.scrollBarH.offset, cursorY - textY + self.controls.scrollBarV.offset)
self.sel = self.caret
self.lastUndoState.caret = self.caret
self:ScrollCaretIntoView()
self.blinkStart = GetTime()
end
elseif key == "ESCAPE" then
return
elseif key == "RETURN" then
if self.lineHeight then
self:Insert("\n")
else
if self.enterFunc then
self.enterFunc(self.buf)
end
return self
end
elseif key == "a" and ctrl then
self:SelectAll()
elseif (key == "c" or key == "x") and ctrl and not self.protected then
if self.sel and self.sel ~= self.caret then
local left = m_min(self.caret, self.sel)
local right = m_max(self.caret, self.sel)
Copy(self.buf:sub(left, right - 1))
if key == "x" then
self:ReplaceSel("")
end
end
elseif key == "v" and ctrl or key == "RIGHTBUTTON" and self.Object:IsMouseOver() then
local text = Paste()
if text then
if self.pasteFilter then
text = self.pasteFilter(text)
end
text = text:gsub("[\128-\255]","?")
if self.sel and self.sel ~= self.caret then
self:ReplaceSel(text)
else
self:Insert(text)
end
end
elseif key == "z" and ctrl then
self:Undo()
elseif key == "y" and ctrl then
self:Redo()
elseif key == "LEFT" then
self.sel = shift and (self.sel or self.caret) or nil
if self.caret > 1 then
if ctrl then
-- Skip leading space, then jump word
self.caret = self.caret - #utf8.match(self.buf:sub(1, self.caret-1), "[%s%p]*$")
self.caret = self.caret - #utf8.match(self.buf:sub(1, self.caret-1), "%w*$")
else
self.caret = utf8.next(self.buf, self.caret, -1) or 0
end
self.lastUndoState.caret = self.caret
self:ScrollCaretIntoView()
self.blinkStart = GetTime()
end
elseif key == "RIGHT" then
self.sel = shift and (self.sel or self.caret) or nil
if self.caret <= #self.buf then
if ctrl then
-- Jump word, then skip trailing space,
self.caret = self.caret + #utf8.match(self.buf:sub(self.caret), "^%w*")
self.caret = self.caret + #utf8.match(self.buf:sub(self.caret), "^[%s%p]*")
else
self.caret = utf8.next(self.buf, self.caret, 1) or #self.buf + 1
end
self.lastUndoState.caret = self.caret
self:ScrollCaretIntoView()
self.blinkStart = GetTime()
end
elseif key == "UP" and self.lineHeight then
self.sel = shift and (self.sel or self.caret) or nil
self:MoveCaretVertically(-self.lineHeight)
elseif key == "DOWN" and self.lineHeight then
self.sel = shift and (self.sel or self.caret) or nil
self:MoveCaretVertically(self.lineHeight)
elseif key == "HOME" then
self.sel = shift and (self.sel or self.caret) or nil
if self.lineHeight and not ctrl then
self.caret = self.caret - #lastLine(self.buf:sub(1, self.caret - 1))
else
self.caret = 1
end
self.lastUndoState.caret = self.caret
self:ScrollCaretIntoView()
self.blinkStart = GetTime()
elseif key == "END" then
self.sel = shift and (self.sel or self.caret) or nil
if self.lineHeight and not ctrl then
self.caret = self.caret + #self.buf:sub(self.caret, -1):match("[^\n]*")
else
self.caret = #self.buf + 1
end
self.lastUndoState.caret = self.caret
self:ScrollCaretIntoView()
self.blinkStart = GetTime()
elseif key == "PAGEUP" and self.lineHeight then
self.sel = shift and (self.sel or self.caret) or nil
local width, height = self:GetSize()
self:MoveCaretVertically(-height + 18)
elseif key == "PAGEDOWN" and self.lineHeight then
self.sel = shift and (self.sel or self.caret) or nil
local width, height = self:GetSize()
self:MoveCaretVertically(height - 18)
elseif key == "BACK" then
if self.sel and self.sel ~= self.caret then
self:ReplaceSel("")
elseif self.caret > 1 then
local len = 1
if IsKeyDown("CTRL") then
while self.caret - len > 1 and self.buf:sub(self.caret - len, self.caret - len):match("%s") and not self.buf:sub(self.caret - len - 1, self.caret - len - 1):match("\n") do
len = len + 1
end
if self.buf:sub(self.caret - len, self.caret - len):match("%w") then
while self.caret - len > 1 and self.buf:sub(self.caret - len - 1, self.caret - len - 1):match("%w") do
len = len + 1
end
end
end
self.buf = self.buf:sub(1, self.caret - 1 - len) .. self.buf:sub(self.caret)
self.caret = self.caret - len
self.sel = nil
self:ScrollCaretIntoView()
self.blinkStart = GetTime()
if self.changeFunc then
self.changeFunc(self.buf)
end
self:AddUndoState()
end
elseif key == "DELETE" then
if self.sel and self.sel ~= self.caret then
self:ReplaceSel("")
elseif self.caret <= #self.buf then
local len = 1
if IsKeyDown("CTRL") then
while self.caret + len <= #self.buf and self.buf:sub(self.caret + len - 1, self.caret + len - 1):match("%s") and not self.buf:sub(self.caret + len, self.caret + len):match("\n") do
len = len + 1
end
if self.buf:sub(self.caret + len - 1, self.caret + len - 1):match("%w") then
while self.caret + len <= #self.buf and self.buf:sub(self.caret + len, self.caret + len):match("%w") do
len = len + 1
end
end
end
self.buf = self.buf:sub(1, self.caret - 1) .. self.buf:sub(self.caret + len)
self.sel = nil
self.blinkStart = GetTime()
if self.changeFunc then
self.changeFunc(self.buf)
end
self:AddUndoState()
end
elseif key == "TAB" then
return self.Object:TabAdvance(shift and -1 or 1)
elseif (key == "+" or key == "-" or key == "0") and ctrl and self.allowZoom then
self:ZoomText(key)
end
return self
end
function EditClass:OnKeyUp(key)
if not self:IsShown() or not self:IsEnabled() then
return
end
if self.selControl then
local newSel = self.selControl:OnKeyUp(key)
if newSel then
return self
else
self.selControl = nil
end
end
local ctrl = IsKeyDown("CTRL")
if key == "LEFTBUTTON" then
if self.drag then
self.drag = false
end
elseif self.isNumeric then
local cur = tonumber(self.buf)
if key == "WHEELUP" or key == "UP" then
if cur then
self:SetText(tostring(cur + (self.numberInc or 1)), true)
else
if self.placeholder then
self:SetText(tostring((tonumber(self.placeholder) or 0) + (self.numberInc or 1)), true)
else
self:SetText("1", true)
end
end
elseif key == "WHEELDOWN" or key == "DOWN" then
if cur and (self.filter ~= "%D" or cur > 0)then
self:SetText(tostring(cur - (self.numberInc or 1)), true)
else
if self.placeholder then
self:SetText(tostring((tonumber(self.placeholder) or 0) - (self.numberInc or 1)), true)
else
self:SetText("0", true)
end
end
end
elseif self.controls.scrollBarV:IsScrollUpKey(key) then
if ctrl and self.allowZoom then
self:ZoomText("+")
elseif self.controls.scrollBarV.enabled then
self.controls.scrollBarV:Scroll(-1)
else
self.controls.scrollBarH:Scroll(-1)
end
elseif self.controls.scrollBarV:IsScrollDownKey(key) then
if ctrl and self.allowZoom then
self:ZoomText("-")
elseif self.controls.scrollBarV.enabled then
self.controls.scrollBarV:Scroll(1)
else
self.controls.scrollBarH:Scroll(1)
end
end
return self.hasFocus and self
end
function EditClass:OnChar(key)
if not self:IsShown() or not self:IsEnabled() then
return
end
if key ~= '\b' then
if self.sel and self.sel ~= self.caret then
self:ReplaceSel(key)
else
self:Insert(key)
end
end
return self
end
function EditClass:CreateUndoState()
local state = {
buf = self.buf,
caret = self.caret,
}
self.lastUndoState = state
return state
end
function EditClass:RestoreUndoState(state)
self.buf = state.buf
self.caret = state.caret
self.sel = nil
self:ScrollCaretIntoView()
if self.changeFunc then
self.changeFunc(self.buf)
end
self.lastUndoState = state
end