Rework ProductionTabsWidget getting/setting of the current queue.

This commit is contained in:
Paul Chote
2011-07-17 15:04:33 +12:00
parent 9fb72c9063
commit 1bafd35d61
2 changed files with 52 additions and 44 deletions

View File

@@ -9,6 +9,7 @@
#endregion #endregion
using System.Linq; using System.Linq;
using OpenRA.FileFormats;
using OpenRA.Traits; using OpenRA.Traits;
using OpenRA.Widgets; using OpenRA.Widgets;
using OpenRA.Mods.RA; using OpenRA.Mods.RA;
@@ -23,13 +24,15 @@ namespace OpenRA.Mods.Cnc.Widgets
class ProductionQueueFromSelection : INotifySelection class ProductionQueueFromSelection : INotifySelection
{ {
ProductionQueueFromSelectionInfo info; Lazy<ProductionTabsWidget> tabsWidget;
readonly World world; readonly World world;
public ProductionQueueFromSelection(World world, ProductionQueueFromSelectionInfo info) public ProductionQueueFromSelection(World world, ProductionQueueFromSelectionInfo info)
{ {
this.world = world; this.world = world;
this.info = info;
tabsWidget = new Lazy<ProductionTabsWidget>(() =>
Widget.RootWidget.GetWidget<ProductionTabsWidget>(info.ProductionTabsWidget));
} }
public void SelectionChanged() public void SelectionChanged()
@@ -39,8 +42,7 @@ namespace OpenRA.Mods.Cnc.Widgets
&& a.World.LocalPlayer == a.Owner && a.World.LocalPlayer == a.Owner
&& a.HasTrait<ProductionQueue>()); && a.HasTrait<ProductionQueue>());
if (producer != null) if (producer != null)
Widget.RootWidget.GetWidget<ProductionTabsWidget>(info.ProductionTabsWidget) tabsWidget.Value.CurrentQueue = producer.TraitsImplementing<ProductionQueue>().First();
.SelectQueue(producer.TraitsImplementing<ProductionQueue>().First());
} }
} }
} }

View File

@@ -60,22 +60,6 @@ namespace OpenRA.Mods.Cnc.Widgets
class ProductionTabsWidget : Widget class ProductionTabsWidget : Widget
{ {
string queueGroup;
public string QueueGroup
{
get
{
return queueGroup;
}
set
{
queueGroup = value;
ListOffset = 0;
Widget.RootWidget.GetWidget<ProductionPaletteWidget>(PaletteWidget)
.CurrentQueue = Groups[queueGroup].Tabs[0].Queue;
}
}
public readonly string PaletteWidget = null; public readonly string PaletteWidget = null;
public readonly string ClickSound = null; public readonly string ClickSound = null;
public readonly string DisabledClickSound = null; public readonly string DisabledClickSound = null;
@@ -90,17 +74,50 @@ namespace OpenRA.Mods.Cnc.Widgets
bool rightPressed = false; bool rightPressed = false;
Rectangle leftButtonRect; Rectangle leftButtonRect;
Rectangle rightButtonRect; Rectangle rightButtonRect;
readonly World world; Lazy<ProductionPaletteWidget> paletteWidget;
string queueGroup;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
public ProductionTabsWidget([ObjectCreator.Param] World world) public ProductionTabsWidget([ObjectCreator.Param] World world)
{ {
this.world = world;
Groups = Rules.Info.Values.SelectMany(a => a.Traits.WithInterface<ProductionQueueInfo>()) Groups = Rules.Info.Values.SelectMany(a => a.Traits.WithInterface<ProductionQueueInfo>())
.Select(q => q.Group).Distinct().ToDictionary(g => g, g => new ProductionTabGroup() { Group = g }); .Select(q => q.Group).Distinct().ToDictionary(g => g, g => new ProductionTabGroup() { Group = g });
// Only visible if the production palette has icons to display // Only visible if the production palette has icons to display
IsVisible = () => queueGroup != null && Groups[queueGroup].Tabs.Count > 0; IsVisible = () => queueGroup != null && Groups[queueGroup].Tabs.Count > 0;
paletteWidget = new Lazy<ProductionPaletteWidget>(() =>
Widget.RootWidget.GetWidget<ProductionPaletteWidget>(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() public override void Draw()
@@ -125,7 +142,6 @@ namespace OpenRA.Mods.Cnc.Widgets
// Draw tab buttons // Draw tab buttons
Game.Renderer.EnableScissor(leftButtonRect.Right, rb.Y + 1, rightButtonRect.Left - leftButtonRect.Right - 1, rb.Height); Game.Renderer.EnableScissor(leftButtonRect.Right, rb.Y + 1, rightButtonRect.Left - leftButtonRect.Right - 1, rb.Height);
var palette = Widget.RootWidget.GetWidget<ProductionPaletteWidget>(PaletteWidget);
var origin = new int2(leftButtonRect.Right - 1 + (int)ListOffset, leftButtonRect.Y); var origin = new int2(leftButtonRect.Right - 1 + (int)ListOffset, leftButtonRect.Y);
SpriteFont font = Game.Renderer.Fonts["TinyBold"]; SpriteFont font = Game.Renderer.Fonts["TinyBold"];
ContentWidth = 0; ContentWidth = 0;
@@ -134,7 +150,7 @@ namespace OpenRA.Mods.Cnc.Widgets
{ {
var rect = new Rectangle(origin.X + ContentWidth, origin.Y, TabWidth, rb.Height); var rect = new Rectangle(origin.X + ContentWidth, origin.Y, TabWidth, rb.Height);
var hover = !leftHover && !rightHover && Widget.MouseOverWidget == this && rect.Contains(Viewport.LastMousePos); 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); ButtonWidget.DrawBackground(baseName, rect, false, false, hover);
ContentWidth += TabWidth - 1; ContentWidth += TabWidth - 1;
@@ -157,8 +173,8 @@ namespace OpenRA.Mods.Cnc.Widgets
{ {
if (a.HasTrait<ProductionQueue>()) if (a.HasTrait<ProductionQueue>())
{ {
var allQueues = world.ActorsWithTrait<ProductionQueue>() var allQueues = a.World.ActorsWithTrait<ProductionQueue>()
.Where(p => p.Actor.Owner == world.LocalPlayer && p.Actor.IsInWorld) .Where(p => p.Actor.Owner == p.Actor.World.LocalPlayer && p.Actor.IsInWorld)
.Select(p => p.Trait).ToArray(); .Select(p => p.Trait).ToArray();
foreach (var g in Groups.Values) foreach (var g in Groups.Values)
g.Update(allQueues); g.Update(allQueues);
@@ -166,19 +182,18 @@ namespace OpenRA.Mods.Cnc.Widgets
if (queueGroup == null) if (queueGroup == null)
return; return;
var palette = Widget.RootWidget.GetWidget<ProductionPaletteWidget>(PaletteWidget);
// Queue destroyed, was last of type: switch to a new group // Queue destroyed, was last of type: switch to a new group
if (Groups[queueGroup].Tabs.Count == 0) if (Groups[queueGroup].Tabs.Count == 0)
{ {
// Find a new group // 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(); .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 // Queue destroyed, others of same type: switch to another tab
else if (!Groups[queueGroup].Tabs.Select(t => t.Queue).Contains(palette.CurrentQueue)) else if (!Groups[queueGroup].Tabs.Select(t => t.Queue).Contains(CurrentQueue))
palette.CurrentQueue = Groups[queueGroup].Tabs[0].Queue; 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); var offsetloc = mi.Location - new int2(leftButtonRect.Right - 1 + (int)ListOffset, leftButtonRect.Y);
if (offsetloc.X > 0 && offsetloc.X < ContentWidth) if (offsetloc.X > 0 && offsetloc.X < ContentWidth)
{ {
var palette = Widget.RootWidget.GetWidget<ProductionPaletteWidget>(PaletteWidget); CurrentQueue = Groups[queueGroup].Tabs[offsetloc.X/(TabWidth - 1)].Queue;
palette.CurrentQueue = Groups[queueGroup].Tabs[offsetloc.X/(TabWidth - 1)].Queue;
Sound.Play(ClickSound); Sound.Play(ClickSound);
return true; return true;
} }
@@ -264,21 +278,13 @@ namespace OpenRA.Mods.Cnc.Widgets
if (queueGroup == null) if (queueGroup == null)
return; return;
var pal = Widget.RootWidget.GetWidget<ProductionPaletteWidget>(PaletteWidget);
var tabs = Groups[queueGroup].Tabs.ToList(); var tabs = Groups[queueGroup].Tabs.ToList();
if (reverse) tabs.Reverse(); 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(); .Skip(1).FirstOrDefault() ?? tabs.FirstOrDefault();
pal.CurrentQueue = tab != null ? tab.Queue : null; CurrentQueue = tab != null ? tab.Queue : null;
}
public void SelectQueue(ProductionQueue queue)
{
queueGroup = queue.Info.Group;
Widget.RootWidget.GetWidget<ProductionPaletteWidget>(PaletteWidget)
.CurrentQueue = queue;
} }
} }
} }