Make control group hotkeys configurable
- Split control groups management to its own interface - Add hotkeys for selecting, creating, adding to and combining with control groups - Add a ControlGroups widget to manage the player interaction
This commit is contained in:
committed by
abcdefg30
parent
04b456d6c2
commit
7a93b9ea8c
@@ -88,7 +88,7 @@ namespace OpenRA.Mods.Common.Activities
|
||||
nt.OnTransform(self);
|
||||
|
||||
var selected = w.Selection.Contains(self);
|
||||
var controlgroup = w.Selection.GetControlGroupForActor(self);
|
||||
var controlgroup = w.ControlGroups.GetControlGroupForActor(self);
|
||||
|
||||
self.Dispose();
|
||||
foreach (var s in Sounds)
|
||||
@@ -141,7 +141,7 @@ namespace OpenRA.Mods.Common.Activities
|
||||
w.Selection.Add(a);
|
||||
|
||||
if (controlgroup.HasValue)
|
||||
w.Selection.AddToControlGroup(a, controlgroup.Value);
|
||||
w.ControlGroups.AddToControlGroup(a, controlgroup.Value);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,7 +55,7 @@ namespace OpenRA.Mods.Common.Traits.Render
|
||||
|
||||
IEnumerable<IRenderable> IDecoration.RenderDecoration(Actor self, WorldRenderer wr, ISelectionDecorations container)
|
||||
{
|
||||
var group = self.World.Selection.GetControlGroupForActor(self);
|
||||
var group = self.World.ControlGroups.GetControlGroupForActor(self);
|
||||
if (group == null)
|
||||
return Enumerable.Empty<IRenderable>();
|
||||
|
||||
|
||||
@@ -60,14 +60,15 @@ namespace OpenRA.Mods.Common.Traits.Render
|
||||
this.self = self;
|
||||
font = Game.Renderer.Fonts[info.Font];
|
||||
color = info.UsePlayerColor ? self.Owner.Color : info.Color;
|
||||
label = new CachedTransform<int, string>(g => g.ToString());
|
||||
|
||||
label = new CachedTransform<int, string>(g => self.World.ControlGroups.Groups[g]);
|
||||
}
|
||||
|
||||
bool IDecoration.RequiresSelection => true;
|
||||
|
||||
IEnumerable<IRenderable> IDecoration.RenderDecoration(Actor self, WorldRenderer wr, ISelectionDecorations container)
|
||||
{
|
||||
var group = self.World.Selection.GetControlGroupForActor(self);
|
||||
var group = self.World.ControlGroups.GetControlGroupForActor(self);
|
||||
if (group == null)
|
||||
return Enumerable.Empty<IRenderable>();
|
||||
|
||||
|
||||
149
OpenRA.Mods.Common/Traits/World/ControlGroups.cs
Normal file
149
OpenRA.Mods.Common/Traits/World/ControlGroups.cs
Normal file
@@ -0,0 +1,149 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2021 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
public class ControlGroupsInfo : TraitInfo, IControlGroupsInfo
|
||||
{
|
||||
public readonly string[] Groups = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "0" };
|
||||
|
||||
public override object Create(ActorInitializer init) { return new ControlGroups(init.World, this); }
|
||||
|
||||
string[] IControlGroupsInfo.Groups => Groups;
|
||||
}
|
||||
|
||||
[TraitLocation(SystemActors.World | SystemActors.EditorWorld)]
|
||||
public class ControlGroups : IControlGroups, ITick, IGameSaveTraitData
|
||||
{
|
||||
readonly World world;
|
||||
public string[] Groups { get; private set; }
|
||||
|
||||
readonly List<Actor>[] controlGroups;
|
||||
|
||||
public ControlGroups(World world, ControlGroupsInfo info)
|
||||
{
|
||||
this.world = world;
|
||||
Groups = info.Groups;
|
||||
controlGroups = Enumerable.Range(0, Groups.Length).Select(_ => new List<Actor>()).ToArray();
|
||||
}
|
||||
|
||||
public void SelectControlGroup(int group)
|
||||
{
|
||||
world.Selection.Combine(world, GetActorsInControlGroup(group), false, false);
|
||||
}
|
||||
|
||||
public void CreateControlGroup(int group)
|
||||
{
|
||||
if (!world.Selection.Actors.Any())
|
||||
return;
|
||||
|
||||
controlGroups[group].Clear();
|
||||
|
||||
RemoveActorsFromAllControlGroups(world.Selection.Actors);
|
||||
|
||||
controlGroups[group].AddRange(world.Selection.Actors.Where(a => a.Owner == world.LocalPlayer));
|
||||
}
|
||||
|
||||
public void AddSelectionToControlGroup(int group)
|
||||
{
|
||||
if (!world.Selection.Actors.Any())
|
||||
return;
|
||||
|
||||
RemoveActorsFromAllControlGroups(world.Selection.Actors);
|
||||
|
||||
controlGroups[group].AddRange(world.Selection.Actors.Where(a => a.Owner == world.LocalPlayer));
|
||||
}
|
||||
|
||||
public void CombineSelectionWithControlGroup(int group)
|
||||
{
|
||||
world.Selection.Combine(world, GetActorsInControlGroup(group), true, false);
|
||||
}
|
||||
|
||||
public void AddToControlGroup(Actor a, int group)
|
||||
{
|
||||
if (!controlGroups[group].Contains(a))
|
||||
controlGroups[group].Add(a);
|
||||
}
|
||||
|
||||
public void RemoveFromControlGroup(Actor a)
|
||||
{
|
||||
var group = GetControlGroupForActor(a);
|
||||
if (group.HasValue)
|
||||
controlGroups[group.Value].Remove(a);
|
||||
}
|
||||
|
||||
public int? GetControlGroupForActor(Actor a)
|
||||
{
|
||||
for (var i = 0; i < controlGroups.Length; i++)
|
||||
if (controlGroups[i].Contains(a))
|
||||
return i;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
void RemoveActorsFromAllControlGroups(IEnumerable<Actor> actors)
|
||||
{
|
||||
for (var i = 0; i < Groups.Length; i++)
|
||||
controlGroups[i].RemoveAll(a => actors.Contains(a));
|
||||
}
|
||||
|
||||
public IEnumerable<Actor> GetActorsInControlGroup(int group)
|
||||
{
|
||||
return controlGroups[group].Where(a => a.IsInWorld);
|
||||
}
|
||||
|
||||
void ITick.Tick(Actor self)
|
||||
{
|
||||
foreach (var cg in controlGroups)
|
||||
{
|
||||
// note: NOT `!a.IsInWorld`, since that would remove things that are in transports.
|
||||
cg.RemoveAll(a => a.Disposed || a.Owner != world.LocalPlayer);
|
||||
}
|
||||
}
|
||||
|
||||
List<MiniYamlNode> IGameSaveTraitData.IssueTraitData(Actor self)
|
||||
{
|
||||
var groups = new List<MiniYamlNode>();
|
||||
for (var i = 0; i < controlGroups.Length; i++)
|
||||
{
|
||||
var cg = controlGroups[i];
|
||||
if (cg.Any())
|
||||
{
|
||||
var actorIds = cg.Select(a => a.ActorID).ToArray();
|
||||
groups.Add(new MiniYamlNode(i.ToString(), FieldSaver.FormatValue(actorIds)));
|
||||
}
|
||||
}
|
||||
|
||||
return new List<MiniYamlNode>()
|
||||
{
|
||||
new MiniYamlNode("Groups", new MiniYaml("", groups))
|
||||
};
|
||||
}
|
||||
|
||||
void IGameSaveTraitData.ResolveTraitData(Actor self, List<MiniYamlNode> data)
|
||||
{
|
||||
var groupsNode = data.FirstOrDefault(n => n.Key == "Groups");
|
||||
if (groupsNode != null)
|
||||
{
|
||||
foreach (var n in groupsNode.Value.Nodes)
|
||||
{
|
||||
var group = FieldLoader.GetValue<uint[]>(n.Key, n.Value.Value)
|
||||
.Select(a => self.World.GetActorById(a)).Where(a => a != null);
|
||||
controlGroups[int.Parse(n.Key)].AddRange(group);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,6 @@
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Primitives;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Traits
|
||||
@@ -174,77 +172,13 @@ namespace OpenRA.Mods.Common.Traits
|
||||
foreach (var ns in worldNotifySelection)
|
||||
ns.SelectionChanged();
|
||||
}
|
||||
|
||||
foreach (var cg in controlGroups.Values)
|
||||
{
|
||||
// note: NOT `!a.IsInWorld`, since that would remove things that are in transports.
|
||||
cg.RemoveAll(a => a.Disposed || a.Owner != world.LocalPlayer);
|
||||
}
|
||||
}
|
||||
|
||||
readonly Cache<int, List<Actor>> controlGroups = new Cache<int, List<Actor>>(_ => new List<Actor>());
|
||||
|
||||
public void DoControlGroup(World world, WorldRenderer worldRenderer, int group, Modifiers mods, int multiTapCount)
|
||||
{
|
||||
var addModifier = Platform.CurrentPlatform == PlatformType.OSX ? Modifiers.Meta : Modifiers.Ctrl;
|
||||
if (mods.HasModifier(addModifier))
|
||||
{
|
||||
if (actors.Count == 0)
|
||||
return;
|
||||
|
||||
if (!mods.HasModifier(Modifiers.Shift))
|
||||
controlGroups[group].Clear();
|
||||
|
||||
for (var i = 0; i < 10; i++) // all control groups
|
||||
controlGroups[i].RemoveAll(a => actors.Contains(a));
|
||||
|
||||
controlGroups[group].AddRange(actors.Where(a => a.Owner == world.LocalPlayer));
|
||||
return;
|
||||
}
|
||||
|
||||
var groupActors = controlGroups[group].Where(a => a.IsInWorld);
|
||||
|
||||
if (mods.HasModifier(Modifiers.Alt) || multiTapCount >= 2)
|
||||
{
|
||||
worldRenderer.Viewport.Center(groupActors);
|
||||
return;
|
||||
}
|
||||
|
||||
Combine(world, groupActors, mods.HasModifier(Modifiers.Shift), false);
|
||||
}
|
||||
|
||||
public void AddToControlGroup(Actor a, int group)
|
||||
{
|
||||
if (!controlGroups[group].Contains(a))
|
||||
controlGroups[group].Add(a);
|
||||
}
|
||||
|
||||
public void RemoveFromControlGroup(Actor a)
|
||||
{
|
||||
var group = GetControlGroupForActor(a);
|
||||
if (group.HasValue)
|
||||
controlGroups[group.Value].Remove(a);
|
||||
}
|
||||
|
||||
public int? GetControlGroupForActor(Actor a)
|
||||
{
|
||||
return controlGroups.Where(g => g.Value.Contains(a))
|
||||
.Select(g => (int?)g.Key)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
|
||||
List<MiniYamlNode> IGameSaveTraitData.IssueTraitData(Actor self)
|
||||
{
|
||||
var groups = controlGroups
|
||||
.Where(cg => cg.Value.Any())
|
||||
.Select(cg => new MiniYamlNode(cg.Key.ToString(),
|
||||
FieldSaver.FormatValue(cg.Value.Select(a => a.ActorID).ToArray())))
|
||||
.ToList();
|
||||
|
||||
return new List<MiniYamlNode>()
|
||||
{
|
||||
new MiniYamlNode("Selection", FieldSaver.FormatValue(Actors.Select(a => a.ActorID).ToArray())),
|
||||
new MiniYamlNode("Groups", new MiniYaml("", groups))
|
||||
new MiniYamlNode("Selection", FieldSaver.FormatValue(Actors.Select(a => a.ActorID).ToArray()))
|
||||
};
|
||||
}
|
||||
|
||||
@@ -257,17 +191,6 @@ namespace OpenRA.Mods.Common.Traits
|
||||
.Select(a => self.World.GetActorById(a)).Where(a => a != null);
|
||||
Combine(self.World, selected, false, false);
|
||||
}
|
||||
|
||||
var groupsNode = data.FirstOrDefault(n => n.Key == "Groups");
|
||||
if (groupsNode != null)
|
||||
{
|
||||
foreach (var n in groupsNode.Value.Nodes)
|
||||
{
|
||||
var group = FieldLoader.GetValue<uint[]>(n.Key, n.Value.Value)
|
||||
.Select(a => self.World.GetActorById(a)).Where(a => a != null);
|
||||
controlGroups[int.Parse(n.Key)].AddRange(group);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
174
OpenRA.Mods.Common/Widgets/ControlGroupsWidget.cs
Normal file
174
OpenRA.Mods.Common/Widgets/ControlGroupsWidget.cs
Normal file
@@ -0,0 +1,174 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2021 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common.Lint;
|
||||
using OpenRA.Traits;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets
|
||||
{
|
||||
public class ControlGroupsWidget : Widget
|
||||
{
|
||||
readonly ModData modData;
|
||||
readonly World world;
|
||||
readonly WorldRenderer worldRenderer;
|
||||
readonly int hotkeyCount;
|
||||
|
||||
HotkeyReference[] selectGroupHotkeys;
|
||||
HotkeyReference[] createGroupHotkeys;
|
||||
HotkeyReference[] addToGroupHotkeys;
|
||||
HotkeyReference[] combineWithGroupHotkeys;
|
||||
HotkeyReference[] jumpToGroupHotkeys;
|
||||
|
||||
// Note: LinterHotkeyNames assumes that these are disabled by default
|
||||
public readonly string SelectGroupKeyPrefix = null;
|
||||
public readonly string CreateGroupKeyPrefix = null;
|
||||
public readonly string AddToGroupKeyPrefix = null;
|
||||
public readonly string CombineWithGroupKeyPrefix = null;
|
||||
public readonly string JumpToGroupKeyPrefix = null;
|
||||
|
||||
[CustomLintableHotkeyNames]
|
||||
public static IEnumerable<string> LinterHotkeyNames(MiniYamlNode widgetNode, Action<string> emitError, Action<string> emitWarning)
|
||||
{
|
||||
var count = Game.ModData.DefaultRules.Actors[SystemActors.World].TraitInfo<IControlGroupsInfo>().Groups.Length;
|
||||
if (count == 0)
|
||||
yield break;
|
||||
|
||||
var selectPrefix = "";
|
||||
var selectPrefixNode = widgetNode.Value.Nodes.FirstOrDefault(n => n.Key == "SelectGroupKeyPrefix");
|
||||
if (selectPrefixNode != null)
|
||||
selectPrefix = selectPrefixNode.Value.Value;
|
||||
|
||||
var createPrefix = "";
|
||||
var createPrefixNode = widgetNode.Value.Nodes.FirstOrDefault(n => n.Key == "CreateGroupKeyPrefix");
|
||||
if (createPrefixNode != null)
|
||||
createPrefix = createPrefixNode.Value.Value;
|
||||
|
||||
var addToPrefix = "";
|
||||
var addToPrefixNode = widgetNode.Value.Nodes.FirstOrDefault(n => n.Key == "AddToGroupKeyPrefix");
|
||||
if (addToPrefixNode != null)
|
||||
addToPrefix = addToPrefixNode.Value.Value;
|
||||
|
||||
var combineWithPrefix = "";
|
||||
var combineWithPrefixNode = widgetNode.Value.Nodes.FirstOrDefault(n => n.Key == "CombineWithGroupKeyPrefix");
|
||||
if (combineWithPrefixNode != null)
|
||||
combineWithPrefix = combineWithPrefixNode.Value.Value;
|
||||
|
||||
var jumpToPrefix = "";
|
||||
var jumpToPrefixNode = widgetNode.Value.Nodes.FirstOrDefault(n => n.Key == "JumpToGroupKeyPrefix");
|
||||
if (jumpToPrefixNode != null)
|
||||
jumpToPrefix = jumpToPrefixNode.Value.Value;
|
||||
|
||||
if (string.IsNullOrEmpty(selectPrefix))
|
||||
emitError($"{widgetNode.Location} must define SelectGroupKeyPrefix if control groups count is greater than 0.");
|
||||
|
||||
if (string.IsNullOrEmpty(createPrefix))
|
||||
emitError($"{widgetNode.Location} must define CreateGroupKeyPrefix if control groups count is greater than 0.");
|
||||
|
||||
if (string.IsNullOrEmpty(addToPrefix))
|
||||
emitError($"{widgetNode.Location} must define AddToGroupKeyPrefix if control groups count is greater than 0.");
|
||||
|
||||
if (string.IsNullOrEmpty(combineWithPrefix))
|
||||
emitError($"{widgetNode.Location} must define CombineWithGroupKeyPrefix if control groups count is greater than 0.");
|
||||
|
||||
if (string.IsNullOrEmpty(jumpToPrefix))
|
||||
emitError($"{widgetNode.Location} must define JumpToGroupKeyPrefix if control groups count is greater than 0.");
|
||||
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
var suffix = (i + 1).ToString("D2");
|
||||
yield return selectPrefix + suffix;
|
||||
yield return createPrefix + suffix;
|
||||
yield return addToPrefix + suffix;
|
||||
yield return combineWithPrefix + suffix;
|
||||
yield return jumpToPrefix + suffix;
|
||||
}
|
||||
}
|
||||
|
||||
[ObjectCreator.UseCtor]
|
||||
public ControlGroupsWidget(ModData modData, World world, WorldRenderer worldRenderer)
|
||||
{
|
||||
this.modData = modData;
|
||||
this.world = world;
|
||||
this.worldRenderer = worldRenderer;
|
||||
hotkeyCount = world.ControlGroups.Groups.Length;
|
||||
}
|
||||
|
||||
public override void Initialize(WidgetArgs args)
|
||||
{
|
||||
base.Initialize(args);
|
||||
|
||||
selectGroupHotkeys = Exts.MakeArray(hotkeyCount,
|
||||
i => modData.Hotkeys[SelectGroupKeyPrefix + (i + 1).ToString("D2")]);
|
||||
|
||||
createGroupHotkeys = Exts.MakeArray(hotkeyCount,
|
||||
i => modData.Hotkeys[CreateGroupKeyPrefix + (i + 1).ToString("D2")]);
|
||||
|
||||
addToGroupHotkeys = Exts.MakeArray(hotkeyCount,
|
||||
i => modData.Hotkeys[AddToGroupKeyPrefix + (i + 1).ToString("D2")]);
|
||||
|
||||
combineWithGroupHotkeys = Exts.MakeArray(hotkeyCount,
|
||||
i => modData.Hotkeys[CombineWithGroupKeyPrefix + (i + 1).ToString("D2")]);
|
||||
|
||||
jumpToGroupHotkeys = Exts.MakeArray(hotkeyCount,
|
||||
i => modData.Hotkeys[JumpToGroupKeyPrefix + (i + 1).ToString("D2")]);
|
||||
}
|
||||
|
||||
public override bool HandleKeyPress(KeyInput e)
|
||||
{
|
||||
if (e.Event != KeyInputEvent.Down)
|
||||
return false;
|
||||
|
||||
for (var i = 0; i < hotkeyCount; i++)
|
||||
{
|
||||
if (selectGroupHotkeys[i].IsActivatedBy(e))
|
||||
{
|
||||
world.ControlGroups.SelectControlGroup(i);
|
||||
|
||||
if (e.MultiTapCount >= 2)
|
||||
worldRenderer.Viewport.Center(world.ControlGroups.GetActorsInControlGroup(i));
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
if (createGroupHotkeys[i].IsActivatedBy(e))
|
||||
{
|
||||
world.ControlGroups.CreateControlGroup(i);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (addToGroupHotkeys[i].IsActivatedBy(e))
|
||||
{
|
||||
world.ControlGroups.AddSelectionToControlGroup(i);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (combineWithGroupHotkeys[i].IsActivatedBy(e))
|
||||
{
|
||||
world.ControlGroups.CombineSelectionWithControlGroup(i);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (jumpToGroupHotkeys[i].IsActivatedBy(e))
|
||||
{
|
||||
worldRenderer.Viewport.Center(world.ControlGroups.GetActorsInControlGroup(i));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,36 +0,0 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2021 The OpenRA Developers (see AUTHORS)
|
||||
* This file is part of OpenRA, which is free software. It is made
|
||||
* available to you under the terms of the GNU General Public License
|
||||
* as published by the Free Software Foundation, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.Widgets.Logic
|
||||
{
|
||||
public class ControlGroupLogic : ChromeLogic
|
||||
{
|
||||
[ObjectCreator.UseCtor]
|
||||
public ControlGroupLogic(Widget widget, World world, WorldRenderer worldRenderer)
|
||||
{
|
||||
var keyhandler = widget.Get<LogicKeyListenerWidget>("CONTROLGROUP_KEYHANDLER");
|
||||
keyhandler.AddHandler(e =>
|
||||
{
|
||||
if (e.Event == KeyInputEvent.Down && e.Key >= Keycode.NUMBER_0 && e.Key <= Keycode.NUMBER_9)
|
||||
{
|
||||
var group = (int)e.Key - (int)Keycode.NUMBER_0;
|
||||
world.Selection.DoControlGroup(world, worldRenderer, group, e.Modifiers, e.MultiTapCount);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,7 +38,7 @@ namespace OpenRA.Mods.Common.Widgets.Logic.Ingame
|
||||
.ToArray();
|
||||
|
||||
foreach (var a in selectedActors)
|
||||
selection.RemoveFromControlGroup(a);
|
||||
world.ControlGroups.RemoveFromControlGroup(a);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user