From 71c3e176bdad701fd7c6f7e41fdc71077254b3aa Mon Sep 17 00:00:00 2001 From: terencehill Date: Sun, 8 Apr 2018 16:46:08 +0200 Subject: [PATCH] Allow key translation --- qcsrc/client/main.qc | 2 + qcsrc/common/util.qc | 140 ++++++++++++++++++++++++++++++++ qcsrc/common/util.qh | 3 + qcsrc/menu/xonotic/keybinder.qc | 3 +- 4 files changed, 147 insertions(+), 1 deletion(-) diff --git a/qcsrc/client/main.qc b/qcsrc/client/main.qc index 37027d25c..935f2b667 100644 --- a/qcsrc/client/main.qc +++ b/qcsrc/client/main.qc @@ -1247,6 +1247,8 @@ string _getcommandkey(string cmd_name, string command, bool forcename) if(!joy_active && substring(key, 0, 3) == "JOY") continue; + key = translate_key(key); + if (keys == "") keys = key; else diff --git a/qcsrc/common/util.qc b/qcsrc/common/util.qc index 35ec9b2c5..cb06ed9ea 100644 --- a/qcsrc/common/util.qc +++ b/qcsrc/common/util.qc @@ -1306,6 +1306,146 @@ float get_model_parameters(string m, float sk) return 1; } +string translate_key(string key) +{ + switch(key) + { + case "TAB": return _("TAB"); + case "ENTER": return _("ENTER"); + case "ESCAPE": return _("ESCAPE"); + case "SPACE": return _("SPACE"); + + case "BACKSPACE": return _("BACKSPACE"); + case "UPARROW": return _("UPARROW"); + case "DOWNARROW": return _("DOWNARROW"); + case "LEFTARROW": return _("LEFTARROW"); + case "RIGHTARROW": return _("RIGHTARROW"); + + case "ALT": return _("ALT"); + case "CTRL": return _("CTRL"); + case "SHIFT": return _("SHIFT"); + + case "F1": return _("F1"); + case "F2": return _("F2"); + case "F3": return _("F3"); + case "F4": return _("F4"); + case "F5": return _("F5"); + case "F6": return _("F6"); + case "F7": return _("F7"); + case "F8": return _("F8"); + case "F9": return _("F9"); + case "F10": return _("F10"); + case "F11": return _("F11"); + case "F12": return _("F12"); + + case "INS": return _("INS"); + case "DEL": return _("DEL"); + case "PGDN": return _("PGDN"); + case "PGUP": return _("PGUP"); + case "HOME": return _("HOME"); + case "END": return _("END"); + + case "PAUSE": return _("PAUSE"); + + case "NUMLOCK": return _("NUMLOCK"); + case "CAPSLOCK": return _("CAPSLOCK"); + case "SCROLLOCK": return _("SCROLLOCK"); + } + + if (substring(key, 0, 3) == "KP_") + { + string subkey = substring(key, 3, -1); + if (IS_DIGIT(substring(key, 3, 1))) // check only first digit + { + return sprintf(_("KP_%d"), stof(subkey)); + } + + switch(subkey) + { + case "INS": return sprintf(_("KP_%s"), _("INS")); + case "END": return sprintf(_("KP_%s"), _("END")); + case "DOWNARROW": return sprintf(_("KP_%s"), _("DOWNARROW")); + case "PGDN": return sprintf(_("KP_%s"), _("PGDN")); + case "LEFTARROW": return sprintf(_("KP_%s"), _("LEFTARROW")); + case "RIGHTARROW": return sprintf(_("KP_%s"), _("RIGHTARROW")); + case "HOME": return sprintf(_("KP_%s"), _("HOME")); + case "UPARROW": return sprintf(_("KP_%s"), _("UPARROW")); + case "PGUP": return sprintf(_("KP_%s"), _("PGUP")); + case "PERIOD": return sprintf(_("KP_%s"), _("PERIOD")); + case "DEL": return sprintf(_("KP_%s"), _("DEL")); + case "DIVIDE": return sprintf(_("KP_%s"), _("DIVIDE")); + case "SLASH": return sprintf(_("KP_%s"), _("SLASH")); + case "MULTIPLY": return sprintf(_("KP_%s"), _("MULTIPLY")); + case "MINUS": return sprintf(_("KP_%s"), _("MINUS")); + case "PLUS": return sprintf(_("KP_%s"), _("PLUS")); + case "ENTER": return sprintf(_("KP_%s"), _("ENTER")); + case "EQUALS": return sprintf(_("KP_%s"), _("EQUALS")); + default: return key; + } + } + + if (key == "PRINTSCREEN") return _("PRINTSCREEN"); + + if (substring(key, 0, 5) == "MOUSE") + return sprintf(_("MOUSE%d"), stof(substring(key, 5, -1))); + + if (key == "MWHEELUP") return _("MWHEELUP"); + if (key == "MWHEELDOWN") return _("MWHEELDOWN"); + + if (substring(key, 0,3) == "JOY") + return sprintf(_("JOY%d"), stof(substring(key, 3, -1))); + + if (substring(key, 0,3) == "AUX") + return sprintf(_("AUX%d"), stof(substring(key, 3, -1))); + + if (substring(key, 0, 4) == "X360_") + { + string subkey = substring(key, 4, -1); + switch(subkey) + { + case "DPAD_UP": return sprintf(_("X360_%s"), _("DPAD_UP")); + case "DPAD_DOWN": return sprintf(_("X360_%s"), _("DPAD_DOWN")); + case "DPAD_LEFT": return sprintf(_("X360_%s"), _("DPAD_LEFT")); + case "DPAD_RIGHT": return sprintf(_("X360_%s"), _("DPAD_RIGHT")); + case "START": return sprintf(_("X360_%s"), _("START")); + case "BACK": return sprintf(_("X360_%s"), _("BACK")); + case "LEFT_THUMB": return sprintf(_("X360_%s"), _("LEFT_THUMB")); + case "RIGHT_THUMB": return sprintf(_("X360_%s"), _("RIGHT_THUMB")); + case "LEFT_SHOULDER": return sprintf(_("X360_%s"), _("LEFT_SHOULDER")); + case "RIGHT_SHOULDER": return sprintf(_("X360_%s"), _("RIGHT_SHOULDER")); + case "LEFT_TRIGGER": return sprintf(_("X360_%s"), _("LEFT_TRIGGER")); + case "RIGHT_TRIGGER": return sprintf(_("X360_%s"), _("RIGHT_TRIGGER")); + case "LEFT_THUMB_UP": return sprintf(_("X360_%s"), _("LEFT_THUMB_UP")); + case "LEFT_THUMB_DOWN": return sprintf(_("X360_%s"), _("LEFT_THUMB_DOWN")); + case "LEFT_THUMB_LEFT": return sprintf(_("X360_%s"), _("LEFT_THUMB_LEFT")); + case "LEFT_THUMB_RIGHT": return sprintf(_("X360_%s"), _("LEFT_THUMB_RIGHT")); + case "RIGHT_THUMB_UP": return sprintf(_("X360_%s"), _("RIGHT_THUMB_UP")); + case "RIGHT_THUMB_DOWN": return sprintf(_("X360_%s"), _("RIGHT_THUMB_DOWN")); + case "RIGHT_THUMB_LEFT": return sprintf(_("X360_%s"), _("RIGHT_THUMB_LEFT")); + case "RIGHT_THUMB_RIGHT": return sprintf(_("X360_%s"), _("RIGHT_THUMB_RIGHT")); + default: return key; + } + } + + if (substring(key, 0, 4) == "JOY_") + { + string subkey = substring(key, 4, -1); + switch(subkey) + { + case "UP": return sprintf(_("JOY_%s"), _("UP")); + case "DOWN": return sprintf(_("JOY_%s"), _("DOWN")); + case "LEFT": return sprintf(_("JOY_%s"), _("LEFT")); + case "RIGHT": return sprintf(_("JOY_%s"), _("RIGHT")); + default: return key; + } + } + + if (substring(key, 0, 8) == "MIDINOTE") + return sprintf(_("MIDINOTE%d"), stof(substring(key, 8, -1))); + + return key; +} + // x-encoding (encoding as zero length invisible string) const string XENCODE_2 = "xX"; const string XENCODE_22 = "0123456789abcdefABCDEF"; diff --git a/qcsrc/common/util.qh b/qcsrc/common/util.qh index 3304d0e7a..3900349e3 100644 --- a/qcsrc/common/util.qh +++ b/qcsrc/common/util.qh @@ -133,6 +133,9 @@ float get_model_parameters_fixbone; string get_model_parameters_desc; float get_model_parameters(string mod, float skn); // call with string_null to clear; skin -1 means mod is the filename of the txt file and is to be split +ERASEABLE +string translate_key(string key); + // x-encoding (encoding as zero length invisible string) // encodes approx. 14 bits into 5 bytes of color code string const float XENCODE_MAX = 21295; // 2*22*22*22-1 diff --git a/qcsrc/menu/xonotic/keybinder.qc b/qcsrc/menu/xonotic/keybinder.qc index cde80d693..36b134478 100644 --- a/qcsrc/menu/xonotic/keybinder.qc +++ b/qcsrc/menu/xonotic/keybinder.qc @@ -373,6 +373,7 @@ float XonoticKeyBinder_keyDown(entity me, int key, bool ascii, float shift) } return r; } + void XonoticKeyBinder_drawListBoxItem(entity me, int i, vector absSize, bool isSelected, bool isFocused) { string s; @@ -437,7 +438,7 @@ void XonoticKeyBinder_drawListBoxItem(entity me, int i, vector absSize, bool isS { if(s != "") s = strcat(s, ", "); - s = strcat(s, keynumtostring(k)); + s = strcat(s, translate_key(keynumtostring(k))); } } s = draw_TextShortenToWidth(s, me.columnKeysSize, 0, me.realFontSize); -- 2.39.2