Merge pull request #3525 from pchote/ra-widget-cleanup

Widget fixes
This commit is contained in:
Matthias Mailänder
2013-07-08 10:39:25 -07:00
23 changed files with 431 additions and 580 deletions

View File

@@ -102,7 +102,6 @@
<Compile Include="Widgets\Logic\CncPerfDebugLogic.cs" />
<Compile Include="Widgets\Logic\CncSettingsLogic.cs" />
<Compile Include="Widgets\Logic\ProductionTooltipLogic.cs" />
<Compile Include="Widgets\Logic\SimpleTooltipLogic.cs" />
<Compile Include="Widgets\Logic\SupportPowerTooltipLogic.cs" />
<Compile Include="Widgets\Logic\WorldTooltipLogic.cs" />
<Compile Include="Widgets\ProductionPaletteWidget.cs" />
@@ -110,7 +109,6 @@
<Compile Include="Widgets\SupportPowersWidget.cs" />
<Compile Include="WithFire.cs" />
<Compile Include="WithRoof.cs" />
<Compile Include="Widgets\ResourceBarWidget.cs" />
<Compile Include="Widgets\Logic\ProductionTabsLogic.cs" />
<Compile Include="Widgets\ProductionTypeButtonWidget.cs" />
</ItemGroup>

View File

@@ -9,6 +9,8 @@
#endregion
using System.Drawing;
using System.Linq;
using OpenRA.Mods.RA;
using OpenRA.Mods.RA.Buildings;
using OpenRA.Mods.RA.Orders;
using OpenRA.Mods.RA.Widgets;
@@ -100,9 +102,22 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
playerWidgets.Get<ButtonWidget>("OPTIONS_BUTTON").OnClick = OptionsClicked;
var winLossWatcher = playerWidgets.Get<LogicTickerWidget>("WIN_LOSS_WATCHER");
winLossWatcher.OnTick = () =>
var radarEnabled = false;
var cachedRadarEnabled = false;
sidebarRoot.Get<RadarWidget>("RADAR_MINIMAP").IsEnabled = () => radarEnabled;
var sidebarTicker = playerWidgets.Get<LogicTickerWidget>("SIDEBAR_TICKER");
sidebarTicker.OnTick = () =>
{
// Update radar bin
radarEnabled = world.ActorsWithTrait<ProvidesRadar>()
.Any(a => a.Actor.Owner == world.LocalPlayer && a.Trait.IsActive);
if (radarEnabled != cachedRadarEnabled)
Sound.PlayNotification(null, "Sounds", (radarEnabled ? "RadarUp" : "RadarDown"), null);
cachedRadarEnabled = radarEnabled;
// Switch to observer mode after win/loss
if (world.LocalPlayer.WinState != WinState.Undefined)
Game.RunAfterTick(() =>
{
@@ -115,11 +130,12 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
siloBar.GetProvided = () => playerResources.OreCapacity;
siloBar.GetUsed = () => playerResources.Ore;
siloBar.TooltipFormat = "Silo Usage: {0}/{1}";
siloBar.RightIndicator = true;
siloBar.GetBarColor = () =>
{
if (playerResources.Ore == playerResources.OreCapacity) return Color.Red;
if (playerResources.Ore >= 0.8 * playerResources.OreCapacity) return Color.Orange;
if (playerResources.Ore == playerResources.OreCapacity)
return Color.Red;
if (playerResources.Ore >= 0.8 * playerResources.OreCapacity)
return Color.Orange;
return Color.LimeGreen;
};
@@ -127,11 +143,12 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic
powerBar.GetProvided = () => powerManager.PowerProvided;
powerBar.GetUsed = () => powerManager.PowerDrained;
powerBar.TooltipFormat = "Power Usage: {0}/{1}";
powerBar.RightIndicator = false;
powerBar.GetBarColor = () =>
{
if (powerManager.PowerState == PowerState.Critical) return Color.Red;
if (powerManager.PowerState == PowerState.Low) return Color.Orange;
if (powerManager.PowerState == PowerState.Critical)
return Color.Red;
if (powerManager.PowerState == PowerState.Low)
return Color.Orange;
return Color.LimeGreen;
};
}

View File

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

View File

@@ -1,84 +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;
using System.Drawing;
using OpenRA.FileFormats;
using OpenRA.Graphics;
using OpenRA.Traits;
using OpenRA.Widgets;
namespace OpenRA.Mods.Cnc.Widgets
{
public class ResourceBarWidget : Widget
{
public readonly string TooltipTemplate = "SIMPLE_TOOLTIP";
public readonly string TooltipContainer;
Lazy<TooltipContainerWidget> tooltipContainer;
public float LowStorageThreshold = 0.8f;
EWMA providedLerp = new EWMA(0.3f);
EWMA usedLerp = new EWMA(0.3f);
public Func<float> GetProvided = () => 0;
public Func<float> GetUsed = () => 0;
public string TooltipFormat = "";
public bool RightIndicator = false;
public Func<Color> GetBarColor = () => Color.White;
[ObjectCreator.UseCtor]
public ResourceBarWidget(World world)
{
tooltipContainer = Lazy.New(() =>
Ui.Root.Get<TooltipContainerWidget>(TooltipContainer));
}
public override void MouseEntered()
{
if (TooltipContainer == null) return;
Func<string> getText = () => TooltipFormat.F(GetUsed(), GetProvided());
tooltipContainer.Value.SetTooltip(TooltipTemplate, new WidgetArgs() {{ "getText", getText }});
}
public override void MouseExited()
{
if (TooltipContainer == null) return;
tooltipContainer.Value.RemoveTooltip();
}
public override void Draw()
{
var scaleBy = 100.0f;
var provided = GetProvided();
var used = GetUsed();
var max = Math.Max(provided, used);
while (max >= scaleBy) scaleBy *= 2;
var providedFrac = providedLerp.Update(provided/scaleBy);
var usedFrac = usedLerp.Update(used/scaleBy);
var color = GetBarColor();
var b = RenderBounds;
var rect = new RectangleF(b.X, float2.Lerp( b.Bottom, b.Top, providedFrac ),
b.Width, providedFrac*b.Height);
Game.Renderer.LineRenderer.FillRect(rect, color);
var indicator = ChromeProvider.GetImage("sidebar-bits",
RightIndicator ? "right-indicator" : "left-indicator");
var indicatorX = RightIndicator ? (b.Right - indicator.size.X) : b.Left;
var pos = new float2(indicatorX, float2.Lerp( b.Bottom, b.Top, usedFrac ) - indicator.size.Y / 2);
Game.Renderer.RgbaSpriteRenderer.DrawSprite(indicator, pos);
}
}
}