From 67aeba44b8e17956725c072d1b6dc541ca3faeb0 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 28 Jun 2015 06:49:03 +1000 Subject: [PATCH] Simplify chat file opening method, simplify pause screen. --- 2D/Screens/PauseScreen.cs | 21 ++++++--------- 2D/Widgets/TextWidget.cs | 9 ++----- 2D/Widgets/Widget.cs | 11 +++++--- Game/Game.Chat.cs | 55 ++++++++++++++++++--------------------- 4 files changed, 42 insertions(+), 54 deletions(-) diff --git a/2D/Screens/PauseScreen.cs b/2D/Screens/PauseScreen.cs index 63f15d53c..21343f53a 100644 --- a/2D/Screens/PauseScreen.cs +++ b/2D/Screens/PauseScreen.cs @@ -41,34 +41,29 @@ namespace ClassicalSharp { gameWidget = TextWidget.Create( Window, 0, -50, "&eBack to game", Docking.Centre, Docking.BottomOrRight, titleFont ); exitWidget = TextWidget.Create( Window, 0, -10, "&eExit", Docking.Centre, Docking.BottomOrRight, titleFont ); - KeyMapping[] mappingsLeft = { KeyMapping.Forward, KeyMapping.Back, KeyMapping.Left, KeyMapping.Right, - KeyMapping.Jump, KeyMapping.Respawn, KeyMapping.SetSpawn, KeyMapping.OpenChat, KeyMapping.SendChat, - KeyMapping.PauseOrExit, KeyMapping.OpenInventory }; string[] descriptionsLeft = { "Forward", "Back", "Left", "Right", "Jump", "Respawn", "Set spawn", "Open chat", "Send chat", "Pause", "Open inventory" }; - MakeKeys( mappingsLeft, descriptionsLeft, 10, out keysLeft ); + MakeKeys( KeyMapping.Forward, descriptionsLeft, 10, out keysLeft ); leftEnd = CalculateMaxWidth( keysLeft ); - KeyMapping[] mappingsRight = { KeyMapping.Screenshot, KeyMapping.Fullscreen, KeyMapping.ThirdPersonCamera, - KeyMapping.VSync, KeyMapping.ViewDistance, KeyMapping.Fly, KeyMapping.Speed, KeyMapping.NoClip, - KeyMapping.FlyUp, KeyMapping.FlyDown, KeyMapping.PlayerList }; string[] descriptionsRight = { "Take screenshot", "Toggle fullscreen", "Toggle 3rd person camera", "Toggle VSync", "Change view distance", "Toggle fly", "Speed", "Toggle noclip", "Fly up", "Fly down", "Display player list" }; - MakeKeys( mappingsRight, descriptionsRight, leftEnd + 30, out keysRight ); + MakeKeys( KeyMapping.Screenshot, descriptionsRight, leftEnd + 30, out keysRight ); } int leftEnd; - void MakeKeys( KeyMapping[] mappings, string[] descriptions, int offset, out KeyMapWidget[] widgets ) { + void MakeKeys( KeyMapping start, string[] descriptions, int offset, out KeyMapWidget[] widgets ) { int startY = controlsWidget.BottomRight.Y + 10; - widgets = new KeyMapWidget[mappings.Length]; + widgets = new KeyMapWidget[descriptions.Length]; - for( int i = 0; i < keysLeft.Length; i++ ) { - Key tkKey = Window.Keys[mappings[i]]; + for( int i = 0; i < widgets.Length; i++ ) { + KeyMapping mapping = (KeyMapping)( (int)start + i ); + Key tkKey = Window.Keys[mapping]; string text = descriptions[i] + ": " + keyNames[(int)tkKey]; TextWidget widget = TextWidget.Create( Window, 0, startY, text, Docking.LeftOrTop, Docking.LeftOrTop, textFont ); widget.XOffset = offset; widget.MoveTo( widget.X + widget.XOffset, widget.Y ); - widgets[i] = new KeyMapWidget( widget, mappings[i], descriptions[i] ); + widgets[i] = new KeyMapWidget( widget, mapping, descriptions[i] ); startY += widget.Height + 5; } } diff --git a/2D/Widgets/TextWidget.cs b/2D/Widgets/TextWidget.cs index ea9631a3f..f45531a1b 100644 --- a/2D/Widgets/TextWidget.cs +++ b/2D/Widgets/TextWidget.cs @@ -47,17 +47,12 @@ namespace ClassicalSharp { parts = Utils2D.SplitText( GraphicsApi, text, true ); size = Utils2D.MeasureSize( parts, font, true ); - X = CalcAdjOffset( XOffset, Window.Width, size.Width, HorizontalDocking ); - Y = CalcAdjOffset( YOffset, Window.Height, size.Height, VerticalDocking ); + X = CalcOffset( Window.Width, size.Width, XOffset, HorizontalDocking ); + Y = CalcOffset( Window.Height, size.Height, YOffset, VerticalDocking ); texture = Utils2D.MakeTextTexture( parts, font, size, X, Y ); UpdateDimensions(); } - int CalcAdjOffset( int offset, int totalSize, int axisSize, Docking mode ) { - if( mode == Docking.LeftOrTop ) return offset; - if( mode == Docking.BottomOrRight) return totalSize - axisSize + offset; - return ( totalSize - axisSize ) / 2 + offset; - } public string GetText() { return textCache; } diff --git a/2D/Widgets/Widget.cs b/2D/Widgets/Widget.cs index f1921148c..6b83ea05d 100644 --- a/2D/Widgets/Widget.cs +++ b/2D/Widgets/Widget.cs @@ -107,10 +107,13 @@ namespace ClassicalSharp { } protected static int CalcDelta( int newVal, int oldVal, Docking mode ) { - if( mode == Docking.LeftOrTop ) return 0; - if( mode == Docking.BottomOrRight) return newVal - oldVal; - if( mode == Docking.Centre ) return ( newVal - oldVal ) / 2; - throw new NotSupportedException( "Unsupported docking mode: " + mode ); + return CalcOffset( newVal, oldVal, 0, mode ); + } + + protected static int CalcOffset( int axisSize, int elemSize, int offset, Docking mode ) { + if( mode == Docking.LeftOrTop ) return offset; + if( mode == Docking.BottomOrRight) return axisSize - elemSize + offset; + return ( axisSize - elemSize ) / 2 + offset; } } diff --git a/Game/Game.Chat.cs b/Game/Game.Chat.cs index 242f0f76a..035c20c58 100644 --- a/Game/Game.Chat.cs +++ b/Game/Game.Chat.cs @@ -76,40 +76,12 @@ namespace ClassicalSharp { Directory.CreateDirectory( "logs" ); } - if( now.Day != last.Day || now.Month != last.Month || now.Year != last.Year ) { + if( now.Day != last.Day || now.Month != last.Month || now.Year != last.Year ) { if( writer != null ) { writer.Close(); writer = null; } - - // Cheap way of ensuring multiple instances do not end up overwriting each other's log entries. - int counter = 0; - while( true ) { - string id = counter == 0 ? "" : " _" + counter; - string fileName = "chat-" + now.ToString( fileNameFormat ) + id + ".log"; - string path = Path.Combine( "logs", fileName ); - FileStream stream = null; - try { - stream = File.Open( path, FileMode.Append, FileAccess.Write, FileShare.Read ); - } catch( IOException ex ) { - if( !ex.Message.Contains( "because it is being used by another process" ) ) { - throw; - } - if( stream != null ) { - stream.Close(); - } - counter++; - if( counter >= 20 ) { - Utils.LogError( "Failed to open or create a chat log file after 20 tries, giving up." ); - break; - } - continue; - } - Utils.LogDebug( "opening chat with id:" + id ); - writer = new StreamWriter( stream ); - writer.AutoFlush = true; - break; - } + OpenChatFile( now ); last = now; } @@ -119,5 +91,28 @@ namespace ClassicalSharp { writer.WriteLine( entry ); } } + + void OpenChatFile( DateTime now ) { + // Cheap way of ensuring multiple instances do not end up overwriting each other's log entries. + for( int i = 0; i < 20; i++ ) { + string id = i == 0 ? "" : " _" + i; + string fileName = "chat-" + now.ToString( fileNameFormat ) + id + ".log"; + string path = Path.Combine( "logs", fileName ); + FileStream stream = null; + try { + stream = File.Open( path, FileMode.Append, FileAccess.Write, FileShare.Read ); + } catch( IOException ex ) { + if( !ex.Message.Contains( "because it is being used by another process" ) ) { + throw; + } + continue; + } + Utils.LogDebug( "opening chat with id:" + id ); + writer = new StreamWriter( stream ); + writer.AutoFlush = true; + return; + } + Utils.LogError( "Failed to open or create a chat log file after 20 tries, giving up." ); + } } } \ No newline at end of file