Show warning screen for first time a texture pack is sent to the client. Fix bugs with being able to press B in both error and map loading screens.

This commit is contained in:
UnknownShadow200 2015-11-14 17:56:36 +11:00
parent 4f546b80d5
commit 97a38f3cf4
8 changed files with 193 additions and 24 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Drawing;
using OpenTK.Input;
namespace ClassicalSharp {
@ -52,5 +53,19 @@ namespace ClassicalSharp {
public override bool HidesHud {
get { return true; }
}
public override bool HandlesKeyDown( Key key ) { return true; }
public override bool HandlesKeyPress( char key ) { return true; }
public override bool HandlesKeyUp( Key key ) { return true; }
public override bool HandlesMouseClick( int mouseX, int mouseY, MouseButton button ) { return true; }
public override bool HandlesMouseMove( int mouseX, int mouseY ) { return true; }
public override bool HandlesMouseScroll( int delta ) { return true; }
public override bool HandlesMouseUp( int mouseX, int mouseY, MouseButton button ) { return true; }
}
}

View File

@ -1,5 +1,6 @@
using System;
using System.Drawing;
using OpenTK.Input;
namespace ClassicalSharp {
@ -77,5 +78,19 @@ namespace ClassicalSharp {
public override bool HidesHud {
get { return true; }
}
public override bool HandlesKeyDown( Key key ) { return true; }
public override bool HandlesKeyPress( char key ) { return true; }
public override bool HandlesKeyUp( Key key ) { return true; }
public override bool HandlesMouseClick( int mouseX, int mouseY, MouseButton button ) { return true; }
public override bool HandlesMouseMove( int mouseX, int mouseY ) { return true; }
public override bool HandlesMouseScroll( int delta ) { return true; }
public override bool HandlesMouseUp( int mouseX, int mouseY, MouseButton button ) { return true; }
}
}

View File

@ -1,13 +1,23 @@
using System;
using System.Drawing;
using OpenTK.Input;
namespace ClassicalSharp {
// TODO: get and set activescreen.
public sealed class WarningScreen : MenuScreen {
public WarningScreen( Game game ) : base( game ) {
public WarningScreen( Game game, object metadata, Action<object> yesClick,
Action<object> noClick, string title, params string[] body ) : base( game ) {
this.Metadata = metadata;
this.yesClick = yesClick;
this.noClick = noClick;
this.title = title;
this.body = body;
}
internal Screen lastScreen;
internal bool lastCursorVisible;
readonly string title;
readonly string[] body;
public override void Init() {
titleFont = new Font( "Arial", 16, FontStyle.Bold );
@ -19,21 +29,41 @@ namespace ClassicalSharp {
ButtonWidget.Create( game, 60, 30, 60, 20, "No", Anchor.Centre,
Anchor.Centre, titleFont, OnNoClick ),
};
labels = new TextWidget[] {
TextWidget.Create( game, 0, -120, "Do you want to XYZ?",
Anchor.Centre, Anchor.Centre, titleFont ),
TextWidget.Create( game, 0, -70, "Warning text here",
Anchor.Centre, Anchor.Centre, regularFont ),
};
labels = new TextWidget[body.Length + 1];
labels[0] = TextWidget.Create( game, 0, -120, title,
Anchor.Centre, Anchor.Centre, titleFont );
for( int i = 0; i < body.Length; i++ ) {
labels[i + 1] = TextWidget.Create( game, 0, -70 + 20 * i, body[i],
Anchor.Centre, Anchor.Centre, regularFont );
}
}
TextWidget[] labels;
Action<object> yesClick, noClick;
void OnYesClick( Game g, Widget w ) {
game.SetNewScreen( null );
if( yesClick != null )
yesClick( Metadata );
Dispose();
CloseScreen();
}
void OnNoClick( Game g, Widget w ) {
game.SetNewScreen( null );
if( noClick != null )
noClick( Metadata );
Dispose();
CloseScreen();
}
void CloseScreen() {
game.WarningScreens.RemoveAt( 0 );
if( game.WarningScreens.Count > 0 ) {
game.activeScreen = game.WarningScreens[0];
} else {
game.activeScreen = lastScreen;
if( game.CursorVisible != lastCursorVisible )
game.CursorVisible = lastCursorVisible;
}
}
public override void Render( double delta ) {
@ -50,5 +80,11 @@ namespace ClassicalSharp {
for( int i = 0; i < labels.Length; i++ )
labels[i].OnResize( oldWidth, oldHeight, width, height );
}
public override void Dispose() {
base.Dispose();
for( int i = 0; i < labels.Length; i++ )
labels[i].Dispose();
}
}
}

View File

@ -211,6 +211,7 @@
<Compile Include="Singleplayer\Commands.cs" />
<Compile Include="Singleplayer\Physics.cs" />
<Compile Include="Singleplayer\Server.cs" />
<Compile Include="TexturePack\AcceptedUrls.cs" />
<Compile Include="TexturePack\Animations.cs" />
<Compile Include="TexturePack\TextureCache.cs" />
<Compile Include="TexturePack\TerrainAtlas1D.cs" />

View File

@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Net;
@ -75,6 +76,8 @@ namespace ClassicalSharp {
internal int CloudsTextureId, RainTextureId, SnowTextureId;
internal bool screenshotRequested;
public Bitmap FontBitmap;
internal List<WarningScreen> WarningScreens = new List<WarningScreen>();
internal AcceptedUrls AcceptedUrls = new AcceptedUrls();
string defTexturePack = "default.zip";
public string DefaultTexturePack {
@ -109,6 +112,7 @@ namespace ClassicalSharp {
Players = new EntityList( this );
Options.Load();
AcceptedUrls.Load();
ViewDistance = Options.GetInt( OptionsKey.ViewDist, 16, 4096, 512 );
InputHandler = new InputHandler( this );
Chat = new ChatLog( this );
@ -192,11 +196,6 @@ namespace ClassicalSharp {
UpdateProjection();
}
public void RefreshHud() {
hudScreen.Dispose();
hudScreen.Init();
}
/// <summary> Gets whether the active screen handles all input. </summary>
public bool ScreenLockedInput {
get { return activeScreen == null ? hudScreen.HandlesAllInput :
@ -335,6 +334,18 @@ namespace ClassicalSharp {
internal Screen activeScreen;
public void SetNewScreen( Screen screen ) {
// don't switch to the new screen immediately if the user
// is currently looking at a warning dialog.
if( activeScreen is WarningScreen ) {
WarningScreen warning = (WarningScreen)activeScreen;
if( warning.lastScreen != null )
warning.lastScreen.Dispose();
warning.lastScreen = screen;
if( warning.lastScreen != null )
screen.Init();
return;
}
InputHandler.ScreenChanged( activeScreen, screen );
if( activeScreen != null )
activeScreen.Dispose();
@ -350,6 +361,25 @@ namespace ClassicalSharp {
activeScreen = screen;
}
public void RefreshHud() {
hudScreen.Dispose();
hudScreen.Init();
}
public void ShowWarning( WarningScreen screen ) {
if( !(activeScreen is WarningScreen) ) {
screen.lastScreen = activeScreen;
activeScreen = screen;
screen.lastCursorVisible = CursorVisible;
if( !CursorVisible) CursorVisible = true;
} else {
screen.lastCursorVisible = WarningScreens[0].lastCursorVisible;
}
WarningScreens.Add( screen );
screen.Init();
}
public void SetCamera( bool thirdPerson ) {
PerspectiveCamera oldCam = (PerspectiveCamera)Camera;
Camera = (thirdPerson && CanUseThirdPersonCamera) ?

View File

@ -290,18 +290,31 @@ namespace ClassicalSharp {
TexturePackExtractor extractor = new TexturePackExtractor();
extractor.Extract( game.DefaultTexturePack, game );
} else if( Utils.IsUrlPrefix( url ) ) {
game.Animations.Dispose();
DateTime lastModified = TextureCache.GetLastModifiedFromCache( url );
if( usingTexturePack )
game.AsyncDownloader.DownloadData( url, true, "texturePack", lastModified );
else
game.AsyncDownloader.DownloadImage( url, true, "terrain", lastModified );
if( !game.AcceptedUrls.HasAccepted( url ) ) {
game.ShowWarning( new WarningScreen(
game, url, DownloadTexturePack, null,
"Do you want to download the server's texture pack?",
"The texture pack is located at:", url ) );
} else {
DownloadTexturePack( url );
}
}
Utils.LogDebug( "Image url: " + url );
}
void DownloadTexturePack( object metadata ) {
string url = (string)metadata;
game.Animations.Dispose();
DateTime lastModified = TextureCache.GetLastModifiedFromCache( url );
if( !game.AcceptedUrls.HasAccepted( url ) )
game.AcceptedUrls.AddAccepted( url );
if( usingTexturePack )
game.AsyncDownloader.DownloadData( url, true, "texturePack", lastModified );
else
game.AsyncDownloader.DownloadImage( url, true, "terrain", lastModified );
}
void HandleCpeEnvWeatherType() {
game.Map.SetWeather( (Weather)reader.ReadUInt8() );
}

View File

@ -0,0 +1,59 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace ClassicalSharp {
public sealed class AcceptedUrls {
List<string> acceptedUrls = new List<string>();
const string folder = "texturecache", file = "acceptedurls.txt";
public void AddAccepted( string url ) {
acceptedUrls.Add( url );
Save();
}
public bool HasAccepted( string url ) {
return acceptedUrls.Contains( url );
}
public bool Load() {
try {
using( Stream fs = File.OpenRead( Path.Combine( folder, file ) ) )
using( StreamReader reader = new StreamReader( fs, false ) )
{
string line;
while( (line = reader.ReadLine()) != null ) {
if( line.Length == 0 && line[0] == '#' ) continue;
acceptedUrls.Add( line );
}
}
return true;
} catch( FileNotFoundException ) {
return true;
} catch( IOException ex ) {
ErrorHandler.LogError( "loading accepted urls", ex );
return false;
}
}
public bool Save() {
try {
if( !Directory.Exists( folder ) )
Directory.CreateDirectory( folder );
using( Stream fs = File.Create( Path.Combine( folder, file ) ) )
using( StreamWriter writer = new StreamWriter( fs ) )
{
foreach( string value in acceptedUrls )
writer.WriteLine( value );
}
return true;
} catch( IOException ex ) {
ErrorHandler.LogError( "saving accepted urls", ex );
return false;
}
}
}
}

View File

@ -97,7 +97,7 @@ namespace ClassicalSharp {
static void LoadFrom( StreamReader reader ) {
string line;
while( ( line = reader.ReadLine() ) != null ) {
while( (line = reader.ReadLine()) != null ) {
if( line.Length == 0 && line[0] == '#' ) continue;
int separatorIndex = line.IndexOf( '=' );