diff --git a/OpenRA.Game/Graphics/ChromeProvider.cs b/OpenRA.Game/Graphics/ChromeProvider.cs index 82dc17213a14..a43b1b2b1535 100644 --- a/OpenRA.Game/Graphics/ChromeProvider.cs +++ b/OpenRA.Game/Graphics/ChromeProvider.cs @@ -144,6 +144,15 @@ static void LoadCollection(string name, MiniYaml yaml) } public static Sprite GetImage(string collectionName, string imageName) + { + var image = TryGetImage(collectionName, imageName); + if (image == null) + throw new ArgumentException($"Sprite `{collectionName}/{imageName}` was not found."); + + return image; + } + + public static Sprite TryGetImage(string collectionName, string imageName) { if (string.IsNullOrEmpty(collectionName)) return null; @@ -153,10 +162,7 @@ public static Sprite GetImage(string collectionName, string imageName) return sprite; if (!collections.TryGetValue(collectionName, out var collection)) - { - Log.Write("debug", "Could not find collection '{0}'", collectionName); return null; - } if (!collection.Regions.TryGetValue(imageName, out var mi)) return null; @@ -176,6 +182,15 @@ public static Sprite GetImage(string collectionName, string imageName) } public static Sprite[] GetPanelImages(string collectionName) + { + var panel = TryGetPanelImages(collectionName); + if (panel == null) + throw new ArgumentException($"Panel `{collectionName}` was not found."); + + return panel; + } + + public static Sprite[] TryGetPanelImages(string collectionName) { if (string.IsNullOrEmpty(collectionName)) return null; @@ -185,17 +200,14 @@ public static Sprite[] GetPanelImages(string collectionName) return cachedSprites; if (!collections.TryGetValue(collectionName, out var collection)) - { - Log.Write("debug", "Could not find collection '{0}'", collectionName); return null; - } Sprite[] sprites; if (collection.PanelRegion != null) { if (collection.PanelRegion.Length != 8) { - Log.Write("debug", "Collection '{0}' does not define a valid PanelRegion", collectionName); + Log.Write("debug", $"Collection '{collectionName}' does not define a valid PanelRegion"); return null; } @@ -222,18 +234,23 @@ public static Sprite[] GetPanelImages(string collectionName) } else { + // PERF: We don't need to search for images if there are no definitions. + // PERF: It's more efficient to send an empty array rather than an array of 9 nulls. + if (!collection.Regions.Any()) + return Array.Empty(); + // Support manual definitions for unusual dialog layouts sprites = new[] { - GetImage(collectionName, "corner-tl"), - GetImage(collectionName, "border-t"), - GetImage(collectionName, "corner-tr"), - GetImage(collectionName, "border-l"), - GetImage(collectionName, "background"), - GetImage(collectionName, "border-r"), - GetImage(collectionName, "corner-bl"), - GetImage(collectionName, "border-b"), - GetImage(collectionName, "corner-br") + TryGetImage(collectionName, "corner-tl"), + TryGetImage(collectionName, "border-t"), + TryGetImage(collectionName, "corner-tr"), + TryGetImage(collectionName, "border-l"), + TryGetImage(collectionName, "background"), + TryGetImage(collectionName, "border-r"), + TryGetImage(collectionName, "corner-bl"), + TryGetImage(collectionName, "border-b"), + TryGetImage(collectionName, "corner-br") }; } diff --git a/OpenRA.Mods.Common/Widgets/ImageWidget.cs b/OpenRA.Mods.Common/Widgets/ImageWidget.cs index 22b603a3b37f..3d347076e0a8 100644 --- a/OpenRA.Mods.Common/Widgets/ImageWidget.cs +++ b/OpenRA.Mods.Common/Widgets/ImageWidget.cs @@ -33,14 +33,7 @@ public class ImageWidget : Widget public Func GetTooltipText; readonly CachedTransform<(string, string), Sprite> getImageCache = new CachedTransform<(string, string), Sprite>( - ((string collection, string image) args) => - { - var sprite = ChromeProvider.GetImage(args.collection, args.image); - if (sprite == null) - throw new ArgumentException($"Sprite {args.collection}/{args.image} was not found."); - - return sprite; - }); + ((string collection, string image) args) => ChromeProvider.GetImage(args.collection, args.image)); public ImageWidget() { diff --git a/OpenRA.Mods.Common/Widgets/ScrollItemWidget.cs b/OpenRA.Mods.Common/Widgets/ScrollItemWidget.cs index ce0d21f77d69..74f897174772 100644 --- a/OpenRA.Mods.Common/Widgets/ScrollItemWidget.cs +++ b/OpenRA.Mods.Common/Widgets/ScrollItemWidget.cs @@ -64,10 +64,7 @@ public override void Draw() if (!IgnoreChildMouseOver && !hover) hover = Children.Contains(Ui.MouseOverWidget); - var panel = getPanelCache.Update((IsDisabled(), Depressed, hover, false, IsSelected() || IsHighlighted())); - - if (panel != null) - WidgetUtils.DrawPanel(RenderBounds, panel); + WidgetUtils.DrawPanel(RenderBounds, getPanelCache.Update((IsDisabled(), Depressed, hover, false, IsSelected() || IsHighlighted()))); } public override Widget Clone() { return new ScrollItemWidget(this); } diff --git a/OpenRA.Mods.Common/Widgets/WidgetUtils.cs b/OpenRA.Mods.Common/Widgets/WidgetUtils.cs index b3ef1a473236..0bc4e8e51259 100644 --- a/OpenRA.Mods.Common/Widgets/WidgetUtils.cs +++ b/OpenRA.Mods.Common/Widgets/WidgetUtils.cs @@ -38,7 +38,7 @@ public static CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Foc { var collectionName = collection + (args.Highlighted ? "-highlighted" : ""); var variantImageName = GetStatefulImageName(imageName, args.Disabled, args.Pressed, args.Hover, args.Focused); - return ChromeProvider.GetImage(collectionName, variantImageName) ?? ChromeProvider.GetImage(collectionName, imageName); + return ChromeProvider.TryGetImage(collectionName, variantImageName) ?? ChromeProvider.GetImage(collectionName, imageName); }); } @@ -50,7 +50,7 @@ public static CachedTransform<(bool Disabled, bool Pressed, bool Hover, bool Foc { var collectionName = collection + (args.Highlighted ? "-highlighted" : ""); var variantCollectionName = GetStatefulImageName(collectionName, args.Disabled, args.Pressed, args.Hover, args.Focused); - return ChromeProvider.GetPanelImages(variantCollectionName) ?? ChromeProvider.GetPanelImages(collectionName); + return ChromeProvider.TryGetPanelImages(variantCollectionName) ?? ChromeProvider.GetPanelImages(collectionName); }); } @@ -78,7 +78,7 @@ public static void DrawSpriteCentered(Sprite s, PaletteReference p, float2 pos, public static void DrawPanel(string collection, Rectangle bounds) { - var sprites = ChromeProvider.GetPanelImages(collection); + var sprites = ChromeProvider.TryGetPanelImages(collection); if (sprites != null) DrawPanel(bounds, sprites); }