Add a Hotkey class for user-configurable keys. Fixes #3779.

Users can now define and use hotkeys that include modifiers (ctrl/meta/shift/alt).
This commit is contained in:
Paul Chote
2013-10-20 11:53:41 +13:00
parent aab6fec68b
commit 7ffbfb9b7e
16 changed files with 297 additions and 52 deletions

View File

@@ -146,8 +146,10 @@ namespace OpenRA.Mods.RA.Widgets
public override bool HandleKeyPress(KeyInput e)
{
if (e.Event == KeyInputEvent.Up) return false;
if (KeycodeExts.DisplayString(e.Key) == Game.Settings.Keys.CycleTabsKey)
if (e.Event == KeyInputEvent.Up)
return false;
if (Hotkey.FromKeyInput(e) == Game.Settings.Keys.CycleTabsKey)
{
TabChange(e.Modifiers.HasModifier(Modifiers.Shift));
return true;

View File

@@ -335,22 +335,13 @@ namespace OpenRA.Mods.RA.Widgets.Logic
return true;
}
void SetupKeyBinding(ScrollItemWidget keyWidget, string description, Func<string> getValue, Action<string> setValue)
void SetupKeyBinding(ScrollItemWidget keyWidget, string description, Func<Hotkey> getValue, Action<Hotkey> setValue)
{
keyWidget.Get<LabelWidget>("FUNCTION").GetText = () => description;
var textBox = keyWidget.Get<TextFieldWidget>("HOTKEY");
textBox.Text = getValue();
textBox.OnLoseFocus = () =>
{
textBox.Text.Trim();
if (textBox.Text.Length == 0)
textBox.Text = getValue();
else
setValue(textBox.Text);
};
textBox.OnEnterKey = () => { textBox.YieldKeyboardFocus(); return true; };
var keyEntry = keyWidget.Get<HotkeyEntryWidget>("HOTKEY");
keyEntry.Key = getValue();
keyEntry.OnLoseFocus = () => setValue(keyEntry.Key);
}
static bool ShowRendererDropdown(DropDownButtonWidget dropdown, GraphicSettings s)

View File

@@ -27,7 +27,7 @@ namespace OpenRA.Mods.RA.Widgets
public OrderButtonWidget()
{
GetImage = () => Enabled() ? Pressed() ? "pressed" : "normal" : "disabled";
GetDescription = () => Key != null ? "{0} ({1})".F(Description, Key.ToUpper()) : Description;
GetDescription = () => Key != Hotkey.Invalid ? "{0} ({1})".F(Description, Key.DisplayString()) : Description;
GetLongDesc = () => LongDesc;
}

View File

@@ -45,38 +45,39 @@ namespace OpenRA.Mods.RA.Widgets
bool ProcessInput(KeyInput e)
{
if (e.Modifiers == Modifiers.None && e.Event == KeyInputEvent.Down)
if (e.Event == KeyInputEvent.Down)
{
var key = Hotkey.FromKeyInput(e);
var ks = Game.Settings.Keys;
if (KeycodeExts.DisplayString(e.Key) == ks.CycleBaseKey)
if (key == ks.CycleBaseKey)
return CycleBases();
if (KeycodeExts.DisplayString(e.Key) == ks.ToLastEventKey)
if (key == ks.ToLastEventKey)
return ToLastEvent();
if (KeycodeExts.DisplayString(e.Key) == ks.ToSelectionKey)
if (key == ks.ToSelectionKey)
return ToSelection();
// Put all functions that aren't unit-specific before this line!
if (!world.Selection.Actors.Any())
return false;
if (KeycodeExts.DisplayString(e.Key) == ks.AttackMoveKey)
if (key == ks.AttackMoveKey)
return PerformAttackMove();
if (KeycodeExts.DisplayString(e.Key) == ks.StopKey)
if (key == ks.StopKey)
return PerformStop();
if (KeycodeExts.DisplayString(e.Key) == ks.ScatterKey)
if (key == ks.ScatterKey)
return PerformScatter();
if (KeycodeExts.DisplayString(e.Key) == ks.DeployKey)
if (key == ks.DeployKey)
return PerformDeploy();
if (KeycodeExts.DisplayString(e.Key) == ks.StanceCycleKey)
if (key == ks.StanceCycleKey)
return PerformStanceCycle();
if (KeycodeExts.DisplayString(e.Key) == ks.GuardKey)
if (key == ks.GuardKey)
return PerformGuard();
}