Display an error label instead of crashing in the Asset Browser

This commit is contained in:
Taryn Hill
2016-09-24 11:02:51 -05:00
parent ba02bf43b2
commit b5a08fd123
3 changed files with 55 additions and 14 deletions

View File

@@ -47,6 +47,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic
bool isVideoLoaded = false;
int currentFrame;
LabelWidget errorLabelWidget;
SpriteFont errorFont;
bool IsErrorLabelVisible { get { return errorLabelWidget != null && errorLabelWidget.Visible; } }
[ObjectCreator.UseCtor]
public AssetBrowserLogic(Widget widget, Action onExit, ModData modData, World world, Dictionary<string, MiniYaml> logicArgs)
{
@@ -84,12 +88,16 @@ namespace OpenRA.Mods.Common.Widgets.Logic
spriteWidget.GetSprite = () => currentSprites != null ? currentSprites[currentFrame] : null;
currentPalette = spriteWidget.Palette;
spriteWidget.GetPalette = () => currentPalette;
spriteWidget.IsVisible = () => !isVideoLoaded;
spriteWidget.IsVisible = () => !isVideoLoaded && !IsErrorLabelVisible;
}
var playerWidget = panel.GetOrNull<VqaPlayerWidget>("PLAYER");
if (playerWidget != null)
playerWidget.IsVisible = () => isVideoLoaded;
playerWidget.IsVisible = () => isVideoLoaded && !IsErrorLabelVisible;
errorLabelWidget = panel.GetOrNull<LabelWidget>("ERROR");
if (errorLabelWidget != null)
errorFont = Game.Renderer.Fonts[errorLabelWidget.Font];
var paletteDropDown = panel.GetOrNull<DropDownButtonWidget>("PALETTE_SELECTOR");
if (paletteDropDown != null)
@@ -314,6 +322,10 @@ namespace OpenRA.Mods.Common.Widgets.Logic
if (!modData.DefaultFileSystem.Exists(filename))
return false;
errorLabelWidget.Visible = false;
try
{
if (Path.GetExtension(filename.ToLowerInvariant()) == ".vqa")
{
player = panel.Get<VqaPlayerWidget>("PLAYER");
@@ -325,14 +337,29 @@ namespace OpenRA.Mods.Common.Widgets.Logic
frameSlider.Ticks = 0;
return true;
}
else
{
currentFilename = filename;
currentSprites = world.Map.Rules.Sequences.SpriteCache[filename];
currentFrame = 0;
frameSlider.MaximumValue = (float)currentSprites.Length - 1;
frameSlider.Ticks = currentSprites.Length;
}
catch (Exception ex)
{
if (errorLabelWidget != null)
{
errorLabelWidget.Text = WidgetUtils.TruncateText(errorLabelWidget.Text.Replace("{filename}", filename),
errorLabelWidget.Bounds.Width, errorFont);
currentSprites = new Sprite[0];
errorLabelWidget.Visible = true;
}
Log.AddChannel("assetbrowser", "assetbrowser.log");
Log.Write("assetbrowser", "Error reading {0}:{3} {1}{3}{2}", filename, ex.Message, ex.StackTrace, Environment.NewLine);
return false;
}
return true;
}

View File

@@ -105,6 +105,13 @@ Container@ASSETBROWSER_PANEL:
Width: PARENT_RIGHT
Height: PARENT_BOTTOM
AspectRatio: 1
Label@ERROR:
X: 5
Width: 490 - 10
Height: 325
Align: Center
Visible: false
Text: Error displaying {filename} check assetbrowser.log
Container@FRAME_SELECTOR:
X: 190
Y: 395

View File

@@ -100,6 +100,13 @@ Background@ASSETBROWSER_PANEL:
Width: PARENT_RIGHT
Height: PARENT_BOTTOM
AspectRatio: 1
Label@ERROR:
X: 5
Width: 490 - 10
Height: 330
Align: Center
Visible: false
Text: Error displaying {filename} check assetbrowser.log
Container@FRAME_SELECTOR:
X: 190
Y: 425