forked from Spring-Chobby/Chobby
-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathconsole.lua
More file actions
438 lines (388 loc) · 11.4 KB
/
console.lua
File metadata and controls
438 lines (388 loc) · 11.4 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
Console = LCS.class{}
function Console:init(channelName, sendMessageListener, noHistoryLoad, onResizeFunc, isBattleChat)
self.listener = sendMessageListener
self.showDate = true
self.dateFormat = "%H:%M"
self.sentMsgHistory = {}
self.sentMsgHistoryCount = 0
self.sentMsgHistoryMax = 500
self.sentMsgHistoryCursor = 1
self.currentMsgBuffer = ""
self.channelName = channelName
local onResize
if onResizeFunc then
onResize = {
function ()
onResizeFunc(self)
end
}
end
-- TODO: currently this is handled by chat windows and battleroom chat separately
self.unreadMessages = 0
self.spHistory = ScrollPanel:New {
name = self.channelName and (self.channelName .. " scroll panel"),
x = 0,
right = 2,
y = 0,
bottom = 35,
verticalSmartScroll = true,
}
self.tbHistory = TextBox:New {
x = 0,
right = 0,
y = 0,
agressiveMaxLines = 500,
agressiveMaxLinesPreserve = 100,
lineSpacing = 2,
bottom = 0,
text = "",
objectOverrideFont = Configuration:GetFont(Configuration.chatFontSize, "console_" .. Configuration.chatFontSize, false, true),
parent = self.spHistory,
selectable = true,
subTooltips = true,
_inmousemove = false,
OnClick = { function(obj)
if not obj._inmousemove then
screen0:FocusControl(self.ebInputText)
end
obj._inmousemove = false
end},
OnMouseMove = { function(obj, x, y, dx, dy, button)
if button ~= 1 then
return
end
obj._inmousemove = true
end},
onResize = onResize
}
self.ebInputText = EditBox:New {
x = 0,
bottom = 7,
height = 25,
right = 2,
text = "",
objectOverrideFont = Configuration:GetFont(Configuration.chatFontSize, "console_" .. Configuration.chatFontSize, false, true),
objectOverrideHintFont = Configuration:GetHintFont(Configuration.chatFontSize, "console_hint_" .. Configuration.chatFontSize, false, true),
--hint = i18n("type_here_to_chat"),
}
local function onConfigurationChange(listener, key, value)
if key == "chatFontSize" then
local oldFont = self.ebInputText.font
-- Relevant settings depend on skin
local fontSettings = {
font = oldFont.font,
color = oldFont.color,
outlineColor = oldFont.outlineColor,
outline = oldFont.outline,
shadow = oldFont.shadow,
size = value,
}
self.ebInputText.font = Configuration:GetFont(value, "console_font", fontSettings, true)
self.ebInputText:UpdateLayout()
self.tbHistory.font = Configuration:GetFont(value, "console_font", fontSettings, true)
self.tbHistory:UpdateLayout()
end
end
Configuration:AddListener("OnConfigurationChange", onConfigurationChange)
local keyUP, keyDOWN, keyENTER, keyKP_ENTER, keyESCAPE, keyTAB =
Spring.GetKeyCode("up"),
Spring.GetKeyCode("down"),
Spring.GetKeyCode("enter"),
Spring.GetKeyCode("numpad_enter"),
Spring.GetKeyCode("escape"),
Spring.GetKeyCode("tab")
self.ebInputText.KeyPress = function(something, key, ...)
if key == keyTAB then
self:Autocomplete(self.ebInputText.text)
return false
else
self.subword = nil
local up, down = key == keyUP, key == keyDOWN
local newtext, block = false, false
if up or down then
local cursor = self.sentMsgHistoryCursor
local count = self.sentMsgHistoryCount
cursor = cursor + (up and -1 or 1)
if cursor == count + 2 then
-- skip, the msg history has not yet be browsed up, nothing to do
elseif cursor == 0 then
-- skip, we've already gone to the first msg in history
else
if cursor == count + 1 then -- we gone back to the current text
newtext = self.currentMsgBuffer
self.currentMsgBuffer = ""
else
if up and cursor == count then -- we're starting going into history, saving the current text
self.currentMsgBuffer = self.ebInputText.text
end
newtext = self.sentMsgHistory[cursor]
end
self.sentMsgHistoryCursor = cursor
end
elseif key == keyESCAPE then
if self.ebInputText.text ~= "" then
-- clearing field and resetting cursor pos
self.sentMsgHistoryCursor = self.sentMsgHistoryCount + 1
newtext = ""
block = true -- block the action of escape that would leave the current lobby tab
end
end
if newtext then
self.ebInputText:SetText(newtext)
self.ebInputText.cursor = #self.ebInputText.text + 1
self.ebInputText:Invalidate()
return block
end
return Chili.EditBox.KeyPress(something, key, ...)
end
end
self.ebInputText.OnKeyPress = {
function(obj, key, mods, ...)
if key == keyENTER or
key == keyKP_ENTER then
self:SendMessage()
return true
end
end
}
self.fakeImage = Image:New {
x = 0, y = 0,
bottom = 0, right = 0,
OnClick = { function()
screen0:FocusControl(self.ebInputText)
end}
}
self.panel = Control:New {
x = 0,
y = 0,
right = 0,
bottom = 0,
padding = {0, 0, 0, 0},
itemPadding = {0, 0, 0, 0},
itemMargin = {0, 0, 0, 0},
children = {
self.spHistory,
self.ebInputText,
self.fakeImage,
},
}
if not noHistoryLoad then
self:LoadHistory(Configuration.lastLoginChatLength)
end
end
function Console:Autocomplete(textSoFar)
if not self.subword then
local start = 0
for i = 1, 100 do
local newStart = (string.find(textSoFar, " ", start) or (start - 1)) + 1
if start ~= newStart then
start = newStart
else
break
end
end
self.subword = string.sub(textSoFar, start)
self.unmodifiedLength = string.len(textSoFar)
local length = string.len(self.subword)
self.suggestion = 0
self.suggestions = {}
if length == 0 then
return
end
local users = lobby:GetUsers()
for name, _ in pairs(users) do
if name:starts(self.subword) then
self.suggestions[#self.suggestions + 1] = string.sub(name, length + 1)
end
end
end
if #self.suggestions == 0 then
return
end
self.suggestion = self.suggestion + 1
if self.suggestion > #self.suggestions then
self.suggestion = 1
end
self.ebInputText.selStart = self.unmodifiedLength + 1
self.ebInputText.selEnd = string.len(textSoFar) + 1
self.ebInputText:ClearSelected()
self.ebInputText:TextInput(self.suggestions[self.suggestion])
end
function Console:SendMessage()
if self.ebInputText.text ~= "" then
message = self.ebInputText.text
local cursor = self.sentMsgHistoryCursor
local count = self.sentMsgHistoryCount
if cursor == count and self.sentMsgHistory[cursor] == self.ebInputText.text then
-- skip, don't add to history the last message coming from history
self.sentMsgHistoryCursor = count + 1
else
if count == self.sentMsgHistoryMax then
table.remove(self.sentMsgHistory, 1)
else
count = count + 1
self.sentMsgHistoryCount = count
end
self.sentMsgHistory[count] = message
self.sentMsgHistoryCursor = count + 1
end
if self.listener then
self.listener(message)
end
self.ebInputText:SetText("")
end
end
-- TODO: Refactor method to use an options table
-- if date is not passed, current time is assumed
function Console:AddMessage(message, userName, dateOverride, color, thirdPerson, nameColor, nameTooltip, supressNameClick, suppressLogging)
nameColor = nameColor or "\255\50\160\255"
nameTooltip = nameTooltip or (userName and ("user_chat_s_" .. userName))
local txt = ""
local whiteText = ""
if self.showDate then
local timeOverride
if dateOverride then
local utcHour = tonumber(os.date("!%H"))
local utcMinute = tonumber(os.date("!%M"))
local localHour = tonumber(os.date("%H"))
local localMinute = tonumber(os.date("%M"))
local messageHour = tonumber(string.sub(dateOverride, 12, 13))
local messageMinute = tonumber(string.sub(dateOverride, 15, 16))
if messageHour and messageMinute then
local hour = (localHour - utcHour + messageHour)%24
if hour < 10 then
hour = "0" .. hour
end
local minute = (localMinute - utcMinute + messageMinute)%60
if minute < 10 then
minute = "0" .. minute
end
timeOverride = hour .. ":" .. minute
else
Spring.Echo("Bad dateOverride", dateOverride, messageHour, messageMinute)
end
end
-- FIXME: the input "date" should ideally be a table so we can coerce the format
local currentDate = timeOverride or os.date(self.dateFormat)
txt = txt .. "\255\128\128\128[" .. currentDate .. "] "
whiteText = whiteText .. "[" .. currentDate .. "] "
if color ~= nil then
txt = txt .. color
end
end
local textTooltip, onTextClick
if userName ~= nil then
local userStartIndex, userEndIndex
if thirdPerson then
userStartIndex = #txt
txt = txt .. userName .. " "
userEndIndex = userStartIndex + #userName
else
userStartIndex = #txt + 4
txt = txt .. nameColor .. userName .. ": \255\255\255\255"
userEndIndex = userStartIndex + #userName
whiteText = whiteText .. userName .. ": "
if color ~= nil then
txt = txt .. color
end
end
textTooltip = {
{
startIndex = userStartIndex,
endIndex = userEndIndex,
tooltip = nameTooltip
}
}
if supressNameClick then
onTextClick = {} -- Think about putting discord link here?
else
onTextClick = {
{
startIndex = userStartIndex,
endIndex = userEndIndex,
OnTextClick = {
function()
WG.UserHandler.GetUserDropdownMenu(userName, isBattleChat)
--Spring.Echo("Clicked on " .. userName .. ". TODO: Spawn popup.")
end
}
}
}
end
end
onTextClick, textTooltip = WG.BattleProposalHandler.AddClickableInvites(userName, txt, message, onTextClick or {}, textTooltip or {})
txt = txt .. message
onTextClick, textTooltip = WG.BrowserHandler.AddClickableUrls(txt, onTextClick or {}, textTooltip or {})
whiteText = whiteText .. message
if self.tbHistory.text == "" then
self.tbHistory:SetText(txt, textTooltip, onTextClick)
else
self.tbHistory:AddLine(txt, textTooltip, onTextClick)
end
if self.channelName and not suppressLogging then
Spring.CreateDir("chatLogs")
local logFile, errorMessage = io.open('chatLogs/' .. self.channelName .. ".txt", 'a')
if logFile then
if dateOverride then
logFile:write("\n" .. ((string.sub(dateOverride, 0, 19) .. " - ") or "") .. whiteText)
else
logFile:write("\n" .. whiteText)
end
io.close(logFile)
end
end
end
function Console:SetTopic(newTopic)
if self.currentTopic == nil or newTopic ~= self.currentTopic then
self.currentTopic = newTopic
self:AddMessage(newTopic, nil, nil, Configuration.meColor, nil, nil, nil, nil, true)
end
end
local function lineIterator(s)
if s:sub(-1)~="\n" then s=s.."\n" end
return s:gmatch("(.-)\n")
end
-- remove trailing whitespace from string.
local function rtrim(s)
local n = #s
while n > 0 and s:find("^%s", n) do n = n - 1 end
return s:sub(1, n)
end
function Console:LoadHistory(numLines)
if not self.channelName then
return
end
local path = 'chatLogs/' .. self.channelName .. ".txt"
if not VFS.FileExists(path) then
return
end
local logfile = VFS.LoadFile(path)
local lineCount = 0
for line in lineIterator(logfile) do
lineCount = lineCount + 1
end
local waitToWrite = lineCount - numLines
for line in lineIterator(logfile) do
if waitToWrite > 0 then
waitToWrite = waitToWrite - 1
else
line = rtrim(line)
local start = string.find(line, "%[")
if start then
local txt = "\255\128\128\128" .. string.sub(line, start)
if self.tbHistory.text == "" then
self.tbHistory:SetText(txt)
else
self.tbHistory:AddLine(txt)
end
end
end
end
--local logFile, errorMessage = io.open(, 'r')
end
function Console:ClearHistory()
self.tbHistory:SetText("")
end
function Console:Delete()
self = nil
end