-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTextArea.lua
2098 lines (1883 loc) · 62.2 KB
/
TextArea.lua
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
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[------------------------TextArea library---------------------------------
Author: Nikolay Yevstakhov aka N1cke
License: MIT
[TextArea]
TextArea is Gideros library to show and edit multiline and oneline texts.
TextArea supports hardware and virtual (built-in) keyboard input. Text can
be scrolled via mouse/touch and selected via mouse or virtual keyboard.
All standard text operations are supported: Select All, Duplicate, Cut, Copy,
Paste, Undo, Redo. If text can't fit into width and height vertical and
horizontal sliders will appear to help with the navigation. Each text can
have it's own colors and alphas of text, sliders, selection and cursor.
Various alignment modes are available: left, right, center and justified
with ability to suppress word breaks ('wholewords' setting). When text
editing is finished TextArea will run callback if defined.
[Virtual keyboard]
Virtual keyboard is fully customizable from within itself: you can set
each color, change font and sound, modify height, set cursor delays etc.
More than 150 layouts is available for it in included keyboard layouts file
(kblayouts.lua). Keyboard settings are automatically saved in a file
('keyboard.json' by default). It also automatically resizes when screen
resolution changes and fits editable text. If you need to popup it without
touch, for example for cursor settings, you can press "Menu" key on hardware
keyboard.
[API]
◘ TextArea.new(t)
creates TextArea instance
accepts a table (t) where all parameters are optional:
'font': [Font] font for the text
'text': [string] text to display
'sample': [string] text to get top and height for lines
'align': ["L"|"C"|"R"|"J" or 0..1 or -1] left/center/right/justified
'valign': [number in 0..1 range] relative vertical positioning
'width': [number] to clip text width
'height': [number] to clip text height
'letterspace': [number] a space between characters
'linespace': [number] line height modifier
'color': [number in 0x000000..0xFFFFFF range] text color
'colors': [table] paragraph colors, can have a fraction for alpha
'wholewords': [boolean] only whole words in lines if enabled
'oneline': [boolean] fits all text into one line if enabled
'linechars': [number] maximum line length restriction (in characters)
'maxchars': [number] maximum text length restriction (in characters)
'undolevels': [number in 0.. range] levels for undo/redo operations
'curwidth': [number] cursor width in pixels
'curcolor': [number in 0x000000..0xFFFFFF range] cursor color
'curalpha': [number in 0..1 range] cursor alpha
'selcolor': [number in 0x000000..0xFFFFFF range] selection color
'selalpha': [number in 0..1 range] selection alpha
'sldwidth': [number] slider width in pixels
'sldcolor': [number in 0x000000..0xFFFFFF range] slider color
'sldalpha': [number in 0..1 range] slider alpha
'edit': [boolean] adds mouse/touch listener to focus and edit if enabled
'scroll': [boolean] adds mouse/touch listener to scroll if enabled
'callback': [function] (textfield, esc) to be called when editing is done
'native': [boolean] enables native onscreen keyboard if available
NOTE: any missing parameter will be defaulted to TextArea.default one
◘ TextArea:update(t)
updates TextArea with new values from table (t)
accepts a table with same parameters as for TextArea.new
◘ TextArea.property
gets property value
'property' is any parameter accepted by TextArea.new
◘ TextArea:setFocus(showkeyboard)
to manually set focus on TextArea
'showkeyboard': [boolean] shows virtual keyboard if enabled
NOTE: you can't set focus on text without it's width and height set
◘ TextArea:removeFocus(hidekeyboard)
to manually set focus from TextArea
'hidekeyboard': [boolean] hides virtual keyboard if enabled
NOTE: you can't remove focus from text if you are not focused on it
◘ TextArea.setKeyboardFonts(fonts)
sets fonts for virtual keyboard
'fonts': [table] list of fonts
◘ TextArea.setKeyboardSounds(sounds)
sets sounds for virtual keyboard
'sounds': [table] list of sounds
◘ TextArea.setKeyboardLayouts(layouts, [langsPerRow], [lang], [lang2])
sets layouts for virtual keyboard
'layouts': [table] list of languages where language is list of layouts
'langsPerRow': [number greater than 0] langs menu columns' number
'lang': [string, optional] sets main language
'lang2': [string, optional] sets extra language
◘ TextArea.setKeyboardOptions(options)
sets options and/or colors for virtual keyboard
'options': [table] accepts same keys as Keyboard.default table
[Keyboard Layouts]
Can be set via TextArea.setKeyboardLayouts(layouts).
All layouts are grouped into languages:
{lang1 = {...}, lang2 = {...}, ...}
Each language group can contain up to 8 main layouts at 1..8 indexes:
{l1, l2, l3, l4, l5, l6, l7, l8}
1: lowercase letters (shift: off, alt: off)
2: uppercase letters (shift: on, alt: off)
3: main symbols (shift: off, alt: on)
4: extra symbols (shift: on, alt: on)
5: bottom bar
6: colors menu
7: options menu
8: cursor menu
Each missing layout will be loaded from default layouts.
Each language group also supports extra layouts. Extra layout is an layout
you can go to when you hold a key with this extra layout name.
--]]-------------------------------------------------------------------------
TextArea = Core.class(Sprite)
local Keyboard = Bitmap.new(RenderTarget.new(0, 0))
local Cursor = Pixel.new()
TextArea.default = {
font = Font.getDefault(),
text = "",
sample = "qP|",
align = false,
width = false,
height = false,
letterspace = 0,
linespace = 0,
color = false,
colors = false,
wholewords = false,
oneline = false,
maxchars = false,
undolevels = 10,
curwidth = 2,
curcolor = 0x000000,
curalpha = 1,
selcolor = 0x888888,
selalpha = 0.25,
sldwidth = 2,
sldcolor = 0x888888,
sldalpha = 0.5,
edit = false,
scroll = false,
callback = false,
native = false,
}
for k,v in pairs(TextArea.default) do TextArea[k] = v end
Keyboard.default = {
settings = "|D|keyboard.json",
layouts = false,
fonts = {Font.getDefault()},
sounds = {},
langsPerRow = 15,
aniFactor = 0.15,
cursorShowTime = 30,
cursorHideTime = 30,
repeatDelay = 500,
repeatSpan = 50,
lang = "en",
lang2 = "en",
fontIndex = 1,
soundIndex = 1,
fixTime = 500,
aniTime = 250,
vibro = true,
groundColor = 0xBB8800,
groundAlpha = 1.0,
keysColor = 0x000000,
keysAlpha = 0.5,
frameColor = 0xFFBB00,
frameAlpha = 1.0,
fillColor = 0xFF0000,
fillAlpha = 0.2,
keysScale = 0.9,
height = 0.5,
margin = 0.0,
}
for k,v in pairs(Keyboard.default) do Keyboard[k] = v end
local function getScreenWidth()
return application:getScaleMode() == "noScale" and
application:getDeviceWidth() or application:getContentWidth()
end
local function getScreenHeight()
return application:getScaleMode() == "noScale" and
application:getDeviceHeight() or application:getContentHeight()
end
-- Keyboard
function Keyboard.importLayouts(filename)
local layouts = {}
local json = require "json"
local file = io.open(filename, "rb")
local data = file:read"*a"
file:close()
local r = "jQuery.keyboard.layouts%[['\"](.-)['\"]%]%s?=%s?({.-});"
for name, layout in data:gmatch(r) do
local layoutjson = layout
:gsub(" ' ", " \\u0057 ")
:gsub(" '\"", " \\u0057\"")
:gsub("\"' ", "\"\\u0057 ")
:gsub(" \" ", " \\u0052 ")
:gsub(" \\\"", " \\u0052")
:gsub("'", "\"")
:gsub("],%s+}", "]\n}")
:gsub("\"\"", "\\u0052")
:gsub("//.-\n", "\n")
:gsub("name%s?:", "\"name\" :")
:gsub("lang%s?:", "\"lang\" :")
:gsub("shift%s?:", "\"shift\" :")
:gsub("alt%s?:", "\"alt\" :")
:gsub("/%*.-%*/", "")
local dec = json.decode(layoutjson)
local t = {}
for k,v in ipairs{"normal", "shift", "alt", "alt-shift"} do
if dec[v] then
dec[v][#dec[v]] = nil
local page = table.concat(dec[v], "\n")
t[k] = page:gsub("\\u", "%%x"):gsub("{%w+}", "")
end
end
layouts[name:gsub("%s", "-")] = t
end
return layouts
end
local colorsMenu = {
{"Keys" , "-R", "R+", "-G", "G+", "-B", "B+", "-A", "A+"},
{"Ground", "-R", "R+", "-G", "G+", "-B", "B+", "-A", "A+"},
{"Frame" , "-R", "R+", "-G", "G+", "-B", "B+", "-A", "A+"},
{"Fill" , "-R", "R+", "-G", "G+", "-B", "B+", "-A", "A+"},
}
local optionsMenu = {
{"<Font" , "Font>" , "<Scale" , "Scale>" , "<Delay", "Delay>"},
{"<Sound" , "Sound>" , "<Vibro" , "Vibro>" , "<Span" , "Span>" },
{"<Height", "Height>", "<Margin", "Margin>", "<Show" , "Show>" },
{"<Hold" , "Hold>" , "<Anim" , "Anim>" , "<Hide" , "Hide>" },
}
local toolbarMenu = {
{"Shift", "Alt", "Space" , "BS" , "Enter" },
{"Shift", "Alt", "Left" , "Right" , "Go" },
{"Shift", "Alt", "Switch", "Cursor", "Esc" },
{"Shift", "Alt", "Langs" , "Colors", "Options"},
}
local cursorMenu = {
{"Undo", "Redo", "Dup" , "All", "Cut" , "Copy" , "Paste" },
{"Home", "Up" , "PageUp", "" , "Home", "Up" , "PageUp"},
{"Left", "Move", "Right" , "" , "Left", "Select", "Right" },
{"End" , "Down", "PageDn", "" , "End" , "Down" , "PageDn"},
}
local defaultLayout = {
[[
1 2 3 4 5 6 7 8 9 0
q w e r t y u i o p
a s d f g h j k l
, z x c v b n m .
]],[[
1 2 3 4 5 6 7 8 9 0
Q W E R T Y U I O P
A S D F G H J K L
, Z X C V B N M .
]],[[
! @ # $ % ^ & * ( )
` ~ - _ = + { } [ ]
; : " ' | \
, < > ? / .
]],[[
¡ ¢ £ ¤ ¥ ¦ § ¨ © ª
« ¬ ® ¯ ° ± ¹ ² ³ ´
µ ¶ · ¸ º » ¼ ½ ¾ ¿
× Ø ÷ ø
]],[[
Shift Alt Space BS Enter
Shift Alt Left Right Go
Shift Alt Switch Cursor Esc
Shift Alt Langs Colors Options
]],[[
Keys -R R+ -G G+ -B B+ -A A+
Ground -R R+ -G G+ -B B+ -A A+
Frame -R R+ -G G+ -B B+ -A A+
Fill -R R+ -G G+ -B B+ -A A+
]],[[
<Font Font> <Scale Scale> <Delay Delay>
<Sound Sound> <Vibro Vibro> <Span Span>
<Height Height> <Margin Margin> <Show Show>
<Hold Hold> <Anim Anim> <Hide Hide>
]],[[
Undo Redo Dup All Cut Copy Paste
Home Up PageUp Home Up PageUp
Left Move Right Left Select Right
End Down PageDn End Down PageDn
]],
["A"] = "À Á Â Ã Ä Å Æ Ā Ă Ą \n \n \n ",
["C"] = "Ç Ć Ĉ Ċ Č \n \n \n ",
["D"] = "Ð Ď Đ \n \n \n ",
["E"] = "È É Ê Ë Ē Ĕ Ė Ę Ě \n \n \n ",
["G"] = "Ĝ Ğ Ġ Ģ \n \n \n ",
["H"] = "Ĥ Ħ \n \n \n ",
["I"] = "Ì Í Î Ï Ĩ Ī Ĭ Į İ IJ \n \n \n ",
["J"] = "Ĵ \n \n \n ",
["K"] = "Ķ \n \n \n ",
["L"] = "Ĺ Ļ Ľ Ŀ Ł \n \n \n ",
["N"] = "Ñ Ń Ņ Ň Ŋ\n \n \n ",
["O"] = "Ò Ó Ô Õ Ö Ō Ŏ Ő Œ \n \n \n ",
["R"] = "Ŕ Ŗ Ř \n \n \n ",
["S"] = "ß Ś Ŝ Ş Š \n \n \n ",
["T"] = "Þ Ţ Ť Ŧ \n \n \n ",
["U"] = "Ù Ú Û Ü Ũ Ū Ŭ Ů Ű Ų \n \n \n ",
["W"] = "Ŵ \n \n \n ",
["Y"] = "Ý Ŷ Ÿ \n \n \n ",
["Z"] = "Ź Ż Ž \n \n \n ",
["a"] = "à á â ã ä å æ ā ă ą \n \n \n ",
["c"] = "ç ć ĉ ċ č \n \n \n ",
["d"] = "ð ď đ \n \n \n ",
["e"] = "è é ê ë ē ĕ ė ę ě \n \n \n ",
["g"] = "ĝ ğ ġ ģ \n \n \n ",
["h"] = "ĥ ħ \n \n \n ",
["i"] = "ì í î ï ĩ ī ĭ į ı ij \n \n \n ",
["j"] = "ĵ \n \n \n ",
["k"] = "ķ ĸ \n \n \n ",
["l"] = "ĺ ļ ľ ŀ ł \n \n \n ",
["n"] = "ñ ń ņ ň ʼn ŋ \n \n \n ",
["o"] = "ò ó ô õ ö ō ŏ ő œ \n \n \n ",
["r"] = "ŕ ŗ ř \n \n \n ",
["s"] = "ſ ś ŝ ş š \n \n \n ",
["t"] = "þ ţ ť ŧ \n \n \n ",
["u"] = "ù ú û ü ũ ū ŭ ů ű ų\n \n \n ",
["w"] = "ŵ \n \n \n ",
["y"] = "ý ÿ ŷ \n \n \n ",
["z"] = "ź ż ž \n \n \n ",
}
if Keyboard.settings then
local file = io.open(Keyboard.settings, "rb")
if file then
local data = file:read "*a"
local cfg = require"json".decode(data)
file:close()
for k,v in pairs(Keyboard.default) do
Keyboard[k] = cfg[k] ~= nil and cfg[k] or v
end
end
Keyboard:addEventListener(Event.APPLICATION_EXIT, function()
local default = {}
for k in pairs(Keyboard.default) do default[k] = Keyboard[k] end
default.layouts, default.fonts, default.sounds = nil
default.settings, default.langsPerRow, default.aniFactor = nil
local cfg = require"json".encode(default)
local file = io.open(Keyboard.settings, "wb")
file:write(cfg)
file:close()
end)
end
function Keyboard.getLangsMenu(layouts, langsPerRow)
local t = {}
for k in pairs(layouts) do t[#t+1] = k end
table.sort(t, function(a, b) return a < b end)
for i = #t, 1, -langsPerRow do table.insert(t, i, "\n") end
return table.concat(t, " ")
end
if not Keyboard.layouts then
Keyboard.layouts = {en = defaultLayout}
Keyboard.default.lang = Keyboard.lang
Keyboard.default.lang2 = Keyboard.lang2
end
local langsMenu = Keyboard.getLangsMenu(Keyboard.layouts,
Keyboard.langsPerRow)
local defaultLang = Keyboard.layouts[Keyboard.default.lang]
for _,lang in pairs(Keyboard.layouts) do
for i = 1, 8 do
lang[i] = lang[i] or defaultLang[i] or defaultLayout[i]
end
end
if not Keyboard.layouts[Keyboard.lang] then
Keyboard.lang = next(Keyboard.layouts)
end
if not Keyboard.layouts[Keyboard.lang2] then
Keyboard.lang2 = next(Keyboard.layouts)
end
local selector = Path2D.new()
Keyboard:addChild(selector)
selector:setVisible(false)
local selTimer = Timer.new(50, 1)
selTimer:addEventListener(Event.TIMER, function()
selector:setVisible(false)
end)
local shiftSelector = Path2D.new()
Keyboard:addChild(shiftSelector)
shiftSelector:setVisible(false)
local shiftFixed = false
local shiftTimer = Timer.new(Keyboard.fixTime, 1)
local altSelector = Path2D.new()
Keyboard:addChild(altSelector)
altSelector:setVisible(false)
local altFixed = false
local altTimer = Timer.new(Keyboard.fixTime, 1)
local function setSelectorsColor()
selector:setLineColor(Keyboard.frameColor, Keyboard.frameAlpha)
selector:setFillColor(Keyboard.fillColor, Keyboard.fillAlpha)
shiftSelector:setLineColor(Keyboard.frameColor, Keyboard.frameAlpha)
shiftSelector:setFillColor(Keyboard.fillColor, Keyboard.fillAlpha)
altSelector:setLineColor(Keyboard.frameColor, Keyboard.frameAlpha)
altSelector:setFillColor(Keyboard.fillColor, Keyboard.fillAlpha)
end
setSelectorsColor()
local function setSelectorSize(s, w, h)
w, h = w - 1, h - 1
s:setSvgPath(string.format("M 1 1 L %s 1 L %s %s L 1 %s Z", w, w, h, h))
end
local function updateColors(color, alpha, key)
local a = math.ceil(alpha * 255)
local b = color % 256
local g = (color - b) / 256 % 256
local r = (color - 256 * g - b) / 65536 % 16777216
local d = 32
if key == "-R" then r = math.max(r - d, 0)
elseif key == "R+" then r = math.min(r + d, 255)
elseif key == "-G" then g = math.max(g - d, 0)
elseif key == "G+" then g = math.min(g + d, 255)
elseif key == "-B" then b = math.max(b - d, 0)
elseif key == "B+" then b = math.min(b + d, 255)
elseif key == "-A" then a = math.max(a - d, 0)
elseif key == "A+" then a = math.min(a + d, 255) end
return r * 65536 + g * 256 + b, a / 255
end
local function getLangBounds(font, lang)
local l = Keyboard.layouts[lang]
local normals, toolbar = nil, l[5]
local langs, colors, options, cursor = langsMenu, l[6], l[7], l[8]
l[5], l[6], l[7], l[8] = nil, nil, nil, nil
local t = {}
for i, layout in pairs(l) do t[#t+1] = layout end
normals = table.concat(t)
l[5], l[6], l[7], l[8] = toolbar, colors, options, cursor
local groups = {
normals = normals,
toolbar = toolbar,
langs = langs,
colors = colors,
options = options,
cursor = cursor
}
local bounds = {}
for name,words in pairs(groups) do
local b = {}
local ym, wm, hm = 0, 0, 0
for word in words:gmatch"%S+" do
local x, y, w, h = font:getBounds(word)
b[word] = {x, y, w, h}
ym, wm, hm = math.min(ym, y), math.max(wm, w), math.max(hm, h)
end
b[1], b[2], b[3] = ym, wm, hm
bounds[name] = b
end
return bounds
end
local function getLines(layout)
local t = {}
local lines = {}
for line in layout:gmatch"([^\r\n]+)" do lines[#lines+1] = line end
for i,line in ipairs(lines) do
local words = {}
local spaces = line:sub(1,1) == " " and 1 or 0
for word in line:gmatch"%S*" do
if word == "" then
spaces = spaces + 1
else
for i = 1, math.floor((spaces - 1) / 2) do
words[#words+1] = ""
end
spaces = 0
words[#words+1] = word
end
end
spaces = spaces - 1
for i = 1, math.floor((spaces) / 2) do
words[#words+1] = ""
end
words.shifted = (#line:match" *" % 2) * (spaces % 2)
t[#t+1] = words
end
return t
end
local font = Keyboard.fonts[math.min(#Keyboard.fonts, Keyboard.fontIndex)]
local sound = Keyboard.sounds[math.min(#Keyboard.sounds, Keyboard.soundIndex)]
local bounds = getLangBounds(font, Keyboard.lang)
local frames = math.max(1, math.ceil(Keyboard.aniTime/16))
local appW, appH = nil, nil
local texture = nil
local lines = nil
local key = nil
local isToolbar = nil
local isExtra = nil
local menu = nil
local lastX, lastY = nil, nil
local toolbarMode = nil
local extTimer = Timer.new(Keyboard.fixTime, 1)
local aniTimer = Timer.new(15, frames)
local realheight = 0
local hidden = true
function Keyboard.updatePosY(t)
local k = Keyboard.aniFactor
appH = getScreenHeight()
local ax, ay = stage:getAnchorPosition()
local h = realheight
local y = k * (1 - t) * h + appH - h + ay
Keyboard:setY(y)
return y
end
function Keyboard.slide()
local t = aniTimer:getCurrentCount() / frames
if hidden then
if t == 1 and Cursor.__parent then
local parent = Cursor.__parent
local escape = Cursor.escape
if escape then
Cursor.escape = false
if escape ~= "Menu" then
local pos = parent.cursorpos
parent.selection = {pos, pos}
Cursor.setSelection(pos, pos)
Cursor.selection:removeFromParent()
Cursor.vslider:removeFromParent()
Cursor.hslider:removeFromParent()
Cursor:removeFromParent()
end
end
Keyboard:removeFromParent()
if escape and escape ~= "Menu" and parent.callback
and parent.text ~= Cursor.origtext then
parent.callback(parent, escape == "Esc")
end
stage:setAnchorPosition(0, 0)
Keyboard.updatePosY(1)
return
else
t = 1 - t
end
end
if Cursor.__parent then
local parent = Cursor.__parent
local h = realheight
local y = (1 - t) * Keyboard.aniFactor * h + appH - h
if t == 1 and not hidden then
local oldheight = parent.height
parent.height = math.min(Cursor.areaHeight, y)
if parent.height ~= oldheight then
if parent.scrollheight == 0 and parent.valign > 0 then
parent:setAnchorPosition(0, 0)
end
parent:updateArea(parent.width, parent.height)
parent:updateSliders()
end
end
local _, y1 = parent:getBounds(stage)
local px, py = parent:getAnchorPosition()
if parent.scrollheight == 0 then py = 0 end
local y2 = y1 + parent.height + py
local ay = t * (y2 - y)
stage:setAnchorPosition(0, ay)
end
Keyboard.updatePosY(t)
Keyboard:setAlpha(t)
end
aniTimer:addEventListener(Event.TIMER, Keyboard.slide)
extTimer:addEventListener(Event.TIMER, function()
isExtra = true
if key then Keyboard.update() end
key = nil
selector:setVisible()
Keyboard.onKeyPress{x = lastX, y = lastY}
end)
function Keyboard.update()
local appW0, appH0 = appW, appH
appW, appH = getScreenWidth(), getScreenHeight()
realheight = appH * Keyboard.height
local layoutHeight = (1 - Keyboard.margin - Keyboard.margin) * realheight
if true or appW ~= appW0 or appH ~= appH0 then
texture = RenderTarget.new(appW, realheight)
end
texture:clear(Keyboard.groundColor, Keyboard.groundAlpha)
local stamp = TextField.new(font, "")
stamp:setTextColor(Keyboard.keysColor)
stamp:setAlpha(Keyboard.keysAlpha)
local shift = shiftSelector:isVisible()
local alt = altSelector:isVisible()
local layout, mode = nil, nil
if menu then
if menu == langsMenu then
layout, mode = langsMenu, "langs"
elseif menu == colorsMenu then
layout, mode = Keyboard.layouts[Keyboard.lang][6], "colors"
elseif menu == optionsMenu then
layout, mode = Keyboard.layouts[Keyboard.lang][7], "options"
elseif menu == cursorMenu then
layout, mode = Keyboard.layouts[Keyboard.lang][8], "cursor"
end
elseif isExtra then
layout, mode = Keyboard.layouts[Keyboard.lang][key], "normals"
else
local m = (shift and 1 or 0) + (alt and 2 or 0) + 1
layout, mode = Keyboard.layouts[Keyboard.lang][m], "normals"
end
local lb = bounds[mode]
local charY, charW, charH = lb[1], lb[2], lb[3]
lines = getLines(layout)
local maxL = 1
for i = 1, #lines do maxL = math.max(maxL, #lines[i]) end
local maxW = appW / maxL
local n = #lines
local lineH = layoutHeight / (n + 1)
local scale = math.min(lineH / charH, maxW / charW) * Keyboard.keysScale
Keyboard.layoutY0 = appH - realheight * (1 - Keyboard.margin)
Keyboard.layoutY1 = appH - realheight * Keyboard.margin - 1
Keyboard.lineH = lineH
local offsetY = -0.5 * (lineH - scale * charH) + scale * charY -
realheight * Keyboard.margin + lineH
stamp:setScale(scale)
Keyboard.lineLengths = {}
Keyboard.lineOffsetXs = {}
for i = 1, n do
stamp:setY(lineH * i - offsetY)
local l = #lines[i]
Keyboard.lineLengths[i] = l
local shifted = lines[i].shifted
local w = appW / (l + shifted)
local x0 = 0.5 * shifted * w
Keyboard.lineOffsetXs[i] = x0
for j = 1, l do
local char = lines[i][j]
if char ~= "" then
local cx, cw = lb[char][1], lb[char][3]
stamp:setText(char)
local offsetX = 0.5 * (w - cw * scale) - cx * scale
stamp:setX(x0 + w * (j - 1) + offsetX)
texture:draw(stamp)
end
end
end
toolbarMode = ((shift and not shiftFixed) and 1 or 0) +
((alt and not altFixed) and 2 or 0) + 1
local specialKeys = getLines(Keyboard.layouts[Keyboard.lang][5])
local words = specialKeys[toolbarMode]
local l = #words
local w0 = appW / l
local lb = bounds.toolbar
local wordY, wordW, wordH = lb[1], lb[2], lb[3]
local scale = Keyboard.keysScale * math.min(lineH/wordH, w0 / wordW)
stamp:setScale(scale)
stamp:setY(lineH * n + 0.5 * (lineH - scale * wordH) -
scale * wordY + realheight * Keyboard.margin)
for i = 1, l do
stamp:setText(words[i])
local x0 = appW * (i - 1) / l
local x, w = lb[words[i]][1], lb[words[i]][3]
stamp:setX(x0 + 0.5 * (w0 - scale * (w - x)))
texture:draw(stamp)
end
setSelectorSize(shiftSelector, w0, lineH)
setSelectorSize(altSelector, w0, lineH)
local y0 = n * lineH + realheight * Keyboard.margin
shiftSelector:setPosition(0, y0)
altSelector:setPosition(w0, y0)
Keyboard:setTexture(texture)
if not hidden and not aniTimer:isRunning() then Keyboard.slide() end
end
function Keyboard.updateOptions(key)
local kb = Keyboard
if key == "<Font" then
kb.fontIndex = kb.fontIndex == 1 and #kb.fonts or kb.fontIndex - 1
font = kb.fonts[kb.fontIndex] or kb.fonts[1]
bounds = getLangBounds(font, Keyboard.lang)
Keyboard.update()
elseif key == "Font>" then
kb.fontIndex = kb.fontIndex == #kb.fonts and 1 or kb.fontIndex + 1
font = kb.fonts[kb.fontIndex] or kb.fonts[1]
bounds = getLangBounds(font, Keyboard.lang)
Keyboard.update()
elseif key == "<Sound" then
kb.soundIndex = kb.soundIndex == 1 and #kb.sounds or
kb.soundIndex - 1
sound = kb.sounds[kb.soundIndex]
elseif key == "Sound>" then
kb.soundIndex = kb.soundIndex == #kb.sounds and 1 or
kb.soundIndex + 1
sound = kb.sounds[kb.soundIndex]
elseif key == "<Vibro" then
Keyboard.vibro = false
elseif key == "Vibro>" then
Keyboard.vibro = true
elseif key == "Scale>" then
kb.keysScale = math.min(kb.keysScale + 0.05, 1.0)
Keyboard.update()
elseif key == "<Scale" then
kb.keysScale = math.max(kb.keysScale - 0.05, 0.5)
Keyboard.update()
elseif key == "Height>" then
kb.height = math.min(kb.height + 0.05, 0.75)
appW, appH = 0, 0
Keyboard.update()
elseif key == "<Height" then
kb.height = math.max(kb.height - 0.05, 0.25)
appW, appH = 0, 0
Keyboard.update()
elseif key == "Margin>" then
kb.margin = math.min(kb.margin + 0.025, 0.25)
Keyboard.update()
elseif key == "<Margin" then
kb.margin = math.max(kb.margin - 0.025, 0.00)
Keyboard.update()
elseif key == "Hold>" then
kb.fixTime = math.min(kb.fixTime + 100, 750)
shiftTimer:setDelay(kb.fixTime)
altTimer:setDelay(kb.fixTime)
extTimer:setDelay(kb.fixTime)
elseif key == "<Hold" then
kb.fixTime = math.max(kb.fixTime - 100, 250)
shiftTimer:setDelay(kb.fixTime)
altTimer:setDelay(kb.fixTime)
extTimer:setDelay(kb.fixTime)
elseif key == "Anim>" then
kb.aniTime = math.min(kb.aniTime + 100, 500)
frames = math.max(1, math.ceil(Keyboard.aniTime/16))
aniTimer:setRepeatCount(frames)
aniTimer:reset()
aniTimer:start()
elseif key == "<Anim" then
kb.aniTime = math.max(kb.aniTime - 100, 0)
frames = math.max(1, math.ceil(Keyboard.aniTime/16))
aniTimer:setRepeatCount(frames)
aniTimer:reset()
aniTimer:start()
elseif key == "Delay>" then
kb.repeatDelay = math.min(kb.repeatDelay + 128, 1024)
elseif key == "<Delay" then
kb.repeatDelay = math.max(kb.repeatDelay - 128, 0)
elseif key == "Span>" then
kb.repeatSpan = math.min(kb.repeatSpan + 32, 256)
elseif key == "<Span" then
kb.repeatSpan = math.max(kb.repeatSpan - 32, 32)
elseif key == "Show>" then
kb.cursorShowTime = math.min(kb.cursorShowTime + 8, 64)
elseif key == "<Show" then
kb.cursorShowTime = math.max(kb.cursorShowTime - 8, 8)
elseif key == "Hide>" then
kb.cursorHideTime = math.min(kb.cursorHideTime + 8, 64)
elseif key == "<Hide" then
kb.cursorHideTime = math.max(kb.cursorHideTime - 8, 8)
end
end
function Keyboard.onKeyPress(e)
local x, y = e.x or e.touch.x, e.y or e.touch.y
if isExtra then selector:setVisible(false) end
key = nil
if y < Keyboard.layoutY0 or y > Keyboard.layoutY1 then
lastX, lastY = nil, nil
return
end
if e.__userdata then e:stopPropagation() end
lastX, lastY = x, y
local row = 1 + math.floor((y - Keyboard.layoutY0) / Keyboard.lineH)
local x0, w0 = 0, 0
local l = #lines
local col = nil
isToolbar = row > l
local toolbar = toolbarMenu[toolbarMode]
if isToolbar then
col = math.floor(x * #toolbar / appW) + 1
w0 = appW / #toolbar
x0 = (col - 1) * w0
else
local w = appW - 2 * Keyboard.lineOffsetXs[row]
col = 1 + math.floor((x - Keyboard.lineOffsetXs[row]) *
Keyboard.lineLengths[row] / w)
if lines[row][col] == nil or lines[row][col] == "" then return end
x0 = Keyboard.lineOffsetXs[row] + (col - 1) * w / #lines[row]
w0 = (appW - 2 * Keyboard.lineOffsetXs[row]) /
Keyboard.lineLengths[row]
end
key = lines[row] and lines[row][col] or toolbarMenu[toolbarMode][col]
if isExtra then
key = nil
local y0 = appH - Keyboard.layoutY0 - (l - row + 2) * Keyboard.lineH
selector:setVisible(true)
selTimer:reset()
setSelectorSize(selector, w0, Keyboard.lineH)
selector:setPosition(x0, y0)
return
end
if isToolbar then
if key == "Langs" then
menu = langsMenu
elseif key == "Colors" then
menu = colorsMenu
elseif key == "Options" then
menu = optionsMenu
elseif key == "Cursor" then
menu = cursorMenu
elseif key == "Shift" then
if shiftTimer:isRunning() and shiftSelector:isVisible() then
shiftTimer:stop()
shiftFixed = true
else
shiftSelector:setVisible(not shiftSelector:isVisible())
shiftTimer:start()
shiftFixed = false
end
if menu then menu = nil; altSelector:setVisible(false) end
elseif key == "Alt" then
if altTimer:isRunning() and altSelector:isVisible() then
altTimer:stop()
altFixed = true
else
altSelector:setVisible(not altSelector:isVisible())
altTimer:start()
altFixed = false
end
if menu then menu = nil; shiftSelector:setVisible(false) end
elseif key == "Switch" then
Keyboard.lang, Keyboard.lang2 = Keyboard.lang2, Keyboard.lang
bounds = getLangBounds(font, Keyboard.lang)
elseif key == "Space" then
Cursor.onKeyDown{key = " "}
else
Cursor.onKeyDown{specialKey = key}
end
if ({Langs=0,Colors=0,Options=0,Cursor=0,Shift=0,Alt=0})[key] then
Keyboard.update()
end
elseif menu then
if menu == langsMenu then
if Keyboard.lang2 == key then Keyboard.lang2 = Keyboard.lang end
Keyboard.lang = key
bounds = getLangBounds(font, Keyboard.lang)
shiftSelector:setVisible(false)
altSelector:setVisible(false)
elseif menu == colorsMenu then
if col == 1 then return end
local name = colorsMenu[row][1]:lower()
Keyboard[name.."Color"], Keyboard[name.."Alpha"] = updateColors(
Keyboard[name.."Color"], Keyboard[name.."Alpha"], key)
if row < 3 then Keyboard.update() else setSelectorsColor() end
elseif menu == optionsMenu then
Keyboard.updateOptions(optionsMenu[row][col])
elseif menu == cursorMenu then
if key == "Move" or key == "Select" then return end
Cursor.onKeyDown{specialKey = cursorMenu[row][col],
shift = col > 3}
end
elseif Keyboard.layouts[Keyboard.lang][key] then
extTimer:start()
else
extTimer:stop()
end
local y0 = appH - Keyboard.layoutY0 - (l - row + 2) * Keyboard.lineH
selector:setVisible(true)
selTimer:reset()
setSelectorSize(selector, w0, Keyboard.lineH)
selector:setPosition(x0, y0)
if sound then sound:play() end
if Keyboard.vibro then application:vibrate() end
end
function Keyboard.onKeyMove(e)
if isExtra then Keyboard.onKeyPress(e) end
if lastX or lastY then e:stopPropagation() end
end
function Keyboard.onKeyRelease(e)
if lastX or lastY then e:stopPropagation() end
local needupdate = false
if isExtra then
isExtra = false
Keyboard.onKeyPress(e)
selector:setVisible(false)
needupdate = true
end
selTimer:start()
extTimer:stop()
if menu == langsMenu and not shiftSelector:isVisible() then
key = nil
menu = nil
needupdate = true
end
if isToolbar and ({Alt = 0, Shift = 0, Left = 0, Right = 0,
Langs = 0, Colors = 0, Options = 0})[key] or menu then
Cursor.lastKeyEvent = nil
return
end
if shiftSelector:isVisible() and not shiftFixed and key then
shiftSelector:setVisible(false)
needupdate = true
end
if altSelector:isVisible() and not altFixed and key then
altSelector:setVisible(false)
needupdate = true
end
if not isToolbar and key then Cursor.onKeyDown{key = key} end
key = nil
if needupdate then Keyboard.update() end
Cursor.lastKeyEvent = nil
end
function Keyboard.onEnterFrame()
if stage:getChildIndex(Keyboard) ~= stage:getNumChildren() then
stage:addChild(Keyboard)
end
end
local events = {
[Event.MOUSE_DOWN] = Keyboard.onKeyPress,
[Event.MOUSE_MOVE] = Keyboard.onKeyMove,
[Event.MOUSE_UP] = Keyboard.onKeyRelease,
[Event.TOUCHES_BEGIN] = Keyboard.onKeyPress,
[Event.TOUCHES_MOVE] = Keyboard.onKeyMove,