/// <summary> /// Create read-only display of a key binding /// </summary> /// <param name="parent">The panel to host it</param> /// <param name="showKey">The key to display</param> public void CreateKeybindText(UIPanel parent, SavedInputKey showKey, float widthFraction) { var label = parent.AddUIComponent <UILabel>(); label.autoSize = false; label.size = new Vector2(ROW_WIDTH * widthFraction, ROW_HEIGHT); label.text = Keybind.ToLocalizedString(showKey); label.verticalAlignment = UIVerticalAlignment.Middle; label.textAlignment = UIHorizontalAlignment.Center; label.textColor = new Color32(128, 128, 128, 255); // grey }
/// <summary> /// Add X button to the right of another button /// </summary> /// <param name="parent">The panel to host the new button</param> /// <param name="editKey">The key to be cleared on click</param> /// <param name="alignTo">Align X button to the right of this</param> private static void AddXButton(UIPanel parent, SavedInputKey editKey, UIButton alignTo) { var btnX = parent.AddUIComponent <UIButton>(); btnX.autoSize = false; btnX.size = new Vector2(ROW_HEIGHT, ROW_HEIGHT); btnX.normalBgSprite = "buttonclose"; btnX.hoveredBgSprite = "buttonclosehover"; btnX.pressedBgSprite = "buttonclosepressed"; btnX.eventClicked += (component, eventParam) => { editKey.value = SavedInputKey.Empty; alignTo.text = Keybind.ToLocalizedString(editKey); }; }
/// <summary> /// Produce a keybind tooltip text, or two if alternate key is set. Prefixed if not empty. /// </summary> /// <param name="prefix">Prefix will be added if any key is not empty</param> /// <returns>String tooltip with the key shortcut or two</returns> public List <string> ToLocalizedStringList() { var result = new List <string>(capacity: 2); if (!Keybind.IsEmpty(Key)) { result.Add(Keybind.ToLocalizedString(Key));; } if (AlternateKey != null && !Keybind.IsEmpty(AlternateKey)) { result.Add(Keybind.ToLocalizedString(AlternateKey)); } return(result); }
private void OnBindingMouseDown(UIComponent comp, UIMouseEventParameter evParam) { var editable = (KeybindSetting.Editable)evParam.source.objectUserData; var keybindButton = evParam.source as UIButton; // This will only work if the user is not in the process of changing the shortcut if (currentlyEditedBinding_ == null) { evParam.Use(); StartKeybindEditMode(editable, keybindButton); } else if (!Keybind.IsUnbindableMouseButton(evParam.buttons)) { // This will work if the user clicks while the shortcut change is in progress evParam.Use(); var editedBinding = currentlyEditedBinding_; // will be nulled by closing modal UIView.PopModal(); var inputKey = SavedInputKey.Encode(Keybind.ButtonToKeycode(evParam.buttons), Keybind.IsControlDown(), Keybind.IsShiftDown(), Keybind.IsAltDown()); var category = editable.Target.Category; var maybeConflict = FindConflict(editedBinding.Value, inputKey, category); if (maybeConflict != string.Empty) { var message = Translation.Options.Get("Keybinds.Dialog.Text:Keybind conflict") + "\n\n" + maybeConflict; Log.Info($"Keybind conflict: {message}"); UIView.library .ShowModal <ExceptionPanel>("ExceptionPanel") .SetMessage("Key Conflict", message, false); } else { editedBinding.Value.TargetKey.value = inputKey; editedBinding.Value.Target.NotifyKeyChanged(); } keybindButton.buttonsMask = UIMouseButton.Left; keybindButton.text = Keybind.ToLocalizedString(editedBinding.Value.TargetKey); currentlyEditedBinding_ = null; } }
private void OnBindingKeyDown(UIComponent comp, UIKeyEventParameter evParam) { try { // This will only work if the user clicked the modify button // otherwise no effect if (!currentlyEditedBinding_.HasValue || Keybind.IsModifierKey(evParam.keycode)) { return; } evParam.Use(); // Consume the event var editedBinding = currentlyEditedBinding_; // will be nulled by closing modal UIView.PopModal(); var keybindButton = evParam.source as UIButton; var inputKey = SavedInputKey.Encode(evParam.keycode, evParam.control, evParam.shift, evParam.alt); var editable = (KeybindSetting.Editable)evParam.source.objectUserData; var category = editable.Target.Category; if (evParam.keycode != KeyCode.Escape) { // Check the key conflict var maybeConflict = FindConflict(editedBinding.Value, inputKey, category); if (maybeConflict != string.Empty) { var message = Translation.Options.Get("Keybinds.Dialog.Text:Keybind conflict") + "\n\n" + maybeConflict; Log.Info($"Keybind conflict: {message}"); UIView.library .ShowModal <ExceptionPanel>("ExceptionPanel") .SetMessage("Key Conflict", message, false); } else { editedBinding.Value.TargetKey.value = inputKey; editedBinding.Value.Target.NotifyKeyChanged(); } } keybindButton.text = Keybind.ToLocalizedString(editedBinding.Value.TargetKey); currentlyEditedBinding_ = null; } catch (Exception e) { Log.Error($"{e}"); } }
public void CreateKeybindButton(UIPanel parent, KeybindSetting setting, SavedInputKey editKey, float widthFraction) { var btn = parent.AddUIComponent <UIButton>(); btn.size = new Vector2(ROW_WIDTH * widthFraction, ROW_HEIGHT); btn.text = Keybind.ToLocalizedString(editKey); btn.hoveredTextColor = new Color32(128, 128, 255, 255); // darker blue btn.pressedTextColor = new Color32(192, 192, 255, 255); // lighter blue btn.normalBgSprite = "ButtonMenu"; btn.eventKeyDown += OnBindingKeyDown; btn.eventMouseDown += OnBindingMouseDown; btn.objectUserData = new KeybindSetting.Editable { Target = setting, TargetKey = editKey }; AddXButton(parent, editKey, btn, setting); }