Added Hotkeys for support powers

This commit is contained in:
sinf
2015-04-03 13:22:00 +03:00
parent f6051b1e2b
commit 9250c61473
7 changed files with 96 additions and 16 deletions

View File

@@ -40,7 +40,7 @@ namespace OpenRA.Mods.Common.Widgets
Animation clock;
Dictionary<Rectangle, SupportPowerIcon> icons = new Dictionary<Rectangle, SupportPowerIcon>();
public SupportPowerInstance TooltipPower { get; private set; }
public SupportPowerIcon TooltipIcon { get; private set; }
Lazy<TooltipContainerWidget> tooltipContainer;
Rectangle eventBounds;
@@ -65,6 +65,7 @@ namespace OpenRA.Mods.Common.Widgets
public SupportPowerInstance Power;
public float2 Pos;
public Sprite Sprite;
public Hotkey Hotkey;
}
public void RefreshIcons()
@@ -76,6 +77,8 @@ namespace OpenRA.Mods.Common.Widgets
IconCount = 0;
var rb = RenderBounds;
var ks = Game.Settings.Keys;
foreach (var p in powers)
{
var rect = new Rectangle(rb.X, rb.Y + IconCount * (IconSize.Y + IconMargin), IconSize.X, IconSize.Y);
@@ -85,7 +88,8 @@ namespace OpenRA.Mods.Common.Widgets
{
Power = p,
Pos = new float2(rect.Location),
Sprite = icon.Image
Sprite = icon.Image,
Hotkey = ks.GetSupportPowerHotkey(IconCount)
};
icons.Add(rect, power);
@@ -98,6 +102,31 @@ namespace OpenRA.Mods.Common.Widgets
OnIconCountChanged(oldIconCount, IconCount);
}
protected void ClickIcon(SupportPowerIcon clicked)
{
if (!clicked.Power.Active)
Sound.PlayToPlayer(spm.Self.Owner, clicked.Power.Info.InsufficientPowerSound);
else
clicked.Power.Target();
}
public override bool HandleKeyPress(KeyInput e)
{
if (e.Event == KeyInputEvent.Down)
{
var hotkey = Hotkey.FromKeyInput(e);
var a = icons.Values.FirstOrDefault(i => i.Hotkey == hotkey);
if (a != null)
{
ClickIcon(a);
return true;
}
}
return false;
}
public override void Draw()
{
var iconOffset = 0.5f * IconSize.ToFloat2() + IconSpriteOffset;
@@ -170,7 +199,7 @@ namespace OpenRA.Mods.Common.Widgets
var icon = icons.Where(i => i.Key.Contains(mi.Location))
.Select(i => i.Value).FirstOrDefault();
TooltipPower = icon != null ? icon.Power : null;
TooltipIcon = icon;
return false;
}
@@ -181,12 +210,7 @@ namespace OpenRA.Mods.Common.Widgets
.Select(i => i.Value).FirstOrDefault();
if (clicked != null)
{
if (!clicked.Power.Active)
Sound.PlayToPlayer(spm.Self.Owner, clicked.Power.Info.InsufficientPowerSound);
clicked.Power.Target();
}
ClickIcon(clicked);
return true;
}