Show progress bar for fetching resources in the launcher.

This commit is contained in:
UnknownShadow200 2016-03-26 20:04:13 +11:00
parent b08917f116
commit d8e8d32340
3 changed files with 86 additions and 45 deletions

View File

@ -139,12 +139,12 @@ namespace ClassicalSharp {
} }
int progress = game.AsyncDownloader.CurrentItemProgress; int progress = game.AsyncDownloader.CurrentItemProgress;
if( progress == lastDownloadStatus ) return; if( progress == lastDownloadStatus ) return;
lastDownloadStatus = progress; lastDownloadStatus = progress;
SetTexturePackMessage( progress ); SetFetchStatus( progress );
} }
void SetTexturePackMessage( int progress ) { void SetFetchStatus( int progress ) {
lastDownload.Clear(); lastDownload.Clear();
int index = 0; int index = 0;
if( progress == -2 ) if( progress == -2 )

View File

@ -20,8 +20,8 @@ namespace ClassicalSharp.Network {
Dictionary<string, DownloadedItem> downloaded = new Dictionary<string, DownloadedItem>(); Dictionary<string, DownloadedItem> downloaded = new Dictionary<string, DownloadedItem>();
string skinServer = null; string skinServer = null;
internal Request CurrentItem; public Request CurrentItem;
internal int CurrentItemProgress = -3; public int CurrentItemProgress = -3;
public AsyncDownloader( string skinServer ) { public AsyncDownloader( string skinServer ) {
this.skinServer = skinServer; this.skinServer = skinServer;
@ -268,7 +268,7 @@ namespace ClassicalSharp.Network {
public enum RequestType { Bitmap, String, ByteArray, ContentLength } public enum RequestType { Bitmap, String, ByteArray, ContentLength }
internal sealed class Request { public sealed class Request {
public string Url; public string Url;
public string Identifier; public string Identifier;

View File

@ -26,6 +26,7 @@ namespace Launcher {
bool failed; bool failed;
public override void Tick() { public override void Tick() {
if( fetcher == null || failed ) return; if( fetcher == null || failed ) return;
CheckCurrentProgress();
if( !fetcher.Check( SetStatus ) ) if( !fetcher.Check( SetStatus ) )
failed = true; failed = true;
@ -44,18 +45,92 @@ namespace Launcher {
public override void Resize() { public override void Resize() {
MakeWidgets(); MakeWidgets();
RedrawAllButtonBackgrounds();
using( drawer ) { using( drawer ) {
drawer.SetBitmap( game.Framebuffer ); drawer.SetBitmap( game.Framebuffer );
drawer.Clear( clearCol ); drawer.Clear( clearCol );
drawer.Clear( backCol, game.Width / 2 - 175, game.Height / 2 - 70, 175 * 2, 70 * 2 ); drawer.Clear( backCol, game.Width / 2 - 190, game.Height / 2 - 70, 190 * 2, 70 * 2 );
}
RedrawAllButtonBackgrounds();
using( drawer ) {
drawer.SetBitmap( game.Framebuffer );
RedrawAll(); RedrawAll();
} }
Dirty = true; Dirty = true;
} }
int lastProgress = int.MinValue;
void CheckCurrentProgress() {
Request item = fetcher.downloader.CurrentItem;
if( item == null ) {
lastProgress = int.MinValue; return;
}
int progress = fetcher.downloader.CurrentItemProgress;
if( progress == lastProgress ) return;
lastProgress = progress;
SetFetchStatus( progress );
}
void SetFetchStatus( int progress ) {
if( progress >= 0 && progress <= 100 )
DrawProgressBox( progress );
}
static FastColour progBack = new FastColour( 220, 220, 220 );
static FastColour progFront = new FastColour( 0, 220, 0 );
void DrawProgressBox( int progress ) {
progress = (200 * progress) / 100;
using( drawer ) {
drawer.SetBitmap( game.Framebuffer );
drawer.DrawRect( progBack, game.Width / 2 - 100, game.Height / 2 + 10, 200, 4 );
drawer.DrawRect( progFront, game.Width / 2 - 100, game.Height / 2 + 10, progress, 4 );
Dirty = true;
}
}
ResourceFetcher fetcher; ResourceFetcher fetcher;
Font textFont;
static FastColour backCol = new FastColour( 120, 85, 151 );
static readonly string mainText = "Some required resources weren't found" +
Environment.NewLine + "Okay to download them?";
static readonly string format = "&eDownload size: {0} megabytes";
static FastColour clearCol = new FastColour( 12, 12, 12 );
bool useStatus;
void MakeWidgets() {
widgetIndex = 0;
if( useStatus ) {
MakeLabelAt( widgets[0].Text, statusFont, Anchor.Centre, Anchor.Centre, 0, -10 );
} else {
float dataSize = game.fetcher.DownloadSize;
string text = String.Format( format, dataSize.ToString( "F2" ) );
MakeLabelAt( text, statusFont, Anchor.Centre, Anchor.Centre, 0, 10 );
}
// Clear the entire previous widgets state.
for( int i = 1; i < widgets.Length; i++ ) {
widgets[i] = null;
selectedWidget = null;
lastClicked = null;
}
if( fetcher == null ) {
MakeLabelAt( mainText, infoFont, Anchor.Centre, Anchor.Centre, 0, -40 );
MakeButtonAt( "Yes", 70, 35, textFont, Anchor.Centre,
-70, 45, DownloadResources );
MakeButtonAt( "No", 70, 35, textFont, Anchor.Centre,
70, 45, (x, y) => GotoNextMenu() );
} else {
MakeButtonAt( "Cancel", 120, 35, textFont, Anchor.Centre,
0, 45, (x, y) => GotoNextMenu() );
}
if( lastProgress >= 0 && lastProgress <= 100 )
DrawProgressBox( lastProgress );
}
void DownloadResources( int mouseX, int mouseY ) { void DownloadResources( int mouseX, int mouseY ) {
if( game.Downloader == null ) if( game.Downloader == null )
game.Downloader = new AsyncDownloader( "null" ); game.Downloader = new AsyncDownloader( "null" );
@ -67,41 +142,6 @@ namespace Launcher {
Resize(); Resize();
} }
Font textFont;
static FastColour backCol = new FastColour( 120, 85, 151 );
static readonly string mainText = "Some required resources weren't found" +
Environment.NewLine + "Okay to download them?";
static readonly string format = "Download size: {0} megabytes";
static FastColour clearCol = new FastColour( 12, 12, 12 );
void MakeWidgets() {
widgetIndex = 0;
float dataSize = game.fetcher.DownloadSize;
string text = widgets[0] != null ? widgets[0].Text
: String.Format( format, dataSize.ToString( "F2" ) );
MakeLabelAt( text, statusFont, Anchor.Centre, Anchor.Centre, 0, 5 );
// Clear the entire previous widgets state.
for( int i = 1; i < widgets.Length; i++ ) {
widgets[i] = null;
selectedWidget = null;
lastClicked = null;
}
if( fetcher == null ) {
MakeLabelAt( mainText, infoFont, Anchor.Centre, Anchor.Centre, 0, -30 );
MakeButtonAt( "Yes", 60, 30, textFont, Anchor.Centre,
-50, 40, DownloadResources );
MakeButtonAt( "No", 60, 30, textFont, Anchor.Centre,
50, 40, (x, y) => GotoNextMenu() );
} else {
MakeButtonAt( "Cancel", 120, 30, textFont, Anchor.Centre,
0, 40, (x, y) => GotoNextMenu() );
}
}
void GotoNextMenu() { void GotoNextMenu() {
if( File.Exists( "options.txt" ) ) if( File.Exists( "options.txt" ) )
game.SetScreen( new MainScreen( game ) ); game.SetScreen( new MainScreen( game ) );
@ -110,11 +150,12 @@ namespace Launcher {
} }
void SetStatus( string text ) { void SetStatus( string text ) {
useStatus = true;
LauncherLabelWidget widget = (LauncherLabelWidget)widgets[0]; LauncherLabelWidget widget = (LauncherLabelWidget)widgets[0];
using( drawer ) { using( drawer ) {
drawer.SetBitmap( game.Framebuffer ); drawer.SetBitmap( game.Framebuffer );
drawer.Clear( backCol, widget.X, widget.Y, widget.Width, widget.Height ); drawer.Clear( backCol, widget.X, widget.Y, widget.Width, widget.Height );
widget.SetDrawData( drawer, text, statusFont, Anchor.Centre, Anchor.Centre, 0, 5 ); widget.SetDrawData( drawer, text, statusFont, Anchor.Centre, Anchor.Centre, 0, -10 );
widget.Redraw( drawer ); widget.Redraw( drawer );
Dirty = true; Dirty = true;
} }