Power/Silo bar tooltips.

This commit is contained in:
Paul Chote
2011-07-05 18:10:35 +12:00
parent b4489028de
commit 2f9114fcf8
6 changed files with 99 additions and 1 deletions

View File

@@ -100,6 +100,7 @@
<Compile Include="Widgets\TooltipContainerWidget.cs" />
<Compile Include="Widgets\TooltipButtonWidget.cs" />
<Compile Include="Widgets\Logic\ButtonTooltipLogic.cs" />
<Compile Include="Widgets\Logic\SimpleTooltipLogic.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\OpenRA.FileFormats\OpenRA.FileFormats.csproj">

View File

@@ -0,0 +1,40 @@
#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;
using OpenRA.Support;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets.Logic
{
public class SimpleTooltipLogic
{
[ObjectCreator.UseCtor]
public SimpleTooltipLogic([ObjectCreator.Param] Widget widget,
[ObjectCreator.Param] Func<string> getText)
{
var label = widget.GetWidget<LabelWidget>("LABEL");
var cachedWidth = 0;
var font = Game.Renderer.Fonts[label.Font];
label.GetText = () =>
{
var text = getText();
var textWidth = font.Measure(text).X;
if (textWidth != cachedWidth)
{
label.Bounds.Width = textWidth;
widget.Bounds.Width = 2*label.Bounds.X + textWidth;
}
return text;
};
}
}
}

View File

@@ -10,6 +10,7 @@
using System;
using System.Drawing;
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Widgets;
@@ -18,14 +19,34 @@ namespace OpenRA.Mods.Cnc.Widgets
{
public class PowerBarWidget : Widget
{
public readonly string TooltipTemplate = "SIMPLE_TOOLTIP";
public readonly string TooltipContainer;
Lazy<TooltipContainerWidget> tooltipContainer;
float? lastProvidedFrac;
float? lastDrainedFrac;
readonly PowerManager pm;
[ObjectCreator.UseCtor]
public PowerBarWidget( [ObjectCreator.Param] World world )
{
pm = world.LocalPlayer.PlayerActor.Trait<PowerManager>();
tooltipContainer = new Lazy<TooltipContainerWidget>(() =>
Widget.RootWidget.GetWidget<TooltipContainerWidget>(TooltipContainer));
}
public override void MouseEntered()
{
if (TooltipContainer == null) return;
Func<string> getText = () => "Power Usage: {0}/{1}".F(pm.PowerDrained, pm.PowerProvided);
tooltipContainer.Value.SetTooltip(
Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "getText", getText }}));
}
public override void MouseExited()
{
if (TooltipContainer == null) return;
tooltipContainer.Value.RemoveTooltip();
}
public override void Draw()

View File

@@ -10,6 +10,7 @@
using System;
using System.Drawing;
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Traits;
using OpenRA.Widgets;
@@ -18,6 +19,10 @@ namespace OpenRA.Mods.Cnc.Widgets
{
public class SiloBarWidget : Widget
{
public readonly string TooltipTemplate = "SIMPLE_TOOLTIP";
public readonly string TooltipContainer;
Lazy<TooltipContainerWidget> tooltipContainer;
public float LowStorageThreshold = 0.8f;
float? lastCapacityFrac;
float? lastStoredFrac;
@@ -27,6 +32,22 @@ namespace OpenRA.Mods.Cnc.Widgets
public SiloBarWidget( [ObjectCreator.Param] World world )
{
pr = world.LocalPlayer.PlayerActor.Trait<PlayerResources>();
tooltipContainer = new Lazy<TooltipContainerWidget>(() =>
Widget.RootWidget.GetWidget<TooltipContainerWidget>(TooltipContainer));
}
public override void MouseEntered()
{
if (TooltipContainer == null) return;
Func<string> getText = () => "Silo Usage: {0}/{1}".F(pr.Ore, pr.OreCapacity);
tooltipContainer.Value.SetTooltip(
Widget.LoadWidget(TooltipTemplate, null, new WidgetArgs() {{ "getText", getText }}));
}
public override void MouseExited()
{
if (TooltipContainer == null) return;
tooltipContainer.Value.RemoveTooltip();
}
public override void Draw()

View File

@@ -151,6 +151,7 @@ Container@INGAME_ROOT:
Y:1
Width:PARENT_RIGHT-2
Height:PARENT_BOTTOM-2
TooltipContainer:TOOLTIP_CONTAINER
Label@POWERICON:
X:4
Y:180
@@ -168,6 +169,7 @@ Container@INGAME_ROOT:
Y:1
Width:PARENT_RIGHT-2
Height:PARENT_BOTTOM-2
TooltipContainer:TOOLTIP_CONTAINER
Label@SILOICON:
X:181
Y:180

View File

@@ -1,3 +1,16 @@
Background@SIMPLE_TOOLTIP:
Id:SIMPLE_TOOLTIP
Logic:SimpleTooltipLogic
Background:panel-black
Width:100
Height:25
Children:
Label@LABEL:
Id:LABEL
X:5
Height:23
Font:Bold
Background@PRODUCTION_TOOLTIP:
Id:PRODUCTION_TOOLTIP
Logic:ProductionTooltipLogic