Add button to overwrite existing maps in singleplayer (Thanks arrco)

This commit is contained in:
UnknownShadow200 2015-12-22 23:59:35 +11:00
parent a5d91ca5cb
commit 52ac0e55df

View File

@ -29,10 +29,12 @@ namespace ClassicalSharp {
} }
public override bool HandlesKeyPress( char key ) { public override bool HandlesKeyPress( char key ) {
RemoveOverwriteButton();
return inputWidget.HandlesKeyPress( key ); return inputWidget.HandlesKeyPress( key );
} }
public override bool HandlesKeyDown( Key key ) { public override bool HandlesKeyDown( Key key ) {
RemoveOverwriteButton();
if( key == Key.Escape ) { if( key == Key.Escape ) {
game.SetNewScreen( null ); game.SetNewScreen( null );
return true; return true;
@ -56,6 +58,7 @@ namespace ClassicalSharp {
buttons = new [] { buttons = new [] {
ButtonWidget.Create( game, 260, 50, 60, 30, "Save", Anchor.Centre, ButtonWidget.Create( game, 260, 50, 60, 30, "Save", Anchor.Centre,
Anchor.Centre, titleFont, OkButtonClick ), Anchor.Centre, titleFont, OkButtonClick ),
null,
MakeBack( false, titleFont, MakeBack( false, titleFont,
(g, w) => g.SetNewScreen( new PauseScreen( g ) ) ), (g, w) => g.SetNewScreen( new PauseScreen( g ) ) ),
}; };
@ -83,18 +86,40 @@ namespace ClassicalSharp {
text = Path.ChangeExtension( text, ".cw" ); text = Path.ChangeExtension( text, ".cw" );
if( File.Exists( text ) ) { if( File.Exists( text ) ) {
MakeDescWidget( "&eFilename already exists" ); buttons[1] = ButtonWidget.Create( game, 0, 90, 260, 30, "Overwrite existing?",
Anchor.Centre, Anchor.Centre, titleFont, OverwriteButtonClick );
} else { } else {
SetPath( text );
RemoveOverwriteButton();
}
}
void OverwriteButtonClick( Game game, Widget widget ) {
string text = inputWidget.GetText();
text = Path.ChangeExtension( text, ".cw" );
SetPath( text );
RemoveOverwriteButton();
}
void SetPath( string text ) {
// NOTE: We don't immediately save here, because otherwise the 'saving...' // NOTE: We don't immediately save here, because otherwise the 'saving...'
// will not be rendered in time because saving is done on the main thread. // will not be rendered in time because saving is done on the main thread.
MakeDescWidget( "Saving.." ); MakeDescWidget( "Saving.." );
textPath = text; textPath = text;
} }
void RemoveOverwriteButton() {
if( buttons[1] == null ) return;
buttons[1].Dispose();
buttons[1] = null;
} }
string textPath; string textPath;
void SaveMap( string path ) { void SaveMap( string path ) {
try { try {
if( File.Exists( path ) )
File.Delete( path );
using( FileStream fs = new FileStream( path, FileMode.CreateNew, FileAccess.Write ) ) { using( FileStream fs = new FileStream( path, FileMode.CreateNew, FileAccess.Write ) ) {
IMapFileFormat map = new MapCw(); IMapFileFormat map = new MapCw();
map.Save( fs, game ); map.Save( fs, game );
@ -104,6 +129,7 @@ namespace ClassicalSharp {
MakeDescWidget( "&cError while trying to save map" ); MakeDescWidget( "&cError while trying to save map" );
return; return;
} }
game.Chat.Add( "&eSaved map to: " + path );
game.SetNewScreen( new PauseScreen( game ) ); game.SetNewScreen( new PauseScreen( game ) );
} }