#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 System.Linq; using OpenRA.Mods.RA; using OpenRA.Mods.RA.Buildings; using OpenRA.Traits; using OpenRA.Widgets; namespace OpenRA.Mods.Cnc.Widgets.Logic { public class ProductionTooltipLogic { [ObjectCreator.UseCtor] public ProductionTooltipLogic(Widget widget, TooltipContainerWidget tooltipContainer, ProductionPaletteWidget palette) { var pm = palette.World.LocalPlayer.PlayerActor.Trait(); var pr = palette.World.LocalPlayer.PlayerActor.Trait(); widget.IsVisible = () => palette.TooltipActor != null; var nameLabel = widget.Get("NAME"); var requiresLabel = widget.Get("REQUIRES"); var powerLabel = widget.Get("POWER"); var timeLabel = widget.Get("TIME"); var costLabel = widget.Get("COST"); var descLabel = widget.Get("DESC"); var font = Game.Renderer.Fonts[nameLabel.Font]; var descFont = Game.Renderer.Fonts[descLabel.Font]; var requiresFont = Game.Renderer.Fonts[requiresLabel.Font]; string lastActor = null; tooltipContainer.BeforeRender = () => { var actor = palette.TooltipActor; if (actor == null || actor == lastActor) return; var info = Rules.Info[actor]; var tooltip = info.Traits.Get(); var buildable = info.Traits.Get(); var cost = info.Traits.Get().Cost; var bi = info.Traits.GetOrDefault(); nameLabel.GetText = () => tooltip.Name; var prereqs = buildable.Prerequisites.Select(a => ActorName(a)); var requiresString = prereqs.Any() ? requiresLabel.Text.F(prereqs.JoinWith(", ")) : ""; requiresLabel.GetText = () => requiresString; var power = bi != null ? bi.Power : 0; var powerString = "P: {0}".F(power); powerLabel.GetText = () => powerString; powerLabel.GetColor = () => ((pm.PowerProvided - pm.PowerDrained) >= -power || power > 0) ? Color.White : Color.Red; powerLabel.IsVisible = () => power != 0; var timeString = "T: {0}".F(WidgetUtils.FormatTime(palette.CurrentQueue.GetBuildTime(actor))); timeLabel.GetText = () => timeString; var costString = "$: {0}".F(cost); costLabel.GetText = () => costString; costLabel.GetColor = () => pr.DisplayCash + pr.DisplayOre >= cost ? Color.White : Color.Red; var descString = tooltip.Description.Replace("\\n", "\n"); descLabel.GetText = () => descString; var leftWidth = new[] { font.Measure(tooltip.Name).X, requiresFont.Measure(requiresString).X, descFont.Measure(descString).X }.Aggregate(Math.Max); var rightWidth = new[] { font.Measure(powerString).X, font.Measure(timeString).X, font.Measure(costString).X }.Aggregate(Math.Max); timeLabel.Bounds.X = powerLabel.Bounds.X = costLabel.Bounds.X = leftWidth + 2 * nameLabel.Bounds.X; widget.Bounds.Width = leftWidth + rightWidth + 3 * nameLabel.Bounds.X; var leftHeight = font.Measure(tooltip.Name).Y + requiresFont.Measure(requiresString).Y + descFont.Measure(descString).Y; var rightHeight = font.Measure(powerString).Y + font.Measure(timeString).Y + font.Measure(costString).Y; widget.Bounds.Height = Math.Max(leftHeight, rightHeight) * 3 / 2 + 3 * nameLabel.Bounds.Y; lastActor = actor; }; } static string ActorName(string a) { ActorInfo ai; Rules.Info.TryGetValue(a.ToLowerInvariant(), out ai); if (ai != null && ai.Traits.Contains()) return ai.Traits.Get().Name; return a; } } }