Rework chrome.yaml format and panel rendering.
This commit is contained in:
215
OpenRA.Mods.Common/UpdateRules/Rules/ReformatChromeProvider.cs
Normal file
215
OpenRA.Mods.Common/UpdateRules/Rules/ReformatChromeProvider.cs
Normal file
@@ -0,0 +1,215 @@
|
||||
#region Copyright & License Information
|
||||
/*
|
||||
* Copyright 2007-2019 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, either version 3 of
|
||||
* the License, or (at your option) any later version. For more
|
||||
* information, see COPYING.
|
||||
*/
|
||||
#endregion
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using OpenRA.Graphics;
|
||||
using OpenRA.Mods.Common.Widgets;
|
||||
|
||||
namespace OpenRA.Mods.Common.UpdateRules.Rules
|
||||
{
|
||||
public class ReformatChromeProvider : UpdateRule
|
||||
{
|
||||
public override string Name { get { return "Reformat UI image definitions."; } }
|
||||
|
||||
public override string Description
|
||||
{
|
||||
get
|
||||
{
|
||||
return "The format of the chrome.yaml file defining image regions for the UI has\n" +
|
||||
"changed to support additional metadata fields. ";
|
||||
}
|
||||
}
|
||||
|
||||
readonly List<string> overrideLocations = new List<string>();
|
||||
readonly List<string> panelLocations = new List<string>();
|
||||
|
||||
public override IEnumerable<string> AfterUpdate(ModData modData)
|
||||
{
|
||||
if (overrideLocations.Any())
|
||||
yield return "Region-specific image overrides are no longer supported. The following definitions must be replaced:\n" +
|
||||
UpdateUtils.FormatMessageList(overrideLocations);
|
||||
|
||||
if (panelLocations.Any())
|
||||
yield return "The following definitions appear to be panels, but could not be converted to the new PanelRegion format.\n" +
|
||||
"You may wish to define PanelRegion/PanelSides manually to reduce duplication:\n" +
|
||||
UpdateUtils.FormatMessageList(panelLocations);
|
||||
|
||||
overrideLocations.Clear();
|
||||
panelLocations.Clear();
|
||||
}
|
||||
|
||||
readonly string[] edgeKeys =
|
||||
{
|
||||
"corner-tl", "corner-tr", "corner-bl", "corner-br",
|
||||
"border-t", "border-b", "border-l", "border-r"
|
||||
};
|
||||
|
||||
bool ExtractPanelDefinition(MiniYamlNode chromeProviderNode, MiniYamlNode regionsNode)
|
||||
{
|
||||
var cNode = regionsNode.LastChildMatching("background");
|
||||
var hasCenter = cNode != null;
|
||||
var hasEdges = edgeKeys.Any(k => regionsNode.LastChildMatching(k) != null);
|
||||
|
||||
// Not a panel
|
||||
if (!hasCenter && !hasEdges)
|
||||
return true;
|
||||
|
||||
// Panels may define just the background
|
||||
if (hasCenter && !hasEdges)
|
||||
{
|
||||
var bgRect = cNode.NodeValue<Rectangle>();
|
||||
chromeProviderNode.AddNode("PanelRegion", new[]
|
||||
{
|
||||
bgRect.X, bgRect.Y,
|
||||
0, 0,
|
||||
bgRect.Width, bgRect.Height,
|
||||
0, 0
|
||||
});
|
||||
|
||||
chromeProviderNode.AddNode("PanelSides", PanelSides.Center);
|
||||
regionsNode.RemoveNode(cNode);
|
||||
return true;
|
||||
}
|
||||
|
||||
// Panels may define just the edges, or edges plus background
|
||||
var tlNode = regionsNode.LastChildMatching("corner-tl");
|
||||
if (tlNode == null)
|
||||
return false;
|
||||
|
||||
var tlRect = tlNode.NodeValue<Rectangle>();
|
||||
|
||||
var tNode = regionsNode.LastChildMatching("border-t");
|
||||
if (tNode == null)
|
||||
return false;
|
||||
|
||||
var tRect = tNode.NodeValue<Rectangle>();
|
||||
if (tRect.Left != tlRect.Right || tRect.Top != tlRect.Top || tRect.Bottom != tlRect.Bottom)
|
||||
return false;
|
||||
|
||||
var trNode = regionsNode.LastChildMatching("corner-tr");
|
||||
if (trNode == null)
|
||||
return false;
|
||||
|
||||
var trRect = trNode.NodeValue<Rectangle>();
|
||||
if (trRect.Left != tRect.Right || trRect.Top != tRect.Top || trRect.Bottom != tRect.Bottom)
|
||||
return false;
|
||||
|
||||
var lNode = regionsNode.LastChildMatching("border-l");
|
||||
if (lNode == null)
|
||||
return false;
|
||||
|
||||
var lRect = lNode.NodeValue<Rectangle>();
|
||||
if (lRect.Left != tlRect.Left || lRect.Top != tlRect.Bottom || lRect.Right != tlRect.Right)
|
||||
return false;
|
||||
|
||||
var rNode = regionsNode.LastChildMatching("border-r");
|
||||
if (rNode == null)
|
||||
return false;
|
||||
|
||||
var rRect = rNode.NodeValue<Rectangle>();
|
||||
if (rRect.Left != trRect.Left || rRect.Top != trRect.Bottom || rRect.Bottom != lRect.Bottom || rRect.Right != trRect.Right)
|
||||
return false;
|
||||
|
||||
var blNode = regionsNode.LastChildMatching("corner-bl");
|
||||
if (blNode == null)
|
||||
return false;
|
||||
|
||||
var blRect = blNode.NodeValue<Rectangle>();
|
||||
if (blRect.Left != lRect.Left || blRect.Top != lRect.Bottom || blRect.Right != lRect.Right)
|
||||
return false;
|
||||
|
||||
var bNode = regionsNode.LastChildMatching("border-b");
|
||||
if (bNode == null)
|
||||
return false;
|
||||
|
||||
var bRect = bNode.NodeValue<Rectangle>();
|
||||
if (bRect.Left != blRect.Right || bRect.Top != blRect.Top || bRect.Bottom != blRect.Bottom || bRect.Right != tRect.Right)
|
||||
return false;
|
||||
|
||||
var brNode = regionsNode.LastChildMatching("corner-br");
|
||||
if (brNode == null)
|
||||
return false;
|
||||
|
||||
var brRect = brNode.NodeValue<Rectangle>();
|
||||
if (brRect.Left != bRect.Right || brRect.Top != bRect.Top || brRect.Bottom != bRect.Bottom || brRect.Right != rRect.Right)
|
||||
return false;
|
||||
|
||||
// Background definition may be omitted
|
||||
if (hasCenter)
|
||||
{
|
||||
var bgRect = cNode.NodeValue<Rectangle>();
|
||||
if (bgRect.Left != lRect.Right || bgRect.Top != lRect.Top || bgRect.Bottom != lRect.Bottom || bgRect.Right != tRect.Right)
|
||||
return false;
|
||||
}
|
||||
|
||||
// Define the short-form panel region
|
||||
chromeProviderNode.AddNode("PanelRegion", new[]
|
||||
{
|
||||
tlRect.X, tlRect.Y,
|
||||
tlRect.Width, tlRect.Height,
|
||||
trRect.Left - tlRect.Right, blRect.Top - tlRect.Bottom,
|
||||
brRect.Width, brRect.Height
|
||||
});
|
||||
|
||||
if (!hasCenter)
|
||||
chromeProviderNode.AddNode("PanelSides", PanelSides.Edges);
|
||||
|
||||
// Remove the now redundant regions
|
||||
regionsNode.RemoveNode(tlNode);
|
||||
regionsNode.RemoveNode(tNode);
|
||||
regionsNode.RemoveNode(trNode);
|
||||
regionsNode.RemoveNode(lNode);
|
||||
regionsNode.RemoveNode(rNode);
|
||||
regionsNode.RemoveNode(blNode);
|
||||
regionsNode.RemoveNode(bNode);
|
||||
regionsNode.RemoveNode(brNode);
|
||||
|
||||
if (cNode != null)
|
||||
regionsNode.RemoveNode(cNode);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public override IEnumerable<string> UpdateChromeProviderNode(ModData modData, MiniYamlNode chromeProviderNode)
|
||||
{
|
||||
// Migrate image rectangles
|
||||
var regionsNode = new MiniYamlNode("Regions", "");
|
||||
foreach (var n in chromeProviderNode.Value.Nodes)
|
||||
{
|
||||
if (n.Key == "Inherits")
|
||||
continue;
|
||||
|
||||
// Reformat region as a list
|
||||
regionsNode.AddNode(n.Key, n.NodeValue<int[]>());
|
||||
|
||||
if (n.Value.Nodes.Any())
|
||||
overrideLocations.Add("{0}.{1} ({2})".F(chromeProviderNode.Key, n.Key, chromeProviderNode.Location.Filename));
|
||||
}
|
||||
|
||||
chromeProviderNode.Value.Nodes.RemoveAll(n => n.Key != "Inherits");
|
||||
|
||||
// Migrate image definition
|
||||
chromeProviderNode.AddNode(new MiniYamlNode("Image", chromeProviderNode.Value.Value));
|
||||
chromeProviderNode.Value.Value = "";
|
||||
|
||||
if (!ExtractPanelDefinition(chromeProviderNode, regionsNode))
|
||||
panelLocations.Add("{0} ({1})".F(chromeProviderNode.Key, chromeProviderNode.Location.Filename));
|
||||
|
||||
if (regionsNode.Value.Nodes.Any())
|
||||
chromeProviderNode.AddNode(regionsNode);
|
||||
|
||||
yield break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -30,6 +30,7 @@ namespace OpenRA.Mods.Common.UpdateRules
|
||||
public virtual IEnumerable<string> UpdateWeaponNode(ModData modData, MiniYamlNode weaponNode) { yield break; }
|
||||
public virtual IEnumerable<string> UpdateChromeNode(ModData modData, MiniYamlNode chromeNode) { yield break; }
|
||||
public virtual IEnumerable<string> UpdateTilesetNode(ModData modData, MiniYamlNode tilesetNode) { yield break; }
|
||||
public virtual IEnumerable<string> UpdateChromeProviderNode(ModData modData, MiniYamlNode chromeProviderNode) { yield break; }
|
||||
|
||||
public virtual IEnumerable<string> BeforeUpdate(ModData modData) { yield break; }
|
||||
public virtual IEnumerable<string> AfterUpdate(ModData modData) { yield break; }
|
||||
|
||||
@@ -145,6 +145,7 @@ namespace OpenRA.Mods.Common.UpdateRules
|
||||
var modWeapons = LoadModYaml(modData, FilterExternalModFiles(modData, modData.Manifest.Weapons, externalFilenames));
|
||||
var modTilesets = LoadModYaml(modData, FilterExternalModFiles(modData, modData.Manifest.TileSets, externalFilenames));
|
||||
var modChromeLayout = LoadModYaml(modData, FilterExternalModFiles(modData, modData.Manifest.ChromeLayout, externalFilenames));
|
||||
var modChromeProvider = LoadModYaml(modData, FilterExternalModFiles(modData, modData.Manifest.Chrome, externalFilenames));
|
||||
|
||||
// Find and add shared map includes
|
||||
foreach (var package in modData.MapCache.EnumerateMapPackagesWithoutCaching())
|
||||
@@ -174,12 +175,14 @@ namespace OpenRA.Mods.Common.UpdateRules
|
||||
manualSteps.AddRange(ApplyTopLevelTransform(modData, modWeapons, rule.UpdateWeaponNode));
|
||||
manualSteps.AddRange(ApplyTopLevelTransform(modData, modTilesets, rule.UpdateTilesetNode));
|
||||
manualSteps.AddRange(ApplyChromeTransform(modData, modChromeLayout, rule.UpdateChromeNode));
|
||||
manualSteps.AddRange(ApplyTopLevelTransform(modData, modChromeProvider, rule.UpdateChromeProviderNode));
|
||||
manualSteps.AddRange(rule.AfterUpdate(modData));
|
||||
|
||||
files = modRules.ToList();
|
||||
files.AddRange(modWeapons);
|
||||
files.AddRange(modTilesets);
|
||||
files.AddRange(modChromeLayout);
|
||||
files.AddRange(modChromeProvider);
|
||||
|
||||
return manualSteps;
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
var percentage = GetPercentage();
|
||||
WidgetUtils.DrawPanel(Background, rb);
|
||||
|
||||
var minBarWidth = (int)(ChromeProvider.GetImage(Bar, "border-l").Size.X + ChromeProvider.GetImage(Bar, "border-r").Size.X);
|
||||
var minBarWidth = ChromeProvider.GetMinimumPanelSize(Bar).Width;
|
||||
var maxBarWidth = rb.Width - BarMargin.Width * 2;
|
||||
var barWidth = wasIndeterminate ? maxBarWidth / 4 : percentage * maxBarWidth / 100;
|
||||
barWidth = Math.Max(barWidth, minBarWidth);
|
||||
|
||||
@@ -40,7 +40,9 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
|
||||
public static void DrawPanel(string collection, Rectangle bounds)
|
||||
{
|
||||
DrawPanelPartial(collection, bounds, PanelSides.All);
|
||||
var sprites = ChromeProvider.GetPanelImages(collection);
|
||||
if (sprites != null)
|
||||
DrawPanel(bounds, sprites);
|
||||
}
|
||||
|
||||
public static void FillRectWithSprite(Rectangle r, Sprite s)
|
||||
@@ -89,95 +91,70 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
Game.Renderer.RgbaColorRenderer.FillEllipse(tl, br, c);
|
||||
}
|
||||
|
||||
public static int[] GetBorderSizes(string collection)
|
||||
{
|
||||
var images = new[] { "border-t", "border-b", "border-l", "border-r" };
|
||||
var ss = images.Select(i => ChromeProvider.GetImage(collection, i)).ToList();
|
||||
return new[] { (int)ss[0].Size.Y, (int)ss[1].Size.Y, (int)ss[2].Size.X, (int)ss[3].Size.X };
|
||||
}
|
||||
|
||||
static bool HasFlags(this PanelSides a, PanelSides b)
|
||||
{
|
||||
// PERF: Enum.HasFlag is slower and requires allocations.
|
||||
return (a & b) == b;
|
||||
}
|
||||
|
||||
public static Rectangle InflateBy(this Rectangle rect, int l, int t, int r, int b)
|
||||
{
|
||||
return Rectangle.FromLTRB(rect.Left - l, rect.Top - t,
|
||||
rect.Right + r, rect.Bottom + b);
|
||||
}
|
||||
|
||||
public static void DrawPanelPartial(string collection, Rectangle bounds, PanelSides ps)
|
||||
/// <summary>
|
||||
/// Fill a rectangle with sprites defining a panel layout.
|
||||
/// Draw order is center, borders, corners to allow mods to define fancy border and corner overlays.
|
||||
/// </summary>
|
||||
/// <param name="bounds">Rectangle to fill.</param>
|
||||
/// <param name="sprites">Nine sprites defining the panel: TL, T, TR, L, C, R, BL, B, BR.</param>
|
||||
public static void DrawPanel(Rectangle bounds, Sprite[] sprites)
|
||||
{
|
||||
DrawPanelPartial(bounds, ps,
|
||||
ChromeProvider.GetImage(collection, "border-t"),
|
||||
ChromeProvider.GetImage(collection, "border-b"),
|
||||
ChromeProvider.GetImage(collection, "border-l"),
|
||||
ChromeProvider.GetImage(collection, "border-r"),
|
||||
ChromeProvider.GetImage(collection, "corner-tl"),
|
||||
ChromeProvider.GetImage(collection, "corner-tr"),
|
||||
ChromeProvider.GetImage(collection, "corner-bl"),
|
||||
ChromeProvider.GetImage(collection, "corner-br"),
|
||||
ChromeProvider.GetImage(collection, "background"));
|
||||
}
|
||||
if (sprites.Length != 9)
|
||||
return;
|
||||
|
||||
public static void DrawPanelPartial(Rectangle bounds, PanelSides ps,
|
||||
Sprite borderTop,
|
||||
Sprite borderBottom,
|
||||
Sprite borderLeft,
|
||||
Sprite borderRight,
|
||||
Sprite cornerTopLeft,
|
||||
Sprite cornerTopRight,
|
||||
Sprite cornerBottomLeft,
|
||||
Sprite cornerBottomRight,
|
||||
Sprite background)
|
||||
{
|
||||
var marginLeft = borderLeft == null ? 0 : (int)borderLeft.Size.X;
|
||||
var marginTop = borderTop == null ? 0 : (int)borderTop.Size.Y;
|
||||
var marginRight = borderRight == null ? 0 : (int)borderRight.Size.X;
|
||||
var marginBottom = borderBottom == null ? 0 : (int)borderBottom.Size.Y;
|
||||
var marginTop = sprites[1] == null ? 0 : (int)sprites[1].Size.Y;
|
||||
var marginLeft = sprites[3] == null ? 0 : (int)sprites[3].Size.X;
|
||||
var marginRight = sprites[5] == null ? 0 : (int)sprites[5].Size.X;
|
||||
var marginBottom = sprites[7] == null ? 0 : (int)sprites[7].Size.Y;
|
||||
var marginWidth = marginRight + marginLeft;
|
||||
var marginHeight = marginBottom + marginTop;
|
||||
|
||||
// Background
|
||||
if (ps.HasFlags(PanelSides.Center) && background != null)
|
||||
// Center
|
||||
if (sprites[4] != null)
|
||||
FillRectWithSprite(new Rectangle(bounds.Left + marginLeft, bounds.Top + marginTop,
|
||||
bounds.Width - marginWidth, bounds.Height - marginHeight),
|
||||
background);
|
||||
bounds.Width - marginWidth, bounds.Height - marginHeight), sprites[4]);
|
||||
|
||||
// Left border
|
||||
if (ps.HasFlags(PanelSides.Left) && borderLeft != null)
|
||||
// Left edge
|
||||
if (sprites[3] != null)
|
||||
FillRectWithSprite(new Rectangle(bounds.Left, bounds.Top + marginTop,
|
||||
marginLeft, bounds.Height - marginHeight),
|
||||
borderLeft);
|
||||
marginLeft, bounds.Height - marginHeight), sprites[3]);
|
||||
|
||||
// Right border
|
||||
if (ps.HasFlags(PanelSides.Right) && borderRight != null)
|
||||
// Right edge
|
||||
if (sprites[5] != null)
|
||||
FillRectWithSprite(new Rectangle(bounds.Right - marginRight, bounds.Top + marginTop,
|
||||
marginLeft, bounds.Height - marginHeight),
|
||||
borderRight);
|
||||
marginLeft, bounds.Height - marginHeight), sprites[5]);
|
||||
|
||||
// Top border
|
||||
if (ps.HasFlags(PanelSides.Top) && borderTop != null)
|
||||
// Top edge
|
||||
if (sprites[1] != null)
|
||||
FillRectWithSprite(new Rectangle(bounds.Left + marginLeft, bounds.Top,
|
||||
bounds.Width - marginWidth, marginTop),
|
||||
borderTop);
|
||||
bounds.Width - marginWidth, marginTop), sprites[1]);
|
||||
|
||||
// Bottom border
|
||||
if (ps.HasFlags(PanelSides.Bottom) && borderBottom != null)
|
||||
// Bottom edge
|
||||
if (sprites[7] != null)
|
||||
FillRectWithSprite(new Rectangle(bounds.Left + marginLeft, bounds.Bottom - marginBottom,
|
||||
bounds.Width - marginWidth, marginTop),
|
||||
borderBottom);
|
||||
bounds.Width - marginWidth, marginTop), sprites[7]);
|
||||
|
||||
if (ps.HasFlags(PanelSides.Left | PanelSides.Top) && cornerTopLeft != null)
|
||||
DrawRGBA(cornerTopLeft, new float2(bounds.Left, bounds.Top));
|
||||
if (ps.HasFlags(PanelSides.Right | PanelSides.Top) && cornerTopRight != null)
|
||||
DrawRGBA(cornerTopRight, new float2(bounds.Right - cornerTopRight.Size.X, bounds.Top));
|
||||
if (ps.HasFlags(PanelSides.Left | PanelSides.Bottom) && cornerBottomLeft != null)
|
||||
DrawRGBA(cornerBottomLeft, new float2(bounds.Left, bounds.Bottom - cornerBottomLeft.Size.Y));
|
||||
if (ps.HasFlags(PanelSides.Right | PanelSides.Bottom) && cornerBottomRight != null)
|
||||
DrawRGBA(cornerBottomRight, new float2(bounds.Right - cornerBottomRight.Size.X, bounds.Bottom - cornerBottomRight.Size.Y));
|
||||
// Top-left corner
|
||||
if (sprites[0] != null)
|
||||
DrawRGBA(sprites[0], new float2(bounds.Left, bounds.Top));
|
||||
|
||||
// Top-right corner
|
||||
if (sprites[2] != null)
|
||||
DrawRGBA(sprites[2], new float2(bounds.Right - sprites[2].Size.X, bounds.Top));
|
||||
|
||||
// Bottom-left corner
|
||||
if (sprites[6] != null)
|
||||
DrawRGBA(sprites[6], new float2(bounds.Left, bounds.Bottom - sprites[6].Size.Y));
|
||||
|
||||
// Bottom-right corner
|
||||
if (sprites[8] != null)
|
||||
DrawRGBA(sprites[8], new float2(bounds.Right - sprites[8].Size.X, bounds.Bottom - sprites[8].Size.Y));
|
||||
}
|
||||
|
||||
public static string FormatTime(int ticks, int timestep)
|
||||
@@ -315,17 +292,4 @@ namespace OpenRA.Mods.Common.Widgets
|
||||
return lastOutput;
|
||||
}
|
||||
}
|
||||
|
||||
[Flags]
|
||||
public enum PanelSides
|
||||
{
|
||||
Left = 1,
|
||||
Top = 2,
|
||||
Right = 4,
|
||||
Bottom = 8,
|
||||
Center = 16,
|
||||
|
||||
Edges = Left | Top | Right | Bottom,
|
||||
All = Edges | Center,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user