Asynchronous loading of map previews in the map chooser dialog

This commit is contained in:
Carko
2013-01-13 15:33:43 +01:00
committed by Chris Forbes
parent c5313375f0
commit 4395a04d57
2 changed files with 22 additions and 9 deletions

View File

@@ -26,7 +26,7 @@ namespace OpenRA.Widgets
public bool IgnoreMouseInput = false; public bool IgnoreMouseInput = false;
public bool ShowSpawnPoints = true; public bool ShowSpawnPoints = true;
static Cache<Map,Bitmap> PreviewCache = new Cache<Map, Bitmap>(stub => Minimap.RenderMapPreview( new Map( stub.Path ))); static readonly Cache<Map,Bitmap> PreviewCache = new Cache<Map, Bitmap>(stub => Minimap.RenderMapPreview( new Map( stub.Path )));
public MapPreviewWidget() : base() { } public MapPreviewWidget() : base() { }
@@ -118,5 +118,16 @@ namespace OpenRA.Widgets
} }
} }
} }
/// <summary>
/// Forces loading the preview into the map cache.
/// </summary>
public Bitmap LoadMapPreview()
{
var map = Map();
if( map == null ) return null;
return PreviewCache[map];
}
} }
} }

View File

@@ -10,6 +10,7 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Threading;
using OpenRA.FileFormats; using OpenRA.FileFormats;
using OpenRA.Widgets; using OpenRA.Widgets;
@@ -21,7 +22,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
ScrollPanelWidget scrollpanel; ScrollPanelWidget scrollpanel;
ScrollItemWidget itemTemplate; ScrollItemWidget itemTemplate;
string gameMode; string gameMode;
[ObjectCreator.UseCtor] [ObjectCreator.UseCtor]
internal MapChooserLogic(Widget widget, string initialMap, Action onExit, Action<Map> onSelect) internal MapChooserLogic(Widget widget, string initialMap, Action onExit, Action<Map> onSelect)
{ {
@@ -38,18 +39,18 @@ namespace OpenRA.Mods.RA.Widgets.Logic
var gameModeDropdown = widget.GetOrNull<DropDownButtonWidget>("GAMEMODE_FILTER"); var gameModeDropdown = widget.GetOrNull<DropDownButtonWidget>("GAMEMODE_FILTER");
if (gameModeDropdown != null) if (gameModeDropdown != null)
{ {
var selectableMaps = Game.modData.AvailableMaps.Where(m => m.Value.Selectable); var selectableMaps = Game.modData.AvailableMaps.Where(m => m.Value.Selectable).ToList();
var gameModes = selectableMaps var gameModes = selectableMaps
.GroupBy(m => m.Value.Type) .GroupBy(m => m.Value.Type)
.Select(g => Pair.New(g.Key, g.Count())).ToList(); .Select(g => Pair.New(g.Key, g.Count())).ToList();
// 'all game types' extra item // 'all game types' extra item
gameModes.Insert( 0, Pair.New( null as string, selectableMaps.Count() ) ); gameModes.Insert(0, Pair.New(null as string, selectableMaps.Count()));
Func<Pair<string,int>, string> showItem = Func<Pair<string, int>, string> showItem =
x => "{0} ({1})".F( x.First ?? "All Game Types", x.Second ); x => "{0} ({1})".F(x.First ?? "All Game Types", x.Second);
Func<Pair<string,int>, ScrollItemWidget, ScrollItemWidget> setupItem = (ii, template) => Func<Pair<string, int>, ScrollItemWidget, ScrollItemWidget> setupItem = (ii, template) =>
{ {
var item = ScrollItemWidget.Setup(template, var item = ScrollItemWidget.Setup(template,
() => gameMode == ii.First, () => gameMode == ii.First,
@@ -64,7 +65,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
gameModeDropdown.GetText = () => showItem(gameModes.First(m => m.First == gameMode)); gameModeDropdown.GetText = () => showItem(gameModes.First(m => m.First == gameMode));
} }
EnumerateMaps(); new Thread(EnumerateMaps).Start();
} }
void EnumerateMaps() void EnumerateMaps()
@@ -91,6 +92,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
previewWidget.IgnoreMouseOver = true; previewWidget.IgnoreMouseOver = true;
previewWidget.IgnoreMouseInput = true; previewWidget.IgnoreMouseInput = true;
previewWidget.Map = () => m; previewWidget.Map = () => m;
previewWidget.LoadMapPreview();
var detailsWidget = item.Get<LabelWidget>("DETAILS"); var detailsWidget = item.Get<LabelWidget>("DETAILS");
if (detailsWidget != null) if (detailsWidget != null)
@@ -112,7 +114,7 @@ namespace OpenRA.Mods.RA.Widgets.Logic
sizeWidget.GetText = () => size; sizeWidget.GetText = () => size;
} }
scrollpanel.AddChild(item); Game.RunAfterTick(() => scrollpanel.AddChild(item));
} }
} }
} }