From: otta8634 Date: Fri, 7 Feb 2025 15:14:29 +0000 (+0800) Subject: Remove duplicate code in qcsrc/menu/xonotic/keybinder.qc X-Git-Url: https://git.rm.cloudns.org/?a=commitdiff_plain;h=d394333073e23223dc1c95c1603b0df2a58b6c78;p=xonotic%2Fxonotic-data.pk3dir.git Remove duplicate code in qcsrc/menu/xonotic/keybinder.qc Merged some duplicate code into a function KeyBinder_Bind_UnbindAllForFunction. --- diff --git a/qcsrc/menu/xonotic/keybinder.qc b/qcsrc/menu/xonotic/keybinder.qc index c5d123eb4..cb3df85b8 100644 --- a/qcsrc/menu/xonotic/keybinder.qc +++ b/qcsrc/menu/xonotic/keybinder.qc @@ -264,10 +264,24 @@ void KeyBinder_Bind_Change(entity btn, entity me) me.clearButton.disabled = 1; keyGrabber = me; } -void XonoticKeyBinder_keyGrabbed(entity me, int key, bool ascii) +void KeyBinder_Bind_UnbindAllForFunction(int n) { - int n, j, i, nvalid; + // usage: call tokenize beforehand, then pass in the output + float k; + for (int j = 0; j < n; ++j) + { + k = stof(argv(j)); + if (k != -1) + { + // bind to empty cmd instead of using unbind so it gets saved in config and overrides any default binds + localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n"); + } + } +} +void XonoticKeyBinder_keyGrabbed(entity me, int key, bool ascii) +{ + int n, j, k, nvalid; me.keyGrabButton.forcePressed = 0; me.clearButton.disabled = 0; @@ -296,15 +310,7 @@ void XonoticKeyBinder_keyGrabbed(entity me, int key, bool ascii) for (j = 2; j < n; ++j) { n = tokenize(findkeysforcommand(argv(j), 0)); // uses '...' strings - for (i = 0; i < n; ++i) - { - k = stof(argv(i)); - if (k != -1) - { - // bind to empty cmd instead of using unbind so it gets saved in config and overrides any default binds - localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n"); - } - } + KeyBinder_Bind_UnbindAllForFunction(n); n = tokenizebyseparator(func, ";"); // reset argv back to the overridden functions } } @@ -318,17 +324,7 @@ void XonoticKeyBinder_keyGrabbed(entity me, int key, bool ascii) ++nvalid; } if (nvalid >= MAX_KEYS_PER_FUNCTION) - { - for (j = 0; j < n; ++j) - { - k = stof(argv(j)); - if (k != -1) - { - // bind to empty cmd instead of using unbind so it gets saved in config and overrides any default binds - localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n"); - } - } - } + KeyBinder_Bind_UnbindAllForFunction(n); m_play_click_sound(MENU_SOUND_SELECT); localcmd("\nbind \"", keynumtostring(key), "\" \"", func, "\"\n"); localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state @@ -389,7 +385,7 @@ void KeyBinder_Bind_Edit(entity btn, entity me) } void KeyBinder_Bind_Clear(entity btn, entity me) { - float n, j, k; + int n; string func = KeyBinds_Functions[me.selectedItem]; if (func == "" || KEYBIND_IS_SPECIAL(func)) @@ -405,15 +401,8 @@ void KeyBinder_Bind_Clear(entity btn, entity me) me.setSelected(me, me.selectedItem); // make it visible if it's hidden n = tokenize(findkeysforcommand(func, 0)); // uses '...' strings - for (j = 0; j < n; ++j) - { - k = stof(argv(j)); - if (k != -1) - { - // bind to empty cmd instead of using unbind so it gets saved in config and overrides any default binds - localcmd("\nbind \"", keynumtostring(k), "\" \"", KEY_NOT_BOUND_CMD, "\"\n"); - } - } + KeyBinder_Bind_UnbindAllForFunction(n); + m_play_click_sound(MENU_SOUND_CLEAR); localcmd("-zoom\n"); // to make sure we aren't in togglezoom'd state cvar_set("_hud_showbinds_reload", "1");