Persist key bindings, fixes #81.

This commit is contained in:
UnknownShadow200 2015-09-22 06:42:17 +10:00
parent 8a076102f4
commit 7271e02f6f
3 changed files with 110 additions and 9 deletions

View File

@ -168,6 +168,7 @@
<Compile Include="Utils\Camera.cs" /> <Compile Include="Utils\Camera.cs" />
<Compile Include="Utils\FastBitmap.cs" /> <Compile Include="Utils\FastBitmap.cs" />
<Compile Include="Utils\FastColour.cs" /> <Compile Include="Utils\FastColour.cs" />
<Compile Include="Utils\Options.cs" />
<Compile Include="Utils\TextureRectangle.cs" /> <Compile Include="Utils\TextureRectangle.cs" />
<Compile Include="Utils\StringBuffer.cs" /> <Compile Include="Utils\StringBuffer.cs" />
<Compile Include="Utils\Utils.cs" /> <Compile Include="Utils\Utils.cs" />

View File

@ -1,7 +1,8 @@
using System; using System;
using ClassicalSharp.Particles; using System.IO;
using OpenTK; using ClassicalSharp.Particles;
using OpenTK.Input; using OpenTK;
using OpenTK.Input;
namespace ClassicalSharp { namespace ClassicalSharp {
@ -195,15 +196,15 @@ namespace ClassicalSharp {
public enum KeyMapping { public enum KeyMapping {
Forward, Back, Left, Right, Jump, Respawn, SetSpawn, OpenChat, Forward, Back, Left, Right, Jump, Respawn, SetSpawn, OpenChat,
SendChat, PauseOrExit, OpenInventory, Screenshot, Fullscreen, VSync, SendChat, PauseOrExit, OpenInventory, Screenshot, Fullscreen, VSync,
ThirdPersonCamera, ViewDistance, Fly, Speed, NoClip, FlyUp, ThirdPersonCamera, ViewDistance, Fly, Speed, NoClip, FlyUp,
FlyDown, PlayerList, HideGui, FlyDown, PlayerList, HideGui,
} }
public class KeyMap { public class KeyMap {
public Key this[KeyMapping key] { public Key this[KeyMapping key] {
get { return Keys[(int)key]; } get { return Keys[(int)key]; }
set { Keys[(int)key] = value; } set { Keys[(int)key] = value; SaveKeyBindings(); }
} }
Key[] Keys; Key[] Keys;
@ -229,18 +230,59 @@ namespace ClassicalSharp {
Keys = new Key[] { Keys = new Key[] {
Key.W, Key.S, Key.A, Key.D, Key.Space, Key.R, Key.Y, Key.T, Key.W, Key.S, Key.A, Key.D, Key.Space, Key.R, Key.Y, Key.T,
Key.Enter, Key.Escape, Key.B, Key.F12, Key.F11, Key.F7, Key.Enter, Key.Escape, Key.B, Key.F12, Key.F11, Key.F7,
Key.F5, Key.F, Key.Z, Key.ShiftLeft, Key.X, Key.Q, Key.F5, Key.F, Key.Z, Key.ShiftLeft, Key.X, Key.Q,
Key.E, Key.Tab, Key.F1 }; Key.E, Key.Tab, Key.F1 };
#else #else
Keys = new Key[23]; Keys = new Key[23];
Keys[0] = Key.W; Keys[1] = Key.S; Keys[2] = Key.A; Keys[3] = Key.D; Keys[0] = Key.W; Keys[1] = Key.S; Keys[2] = Key.A; Keys[3] = Key.D;
Keys[4] = Key.Space; Keys[5] = Key.R; Keys[6] = Key.Y; Keys[7] = Key.T; Keys[4] = Key.Space; Keys[5] = Key.R; Keys[6] = Key.Y; Keys[7] = Key.T;
Keys[8] = Key.Enter; Keys[9] = Key.Escape; Keys[10] = Key.B; Keys[8] = Key.Enter; Keys[9] = Key.Escape; Keys[10] = Key.B;
Keys[11] = Key.F12; Keys[12] = Key.F11; Keys[13] = Key.F7; Keys[11] = Key.F12; Keys[12] = Key.F11; Keys[13] = Key.F7;
Keys[14] = Key.F5; Keys[15] = Key.F; Keys[16] = Key.Z; Keys[14] = Key.F5; Keys[15] = Key.F; Keys[16] = Key.Z;
Keys[17] = Key.ShiftLeft; Keys[18] = Key.X; Keys[19] = Key.Q; Keys[17] = Key.ShiftLeft; Keys[18] = Key.X; Keys[19] = Key.Q;
Keys[20] = Key.E; Keys[21] = Key.Tab; Keys[22] = Key.F1; Keys[20] = Key.E; Keys[21] = Key.Tab; Keys[22] = Key.F1;
#endif #endif
LoadKeyBindings();
}
void LoadKeyBindings() {
try {
Options.Load();
} catch( IOException ) {
Utils.LogWarning( "Unable to load options.txt" );
return;
}
string[] names = KeyMapping.GetNames( typeof( KeyMapping ) );
for( int i = 0; i < names.Length; i++ ) {
string key = "key-" + names[i];
string value = Options.Get( key );
if( value == null ) {
Options.Set( key, Keys[i].ToString() );
continue;
}
Key mapping;
try {
mapping = (Key)Enum.Parse( typeof( Key ), value, true );
} catch( ArgumentException ) {
Options.Set( key, Keys[i].ToString() );
continue;
}
if( !IsReservedKey( mapping ) )
Keys[i] = mapping;
}
}
void SaveKeyBindings() {
string[] names = KeyMapping.GetNames( typeof( KeyMapping ) );
for( int i = 0; i < names.Length; i++ ) {
Options.Set( "key-" + names[i], Keys[i].ToString() );
}
try {
Options.Save();
} catch( IOException ) {
Utils.LogWarning( "Unable to sace options.txt" );
}
} }
} }
} }

View File

@ -0,0 +1,58 @@
using System;
using System.Collections.Generic;
using System.IO;
namespace ClassicalSharp {
public static class Options {
static Dictionary<string, string> OptionsSet = new Dictionary<string, string>();
public static string Get( string key ) {
string value;
return OptionsSet.TryGetValue( key, out value ) ? value : null;
}
public static void Set( string key, string value ) {
OptionsSet[key] = value;
}
public const string OptionsFile = "options.txt";
public static void Load() {
try {
using( StreamReader reader = new StreamReader( OptionsFile, false ) )
LoadFrom( reader );
} catch( FileNotFoundException ) {
}
}
static void LoadFrom( StreamReader reader ) {
string line;
while( ( line = reader.ReadLine() ) != null ) {
if( line.Length == 0 && line[0] == '#' ) continue;
int separatorIndex = line.IndexOf( '=' );
if( separatorIndex <= 0 ) continue;
string key = line.Substring( 0, separatorIndex );
separatorIndex++;
if( separatorIndex == line.Length ) continue;
string value = line.Substring( separatorIndex, line.Length - separatorIndex );
OptionsSet[key] = value;
}
}
public static void Save() {
using( StreamWriter writer = new StreamWriter( OptionsFile ) ) {
foreach( KeyValuePair<string, string> pair in OptionsSet ) {
writer.Write( pair.Key );
writer.Write( '=' );
writer.Write( pair.Value );
writer.WriteLine();
}
}
}
}
}