Support multiple key handlers.
This commit is contained in:
@@ -20,7 +20,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
public ControlGroupLogic(Widget widget, World world, WorldRenderer worldRenderer)
|
||||
{
|
||||
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)
|
||||
{
|
||||
@@ -30,7 +30,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -153,7 +153,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
CloseChat();
|
||||
|
||||
var keyListener = chatChrome.Get<LogicKeyListenerWidget>("KEY_LISTENER");
|
||||
keyListener.OnKeyPress = e =>
|
||||
keyListener.AddHandler(e =>
|
||||
{
|
||||
if (e.Event == KeyInputEvent.Up || !chatText.IsDisabled())
|
||||
return false;
|
||||
@@ -165,7 +165,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -138,7 +138,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
shroudLabelAlt.GetColor = () => selected.Color;
|
||||
|
||||
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.OnClick();
|
||||
|
||||
@@ -46,7 +46,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
nextKey = new NamedHotkey(yaml.Value, ks);
|
||||
|
||||
var keyhandler = widget.Get<LogicKeyListenerWidget>("GLOBAL_KEYHANDLER");
|
||||
keyhandler.OnKeyPress += e =>
|
||||
keyhandler.AddHandler(e =>
|
||||
{
|
||||
if (e.Event == KeyInputEvent.Down)
|
||||
{
|
||||
@@ -63,7 +63,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
void PauseOrResumeMusic()
|
||||
|
||||
@@ -10,17 +10,27 @@
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets
|
||||
{
|
||||
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)
|
||||
{
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user