Support multiple key handlers.

This commit is contained in:
Paul Chote
2017-09-03 16:50:41 +01:00
committed by reaperrr
parent 7459050af9
commit c6fe1639db
5 changed files with 19 additions and 9 deletions

View File

@@ -20,7 +20,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
public ControlGroupLogic(Widget widget, World world, WorldRenderer worldRenderer) public ControlGroupLogic(Widget widget, World world, WorldRenderer worldRenderer)
{ {
var keyhandler = widget.Get<LogicKeyListenerWidget>("CONTROLGROUP_KEYHANDLER"); var keyhandler = widget.Get<LogicKeyListenerWidget>("CONTROLGROUP_KEYHANDLER");
keyhandler.OnKeyPress = e => keyhandler.AddHandler(e =>
{ {
if (e.Event == KeyInputEvent.Down && e.Key >= Keycode.NUMBER_0 && e.Key <= Keycode.NUMBER_9) if (e.Event == KeyInputEvent.Down && e.Key >= Keycode.NUMBER_0 && e.Key <= Keycode.NUMBER_9)
{ {
@@ -30,7 +30,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
} }
return false; return false;
}; });
} }
} }
} }

View File

@@ -153,7 +153,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
CloseChat(); CloseChat();
var keyListener = chatChrome.Get<LogicKeyListenerWidget>("KEY_LISTENER"); var keyListener = chatChrome.Get<LogicKeyListenerWidget>("KEY_LISTENER");
keyListener.OnKeyPress = e => keyListener.AddHandler(e =>
{ {
if (e.Event == KeyInputEvent.Up || !chatText.IsDisabled()) if (e.Event == KeyInputEvent.Up || !chatText.IsDisabled())
return false; return false;
@@ -165,7 +165,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
} }
return false; return false;
}; });
} }
} }

View File

@@ -138,7 +138,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
shroudLabelAlt.GetColor = () => selected.Color; shroudLabelAlt.GetColor = () => selected.Color;
var keyhandler = shroudSelector.Get<LogicKeyListenerWidget>("SHROUD_KEYHANDLER"); var keyhandler = shroudSelector.Get<LogicKeyListenerWidget>("SHROUD_KEYHANDLER");
keyhandler.OnKeyPress = HandleKeyPress; keyhandler.AddHandler(HandleKeyPress);
selected = limitViews ? groups.First().Value.First() : world.WorldActor.Owner.Shroud.ExploreMapEnabled ? combined : disableShroud; selected = limitViews ? groups.First().Value.First() : world.WorldActor.Owner.Shroud.ExploreMapEnabled ? combined : disableShroud;
selected.OnClick(); selected.OnClick();

View File

@@ -46,7 +46,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
nextKey = new NamedHotkey(yaml.Value, ks); nextKey = new NamedHotkey(yaml.Value, ks);
var keyhandler = widget.Get<LogicKeyListenerWidget>("GLOBAL_KEYHANDLER"); var keyhandler = widget.Get<LogicKeyListenerWidget>("GLOBAL_KEYHANDLER");
keyhandler.OnKeyPress += e => keyhandler.AddHandler(e =>
{ {
if (e.Event == KeyInputEvent.Down) if (e.Event == KeyInputEvent.Down)
{ {
@@ -63,7 +63,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
} }
return false; return false;
}; });
} }
void PauseOrResumeMusic() void PauseOrResumeMusic()

View File

@@ -10,17 +10,27 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using OpenRA.Widgets; using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Widgets namespace OpenRA.Mods.Common.Widgets
{ {
public class LogicKeyListenerWidget : Widget public class LogicKeyListenerWidget : Widget
{ {
public Func<KeyInput, bool> OnKeyPress = _ => false; List<Func<KeyInput, bool>> handlers = new List<Func<KeyInput, bool>>();
public override bool HandleKeyPress(KeyInput e) public override bool HandleKeyPress(KeyInput e)
{ {
return OnKeyPress(e); foreach (var handler in handlers)
if (handler(e))
return true;
return false;
}
public void AddHandler(Func<KeyInput, bool> func)
{
handlers.Add(func);
} }
} }
} }