Crash on image/panel not found and add TryGet functions for searching
This commit is contained in:
@@ -144,6 +144,15 @@ namespace OpenRA.Graphics
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Sprite GetImage(string collectionName, string imageName)
|
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))
|
if (string.IsNullOrEmpty(collectionName))
|
||||||
return null;
|
return null;
|
||||||
@@ -153,10 +162,7 @@ namespace OpenRA.Graphics
|
|||||||
return sprite;
|
return sprite;
|
||||||
|
|
||||||
if (!collections.TryGetValue(collectionName, out var collection))
|
if (!collections.TryGetValue(collectionName, out var collection))
|
||||||
{
|
|
||||||
Log.Write("debug", "Could not find collection '{0}'", collectionName);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
|
|
||||||
if (!collection.Regions.TryGetValue(imageName, out var mi))
|
if (!collection.Regions.TryGetValue(imageName, out var mi))
|
||||||
return null;
|
return null;
|
||||||
@@ -176,6 +182,15 @@ namespace OpenRA.Graphics
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static Sprite[] GetPanelImages(string collectionName)
|
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))
|
if (string.IsNullOrEmpty(collectionName))
|
||||||
return null;
|
return null;
|
||||||
@@ -185,17 +200,14 @@ namespace OpenRA.Graphics
|
|||||||
return cachedSprites;
|
return cachedSprites;
|
||||||
|
|
||||||
if (!collections.TryGetValue(collectionName, out var collection))
|
if (!collections.TryGetValue(collectionName, out var collection))
|
||||||
{
|
|
||||||
Log.Write("debug", "Could not find collection '{0}'", collectionName);
|
|
||||||
return null;
|
return null;
|
||||||
}
|
|
||||||
|
|
||||||
Sprite[] sprites;
|
Sprite[] sprites;
|
||||||
if (collection.PanelRegion != null)
|
if (collection.PanelRegion != null)
|
||||||
{
|
{
|
||||||
if (collection.PanelRegion.Length != 8)
|
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;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -222,18 +234,23 @@ namespace OpenRA.Graphics
|
|||||||
}
|
}
|
||||||
else
|
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<Sprite>();
|
||||||
|
|
||||||
// Support manual definitions for unusual dialog layouts
|
// Support manual definitions for unusual dialog layouts
|
||||||
sprites = new[]
|
sprites = new[]
|
||||||
{
|
{
|
||||||
GetImage(collectionName, "corner-tl"),
|
TryGetImage(collectionName, "corner-tl"),
|
||||||
GetImage(collectionName, "border-t"),
|
TryGetImage(collectionName, "border-t"),
|
||||||
GetImage(collectionName, "corner-tr"),
|
TryGetImage(collectionName, "corner-tr"),
|
||||||
GetImage(collectionName, "border-l"),
|
TryGetImage(collectionName, "border-l"),
|
||||||
GetImage(collectionName, "background"),
|
TryGetImage(collectionName, "background"),
|
||||||
GetImage(collectionName, "border-r"),
|
TryGetImage(collectionName, "border-r"),
|
||||||
GetImage(collectionName, "corner-bl"),
|
TryGetImage(collectionName, "corner-bl"),
|
||||||
GetImage(collectionName, "border-b"),
|
TryGetImage(collectionName, "border-b"),
|
||||||
GetImage(collectionName, "corner-br")
|
TryGetImage(collectionName, "corner-br")
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,14 +33,7 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
public Func<string> GetTooltipText;
|
public Func<string> GetTooltipText;
|
||||||
|
|
||||||
readonly CachedTransform<(string, string), Sprite> getImageCache = new CachedTransform<(string, string), Sprite>(
|
readonly CachedTransform<(string, string), Sprite> getImageCache = new CachedTransform<(string, string), Sprite>(
|
||||||
((string collection, string image) args) =>
|
((string collection, string image) args) => ChromeProvider.GetImage(args.collection, args.image));
|
||||||
{
|
|
||||||
var sprite = ChromeProvider.GetImage(args.collection, args.image);
|
|
||||||
if (sprite == null)
|
|
||||||
throw new ArgumentException($"Sprite {args.collection}/{args.image} was not found.");
|
|
||||||
|
|
||||||
return sprite;
|
|
||||||
});
|
|
||||||
|
|
||||||
public ImageWidget()
|
public ImageWidget()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -64,10 +64,7 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
if (!IgnoreChildMouseOver && !hover)
|
if (!IgnoreChildMouseOver && !hover)
|
||||||
hover = Children.Contains(Ui.MouseOverWidget);
|
hover = Children.Contains(Ui.MouseOverWidget);
|
||||||
|
|
||||||
var panel = getPanelCache.Update((IsDisabled(), Depressed, hover, false, IsSelected() || IsHighlighted()));
|
WidgetUtils.DrawPanel(RenderBounds, getPanelCache.Update((IsDisabled(), Depressed, hover, false, IsSelected() || IsHighlighted())));
|
||||||
|
|
||||||
if (panel != null)
|
|
||||||
WidgetUtils.DrawPanel(RenderBounds, panel);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public override Widget Clone() { return new ScrollItemWidget(this); }
|
public override Widget Clone() { return new ScrollItemWidget(this); }
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
{
|
{
|
||||||
var collectionName = collection + (args.Highlighted ? "-highlighted" : "");
|
var collectionName = collection + (args.Highlighted ? "-highlighted" : "");
|
||||||
var variantImageName = GetStatefulImageName(imageName, args.Disabled, args.Pressed, args.Hover, args.Focused);
|
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 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
{
|
{
|
||||||
var collectionName = collection + (args.Highlighted ? "-highlighted" : "");
|
var collectionName = collection + (args.Highlighted ? "-highlighted" : "");
|
||||||
var variantCollectionName = GetStatefulImageName(collectionName, args.Disabled, args.Pressed, args.Hover, args.Focused);
|
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 @@ namespace OpenRA.Mods.Common.Widgets
|
|||||||
|
|
||||||
public static void DrawPanel(string collection, Rectangle bounds)
|
public static void DrawPanel(string collection, Rectangle bounds)
|
||||||
{
|
{
|
||||||
var sprites = ChromeProvider.GetPanelImages(collection);
|
var sprites = ChromeProvider.TryGetPanelImages(collection);
|
||||||
if (sprites != null)
|
if (sprites != null)
|
||||||
DrawPanel(bounds, sprites);
|
DrawPanel(bounds, sprites);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user