diff --git a/OpenRA.Mods.Common/Traits/SupportPowers/SelectDirectionalTarget.cs b/OpenRA.Mods.Common/Traits/SupportPowers/SelectDirectionalTarget.cs index 68fa467665..9b29e4f2e0 100644 --- a/OpenRA.Mods.Common/Traits/SupportPowers/SelectDirectionalTarget.cs +++ b/OpenRA.Mods.Common/Traits/SupportPowers/SelectDirectionalTarget.cs @@ -13,7 +13,9 @@ using System; using System.Collections.Generic; using System.Linq; using OpenRA.Graphics; +using OpenRA.Mods.Common.Widgets; using OpenRA.Traits; +using OpenRA.Widgets; namespace OpenRA.Mods.Common.Traits { @@ -36,6 +38,7 @@ namespace OpenRA.Mods.Common.Traits bool activated; bool dragStarted; Arrow currentArrow; + MouseAttachmentWidget mouseAttachment; public SelectDirectionalTarget(World world, string order, SupportPowerManager manager, string cursor, string directionArrowAnimation, string directionArrowPalette) @@ -47,6 +50,7 @@ namespace OpenRA.Mods.Common.Traits this.directionArrowPalette = directionArrowPalette; directionArrows = LoadArrows(directionArrowAnimation, world, arrows.Length); + mouseAttachment = Ui.Root.Get("MOUSE_ATTATCHMENT"); } IEnumerable IOrderGenerator.Order(World world, CPos cell, int2 worldPixel, MouseInput mi) @@ -82,6 +86,8 @@ namespace OpenRA.Mods.Common.Traits dragDirection = -MaxDragThreshold * float2.FromAngle((float)(angle * (Math.PI / 180))); currentArrow = GetArrow(angle); + + mouseAttachment.SetAttachment(targetLocation, currentArrow.Sprite, directionArrowPalette); dragStarted = true; } @@ -111,19 +117,7 @@ namespace OpenRA.Mods.Common.Traits IEnumerable IOrderGenerator.Render(WorldRenderer wr, World world) { yield break; } - IEnumerable IOrderGenerator.RenderAboveShroud(WorldRenderer wr, World world) - { - if (!activated || !IsOutsideDragZone) - yield break; - - var worldPx = wr.Viewport.ViewToWorldPx(targetLocation); - var worldPos = wr.ProjectedPosition(worldPx); - - var scale = (Game.Cursor is SoftwareCursor && CursorProvider.CursorViewportZoomed ? 2 : 1) / wr.Viewport.Zoom; - - var directionPalette = wr.Palette(directionArrowPalette); - yield return new SpriteRenderable(currentArrow.Sprite, worldPos, WVec.Zero, -511, directionPalette, scale, true); - } + IEnumerable IOrderGenerator.RenderAboveShroud(WorldRenderer wr, World world) { yield break; } string IOrderGenerator.GetCursor(World world, CPos cell, int2 worldPixel, MouseInput mi) { return cursor; } @@ -132,7 +126,10 @@ namespace OpenRA.Mods.Common.Traits void IOrderGenerator.Deactivate() { if (activated) + { + mouseAttachment.Reset(); Game.Cursor.Unlock(); + } } // Starting at (0, -1) and rotating in CCW diff --git a/OpenRA.Mods.Common/Widgets/MouseAttachmentWidget.cs b/OpenRA.Mods.Common/Widgets/MouseAttachmentWidget.cs new file mode 100644 index 0000000000..fcea756b4b --- /dev/null +++ b/OpenRA.Mods.Common/Widgets/MouseAttachmentWidget.cs @@ -0,0 +1,57 @@ +#region Copyright & License Information +/* + * Copyright 2007-2019 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 +{ + public class MouseAttachmentWidget : Widget + { + public bool ClickThrough = true; + + Sprite sprite; + readonly WorldRenderer worldRenderer; + string palette; + int2 location; + + [ObjectCreator.UseCtor] + public MouseAttachmentWidget(WorldRenderer worldRenderer) + { + this.worldRenderer = worldRenderer; + } + + public override void Draw() + { + if (sprite != null && palette != null) + { + var scale = Game.Cursor is SoftwareCursor && CursorProvider.CursorViewportZoomed ? 2 : 1; + var directionPalette = worldRenderer.Palette(palette); + WidgetUtils.DrawSHPCentered(sprite, ChildOrigin, directionPalette, scale); + } + } + + public void SetAttachment(int2 location, Sprite sprite, string palette) + { + this.sprite = sprite; + this.location = location; + this.palette = palette; + } + + public void Reset() + { + sprite = null; + palette = null; + } + + public override int2 ChildOrigin { get { return location; } } + } +} diff --git a/mods/cnc/chrome/ingame.yaml b/mods/cnc/chrome/ingame.yaml index ba6449e6a8..46e8d41fc6 100644 --- a/mods/cnc/chrome/ingame.yaml +++ b/mods/cnc/chrome/ingame.yaml @@ -45,6 +45,7 @@ Container@INGAME_ROOT: Container@PLAYER_ROOT: Container@MENU_ROOT: TooltipContainer@TOOLTIP_CONTAINER: + MouseAttachment@MOUSE_ATTATCHMENT: Container@PERF_WIDGETS: Logic: PerfDebugLogic diff --git a/mods/common/chrome/ingame.yaml b/mods/common/chrome/ingame.yaml index bca8cf8632..3f2b9780b4 100644 --- a/mods/common/chrome/ingame.yaml +++ b/mods/common/chrome/ingame.yaml @@ -60,3 +60,4 @@ Container@INGAME_ROOT: Container@PERF_ROOT: Container@MENU_ROOT: TooltipContainer@TOOLTIP_CONTAINER: + MouseAttachment@MOUSE_ATTATCHMENT: