diff --git a/OpenRA.Mods.Common/Traits/Render/RenderMouseBounds.cs b/OpenRA.Mods.Common/Traits/Render/RenderMouseBounds.cs new file mode 100644 index 0000000000..9255bd9a49 --- /dev/null +++ b/OpenRA.Mods.Common/Traits/Render/RenderMouseBounds.cs @@ -0,0 +1,67 @@ +#region Copyright & License Information +/* + * Copyright (c) The OpenRA Developers and Contributors + * 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.Graphics; +using OpenRA.Mods.Common.Graphics; +using OpenRA.Primitives; +using OpenRA.Traits; + +namespace OpenRA.Mods.Common.Traits.Render +{ + [Desc("Renders polygon for mouse bounds (usually defined by " + nameof(Interactable) + " or " + nameof(Selectable) + ").", + "Put on actor for which the polygon should be rendered.")] + public class RenderMouseBoundsInfo : TraitInfo, Requires + { + [Desc("Color to use for the polygon lines.")] + public readonly Color PolygonLineColor = Color.Green; + + public override object Create(ActorInitializer init) { return new RenderMouseBounds(init.Self, this); } + } + + public class RenderMouseBounds : IRenderAnnotations + { + readonly IMouseBounds mouseBounds; + readonly RenderMouseBoundsInfo info; + + public RenderMouseBounds(Actor self, RenderMouseBoundsInfo info) + { + mouseBounds = self.Trait(); + this.info = info; + } + + bool IRenderAnnotations.SpatiallyPartitionable => true; + + IEnumerable IRenderAnnotations.RenderAnnotations(Actor self, WorldRenderer wr) + { + if (self.World.FogObscures(self)) + return Enumerable.Empty(); + + return DrawDecorations(self, wr); + } + + IEnumerable DrawDecorations(Actor self, WorldRenderer wr) + { + var polygon = mouseBounds.MouseoverBounds(self, wr); + var vertices = new WPos[polygon.Vertices.Length]; + + for (var i = 0; i < polygon.Vertices.Length; i++) + { + var screenVertex = polygon.Vertices[i]; + + vertices[i] = wr.ProjectedPosition(screenVertex); + } + + yield return new PolygonAnnotationRenderable(vertices, vertices[0], 1, info.PolygonLineColor); + } + } +}