Simplify chat file opening method, simplify pause screen.

This commit is contained in:
UnknownShadow200 2015-06-28 06:49:03 +10:00
parent 83b2228896
commit 67aeba44b8
4 changed files with 42 additions and 54 deletions

View File

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

View File

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

View File

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

View File

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