Debian Codegraph @

CorsixTH/Lua/dialogs/resizables/hotkey_assign.lua

raw
1 --[[ Copyright (c) 2019 James "leiget" Russell
2
3 Permission is hereby granted, free of charge, to any person obtaining a copy of
4 this software and associated documentation files (the "Software"), to deal in
5 the Software without restriction, including without limitation the rights to
6 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
7 of the Software, and to permit persons to whom the Software is furnished to do
8 so, subject to the following conditions:
9
10 The above copyright notice and this permission notice shall be included in all
11 copies or substantial portions of the Software.
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19 SOFTWARE. --]]
20
21 --! Custom key bindings
22 class "UIHotkeyAssign" (UIResizable)
23
24 ---@type UIHotkeyAssign
25 local UIHotkeyAssign = _G["UIHotkeyAssign"]
26
27 local col_bg = {
28 red = 154,
29 green = 146,
30 blue = 198,
31 }
32
33 local col_hotkeybox = {
34 red = 81,
35 green = 76,
36 blue = 150,
37 }
38
39 local col_hotkeybox_lowered = {
40 red = 41,
41 green = 36,
42 blue = 90,
43 }
44
45 local col_highlight = {
46 red = 174,
47 green = 166,
48 blue = 218,
49 }
50
51 local col_shadow = {
52 red = 134,
53 green = 126,
54 blue = 178,
55 }
56
57 local col_caption = {
58 red = 174,
59 green = 166,
60 blue = 218,
61 }
62
63 local hotkey_backup = {}
64 local hotkeys_backedUp = false
65 local hotkey_buttons = {}
66
67 local key_hierarchy = {
68 "ctrl", "alt", "shift",
69 "gui", "menu", "return", "enter", "escape", "backspace", "tab", "space",
70 "!", "\"", "#", "%", "$", "&", "\'", "(", ")", "*", "+", ",", "-", ".", "/",
71 "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
72 ":", ";", "<", "=", ">", "?", "@", "[", "\\", "]", "^", "_", "`",
73 "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
74 "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
75 "capslock",
76 "f1", "f2", "f3", "f4", "f5", "f6", "f7", "f8", "f9", "f10", "f11", "f12",
77 "printscreen", "scrolllock", "pause", "insert", "home", "pageup", "delete",
78 "end", "pagedown", "right", "left", "down", "up", "numlock",
79 "f13", "f14", "f15", "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
80 "f24",
81 "keypad 0", "keypad 1", "keypad 2", "keypad 3", "keypad 4", "keypad 6",
82 "keypad 7", "keypad 8", "keypad 9", "keypad .",
83 }
84
85 --! Removes qualifiers like left and right from modifier keys
86 --! e.g. left shift becomes shift.
87 --!
88 --!param noted_keys (table) Array of keys to normalize
89 --!return (table) New array of normalized keys
90 local function normalize_modifiers(noted_keys)
91 local res = shallow_clone(noted_keys)
92
93 -- Go through the new key table and remove "left" or "right" from any modifier strings.
94 for k, v in ipairs(res) do
95 -- Ctrl
96 if string.find(v, "ctrl", 1, true) then
97 res[k] = "ctrl"
98 end
99 -- Alt
100 if string.find(v, "alt", 1, true) then
101 res[k] = "alt"
102 end
103 -- Shift
104 if string.find(v, "shift", 1, true) then
105 res[k] = "shift"
106 end
107 -- GUI
108 if string.find(v, "gui", 1, true) then
109 res[k] = "gui"
110 end
111 -- MENU
112 if string.find(v, "menu", 1, true) then
113 res[k] = "menu"
114 end
115 end
116
117 return res
118 end
119
120 --! Return an array of the keys from the given array, sorted into a
121 --! deterministic order.
122 local function sort_noted_keys(noted_keys)
123 -- Go through the new noted_keys and order it according to the key hairarchy.
124 local result = {}
125 local idx = 1
126
127 for _, v1 in ipairs(key_hierarchy) do
128 for _, v2 in ipairs(noted_keys) do
129 if v1 == v2 then
130 result[idx] = v2
131 idx = idx + 1
132 end
133 end
134 end
135
136 return result
137 end
138
139 --! Return an array with the modifier keys removed.
140 --! Used when assigning the scroll keys.
141 --!
142 --!param noted_keys (table) Array of keys to check for modifiers
143 --!return (table) New array of keys without modifiers
144 local function remove_modifiers(noted_keys)
145 local res = shallow_clone(noted_keys)
146
147 for k, v in ipairs(res) do
148 -- Ctrl
149 if string.find(v, "ctrl", 1, true) then
150 table.remove(res, k)
151 end
152 -- Alt
153 if string.find(v, "alt", 1, true) then
154 table.remove(res, k)
155 end
156 -- Shift
157 if string.find(v, "shift", 1, true) then
158 table.remove(res, k)
159 end
160 -- GUI
161 if string.find(v, "gui", 1, true) then
162 table.remove(res, k)
163 end
164 -- MENU
165 if string.find(v, "menu", 1, true) then
166 table.remove(res, k)
167 end
168 end
169
170 return res
171 end
172
173 --! Determine if the given key sequence is already being used for an action in
174 --! the app.
175 --!
176 --!param keys (table) A sorted array of the key sequence to test
177 --!param app (App) The App
178 --!return (boolean,string) Whether the key is used, and if so, for what action
179 local function is_hotkey_used(keys, app)
180 -- Find out if there is another hotkey with the same key assignment.
181 -- Make sure it's not the same key we are currently mapping.
182 local our_key_str = serialize(keys)
183 local key_str = ""
184 -- Go through the app.hotkeys table...
185 for k, _ in pairs(app.hotkeys) do
186 if type(app.hotkeys[k]) == "table" then
187 key_str = serialize(app.hotkeys[k])
188 elseif type(app.hotkeys[k]) == "string" then
189 key_str = serialize({app.hotkeys[k]})
190 end
191
192 -- If the key(s) that were pressed match the current key in the "app.hotkey" table...
193 if our_key_str == key_str then
194 return true, k
195 end
196 end
197
198 return false
199 end
200
201 --! Assign the given key sequence to a hotkey.
202 --!
203 --! Validates a given key sequence and applies it to the given hotkey.
204 --! If the key sequence is already used, first swap that hotkey with the given
205 --! one to prevent duplicates.
206 --!
207 --!param hotkey (string) The hotkey to change
208 --!param hotkey_buttons_table (table) Configuration table of all hotkeys
209 --!param app (App) The App
210 local function hotkey_input(hotkey, hotkey_buttons_table, app)
211 --[[
212 TODO:
213 -- Keypad when numlock is off doesn't work correctly.
214 Seems that keypad input isn't working correctly in ui.lua or something.
215 Left for future patch.
216 -- Disable the "global_exitApp" hotkey while assigning hotkeys?
217 Even when "global_exitApp" isn't added at startup Alt+F4 still abandons program. Why?
218 -- Modifier keys for other languages necessary?
219 -- Ex: STRG for german's "CTRL".
220 ]]
221
222 local noted_keys = hotkey_buttons_table[hotkey].noted_keys
223
224 -- Check if the table even has anything or has too much.
225 local table_length = #noted_keys
226 if table_length == 0 or table_length > 4 then
227 hotkey_buttons_table[hotkey]:abort()
228 return
229 end
230
231 -- If the noted key input is "enter", "return", or "escape"...
232 if array_join(noted_keys) == array_join( {"enter"} ) or
233 array_join(noted_keys) == array_join( {"return"} ) or
234 array_join(noted_keys) == array_join( {"escape"} ) then
235 -- Abort, as we don't want the enter or esc key used for anything other
236 -- than "global_confirm" and "global_cancel".
237 hotkey_buttons_table[hotkey]:abort()
238 return
239 end
240
241 noted_keys = normalize_modifiers(noted_keys)
242 noted_keys = sort_noted_keys(noted_keys)
243
244 -- If the current hotkey is a scroll key...
245 if hotkey == "ingame_scroll_up" or
246 hotkey == "ingame_scroll_down" or
247 hotkey == "ingame_scroll_left" or
248 hotkey == "ingame_scroll_right" then
249 -- Get rid of any modifiers, as they won't work correctly, anyway.
250 noted_keys = remove_modifiers(noted_keys)
251 end
252
253 local hotkey_used, hotkey_used_key = is_hotkey_used(noted_keys, app)
254
255 -- If this hotkey was used for a different action, swap with current assignment
256 if hotkey_used and hotkey ~= hotkey_used_key then
257 app.hotkeys[hotkey_used_key] = shallow_clone(app.hotkeys[hotkey])
258 if hotkey_buttons_table[hotkey_used_key] then
259 hotkey_buttons_table[hotkey_used_key]:setText( string.upper(array_join(app.hotkeys[hotkey_used_key], "+")) )
260 end
261 end
262
263 if #noted_keys == 1 then
264 noted_keys = noted_keys[1]
265 end
266
267 app.hotkeys[hotkey] = noted_keys
268 -- If the key is "global_cancel_alt"...
269 if hotkey == "global_cancel_alt" then
270 app.hotkeys["global_stop_movie_alt"] = noted_keys
271 app.hotkeys["global_window_close_alt"] = noted_keys
272 end
273
274 hotkey_buttons_table[hotkey]:setText( string.upper( array_join(app.hotkeys[hotkey], "+") ) )
275 end
276
277 function UIHotkeyAssign:UIHotkeyAssign(ui, mode)
278 self:UIResizable(ui, 640, 480, col_bg)
279
280 local current_pos_y = 1
281
282 -- Panel x position table.
283 self.panel_pos_table_x = {}
284 self.panel_pos_table_x[1] = 10
285 self.panel_pos_table_x[2] = 220
286 self.panel_pos_table_x[3] = 430
287
288 -- Panel y button position table.
289 self.panel_pos_table_y = {}
290 for i=1, 12, 1 do
291 self.panel_pos_table_y[i] = (i*30)+15
292 end
293
294 self.ui = ui
295 self.mode = mode
296 self.modal_class = mode == "menu" and "main menu" or "options" or "folders"
297 self.on_top = mode == "menu"
298 self.esc_closes = true
299 self.resizable = false
300 self:setDefaultPosition(0.5, 0.25)
301 self.default_button_sound = "selectx.wav"
302 self.app = ui.app
303 self.hotkey_buttons = hotkey_buttons
304
305 if not hotkeys_backedUp then
306 hotkey_backup = shallow_clone(self.app.hotkeys)
307 hotkeys_backedUp = true
308 end
309
310 self.key_pane = nil
311 self.key_options = {{
312 id = "global",
313 title = _S.hotkey_window.panel_globalKeys,
314 tooltip = _S.tooltip.hotkey_window.panel_globalKeys,
315 sections = {{
316 title = _S.hotkey_window.panel_globalKeys,
317 keys = {
318 "global_exitApp",
319 "global_resetApp",
320 "global_screenshot",
321 "global_releaseMouse",
322 "global_confirm_alt",
323 "global_cancel_alt" }}}},
324 {
325 id = "ingame",
326 title = _S.hotkey_window.panel_generalInGameKeys,
327 tooltip = _S.tooltip.hotkey_window.panel_generalInGameKeys,
328 sections = {{
329 title = _S.hotkey_window.panel_generalInGameKeys,
330 keys = {
331 "ingame_showmenubar",
332 "ingame_saveMenu",
333 "ingame_loadMenu",
334 "ingame_jukebox",
335 "ingame_openFirstMessage",
336 "ingame_quickSave",
337 "ingame_quickLoad",
338 "ingame_restartLevel",
339 "ingame_quitLevel",
340 "ingame_rotateobject",
341 "ingame_sellPickedUpItem",
342 "ingame_patient_gohome",
343 "ingame_setTransparent",
344 "ingame_toggleTransparent" }}}},
345 {
346 id = "speed",
347 title = _S.hotkey_window.button_gameSpeedKeys,
348 tooltip = _S.tooltip.hotkey_window.button_gameSpeedKeys,
349 sections = {{
350 title = _S.hotkey_window.panel_gameSpeedKeys,
351 keys = {
352 "ingame_pause",
353 "ingame_gamespeed_slowest",
354 "ingame_gamespeed_slower",
355 "ingame_gamespeed_normal",
356 "ingame_gamespeed_max",
357 "ingame_gamespeed_thensome",
358 "ingame_gamespeed_speedup" }}}},
359 {
360 id = "scroll",
361 title = _S.hotkey_window.panel_scrollKeys,
362 tooltip = _S.tooltip.hotkey_window.panel_scrollKeys,
363 sections = {{
364 title = _S.hotkey_window.panel_scrollKeys,
365 keys = {
366 "ingame_scroll_up",
367 "ingame_scroll_down",
368 "ingame_scroll_left",
369 "ingame_scroll_right",
370 "ingame_scroll_shift"
371 }}}},
372 {
373 id = "zoom",
374 title = _S.hotkey_window.panel_zoomKeys,
375 tooltip = _S.tooltip.hotkey_window.panel_zoomKeys,
376 sections = {{
377 title = _S.hotkey_window.panel_zoomKeys,
378 keys = {
379 "ingame_zoom_in",
380 "ingame_zoom_in_more",
381 "ingame_zoom_out",
382 "ingame_zoom_out_more",
383 "ingame_reset_zoom" }}}},
384 {
385 id = "toggle",
386 title = _S.hotkey_window.panel_toggleKeys,
387 tooltip = _S.tooltip.hotkey_window.panel_toggleKeys,
388 sections = {{
389 title = _S.hotkey_window.panel_toggleKeys,
390 keys = {
391 "ingame_toggleAnnouncements",
392 "ingame_toggleSounds",
393 "ingame_toggleMusic",
394 "ingame_toggleAdvisor",
395 "ingame_toggleInfo" }}}},
396 {
397 id = "panels",
398 title = _S.hotkey_window.caption_panels,
399 tooltip = _S.tooltip.hotkey_window.caption_panels,
400 sections = {{
401 title = _S.hotkey_window.caption_panels,
402 keys = {
403 "ingame_panel_bankManager",
404 "ingame_panel_bankStats",
405 "ingame_panel_staffManage",
406 "ingame_panel_townMap",
407 "ingame_panel_casebook",
408 "ingame_panel_research",
409 "ingame_panel_status",
410 "ingame_panel_charts",
411 "ingame_panel_policy",
412 "ingame_panel_machineMenu",
413 "ingame_panel_adviserHistory",
414 "ingame_panel_buildRoom",
415 "ingame_panel_furnishCorridor",
416 "ingame_panel_editRoom",
417 "ingame_panel_hireStaff",}}}},
418 {
419 -- FIXME: Rename to "Alternate Panel Keys"
420 -- New strings required but string freeze applies at time of patch
421 id = "panels2",
422 title = _S.hotkey_window.caption_panels,
423 tooltip = _S.tooltip.hotkey_window.caption_panels,
424 sections = {{
425 title = _S.hotkey_window.caption_panels,
426 keys = {
427 "ingame_panel_map_alt",
428 "ingame_panel_research_alt",
429 "ingame_panel_casebook_alt",
430 "ingame_panel_casebook_alt02" }}}},
431 {
432 id = "recall",
433 title = _S.hotkey_window.button_recallPosKeys,
434 tooltip = _S.tooltip.hotkey_window.button_recallPosKeys,
435 sections = {{
436 title = _S.hotkey_window.panel_storePosKeys,
437 keys = {
438 "ingame_storePosition_1",
439 "ingame_storePosition_2",
440 "ingame_storePosition_3",
441 "ingame_storePosition_4",
442 "ingame_storePosition_5",
443 "ingame_storePosition_6",
444 "ingame_storePosition_7",
445 "ingame_storePosition_8",
446 "ingame_storePosition_9",
447 "ingame_storePosition_0" }},
448 {
449 title = _S.hotkey_window.panel_recallPosKeys,
450 keys = {
451 "ingame_recallPosition_1",
452 "ingame_recallPosition_2",
453 "ingame_recallPosition_3",
454 "ingame_recallPosition_4",
455 "ingame_recallPosition_5",
456 "ingame_recallPosition_6",
457 "ingame_recallPosition_7",
458 "ingame_recallPosition_8",
459 "ingame_recallPosition_9",
460 "ingame_recallPosition_0" }}}}}
461
462 if self.ui.app.config.debug then
463 table.insert(self.key_options, {
464 id = "debug",
465 title = _S.hotkey_window.panel_debugKeys,
466 tooltip = _S.tooltip.hotkey_window.panel_debugKeys,
467 sections = {{
468 title = _S.hotkey_window.panel_debugKeys,
469 keys = {
470 "global_showLuaConsole",
471 "global_runDebugScript",
472 "ingame_showCheatWindow",
473 "ingame_poopLog",
474 "ingame_poopStrings" }}}})
475 end
476
477 -- Title
478 self:addBevelPanel(220, 10, 200, 20, col_caption):setLabel(_S.hotkey_window.caption_main)
479
480 -- Location of original game
481 local built_in = self.app.gfx:loadMenuFont()
482
483 self.key_windows = {}
484 for _, key_opt_set in ipairs(self.key_options) do
485 local win = UIHotkeyAssignKeyPane(220, 0, ui, key_opt_set, self.app.hotkeys)
486 self:addWindow(win)
487
488 local btn_x = self.panel_pos_table_x[1]
489 local btn_y = self.panel_pos_table_y[current_pos_y]
490 local btn = self:addBevelPanel(btn_x, btn_y, 200, 30, col_bg):setLabel(key_opt_set.title)
491 :makeButton(0, 0, 200, 30, nil, function(s) s:showKeyPane(key_opt_set.id) end)
492 :makeToggle()
493 :setTooltip(key_opt_set.tooltip)
494
495 self.key_windows[key_opt_set.id] = { window = win, button = btn }
496 current_pos_y = current_pos_y + 1
497 end
498
499 self:showKeyPane("global")
500
501 -- "Accept" button
502 self:addBevelPanel(self.panel_pos_table_x[1], 430, 200, 40, col_bg)
503 :setLabel(_S.hotkey_window.button_accept)
504 :makeButton(0, 0, 180, 40, nil, self.buttonAccept)
505 :setTooltip(_S.tooltip.hotkey_window.button_accept)
506 -- Reset to defaults button.
507 self:addBevelPanel(self.panel_pos_table_x[2], 430, 200, 40, col_bg)
508 :setLabel(_S.hotkey_window.button_defaults)
509 :makeButton(0, 0, 180, 40, nil, self.buttonDefaults)
510 :setTooltip(_S.tooltip.hotkey_window.button_defaults)
511 -- "Cancel" button
512 self:addBevelPanel(self.panel_pos_table_x[3], 430, 200, 40, col_bg)
513 :setLabel(_S.hotkey_window.button_cancel)
514 :makeButton(0, 0, 180, 40, nil, self.buttonCancel)
515 :setTooltip(_S.tooltip.hotkey_window.button_cancel)
516
517 self.built_in_font = built_in
518 end
519
520 function UIHotkeyAssign:buttonAccept()
521 self.app:saveHotkeys()
522 hotkeys_backedUp = false
523
524 self:close()
525 local window = UIOptions(self.ui, "menu")
526 self.ui:addWindow(window)
527 end
528
529 function UIHotkeyAssign:buttonDefaults()
530 -- Copy the default hotkeys into the app's current hotkey table.
531 self.app.hotkeys = corsixth.require("config_finder").hotkeys_defaults()
532
533 -- Reload all hotkey boxes' text.
534 for _, v in pairs(self.key_windows) do
535 v.window:resetAllButtonText(self.app.hotkeys)
536 end
537 end
538
539 function UIHotkeyAssign:buttonCancel()
540 --Reset all keys back to what they were before opening the hotkey window.
541 self.app.hotkeys = shallow_clone(hotkey_backup)
542 hotkeys_backedUp = false
543
544 self:close()
545 local window = UIOptions(self.ui, "menu")
546 self.ui:addWindow(window)
547 end
548
549 function UIHotkeyAssign:close()
550 UIResizable.close(self)
551 if self.mode == "menu" then
552 self.ui:addWindow(UIOptions(self.ui, "menu"))
553 end
554 end
555
556 function UIHotkeyAssign:showKeyPane(pane)
557 self.key_pane = pane
558 for _, box in ipairs(self.ui.hotkeyboxes) do
559 box:abort()
560 end
561 for k, v in pairs(self.key_windows) do
562 v.window:setVisible(k == pane)
563 v.button:setToggleState(k == pane)
564 end
565 end
566
567 --! Child window for setting key bindings
568 class "UIHotkeyAssignKeyPane" (Window)
569
570 ---@type UIHotkeyAssignKeyPane
571 local UIHotkeyAssignKeyPane = _G["UIHotkeyAssignKeyPane"]
572
573 function UIHotkeyAssignKeyPane:UIHotkeyAssignKeyPane(x, y, ui, key_options, app_hotkeys)
574 self:Window()
575
576 self.x = x
577 self.y = y
578 self.ui = ui
579 self.hotkey_buttons = {}
580
581 local panel_width = 100
582 local panel_height = 20
583
584 -- Panel x position table.
585 self.panel_pos_table_x = {}
586 self.panel_pos_table_x[1] = 0
587 self.panel_pos_table_x[2] = 210
588 if #key_options.sections == 1 then
589 panel_width = 205
590 elseif #key_options.sections == 2 then
591 panel_width = 100
592 end
593
594 -- Panel y position table.
595 self.panel_pos_table_y = {}
596 for i=1, 20, 1 do
597 self.panel_pos_table_y[i] = (i*20)+25
598 end
599
600 local current_pos_x = 1
601 for _, section in ipairs(key_options.sections) do
602 local current_pos_y = 1
603 local pos_x = self.panel_pos_table_x[current_pos_x]
604 local pos_y = self.panel_pos_table_y[current_pos_y]
605 self:addBevelPanel(pos_x, pos_y, panel_width*2, panel_height, col_caption):setLabel(section.title)
606 current_pos_y = current_pos_y + 1
607 for _, key in ipairs(section.keys) do
608 pos_x = self.panel_pos_table_x[current_pos_x]
609 pos_y = self.panel_pos_table_y[current_pos_y]
610 self:addBevelPanel(pos_x, pos_y, panel_width, panel_height, col_shadow, col_bg, col_bg):setLabel(_S.hotkey_window[key])
611 self.hotkey_buttons[key] = self:addBevelPanel(
612 pos_x + panel_width, pos_y, panel_width, panel_height, col_hotkeybox,
613 col_highlight, col_shadow, nil, col_hotkeybox_lowered)
614 :makeHotkeyBox(function() self:confirm_func(key) end, nil)
615 :setText(string.upper(array_join(app_hotkeys[key], "+")))
616 current_pos_y = current_pos_y + 1
617 end
618 current_pos_x = current_pos_x + 1
619 end
620 end
621
622 function UIHotkeyAssignKeyPane:setVisible(visibility)
623 self.visible = visibility
624 end
625
626 function UIHotkeyAssignKeyPane:confirm_func(hotkey)
627 hotkey_input(hotkey, self.hotkey_buttons, _G.TheApp)
628 end
629
630 function UIHotkeyAssignKeyPane:resetAllButtonText(app_hotkeys)
631 for k, _ in pairs(self.hotkey_buttons) do
632 self.hotkey_buttons[k]:setText(string.upper(array_join(app_hotkeys[k], "+")))
633 end
634 end