First step to making input keys configurable.

This commit is contained in:
UnknownShadow200 2015-01-02 17:01:35 +11:00
parent 55aade0f39
commit c8c156791a
7 changed files with 124 additions and 43 deletions

View File

@ -247,7 +247,7 @@ namespace ClassicalSharp {
} }
public override bool HandlesKeyDown( Key key ) { public override bool HandlesKeyDown( Key key ) {
if( key == Key.Escape || key == Key.Q ) { if( key == Window.Keys[KeyMapping.PauseOrExit] ) {
Window.SetNewScreen( new NormalScreen( Window ) ); Window.SetNewScreen( new NormalScreen( Window ) );
} else if( key == Key.Left ) { } else if( key == Key.Left ) {
MovePointerLeft(); MovePointerLeft();

View File

@ -178,7 +178,7 @@ namespace ClassicalSharp {
suppressNextPress = false; suppressNextPress = false;
if( HandlesAllInput ) { // text input bar if( HandlesAllInput ) { // text input bar
if( key == Key.Enter ) { if( key == Window.Keys[KeyMapping.SendChat] ) {
HandlesAllInput = false; HandlesAllInput = false;
Window.Camera.RegrabMouse(); Window.Camera.RegrabMouse();
textInput.SendTextInBufferAndReset(); textInput.SendTextInBufferAndReset();
@ -188,11 +188,11 @@ namespace ClassicalSharp {
return true; return true;
} }
if( key == Key.T ) { if( key == Window.Keys[KeyMapping.OpenChat] ) {
OpenTextInputBar( "" ); OpenTextInputBar( "" );
} else if( key == Key.Slash ) { } else if( key == Key.Slash ) {
OpenTextInputBar( "/" ); OpenTextInputBar( "/" );
} else if( key == Key.H ) { } else if( key == Window.Keys[KeyMapping.ChatHistoryMode] ) {
HistoryMode = !HistoryMode; HistoryMode = !HistoryMode;
normalChat.Dispose(); normalChat.Dispose();
InitChat(); InitChat();

View File

@ -18,7 +18,7 @@ namespace ClassicalSharp {
if( playerList != null ) { if( playerList != null ) {
playerList.Render( delta ); playerList.Render( delta );
// NOTE: Should usually be caught by KeyUp, but just in case. // NOTE: Should usually be caught by KeyUp, but just in case.
if( !Window.IsKeyDown( Key.Tab ) ) { if( !Window.IsKeyDown( KeyMapping.PlayerList ) ) {
playerList.Dispose(); playerList.Dispose();
playerList = null; playerList = null;
} }
@ -61,7 +61,7 @@ namespace ClassicalSharp {
} }
public override bool HandlesKeyDown( Key key ) { public override bool HandlesKeyDown( Key key ) {
if( key == Key.Tab ) { if( key == Window.Keys[KeyMapping.PlayerList] ) {
if( playerList == null ) { if( playerList == null ) {
if( Window.Network.UsingExtPlayerList ) { if( Window.Network.UsingExtPlayerList ) {
playerList = new ExtPlayerListWidget( Window ); playerList = new ExtPlayerListWidget( Window );
@ -78,7 +78,7 @@ namespace ClassicalSharp {
} }
public override bool HandlesKeyUp( Key key ) { public override bool HandlesKeyUp( Key key ) {
if( key == Key.Tab ) { if( key == Window.Keys[KeyMapping.PlayerList] ) {
if( playerList != null ) { if( playerList != null ) {
playerList.Dispose(); playerList.Dispose();
playerList = null; playerList = null;

View File

@ -29,18 +29,28 @@ namespace ClassicalSharp {
controlsWidget = CreateTextWidget( 0, 30, "&eControls list", Docking.LeftOrTop, 16, FontStyle.Bold ); controlsWidget = CreateTextWidget( 0, 30, "&eControls list", Docking.LeftOrTop, 16, FontStyle.Bold );
gameWidget = CreateTextWidget( 0, -60, "&eBack to game", Docking.BottomOrRight, 16, FontStyle.Bold ); gameWidget = CreateTextWidget( 0, -60, "&eBack to game", Docking.BottomOrRight, 16, FontStyle.Bold );
exitWidget = CreateTextWidget( 0, -10, "&eExit", Docking.BottomOrRight, 16, FontStyle.Bold ); exitWidget = CreateTextWidget( 0, -10, "&eExit", Docking.BottomOrRight, 16, FontStyle.Bold );
MakeKeysLeft( "Forward: W", "Back: S", "Left: A", "Right: D", "Jump: Space", "Respawn: R", "Set spawn: Y",
"Open chat: T", "Send chat: Enter", "Pause: Escape", "Open inventory: B" ); KeyMapping[] mappingsLeft = { KeyMapping.Forward, KeyMapping.Back, KeyMapping.Left, KeyMapping.Right,
MakeKeysRight( "Take screenshot: F12", "Toggle fullscreen: F11", "Toggle 3rd person camera: F5", KeyMapping.Jump, KeyMapping.Respawn, KeyMapping.SetSpawn, KeyMapping.OpenChat, KeyMapping.SendChat,
"Toggle VSync: F7", "Change view distance: F6", "Toggle fly: Z", "Speed: Shift", KeyMapping.PauseOrExit, KeyMapping.OpenInventory };
"Toggle noclip: X", "Fly up: Q", "Fly down: E", "Display player list: Tab" ); string[] descriptionsLeft = { "Forward", "Back", "Left", "Right", "Jump", "Respawn", "Set spawn",
"Open chat", "Send chat", "Pause", "Open inventory" };
MakeKeysLeft( mappingsLeft, descriptionsLeft );
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" };
MakeKeysRight( mappingsRight, descriptionsRight );
} }
void MakeKeysLeft( params string[] keyBindings ) { void MakeKeysLeft( KeyMapping[] mappings, string[] descriptions ) {
int startY = controlsWidget.BottomRight.Y + 10; int startY = controlsWidget.BottomRight.Y + 10;
keysLeft = new TextWidget[keyBindings.Length]; keysLeft = new TextWidget[mappings.Length];
for( int i = 0; i < keysLeft.Length; i++ ) { for( int i = 0; i < keysLeft.Length; i++ ) {
TextWidget widget = CreateTextWidget( 0, startY, keyBindings[i], Docking.LeftOrTop, 14, FontStyle.Bold ); string text = descriptions[i] + ": " + Window.Keys[mappings[i]];
TextWidget widget = CreateTextWidget( 0, startY, text, Docking.LeftOrTop, 14, FontStyle.Bold );
widget.XOffset = -widget.Width / 2 - 20; widget.XOffset = -widget.Width / 2 - 20;
widget.MoveTo( widget.X + widget.XOffset, widget.Y ); widget.MoveTo( widget.X + widget.XOffset, widget.Y );
keysLeft[i] = widget; keysLeft[i] = widget;
@ -48,11 +58,12 @@ namespace ClassicalSharp {
} }
} }
void MakeKeysRight( params string[] keyBindings ) { void MakeKeysRight( KeyMapping[] mappings, string[] descriptions ) {
int startY = controlsWidget.BottomRight.Y + 10; int startY = controlsWidget.BottomRight.Y + 10;
keysRight = new TextWidget[keyBindings.Length]; keysRight = new TextWidget[mappings.Length];
for( int i = 0; i < keysRight.Length; i++ ) { for( int i = 0; i < keysRight.Length; i++ ) {
TextWidget widget = CreateTextWidget( 0, startY, keyBindings[i], Docking.LeftOrTop, 14, FontStyle.Bold ); string text = descriptions[i] + ": " + Window.Keys[mappings[i]];
TextWidget widget = CreateTextWidget( 0, startY, text, Docking.LeftOrTop, 14, FontStyle.Bold );
widget.XOffset = widget.Width / 2 + 20; widget.XOffset = widget.Width / 2 + 20;
widget.MoveTo( widget.X + widget.XOffset, widget.Y ); widget.MoveTo( widget.X + widget.XOffset, widget.Y );
keysRight[i] = widget; keysRight[i] = widget;

View File

@ -121,16 +121,15 @@ namespace ClassicalSharp {
if( Window.ScreenLockedInput ) { if( Window.ScreenLockedInput ) {
jumping = speeding = flyingUp = flyingDown = false; jumping = speeding = flyingUp = flyingDown = false;
} else { } else {
if( Window.IsKeyDown( Key.W ) ) xMoving -= 0.98f; if( Window.IsKeyDown( KeyMapping.Forward ) ) xMoving -= 0.98f;
if( Window.IsKeyDown( Key.S ) ) xMoving += 0.98f; if( Window.IsKeyDown( KeyMapping.Back ) ) xMoving += 0.98f;
if( Window.IsKeyDown( Key.A ) ) zMoving -= 0.98f; if( Window.IsKeyDown( KeyMapping.Left ) ) zMoving -= 0.98f;
if( Window.IsKeyDown( Key.D ) ) zMoving += 0.98f; if( Window.IsKeyDown( KeyMapping.Right ) ) zMoving += 0.98f;
jumping = Window.IsKeyDown( Key.Space ); jumping = Window.IsKeyDown( KeyMapping.Jump );
bool shiftDown = Window.IsKeyDown( Key.ShiftLeft ) || Window.IsKeyDown( Key.ShiftRight ); speeding = CanSpeed && Window.IsKeyDown( KeyMapping.Speed );
speeding = CanSpeed && shiftDown; flyingUp = Window.IsKeyDown( KeyMapping.FlyUp );
flyingUp = Window.IsKeyDown( Key.Q ); flyingDown = Window.IsKeyDown( KeyMapping.FlyDown );
flyingDown = Window.IsKeyDown( Key.E );
} }
} }
@ -347,14 +346,14 @@ namespace ClassicalSharp {
} }
public bool HandleKeyDown( Key key ) { public bool HandleKeyDown( Key key ) {
if( key == Key.R && canRespawn ) { if( key == Window.Keys[KeyMapping.Respawn] && canRespawn ) {
LocationUpdate update = LocationUpdate.MakePos( SpawnPoint, false ); LocationUpdate update = LocationUpdate.MakePos( SpawnPoint, false );
SetLocation( update, false ); SetLocation( update, false );
} else if( key == Key.Y && canRespawn ) { } else if( key == Window.Keys[KeyMapping.SetSpawn] && canRespawn ) {
SpawnPoint = Position; SpawnPoint = Position;
} else if( key == Key.Z && canFly ) { } else if( key == Window.Keys[KeyMapping.Fly] && canFly ) {
flying = !flying; flying = !flying;
} else if( key == Key.X && canNoclip ) { } else if( key == Window.Keys[KeyMapping.NoClip] && canNoclip ) {
noClip = !noClip; noClip = !noClip;
} else { } else {
return false; return false;

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using ClassicalSharp.Particles; using ClassicalSharp.Particles;
using OpenTK; using OpenTK;
using OpenTK.Input; using OpenTK.Input;
@ -11,6 +12,11 @@ namespace ClassicalSharp {
return Keyboard[key]; return Keyboard[key];
} }
public bool IsKeyDown( KeyMapping mapping ) {
Key key = Keys[mapping];
return Keyboard[key];
}
public bool IsMousePressed( MouseButton button ) { public bool IsMousePressed( MouseButton button ) {
return Mouse[button]; return Mouse[button];
} }
@ -69,21 +75,20 @@ namespace ClassicalSharp {
static int[] viewDistances = { 16, 32, 64, 128, 256, 512 }; static int[] viewDistances = { 16, 32, 64, 128, 256, 512 };
void KeyDownHandler( object sender, KeyboardKeyEventArgs e ) { void KeyDownHandler( object sender, KeyboardKeyEventArgs e ) {
Key key = e.Key; Key key = e.Key;
if( key == Key.F12 ) { if( key == Keys[KeyMapping.Screenshot] ) {
screenshotRequested = true; screenshotRequested = true;
} else if( key == Key.F11 ) { } else if( key == Keys[KeyMapping.Fullscreen] ) {
WindowState state = WindowState; WindowState state = WindowState;
if( state == WindowState.Fullscreen ) { if( state != WindowState.Minimized ) {
WindowState = WindowState.Normal; WindowState = state == WindowState.Fullscreen ?
} else if( state == WindowState.Normal || state == WindowState.Maximized ) { WindowState.Normal : WindowState.Fullscreen;
WindowState = WindowState.Fullscreen;
} }
} else if( key == Key.F5 ) { } else if( key == Keys[KeyMapping.ThirdPersonCamera] ) {
bool useThirdPerson = Camera is FirstPersonCamera; bool useThirdPerson = Camera is FirstPersonCamera;
SetCamera( useThirdPerson ); SetCamera( useThirdPerson );
} else if( key == Key.F7 ) { } else if( key == Keys[KeyMapping.VSync] ) {
VSync = VSync == VSyncMode.Off ? VSyncMode.On : VSyncMode.Off; VSync = VSync == VSyncMode.Off ? VSyncMode.On : VSyncMode.Off;
} else if( key == Key.F6 ) { } else if( key == Keys[KeyMapping.ViewDistance] ) {
if( ViewDistance >= viewDistances[viewDistances.Length - 1] ) { if( ViewDistance >= viewDistances[viewDistances.Length - 1] ) {
SetViewDistance( viewDistances[0] ); SetViewDistance( viewDistances[0] );
} else { } else {
@ -95,12 +100,12 @@ namespace ClassicalSharp {
} }
} }
} }
} else if( key == Key.Escape && !Map.IsNotLoaded ) { } else if( key == Keys[KeyMapping.PauseOrExit] && !Map.IsNotLoaded ) {
if( !( activeScreen is PauseScreen ) ) { if( !( activeScreen is PauseScreen ) ) {
SetNewScreen( new PauseScreen( this ) ); SetNewScreen( new PauseScreen( this ) );
} }
} else if( activeScreen == null || !activeScreen.HandlesKeyDown( key ) ) { } else if( activeScreen == null || !activeScreen.HandlesKeyDown( key ) ) {
if( key == Key.B ) { if( key == Keys[KeyMapping.OpenInventory] ) {
SetNewScreen( new BlockSelectScreen( this ) ); SetNewScreen( new BlockSelectScreen( this ) );
} else { } else {
LocalPlayer.HandleKeyDown( key ); LocalPlayer.HandleKeyDown( key );
@ -142,5 +147,71 @@ namespace ClassicalSharp {
bool CanReplace( byte block ) { bool CanReplace( byte block ) {
return block == 0 || ( !CanPlace[block] && !CanDelete[block] && BlockInfo.IsLiquid( block ) ); return block == 0 || ( !CanPlace[block] && !CanDelete[block] && BlockInfo.IsLiquid( block ) );
} }
public KeyMap Keys = new KeyMap();
} }
public class KeyMap {
public Key this[KeyMapping key] {
get { return Keys[(int)key]; }
set { Keys[(int)key] = value; }
}
[Obsolete( "It's better to use the KeyMapping indexer")]
public Key this[string key] {
get {
KeyMapping mapping = (KeyMapping)KeyMapping.Parse( typeof( KeyMapping ), key, true );
return Keys[(int)mapping];
}
set {
KeyMapping mapping = (KeyMapping)KeyMapping.Parse( typeof( KeyMapping ), key, true );
Keys[(int)mapping] = value;
}
}
Key[] Keys = new Key[] {
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.F5, Key.F6, Key.Z, Key.LShift, Key.X, Key.Q, Key.E,
Key.Tab, Key.H,
};
bool IsLockedKey( Key key ) {
return key == Key.Escape || ( key >= Key.F1 && key <= Key.F35 );
}
bool IsReservedKey( Key key ) {
return key == Key.Slash || key == Key.BackSpace ||
( key >= Key.Insert && key <= Key.End ) ||
( key >= Key.Up && key <= Key.Right ) || // chat screen movement
( key >= Key.Number0 && key <= Key.Number9 ); // block hotbar
}
public bool IsKeyOkay( Key key, out string reason ) {
if( IsLockedKey( key ) ) {
reason = "Given key mapping cannot be changed.";
return false;
}
if( IsReservedKey( key ) ) {
reason = "Given key is reserved for gui.";
return false;
}
for( int i = 0; i < Keys.Length; i++ ) {
if( Keys[i] == key ) {
reason = "Key is already assigned.";
return false;
}
}
reason = null;
return true;
}
}
public enum KeyMapping {
Forward, Back, Left, Right, Jump, Respawn, SetSpawn, OpenChat,
SendChat, PauseOrExit, OpenInventory, Screenshot, Fullscreen, VSync,
ThirdPersonCamera, ViewDistance, Fly, Speed, NoClip, FlyUp, FlyDown,
PlayerList, ChatHistoryMode,
}
} }

View File

@ -140,7 +140,7 @@ namespace ClassicalSharp {
for( int i = 0; i < drawInfoBuffer.Length; i++ ) { for( int i = 0; i < drawInfoBuffer.Length; i++ ) {
DrawInfo1D info = drawInfoBuffer[i]; DrawInfo1D info = drawInfoBuffer[i];
if( info.totalVertices > info.vertices.Length ) { if( info.totalVertices > info.vertices.Length ) {
Array.Resize( ref info.vertices, info.totalVertices ); info.vertices = new VertexPos3fTex2fCol4b[info.totalVertices];
} }
} }
} }