From fe1d808025ddfbce2afdb20c19b437abae3850cc Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 24 Sep 2015 19:39:51 +1000 Subject: [PATCH] Fix crashing when pressing button in OptionsScreen, save and load default view distance, other minor gui touchups. --- .../2D/Screens/Menu/EnvSettingsScreen.cs | 36 ++++++++++--------- .../2D/Screens/Menu/MenuInputScreen.cs | 20 +++++++++-- .../2D/Screens/Menu/OptionsScreen.cs | 8 ++--- ClassicalSharp/2D/Screens/Menu/PauseScreen.cs | 10 ++++-- ClassicalSharp/2D/Screens/NormalScreen.cs | 1 + ClassicalSharp/2D/Widgets/ButtonWidget.cs | 1 + .../2D/Widgets/Menu/MenuInputValidator.cs | 8 ++--- ClassicalSharp/Game/Game.Chat.cs | 2 +- ClassicalSharp/Game/Game.InputHandling.cs | 6 ---- ClassicalSharp/Game/Game.cs | 15 +++++--- ClassicalSharp/GraphicsAPI/VertexFormats.cs | 6 ---- .../Rendering/StandardEnvRenderer.cs | 23 ++++++------ ClassicalSharp/Utils/Options.cs | 18 ++++++++++ 13 files changed, 97 insertions(+), 57 deletions(-) diff --git a/ClassicalSharp/2D/Screens/Menu/EnvSettingsScreen.cs b/ClassicalSharp/2D/Screens/Menu/EnvSettingsScreen.cs index 34f4393f9..12ad9d2e0 100644 --- a/ClassicalSharp/2D/Screens/Menu/EnvSettingsScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/EnvSettingsScreen.cs @@ -15,42 +15,45 @@ namespace ClassicalSharp { hintFont = new Font( "Arial", 14, FontStyle.Italic ); buttons = new ButtonWidget[] { - Make( -140, -100, "Clouds colour", Docking.Centre, OnWidgetClick, + Make( -140, -150, "Clouds colour", Docking.Centre, OnWidgetClick, g => g.Map.CloudsCol.ToRGBHexString(), (g, v) => g.Map.SetCloudsColour( FastColour.Parse( v ) ) ), - Make( -140, -50, "Sky colour", Docking.Centre, OnWidgetClick, + Make( -140, -100, "Sky colour", Docking.Centre, OnWidgetClick, g => g.Map.SkyCol.ToRGBHexString(), (g, v) => g.Map.SetSkyColour( FastColour.Parse( v ) ) ), - Make( -140, 0, "Fog colour", Docking.Centre, OnWidgetClick, + Make( -140, -50, "Fog colour", Docking.Centre, OnWidgetClick, g => g.Map.FogCol.ToRGBHexString(), (g, v) => g.Map.SetFogColour( FastColour.Parse( v ) ) ), - Make( -140, 50, "Clouds speed", Docking.Centre, OnWidgetClick, - g => { - StandardEnvRenderer env = game.EnvRenderer as StandardEnvRenderer; - return env == null ? "(not active)" : env.CloudsSpeed.ToString(); - }, - (g, v) => { - StandardEnvRenderer env = game.EnvRenderer as StandardEnvRenderer; + Make( -140, 0, "Clouds speed", Docking.Centre, OnWidgetClick, + g => { StandardEnvRenderer env = game.EnvRenderer as StandardEnvRenderer; + return env == null ? "(not active)" : env.CloudsSpeed.ToString(); }, + (g, v) => { StandardEnvRenderer env = game.EnvRenderer as StandardEnvRenderer; if( env != null ) - env.CloudsSpeed = Single.Parse( v ); - } ), + env.CloudsSpeed = Single.Parse( v ); } ), - Make( 140, -100, "Sunlight colour", Docking.Centre, OnWidgetClick, + Make( -140, 50, "Clouds offset", Docking.Centre, OnWidgetClick, + g => { StandardEnvRenderer env = game.EnvRenderer as StandardEnvRenderer; + return env == null ? "(not active)" : env.CloudsOffset.ToString(); }, + (g, v) => { StandardEnvRenderer env = game.EnvRenderer as StandardEnvRenderer; + if( env != null ) + env.SetCloudsOffset( Int32.Parse( v ) ); } ), + + Make( 140, -150, "Sunlight colour", Docking.Centre, OnWidgetClick, g => g.Map.Sunlight.ToRGBHexString(), (g, v) => g.Map.SetSunlight( FastColour.Parse( v ) ) ), - Make( 140, -50, "Shadow colour", Docking.Centre, OnWidgetClick, + Make( 140, -100, "Shadow colour", Docking.Centre, OnWidgetClick, g => g.Map.Shadowlight.ToRGBHexString(), (g, v) => g.Map.SetShadowlight( FastColour.Parse( v ) ) ), - Make( 140, 0, "Weather", Docking.Centre, OnWidgetClick, + Make( 140, -50, "Weather", Docking.Centre, OnWidgetClick, g => ((int)g.Map.Weather).ToString(), (g, v) => g.Map.SetWeather( (Weather)Int32.Parse( v ) ) ), - Make( 140, 50, "Water level", Docking.Centre, OnWidgetClick, + Make( 140, 0, "Water level", Docking.Centre, OnWidgetClick, g => g.Map.WaterHeight.ToString(), (g, v) => g.Map.SetWaterLevel( Int32.Parse( v ) ) ), @@ -64,6 +67,7 @@ namespace ClassicalSharp { new HexColourValidator(), new HexColourValidator(), new RealValidator( 0, 1000 ), + new IntegerValidator( -1000, 1000 ), new HexColourValidator(), new HexColourValidator(), new IntegerValidator( 0, 2 ), diff --git a/ClassicalSharp/2D/Screens/Menu/MenuInputScreen.cs b/ClassicalSharp/2D/Screens/Menu/MenuInputScreen.cs index 2d1d2f336..62ed1a9a0 100644 --- a/ClassicalSharp/2D/Screens/Menu/MenuInputScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/MenuInputScreen.cs @@ -27,14 +27,21 @@ namespace ClassicalSharp { } public override bool HandlesKeyPress( char key ) { + if( inputWidget == null ) return true; return inputWidget.HandlesKeyPress( key ); } public override bool HandlesKeyDown( Key key ) { + if( key == Key.Escape ) { + game.SetNewScreen( new NormalScreen( game ) ); + return true; + } + if( inputWidget == null ) return true; return inputWidget.HandlesKeyDown( key ); } public override bool HandlesKeyUp( Key key ) { + if( inputWidget == null ) return true; return inputWidget.HandlesKeyUp( key ); } @@ -85,14 +92,21 @@ namespace ClassicalSharp { buttons[okayIndex].Dispose(); buttons[okayIndex] = null; return; + } + + int index = Array.IndexOf( buttons, widget ); + MenuInputValidator validator = validators[index]; + if( validator is BooleanValidator ) { + string value = widget.GetValue( game ); + widget.SetValue( game, value == "yes" ? "no" : "yes" ); + UpdateDescription( widget ); + return; } if( inputWidget != null ) inputWidget.Dispose(); - targetWidget = selectedWidget; - int index = Array.IndexOf( buttons, widget ); - MenuInputValidator validator = validators[index]; + targetWidget = selectedWidget; inputWidget = MenuInputWidget.Create( game, 0, 150, 400, 25, widget.GetValue( game ), Docking.Centre, Docking.Centre, regularFont, titleFont, hintFont, validator ); diff --git a/ClassicalSharp/2D/Screens/Menu/OptionsScreen.cs b/ClassicalSharp/2D/Screens/Menu/OptionsScreen.cs index 6966f6b76..6fc1acf2d 100644 --- a/ClassicalSharp/2D/Screens/Menu/OptionsScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/OptionsScreen.cs @@ -15,16 +15,16 @@ namespace ClassicalSharp { buttons = new ButtonWidget[] { Make( -140, -50, "Use animations", Docking.Centre, OnWidgetClick, - g => g.Animations.Enabled ? "y" : "n", - (g, v) => g.Animations.Enabled = v == "y" ), + g => g.Animations.Enabled ? "yes" : "no", + (g, v) => g.Animations.Enabled = v == "yes" ), Make( -140, 0, "View distance", Docking.Centre, OnWidgetClick, g => g.ViewDistance.ToString(), (g, v) => g.SetViewDistance( Int32.Parse( v ) ) ), Make( -140, 50, "VSync active", Docking.Centre, OnWidgetClick, - g => g.VSync ? "y" : "n", - (g, v) => g.Graphics.SetVSync( g, v == "y" ) ), + g => g.VSync ? "yes" : "no", + (g, v) => g.Graphics.SetVSync( g, v == "yes" ) ), Make( 140, -50, "Mouse sensitivity", Docking.Centre, OnWidgetClick, g => g.MouseSensitivity.ToString(), (g, v) => g.MouseSensitivity = Int32.Parse( v ) ), diff --git a/ClassicalSharp/2D/Screens/Menu/PauseScreen.cs b/ClassicalSharp/2D/Screens/Menu/PauseScreen.cs index c4f8360f6..ef6d3f4fd 100644 --- a/ClassicalSharp/2D/Screens/Menu/PauseScreen.cs +++ b/ClassicalSharp/2D/Screens/Menu/PauseScreen.cs @@ -21,14 +21,20 @@ namespace ClassicalSharp { buttons = new ButtonWidget[] { Make( 0, -50, "Options", Docking.Centre, (g, w) => g.SetNewScreen( new OptionsScreen( g ) ) ), Make( 0, 0, "Environment settings", Docking.Centre, (g, w) => g.SetNewScreen( new EnvSettingsScreen( g ) ) ), - Make( 0, 50, "Key mappings", Docking.Centre, (g, w) => g.SetNewScreen( new KeyMappingsScreen( g ) ) ), // TODO: Temp fix + Make( 0, 50, "Key mappings", Docking.Centre, (g, w) => g.SetNewScreen( new KeyMappingsScreen( g ) ) ), Make( 0, 55, "Back to game", Docking.BottomOrRight, (g, w) => g.SetNewScreen( new NormalScreen( g ) ) ), - Make( 0, 5, "Exit", Docking.BottomOrRight, (g, w) => g.Exit() ), + Make( 0, 5, "Quit game", Docking.BottomOrRight, (g, w) => g.Exit() ), }; } ButtonWidget Make( int x, int y, string text, Docking vDocking, Action onClick ) { return ButtonWidget.Create( game, x, y, 240, 35, text, Docking.Centre, vDocking, titleFont, onClick ); } + + public override bool HandlesKeyDown( Key key ) { + if( key == Key.Escape ) + game.SetNewScreen( new NormalScreen( game ) ); + return true; + } } } \ No newline at end of file diff --git a/ClassicalSharp/2D/Screens/NormalScreen.cs b/ClassicalSharp/2D/Screens/NormalScreen.cs index 261477343..1716f11e8 100644 --- a/ClassicalSharp/2D/Screens/NormalScreen.cs +++ b/ClassicalSharp/2D/Screens/NormalScreen.cs @@ -75,6 +75,7 @@ namespace ClassicalSharp { hotbar.Init(); if( game.CursorVisible ) game.CursorVisible = false; + game.Camera.RegrabMouse(); } public override bool HandlesAllInput { diff --git a/ClassicalSharp/2D/Widgets/ButtonWidget.cs b/ClassicalSharp/2D/Widgets/ButtonWidget.cs index 6767cf932..2875c6209 100644 --- a/ClassicalSharp/2D/Widgets/ButtonWidget.cs +++ b/ClassicalSharp/2D/Widgets/ButtonWidget.cs @@ -113,6 +113,7 @@ namespace ClassicalSharp { texture = Utils2D.Make2DTexture( graphicsApi, bmp, size, 0, 0 ); } } + GraphicsPath MakePath( float offset, float width, float height ) { GraphicsPath path = new GraphicsPath(); float x1 = offset, y1 = offset; diff --git a/ClassicalSharp/2D/Widgets/Menu/MenuInputValidator.cs b/ClassicalSharp/2D/Widgets/Menu/MenuInputValidator.cs index e3c8528d6..0a6097a43 100644 --- a/ClassicalSharp/2D/Widgets/Menu/MenuInputValidator.cs +++ b/ClassicalSharp/2D/Widgets/Menu/MenuInputValidator.cs @@ -95,19 +95,19 @@ namespace ClassicalSharp { public sealed class BooleanValidator : MenuInputValidator { public BooleanValidator() { - Range = "&7(y or n)"; + Range = "&7(yes or no)"; } public override bool IsValidChar( char c ) { - return c == 'y' || c == 'n'; + return c >= 'a' && c <= 'z'; } public override bool IsValidString( string s ) { - return s.Length <= 1; + return s.Length <= 3; } public override bool IsValidValue( string s ) { - return s == "y" || s == "n"; + return s == "yes" || s == "no"; } } } diff --git a/ClassicalSharp/Game/Game.Chat.cs b/ClassicalSharp/Game/Game.Chat.cs index 9e0ae756c..634aca242 100644 --- a/ClassicalSharp/Game/Game.Chat.cs +++ b/ClassicalSharp/Game/Game.Chat.cs @@ -73,7 +73,7 @@ namespace ClassicalSharp { if( now.Day != last.Day || now.Month != last.Month || now.Year != last.Year ) { if( writer != null ) { - writer.Close(); + writer.Dispose(); writer = null; } OpenChatFile( now ); diff --git a/ClassicalSharp/Game/Game.InputHandling.cs b/ClassicalSharp/Game/Game.InputHandling.cs index 007327a3b..75148f0bc 100644 --- a/ClassicalSharp/Game/Game.InputHandling.cs +++ b/ClassicalSharp/Game/Game.InputHandling.cs @@ -249,12 +249,6 @@ namespace ClassicalSharp { } void LoadKeyBindings() { - try { - Options.Load(); - } catch( IOException ) { - Utils.LogWarning( "Unable to load options.txt" ); - return; - } string[] names = KeyMapping.GetNames( typeof( KeyMapping ) ); for( int i = 0; i < names.Length; i++ ) { string key = "key-" + names[i]; diff --git a/ClassicalSharp/Game/Game.cs b/ClassicalSharp/Game/Game.cs index 35d71f752..1ade0e673 100644 --- a/ClassicalSharp/Game/Game.cs +++ b/ClassicalSharp/Game/Game.cs @@ -142,6 +142,12 @@ namespace ClassicalSharp { #else Graphics = new Direct3D9Api( this ); #endif + try { + Options.Load(); + } catch( IOException ) { + Utils.LogWarning( "Unable to load options.txt" ); + } + ViewDistance = Options.GetInt( "viewdist", 16, 8192, 512 ); defaultIb = Graphics.MakeDefaultIb(); ModelCache = new ModelCache( this ); ModelCache.InitCache(); @@ -201,6 +207,8 @@ namespace ClassicalSharp { public void SetViewDistance( int distance ) { ViewDistance = distance; Utils.LogDebug( "setting view distance to: " + distance ); + Options.Set( "viewdist", distance.ToString() ); + Options.Save(); Raise( ViewDistanceChanged ); UpdateProjection(); } @@ -332,10 +340,9 @@ namespace ClassicalSharp { if( activeScreen != null ) { activeScreen.Dispose(); } - if( activeScreen != null && activeScreen.HandlesAllInput ) { - Camera.RegrabMouse(); + if( activeScreen != null && activeScreen.HandlesAllInput ) lastClick = DateTime.UtcNow; - } + activeScreen = screen; if( screen != null ) { screen.game = this; @@ -381,7 +388,7 @@ namespace ClassicalSharp { Players.Dispose(); AsyncDownloader.Dispose(); if( writer != null ) { - writer.Close(); + writer.Dispose(); } if( activeScreen != null ) { activeScreen.Dispose(); diff --git a/ClassicalSharp/GraphicsAPI/VertexFormats.cs b/ClassicalSharp/GraphicsAPI/VertexFormats.cs index ba789b005..34d9bb076 100644 --- a/ClassicalSharp/GraphicsAPI/VertexFormats.cs +++ b/ClassicalSharp/GraphicsAPI/VertexFormats.cs @@ -68,12 +68,6 @@ namespace ClassicalSharp { R = c.R; G = c.G; B = c.B; A = c.A; } - public VertexPos3fTex2fCol4b( float x, float y, float z, float u, float v, Color c ) { - X = x; Y = y; Z = z; - U = u; V = v; - R = c.R; G = c.G; B = c.B; A = c.A; - } - public VertexPos3fTex2fCol4b( float x, float y, float z, float u, float v, byte r, byte g, byte b, byte a ) { X = x; Y = y; Z = z; U = u; V = v; diff --git a/ClassicalSharp/Rendering/StandardEnvRenderer.cs b/ClassicalSharp/Rendering/StandardEnvRenderer.cs index 5e8fd2f53..1f2f167ec 100644 --- a/ClassicalSharp/Rendering/StandardEnvRenderer.cs +++ b/ClassicalSharp/Rendering/StandardEnvRenderer.cs @@ -12,11 +12,17 @@ namespace ClassicalSharp.Renderers { map = game.Map; } - int cloudsVb = -1, cloudsIndices; - int skyOffset = 10, skyVb = -1, skyIndices; - public float CloudsSpeed = 1; + int cloudsVb = -1, cloudsIndices, skyVb = -1, skyIndices; + public float CloudsSpeed = 1; + public int CloudsOffset = 2; bool legacy; + public void SetCloudsOffset( int offset ) { + CloudsOffset = offset; + ResetClouds(); + ResetSky(); + } + public void SetUseLegacyMode( bool legacy ) { this.legacy = legacy; ResetSky(); @@ -27,7 +33,7 @@ namespace ClassicalSharp.Renderers { if( skyVb == -1 || cloudsVb == -1 ) return; Vector3 pos = game.LocalPlayer.EyePosition; - if( pos.Y < map.Height + skyOffset ) { + if( pos.Y < map.Height + CloudsOffset + 8 ) { graphics.BeginVbBatch( VertexFormat.Pos3fCol4b ); graphics.BindVb( skyVb ); graphics.DrawIndexedVb( DrawMode.Triangles, skyIndices, 0 ); @@ -35,11 +41,6 @@ namespace ClassicalSharp.Renderers { RenderClouds( deltaTime ); ResetFog(); } - - public void SetSkyOffset( int offset ) { - skyOffset = offset; - ResetSky(); - } protected override void CloudsColourChanged() { ResetClouds(); @@ -159,7 +160,7 @@ namespace ClassicalSharp.Renderers { cloudsIndices = Utils.CountIndices( x2 - x1, z2 - z1, axisSize ); VertexPos3fTex2fCol4b* vertices = stackalloc VertexPos3fTex2fCol4b[cloudsIndices / 6 * 4]; - DrawCloudsY( x1, z1, x2, z2, map.Height + 2, axisSize, map.CloudsCol, vertices ); + DrawCloudsY( x1, z1, x2, z2, map.Height + CloudsOffset, axisSize, map.CloudsCol, vertices ); cloudsVb = graphics.CreateVb( (IntPtr)vertices, VertexFormat.Pos3fTex2fCol4b, cloudsIndices / 6 * 4 ); } @@ -169,7 +170,7 @@ namespace ClassicalSharp.Renderers { skyIndices = Utils.CountIndices( x2 - x1, z2 - z1, axisSize ); VertexPos3fCol4b* vertices = stackalloc VertexPos3fCol4b[skyIndices / 6 * 4]; - DrawSkyY( x1, z1, x2, z2, map.Height + skyOffset, axisSize, map.SkyCol, vertices ); + DrawSkyY( x1, z1, x2, z2, map.Height + CloudsOffset + 8, axisSize, map.SkyCol, vertices ); skyVb = graphics.CreateVb( (IntPtr)vertices, VertexFormat.Pos3fCol4b, skyIndices / 6 * 4 ); } diff --git a/ClassicalSharp/Utils/Options.cs b/ClassicalSharp/Utils/Options.cs index b390edc83..3f20a6c25 100644 --- a/ClassicalSharp/Utils/Options.cs +++ b/ClassicalSharp/Utils/Options.cs @@ -13,6 +13,24 @@ namespace ClassicalSharp { return OptionsSet.TryGetValue( key, out value ) ? value : null; } + public static bool TryGetInt( string key, out int valueInt ) { + string value; + valueInt = 0; + OptionsSet.TryGetValue( key, out value ); + + if( String.IsNullOrEmpty( value ) ) return false; + return Int32.TryParse( value, out valueInt ); + } + + public static int GetInt( string key, int min, int max, int defValue ) { + int valueInt = 0; + if( TryGetInt( key, out valueInt ) ) { + Utils.Clamp( ref valueInt, min, max ); + return valueInt; + } + return defValue; + } + public static void Set( string key, string value ) { OptionsSet[key] = value; }