Skip to content

Commit

Permalink
Fixed hanging Press any key state on keybind buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
kvakvs committed Jul 1, 2019
1 parent 537dbf6 commit 59277cd
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 31 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -183,8 +183,9 @@ UpgradeLog*.htm
FakesAssemblies/
/logo/

# MSVS 2017 artifacts
# MSVS 2017, IntelliJ IDEA/Rider artifacts
.vs/
.idea/

# Dependecies game dlls
/TLM/dependencies
5 changes: 4 additions & 1 deletion TLM/TLM/State/Keybinds/KeybindSettingsPage.cs
Expand Up @@ -6,9 +6,12 @@ public class KeybindSettingsPage : KeybindSettingsBase {
TryCreateConfig();

keybindUi_.BeginForm(component);

// Section: Global
keybindUi_.AddGroup(Translation.GetString("Keybind_category_Global"),
CreateUI_Global);
// New section: Lane Connector Tool

// Section: Lane Connector Tool
keybindUi_.AddGroup(Translation.GetString("Keybind_category_LaneConnector"),
CreateUI_LaneConnector);
}
Expand Down
78 changes: 49 additions & 29 deletions TLM/TLM/State/Keybinds/KeybindUI.cs
Expand Up @@ -157,7 +157,6 @@ public class KeybindUI {
btnX.normalBgSprite = "buttonclose";
btnX.hoveredBgSprite = "buttonclosehover";
btnX.pressedBgSprite = "buttonclosepressed";
btnX.text = "X";
btnX.eventClicked += (component, eventParam) => {
editKey.value = SavedInputKey.Empty;
alignTo.text = Keybind.Str(editKey);
Expand Down Expand Up @@ -198,52 +197,58 @@ public class KeybindUI {
}

p.Use(); // Consume the event

var editedBinding = currentlyEditedBinding_; // will be nulled by popmodal
UIView.PopModal();
var keycode = p.keycode;
var inputKey = (p.keycode == KeyCode.Escape)
? currentlyEditedBinding_.Value.TargetKey
: SavedInputKey.Encode(keycode, p.control, p.shift, p.alt);
currentlyEditedBinding_ = editedBinding;

var editable = (KeybindSetting.Editable) p.source.objectUserData;
var category = editable.Target.Category;

var maybeConflict = FindConflict(inputKey, category);
if (maybeConflict != string.Empty) {
UIView.library.ShowModal<ExceptionPanel>("ExceptionPanel").SetMessage(
"Key Conflict",
Translation.GetString("Keybind_conflict") + "\n\n" + maybeConflict,
false);
} else {
currentlyEditedBinding_.Value.TargetKey.value = inputKey;
currentlyEditedBinding_.Value.Target.NotifyKeyChanged();
var keybindButton = p.source as UIButton;

if (p.keycode != KeyCode.Escape) {
var inputKey = SavedInputKey.Encode(p.keycode, p.control, p.shift, p.alt);
var editable = (KeybindSetting.Editable) p.source.objectUserData;
var category = editable.Target.Category;

var maybeConflict = FindConflict(inputKey, category);
if (maybeConflict != string.Empty) {
UIView.library.ShowModal<ExceptionPanel>("ExceptionPanel").SetMessage(
"Key Conflict",
Translation.GetString("Keybind_conflict") + "\n\n" + maybeConflict,
false);
} else {
currentlyEditedBinding_.Value.TargetKey.value = inputKey;
currentlyEditedBinding_.Value.Target.NotifyKeyChanged();
}
}

// Update text on the button
var button = p.source as UIButton;
button.text = Keybind.Str(currentlyEditedBinding_.Value.TargetKey);
currentlyEditedBinding_ = null;
EndButtonEditMode(keybindButton);
}

private void OnBindingMouseDown(UIComponent comp, UIMouseEventParameter p) {
var editable = (KeybindSetting.Editable) p.source.objectUserData;
var keybindButton = p.source as UIButton;

// This will only work if the user is not in the process of changing the shortcut
if (currentlyEditedBinding_ == null) {
p.Use();
currentlyEditedBinding_ = editable;

var uIButton = p.source as UIButton;
uIButton.buttonsMask =
keybindButton.buttonsMask =
UIMouseButton.Left | UIMouseButton.Right | UIMouseButton.Middle |
UIMouseButton.Special0 | UIMouseButton.Special1 | UIMouseButton.Special2 |
UIMouseButton.Special3;
uIButton.text = "Press any key";
p.source.Focus();
UIView.PushModal(p.source);
keybindButton.text = "Press any key";
keybindButton.Focus();
UIView.PushModal(keybindButton, OnKeybindModalPopped);
} else if (!Keybind.IsUnbindableMouseButton(p.buttons)) {
// This will work if the user clicks while the shortcut change is in progress
p.Use();

var editedBinding = currentlyEditedBinding_; // it will be nulled on modal pop
UIView.PopModal();
currentlyEditedBinding_ = editedBinding;

var inputKey = SavedInputKey.Encode(Keybind.ButtonToKeycode(p.buttons),
Keybind.IsControlDown(),
Keybind.IsShiftDown(),
Expand All @@ -260,11 +265,26 @@ public class KeybindUI {
currentlyEditedBinding_.Value.Target.NotifyKeyChanged();
}

var button = p.source as UIButton;
button.text = Keybind.Str(currentlyEditedBinding_.Value.TargetKey);
button.buttonsMask = UIMouseButton.Left;
currentlyEditedBinding_ = null;
keybindButton.buttonsMask = UIMouseButton.Left;
EndButtonEditMode(keybindButton);
}
}

/// <summary>
/// Called by the UIView when modal was popped without us knowing
/// </summary>
/// <param name="component">The button which temporarily was modal</param>
private void OnKeybindModalPopped(UIComponent component) {
if (!(component is UIButton) || currentlyEditedBinding_ == null) {
return;
}

EndButtonEditMode((UIButton) component);
}

private void EndButtonEditMode(UIButton b) {
b.text = Keybind.Str(currentlyEditedBinding_.Value.TargetKey);
currentlyEditedBinding_ = null;
}

/// <summary>
Expand Down

0 comments on commit 59277cd

Please sign in to comment.