/
gui_chili_display_keys.lua
378 lines (326 loc) · 8.54 KB
/
gui_chili_display_keys.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
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
function widget:GetInfo()
return {
name = "Display Keys",
desc = "Displays the current key combination.",
author = "GoogleFrog",
date = "12 August 2015",
license = "GNU GPL, v2 or later",
layer = -10000,
enabled = false
}
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
include("keysym.h.lua")
local keyData, mouseData
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Options
options_path = 'Settings/HUD Panels/Extras/Display Keys'
options_order = {
'keyReleaseTimeout', 'mouseReleaseTimeout',
}
options = {
keyReleaseTimeout = {
name = "Key Release Timeout",
type = "number",
value = 0.6, min = 0, max = 5, step = 0.025,
},
mouseReleaseTimeout = {
name = "Mouse Release Timeout",
type = "number",
value = 0.3, min = 0, max = 5, step = 0.025,
},
}
local panelColor = {1,1,1,0.7}
local highlightColor = {1,0.8, 0.1, 0.9}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Window Creation
local function InitializeDisplayLabelControl(name)
local data = {}
local screenWidth, screenHeight = Spring.GetWindowGeometry()
local window = Chili.Window:New{
parent = screen0,
backgroundColor = {0, 0, 0, 0},
color = {0, 0, 0, 0},
dockable = true,
name = name,
padding = {0,0,0,0},
x = 0,
y = 740,
clientWidth = 380,
clientHeight = 64,
draggable = false,
resizable = false,
tweakDraggable = true,
tweakResizable = true,
minimizable = false,
OnMouseDown = ShowOptions,
}
local mainPanel = Chili.Panel:New{
backgroundColor = {1,1,1,0.7},
color = {1,1,1, 0.7},
parent = window,
padding = {0,0,0,0},
y = 0,
x = 0,
right = 0,
bottom = 0,
dockable = false;
draggable = false,
resizable = false,
OnMouseDown = ShowOptions,
}
local displayLabel = Chili.Label:New{
parent = mainPanel,
x = 15,
y = 10,
right = 10,
bottom = 12,
caption = "",
valign = "center",
align = "center",
autosize = false,
font = {
size = 36,
outline = true,
outlineWidth = 2,
outlineWeight = 2,
},
}
local function UpdateWindow(val)
displayLabel:SetCaption(val)
end
local function Dispose()
window:Dispose()
end
local data = {
UpdateWindow = UpdateWindow,
Dispose = Dispose,
}
return data
end
local function InitializeMouseButtonControl(name)
local window = Chili.Window:New{
parent = screen0,
backgroundColor = {0, 0, 0, 0},
color = {0, 0, 0, 0},
dockable = true,
name = name,
padding = {0,0,0,0},
x = 60,
y = 676,
clientWidth = 260,
clientHeight = 64,
draggable = false,
resizable = false,
tweakDraggable = true,
tweakResizable = true,
minimizable = false,
OnMouseDown = ShowOptions,
}
local leftPanel = Chili.Panel:New{
backgroundColor = panelColor,
color = panelColor,
parent = window,
padding = {0,0,0,0},
y = 0,
x = 0,
right = "62%",
bottom = 0,
dockable = false;
draggable = false,
resizable = false,
OnMouseDown = ShowOptions,
}
local middlePanel = Chili.Panel:New{
backgroundColor = panelColor,
color = panelColor,
parent = window,
padding = {0,0,0,0},
y = 0,
x = "38%",
right = "38%",
bottom = 0,
dockable = false;
draggable = false,
resizable = false,
OnMouseDown = ShowOptions,
}
local rightPanel = Chili.Panel:New{
backgroundColor = panelColor,
color = panelColor,
parent = window,
padding = {0,0,0,0},
y = 0,
x = "62%",
right = 0,
bottom = 0,
dockable = false;
draggable = false,
resizable = false,
OnMouseDown = ShowOptions,
}
local function UpdateWindow(val)
if val == 1 then
leftPanel.backgroundColor = highlightColor
leftPanel.color = highlightColor
middlePanel.backgroundColor = panelColor
middlePanel.color = panelColor
rightPanel.backgroundColor = panelColor
rightPanel.color = panelColor
elseif val == 2 then
leftPanel.backgroundColor = panelColor
leftPanel.color = panelColor
middlePanel.backgroundColor = highlightColor
middlePanel.color = highlightColor
rightPanel.backgroundColor = panelColor
rightPanel.color = panelColor
elseif val == 3 then
leftPanel.backgroundColor = panelColor
leftPanel.color = panelColor
middlePanel.backgroundColor = panelColor
middlePanel.color = panelColor
rightPanel.backgroundColor = highlightColor
rightPanel.color = highlightColor
else
leftPanel.backgroundColor = panelColor
leftPanel.color = panelColor
middlePanel.backgroundColor = panelColor
middlePanel.color = panelColor
rightPanel.backgroundColor = panelColor
rightPanel.color = panelColor
end
leftPanel:Invalidate()
middlePanel:Invalidate()
rightPanel:Invalidate()
window:Invalidate()
end
local function Dispose()
window:Dispose()
end
local data = {
UpdateWindow = UpdateWindow,
Dispose = Dispose,
}
return data
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- General Functions
local function DoDelayedUpdate(data, dt)
if not data then
return
end
if not data.updateTime then
return
end
data.updateTime = data.updateTime - dt
if data.updateTime > 0 then
return
end
data.UpdateWindow(data.updateData)
data.updateTime = false
end
function widget:Update(dt)
if mouseData.pressed then
local x, y, lmb, mmb, rmb = Spring.GetMouseState()
if not (lmb or mmb or rmb) then
mouseData.pressed = false
mouseData.updateData = false
mouseData.updateTime = options.mouseReleaseTimeout.value
end
end
DoDelayedUpdate(keyData, dt)
DoDelayedUpdate(mouseData, dt)
end
function widget:Shutdown()
if keyData then
keyData.Dispose()
end
if mouseData then
mouseData.Dispose()
end
end
function widget:Initialize()
Chili = WG.Chili
screen0 = Chili.Screen0
if (not Chili) then
widgetHandler:RemoveWidget()
return
end
mouseData = InitializeMouseButtonControl("Mouse Display", 280)
keyData = InitializeDisplayLabelControl("Key Display", 380)
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Keys
local function IsMod(key)
return key == 32 or key >= 128
end
local onlyMods = false
local function Conc(str, add, val)
if val then
if onlyMods then
return (str or "") .. (str and " + " or "") .. add
else
return (str or "") .. add .. " + "
end
else
return str
end
end
function widget:KeyPress(key, modifier, isRepeat)
if not keyData then
return
end
onlyMods = IsMod(key) and not keyData.pressed
local keyText = Conc(false, "Space", modifier.meta)
keyText = Conc(keyText, "Ctrl", modifier.ctrl)
keyText = Conc(keyText, "Alt", modifier.alt)
keyText = Conc(keyText, "Shift", modifier.shift)
if not onlyMods then
if not keyData.pressed then
keyData.pressedString = string.upper(tostring(string.char(key)))
end
keyData.pressed = true
keyText = (keyText or "") .. keyData.pressedString
end
keyData.UpdateWindow(keyText or "")
keyData.updateData = keyText or ""
keyData.updateTime = false
end
function widget:KeyRelease(key, modifier, isRepeat)
if not keyData then
return
end
if not IsMod(key) then
keyData.pressed = false
end
onlyMods = not keyData.pressed
local keyText = Conc(false, "Space", modifier.meta)
keyText = Conc(keyText, "Ctrl", modifier.ctrl)
keyText = Conc(keyText, "Alt", modifier.alt)
keyText = Conc(keyText, "Shift", modifier.shift)
if not onlyMods then
keyText = (keyText or "") .. keyData.pressedString
end
keyData.updateData = keyText or ""
keyData.updateTime = options.keyReleaseTimeout.value
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
-- Mouse
function widget:MousePress(x, y, button)
if not mouseData then
return
end
mouseData.pressed = true
mouseData.UpdateWindow(button)
mouseData.updateTime = false
end
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------