Allow selection priority to be modified using a hotkey
This commit is contained in:
committed by
teinarss
parent
3e39ada304
commit
c1fc0c1b74
@@ -22,14 +22,14 @@ namespace OpenRA.Orders
|
||||
{
|
||||
var actor = world.ScreenMap.ActorsAtMouse(mi)
|
||||
.Where(a => !a.Actor.IsDead && a.Actor.Info.HasTraitInfo<ITargetableInfo>() && !world.FogObscures(a.Actor))
|
||||
.WithHighestSelectionPriority(worldPixel);
|
||||
.WithHighestSelectionPriority(worldPixel, mi.Modifiers);
|
||||
|
||||
if (actor != null)
|
||||
return Target.FromActor(actor);
|
||||
|
||||
var frozen = world.ScreenMap.FrozenActorsAtMouse(world.RenderPlayer, mi)
|
||||
.Where(a => a.Info.HasTraitInfo<ITargetableInfo>() && a.Visible && a.HasRenderables)
|
||||
.WithHighestSelectionPriority(worldPixel);
|
||||
.WithHighestSelectionPriority(worldPixel, mi.Modifiers);
|
||||
|
||||
if (frozen != null)
|
||||
return Target.FromFrozenActor(frozen);
|
||||
@@ -93,7 +93,7 @@ namespace OpenRA.Orders
|
||||
{
|
||||
var actor = world.ScreenMap.ActorsAtMouse(xy)
|
||||
.Where(a => !a.Actor.IsDead)
|
||||
.WithHighestSelectionPriority(xy);
|
||||
.WithHighestSelectionPriority(xy, mi.Modifiers);
|
||||
|
||||
if (actor == null)
|
||||
return true;
|
||||
@@ -103,7 +103,7 @@ namespace OpenRA.Orders
|
||||
var actorsAt = world.ActorMap.GetActorsAt(cell).ToList();
|
||||
var underCursor = world.Selection.Actors
|
||||
.Select(a => new ActorBoundsPair(a, a.MouseBounds(wr)))
|
||||
.WithHighestSelectionPriority(xy);
|
||||
.WithHighestSelectionPriority(xy, mi.Modifiers);
|
||||
|
||||
var o = OrderForUnit(underCursor, target, actorsAt, cell, mi);
|
||||
if (o != null)
|
||||
|
||||
@@ -18,17 +18,19 @@ namespace OpenRA.Traits
|
||||
{
|
||||
public static class SelectableExts
|
||||
{
|
||||
public static int SelectionPriority(this ActorInfo a)
|
||||
public static int SelectionPriority(this ActorInfo a, Modifiers modifiers)
|
||||
{
|
||||
var selectableInfo = a.TraitInfoOrDefault<SelectableInfo>();
|
||||
return selectableInfo != null ? selectableInfo.Priority : int.MinValue;
|
||||
return selectableInfo != null ? BaseSelectionPriority(selectableInfo, modifiers) : int.MinValue;
|
||||
}
|
||||
|
||||
const int PriorityRange = 30;
|
||||
|
||||
public static int SelectionPriority(this Actor a)
|
||||
public static int SelectionPriority(this Actor a, Modifiers modifiers)
|
||||
{
|
||||
var basePriority = a.Info.TraitInfo<SelectableInfo>().Priority;
|
||||
var info = a.Info.TraitInfo<SelectableInfo>();
|
||||
var basePriority = BaseSelectionPriority(info, modifiers);
|
||||
|
||||
var lp = a.World.LocalPlayer;
|
||||
|
||||
if (a.Owner == lp || lp == null)
|
||||
@@ -45,37 +47,50 @@ namespace OpenRA.Traits
|
||||
}
|
||||
}
|
||||
|
||||
public static Actor WithHighestSelectionPriority(this IEnumerable<ActorBoundsPair> actors, int2 selectionPixel)
|
||||
static int BaseSelectionPriority(SelectableInfo info, Modifiers modifiers)
|
||||
{
|
||||
var priority = info.Priority;
|
||||
|
||||
if (modifiers.HasModifier(Modifiers.Ctrl) && !modifiers.HasModifier(Modifiers.Alt) && info.PriorityModifiers.HasFlag(SelectionPriorityModifiers.Ctrl))
|
||||
priority = int.MaxValue;
|
||||
|
||||
if (modifiers.HasModifier(Modifiers.Alt) && !modifiers.HasModifier(Modifiers.Ctrl) && info.PriorityModifiers.HasFlag(SelectionPriorityModifiers.Alt))
|
||||
priority = int.MaxValue;
|
||||
|
||||
return priority;
|
||||
}
|
||||
|
||||
public static Actor WithHighestSelectionPriority(this IEnumerable<ActorBoundsPair> actors, int2 selectionPixel, Modifiers modifiers)
|
||||
{
|
||||
if (!actors.Any())
|
||||
return null;
|
||||
|
||||
return actors.MaxBy(a => CalculateActorSelectionPriority(a.Actor.Info, a.Bounds, selectionPixel)).Actor;
|
||||
return actors.MaxBy(a => CalculateActorSelectionPriority(a.Actor.Info, a.Bounds, selectionPixel, modifiers)).Actor;
|
||||
}
|
||||
|
||||
public static FrozenActor WithHighestSelectionPriority(this IEnumerable<FrozenActor> actors, int2 selectionPixel)
|
||||
public static FrozenActor WithHighestSelectionPriority(this IEnumerable<FrozenActor> actors, int2 selectionPixel, Modifiers modifiers)
|
||||
{
|
||||
return actors.MaxByOrDefault(a => CalculateActorSelectionPriority(a.Info, a.MouseBounds, selectionPixel));
|
||||
return actors.MaxByOrDefault(a => CalculateActorSelectionPriority(a.Info, a.MouseBounds, selectionPixel, modifiers));
|
||||
}
|
||||
|
||||
static long CalculateActorSelectionPriority(ActorInfo info, Rectangle bounds, int2 selectionPixel)
|
||||
static long CalculateActorSelectionPriority(ActorInfo info, Rectangle bounds, int2 selectionPixel, Modifiers modifiers)
|
||||
{
|
||||
if (bounds.IsEmpty)
|
||||
return info.SelectionPriority();
|
||||
return info.SelectionPriority(modifiers);
|
||||
|
||||
var centerPixel = new int2(
|
||||
bounds.Left + bounds.Size.Width / 2,
|
||||
bounds.Top + bounds.Size.Height / 2);
|
||||
|
||||
var pixelDistance = (centerPixel - selectionPixel).Length;
|
||||
return ((long)-pixelDistance << 32) + info.SelectionPriority();
|
||||
return ((long)-pixelDistance << 32) + info.SelectionPriority(modifiers);
|
||||
}
|
||||
|
||||
static readonly Actor[] NoActors = { };
|
||||
|
||||
public static IEnumerable<Actor> SubsetWithHighestSelectionPriority(this IEnumerable<Actor> actors)
|
||||
public static IEnumerable<Actor> SubsetWithHighestSelectionPriority(this IEnumerable<Actor> actors, Modifiers modifiers)
|
||||
{
|
||||
return actors.GroupBy(x => x.SelectionPriority())
|
||||
return actors.GroupBy(x => x.SelectionPriority(modifiers))
|
||||
.OrderByDescending(g => g.Key)
|
||||
.Select(g => g.AsEnumerable())
|
||||
.DefaultIfEmpty(NoActors)
|
||||
|
||||
@@ -9,13 +9,29 @@
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
|
||||
namespace OpenRA.Traits
|
||||
{
|
||||
[Desc("This actor is selectable. Defines bounds of selectable area, selection class and selection priority.")]
|
||||
[Flags]
|
||||
public enum SelectionPriorityModifiers
|
||||
{
|
||||
None = 0,
|
||||
Ctrl = 1,
|
||||
Alt = 2
|
||||
}
|
||||
|
||||
[Desc("This actor is selectable. Defines bounds of selectable area, selection class, selection priority and selection priority modifiers.")]
|
||||
public class SelectableInfo : InteractableInfo
|
||||
{
|
||||
public readonly int Priority = 10;
|
||||
|
||||
[Desc("Allow selection priority to be modified using a hotkey.",
|
||||
"Valid values are None (priority is not affected by modifiers)",
|
||||
"Ctrl (priority is raised when Ctrl pressed) and",
|
||||
"Alt (priority is raised when Alt pressed).")]
|
||||
public readonly SelectionPriorityModifiers PriorityModifiers = SelectionPriorityModifiers.None;
|
||||
|
||||
[Desc("All units having the same selection class specified will be selected with select-by-type commands (e.g. double-click). "
|
||||
+ "Defaults to the actor name when not defined or inherited.")]
|
||||
public readonly string Class = null;
|
||||
|
||||
Reference in New Issue
Block a user