From 2de9d3780b203dacccdfb070ac61575d438ccb37 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sat, 12 Jul 2014 21:37:06 +1200 Subject: [PATCH] Extract ProductionPalette background drawing into logic code. --- .../Widgets/Logic/ProductionTabsLogic.cs | 28 ++++++++++++++ .../Widgets/ProductionPaletteWidget.cs | 38 +++++++++++++------ .../Widgets/ProductionTabsWidget.cs | 1 + mods/cnc/chrome/ingame.yaml | 24 ++++++++---- 4 files changed, 72 insertions(+), 19 deletions(-) diff --git a/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTabsLogic.cs b/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTabsLogic.cs index 1744623471..aebc3e63f0 100644 --- a/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTabsLogic.cs +++ b/OpenRA.Mods.Cnc/Widgets/Logic/ProductionTabsLogic.cs @@ -55,6 +55,34 @@ namespace OpenRA.Mods.Cnc.Widgets.Logic 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() diff --git a/OpenRA.Mods.RA/Widgets/ProductionPaletteWidget.cs b/OpenRA.Mods.RA/Widgets/ProductionPaletteWidget.cs index 5458939f74..b9c7143376 100644 --- a/OpenRA.Mods.RA/Widgets/ProductionPaletteWidget.cs +++ b/OpenRA.Mods.RA/Widgets/ProductionPaletteWidget.cs @@ -37,6 +37,8 @@ namespace OpenRA.Mods.RA.Widgets public readonly ReadyTextStyleOptions ReadyTextStyle = ReadyTextStyleOptions.Blinking; public readonly Color ReadyTextAltColor = Color.Red; public readonly int Columns = 3; + public readonly int2 IconSize = new int2(64, 48); + public readonly string TabClick = null; public readonly string DisabledTabClick = null; public readonly string TooltipContainer; @@ -45,6 +47,9 @@ namespace OpenRA.Mods.RA.Widgets [Translate] public readonly string ReadyText = ""; [Translate] public readonly string HoldText = ""; + public int IconCount { get; private set; } + public event Action OnIconCountChanged = (a, b) => {}; + public string TooltipActor { get; private set; } public readonly World World; readonly OrderManager orderManager; @@ -176,18 +181,30 @@ namespace OpenRA.Mods.RA.Widgets { icons = new Dictionary(); if (CurrentQueue == null) + { + if (IconCount != 0) + { + OnIconCountChanged(IconCount, 0); + IconCount = 0; + } + return; + } var allBuildables = CurrentQueue.AllItems().OrderBy(a => a.Traits.Get().BuildPaletteOrder); - var i = 0; + + var oldIconCount = IconCount; + IconCount = 0; + var rb = RenderBounds; foreach (var item in allBuildables) { - var x = i % Columns; - var y = i / Columns; - var rect = new Rectangle(rb.X + x * 64 + 1, rb.Y + y * 48 + 1, 64, 48); + var x = IconCount % Columns; + var y = IconCount / Columns; + var rect = new Rectangle(rb.X + x * IconSize.X + 1, rb.Y + y * IconSize.Y + 1, IconSize.X, IconSize.Y); var icon = new Animation(World, RenderSimple.GetImage(item)); icon.Play(item.Traits.Get().Icon); + var pi = new ProductionIcon() { Name = item.Name, @@ -195,17 +212,20 @@ namespace OpenRA.Mods.RA.Widgets Pos = new float2(rect.Location), Queued = CurrentQueue.AllQueued().Where(a => a.Item == item.Name).ToList(), }; + icons.Add(rect, pi); - i++; + IconCount++; } eventBounds = icons.Any() ? icons.Keys.Aggregate(Rectangle.Union) : Rectangle.Empty; + + if (oldIconCount != IconCount) + OnIconCountChanged(oldIconCount, IconCount); } public override void Draw() { - var iconSize = new float2(64, 48); - var iconOffset = 0.5f * iconSize; + var iconOffset = 0.5f * IconSize.ToFloat2(); overlayFont = Game.Renderer.Fonts["TinyBold"]; timeOffset = iconOffset - overlayFont.Measure(WidgetUtils.FormatTime(0)) / 2; @@ -218,10 +238,6 @@ namespace OpenRA.Mods.RA.Widgets var buildableItems = CurrentQueue.BuildableItems(); - // Background - foreach (var rect in icons.Keys) - WidgetUtils.DrawPanel("panel-black", rect.InflateBy(1, 1, 1, 1)); - // Icons foreach (var icon in icons.Values) { diff --git a/OpenRA.Mods.RA/Widgets/ProductionTabsWidget.cs b/OpenRA.Mods.RA/Widgets/ProductionTabsWidget.cs index a605db5f90..98f881aa99 100644 --- a/OpenRA.Mods.RA/Widgets/ProductionTabsWidget.cs +++ b/OpenRA.Mods.RA/Widgets/ProductionTabsWidget.cs @@ -63,6 +63,7 @@ namespace OpenRA.Mods.RA.Widgets public readonly string PaletteWidget = null; public readonly string TypesContainer = null; + public readonly string BackgroundContainer = null; public readonly int TabWidth = 30; public readonly int ArrowWidth = 20; diff --git a/mods/cnc/chrome/ingame.yaml b/mods/cnc/chrome/ingame.yaml index 2b938e8feb..b81bcb853a 100644 --- a/mods/cnc/chrome/ingame.yaml +++ b/mods/cnc/chrome/ingame.yaml @@ -369,21 +369,29 @@ Container@PLAYER_WIDGETS: X: 7 Y: 7 ImageCollection: production-icons - ProductionTabs@PRODUCTION_TABS: - Logic: ProductionTabsLogic - PaletteWidget: PRODUCTION_PALETTE - TypesContainer: PRODUCTION_TYPES + Container@PRODUCTION_BACKGROUND: X: WINDOW_RIGHT - 204 - Y: 268 - Width: 194 - Height: 20 + Y: 287 + Children: + Background@ICON_TEMPLATE: + Width: 66 + Height: 50 + Background: panel-black ProductionPalette@PRODUCTION_PALETTE: X: WINDOW_RIGHT - 204 Y: 287 TooltipContainer: TOOLTIP_CONTAINER ReadyText: Ready HoldText: On Hold - + ProductionTabs@PRODUCTION_TABS: + Logic: ProductionTabsLogic + PaletteWidget: PRODUCTION_PALETTE + TypesContainer: PRODUCTION_TYPES + BackgroundContainer: PRODUCTION_BACKGROUND + X: WINDOW_RIGHT - 204 + Y: 268 + Width: 194 + Height: 20 Background@FMVPLAYER: Width: WINDOW_RIGHT Height: WINDOW_BOTTOM