#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.Support; using OpenRA.Traits; using OpenRA.Widgets; using OpenRA.Mods.RA; using OpenRA.Mods.RA.Buildings; namespace OpenRA.Mods.Cnc.Widgets.Logic { public class ProductionTooltipLogic { [ObjectCreator.UseCtor] public ProductionTooltipLogic([ObjectCreator.Param] Widget widget, [ObjectCreator.Param] TooltipContainerWidget tooltipContainer, [ObjectCreator.Param] ProductionPaletteWidget palette) { var pm = palette.world.LocalPlayer.PlayerActor.Trait(); var pr = palette.world.LocalPlayer.PlayerActor.Trait(); widget.IsVisible = () => palette.TooltipActor != null; var nameLabel = widget.GetWidget("NAME"); var requiresLabel = widget.GetWidget("REQUIRES"); var powerLabel = widget.GetWidget("POWER"); var timeLabel = widget.GetWidget("TIME"); var costLabel = widget.GetWidget("COST"); var font = Game.Renderer.Fonts[nameLabel.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() ? "Requires {0}".F(string.Join(", ", prereqs.ToArray())) : ""; 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 leftWidth = Math.Max(font.Measure(tooltip.Name).X, requiresFont.Measure(requiresString).X); 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; widget.Bounds.Height = power != 0 ? 65 : 45; lastActor = actor; }; } static string ActorName( string a ) { // hack hack hack - going to die soon anyway if (a == "barracks") return "Infantry Production"; if (a == "vehicleproduction") return "Vehicle Production"; if (a == "techcenter") return "Tech Center"; if (a == "anypower") return "Power Plant"; ActorInfo ai; Rules.Info.TryGetValue(a.ToLowerInvariant(), out ai); if (ai != null && ai.Traits.Contains()) return ai.Traits.Get().Name; return a; } } }