-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextInput.lua
More file actions
602 lines (496 loc) · 19.1 KB
/
TextInput.lua
File metadata and controls
602 lines (496 loc) · 19.1 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
if not gui then
gui = {}
end
gui.textInput = class("gui.textInput")
function gui.textInput:created(x)
self.entity.hitTest = true
self.entity.hoverTest = true
self.padding = vec2(5)
self.textArea = self.entity:child("textArea")
self.textArea.size = self.entity.size - self.padding * 2
self.textObject = self.textArea:child("label")
self.textObject.size = self.entity.size
self.label = self.textObject:add(gui.label, true)
--self.textObject.size = self.entity.size
self.textObject.pivot = vec2(0, 0.5)
self.textArea.clip = true
self.style = {
cursorFlickerColor = color(117, 214, 226),
cursorSelectColor = color(117, 194, 226, 86),
selectedColor = color(101),
normalColor = color(128),
hoverColor = color(137),
hoverSelectedColor = color(148),
typingColor = color(255),
placeHolderColor = color(203, 206, 207)
}
self.textAlign = CENTER
self.typingText = ""
self.oldTypingText = ""
self.placeHolderText = "Enter your name:"
self.textObject:anchor(self.textAlign, MIDDLE)
self.isTyping = false
self.beginSelect = false
self.cursor = self.textArea:child("cursor")
self.cursorSize = vec2(3, self.entity.size.y - 2* self.padding.y)
self.cursor.size = vec2(self.cursorSize:unpack())
self.cursor.sprite = asset.Square
self.cursor.color = self.style.cursorFlickerColor
self.flickerOffset = 0.4
self.startFlickerTime = nil
self.pressedFirstOffset = 0.5
self.pressedOffset = 0.05
self.canType = false
self.futurePressedTime = nil
self.shouldReposition = false
self.beginCharPos = nil
self.endCharPos = nil
self.lastTouchTime = num
self.touchOffsetTime = 0.35
self.numTaps = 0
--self.offsetInside
self.isPressing = false
self.charPos = nil
self.canEdit = true
end
function gui.textInput:start()
self.oldTypingText = self.typingText
self.cursorSize = vec2(3, self.entity.size.y - 2* self.padding.y)
self.cursor.size = vec2(self.cursorSize:unpack())
self.textObject:anchor(self.textAlign, MIDDLE)
self:update()
self:layout()
end
function gui.textInput:getChar(num)
style.fontSize(self.label.fontSize)
style.textAlign(self.label.align).textStyle(self.label.style)
style.strokeWidth(self.label.borderWidth).stroke(self.label.borderColor)
style.textAlign(self.label.align)
style.font(self.label.font)
local lastText = string.sub(self.typingText, 1, num-1)
local w, h = textSize(lastText)
local currentChar = string.sub(self.typingText, 1, num)
local cW, cH = textSize(currentChar)
local x = self.textObject.worldPosition.x + w
local y = self.textObject.worldPosition.y + h
return vec2(x, y), vec2(cW-w, cH-h)
end
function gui.textInput:whichChar(pos)
local charPos = 0
for k = 1, #self.typingText do
local p, size = self:getChar(k)
local nP, nSize = self:getChar(k+1)
if k == 1 and pos.x < p.x + size.x/2 then
charPos = 0
break
end
if k == #self.typingText then
if p.x + size.x/2 <= pos.x then
charPos = k
end
else
if p.x + size.x/2 <= pos.x and pos.x <= nP.x + nSize.x /2 then
charPos = k
break
end
end
end
return charPos
end
function gui.textInput:willDeselect()
if self.charPos ~= nil and ((CurrentTouch.began and not gui.wasTouched(self.entity)) or gui.textInput.selectedEntity ~= self.entity) then
self.charPos = nil
gui.textInput.selectedEntity = nil
self.entity:dispatch("onDeselect", self.typingText, self.oldTypingText, class)
end
end
function gui.textInput:update()
self:checkHover()
self:handleState()
self.isTyping = false
if self.charPos then self.isTyping = true end
if self.charPos then
self:handleCursor()
self:handleKeyboard()
self:willDeselect()
else
if gui.textInput.selectedEntity and not gui.textInput.selectedEntity.valid then
gui.textInput.selectedEntity = nil
end
self.cursor.active = false
end
self.textObject:anchor(self.textAlign, MIDDLE)
self.textArea.size = self.entity.size - self.padding * 2
self:displayText()
end
function gui.textInput:checkHover()
if mouse and mouse.active then
if gui.wasHovered(self.entity) then
if not self.hovered then
self.entity:dispatch("onMouseEnter", self)
end
self.entity:dispatch('hovering', self)
self.hovered = true
else
if self.hovered then
self.entity:dispatch('onMouseExit', self)
end
self.hovered = false
end
end
end
function gui.textInput:layout()
if self.charPos then
self:handleMoveOut()
end
self:repositionText()
end
function gui.textInput:displayText()
if #self.typingText ~= 0 then
self.label.text = self.typingText
self.label.color = self.style.typingColor
else
self.label.text = self.placeHolderText
self.label.color = self.style.placeHolderColor
end
end
function gui.textInput:handleState()
self.entity.color = ((self.charPos ~= nil and self.hovered) and self.style.hoverSelectedColor) or (self.charPos ~= nil and self.style.selectedColor) or (self.hovered and self.style.hoverColor) or self.style.normalColor
end
function gui.textInput:handleMoveOut()
local p, s = self:getChar(self.charPos)
if p.x - (self.entity.worldPosition.x + self.entity.size.x - s.x) > -self.padding.x then
local offset = p.x - (self.entity.worldPosition.x + self.entity.size.x - s.x - self.padding.x)
self.textObject.x = self.textObject.x - offset - self.cursorSize.x/2
end
p, s = self:getChar(self.charPos + 1)
if p.x - self.entity.worldPosition.x < self.padding.x then
local offset = p.x - self.entity.worldPosition.x - self.padding.x
self.textObject.x = self.textObject.x - offset + self.cursorSize.x/2
end
end
function gui.textInput:handleKeyboard()
if self.futurePressedTime and self.futurePressedTime <= time.elapsed then
self.canType = true
self.futurePressedTime = self.futurePressedTime + self.pressedOffset
end
for k, character in pairs(typeCharactersList) do
self:canPress(k,function()
local char = (key.modifiers(key.shift) > 0 and key.modifiers(key.shift)&key.shift == key.shift) and character[2] or character[1]
if self.endCharPos and key.modifiers(key.cmd)&key.cmd == key.cmd and (k == "c" or k == "x") then
self:copyText()
if k == "x" then
self:deleteText()
end
return
end
if key.modifiers(key.cmd)&key.cmd == key.cmd and k == "v" then
self:pasteText()
return
end
if self.endCharPos then
self:deleteText()
end
local back = string.sub(self.typingText, 1, self.charPos)
local front = string.sub(self.typingText, self.charPos+1, #self.typingText)
self.typingText = back..char..front
self.charPos = self.charPos + 1
self.entity:dispatch('onTextChanged', self.typingText)
end)
end
self:canPress("backspace",function()
self:deleteText()
self.entity:dispatch('onTextChanged', self.typingText)
end)
self:canPress("delete",function()
if self.charPos ~= #self.typingText then
if self.endCharPos then
self:deleteText()
return
end
local back = string.sub(self.typingText, 1, self.charPos)
local front = string.sub(self.typingText, self.charPos+2, #self.typingText)
local p, s = self:getChar(self.charPos)
self.typingText = back..front
self.charPos = self.charPos
self.entity:dispatch('onTextChanged', self.typingText)
end
end)
self:canPress("enter",function()
self:deselect()
self.entity:dispatch('onEnter', self.typingText, self.oldTypingText)
end)
self:canPress("esc",function()
self:deselect()
self.entity:dispatch('onEscape', self.typingText)
self.typingText = self.oldTypingText
end)
self:canPress("left",function()
if self.endCharPos then
local beginPos, endPos = self:getSelectNumbers()
self.charPos = beginPos
self.endCharPos = nil
else
self.charPos = self.charPos-1
if self.charPos < 0 then
self.charPos = 0
end
end
end)
self:canPress("right",function()
if self.endCharPos then
local beginPos, endPos = self:getSelectNumbers()
self.charPos = endPos
self.endCharPos = nil
else
self.charPos = self.charPos+1
if self.charPos > #self.typingText then
self.charPos = #self.typingText
end
end
end)
self.canType = false
end
function gui.textInput:pasteText()
local paste = objc.UIPasteboard.generalPasteboard_()
if self.endCharPos then
self:deleteText()
end
local back = string.sub(self.typingText, 1, self.charPos)
local front = string.sub(self.typingText, self.charPos+1, #self.typingText)
self.typingText = back..paste.string..front
self.charPos = self.charPos + #paste.string
end
function gui.textInput:copyText()
local beginPos, endPos = self:getSelectNumbers()
local str = string.sub(self.typingText, beginPos + 1, endPos)
local paste = objc.UIPasteboard.generalPasteboard_()
paste.string = str
end
function gui.textInput:deleteText()
if self.charPos ~= 0 or self.endCharPos ~= nil then
local back, front = self:backSpaceValues()
local p, s = self:getChar(self.charPos)
self.typingText = back..front
--self.textObject.x = self.textObject.x + s.x
end
end
function gui.textInput:backSpaceValues()
local back = nil
local front = nil
if self.endCharPos == nil then
back = string.sub(self.typingText, 0, self.charPos-1)
front = string.sub(self.typingText, self.charPos+1, #self.typingText)
self.charPos = self.charPos-1
else
local beginPos, endPos = self:getSelectNumbers()
back = string.sub(self.typingText, 1, beginPos)
front = string.sub(self.typingText, endPos+1, #self.typingText)
self.charPos = beginPos
self.endCharPos = nil
end
return back, front
end
function gui.textInput:canPress(keyName, extraFunction)
if key.wasPressed(key[keyName]) or (key.pressing(key[keyName]) and self.canType)then
if key.wasPressed(key[keyName]) then
self.futurePressedTime = time.elapsed + self.pressedFirstOffset
end
extraFunction()
self.startFlickerTime = time.elapsed
self.shouldReposition = true
end
end
function gui.textInput:repositionText()
--local p, s = self:getChar(self.charPos)
if self.textObject.size.x > self.textArea.size.x then
if self.textObject.worldPosition.x > self.textArea.worldPosition.x + self.cursor.size.x/2 then
self.textObject.x = self.textObject.x - (self.textObject.worldPosition.x - self.textArea.worldPosition.x) + self.cursorSize.x/2
end
if self.textObject.worldPosition.x + self.textObject.size.x < self.textArea.worldPosition.x + self.textArea.size.x then
self.textObject.x = self.textObject.x + ((self.textArea.worldPosition.x + self.textArea.size.x )- (self.textObject.worldPosition.x + self.textObject.size.x)) - self.cursorSize.x/2
end
else
if self.textAlign == LEFT then
self.textObject.x = self.cursorSize.x/2
elseif self.textAlign == CENTER then
self.textObject.x = -self.textObject.size.x/2
elseif self.textAlign == RIGHT then
self.textObject.x = -self.textObject.size.x - self.cursorSize.x/2
end
end
end
function gui.textInput:handleCursor()
if self.endCharPos == nil then
self:handleFlicker()
else
self:handleSelect()
end
end
function gui.textInput:handleSelect()
local beginPos, endPos = self:getSelectNumbers()
self.cursor.active = true
local bPos, bSize = self:getChar(beginPos+1)
if beginPos == 0 then
bPos, bSize = vec2(self.textObject.worldPosition.x,0), vec2(0)
end
self.cursor.color = self.style.cursorSelectColor
local ePos, eSize = self:getChar(endPos)
local selectSize = (ePos.x + eSize.x) - bPos.x
self.cursor.size = vec2(selectSize, self.cursorSize.y)
local x = bPos.x + self.cursor.size.x/2
self.cursor.worldPosition = vec3(x,self.cursor.worldPosition.y, 0) + vec3(0, self.cursor.size.y/2, 0)
end
function gui.textInput:getSelectNumbers()
local beginPos, endPos = self.beginCharPos, self.endCharPos
if endPos then
if beginPos > endPos then
local hold = endPos
endPos = beginPos
beginPos = hold
end
end
return beginPos, endPos
end
function gui.textInput:deselect()
self.charPos = nil
self.beginCharPos = nil
self.endCharPos = nil
end
function gui.textInput:handleFlicker()
local timeOffset = time.elapsed - self.startFlickerTime
self.cursor.color = self.style.cursorFlickerColor
self.cursor.size = self.cursorSize
self.cursor.active = math.floor(timeOffset / self.flickerOffset) % 2 == 0
if self.charPos == 0 then
local sX, sY = (self.cursor.size/2):unpack()
self.cursor.worldPosition = vec3(self.textObject.worldPosition.x - self.cursorSize.x/2,self.cursor.worldPosition.y, 0) + vec3(self.cursor.size.x/2, sY, 0)
if #self.typingText == 0 then
if self.textAlign == CENTER then
self.cursor.worldPosition = vec3(self.textObject.worldPosition.x + self.textObject.size.x/2,self.cursor.worldPosition.y, 0) + vec3(self.cursor.size.x/2, sY, 0)
elseif self.textAlign == RIGHT then
self.cursor.worldPosition = vec3(self.textObject.worldPosition.x + self.textObject.size.x - self.cursorSize.x/2,self.cursor.worldPosition.y, 0) + vec3(self.cursor.size.x/2, sY, 0)
end
end
else
local pos, size = self:getChar(self.charPos)
local x = pos.x + size.x
local sX, sY = (self.cursor.size/2):unpack()
self.cursor.worldPosition = vec3(x,self.cursor.worldPosition.y, 0) + vec3(0, sY, 0)
end
end
function gui.textInput:touched(touc)
local touch = gui.mapTouchToScene(touc, self.scene)
if self.canEdit then
if touch.type == touch.pointer then
self.startFlickerTime = time.elapsed
gui.textInput.selectedEntity = self.entity
if touch.began and self.charPos == nil then
self.oldTypingText = self.typingText
self.entity:dispatch('onTapped')
end
if touch.began then
if self.beginSelect and self.charPos == nil and self.typingText ~= "" then
self.charPos = 0
self.beginCharPos = 0
self.endCharPos = #self.typingText
self.saveCharPos = self.charPos
self.lastTouchTime = time.elapsed
return true
end
if self.lastTouchTime == nil or time.elapsed - self.lastTouchTime > self.touchOffsetTime then
self.numTaps = 1
else
self.numTaps = self.numTaps +1
end
self.charPos = self:whichChar(touch.pos)
self.isPressing = true
self.beginCharPos = self.charPos
self.endCharPos = nil
self.lastTouchTime = time.elapsed
self.isPressing = false
if self.numTaps >= 3 then
self.beginCharPos = 0
self.endCharPos = #self.typingText
elseif self.numTaps == 2 then
self:highLightWord()
end
self.saveCharPos = self.charPos
end
if touch.moving and self.charPos then
self.charPos = self:whichChar(touch.pos)
self.beginCharPos = self.saveCharPos
if self.beginCharPos ~= self.charPos then
self.endCharPos = self.charPos
else
self.endCharPos = nil
end
end
elseif touch.began then
self.isTyping = true
gui.textInput.selectedEntity = self.entity
self:deselect()
createAlertBox("Input", self.placeHolderText, function(txt)
self.typingText = txt
self.entity:dispatch('onEnter', self.typingText, self.oldTypingText )
gui.textInput.selectedEntity = nil
end)
end
return true
else
self:deselect()
end
end
function gui.textInput:highLightWord()
local back = string.sub(self.typingText, 0, self.charPos)
local front = string.sub(self.typingText, self.charPos+1, #self.typingText)
local _, bNum, ext = string.find(front, "(%W*)%w*")
local endN, ei, ext2 = string.find(back, "%w-(%W*)$")
self.beginCharPos = endN - 1
self.endCharPos = bNum + self.charPos
if ext ~= "" then
self.endCharPos = self.charPos
return
end
if ext == "" and ext2 ~= "" then
self.beginCharPos = self.charPos
return
end
end
local lowerCase = "qwertyuiopasdfghjklzxcvbnm"
local upperCase = "QWERTYUIOPASDFGHJKLZXCVBNM"
typeCharactersList = {}
for k = 1, #lowerCase do
typeCharactersList[string.sub(lowerCase,k,k)] = {string.sub(lowerCase,k,k), string.sub(upperCase,k,k)}
end
typeCharactersList["space"] = {" ", " "}
for k = 1, #lowerCase do
typeCharactersList[string.sub(lowerCase,k,k)] = {string.sub(lowerCase,k,k), string.sub(upperCase,k,k)}
end
otherList = {
num1 = {"1", "!"},
num2 = {"2", "@"},
num3 = {"3", "#"},
num4 = {"4", "$"},
num5 = {"5", "%"},
num6 = {"6", "^"},
num7 = {"7", "&"},
num8 = {"8", "*"},
num9 = {"9", "("},
num0 = {"0", ")"},
minus = {"-", "_"},
plus = {"=", "+"},
leftbracket = {"[", "{"},
rightbracket = {"]", "}"},
backslash = {"\\", "|"},
semicolon = {";", ":"},
quote = {"'", "\""},
comma = {",", "<"},
period = {".", ">"},
slash = {"/", "?"},
tilde = {"`", "~"}
}
for k, v in pairs(otherList) do
typeCharactersList[k] = v
end
Profiler.wrapClass(gui.textInput)