Clean up SupportPowerTooltipLogic:
- Avoid creating unnecessary bindings - Avoid duplicated text size calculations - Relayout panel when (and only when) needed - Color timer red when paused
This commit is contained in:
@@ -10,6 +10,7 @@
|
|||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
using System.Drawing;
|
||||||
using OpenRA.Mods.Common.Traits;
|
using OpenRA.Mods.Common.Traits;
|
||||||
using OpenRA.Widgets;
|
using OpenRA.Widgets;
|
||||||
|
|
||||||
@@ -18,70 +19,75 @@ namespace OpenRA.Mods.Common.Widgets.Logic
|
|||||||
public class SupportPowerTooltipLogic : ChromeLogic
|
public class SupportPowerTooltipLogic : ChromeLogic
|
||||||
{
|
{
|
||||||
[ObjectCreator.UseCtor]
|
[ObjectCreator.UseCtor]
|
||||||
public SupportPowerTooltipLogic(Widget widget, TooltipContainerWidget tooltipContainer, SupportPowersWidget palette, World world)
|
public SupportPowerTooltipLogic(Widget widget, TooltipContainerWidget tooltipContainer, Player player, SupportPowersWidget palette, World world)
|
||||||
{
|
{
|
||||||
widget.IsVisible = () => palette.TooltipIcon != null;
|
widget.IsVisible = () => palette.TooltipIcon != null && palette.TooltipIcon.Power.Info != null;
|
||||||
var nameLabel = widget.Get<LabelWidget>("NAME");
|
var nameLabel = widget.Get<LabelWidget>("NAME");
|
||||||
var hotkeyLabel = widget.Get<LabelWidget>("HOTKEY");
|
var hotkeyLabel = widget.Get<LabelWidget>("HOTKEY");
|
||||||
var timeLabel = widget.Get<LabelWidget>("TIME");
|
var timeLabel = widget.Get<LabelWidget>("TIME");
|
||||||
var descLabel = widget.Get<LabelWidget>("DESC");
|
var descLabel = widget.Get<LabelWidget>("DESC");
|
||||||
var nameFont = Game.Renderer.Fonts[nameLabel.Font];
|
var nameFont = Game.Renderer.Fonts[nameLabel.Font];
|
||||||
|
var hotkeyFont = Game.Renderer.Fonts[hotkeyLabel.Font];
|
||||||
var timeFont = Game.Renderer.Fonts[timeLabel.Font];
|
var timeFont = Game.Renderer.Fonts[timeLabel.Font];
|
||||||
var descFont = Game.Renderer.Fonts[descLabel.Font];
|
var descFont = Game.Renderer.Fonts[descLabel.Font];
|
||||||
var name = "";
|
|
||||||
var time = "";
|
|
||||||
var desc = "";
|
|
||||||
var baseHeight = widget.Bounds.Height;
|
var baseHeight = widget.Bounds.Height;
|
||||||
var timeOffset = timeLabel.Bounds.X;
|
var timeOffset = timeLabel.Bounds.X;
|
||||||
|
var pm = player.PlayerActor.Trait<PowerManager>();
|
||||||
|
|
||||||
SupportPowerInstance lastPower = null;
|
SupportPowerInstance lastPower = null;
|
||||||
|
Hotkey lastHotkey = Hotkey.Invalid;
|
||||||
|
var lastRemainingSeconds = 0;
|
||||||
|
|
||||||
tooltipContainer.BeforeRender = () =>
|
tooltipContainer.BeforeRender = () =>
|
||||||
{
|
{
|
||||||
var icon = palette.TooltipIcon;
|
var icon = palette.TooltipIcon;
|
||||||
|
|
||||||
if (icon == null)
|
if (icon == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var sp = icon.Power;
|
var sp = icon.Power;
|
||||||
|
|
||||||
if (sp.Info == null)
|
// HACK: This abuses knowledge of the internals of WidgetUtils.FormatTime
|
||||||
return; // no instances actually exist (race with destroy)
|
// to efficiently work when the label is going to change, requiring a panel relayout
|
||||||
|
var remainingSeconds = (int)Math.Ceiling(sp.RemainingTime * world.Timestep / 1000f);
|
||||||
|
|
||||||
|
var hotkey = icon.Hotkey != null ? icon.Hotkey.GetValue() : Hotkey.Invalid;
|
||||||
|
if (sp == lastPower && hotkey == lastHotkey && lastRemainingSeconds == remainingSeconds)
|
||||||
|
return;
|
||||||
|
|
||||||
|
nameLabel.Text = sp.Info.Description;
|
||||||
|
var nameSize = nameFont.Measure(nameLabel.Text);
|
||||||
|
|
||||||
|
descLabel.Text = sp.Info.LongDesc.Replace("\\n", "\n");
|
||||||
|
var descSize = descFont.Measure(descLabel.Text);
|
||||||
|
|
||||||
var remaining = WidgetUtils.FormatTime(sp.RemainingTime, world.Timestep);
|
var remaining = WidgetUtils.FormatTime(sp.RemainingTime, world.Timestep);
|
||||||
var total = WidgetUtils.FormatTime(sp.Info.ChargeTime * 25, world.Timestep);
|
var total = WidgetUtils.FormatTime(sp.Info.ChargeTime * 25, world.Timestep);
|
||||||
time = "{0} / {1}".F(remaining, total);
|
timeLabel.Text = "{0} / {1}".F(remaining, total);
|
||||||
|
var timeSize = timeFont.Measure(timeLabel.Text);
|
||||||
if (sp == lastPower)
|
|
||||||
return;
|
|
||||||
|
|
||||||
name = sp.Info.Description;
|
|
||||||
desc = sp.Info.LongDesc.Replace("\\n", "\n");
|
|
||||||
|
|
||||||
var hotkeyWidth = 0;
|
var hotkeyWidth = 0;
|
||||||
var hotkey = icon.Hotkey != null ? icon.Hotkey.GetValue() : Hotkey.Invalid;
|
|
||||||
hotkeyLabel.Visible = hotkey.IsValid();
|
hotkeyLabel.Visible = hotkey.IsValid();
|
||||||
|
|
||||||
if (hotkeyLabel.Visible)
|
if (hotkeyLabel.Visible)
|
||||||
{
|
{
|
||||||
var hotkeyText = "({0})".F(hotkey.DisplayString());
|
var hotkeyText = "({0})".F(hotkey.DisplayString());
|
||||||
|
|
||||||
hotkeyWidth = nameFont.Measure(hotkeyText).X + 2 * nameLabel.Bounds.X;
|
hotkeyWidth = hotkeyFont.Measure(hotkeyText).X + 2 * nameLabel.Bounds.X;
|
||||||
hotkeyLabel.Text = hotkeyText;
|
hotkeyLabel.Text = hotkeyText;
|
||||||
hotkeyLabel.Bounds.X = nameFont.Measure(name).X + 2 * nameLabel.Bounds.X;
|
hotkeyLabel.Bounds.X = nameSize.X + 2 * nameLabel.Bounds.X;
|
||||||
}
|
}
|
||||||
|
|
||||||
var timeWidth = timeFont.Measure(time).X;
|
var timeWidth = timeSize.X;
|
||||||
var topWidth = nameFont.Measure(name).X + hotkeyWidth + timeWidth + timeOffset;
|
var topWidth = nameSize.X + hotkeyWidth + timeWidth + timeOffset;
|
||||||
var descSize = descFont.Measure(desc);
|
|
||||||
widget.Bounds.Width = 2 * nameLabel.Bounds.X + Math.Max(topWidth, descSize.X);
|
widget.Bounds.Width = 2 * nameLabel.Bounds.X + Math.Max(topWidth, descSize.X);
|
||||||
widget.Bounds.Height = baseHeight + descSize.Y;
|
widget.Bounds.Height = baseHeight + descSize.Y;
|
||||||
timeLabel.Bounds.X = widget.Bounds.Width - nameLabel.Bounds.X - timeWidth;
|
timeLabel.Bounds.X = widget.Bounds.Width - nameLabel.Bounds.X - timeWidth;
|
||||||
|
|
||||||
lastPower = sp;
|
lastPower = sp;
|
||||||
|
lastHotkey = hotkey;
|
||||||
|
lastRemainingSeconds = remainingSeconds;
|
||||||
};
|
};
|
||||||
|
|
||||||
nameLabel.GetText = () => name;
|
timeLabel.GetColor = () => pm.PowerState != PowerState.Normal ? Color.Red : Color.White;
|
||||||
timeLabel.GetText = () => time;
|
|
||||||
descLabel.GetText = () => desc;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -228,7 +228,7 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
tooltipContainer.Value.SetTooltip(TooltipTemplate,
|
tooltipContainer.Value.SetTooltip(TooltipTemplate,
|
||||||
new WidgetArgs() { { "world", worldRenderer.World }, { "palette", this } });
|
new WidgetArgs() { { "world", worldRenderer.World }, { "player", spm.Self.Owner }, { "palette", this } });
|
||||||
}
|
}
|
||||||
|
|
||||||
public override void MouseExited()
|
public override void MouseExited()
|
||||||
|
|||||||
Reference in New Issue
Block a user