-
Notifications
You must be signed in to change notification settings - Fork 6
/
gui_chat.lua
2207 lines (2053 loc) · 69.5 KB
/
gui_chat.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
function widget:GetInfo()
return {
name = "Chat",
desc = "chat/console (do /clearconsole to wipe history). Every 1000 lines of console gets flushed to a file and console autocleared.",
author = "Errrrrrr, Floris",
date = "June 2023",
license = "GNU GPL, v2 or later",
layer = -980000,
enabled = true
}
end
local utf8 = VFS.Include('common/luaUtilities/utf8.lua')
local showHistoryWhenChatInput = true
local enableShortcutClick = true -- enable ctrl+click to goto mapmark coords... while not being in history mode
local vsx, vsy = gl.GetViewSizes()
local posY = 0.81
local posX = 0.3
local posX2 = 0.74
local charSize = 21 - (3.5 * ((vsx / vsy) - 1.78))
local consoleFontSizeMult = 0.85
local maxLines = 5
local maxConsoleLines = 2
local maxLinesScrollFull = 16
local maxLinesScrollChatInput = 9
local lineHeightMult = 1.36
local lineTTL = 40
local backgroundOpacity = 0.25
local handleTextInput = true -- handle chat text input instead of using spring's input method
local maxTextInputChars = 127 -- tested 127 as being the true max
local inputButton = true
local allowMultiAutocomplete = true
local allowMultiAutocompleteMax = 10
local ui_scale = tonumber(Spring.GetConfigFloat("ui_scale", 1) or 1)
local ui_opacity = tonumber(Spring.GetConfigFloat("ui_opacity", 0.7) or 0.6)
local widgetScale = (((vsx * 0.3 + (vsy * 2.33)) / 2000) * 0.55) * (0.95 + (ui_scale - 1) / 1.5)
local maxLinesScroll = maxLinesScrollFull
local hide = false
local fontsizeMult = 1
local usedFontSize = charSize * widgetScale * fontsizeMult
local usedConsoleFontSize = usedFontSize * consoleFontSizeMult
local orgLines = {}
local chatLines = {}
local consoleLines = {}
local ignoredPlayers = {}
local activationArea = { 0, 0, 0, 0 }
local consoleActivationArea = { 0, 0, 0, 0 }
local currentChatLine = 0
local currentConsoleLine = 0
local historyMode = false
local scrollingPosY = 0.66
local consolePosY = 0.9
local displayedChatLines = 0
local hideSpecChat = (Spring.GetConfigInt('HideSpecChat', 0) == 1)
local lastMapmarkCoords
local myName = Spring.GetPlayerInfo(Spring.GetMyPlayerID(), false)
local isSpec = Spring.GetSpectatingState()
local fontfile2 = "fonts/" .. Spring.GetConfigString("bar_font2", "Exo2-SemiBold.otf")
local fontfile3 = "fonts/monospaced/" .. Spring.GetConfigString("bar_font3", "SourceCodePro-Medium.otf")
local font, font2, font3, chobbyInterface, hovering
local RectRound, UiElement, UiSelectHighlight, UiScroller, elementCorner, elementPadding, elementMargin
local playSound = true
local sndChatFile = 'beep4'
local sndChatFileVolume = 0.55
local sndMapmarkFile = 'sounds/ui/mappoint2.wav'
local sndMapmarkFileVolume = 0.5
local colorOther = { 1, 1, 1 } -- normal chat color
local colorAlly = { 0, 1, 0 }
local colorSpec = { 1, 1, 0 }
local colorOtherAlly = { 1, 0.7, 0.45 } -- enemy ally messages (seen only when spectating)
local colorGame = { 0.4, 1, 1 } -- server (autohost) chat
local colorConsole = { 0.85, 0.85, 0.85 }
local chatSeparator = '\255\210\210\210:'
local pointSeparator = '\255\255\255\255*'
local longestPlayername = '(s) [xx]playername' -- setting a default minimum width
local maxPlayernameWidth = 50
local maxTimeWidth = 20
local lineSpaceWidth = 24 * widgetScale
local lineMaxWidth = 0
local lineHeight = math.floor(usedFontSize * lineHeightMult)
local consoleLineHeight = math.floor(usedConsoleFontSize * lineHeightMult)
local consoleLineMaxWidth = 0
local backgroundPadding = usedFontSize
local gameOver = false
local textInputDlist
local updateTextInputDlist = true
local textCursorRect
local showTextInput = false
local inputText = ''
local inputTextPosition = 0
local cursorBlinkTimer = 0
local cursorBlinkDuration = 1
local anonymousMode = Spring.GetModOptions().teamcolors_anonymous_mode
local anonymousTeamColor = { Spring.GetConfigInt("anonymousColorR", 255) / 255,
Spring.GetConfigInt("anonymousColorG", 0) / 255, Spring.GetConfigInt("anonymousColorB", 0) / 255 }
local inputMode = ''
if isSpec then
inputMode = 's:'
else
if #Spring.GetTeamList(Spring.GetMyAllyTeamID()) > 1 then
inputMode = 'a:'
end
end
local inputTextInsertActive = false
local inputHistory = {}
local inputHistoryCurrent = 0
local inputButtonRect
local autocompleteWords = {}
local prevAutocompleteLetters
local glPopMatrix = gl.PopMatrix
local glPushMatrix = gl.PushMatrix
local glDeleteList = gl.DeleteList
local glCreateList = gl.CreateList
local glCallList = gl.CallList
local glTranslate = gl.Translate
local glColor = gl.Color
local string_lines = string.lines
local math_isInRect = math.isInRect
local floor = math.floor
local clock = os.clock
local schar = string.char
local slen = string.len
local ssub = string.sub
local sfind = string.find
local spGetPlayerRoster = Spring.GetPlayerRoster
local spGetTeamColor = Spring.GetTeamColor
local spGetMyAllyTeamID = Spring.GetMyAllyTeamID
local spPlaySoundFile = Spring.PlaySoundFile
local spGetGameFrame = Spring.GetGameFrame
local autocompleteCommands = {
-- engine
'advmapshading',
'advmodelshading',
'aicontrol',
'aikill',
'ailist',
'aireload',
'airmesh',
'allmapmarks',
'ally',
'atm',
'buffertext',
'chat',
'chatall',
'chatally',
'chatspec',
'cheat',
'clearmapmarks',
--'clock',
'cmdcolors',
'commandhelp',
'commandlist',
'console',
'controlunit',
'crash',
'createvideo',
'cross',
'ctrlpanel',
'debug',
'debugcolvol',
'debugdrawai',
'debuggl',
'debugglerrors',
'debuginfo',
'debugpath',
'debugtraceray',
'decguiopacity',
'decreaseviewradius',
'deselect',
'destroy',
'devlua',
'distdraw',
'disticon',
'divbyzero',
'drawinmap',
'drawlabel',
'drawtrees',
'dumpstate',
'dynamicsky',
'echo',
'editdefs',
'endgraph',
'exception',
'font',
'fps',
'fpshud',
'fullscreen',
'gameinfo',
'gathermode',
'give',
'globallos',
'godmode',
'grabinput',
'grounddecals',
'grounddetail',
'group',
'group0',
'group1',
'group2',
'group3',
'group4',
'group5',
'group6',
'group7',
'group8',
'group9',
'hardwarecursor',
'hideinterface',
'incguiopacity',
'increaseviewradius',
'info',
'inputtextgeo',
'lastmsgpos',
'lessclouds',
'lesstrees',
'lodscale',
'luagaia',
'luarules',
'luasave',
'luaui',
'mapborder',
'mapmarks',
'mapmeshdrawer',
'mapshadowpolyoffset',
'maxnanoparticles',
'maxparticles',
'minimap',
'moreclouds',
'moretrees',
'mouse1',
'mouse2',
'mouse3',
'mouse4',
'mouse5',
'moveback',
'movedown',
'movefast',
'moveforward',
'moveleft',
'moveright',
'moveslow',
'moveup',
'mutesound',
'nocost',
'nohelp',
'noluadraw',
'nospecdraw',
'nospectatorchat',
'pastetext',
'pause',
'quitforce',
'quitmenu',
'quitmessage',
'reloadcegs',
'reloadcob',
'reloadforce',
'reloadgame',
'reloadshaders',
'reloadtextures',
'resbar',
'resync',
'safegl',
'save',
'savegame',
'say',
'screenshot',
'select',
'selectcycle',
'selectunits',
'send',
'set',
'shadows',
'sharedialog',
'showelevation',
'showmetalmap',
'showpathcost',
'showpathflow',
'showpathheat',
'showpathtraversability',
'showpathtype',
'showstandard',
'skip',
'slowdown',
'soundchannelenablec',
'sounddevice',
'specfullview',
'spectator',
'specteam',
--'speed',
'speedcontrol',
'speedup',
'take',
'team',
'teamhighlight',
'toggleinfo',
'togglelos',
'tooltip',
'track',
'trackmode',
'trackoff',
'tset',
'viewselection',
'vsync',
'water',
'wbynum',
'wiremap',
'wiremodel',
'wiresky',
'wiretree',
'wirewater',
'widgetselector',
-- gadgets
'luarules battleroyaledebug',
'luarules buildicon',
'luarules cmd',
'luarules clearwrecks',
'luarules destroyunits',
'luarules disablecus',
'luarules disablecusgl4',
'luarules fightertest',
'luarules give',
'luarules givecat',
'luarules halfhealth',
'luarules kill_profiler',
'luarules loadmissiles',
'luarules profile',
'luarules reclaimunits',
'luarules reloadcus',
'luarules reloadcusgl4',
'luarules removeunits',
'luarules removeunitdef',
'luarules spawnceg',
'luarules undo',
'luarules unitcallinsgadget',
'luarules updatesun',
'luarules waterlevel',
'luarules wreckunits',
'luarules xpunits',
-- widgets
'devmode',
'profile',
'grapher',
'luaui reload',
'luaui profile',
--'luaui selector', -- pops up engine version
'luaui reset',
'luaui factoryreset',
'luaui disable',
'luaui enable',
'savegame',
'addmessage',
'radarpulse',
'snow',
'clearconsole',
'ecostatstext',
'defrange ally air',
'defrange ally nuke',
'defrange ally ground',
'defrange enemy air',
'defrange enemy nuke',
'defrange enemy ground',
'playertv',
'playerview',
'hidespecchat',
'speclist',
}
local autocompleteText
local autocompletePlayernames = {}
local playersList = Spring.GetPlayerList()
for _, playerID in ipairs(playersList) do
local name = Spring.GetPlayerInfo(playerID, false)
autocompletePlayernames[#autocompletePlayernames + 1] = name
end
local autocompleteUnitNames = {}
local autocompleteUnitCodename = {}
local uniqueHumanNames = {}
for unitDefID, unitDef in pairs(UnitDefs) do
if not uniqueHumanNames[unitDef.translatedHumanName] then
uniqueHumanNames[unitDef.translatedHumanName] = true
autocompleteUnitNames[#autocompleteUnitNames + 1] = unitDef.translatedHumanName
end
if not string.find(unitDef.name, "_scav", nil, true) then
autocompleteUnitCodename[#autocompleteUnitCodename + 1] = unitDef.name:lower()
end
end
uniqueHumanNames = nil
for featureDefID, featureDef in pairs(FeatureDefs) do
autocompleteUnitCodename[#autocompleteUnitCodename + 1] = featureDef.name:lower()
end
local teamColorKeys = {}
local teams = Spring.GetTeamList()
for i = 1, #teams do
local r, g, b, a = spGetTeamColor(teams[i])
teamColorKeys[teams[i]] = r .. '_' .. g .. '_' .. b
end
teams = nil
local function wordWrap(text, maxWidth, fontSize)
local lines = {}
local lineCount = 0
for _, line in ipairs(text) do
local words = {}
local wordsCount = 0
local linebuffer = ''
for w in line:gmatch("%S+") do
wordsCount = wordsCount + 1
words[wordsCount] = w
end
for _, word in ipairs(words) do
if font:GetTextWidth(linebuffer .. ' ' .. word) * fontSize > maxWidth then
lineCount = lineCount + 1
lines[lineCount] = linebuffer
linebuffer = ''
end
linebuffer = (linebuffer ~= '' and linebuffer .. ' ' .. word or word)
end
if linebuffer ~= '' then
lineCount = lineCount + 1
lines[lineCount] = linebuffer
end
end
return lines
end
local function addConsoleLine(gameFrame, lineType, text, isLive)
if not text or text == '' then return end
-- convert /n into lines
local textLines = string_lines(text)
-- word wrap text into lines
local wordwrappedText = wordWrap(textLines, consoleLineMaxWidth, usedConsoleFontSize)
local consoleLinesCount = #consoleLines
-- we want to saved consoleLines to a timestamped file then clear it if it's > 50 lines
if consoleLinesCount >= 1000 then
local fileName = "LuaUI/Config/ConsoleLog_" .. os.date("%Y-%m-%d_%H-%M-%S") .. ".txt"
local file = io.open(fileName, "w")
if file then
for i, line in ipairs(consoleLines) do
-- trim first 4 chars (color code) off and write it to file
file:write(ssub(line.text, 5).."\n")
end
file:close()
end
consoleLines = {}
consoleLinesCount = 0
Spring.SendCommands({ "clearconsole" })
Spring.Echo("Console log flushed to: " .. fileName)
end
local lineColor = #wordwrappedText > 1 and ssub(wordwrappedText[1], 1, 4) or ''
local startTime = clock()
for i, line in ipairs(wordwrappedText) do
consoleLinesCount = consoleLinesCount + 1
consoleLines[consoleLinesCount] = {
startTime = startTime,
gameFrame = i == 1 and gameFrame,
lineType = lineType,
text = (i > 1 and lineColor or '') .. line,
--lineDisplayList = glCreateList(function() end),
--timeDisplayList = glCreateList(function() end),
}
end
if historyMode ~= 'console' then
currentConsoleLine = consoleLinesCount
end
end
local function colourNames(teamID)
local nameColourR, nameColourG, nameColourB = Spring.GetTeamColor(teamID)
if (not isSpec) and anonymousMode ~= "disabled" then
nameColourR, nameColourG, nameColourB = anonymousTeamColor[1], anonymousTeamColor[2], anonymousTeamColor[3]
end
local R255 = math.floor(nameColourR * 255) --the first \255 is just a tag (not colour setting) no part can end with a zero due to engine limitation (C)
local G255 = math.floor(nameColourG * 255)
local B255 = math.floor(nameColourB * 255)
if R255 % 10 == 0 then
R255 = R255 + 1
end
if G255 % 10 == 0 then
G255 = G255 + 1
end
if B255 % 10 == 0 then
B255 = B255 + 1
end
return "\255" .. string.char(R255) .. string.char(G255) .. string.char(B255) --works thanks to zwzsg
end
local function teamcolorPlayername(playername)
local playersList = Spring.GetPlayerList()
for _, playerID in ipairs(playersList) do
local name, _, _, teamID = Spring.GetPlayerInfo(playerID, false)
if name == playername then
return colourNames(teamID) .. playername
end
end
return playername
end
local function addChat(gameFrame, lineType, name, text, isLive)
if not text or text == '' then return end
-- determine text typing start time
local startTime = clock()
-- convert /n into lines
local textLines = string_lines(text)
-- word wrap text into lines
local wordwrappedText = wordWrap(textLines, lineMaxWidth, usedFontSize)
local sendMetal, sendEnergy, sendUnits
local msgColor = '\255\180\180\180'
local msgHighlightColor = '\255\215\215\215'
local metalColor = '\255\255\255\255'
local energyColor = '\255\255\255\180'
-- metal/energy given
if lineType == 1 and sfind(text, 'I sent ', nil, true) then
if sfind(text, ' metal to ', nil, true) then
sendMetal = tonumber(string.match(ssub(text, sfind(text, 'I sent ') + 7), '([0-9]*)'))
local playername = teamcolorPlayername(ssub(text, sfind(text, ' metal to ') + 10))
--text = ssub(text, 1, sfind(text, 'I sent ')-1)..' shared: '..sendMetal..' metal to '..playername
--msgColor = ssub(text, 1, sfind(text, 'I sent ')-1)
text = msgColor .. 'shared ' .. metalColor .. sendMetal ..
metalColor .. ' metal' .. msgColor .. ' to ' .. playername
lineType = 5
elseif sfind(text, ' energy to ', nil, true) then
sendEnergy = tonumber(string.match(ssub(text, sfind(text, 'I sent ') + 7), '([0-9]*)'))
local playername = teamcolorPlayername(ssub(text, sfind(text, ' energy to ') + 11)) -- no dot stripping needed here
--text = ssub(text, 1, sfind(text, 'I sent ')-1)..' shared: '..sendEnergy..' energy to '..playername
--msgColor = ssub(text, 1, sfind(text, 'I sent ')-1)
text = msgColor .. 'shared ' .. energyColor ..
sendEnergy .. energyColor .. ' energy' .. msgColor .. ' to ' .. playername
lineType = 5
end
-- units given
elseif lineType == 1 and sfind(text, 'I gave ', nil, true) then
if sfind(text, ' units to ', nil, true) then
sendUnits = tonumber(string.match(ssub(text, sfind(text, 'I gave ') + 7), '([0-9]*)'))
local playername = teamcolorPlayername(ssub(text, sfind(text, ' units to ') + 10, slen(text) - 1)) -- adding "slen(text)-1" to strip the dot.
--text = ssub(text, 1, sfind(text, 'I gave ')-1)..' shared: '..sendUnits..' units to '..playername
--msgColor = ssub(text, 1, sfind(text, 'I gave ')-1)
text = msgColor ..
'shared ' ..
msgHighlightColor .. sendUnits .. msgColor .. ' ' ..
(sendUnits == 1 and 'unit' or 'units') .. ' to ' .. playername
lineType = 5
end
-- player taken
elseif lineType == 1 and sfind(text, 'I took ', nil, true) then --<StarDoM> Allies: I took --- .
if sfind(text, 'I took ', nil, true) then
local playernameStart = sfind(text, 'I took ') + 7
local playername = ssub(text, playernameStart, slen(text) - 1) -- strip dot.
local colonChar = sfind(playername, ':')
local addition = ''
if colonChar then
local leftover = playername
playername = ssub(playername, 1, colonChar - 1)
addition = msgColor .. ssub(leftover, colonChar)
end
playername = teamcolorPlayername(playername)
text = msgColor .. 'took ' .. playername .. addition
lineType = 5
end
end
local chatLinesCount = #chatLines
local lineColor = #wordwrappedText > 1 and ssub(wordwrappedText[1], 1, 4) or ''
for i, line in ipairs(wordwrappedText) do
chatLinesCount = chatLinesCount + 1
chatLines[chatLinesCount] = {
startTime = startTime,
gameFrame = i == 1 and gameFrame,
lineType = lineType,
playerName = name,
text = (i > 1 and lineColor or '') .. line,
--lineDisplayList = glCreateList(function() end),
--timeDisplayList = glCreateList(function() end),
}
if lineType == 3 and lastMapmarkCoords then
chatLines[chatLinesCount].coords = lastMapmarkCoords
lastMapmarkCoords = nil
end
if lineType == 5 then
chatLines[chatLinesCount].text = text
end
if sendMetal then
chatLines[chatLinesCount].sendMetal = sendMetal
end
if sendEnergy then
chatLines[chatLinesCount].sendEnergy = sendEnergy
end
if sendUnits then
chatLines[chatLinesCount].sendUnits = sendUnits
end
if sendUnits then
chatLines[chatLinesCount].sendUnits = sendUnits
end
end
if historyMode ~= 'chat' then
currentChatLine = #chatLines
end
-- play sound for player/spectator chat
if isLive and (lineType == 1 or lineType == 2) and playSound and not Spring.IsGUIHidden() then
spPlaySoundFile(sndChatFile, sndChatFileVolume, nil, "ui")
end
end
local function cancelChatInput()
showTextInput = false
if showHistoryWhenChatInput then
historyMode = false
currentChatLine = #chatLines
end
inputText = ''
inputTextPosition = 0
inputTextInsertActive = false
inputHistoryCurrent = #inputHistory
autocompleteText = nil
autocompleteWords = {}
if WG['guishader'] then
WG['guishader'].RemoveRect('chatinput')
WG['guishader'].RemoveRect('chatinputautocomplete')
end
widgetHandler:DisownText()
end
function widget:PlayerChanged(playerID)
isSpec = Spring.GetSpectatingState()
if isSpec and inputMode == 'a:' then
inputMode = 's:'
end
end
function widget:PlayerAdded(playerID)
local name = Spring.GetPlayerInfo(playerID, false)
autocompletePlayernames[#autocompletePlayernames + 1] = name
end
function widget:Initialize()
Spring.SDLStartTextInput() -- because: touch chobby's text edit field once and widget:TextInput is gone for the game, so we make sure its started!
widget:ViewResize()
Spring.SendCommands("console 0")
if WG.ignoredPlayers then
ignoredPlayers = table.copy(WG.ignoredPlayers)
end
WG['chat'] = {}
WG['chat'].isInputActive = function()
return showTextInput
end
WG['chat'].getInputButton = function()
return inputButton
end
WG['chat'].setHide = function(value)
hide = value
end
WG['chat'].getHide = function()
return hide
end
WG['chat'].setChatInputHistory = function(value)
showHistoryWhenChatInput = value
end
WG['chat'].getChatInputHistory = function()
return showHistoryWhenChatInput
end
WG['chat'].setInputButton = function(value)
inputButton = value
end
WG['chat'].getHandleInput = function()
return handleTextInput
end
WG['chat'].setHandleInput = function(value)
handleTextInput = value
if not handleTextInput then
cancelChatInput()
end
Spring.SDLStartTextInput() -- because: touch chobby's text edit field once and widget:TextInput is gone for the game, so we make sure its started!
end
WG['chat'].getChatVolume = function()
return sndChatFileVolume
end
WG['chat'].setChatVolume = function(value)
sndChatFileVolume = value
end
WG['chat'].getMapmarkVolume = function()
return sndMapmarkFileVolume
end
WG['chat'].setMapmarkVolume = function(value)
sndMapmarkFileVolume = value
end
WG['chat'].getBackgroundOpacity = function()
return backgroundOpacity
end
WG['chat'].setBackgroundOpacity = function(value)
backgroundOpacity = value
end
WG['chat'].getMaxLines = function()
return maxLines
end
WG['chat'].setMaxLines = function(value)
maxLines = value
widget:ViewResize()
end
WG['chat'].getMaxConsoleLines = function()
return maxLines
end
WG['chat'].setMaxConsoleLines = function(value)
maxConsoleLines = value
widget:ViewResize()
end
WG['chat'].getFontsize = function()
return fontsizeMult
end
WG['chat'].setFontsize = function(value)
fontsizeMult = value
widget:ViewResize()
end
end
local uiSec = 0
function widget:Update(dt)
cursorBlinkTimer = cursorBlinkTimer + dt
if cursorBlinkTimer > cursorBlinkDuration then cursorBlinkTimer = 0 end
uiSec = uiSec + dt
if uiSec > 1 then
uiSec = 0
if not addedOptionsList and WG['options'] and WG['options'].getOptionsList then
local optionsList = WG['options'].getOptionsList()
addedOptionsList = true
for i, option in ipairs(optionsList) do
autocompleteCommands[#autocompleteCommands + 1] = 'option ' .. option
end
end
if hideSpecChat ~= (Spring.GetConfigInt('HideSpecChat', 0) == 1) then
hideSpecChat = (Spring.GetConfigInt('HideSpecChat', 0) == 1)
widget:ViewResize()
end
-- check if team colors have changed
local teams = Spring.GetTeamList()
local detectedChanges = false
for i = 1, #teams do
local r, g, b = spGetTeamColor(teams[i])
if teamColorKeys[teams[i]] ~= r .. '_' .. g .. '_' .. b then
teamColorKeys[teams[i]] = r .. '_' .. g .. '_' .. b
detectedChanges = true
end
end
-- detect a change in muted players
if WG.ignoredPlayers then
for name, _ in pairs(ignoredPlayers) do
if not WG.ignoredPlayers[name] then
detectedChanges = true
end
end
for name, _ in pairs(WG.ignoredPlayers) do
if not ignoredPlayers[name] then
detectedChanges = true
end
end
ignoredPlayers = table.copy(WG.ignoredPlayers)
end
if detectedChanges then
widget:ViewResize()
end
end
local x, y, b = Spring.GetMouseState()
if topbarArea then
scrollingPosY = floor(topbarArea[2] - elementMargin - backgroundPadding - backgroundPadding -
(lineHeight * maxLinesScroll)) / vsy
end
local chatlogHeightDiff = historyMode and floor(vsy * (scrollingPosY - posY)) or 0
if WG['topbar'] and WG['topbar'].showingQuit() then
historyMode = false
currentChatLine = #chatLines
elseif math_isInRect(x, y, activationArea[1], activationArea[2], activationArea[3], activationArea[4]) then
local alt, ctrl, meta, shift = Spring.GetModKeyState()
if ctrl and shift then
if math_isInRect(x, y, consoleActivationArea[1], consoleActivationArea[2], consoleActivationArea[3], consoleActivationArea[4]) then
historyMode = 'console'
else
historyMode = 'chat'
end
maxLinesScroll = maxLinesScrollFull
end
elseif historyMode and math_isInRect(x, y, activationArea[1], activationArea[2] + chatlogHeightDiff, activationArea[3], activationArea[2]) then
-- do nothing
else
if not showHistoryWhenChatInput or not showTextInput then
historyMode = false
currentChatLine = #chatLines
end
end
end
local function createGameTimeDisplayList(gametime)
return glCreateList(function()
local minutes = floor((gametime / 30 / 60))
local seconds = floor((gametime - ((minutes * 60) * 30)) / 30)
if seconds == 0 then
seconds = '00'
elseif seconds < 10 then
seconds = '0' .. seconds
end
local offset = 0
if minutes >= 100 then
offset = (usedFontSize * 0.2 * widgetScale)
end
local gameTime = '\255\200\200\200' .. minutes .. ':' .. seconds
font3:Begin()
font3:Print(gameTime, maxTimeWidth + offset, usedFontSize * 0.3, usedFontSize * 0.82, "ro")
font3:End()
end)
end
local function processConsoleLine(i)
if consoleLines[i].lineDisplayList == nil then
glDeleteList(consoleLines[i].lineDisplayList)
local fontHeightOffset = usedFontSize * 0.3
consoleLines[i].lineDisplayList = glCreateList(function()
font:Begin()
font:Print(consoleLines[i].text, 0, fontHeightOffset, usedConsoleFontSize, "o")
font:End()
end)
-- game time (for when viewing history)
if consoleLines[i].gameFrame then
glDeleteList(consoleLines[i].timeDisplayList)
consoleLines[i].timeDisplayList = createGameTimeDisplayList(consoleLines[i].gameFrame)
end
end
end
local function processLine(i)
if chatLines[i].lineDisplayList == nil then
glDeleteList(chatLines[i].lineDisplayList)
local fontHeightOffset = usedFontSize * 0.3
chatLines[i].lineDisplayList = glCreateList(function()
font:Begin()
if chatLines[i].gameFrame then
if chatLines[i].lineType == 3 then -- mapmark point
-- player name
font2:Begin()
font2:Print(chatLines[i].playerName, maxPlayernameWidth, fontHeightOffset, usedFontSize, "or")
font2:End()
-- divider
font2:Print(pointSeparator, maxPlayernameWidth + (lineSpaceWidth / 2), fontHeightOffset * 0.07,
usedFontSize, "oc")
elseif chatLines[i].lineType == 5 then -- system message: sharing resources, taken player
-- player name
font3:Begin()
font3:Print(chatLines[i].playerName, maxPlayernameWidth, fontHeightOffset * 1.2, usedFontSize * 0.9,
"or")
font3:End()
else
-- player name
font2:Begin()
font2:Print(chatLines[i].playerName, maxPlayernameWidth, fontHeightOffset, usedFontSize, "or")
font2:End()
-- divider
font:Print(chatSeparator, maxPlayernameWidth + (lineSpaceWidth / 3.75), fontHeightOffset,
usedFontSize, "oc")
end
end
if chatLines[i].lineType == 5 then -- system message: sharing resources, taken player
font3:Begin()
font3:Print(chatLines[i].text, maxPlayernameWidth + lineSpaceWidth - (usedFontSize * 0.5),
fontHeightOffset * 1.2, usedFontSize * 0.88, "o")
font3:End()
else
font:Print(chatLines[i].text, maxPlayernameWidth + lineSpaceWidth, fontHeightOffset, usedFontSize, "o")
end
font:End()
end)
-- game time (for when viewing history)
if chatLines[i].gameFrame then
glDeleteList(chatLines[i].timeDisplayList)
chatLines[i].timeDisplayList = createGameTimeDisplayList(chatLines[i].gameFrame)
end
end
end
function widget:RecvLuaMsg(msg, playerID)
if msg:sub(1, 18) == 'LobbyOverlayActive' then
chobbyInterface = (msg:sub(1, 19) == 'LobbyOverlayActive1')
if not chobbyInterface then
Spring.SDLStartTextInput() -- because: touch chobby's text edit field once and widget:TextInput is gone for the game, so we make sure its started!
end
end
end
local function drawChatInputCursor()
if textCursorRect then
local a = 1 - (cursorBlinkTimer * (1 / cursorBlinkDuration)) + 0.15
glColor(0.7, 0.7, 0.7, a)
gl.Rect(textCursorRect[1], textCursorRect[2], textCursorRect[3], textCursorRect[4])
glColor(1, 1, 1, 1)
end
end
local function drawChatInput()
if showTextInput then
if topbarArea then
scrollingPosY = floor(topbarArea[2] - elementMargin - backgroundPadding - backgroundPadding -
(lineHeight * maxLinesScroll)) / vsy
end
updateTextInputDlist = false
textInputDlist = glDeleteList(textInputDlist)
textInputDlist = glCreateList(function()
local x, y, _ = Spring.GetMouseState()
local chatlogHeightDiff = historyMode and floor(vsy * (scrollingPosY - posY)) or 0
local inputFontSize = floor(usedFontSize * 1.03)
local inputHeight = floor(inputFontSize * 2.3)
local leftOffset = floor(lineHeight * 0.7)
local distance = (historyMode and inputHeight + elementMargin + elementMargin or elementMargin)
local isCmd = ssub(inputText, 1, 1) == '/'
local usedFont = isCmd and font3 or font
local modeText = Spring.I18N('ui.chat.everyone')
if inputMode == 'a:' then
modeText = Spring.I18N('ui.chat.allies')
elseif inputMode == 's:' then
modeText = Spring.I18N('ui.chat.spectators')
end
if isCmd then
modeText = Spring.I18N('ui.chat.cmd')
end
local modeTextPosX = floor(activationArea[1] + elementPadding + elementPadding + leftOffset)
local textPosX = floor(modeTextPosX + (usedFont:GetTextWidth(modeText) * inputFontSize) + leftOffset +
inputFontSize)
local textCursorWidth = 1 + math.floor(inputFontSize / 14)
if inputTextInsertActive then
textCursorWidth = math.floor(textCursorWidth * 5)
end
local textCursorPos = floor(usedFont:GetTextWidth(utf8.sub(inputText, 1, inputTextPosition)) * inputFontSize)
-- background
local r, g, b, a
local inputAlpha = math.min(0.36, ui_opacity * 0.66)
local x2 = math.max(
textPosX + lineHeight +
floor(usedFont:GetTextWidth(inputText .. (autocompleteText and autocompleteText or '')) * inputFontSize),
floor(activationArea[1] + ((activationArea[3] - activationArea[1]) / 3)))
UiElement(activationArea[1], activationArea[2] + chatlogHeightDiff - distance - inputHeight, x2,
activationArea[2] + chatlogHeightDiff - distance, nil, nil, nil, nil, nil, nil, nil, nil, inputAlpha)
if WG['guishader'] then
WG['guishader'].InsertRect(activationArea[1], activationArea[2] + chatlogHeightDiff - distance -
inputHeight, x2, activationArea[2] + chatlogHeightDiff - distance, 'chatinput')
end
-- button background
inputButtonRect = { activationArea[1] + elementPadding,
activationArea[2] + chatlogHeightDiff - distance - inputHeight + elementPadding, textPosX - inputFontSize,
activationArea[2] + chatlogHeightDiff - distance - elementPadding }
if isCmd then
r, g, b = 0, 0, 0
elseif inputMode == 'a:' then
r, g, b = 0, 0.1, 0
elseif inputMode == 's:' then
r, g, b = 0.1, 0.094, 0
else