public static Hotkey CheckConflicts(Keybinding keybinding, Hotkey hotkey) { if (!keybinding.IsEmpty) { var inputconflicts = Settings.KeybindConflicts[hotkey]; if (inputconflicts == KeyConflicts.HardCoded) { return(Hotkey.None); } foreach (var keybinds in Settings.Keybinds) { var hk = keybinds.Key; var conflicts = Settings.KeybindConflicts[hk]; //if the conflicts is equal to or below inputconflicts //then we can compare for conflict //if conflicts is above inputconflicts, ignore //also, if theyre both hardcoded they cannot conflict. if (inputconflicts.HasFlag(conflicts)) { foreach (var keybind in keybinds.Value) { if (keybind.IsBindingEqual(keybinding) && !(inputconflicts == KeyConflicts.HardCoded && inputconflicts == conflicts)) { return(hk); } } } } } return(Hotkey.None); }
private bool GetKeybindingState(Keybinding keybinding) { if (!keybinding.enabled) { return(false); } if (Event.current.type != EventType.KeyDown) { return(false); } if (keybinding.alt != Event.current.alt) { return(false); } if (keybinding.control != Event.current.control) { return(false); } if (keybinding.shift != Event.current.shift) { return(false); } if (keybinding.key != Event.current.keyCode) { return(false); } return(true); }
void btn_Click(object sender, EventArgs e) { Button dis = (Button)sender; Keybinding kb = null; foreach (Keybinding k in bindings) { if (k == null) { continue; } if (k.btn != dis) { continue; } kb = k; break; } if (kb == null) { throw new Exception("umm... wat? this button isn't bound to anything!"); } using (FormBuilder frmb = new FormBuilder(kb.btn.Text, kb.edown, kb.eup, kb.eheld)) { frmb.StartPosition = FormStartPosition.CenterParent; frmb.ShowDialog(); } }
private static void CreateKeybind(Hotkey hotkey, Keybinding keybinding) { var conflict = CheckConflicts(keybinding, hotkey); if (keybinding.IsEmpty || conflict != Hotkey.None) { return; } Keybinds[hotkey].Add(keybinding); }
private static void CreateKeybind(Hotkey hotkey, Keybinding keybinding) { if (keybinding.IsEmpty) { return; } if (!Keybinds.ContainsKey(hotkey)) { Keybinds[hotkey] = new List <Keybinding>(); } Keybinds[hotkey].Add(keybinding); }
private static void LoadKeybinding(string[] config, Hotkey hotkey) { if (KeybindConflicts[hotkey] == KeyConflicts.HardCoded) { return; } int line = 0; var hotkeyname = hotkey.ToString(); var setting = GetSetting(config, hotkeyname, ref line); if (setting != null) { Keybinds[hotkey] = new List <Keybinding>(); } while (setting != null) { line++; var items = setting.Trim(' ', '\t', '[', ']').Split('+'); Keybinding ret = new Keybinding(); foreach (var item in items) { if (!ret.UsesModifiers && Enum.TryParse <KeyModifiers>(item, true, out var modifiers)) { ret.Modifiers = modifiers; } else if (!ret.UsesKeys && Enum.TryParse <Key>(item, true, out Key key)) { ret.Key = key; } else if (!ret.UsesMouse && Enum.TryParse <MouseButton>(item, true, out var mouse)) { ret.MouseButton = mouse; } } try { if (!ret.IsEmpty) { CreateKeybind(hotkey, ret); } } catch (Exception e) { Debug.WriteLine($"An error occured loading the hotkey {hotkey}\n{e}"); } setting = GetSetting(config, hotkeyname, ref line); } }
private static void SetupDefaultKeybind(Hotkey hotkey, Keybinding keybinding, Keybinding secondary = null) { if (keybinding.IsEmpty) { return; } DefaultKeybinds[hotkey] = new List <Keybinding>(); DefaultKeybinds[hotkey].Add(keybinding); if (secondary != null) { DefaultKeybinds[hotkey].Add(secondary); } }
public void AddKeybinding(Keybinding keybinding, Keybinding.Callback callback) { Keybinding existingKB = GetKeybindingByID(keybinding.id); if (existingKB == null) { existingKB = keybinding; m_keybindings.Add(keybinding); EditorUtility.SetDirty(this); } existingKB.callback = callback; }
private void DrawKeybinding(Keybinding keybinding) { EditorGUILayout.BeginHorizontal(); { keybinding.enabled = EditorGUILayout.Toggle(keybinding.enabled, GUILayout.MinWidth(14f), GUILayout.Width(14f)); EditorGUI.BeginDisabledGroup(!keybinding.enabled); { EditorGUILayout.LabelField(keybinding.label, GUILayout.MinWidth(126f)); keybinding.control = EditorGUILayout.ToggleLeft("Ctrl", keybinding.control, GUILayout.MinWidth(FIELD_WIDTH)); keybinding.alt = EditorGUILayout.ToggleLeft("Alt", keybinding.alt, GUILayout.MinWidth(FIELD_WIDTH)); keybinding.shift = EditorGUILayout.ToggleLeft("Shift", keybinding.shift, GUILayout.MinWidth(FIELD_WIDTH)); keybinding.key = (KeyCode)EditorGUILayout.EnumPopup(keybinding.key, GUILayout.MinWidth(FIELD_WIDTH)); } EditorGUI.EndDisabledGroup(); } EditorGUILayout.EndHorizontal(); }
private static void LoadKeybinding(string[] config, string hotkeyname) { var hotkey = (Hotkey)Enum.Parse(typeof(Hotkey), hotkeyname); int line = 0; var setting = GetSetting(config, hotkeyname, ref line); if (setting != null) { Keybinds[hotkey] = new List <Keybinding>(); } while (setting != null) { line++; int modstart = setting.IndexOf("Mod="); int keystart = setting.IndexOf("Key="); int mousestart = setting.IndexOf("Mouse="); if (modstart == -1 || keystart == -1 || mousestart == -1) { return; } modstart += 4; keystart += 4; mousestart += 6; int modend = setting.IndexOf(";", modstart); int keyend = setting.IndexOf(";", keystart); int mouseend = setting.IndexOf(";", mousestart); if (modend == -1 || keyend == -1 || mouseend == -1) { return; } try { Keybinding ret = new Keybinding(); ret.Modifiers = (KeyModifiers)Enum.Parse(typeof(KeyModifiers), setting.Substring(modstart, modend - modstart)); ret.MouseButton = (MouseButton)Enum.Parse(typeof(MouseButton), setting.Substring(mousestart, mouseend - mousestart)); ret.Key = (Key)Enum.Parse(typeof(Key), setting.Substring(keystart, keyend - keystart)); CreateKeybind(hotkey, ret); } catch { } setting = GetSetting(config, hotkeyname, ref line); } }
public void SetKeybind(Button btn, int nth, IEnumerable <Event> edown, IEnumerable <Event> eup, IEnumerable <Event> eheld) { if (nth < 0 || nth > 31) { return; } bindings[nth] = new Keybinding() { btn = btn, nth = nth }; btn.Click += btn_Click; if (edown != null) { foreach (Event e in edown) { bindings[nth].edown.Add(e); } } if (eup != null) { foreach (Event e in eup) { bindings[nth].eup.Add(e); } } if (eheld != null) { foreach (Event e in eheld) { bindings[nth].eheld.Add(e); } } }
public override void OnInspectorGUI() { controller = (PlayerController)target; Event e = Event.current; if (getKeycodeIndex >= 0) { if (e.isKey || e.isMouse) { bool isValidKey = false; bool isInvalidKey = false; KeyCode code = KeyCode.Escape; switch (e.type) { case EventType.MouseDown: isValidKey = e.button == 0 || e.button == 1; if (e.button == 0) { code = KeyCode.Mouse0; } if (e.button == 1) { code = KeyCode.Mouse1; } break; case EventType.KeyDown: code = e.keyCode; isInvalidKey = code == KeyCode.Escape; isValidKey = !isInvalidKey; break; } if (isValidKey) { var binding = controller.KeyBindings[getKeycodeIndex]; binding.KeyboardBinding = code; controller.KeyBindings[getKeycodeIndex] = binding; } if (isValidKey || isInvalidKey) { getKeycodeIndex = -1; EditorUtility.SetDirty(controller); } } } for (int i = 0; i < controller.KeyBindings.Count; i++) { var binding = controller.KeyBindings[i]; EditorGUILayout.BeginHorizontal(); string className = binding.HandlerTypeString.Split('.').Last(); EditorGUILayout.LabelField(className); if (getKeycodeIndex == i) { EditorGUILayout.LabelField("press a key"); //binding.KeyboardBinding = (KeyCode)EditorGUILayout.EnumPopup(binding.KeyboardBinding); } else { if (GUILayout.Button(binding.KeyboardBinding.ToString())) { getKeycodeIndex = i; } } binding.ControllerBinding = (BindableButtons)EditorGUILayout.EnumPopup(binding.ControllerBinding); controller.KeyBindings[i] = binding; EditorGUILayout.EndHorizontal(); } if (isAddingButton) { EditorGUILayout.BeginHorizontal(); bindingIndex = EditorGUILayout.Popup(bindingIndex, classStrings); newKeybinding.HandlerTypeString = classStrings[bindingIndex]; newKeybinding.KeyboardBinding = (KeyCode)EditorGUILayout.EnumPopup(newKeybinding.KeyboardBinding); newKeybinding.ControllerBinding = (BindableButtons)EditorGUILayout.EnumPopup(newKeybinding.ControllerBinding); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Add")) { controller.KeyBindings.Add(newKeybinding); isAddingButton = false; } else if (GUILayout.Button("Cancel")) { isAddingButton = false; } EditorGUILayout.EndHorizontal(); } else if (GUILayout.Button("Add Button")) { isAddingButton = true; newKeybinding = new Keybinding(); bindingIndex = 0; var classes = Assembly.GetAssembly(typeof(PlayerControllerButtonHandler)).GetTypes() .Where(myType => myType.IsClass && myType.IsSubclassOf(typeof(PlayerControllerButtonHandler))).ToArray(); classStrings = classes.Select(c => c.ToString()).ToArray(); } }
public string GetKeybindingDescription(string id) { Keybinding kb = GetKeybindingByID(id); return(kb != null ? kb.ToString() : string.Empty); }