Fix crashing when pressing button in OptionsScreen, save and load default view distance, other minor gui touchups.

This commit is contained in:
UnknownShadow200 2015-09-24 19:39:51 +10:00
parent e01f24bebb
commit fe1d808025
13 changed files with 97 additions and 57 deletions

View File

@ -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 ),

View File

@ -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<ButtonWidget>( 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<ButtonWidget>( 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 );

View File

@ -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 ) ),

View File

@ -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<Game, ButtonWidget> 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;
}
}
}

View File

@ -75,6 +75,7 @@ namespace ClassicalSharp {
hotbar.Init();
if( game.CursorVisible )
game.CursorVisible = false;
game.Camera.RegrabMouse();
}
public override bool HandlesAllInput {

View File

@ -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;

View File

@ -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";
}
}
}

View File

@ -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 );

View File

@ -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];

View File

@ -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();

View File

@ -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;

View File

@ -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 );
}

View File

@ -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;
}