Specify interaction bounds relative to the mod tile size.
This commit is contained in:
@@ -21,13 +21,13 @@ namespace OpenRA.Mods.Common.Traits
|
||||
{
|
||||
[Desc("Defines a custom rectangle for mouse interaction with the actor.",
|
||||
"If null, the engine will guess an appropriate size based on the With*Body trait.",
|
||||
"The first two numbers define the width and height of the rectangle.",
|
||||
"The first two numbers define the width and height of the rectangle as a world distance.",
|
||||
"The (optional) second two numbers define an x and y offset from the actor center.")]
|
||||
public readonly int[] Bounds = null;
|
||||
public readonly WDist[] Bounds = null;
|
||||
|
||||
[Desc("Defines a custom rectangle for Decorations (e.g. the selection box).",
|
||||
"If null, Bounds will be used instead")]
|
||||
public readonly int[] DecorationBounds = null;
|
||||
public readonly WDist[] DecorationBounds = null;
|
||||
|
||||
public override object Create(ActorInitializer init) { return new Interactable(this); }
|
||||
}
|
||||
@@ -52,16 +52,17 @@ namespace OpenRA.Mods.Common.Traits
|
||||
return autoBounds.Select(s => s.AutoMouseoverBounds(self, wr)).FirstOrDefault(r => !r.IsEmpty);
|
||||
}
|
||||
|
||||
Polygon Bounds(Actor self, WorldRenderer wr, int[] bounds)
|
||||
Polygon Bounds(Actor self, WorldRenderer wr, WDist[] bounds)
|
||||
{
|
||||
if (bounds == null)
|
||||
return new Polygon(AutoBounds(self, wr));
|
||||
|
||||
var size = new int2(bounds[0], bounds[1]);
|
||||
// Convert from WDist to pixels
|
||||
var size = new int2(bounds[0].Length * wr.TileSize.Width / wr.TileScale, bounds[1].Length * wr.TileSize.Height / wr.TileScale);
|
||||
|
||||
var offset = -size / 2;
|
||||
if (bounds.Length > 2)
|
||||
offset += new int2(bounds[2], bounds[3]);
|
||||
offset += new int2(bounds[2].Length * wr.TileSize.Width / wr.TileScale, bounds[3].Length * wr.TileSize.Height / wr.TileScale);
|
||||
|
||||
var xy = wr.ScreenPxPosition(self.CenterPosition) + offset;
|
||||
return new Polygon(new Rectangle(xy.X, xy.Y, size.X, size.Y));
|
||||
|
||||
@@ -87,12 +87,13 @@ namespace OpenRA.Mods.Common.Traits
|
||||
int2 left, right, top, bottom;
|
||||
if (bounds != null)
|
||||
{
|
||||
var offset = bounds.Length >= 4 ? new int2(bounds[2], bounds[3]) : int2.Zero;
|
||||
// Convert from WDist to pixels
|
||||
var offset = bounds.Length >= 4 ? new int2(bounds[2] * wr.TileSize.Width / wr.TileScale, bounds[3] * wr.TileSize.Height / wr.TileScale) : int2.Zero;
|
||||
var center = wr.ScreenPxPosition(self.CenterPosition) + offset;
|
||||
left = center - new int2(bounds[0] / 2, 0);
|
||||
right = left + new int2(bounds[0], 0);
|
||||
top = center - new int2(0, bounds[1] / 2);
|
||||
bottom = top + new int2(0, bounds[1]);
|
||||
left = center - new int2(bounds[0] * wr.TileSize.Width / (2 * wr.TileScale), 0);
|
||||
right = left + new int2(bounds[0] * wr.TileSize.Width / wr.TileScale, 0);
|
||||
top = center - new int2(0, bounds[1] * wr.TileSize.Height / (2 * wr.TileScale));
|
||||
bottom = top + new int2(0, bounds[1] * wr.TileSize.Height / wr.TileScale);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
#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;
|
||||
|
||||
namespace OpenRA.Mods.Common.UpdateRules.Rules
|
||||
{
|
||||
public class ConvertBoundsToWDist : UpdateRule
|
||||
{
|
||||
public override string Name => "Convert Interactable and Selection bounds from pixels to WDist.";
|
||||
|
||||
public override string Description =>
|
||||
"The Bounds and DecorationBounds fields on Interactable and Selectable have been converted from pixels to WDist.\n" +
|
||||
"All bounds must be scaled by 1024 (Rectangular map grid) or 1448 (Isometric map grid) divided by your mod tile size.";
|
||||
|
||||
readonly string[] traits = { "Interactable", "Selectable", "IsometricSelectable" };
|
||||
readonly string[] fields = { "Bounds", "DecorationBounds" };
|
||||
|
||||
public override IEnumerable<string> UpdateActorNode(ModData modData, MiniYamlNode actorNode)
|
||||
{
|
||||
var grid = modData.Manifest.Get<MapGrid>();
|
||||
var tileSize = grid.TileSize;
|
||||
var tileScale = grid.Type == MapGridType.RectangularIsometric ? 1448 : 1024;
|
||||
|
||||
foreach (var trait in traits)
|
||||
{
|
||||
foreach (var traitNode in actorNode.ChildrenMatching(trait))
|
||||
{
|
||||
foreach (var field in fields)
|
||||
{
|
||||
foreach (var fieldNode in traitNode.ChildrenMatching(field))
|
||||
{
|
||||
var value = fieldNode.NodeValue<int[]>();
|
||||
for (var i = 0; i < value.Length; i++)
|
||||
value[i] = value[i] * tileScale / (i % 2 == 1 ? tileSize.Height : tileSize.Width);
|
||||
|
||||
fieldNode.ReplaceValue(FieldSaver.FormatValue(value));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -92,6 +92,7 @@ namespace OpenRA.Mods.Common.UpdateRules
|
||||
new ReplaceShadowPalette(),
|
||||
new ReplaceResourceValueModifiers(),
|
||||
new RemoveResourceType(),
|
||||
new ConvertBoundsToWDist(),
|
||||
})
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user