From c2bd4c7971a8df2cb8120f5cad91146c83613a92 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 29 Oct 2015 20:41:18 +1100 Subject: [PATCH] Store last entered text in new launcher, more comments in new launcher, don't scroll past last server unless the launcher can show all servers in the table. --- .../2D/Screens/BlockSelectScreen.cs | 8 ++-- .../2D/Widgets/BlockHotbarWidget.cs | 2 +- ClassicalSharp/GraphicsAPI/IGraphicsApi.cs | 8 ++-- ClassicalSharp/Physics/PickedPos.cs | 16 ++++--- Launcher2/Gui/PlatformDrawer.cs | 6 +-- Launcher2/Gui/Screens/ClassiCubeScreen.cs | 32 +++++++++++++- .../Gui/Screens/ClassiCubeServersScreen.cs | 17 +++----- Launcher2/Gui/Screens/DirectConnectScreen.cs | 36 ++++++++++++++-- Launcher2/Gui/Screens/LauncherInputScreen.cs | 4 +- Launcher2/Gui/Screens/MainScreen.cs | 14 +++---- Launcher2/Gui/Screens/ResourcesScreen.cs | 4 +- .../TableWidget/LauncherTableWidget.Input.cs | 10 ++--- .../Gui/TableWidget/LauncherTableWidget.cs | 26 ++++++++---- Launcher2/LauncherWindow.cs | 42 ++++++++++++++----- OpenTK/Platform/Windows/WinGLNative.cs | 3 +- 15 files changed, 160 insertions(+), 68 deletions(-) diff --git a/ClassicalSharp/2D/Screens/BlockSelectScreen.cs b/ClassicalSharp/2D/Screens/BlockSelectScreen.cs index c9fa99a9a..817afecf0 100644 --- a/ClassicalSharp/2D/Screens/BlockSelectScreen.cs +++ b/ClassicalSharp/2D/Screens/BlockSelectScreen.cs @@ -1,7 +1,7 @@ -using System; -using System.Drawing; -using ClassicalSharp.GraphicsAPI; -using OpenTK.Input; +using System; +using System.Drawing; +using ClassicalSharp.GraphicsAPI; +using OpenTK.Input; namespace ClassicalSharp { diff --git a/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs b/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs index 485d046a5..7111f4dc7 100644 --- a/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs +++ b/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs @@ -47,7 +47,7 @@ namespace ClassicalSharp { for( int i = 0; i < hotbarCount; i++ ) { int x = X + i * blockSize; IsometricBlockDrawer.Draw( game, (byte)game.Inventory.Hotbar[i], 10, - x + blockSize / 2, game.Height - 18 ); + x + blockSize / 2, game.Height - 19 ); if( i == game.Inventory.HeldBlockIndex ) selectedBlock.X1 = x; } diff --git a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs index 52117992b..7aa102273 100644 --- a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs +++ b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs @@ -1,7 +1,7 @@ -using System; -using System.Drawing; -using System.Drawing.Imaging; -using OpenTK; +using System; +using System.Drawing; +using System.Drawing.Imaging; +using OpenTK; namespace ClassicalSharp.GraphicsAPI { diff --git a/ClassicalSharp/Physics/PickedPos.cs b/ClassicalSharp/Physics/PickedPos.cs index 59d6970c6..90c572d95 100644 --- a/ClassicalSharp/Physics/PickedPos.cs +++ b/ClassicalSharp/Physics/PickedPos.cs @@ -8,16 +8,22 @@ namespace ClassicalSharp { /// Minimum world coordinates of the block's bounding box. public Vector3 Min; + /// Maximum world coordinates of the block's bounding box. - public Vector3 Max; + public Vector3 Max; + /// Integer world coordinates of the block. - public Vector3I BlockPos; + public Vector3I BlockPos; + /// Integer world coordinates of the neighbouring block that is closest to the client. - public Vector3I TranslatedPos; + public Vector3I TranslatedPos; + /// Whether this instance actually has a selected block currently. - public bool Valid = true; + public bool Valid = true; + /// Face of the picked block that is closet to the client. - public CpeBlockFace BlockFace; + public CpeBlockFace BlockFace; + /// Block type of this selected block. public byte BlockType; diff --git a/Launcher2/Gui/PlatformDrawer.cs b/Launcher2/Gui/PlatformDrawer.cs index bc1b31787..4b825e65d 100644 --- a/Launcher2/Gui/PlatformDrawer.cs +++ b/Launcher2/Gui/PlatformDrawer.cs @@ -1,12 +1,12 @@ using System; using System.Drawing; -using OpenTK; +using ClassicalSharp; using OpenTK.Platform; using OpenTK.Platform.X11; -using ClassicalSharp; namespace Launcher2 { + /// Platform specific class used to transfer a bitmap directly to the screen. public abstract class PlatformDrawer { public abstract void Init( IWindowInfo info ); @@ -48,7 +48,7 @@ namespace Launcher2 { } public override void Draw( IWindowInfo info, Bitmap framebuffer ) { - X11WindowInfo x11Info = info as X11WindowInfo; + X11WindowInfo x11Info = (X11WindowInfo)info; using( FastBitmap fastBmp = new FastBitmap( framebuffer, true ) ) { IntPtr image = API.XCreateImage( API.DefaultDisplay, x11Info.VisualInfo.Visual, 24, ImageFormat.ZPixmap, 0, fastBmp.Scan0, diff --git a/Launcher2/Gui/Screens/ClassiCubeScreen.cs b/Launcher2/Gui/Screens/ClassiCubeScreen.cs index 0924a20d3..e16ca49e7 100644 --- a/Launcher2/Gui/Screens/ClassiCubeScreen.cs +++ b/Launcher2/Gui/Screens/ClassiCubeScreen.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Drawing; using System.IO; using System.Net; @@ -44,11 +45,22 @@ namespace Launcher2 { } void LoadSavedInfo( IDrawer2D drawer ) { + Dictionary metadata; + // restore what user last typed into the various fields + if( game.ScreenMetadata.TryGetValue( "screen-CC", out metadata ) ) { + Set( 2, (string)metadata["user"] ); + Set( 3, (string)metadata["pass"] ); + } else { + LoadFromOptions(); + } + } + + void LoadFromOptions() { if( !Options.Load() ) return; - string user = Options.Get( "launcher-cc-username" ) ?? ""; - string pass = Options.Get( "launcher-cc-password" ) ?? ""; + string user = Options.Get( "launcher-cc-username" ) ?? "UserXYZ"; + string pass = Options.Get( "launcher-cc-password" ) ?? "PassXYZ"; pass = Secure.Decode( pass, user ); Set( 2, user ); @@ -166,5 +178,21 @@ namespace Launcher2 { SetStatus( text ); } } + + public override void Dispose() { + StoreFields(); + base.Dispose(); + } + + void StoreFields() { + Dictionary metadata; + if( !game.ScreenMetadata.TryGetValue( "screen-CC", out metadata ) ) { + metadata = new Dictionary(); + game.ScreenMetadata["screen-CC"] = metadata; + } + + metadata["user"] = Get( 2 ); + metadata["pass"] = Get( 3 ); + } } } diff --git a/Launcher2/Gui/Screens/ClassiCubeServersScreen.cs b/Launcher2/Gui/Screens/ClassiCubeServersScreen.cs index 4b75b7079..826b3debe 100644 --- a/Launcher2/Gui/Screens/ClassiCubeServersScreen.cs +++ b/Launcher2/Gui/Screens/ClassiCubeServersScreen.cs @@ -39,7 +39,7 @@ namespace Launcher2 { if( lastInput == widgets[1] ) { LauncherTableWidget table = (LauncherTableWidget)widgets[tableIndex]; table.FilterEntries( lastInput.Text ); - ClampIndex(); + table.ClampIndex(); Resize(); } } @@ -56,6 +56,9 @@ namespace Launcher2 { drawer.SetBitmap( game.Framebuffer ); drawer.Clear( game.clearColour ); Draw(); + LauncherTableWidget table = (LauncherTableWidget)widgets[tableIndex]; + table.ClampIndex(); + table.Redraw( drawer, inputFont, titleFont ); } Dirty = true; } @@ -85,7 +88,7 @@ namespace Launcher2 { void MakeTableWidget() { if( widgets[tableIndex] != null ) { - LauncherTableWidget table = widgets[tableIndex] as LauncherTableWidget; + LauncherTableWidget table = (LauncherTableWidget)widgets[tableIndex]; table.Redraw( drawer, inputFont, titleFont ); return; } @@ -115,18 +118,10 @@ namespace Launcher2 { void MouseWheelChanged( object sender, MouseWheelEventArgs e ) { LauncherTableWidget table = (LauncherTableWidget)widgets[tableIndex]; table.CurrentIndex -= e.Delta; - ClampIndex(); + table.ClampIndex(); Resize(); } - void ClampIndex() { - LauncherTableWidget table = (LauncherTableWidget)widgets[tableIndex]; - if( table.CurrentIndex >= table.Count ) - table.CurrentIndex = table.Count - 1; - if( table.CurrentIndex < 0 ) - table.CurrentIndex = 0; - } - string HashFilter( string input ) { // Server url look like http://www.classicube.net/server/play/aaaaa/ diff --git a/Launcher2/Gui/Screens/DirectConnectScreen.cs b/Launcher2/Gui/Screens/DirectConnectScreen.cs index d9b8ede50..b109c0c0e 100644 --- a/Launcher2/Gui/Screens/DirectConnectScreen.cs +++ b/Launcher2/Gui/Screens/DirectConnectScreen.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Drawing; using System.IO; using System.Net; @@ -20,7 +21,7 @@ namespace Launcher2 { Resize(); using( drawer ) { drawer.SetBitmap( game.Framebuffer ); - LoadSavedInfo( drawer ); + LoadSavedInfo(); } } @@ -28,11 +29,23 @@ namespace Launcher2 { } bool ccSkins; - void LoadSavedInfo( IDrawer2D drawer ) { + void LoadSavedInfo() { + Dictionary metadata; + // restore what user last typed into the various fields + if( game.ScreenMetadata.TryGetValue( "screen-DC", out metadata ) ) { + Set( 3, (string)metadata["user"] ); + Set( 4, (string)metadata["address"] ); + Set( 5, (string)metadata["mppass"] ); + } else { + LoadFromOptions(); + } + } + + void LoadFromOptions() { if( !Options.Load() ) return; - string user = Options.Get( "launcher-username" ) ?? ""; + string user = Options.Get( "launcher-username" ) ?? "UserXYZ"; string ip = Options.Get( "launcher-ip" ) ?? "127.0.0.1"; string port = Options.Get( "launcher-port" ) ?? "25565"; ccSkins = Options.GetBool( "launcher-ccskins", false ); @@ -130,5 +143,22 @@ namespace Launcher2 { ClientStartData data = new ClientStartData( Get( 3 ), mppass, ipPart, portPart ); Client.Start( data, ccSkins ); } + + public override void Dispose() { + StoreFields(); + base.Dispose(); + } + + void StoreFields() { + Dictionary metadata; + if( !game.ScreenMetadata.TryGetValue( "screen-DC", out metadata ) ) { + metadata = new Dictionary(); + game.ScreenMetadata["screen-DC"] = metadata; + } + + metadata["user"] = Get( 3 ); + metadata["address"] = Get( 4 ); + metadata["mppass"] = Get( 5 ); + } } } diff --git a/Launcher2/Gui/Screens/LauncherInputScreen.cs b/Launcher2/Gui/Screens/LauncherInputScreen.cs index 01c8a7124..4bca863fc 100644 --- a/Launcher2/Gui/Screens/LauncherInputScreen.cs +++ b/Launcher2/Gui/Screens/LauncherInputScreen.cs @@ -84,11 +84,11 @@ namespace Launcher2 { protected string Get( int index ) { LauncherWidget widget = widgets[index]; - return widget == null ? "" : ((widget as LauncherInputWidget)).Text; + return widget == null ? "" : ((LauncherInputWidget)widget).Text; } protected void Set( int index, string text ) { - (widgets[index] as LauncherInputWidget) + ((LauncherInputWidget)widgets[index]) .Redraw( drawer, text, inputFont ); } diff --git a/Launcher2/Gui/Screens/MainScreen.cs b/Launcher2/Gui/Screens/MainScreen.cs index d3e2432f0..c8b0af102 100644 --- a/Launcher2/Gui/Screens/MainScreen.cs +++ b/Launcher2/Gui/Screens/MainScreen.cs @@ -13,14 +13,14 @@ namespace Launcher2 { } protected override void UnselectWidget( LauncherWidget widget ) { - LauncherButtonWidget button = widget as LauncherButtonWidget; + LauncherButtonWidget button = (LauncherButtonWidget)widget; button.Active = false; button.Redraw( drawer, button.Text, textFont ); Dirty = true; } protected override void SelectWidget( LauncherWidget widget ) { - LauncherButtonWidget button = widget as LauncherButtonWidget; + LauncherButtonWidget button = (LauncherButtonWidget)widget; button.Active = true; button.Redraw( drawer, button.Text, textFont ); Dirty = true; @@ -59,14 +59,14 @@ namespace Launcher2 { Font textFont; void Draw() { widgetIndex = 0; - MakeButtonAt( "Direct connect", Anchor.Centre, Anchor.Centre, - buttonWidth, buttonHeight, 0, -100, - (x, y) => game.SetScreen( new DirectConnectScreen( game ) ) ); - MakeButtonAt( "ClassiCube.net", Anchor.Centre, Anchor.Centre, - buttonWidth, buttonHeight, 0, -50, + buttonWidth, buttonHeight, 0, -100, (x, y) => game.SetScreen( new ClassiCubeScreen( game ) ) ); + MakeButtonAt( "Direct connect", Anchor.Centre, Anchor.Centre, + buttonWidth, buttonHeight, 0, -50, + (x, y) => game.SetScreen( new DirectConnectScreen( game ) ) ); + MakeButtonAt( "Singleplayer", Anchor.LeftOrTop, Anchor.BottomOrRight, sideButtonWidth, buttonHeight, 10, -10, (x, y) => Client.Start( "default.zip" ) ); diff --git a/Launcher2/Gui/Screens/ResourcesScreen.cs b/Launcher2/Gui/Screens/ResourcesScreen.cs index 715020f5b..05aac9da5 100644 --- a/Launcher2/Gui/Screens/ResourcesScreen.cs +++ b/Launcher2/Gui/Screens/ResourcesScreen.cs @@ -86,7 +86,7 @@ namespace Launcher2 { string text = widgets[0] == null ? String.Format( format, ResourceFetcher.EstimateDownloadSize().ToString( "F2" ) ) - : (widgets[0] as LauncherLabelWidget).Text; + : ((LauncherLabelWidget)widgets[0]).Text; MakeTextAt( statusFont, text, 0, 5 ); // Clear the entire previous widgets state. @@ -110,7 +110,7 @@ namespace Launcher2 { } void SetStatus( string text ) { - LauncherLabelWidget widget = widgets[0] as LauncherLabelWidget; + LauncherLabelWidget widget = (LauncherLabelWidget)widgets[0]; using( drawer ) { drawer.SetBitmap( game.Framebuffer ); drawer.Clear( backCol, widget.X, widget.Y, widget.Width, widget.Height ); diff --git a/Launcher2/Gui/TableWidget/LauncherTableWidget.Input.cs b/Launcher2/Gui/TableWidget/LauncherTableWidget.Input.cs index c7a5373ba..4bbca8a19 100644 --- a/Launcher2/Gui/TableWidget/LauncherTableWidget.Input.cs +++ b/Launcher2/Gui/TableWidget/LauncherTableWidget.Input.cs @@ -17,16 +17,16 @@ namespace Launcher2 { return; } - if( mouseY >= HeaderStartY && mouseY < HeaderEndY ) { - if( mouseX < ColumnWidths[0] - 10 ) { - nameComp.Invert = !nameComp.Invert; + if( mouseY >= headerStartY && mouseY < headerEndY ) { + if( mouseX < ColumnWidths[0] - 10 ) { Array.Sort( usedEntries, 0, Count, nameComp ); Array.Sort( entries, 0, entries.Length, nameComp ); + nameComp.Invert = !nameComp.Invert; NeedRedraw(); - } else if( mouseX > ColumnWidths[0] + 10 ) { - playerComp.Invert = !playerComp.Invert; + } else if( mouseX > ColumnWidths[0] + 10 ) { Array.Sort( usedEntries, 0, Count, playerComp ); Array.Sort( entries, 0, entries.Length, playerComp ); + playerComp.Invert = !playerComp.Invert; NeedRedraw(); } else { DraggingWidth = true; diff --git a/Launcher2/Gui/TableWidget/LauncherTableWidget.cs b/Launcher2/Gui/TableWidget/LauncherTableWidget.cs index fa49a073e..332497b13 100644 --- a/Launcher2/Gui/TableWidget/LauncherTableWidget.cs +++ b/Launcher2/Gui/TableWidget/LauncherTableWidget.cs @@ -81,12 +81,12 @@ namespace Launcher2 { DrawTextArgs args = new DrawTextArgs( header, titleFont, true ); TableEntry headerEntry = default( TableEntry ); DrawColumnEntry( drawer, ref args, maxWidth, x, ref y, ref headerEntry ); - MaxIndex = Count; + maxIndex = Count; for( int i = CurrentIndex; i < Count; i++ ) { args = new DrawTextArgs( filter( usedEntries[i] ), font, true ); if( !DrawColumnEntry( drawer, ref args, maxWidth, x, ref y, ref usedEntries[i] ) ) { - MaxIndex = i; + maxIndex = i; break; } } @@ -112,36 +112,46 @@ namespace Launcher2 { return true; } - int HeaderStartY, HeaderEndY; + int headerStartY, headerEndY; + int numEntries = 0; void DrawGrid( IDrawer2D drawer, Font font, Font titleFont ) { DrawTextArgs args = new DrawTextArgs( "I", titleFont, true ); Size size = drawer.MeasureSize( ref args ); drawer.DrawRect( foreCol, 0, Y, Window.Width, 5 ); drawer.DrawRect( foreCol, 0, Y + size.Height + 10, Window.Width, 3 ); - HeaderStartY = Y; - HeaderEndY = Y + size.Height + 10; + headerStartY = Y; + headerEndY = Y + size.Height + 10; args = new DrawTextArgs( "I", font, true ); int y = Y + size.Height + 10; size = drawer.MeasureSize( ref args ); - for( ; ; ) { + numEntries = 0; + for( ; ; ) { if( y + size.Height > Window.Height ) break; + numEntries++; drawer.DrawRect( foreCol, 0, y, Window.Width, 1 ); y += size.Height + 5; } } - int MaxIndex; + int maxIndex; void DrawScrollbar( IDrawer2D drawer ) { drawer.DrawRect( backCol, Window.Width - 10, Y, 10, Window.Height - Y ); float scale = (Window.Height - 10 - Y) / (float)Count; int y1 = (int)(Y + CurrentIndex * scale); - int height = (int)((MaxIndex - CurrentIndex) * scale); + int height = (int)((maxIndex - CurrentIndex) * scale); drawer.DrawRect( scrollCol, Window.Width - 10, y1, 10, height ); } + public void ClampIndex() { + if( CurrentIndex >= Count - numEntries ) + CurrentIndex = Count - numEntries; + if( CurrentIndex < 0 ) + CurrentIndex = 0; + } + class NameComparer : IComparer { public bool Invert = false; diff --git a/Launcher2/LauncherWindow.cs b/Launcher2/LauncherWindow.cs index 81d78cfde..99a5dd88c 100644 --- a/Launcher2/LauncherWindow.cs +++ b/Launcher2/LauncherWindow.cs @@ -1,28 +1,50 @@ using System; +using System.Collections.Generic; using System.Drawing; +using System.Net; using System.Threading; using ClassicalSharp; using ClassicalSharp.Network; using OpenTK; using OpenTK.Graphics; -using System.Net; namespace Launcher2 { public sealed class LauncherWindow { + /// Underlying native window instance. public NativeWindow Window; + + /// Platform specific class used to draw 2D elements, + /// such as text, rounded rectangles and lines. public IDrawer2D Drawer; - public LauncherScreen screen; + + /// Currently active screen. + public LauncherScreen Screen; + + /// Whether the client drawing area needs to be redrawn/presented to the screen. public bool Dirty; + + /// Currently active logged in session with classicube.net. public ClassicubeSession Session = new ClassicubeSession(); + + /// Queue used to download resources asynchronously. public AsyncDownloader Downloader; + /// Returns the width of the client drawing area. public int Width { get { return Window.Width; } } + /// Returns the height of the client drawing area. public int Height { get { return Window.Height; } } + /// Bitmap that contains the entire array of pixels that describe the client drawing area. public Bitmap Framebuffer; + + /// Contains metadata attached for different screen instances, + /// typically used to save 'last text entered' text when a screen is disposed. + public Dictionary> ScreenMetadata = + new Dictionary>(); + Font logoFont, logoItalicFont; PlatformDrawer platformDrawer; public void Init() { @@ -42,21 +64,21 @@ namespace Launcher2 { void FocusedChanged( object sender, EventArgs e ) { MakeBackground(); - screen.Resize(); + Screen.Resize(); } void Resize( object sender, EventArgs e ) { platformDrawer.Resize( Window.WindowInfo ); MakeBackground(); - screen.Resize(); + Screen.Resize(); } public void SetScreen( LauncherScreen screen ) { - if( this.screen != null ) - this.screen.Dispose(); + if( this.Screen != null ) + this.Screen.Dispose(); MakeBackground(); - this.screen = screen; + this.Screen = screen; screen.Init(); } @@ -90,8 +112,8 @@ namespace Launcher2 { Window.ProcessEvents(); if( !Window.Exists ) break; - screen.Tick(); - if( Dirty || screen.Dirty ) + Screen.Tick(); + if( Dirty || Screen.Dirty ) Display(); Thread.Sleep( 1 ); } @@ -99,7 +121,7 @@ namespace Launcher2 { void Display() { Dirty = false; - screen.Dirty = false; + Screen.Dirty = false; platformDrawer.Draw( Window.WindowInfo, Framebuffer ); } diff --git a/OpenTK/Platform/Windows/WinGLNative.cs b/OpenTK/Platform/Windows/WinGLNative.cs index 4dd4680d0..d81ec7da1 100644 --- a/OpenTK/Platform/Windows/WinGLNative.cs +++ b/OpenTK/Platform/Windows/WinGLNative.cs @@ -536,7 +536,8 @@ namespace OpenTK.Platform.Windows icon = value; if (window.WindowHandle != IntPtr.Zero) { - API.SendMessage(window.WindowHandle, WindowMessage.SETICON, (IntPtr)0, icon == null ? IntPtr.Zero : value.Handle); + Icon small = new Icon( value, 16, 16 ); + API.SendMessage(window.WindowHandle, WindowMessage.SETICON, (IntPtr)0, icon == null ? IntPtr.Zero : small.Handle); API.SendMessage(window.WindowHandle, WindowMessage.SETICON, (IntPtr)1, icon == null ? IntPtr.Zero : value.Handle); } }