#region Copyright & License Information /* * Copyright 2007-2014 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.Mods.RA.Widgets; using OpenRA.Widgets; namespace OpenRA.Mods.Cnc.Widgets.Logic { public class ProductionTabsLogic { ProductionTabsWidget tabs; readonly World world; void SetupProductionGroupButton(ProductionTypeButtonWidget button) { if (button == null) return; Action selectTab = reverse => { if (tabs.QueueGroup == button.ProductionGroup) tabs.SelectNextTab(reverse); else tabs.QueueGroup = button.ProductionGroup; }; button.IsDisabled = () => tabs.Groups[button.ProductionGroup].Tabs.Count == 0; button.OnMouseUp = mi => selectTab(mi.Modifiers.HasModifier(Modifiers.Shift)); button.OnKeyPress = e => selectTab(e.Modifiers.HasModifier(Modifiers.Shift)); button.IsHighlighted = () => tabs.QueueGroup == button.ProductionGroup; var chromeName = button.ProductionGroup.ToLowerInvariant(); var icon = button.Get("ICON"); icon.GetImageName = () => button.IsDisabled() ? chromeName + "-disabled" : tabs.Groups[button.ProductionGroup].Alert ? chromeName + "-alert" : chromeName; } [ObjectCreator.UseCtor] public ProductionTabsLogic(Widget widget, World world) { this.world = world; tabs = widget.Get("PRODUCTION_TABS"); world.ActorAdded += tabs.ActorChanged; world.ActorRemoved += tabs.ActorChanged; Game.BeforeGameStart += UnregisterEvents; var typesContainer = Ui.Root.Get(tabs.TypesContainer); foreach (var i in typesContainer.Children) SetupProductionGroupButton(i as ProductionTypeButtonWidget); var background = Ui.Root.GetOrNull(tabs.BackgroundContainer); if (background != null) { var palette = tabs.Parent.Get(tabs.PaletteWidget); var icontemplate = background.Get("ICON_TEMPLATE"); Action updateBackground = (oldCount, newCount) => { background.RemoveChildren(); for (var i = 0; i < newCount; i++) { var x = i % palette.Columns; var y = i / palette.Columns; var bg = icontemplate.Clone(); bg.Bounds.X = palette.IconSize.X * x; bg.Bounds.Y = palette.IconSize.Y * y; background.AddChild(bg); } }; palette.OnIconCountChanged += updateBackground; // Set the initial palette state updateBackground(0, 0); } } void UnregisterEvents() { Game.BeforeGameStart -= UnregisterEvents; world.ActorAdded -= tabs.ActorChanged; world.ActorRemoved -= tabs.ActorChanged; } } }