Beispiel #1
0
 public static Keybind FindKeybindByKeyCode(KeyCode keyCode)
 {
     for (int i = 0; i < UmbraMenu.keybindDict.Count; i++)
     {
         Keybind keybind = UmbraMenu.keybindDict[UmbraMenu.keyBindNames[i]];
         if (keybind.KeyCode == keyCode)
         {
             return(keybind);
         }
     }
     throw new NullReferenceException($"Keybind using keycode '{keyCode}' was not found.");
 }
Beispiel #2
0
 public static bool KeyCodeInUse(KeyCode keyCode)
 {
     for (int i = 0; i < UmbraMenu.keybindDict.Count; i++)
     {
         Keybind keybind = UmbraMenu.keybindDict[UmbraMenu.keyBindNames[i]];
         if (keybind.KeyCode == keyCode)
         {
             return(true);
         }
     }
     return(false);
 }
        private void SetKeybindRoutine()
        {
            if (listenForKeybind)
            {
                Dictionary <string, string> numKeys = new Dictionary <string, string>()
                {
                    { "0", "Alpha0" },
                    { "1", "Alpha1" },
                    { "2", "Alpha2" },
                    { "3", "Alpha3" },
                    { "4", "Alpha4" },
                    { "5", "Alpha5" },
                    { "6", "Alpha6" },
                    { "7", "Alpha7" },
                    { "8", "Alpha8" },
                    { "9", "Alpha9" },
                };
                Regex  rgAlpha    = new Regex(@"^[A-Z]$");
                Regex  rgNum      = new Regex(@"^[0-9]$");
                Button button     = Utility.GetEnabledKeybindButton();
                string newKeybind = Input.inputString;
                if (newKeybind.Length > 1)
                {
                    newKeybind = newKeybind.First().ToString().ToUpper() + newKeybind.Substring(1);
                }
                else
                {
                    newKeybind = newKeybind.ToUpper();
                }

                bool validKeybind = newKeybind != "V" && newKeybind != "W" && newKeybind != "A" && newKeybind != "S" && newKeybind != "D" && rgAlpha.IsMatch(newKeybind) && newKeybind != "" && newKeybind != " " && newKeybind != null;
                if (validKeybind)
                {
                    KeyCode keyCode = (KeyCode)Enum.Parse(typeof(KeyCode), newKeybind);
                    if (!Utility.KeyCodeInUse(keyCode))
                    {
                        string keybindName = button.GetOffText().Split(new string[] { " :" }, StringSplitOptions.None)[0];
                        keybindDict[keybindName].KeyCode = keyCode;
                        listenForKeybind = false;
                        button.SetEnabled(false);
                        Utility.SaveSettings();
                        Utility.SoftResetMenu(true);
                    }
                    else
                    {
                        Keybind keybind = Utility.FindKeybindByKeyCode(keyCode);
                        keybind.KeyCode = KeyCode.None;

                        string keybindName = button.GetOffText().Split(new string[] { " :" }, StringSplitOptions.None)[0];
                        keybindDict[keybindName].KeyCode = keyCode;
                        listenForKeybind = false;
                        button.SetEnabled(false);
                        Utility.SaveSettings();
                        Utility.SoftResetMenu(true);
                    }
                }
                else if (!validKeybind && rgNum.IsMatch(newKeybind))
                {
                    newKeybind = numKeys[newKeybind];
                    if (newKeybind != "Alpha0")
                    {
                        string keybindName = button.GetOffText().Split(new string[] { " :" }, StringSplitOptions.None)[0];
                        keybindDict[keybindName].KeyCode = (KeyCode)Enum.Parse(typeof(KeyCode), newKeybind);
                        listenForKeybind = false;
                        button.SetEnabled(false);
                        Utility.SaveSettings();
                        Utility.SoftResetMenu(true);
                    }
                    else
                    {
                        string keybindName = button.GetOffText().Split(new string[] { " :" }, StringSplitOptions.None)[0];
                        keybindDict[keybindName].KeyCode = KeyCode.None;
                        listenForKeybind = false;
                        button.SetEnabled(false);
                        Utility.SaveSettings();
                        Utility.SoftResetMenu(true);
                    }
                }
            }
        }