From 1bafd35d61f3ccadce64f00768fb7b502b3ac6e6 Mon Sep 17 00:00:00 2001 From: Paul Chote Date: Sun, 17 Jul 2011 15:04:33 +1200 Subject: [PATCH] Rework ProductionTabsWidget getting/setting of the current queue. --- .../ProductionQueueFromSelection.cs | 12 +-- .../Widgets/ProductionTabsWidget.cs | 84 ++++++++++--------- 2 files changed, 52 insertions(+), 44 deletions(-) diff --git a/OpenRA.Mods.Cnc/ProductionQueueFromSelection.cs b/OpenRA.Mods.Cnc/ProductionQueueFromSelection.cs index b239d541a2..2e21d6b584 100644 --- a/OpenRA.Mods.Cnc/ProductionQueueFromSelection.cs +++ b/OpenRA.Mods.Cnc/ProductionQueueFromSelection.cs @@ -9,6 +9,7 @@ #endregion using System.Linq; +using OpenRA.FileFormats; using OpenRA.Traits; using OpenRA.Widgets; using OpenRA.Mods.RA; @@ -23,13 +24,15 @@ namespace OpenRA.Mods.Cnc.Widgets class ProductionQueueFromSelection : INotifySelection { - ProductionQueueFromSelectionInfo info; + Lazy tabsWidget; readonly World world; public ProductionQueueFromSelection(World world, ProductionQueueFromSelectionInfo info) { this.world = world; - this.info = info; + + tabsWidget = new Lazy(() => + Widget.RootWidget.GetWidget(info.ProductionTabsWidget)); } public void SelectionChanged() @@ -38,9 +41,8 @@ namespace OpenRA.Mods.Cnc.Widgets var producer = world.Selection.Actors.FirstOrDefault(a => a.IsInWorld && a.World.LocalPlayer == a.Owner && a.HasTrait()); - if (producer != null) - Widget.RootWidget.GetWidget(info.ProductionTabsWidget) - .SelectQueue(producer.TraitsImplementing().First()); + if (producer != null) + tabsWidget.Value.CurrentQueue = producer.TraitsImplementing().First(); } } } diff --git a/OpenRA.Mods.Cnc/Widgets/ProductionTabsWidget.cs b/OpenRA.Mods.Cnc/Widgets/ProductionTabsWidget.cs index 48db584622..4a87588b5c 100755 --- a/OpenRA.Mods.Cnc/Widgets/ProductionTabsWidget.cs +++ b/OpenRA.Mods.Cnc/Widgets/ProductionTabsWidget.cs @@ -60,22 +60,6 @@ namespace OpenRA.Mods.Cnc.Widgets class ProductionTabsWidget : Widget { - string queueGroup; - public string QueueGroup - { - get - { - return queueGroup; - } - set - { - queueGroup = value; - ListOffset = 0; - Widget.RootWidget.GetWidget(PaletteWidget) - .CurrentQueue = Groups[queueGroup].Tabs[0].Queue; - } - } - public readonly string PaletteWidget = null; public readonly string ClickSound = null; public readonly string DisabledClickSound = null; @@ -90,19 +74,52 @@ namespace OpenRA.Mods.Cnc.Widgets bool rightPressed = false; Rectangle leftButtonRect; Rectangle rightButtonRect; - readonly World world; + Lazy paletteWidget; + string queueGroup; [ObjectCreator.UseCtor] public ProductionTabsWidget([ObjectCreator.Param] World world) { - this.world = world; Groups = Rules.Info.Values.SelectMany(a => a.Traits.WithInterface()) .Select(q => q.Group).Distinct().ToDictionary(g => g, g => new ProductionTabGroup() { Group = g }); // Only visible if the production palette has icons to display IsVisible = () => queueGroup != null && Groups[queueGroup].Tabs.Count > 0; + + paletteWidget = new Lazy(() => + Widget.RootWidget.GetWidget(PaletteWidget)); + } + + public string QueueGroup + { + get + { + return queueGroup; + } + set + { + ListOffset = 0; + + // TODO: prioritize alerted tabs + CurrentQueue = Groups[value].Tabs[0].Queue; + } } + public ProductionQueue CurrentQueue + { + get + { + return paletteWidget.Value.CurrentQueue; + } + set + { + paletteWidget.Value.CurrentQueue = value; + queueGroup = value != null ? value.Info.Group : null; + + // TODO: Scroll tabs so selected queue is visible + } + } + public override void Draw() { var rb = RenderBounds; @@ -125,7 +142,6 @@ namespace OpenRA.Mods.Cnc.Widgets // Draw tab buttons Game.Renderer.EnableScissor(leftButtonRect.Right, rb.Y + 1, rightButtonRect.Left - leftButtonRect.Right - 1, rb.Height); - var palette = Widget.RootWidget.GetWidget(PaletteWidget); var origin = new int2(leftButtonRect.Right - 1 + (int)ListOffset, leftButtonRect.Y); SpriteFont font = Game.Renderer.Fonts["TinyBold"]; ContentWidth = 0; @@ -134,7 +150,7 @@ namespace OpenRA.Mods.Cnc.Widgets { var rect = new Rectangle(origin.X + ContentWidth, origin.Y, TabWidth, rb.Height); var hover = !leftHover && !rightHover && Widget.MouseOverWidget == this && rect.Contains(Viewport.LastMousePos); - var baseName = tab.Queue == palette.CurrentQueue ? "button-toggled" : "button"; + var baseName = tab.Queue == CurrentQueue ? "button-toggled" : "button"; ButtonWidget.DrawBackground(baseName, rect, false, false, hover); ContentWidth += TabWidth - 1; @@ -157,8 +173,8 @@ namespace OpenRA.Mods.Cnc.Widgets { if (a.HasTrait()) { - var allQueues = world.ActorsWithTrait() - .Where(p => p.Actor.Owner == world.LocalPlayer && p.Actor.IsInWorld) + var allQueues = a.World.ActorsWithTrait() + .Where(p => p.Actor.Owner == p.Actor.World.LocalPlayer && p.Actor.IsInWorld) .Select(p => p.Trait).ToArray(); foreach (var g in Groups.Values) g.Update(allQueues); @@ -166,19 +182,18 @@ namespace OpenRA.Mods.Cnc.Widgets if (queueGroup == null) return; - var palette = Widget.RootWidget.GetWidget(PaletteWidget); // Queue destroyed, was last of type: switch to a new group if (Groups[queueGroup].Tabs.Count == 0) { // Find a new group - queueGroup = Groups.Where(g => g.Value.Tabs.Count > 0) + var qg = Groups.Where(g => g.Value.Tabs.Count > 0) .Select(g => g.Key).FirstOrDefault(); - palette.CurrentQueue = queueGroup != null ? Groups[queueGroup].Tabs[0].Queue : null; + CurrentQueue = qg != null ? Groups[qg].Tabs[0].Queue : null; } // Queue destroyed, others of same type: switch to another tab - else if (!Groups[queueGroup].Tabs.Select(t => t.Queue).Contains(palette.CurrentQueue)) - palette.CurrentQueue = Groups[queueGroup].Tabs[0].Queue; + else if (!Groups[queueGroup].Tabs.Select(t => t.Queue).Contains(CurrentQueue)) + CurrentQueue = Groups[queueGroup].Tabs[0].Queue; } } @@ -237,8 +252,7 @@ namespace OpenRA.Mods.Cnc.Widgets var offsetloc = mi.Location - new int2(leftButtonRect.Right - 1 + (int)ListOffset, leftButtonRect.Y); if (offsetloc.X > 0 && offsetloc.X < ContentWidth) { - var palette = Widget.RootWidget.GetWidget(PaletteWidget); - palette.CurrentQueue = Groups[queueGroup].Tabs[offsetloc.X/(TabWidth - 1)].Queue; + CurrentQueue = Groups[queueGroup].Tabs[offsetloc.X/(TabWidth - 1)].Queue; Sound.Play(ClickSound); return true; } @@ -264,21 +278,13 @@ namespace OpenRA.Mods.Cnc.Widgets if (queueGroup == null) return; - var pal = Widget.RootWidget.GetWidget(PaletteWidget); var tabs = Groups[queueGroup].Tabs.ToList(); if (reverse) tabs.Reverse(); - var tab = tabs.SkipWhile(q => q.Queue != pal.CurrentQueue) + var tab = tabs.SkipWhile(q => q.Queue != CurrentQueue) .Skip(1).FirstOrDefault() ?? tabs.FirstOrDefault(); - pal.CurrentQueue = tab != null ? tab.Queue : null; - } - - public void SelectQueue(ProductionQueue queue) - { - queueGroup = queue.Info.Group; - Widget.RootWidget.GetWidget(PaletteWidget) - .CurrentQueue = queue; + CurrentQueue = tab != null ? tab.Queue : null; } } }