From 448ec55e6e32c6f686d282c14cff693f4bd23019 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sat, 10 Oct 2015 18:21:41 +1100 Subject: [PATCH] Add screen for loading maps in singleplayer. --- ClassicalSharp/2D/Screens/FilesScreen.cs | 60 ++++------------ .../2D/Screens/Menu/LoadLevelScreen.cs | 70 +++++++++++++++++++ ClassicalSharp/2D/Screens/Menu/PauseScreen.cs | 32 ++++++--- .../2D/Screens/Menu/TexturePackScreen.cs | 41 +++++++++++ ClassicalSharp/2D/Screens/NormalScreen.cs | 3 +- ClassicalSharp/ClassicalSharp.csproj | 2 + ClassicalSharp/Singleplayer/Commands.cs | 50 ------------- ClassicalSharp/Singleplayer/Server.cs | 1 - Launcher/MainForm.cs | 2 +- 9 files changed, 153 insertions(+), 108 deletions(-) create mode 100644 ClassicalSharp/2D/Screens/Menu/LoadLevelScreen.cs create mode 100644 ClassicalSharp/2D/Screens/Menu/TexturePackScreen.cs diff --git a/ClassicalSharp/2D/Screens/FilesScreen.cs b/ClassicalSharp/2D/Screens/FilesScreen.cs index 7b7ca8ee4..1ac554cbd 100644 --- a/ClassicalSharp/2D/Screens/FilesScreen.cs +++ b/ClassicalSharp/2D/Screens/FilesScreen.cs @@ -1,8 +1,6 @@ using System; using System.Drawing; using OpenTK.Input; -using System.IO; -using ClassicalSharp.TexturePack; namespace ClassicalSharp { @@ -64,7 +62,7 @@ namespace ClassicalSharp { protected abstract void TextButtonClick( Game game, ButtonWidget widget ); - void PageClick( bool forward ) { + protected void PageClick( bool forward ) { currentIndex += forward ? 5 : -5; if( currentIndex >= files.Length ) currentIndex -= 5; @@ -76,6 +74,19 @@ namespace ClassicalSharp { } } + public override bool HandlesKeyDown( Key key ) { + if( key == Key.Escape ) { + game.SetNewScreen( new NormalScreen( game ) ); + } else if( key == Key.Left ) { + PageClick( false ); + } else if( key == Key.Right ) { + PageClick( true ); + } else { + return false; + } + return true; + } + public override bool HandlesMouseMove( int mouseX, int mouseY ) { for( int i = 0; i < buttons.Length; i++ ) buttons[i].Active = false; @@ -121,45 +132,4 @@ namespace ClassicalSharp { graphicsApi.Texturing = false; } } - - public sealed class TexturePackScreen : FilesScreen { - - public TexturePackScreen( Game game ) : base( game ) { - titleText = "Select a texture pack zip"; - string directory = Environment.CurrentDirectory; - files = Directory.GetFiles( directory, "*.zip", SearchOption.AllDirectories ); - - for( int i = 0; i < files.Length; i++ ) { - string absolutePath = files[i]; - files[i] = absolutePath.Substring( directory.Length + 1 ); - } - } - - public override bool HandlesKeyDown( Key key ) { - if( key == Key.Escape ) { - game.SetNewScreen( new NormalScreen( game ) ); - return true; - } - return false; - } - - public override void Init() { - base.Init(); - buttons[buttons.Length - 1] = - Make( 0, 5, "Back to menu", (g, w) => g.SetNewScreen( new PauseScreen( g ) ) ); - } - - ButtonWidget Make( int x, int y, string text, Action onClick ) { - return ButtonWidget.Create( game, x, y, 240, 35, text, - Anchor.Centre, Anchor.BottomOrRight, titleFont, onClick ); - } - - protected override void TextButtonClick( Game game, ButtonWidget widget ) { - string path = widget.Text; - if( File.Exists( path ) ) { - TexturePackExtractor extractor = new TexturePackExtractor(); - extractor.Extract( path, game ); - } - } - } -} +} \ No newline at end of file diff --git a/ClassicalSharp/2D/Screens/Menu/LoadLevelScreen.cs b/ClassicalSharp/2D/Screens/Menu/LoadLevelScreen.cs new file mode 100644 index 000000000..1ff9e346f --- /dev/null +++ b/ClassicalSharp/2D/Screens/Menu/LoadLevelScreen.cs @@ -0,0 +1,70 @@ +using System; +using System.IO; + +namespace ClassicalSharp { + + public sealed class LoadLevelScreen : FilesScreen { + + public LoadLevelScreen( Game game ) : base( game ) { + titleText = "Select a level"; + string directory = Environment.CurrentDirectory; + string[] cwFiles = Directory.GetFiles( directory, "*.cw", SearchOption.AllDirectories ); + string[] datFiles = Directory.GetFiles( directory, "*.dat", SearchOption.AllDirectories ); + files = new string[cwFiles.Length + datFiles.Length]; + Array.Copy( cwFiles, 0, files, 0, cwFiles.Length ); + Array.Copy( datFiles, 0, files, cwFiles.Length, datFiles.Length ); + + for( int i = 0; i < files.Length; i++ ) { + string absolutePath = files[i]; + files[i] = absolutePath.Substring( directory.Length + 1 ); + } + Array.Sort( files ); + } + + public override void Init() { + base.Init(); + buttons[buttons.Length - 1] = + Make( 0, 5, "Back to menu", (g, w) => g.SetNewScreen( new PauseScreen( g ) ) ); + } + + ButtonWidget Make( int x, int y, string text, Action onClick ) { + return ButtonWidget.Create( game, x, y, 240, 35, text, + Anchor.Centre, Anchor.BottomOrRight, titleFont, onClick ); + } + + protected override void TextButtonClick( Game game, ButtonWidget widget ) { + string path = widget.Text; + if( File.Exists( path ) ) + LoadMap( path ); + } + + void LoadMap( string path ) { + IMapFile mapFile = null; + if( path.EndsWith( ".dat" ) ) { + mapFile = new MapDat(); + } else if( path.EndsWith( ".fcm" ) ) { + mapFile = new MapFcm3(); + } else if( path.EndsWith( ".cw" ) ) { + mapFile = new MapCw(); + } + + try { + using( FileStream fs = new FileStream( path, FileMode.Open, FileAccess.Read, FileShare.Read ) ) { + int width, height, length; + game.Map.Reset(); + + byte[] blocks = mapFile.Load( fs, game, out width, out height, out length ); + game.Map.UseRawMap( blocks, width, height, length ); + game.Events.RaiseOnNewMapLoaded(); + + LocalPlayer p = game.LocalPlayer; + LocationUpdate update = LocationUpdate.MakePos( p.SpawnPoint, false ); + p.SetLocation( update, false ); + } + } catch( Exception ex ) { + Utils.LogError( "Error while trying to load map: {0}{1}", Environment.NewLine, ex ); + game.Chat.Add( "&e/client loadmap: Failed to load map \"" + path + "\"" ); + } + } + } +} \ No newline at end of file diff --git a/ClassicalSharp/2D/Screens/Menu/PauseScreen.cs b/ClassicalSharp/2D/Screens/Menu/PauseScreen.cs index 6cbaf37f8..0713711a8 100644 --- a/ClassicalSharp/2D/Screens/Menu/PauseScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/PauseScreen.cs @@ -7,7 +7,7 @@ namespace ClassicalSharp { public class PauseScreen : MenuScreen { public PauseScreen( Game game ) : base( game ) { - } + } public override void Render( double delta ) { RenderMenuBounds(); @@ -18,15 +18,27 @@ namespace ClassicalSharp { public override void Init() { titleFont = new Font( "Arial", 16, FontStyle.Bold ); - buttons = new ButtonWidget[] { - Make( 0, -100, "Options", Anchor.Centre, (g, w) => g.SetNewScreen( new OptionsScreen( g ) ) ), - Make( 0, -50, "Environment settings", Anchor.Centre, (g, w) => g.SetNewScreen( new EnvSettingsScreen( g ) ) ), - Make( 0, 0, "Select texture pack", Anchor.Centre, (g, w) => g.SetNewScreen( new TexturePackScreen( g ) ) ), - Make( 0, 50, "Save level", Anchor.Centre, (g, w) => g.SetNewScreen( new SaveLevelScreen( g ) ) ), - // TODO: singleplayer Make( 0, 50, "Load/Save/Gen level", Docking.Centre, (g, w) => g.SetNewScreen( new SaveLevelScreen( g ) ) ), - Make( 0, 55, "Back to game", Anchor.BottomOrRight, (g, w) => g.SetNewScreen( new NormalScreen( g ) ) ), - Make( 0, 5, "Quit game", Anchor.BottomOrRight, (g, w) => g.Exit() ), - }; + if( game.Network.IsSinglePlayer ) { + buttons = new ButtonWidget[] { + Make( -140, -50, "Options", Anchor.Centre, (g, w) => g.SetNewScreen( new OptionsScreen( g ) ) ), + Make( -140, 0, "Environment settings", Anchor.Centre, (g, w) => g.SetNewScreen( new EnvSettingsScreen( g ) ) ), + Make( -140, 50, "Select texture pack", Anchor.Centre, (g, w) => g.SetNewScreen( new TexturePackScreen( g ) ) ), + Make( 140, -50, "Save level", Anchor.Centre, (g, w) => g.SetNewScreen( new SaveLevelScreen( g ) ) ), + Make( 140, 0, "Load level", Anchor.Centre, (g, w) => g.SetNewScreen( new LoadLevelScreen( g ) ) ), + // TODO: singleplayer Make( 0, 50, "Load/Save/Gen level", Docking.Centre, (g, w) => g.SetNewScreen( new SaveLevelScreen( g ) ) ), + Make( 0, 55, "Back to game", Anchor.BottomOrRight, (g, w) => g.SetNewScreen( new NormalScreen( g ) ) ), + Make( 0, 5, "Quit game", Anchor.BottomOrRight, (g, w) => g.Exit() ), + }; + } else { + buttons = new ButtonWidget[] { + Make( 0, -100, "Options", Anchor.Centre, (g, w) => g.SetNewScreen( new OptionsScreen( g ) ) ), + Make( 0, -50, "Environment settings", Anchor.Centre, (g, w) => g.SetNewScreen( new EnvSettingsScreen( g ) ) ), + Make( 0, 0, "Select texture pack", Anchor.Centre, (g, w) => g.SetNewScreen( new TexturePackScreen( g ) ) ), + Make( 0, 50, "Save level", Anchor.Centre, (g, w) => g.SetNewScreen( new SaveLevelScreen( g ) ) ), + Make( 0, 55, "Back to game", Anchor.BottomOrRight, (g, w) => g.SetNewScreen( new NormalScreen( g ) ) ), + Make( 0, 5, "Quit game", Anchor.BottomOrRight, (g, w) => g.Exit() ), + }; + } } ButtonWidget Make( int x, int y, string text, Anchor vDocking, Action onClick ) { diff --git a/ClassicalSharp/2D/Screens/Menu/TexturePackScreen.cs b/ClassicalSharp/2D/Screens/Menu/TexturePackScreen.cs new file mode 100644 index 000000000..13b8d3978 --- /dev/null +++ b/ClassicalSharp/2D/Screens/Menu/TexturePackScreen.cs @@ -0,0 +1,41 @@ +using System; +using System.IO; +using ClassicalSharp.TexturePack; +using OpenTK.Input; + +namespace ClassicalSharp { + + public sealed class TexturePackScreen : FilesScreen { + + public TexturePackScreen( Game game ) : base( game ) { + titleText = "Select a texture pack zip"; + string directory = Environment.CurrentDirectory; + files = Directory.GetFiles( directory, "*.zip", SearchOption.AllDirectories ); + + for( int i = 0; i < files.Length; i++ ) { + string absolutePath = files[i]; + files[i] = absolutePath.Substring( directory.Length + 1 ); + } + Array.Sort( files ); + } + + public override void Init() { + base.Init(); + buttons[buttons.Length - 1] = + Make( 0, 5, "Back to menu", (g, w) => g.SetNewScreen( new PauseScreen( g ) ) ); + } + + ButtonWidget Make( int x, int y, string text, Action onClick ) { + return ButtonWidget.Create( game, x, y, 240, 35, text, + Anchor.Centre, Anchor.BottomOrRight, titleFont, onClick ); + } + + protected override void TextButtonClick( Game game, ButtonWidget widget ) { + string path = widget.Text; + if( File.Exists( path ) ) { + TexturePackExtractor extractor = new TexturePackExtractor(); + extractor.Extract( path, game ); + } + } + } +} \ No newline at end of file diff --git a/ClassicalSharp/2D/Screens/NormalScreen.cs b/ClassicalSharp/2D/Screens/NormalScreen.cs index 5ab4c2713..1752b4751 100644 --- a/ClassicalSharp/2D/Screens/NormalScreen.cs +++ b/ClassicalSharp/2D/Screens/NormalScreen.cs @@ -18,7 +18,8 @@ namespace ClassicalSharp { public override void Render( double delta ) { if( game.HideGui ) return; - chat.RenderBackground(); + if( chat.HandlesAllInput ) + chat.RenderBackground(); graphicsApi.Texturing = true; chat.Render( delta ); hotbar.Render( delta ); diff --git a/ClassicalSharp/ClassicalSharp.csproj b/ClassicalSharp/ClassicalSharp.csproj index 7d25fe73f..f996b77d8 100644 --- a/ClassicalSharp/ClassicalSharp.csproj +++ b/ClassicalSharp/ClassicalSharp.csproj @@ -78,11 +78,13 @@ + + diff --git a/ClassicalSharp/Singleplayer/Commands.cs b/ClassicalSharp/Singleplayer/Commands.cs index fc0c7911e..c29121725 100644 --- a/ClassicalSharp/Singleplayer/Commands.cs +++ b/ClassicalSharp/Singleplayer/Commands.cs @@ -41,54 +41,4 @@ namespace ClassicalSharp.Singleplayer { } } } - - public sealed class LoadMapCommand : Command { - - public LoadMapCommand() { - Name = "LoadMap"; - Help = new [] { - "&a/client loadmap [filename]", - "&bfilename: &eLoads a map from the specified filename.", - "&eSupported formats are .fcm (fCraft map) and ", - "&e.dat (Original classic map or WoM saved map)", - }; - } - - public override void Execute( CommandReader reader ) { - string path = reader.NextAll(); - if( String.IsNullOrEmpty( path ) ) return; - - IMapFile mapFile; - if( path.EndsWith( ".dat" ) ) { - mapFile = new MapDat(); - } else if( path.EndsWith( ".fcm" ) ) { - mapFile = new MapFcm3(); - } else if( path.EndsWith( ".cw" ) ) { - mapFile = new MapCw(); - } else { - game.Chat.Add( "&e/client loadmap: Map format of file \"" + path + "\" not supported" ); - return; - } - - try { - using( FileStream fs = new FileStream( path, FileMode.Open, FileAccess.Read, FileShare.Read ) ) { - int width, height, length; - game.Map.Reset(); - - byte[] blocks = mapFile.Load( fs, game, out width, out height, out length ); - game.Map.UseRawMap( blocks, width, height, length ); - game.Events.RaiseOnNewMapLoaded(); - - LocalPlayer p = game.LocalPlayer; - LocationUpdate update = LocationUpdate.MakePos( p.SpawnPoint, false ); - p.SetLocation( update, false ); - } - } catch( FileNotFoundException ) { - game.Chat.Add( "&e/client loadmap: Couldn't find file \"" + path + "\"" ); - } catch( Exception ex ) { - Utils.LogError( "Error while trying to load map: {0}{1}", Environment.NewLine, ex ); - game.Chat.Add( "&e/client loadmap: Failed to load map \"" + path + "\"" ); - } - } - } } diff --git a/ClassicalSharp/Singleplayer/Server.cs b/ClassicalSharp/Singleplayer/Server.cs index ed42c3e39..430bcad5e 100644 --- a/ClassicalSharp/Singleplayer/Server.cs +++ b/ClassicalSharp/Singleplayer/Server.cs @@ -27,7 +27,6 @@ namespace ClassicalSharp.Singleplayer { NewMap(); MakeMap( 128, 128, 128 ); game.CommandManager.RegisterCommand( new GenerateCommand() ); - game.CommandManager.RegisterCommand( new LoadMapCommand() ); } public override void SendChat( string text ) { diff --git a/Launcher/MainForm.cs b/Launcher/MainForm.cs index 0237fb4a8..7c5356f16 100644 --- a/Launcher/MainForm.cs +++ b/Launcher/MainForm.cs @@ -134,7 +134,7 @@ namespace Launcher { mppass = Secure.Decode( mppass, txtDCuser.Text ); if( mppass != null ) txtDCmppass.Text = mppass; - + mppass = Options.Get( "launcher-cc-password" ) ?? null; mppass = Secure.Decode( mppass, txtCCUser.Text ); if( mppass != null )