From d8e8d32340f0248835d0e3d33e1fc5b311b7b6d6 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 26 Mar 2016 20:04:13 +1100 Subject: [PATCH] Show progress bar for fetching resources in the launcher. --- ClassicalSharp/2D/Screens/ChatScreen.cs | 6 +- .../Network/Utils/AsyncDownloader.cs | 6 +- Launcher2/Gui/Screens/ResourcesScreen.cs | 119 ++++++++++++------ 3 files changed, 86 insertions(+), 45 deletions(-) diff --git a/ClassicalSharp/2D/Screens/ChatScreen.cs b/ClassicalSharp/2D/Screens/ChatScreen.cs index 27234385a..7d0384d21 100644 --- a/ClassicalSharp/2D/Screens/ChatScreen.cs +++ b/ClassicalSharp/2D/Screens/ChatScreen.cs @@ -139,12 +139,12 @@ namespace ClassicalSharp { } int progress = game.AsyncDownloader.CurrentItemProgress; - if( progress == lastDownloadStatus ) return; + if( progress == lastDownloadStatus ) return; lastDownloadStatus = progress; - SetTexturePackMessage( progress ); + SetFetchStatus( progress ); } - void SetTexturePackMessage( int progress ) { + void SetFetchStatus( int progress ) { lastDownload.Clear(); int index = 0; if( progress == -2 ) diff --git a/ClassicalSharp/Network/Utils/AsyncDownloader.cs b/ClassicalSharp/Network/Utils/AsyncDownloader.cs index ab3edd00e..9e320b2fd 100644 --- a/ClassicalSharp/Network/Utils/AsyncDownloader.cs +++ b/ClassicalSharp/Network/Utils/AsyncDownloader.cs @@ -20,8 +20,8 @@ namespace ClassicalSharp.Network { Dictionary downloaded = new Dictionary(); string skinServer = null; - internal Request CurrentItem; - internal int CurrentItemProgress = -3; + public Request CurrentItem; + public int CurrentItemProgress = -3; public AsyncDownloader( string skinServer ) { this.skinServer = skinServer; @@ -268,7 +268,7 @@ namespace ClassicalSharp.Network { public enum RequestType { Bitmap, String, ByteArray, ContentLength } - internal sealed class Request { + public sealed class Request { public string Url; public string Identifier; diff --git a/Launcher2/Gui/Screens/ResourcesScreen.cs b/Launcher2/Gui/Screens/ResourcesScreen.cs index 88574cc4c..242aa11da 100644 --- a/Launcher2/Gui/Screens/ResourcesScreen.cs +++ b/Launcher2/Gui/Screens/ResourcesScreen.cs @@ -26,6 +26,7 @@ namespace Launcher { bool failed; public override void Tick() { if( fetcher == null || failed ) return; + CheckCurrentProgress(); if( !fetcher.Check( SetStatus ) ) failed = true; @@ -44,18 +45,92 @@ namespace Launcher { public override void Resize() { MakeWidgets(); - RedrawAllButtonBackgrounds(); - using( drawer ) { drawer.SetBitmap( game.Framebuffer ); 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(); } 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; + 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 ) { if( game.Downloader == null ) game.Downloader = new AsyncDownloader( "null" ); @@ -67,41 +142,6 @@ namespace Launcher { 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() { if( File.Exists( "options.txt" ) ) game.SetScreen( new MainScreen( game ) ); @@ -110,11 +150,12 @@ namespace Launcher { } void SetStatus( string text ) { + useStatus = true; LauncherLabelWidget widget = (LauncherLabelWidget)widgets[0]; using( drawer ) { drawer.SetBitmap( game.Framebuffer ); 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 ); Dirty = true; }