Added a MouseAttachmentWidget to render the Direction arrows in SelectDirectionalTarget

This commit is contained in:
teinarss
2019-06-09 20:33:54 +02:00
committed by abcdefg30
parent f07fb57e98
commit d7643602c1
4 changed files with 69 additions and 13 deletions

View File

@@ -13,7 +13,9 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Graphics; using OpenRA.Graphics;
using OpenRA.Mods.Common.Widgets;
using OpenRA.Traits; using OpenRA.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
@@ -36,6 +38,7 @@ namespace OpenRA.Mods.Common.Traits
bool activated; bool activated;
bool dragStarted; bool dragStarted;
Arrow currentArrow; Arrow currentArrow;
MouseAttachmentWidget mouseAttachment;
public SelectDirectionalTarget(World world, string order, SupportPowerManager manager, string cursor, public SelectDirectionalTarget(World world, string order, SupportPowerManager manager, string cursor,
string directionArrowAnimation, string directionArrowPalette) string directionArrowAnimation, string directionArrowPalette)
@@ -47,6 +50,7 @@ namespace OpenRA.Mods.Common.Traits
this.directionArrowPalette = directionArrowPalette; this.directionArrowPalette = directionArrowPalette;
directionArrows = LoadArrows(directionArrowAnimation, world, arrows.Length); directionArrows = LoadArrows(directionArrowAnimation, world, arrows.Length);
mouseAttachment = Ui.Root.Get<MouseAttachmentWidget>("MOUSE_ATTATCHMENT");
} }
IEnumerable<Order> IOrderGenerator.Order(World world, CPos cell, int2 worldPixel, MouseInput mi) IEnumerable<Order> 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))); dragDirection = -MaxDragThreshold * float2.FromAngle((float)(angle * (Math.PI / 180)));
currentArrow = GetArrow(angle); currentArrow = GetArrow(angle);
mouseAttachment.SetAttachment(targetLocation, currentArrow.Sprite, directionArrowPalette);
dragStarted = true; dragStarted = true;
} }
@@ -111,19 +117,7 @@ namespace OpenRA.Mods.Common.Traits
IEnumerable<IRenderable> IOrderGenerator.Render(WorldRenderer wr, World world) { yield break; } IEnumerable<IRenderable> IOrderGenerator.Render(WorldRenderer wr, World world) { yield break; }
IEnumerable<IRenderable> IOrderGenerator.RenderAboveShroud(WorldRenderer wr, World world) IEnumerable<IRenderable> IOrderGenerator.RenderAboveShroud(WorldRenderer wr, World world) { yield break; }
{
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);
}
string IOrderGenerator.GetCursor(World world, CPos cell, int2 worldPixel, MouseInput mi) { return cursor; } string IOrderGenerator.GetCursor(World world, CPos cell, int2 worldPixel, MouseInput mi) { return cursor; }
@@ -132,8 +126,11 @@ namespace OpenRA.Mods.Common.Traits
void IOrderGenerator.Deactivate() void IOrderGenerator.Deactivate()
{ {
if (activated) if (activated)
{
mouseAttachment.Reset();
Game.Cursor.Unlock(); Game.Cursor.Unlock();
} }
}
// Starting at (0, -1) and rotating in CCW // Starting at (0, -1) and rotating in CCW
static double AngleOf(float2 delta) static double AngleOf(float2 delta)

View File

@@ -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; } }
}
}

View File

@@ -45,6 +45,7 @@ Container@INGAME_ROOT:
Container@PLAYER_ROOT: Container@PLAYER_ROOT:
Container@MENU_ROOT: Container@MENU_ROOT:
TooltipContainer@TOOLTIP_CONTAINER: TooltipContainer@TOOLTIP_CONTAINER:
MouseAttachment@MOUSE_ATTATCHMENT:
Container@PERF_WIDGETS: Container@PERF_WIDGETS:
Logic: PerfDebugLogic Logic: PerfDebugLogic

View File

@@ -60,3 +60,4 @@ Container@INGAME_ROOT:
Container@PERF_ROOT: Container@PERF_ROOT:
Container@MENU_ROOT: Container@MENU_ROOT:
TooltipContainer@TOOLTIP_CONTAINER: TooltipContainer@TOOLTIP_CONTAINER:
MouseAttachment@MOUSE_ATTATCHMENT: