New spawn selector tooltip for C&C.

This commit is contained in:
Paul Chote
2013-04-06 14:49:13 +13:00
parent 0fb8878273
commit 2cb634b8f5
9 changed files with 144 additions and 14 deletions

View File

@@ -111,10 +111,10 @@
<Compile Include="Widgets\ProductionTabsWidget.cs" />
<Compile Include="Widgets\SupportPowersWidget.cs" />
<Compile Include="Widgets\ToggleButtonWidget.cs" />
<Compile Include="Widgets\TooltipContainerWidget.cs" />
<Compile Include="WithFire.cs" />
<Compile Include="WithRoof.cs" />
<Compile Include="Widgets\ResourceBarWidget.cs" />
<Compile Include="Widgets\Logic\SpawnSelectorTooltipLogic.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -0,0 +1,79 @@
#region Copyright & License Information
/*
* Copyright 2007-2013 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. For more information,
* see COPYING.
*/
#endregion
using System;
using System.Drawing;
using System.Linq;
using OpenRA.Widgets;
using OpenRA.Network;
namespace OpenRA.Mods.Cnc.Widgets.Logic
{
public class SpawnSelectorTooltipLogic
{
[ObjectCreator.UseCtor]
public SpawnSelectorTooltipLogic(Widget widget, TooltipContainerWidget tooltipContainer, MapPreviewWidget preview)
{
widget.IsVisible = () => preview.TooltipSpawnIndex != -1;
var label = widget.Get<LabelWidget>("LABEL");
var flag = widget.Get<ImageWidget>("FLAG");
var team = widget.Get<LabelWidget>("TEAM");
var ownerFont = Game.Renderer.Fonts[label.Font];
var teamFont = Game.Renderer.Fonts[team.Font];
var cachedWidth = 0;
var labelText = "";
string playerCountry = null;
var playerTeam = -1;
tooltipContainer.BeforeRender = () =>
{
var client = preview.SpawnClients().Values.FirstOrDefault(c => c.SpawnPoint == preview.TooltipSpawnIndex);
var teamWidth = 0;
if (client == null)
{
labelText = "Available spawn";
playerCountry = null;
playerTeam = 0;
widget.Bounds.Height = 25;
}
else
{
labelText = client.Name;
playerCountry = client.Country;
playerTeam = client.Team;
widget.Bounds.Height = playerTeam > 0 ? 40 : 25;
teamWidth = teamFont.Measure(team.GetText()).X;
}
label.Bounds.X = playerCountry != null ? flag.Bounds.Right + 5 : 5;
var textWidth = ownerFont.Measure(labelText).X;
if (textWidth != cachedWidth)
{
label.Bounds.Width = textWidth;
widget.Bounds.Width = 2*label.Bounds.X + textWidth;
}
widget.Bounds.Width = Math.Max(teamWidth + 10, label.Bounds.Right + 5);
team.Bounds.Width = widget.Bounds.Width;
};
label.GetText = () => labelText;
flag.IsVisible = () => playerCountry != null;
flag.GetImageCollection = () => "flags";
flag.GetImageName = () => playerCountry;
team.GetText = () => "Team {0}".F(playerTeam);
team.IsVisible = () => playerTeam > 0;
}
}
}

View File

@@ -1,68 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2011 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. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Mods.RA;
using OpenRA.Widgets;
using System;
namespace OpenRA.Mods.Cnc.Widgets
{
public class TooltipContainerWidget : Widget
{
static readonly Action Nothing = () => {};
public int2 CursorOffset = new int2(0, 20);
public Action BeforeRender = Nothing;
public int TooltipDelay = 2;
Widget tooltip;
public TooltipContainerWidget()
{
IsVisible = () => Viewport.TicksSinceLastMove >= TooltipDelay;
}
public void SetTooltip(string id, WidgetArgs args)
{
RemoveTooltip();
tooltip = Ui.LoadWidget(id, this, new WidgetArgs(args) {{ "tooltipContainer", this }});
}
public void RemoveTooltip()
{
RemoveChildren();
BeforeRender = Nothing;
}
public override void Draw() { BeforeRender(); }
public override Rectangle GetEventBounds() { return Rectangle.Empty; }
public override int2 ChildOrigin
{
get
{
var pos = Viewport.LastMousePos + CursorOffset;
if (tooltip != null)
{
if (pos.X + tooltip.Bounds.Right > Game.viewport.Width)
pos.X = Game.viewport.Width - tooltip.Bounds.Right;
}
return pos;
}
}
public override string GetCursor(int2 pos) { return null; }
}
}