Extract ProductionPalette background drawing into logic code.

This commit is contained in:
Paul Chote
2014-07-12 21:37:06 +12:00
parent 54b5da3460
commit 2de9d3780b
4 changed files with 72 additions and 19 deletions

View File

@@ -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<ProductionPaletteWidget>(tabs.PaletteWidget);
var icontemplate = background.Get("ICON_TEMPLATE");
Action<int, int> 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()

View File

@@ -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<int, int> 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<Rectangle, ProductionIcon>();
if (CurrentQueue == null)
{
if (IconCount != 0)
{
OnIconCountChanged(IconCount, 0);
IconCount = 0;
}
return;
}
var allBuildables = CurrentQueue.AllItems().OrderBy(a => a.Traits.Get<BuildableInfo>().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<TooltipInfo>().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)
{

View File

@@ -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;

View File

@@ -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