2016-06-11 15:29:45 +10:00

85 lines
2.7 KiB
C#

// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT
using System;
using OpenTK.Input;
namespace ClassicalSharp {
public enum KeyBind {
Forward, Back, Left, Right, Jump, Respawn, SetSpawn, Chat,
Inventory, ToggleFog, SendChat, PauseOrExit, PlayerList,
Speed, NoClip, Fly, FlyUp, FlyDown, ExtInput, HideFps,
Screenshot, Fullscreen, ThirdPerson, HideGui, AxisLines,
ZoomScrolling, HalfSpeed, MouseLeft, MouseMiddle, MouseRight,
}
public class KeyMap {
public Key this[KeyBind key] {
get { return keys[(int)key]; }
set { keys[(int)key] = value; SaveKeyBindings(); }
}
public Key GetDefault( KeyBind key ) {
return defaultKeys[(int)key];
}
Key[] keys, defaultKeys;
bool IsReservedKey( Key key ) {
return key == Key.Escape || key == Key.Slash || key == Key.BackSpace ||
(key >= Key.Insert && key <= Key.End) ||
(key >= Key.Number0 && key <= Key.Number9); // block hotbar
}
public bool IsKeyOkay( Key oldKey, Key key, out string reason ) {
if( oldKey == Key.Escape || oldKey == Key.F12 ) {
reason = "This binding is locked";
return false;
}
if( IsReservedKey( key ) ) {
reason = "New key is reserved";
return false;
}
reason = null;
return true;
}
public KeyMap() {
// We can't use enum array initaliser because this causes problems when building with mono
// and running on default .NET (https://bugzilla.xamarin.com/show_bug.cgi?id=572)
keys = new Key[30];
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.Enter; keys[7] = Key.T;
keys[8] = Key.B; keys[9] = Key.F; keys[10] = Key.Enter;
keys[11] = Key.Escape; keys[12] = Key.Tab; keys[13] = Key.ShiftLeft;
keys[14] = Key.X; keys[15] = Key.Z; keys[16] = Key.Q;
keys[17] = Key.E; keys[18] = Key.AltLeft; keys[19] = Key.F3;
keys[20] = Key.F12; keys[21] = Key.F11; keys[22] = Key.F5;
keys[23] = Key.F1; keys[24] = Key.F7; keys[25] = Key.C;
keys[26] = Key.ControlLeft;
keys[27] = Key.Unknown; keys[28] = Key.Unknown; keys[29] = Key.Unknown;
defaultKeys = new Key[keys.Length];
for( int i = 0; i < defaultKeys.Length; i++ )
defaultKeys[i] = keys[i];
LoadKeyBindings();
}
void LoadKeyBindings() {
string[] names = KeyBind.GetNames( typeof( KeyBind ) );
for( int i = 0; i < names.Length; i++ ) {
string key = "key-" + names[i];
Key mapping = Options.GetEnum( key, keys[i] );
if( !IsReservedKey( mapping ) )
keys[i] = mapping;
}
}
void SaveKeyBindings() {
string[] names = KeyBind.GetNames( typeof( KeyBind ) );
for( int i = 0; i < names.Length; i++ ) {
Options.Set( "key-" + names[i], keys[i] );
}
}
}
}