Port Custom hotkeys from TGMC (#47003)

* custom keybindings

* Update _lists.dm

* Update robot.dm

* modify weights and clean up some vars

* Update say.dm

* Apply suggestions from code review

Co-Authored-By: Emmett Gaines <ninjanomnom@gmail.com>

* some review changes

* formatting

* include focus hack, remove me_wrapper, give default keybinds to new characters, misc fixes

* revert hack and more reviews

* remove another focus hack

this was causing issues with the keydown proc returning early
This commit is contained in:
Couls
2019-11-03 01:35:30 -07:00
committed by Rob Bailey
co-authored by Emmett Gaines
parent 36643ddf36
commit 070bb5e69c
29 changed files with 874 additions and 339 deletions
+129 -5
View File
@@ -28,6 +28,10 @@ GLOBAL_LIST_EMPTY(preferences_datums)
var/UI_style = null
var/buttons_locked = FALSE
var/hotkeys = FALSE
// Custom Keybindings
var/list/key_bindings = list()
var/tgui_fancy = TRUE
var/tgui_lock = TRUE
var/windowflashing = TRUE
@@ -125,6 +129,7 @@ GLOBAL_LIST_EMPTY(preferences_datums)
return
//we couldn't load character data so just randomize the character appearance + name
random_character() //let's create a random character then - rather than a fat, bald and naked man.
key_bindings = deepCopyList(GLOB.keybinding_list_by_key) // give them default keybinds too
real_name = pref_species.random_name(gender,1)
if(!loaded_preferences_successfully)
save_preferences()
@@ -147,6 +152,7 @@ GLOBAL_LIST_EMPTY(preferences_datums)
dat += "<a href='?_src_=prefs;preference=tab;tab=0' [current_tab == 0 ? "class='linkOn'" : ""]>Character Settings</a>"
dat += "<a href='?_src_=prefs;preference=tab;tab=1' [current_tab == 1 ? "class='linkOn'" : ""]>Game Preferences</a>"
dat += "<a href='?_src_=prefs;preference=tab;tab=2' [current_tab == 2 ? "class='linkOn'" : ""]>OOC Preferences</a>"
dat += "<a href='?_src_=prefs;preference=tab;tab=3' [current_tab == 3 ? "class='linkOn'" : ""]>Custom Keybindings</a>"
if(!path)
dat += "<div class='notice'>Please create an account to save your preferences</div>"
@@ -512,7 +518,7 @@ GLOBAL_LIST_EMPTY(preferences_datums)
dat += "<b>tgui Style:</b> <a href='?_src_=prefs;preference=tgui_fancy'>[(tgui_fancy) ? "Fancy" : "No Frills"]</a><br>"
dat += "<br>"
dat += "<b>Action Buttons:</b> <a href='?_src_=prefs;preference=action_buttons'>[(buttons_locked) ? "Locked In Place" : "Unlocked"]</a><br>"
dat += "<b>Keybindings:</b> <a href='?_src_=prefs;preference=hotkeys'>[(hotkeys) ? "Hotkeys" : "Default"]</a><br>"
dat += "<b>Hotkey mode:</b> <a href='?_src_=prefs;preference=hotkeys'>[(hotkeys) ? "Hotkeys" : "Default"]</a><br>"
dat += "<br>"
dat += "<b>PDA Color:</b> <span style='border:1px solid #161616; background-color: [pda_color];'>&nbsp;&nbsp;&nbsp;</span> <a href='?_src_=prefs;preference=pda_color;task=input'>Change</a><BR>"
dat += "<b>PDA Style:</b> <a href='?_src_=prefs;task=input;preference=pda_style'>[pda_style]</a><br>"
@@ -616,9 +622,7 @@ GLOBAL_LIST_EMPTY(preferences_datums)
dat += "<b>Be [capitalize(i)]:</b> <a href='?_src_=prefs;preference=be_special;be_special_type=[i]'>[(i in be_special) ? "Enabled" : "Disabled"]</a><br>"
dat += "<br>"
dat += "<b>Midround Antagonist:</b> <a href='?_src_=prefs;preference=allow_midround_antag'>[(toggles & MIDROUND_ANTAG) ? "Enabled" : "Disabled"]</a><br>"
dat += "</td></tr></table>"
if(2) //OOC Preferences
dat += "<table><tr><td width='340px' height='300px' valign='top'>"
dat += "<h2>OOC Settings</h2>"
@@ -687,7 +691,36 @@ GLOBAL_LIST_EMPTY(preferences_datums)
dat += "</td>"
dat += "</tr></table>"
if(3) // Custom keybindings
// Create an inverted list of keybindings -> key
var/list/user_binds = list()
for (var/key in key_bindings)
for(var/kb_name in key_bindings[key])
user_binds[kb_name] = key
var/list/kb_categories = list()
// Group keybinds by category
for (var/name in GLOB.keybindings_by_name)
var/datum/keybinding/kb = GLOB.keybindings_by_name[name]
if (!(kb.category in kb_categories))
kb_categories[kb.category] = list()
kb_categories[kb.category] += list(kb)
dat += "<style>label { display: inline-block; width: 200px; }</style><body>"
for (var/category in kb_categories)
dat += "<h3>[category]</h3>"
for (var/i in kb_categories[category])
var/datum/keybinding/kb = i
var/bound_key = user_binds[kb.name]
bound_key = (bound_key) ? bound_key : "Unbound"
dat += "<label>[kb.full_name]</label> <a href ='?_src_=prefs;preference=keybindings_capture;keybinding=[kb.name];old_key=[bound_key]'>[bound_key] Default: ( [kb.key] )</a>"
dat += "<br>"
dat += "<br><br>"
dat += "<a href ='?_src_=prefs;preference=keybindings_reset'>\[Reset to default\]</a>"
dat += "</body>"
dat += "<hr><center>"
if(!IsGuestKey(user.key))
@@ -706,6 +739,28 @@ GLOBAL_LIST_EMPTY(preferences_datums)
#undef APPEARANCE_CATEGORY_COLUMN
#undef MAX_MUTANT_ROWS
/datum/preferences/proc/CaptureKeybinding(mob/user, datum/keybinding/kb, var/old_key)
var/HTML = {"
<div id='focus' style="outline: 0;" tabindex=0>Keybinding: [kb.full_name]<br>[kb.description]<br><br><b>Press any key to change<br>Press ESC to clear</b></div>
<script>
document.onkeyup = function(e) {
var shift = e.shiftKey ? 1 : 0;
var alt = e.altKey ? 1 : 0;
var ctrl = e.ctrlKey ? 1 : 0;
var numpad = (95 < e.keyCode && e.keyCode < 112) ? 1 : 0;
var escPressed = e.keyCode == 27 ? 1 : 0;
var url = 'byond://?_src_=prefs;preference=keybindings_set;keybinding=[kb.name];old_key=[old_key];clear_key='+escPressed+';key='+e.key+';shift='+shift+';alt='+alt+';ctrl='+ctrl+';numpad='+numpad+';key_code='+e.keyCode;
window.location=url;
}
document.getElementById('focus').focus();
</script>
"}
winshow(user, "capturekeypress", TRUE)
var/datum/browser/popup = new(user, "capturekeypress", "<div align='center'>Keybindings</div>", 350, 300)
popup.set_content(HTML)
popup.open(FALSE)
onclose(user, "capturekeypress", src)
/datum/preferences/proc/SetChoices(mob/user, limit = 17, list/splitJobs = list("Chief Engineer"), widthPerColumn = 295, height = 620)
if(!SSjob)
return
@@ -1438,9 +1493,78 @@ GLOBAL_LIST_EMPTY(preferences_datums)
if("hotkeys")
hotkeys = !hotkeys
if(hotkeys)
winset(user, null, "input.focus=true input.background-color=[COLOR_INPUT_ENABLED] mainwindow.macro=default")
winset(user, null, "input.focus=true input.background-color=[COLOR_INPUT_ENABLED]")
else
winset(user, null, "input.focus=true input.background-color=[COLOR_INPUT_ENABLED] mainwindow.macro=old_default")
winset(user, null, "input.focus=true input.background-color=[COLOR_INPUT_DISABLED]")
if("keybindings_capture")
var/datum/keybinding/kb = GLOB.keybindings_by_name[href_list["keybinding"]]
var/old_key = href_list["old_key"]
CaptureKeybinding(user, kb, old_key)
return
if("keybindings_set")
var/kb_name = href_list["keybinding"]
if(!kb_name)
user << browse(null, "window=capturekeypress")
ShowChoices(user)
return
var/clear_key = text2num(href_list["clear_key"])
var/old_key = href_list["old_key"]
if(clear_key)
if(old_key != "Unbound") // if it was already set
key_bindings[old_key] -= kb_name
key_bindings["Unbound"] += list(kb_name)
user << browse(null, "window=capturekeypress")
save_preferences()
ShowChoices(user)
return
var/key = href_list["key"]
var/numpad = text2num(href_list["numpad"])
var/AltMod = text2num(href_list["alt"]) ? "Alt-" : ""
var/CtrlMod = text2num(href_list["ctrl"]) ? "Ctrl-" : ""
var/ShiftMod = text2num(href_list["shift"]) ? "Shift-" : ""
// var/key_code = text2num(href_list["key_code"])
var/new_key = uppertext(key)
// This is a mapping from JS keys to Byond - ref: https://keycode.info/
var/static/list/_kbMap = list(
"UP" = "North",
"RIGHT" = "East",
"DOWN" = "South",
"LEFT" = "West",
"INSERT" = "Insert",
"HOME" = "Northwest",
"PAGEUP" = "Northeast",
"DEL" = "Delete",
"END" = "Southwest",
"PAGEDOWN" = "Southeast",
"SPACEBAR" = "Space",
"ALT" = "Alt",
"SHIFT" = "Shift",
"CONTROL" = "Ctrl"
)
new_key = _kbMap[new_key] ? _kbMap[new_key] : new_key
if (numpad)
new_key = "Numpad[new_key]"
var/full_key = "[AltMod][CtrlMod][ShiftMod][new_key]"
if(!key_bindings[old_key])
key_bindings[old_key] = list()
key_bindings[old_key] -= kb_name
key_bindings[full_key] += list(kb_name)
key_bindings[full_key] = sortList(key_bindings[full_key])
user << browse(null, "window=capturekeypress")
save_preferences()
if("keybindings_reset")
key_bindings = deepCopyList(GLOB.keybinding_list_by_key)
if("action_buttons")
buttons_locked = !buttons_locked
if("tgui_fancy")
+14 -3
View File
@@ -5,7 +5,7 @@
// You do not need to raise this if you are adding new values that have sane defaults.
// Only raise this value when changing the meaning/format/name/layout of an existing value
// where you would want the updater procs below to run
#define SAVEFILE_VERSION_MAX 25
#define SAVEFILE_VERSION_MAX 26
/*
SAVEFILE UPDATING/VERSIONING - 'Simplified', or rather, more coder-friendly ~Carn
@@ -126,6 +126,14 @@ SAVEFILE UPDATING/VERSIONING - 'Simplified', or rather, more coder-friendly ~Car
S["hair_style_name"] >> hairstyle
if(S["facial_style_name"])
S["facial_style_name"] >> facial_hairstyle
if(current_version < 26)
key_bindings = sanitize_islist(key_bindings, deepCopyList(GLOB.keybinding_list_by_key))
key_bindings["T"] = list(1 = "say")
key_bindings["M"] = list(1 = "me")
key_bindings["O"] = list(1 = "ooc")
key_bindings["L"] = list(1 = "looc")
WRITE_FILE(S["key_bindings"], key_bindings)
/datum/preferences/proc/load_path(ckey,filename="preferences.sav")
if(!ckey)
@@ -183,6 +191,9 @@ SAVEFILE UPDATING/VERSIONING - 'Simplified', or rather, more coder-friendly ~Car
S["pda_style"] >> pda_style
S["pda_color"] >> pda_color
// Custom hotkeys
S["key_bindings"] >> key_bindings
//try to fix any outdated data if necessary
if(needs_update >= 0)
update_preferences(needs_update, S) //needs_update = savefile_version if we need an update (positive integer)
@@ -212,7 +223,7 @@ SAVEFILE UPDATING/VERSIONING - 'Simplified', or rather, more coder-friendly ~Car
be_special = SANITIZE_LIST(be_special)
pda_style = sanitize_inlist(pda_style, GLOB.pda_styles, initial(pda_style))
pda_color = sanitize_hexcolor(pda_color, 6, 1, initial(pda_color))
key_bindings = sanitize_islist(key_bindings, deepCopyList(GLOB.keybinding_list_by_key))
return TRUE
/datum/preferences/proc/save_preferences()
@@ -258,7 +269,7 @@ SAVEFILE UPDATING/VERSIONING - 'Simplified', or rather, more coder-friendly ~Car
WRITE_FILE(S["tip_delay"], tip_delay)
WRITE_FILE(S["pda_style"], pda_style)
WRITE_FILE(S["pda_color"], pda_color)
WRITE_FILE(S["key_bindings"], key_bindings)
return TRUE
/datum/preferences/proc/load_character(slot)
+5
View File
@@ -1,6 +1,11 @@
GLOBAL_VAR_INIT(OOC_COLOR, null)//If this is null, use the CSS for OOC. Otherwise, use a custom colour.
GLOBAL_VAR_INIT(normal_ooc_colour, "#002eb8")
/client/verb/ooc_wrapper()
set hidden = TRUE
var/message = input("", "OOC") as text
ooc(message)
/client/verb/ooc(msg as text)
set name = "OOC" //Gave this shit a shorter name so you only have to time out "ooc" rather than "ooc message" to use it --NeoFite
set category = "OOC"