Able to bind mouse buttons to keyboard, fix various things with the launcher.

This commit is contained in:
UnknownShadow200 2015-10-22 12:03:47 +11:00
parent c1041b0c40
commit a10fe0336f
16 changed files with 174 additions and 93 deletions

View file

@ -187,8 +187,8 @@ namespace ClassicalSharp {
}
public override bool HandlesKeyDown( Key key ) {
if( key == game.Keys[KeyMapping.PauseOrExit] ||
key == game.Keys[KeyMapping.OpenInventory] ) {
if( key == game.Mapping( KeyMapping.PauseOrExit ) ||
key == game.Mapping( KeyMapping.OpenInventory ) ) {
game.SetNewScreen( new NormalScreen( game ) );
}
return true;

View file

@ -200,14 +200,15 @@ namespace ClassicalSharp {
suppressNextPress = false;
if( HandlesAllInput ) { // text input bar
if( key == game.Keys[KeyMapping.SendChat] || key == game.Keys[KeyMapping.PauseOrExit] ) {
if( key == game.Mapping( KeyMapping.SendChat )
|| key == game.Mapping( KeyMapping.PauseOrExit ) ) {
HandlesAllInput = false;
if( game.CursorVisible )
game.CursorVisible = false;
game.Camera.RegrabMouse();
game.Keyboard.KeyRepeat = false;
if( key == game.Keys[KeyMapping.PauseOrExit] )
if( key == game.Mapping( KeyMapping.PauseOrExit ) )
textInput.chatInputText.Clear();
textInput.SendTextInBufferAndReset();
} else if( key == Key.PageUp ) {
@ -221,7 +222,7 @@ namespace ClassicalSharp {
if( chatIndex > game.Chat.Log.Count - chatLines )
chatIndex = game.Chat.Log.Count - chatLines;
ResetChat();
} else if( key == game.Keys[KeyMapping.HideGui] ) {
} else if( key == game.Mapping( KeyMapping.HideGui ) ) {
game.HideGui = !game.HideGui;
} else {
textInput.HandlesKeyDown( key );
@ -229,7 +230,7 @@ namespace ClassicalSharp {
return true;
}
if( key == game.Keys[KeyMapping.OpenChat] ) {
if( key == game.Mapping( KeyMapping.OpenChat ) ) {
OpenTextInputBar( "" );
} else if( key == Key.Slash ) {
OpenTextInputBar( "/" );

View file

@ -44,7 +44,8 @@ namespace ClassicalSharp {
int y = -180;
for( int i = 0; i < len; i++ ) {
KeyMapping mapping = (KeyMapping)( (int)start + i );
string text = descriptions[start + i] + ": " + keyNames[(int)game.Keys[mapping]];
string text = descriptions[start + i] + ": "
+ keyNames[(int)game.Mapping( mapping )];
buttons[index++] = ButtonWidget.Create( game, x, y, 240, 25, text,
Anchor.Centre, Anchor.Centre, keyFont, OnWidgetClick );
@ -68,18 +69,20 @@ namespace ClassicalSharp {
} else if( widget != null ) {
int index = Array.IndexOf<ButtonWidget>( buttons, widget );
KeyMapping mapping = (KeyMapping)index;
Key oldKey = game.Keys[mapping];
KeyMap map = game.InputHandler.Keys;
Key oldKey = map[mapping];
string reason;
if( !game.Keys.IsKeyOkay( oldKey, key, out reason ) ) {
if( !map.IsKeyOkay( oldKey, key, out reason ) ) {
const string format = "&eFailed to change mapping \"{0}\". &c({1})";
statusWidget.SetText( String.Format( format, descriptions[index], reason ) );
} else {
const string format = "&eChanged mapping \"{0}\" from &7{1} &eto &7{2}&e.";
statusWidget.SetText( String.Format( format, descriptions[index], oldKey, key ) );
string text = descriptions[index] + " : " + keyNames[(int)key];
widget.SetText( text );
game.Keys[mapping] = key;
map[mapping] = key;
}
widget = null;
}

View file

@ -96,7 +96,7 @@ namespace ClassicalSharp {
}
public override bool HandlesKeyDown( Key key ) {
if( key == game.Keys[KeyMapping.PlayerList] ) {
if( key == game.Mapping( KeyMapping.PlayerList ) ) {
if( playerList == null ) {
if( game.Network.UsingExtPlayerList ) {
playerList = new ExtPlayerListWidget( game, playerFont );
@ -114,7 +114,7 @@ namespace ClassicalSharp {
}
public override bool HandlesKeyUp( Key key ) {
if( key == game.Keys[KeyMapping.PlayerList] ) {
if( key == game.Mapping( KeyMapping.PlayerList ) ) {
if( playerList != null ) {
playerList.Dispose();
playerList = null;

View file

@ -251,7 +251,8 @@ namespace ClassicalSharp {
}
internal void HandleKeyDown( Key key ) {
if( key == game.Keys[KeyMapping.Respawn] && canRespawn ) {
KeyMap keys = game.InputHandler.Keys;
if( key == keys[KeyMapping.Respawn] && canRespawn ) {
Vector3I p = Vector3I.Floor( SpawnPoint );
if( game.Map.IsValidPos( p ) ) {
// Spawn player at highest valid position.
@ -268,11 +269,11 @@ namespace ClassicalSharp {
Vector3 spawn = (Vector3)p + new Vector3( 0.5f, 0.01f, 0.5f );
LocationUpdate update = LocationUpdate.MakePos( spawn, false );
SetLocation( update, false );
} else if( key == game.Keys[KeyMapping.SetSpawn] && canRespawn ) {
} else if( key == keys[KeyMapping.SetSpawn] && canRespawn ) {
SpawnPoint = Position;
} else if( key == game.Keys[KeyMapping.Fly] && canFly ) {
} else if( key == keys[KeyMapping.Fly] && canFly ) {
flying = !flying;
} else if( key == game.Keys[KeyMapping.NoClip] && canNoclip ) {
} else if( key == keys[KeyMapping.NoClip] && canNoclip ) {
noClip = !noClip;
}
}

View file

@ -85,23 +85,6 @@ namespace ClassicalSharp {
Events.RaiseTerrainAtlasChanged();
}
public Game( string username, string mppass, string skinServer, string defaultTexPack )
#if USE_DX
: base( 640, 480, GraphicsMode.Default, Utils.AppName, true, 0, DisplayDevice.Default ) {
#else
: base( 640, 480, GraphicsMode.Default, Utils.AppName, false, 0, DisplayDevice.Default ) {
#endif
Username = username;
Mppass = mppass;
this.skinServer = skinServer;
this.defaultTexPack = defaultTexPack;
if( !File.Exists( defaultTexPack ) ) {
Utils.LogWarning( defaultTexPack + " not found" );
this.defaultTexPack = "default.zip";
}
}
protected override void OnLoad( EventArgs e ) {
#if !USE_DX
Graphics = new OpenGLApi();
@ -114,7 +97,6 @@ namespace ClassicalSharp {
Utils.LogWarning( "Unable to load options.txt" );
}
ViewDistance = Options.GetInt( OptionsKey.ViewDist, 16, 4096, 512 );
Keys = new KeyMap();
InputHandler = new InputHandler( this );
Chat = new ChatLog( this );
Chat.FontSize = Options.GetInt( OptionsKey.FontSize, 6, 30, 12 );
@ -232,10 +214,7 @@ namespace ClassicalSharp {
Picking.Render( e.Time, SelectedPos );
SelectionManager.Render( e.Time );
WeatherRenderer.Render( e.Time );
bool left = IsMousePressed( MouseButton.Left );
bool right = IsMousePressed( MouseButton.Right );
bool middle = IsMousePressed( MouseButton.Middle );
InputHandler.PickBlocks( true, left, right, middle );
InputHandler.PickBlocks( true );
} else {
SelectedPos.SetAsInvalid();
}
@ -350,19 +329,13 @@ namespace ClassicalSharp {
MapRenderer.RedrawBlock( x, y, z, block, oldHeight, newHeight );
}
public KeyMap Keys;
public bool IsKeyDown( Key key ) {
return Keyboard[key];
}
public bool IsKeyDown( Key key ) { return InputHandler.IsKeyDown( key ); }
public bool IsKeyDown( KeyMapping mapping ) {
Key key = Keys[mapping];
return Keyboard[key];
}
public bool IsKeyDown( KeyMapping mapping ) { return InputHandler.IsKeyDown( mapping ); }
public bool IsMousePressed( MouseButton button ) {
return Mouse[button];
}
public bool IsMousePressed( MouseButton button ) { return InputHandler.IsMousePressed( button ); }
public Key Mapping( KeyMapping mapping ) { return InputHandler.Keys[mapping]; }
public override void Dispose() {
MapRenderer.Dispose();
@ -405,6 +378,23 @@ namespace ClassicalSharp {
return !(block == 0 || (BlockInfo.IsLiquid[block] &&
!(Inventory.CanPlace[block] && Inventory.CanDelete[block])));
}
public Game( string username, string mppass, string skinServer, string defaultTexPack )
#if USE_DX
: base( 640, 480, GraphicsMode.Default, Utils.AppName, true, 0, DisplayDevice.Default ) {
#else
: base( 640, 480, GraphicsMode.Default, Utils.AppName, false, 0, DisplayDevice.Default ) {
#endif
Username = username;
Mppass = mppass;
this.skinServer = skinServer;
this.defaultTexPack = defaultTexPack;
if( !File.Exists( defaultTexPack ) ) {
Utils.LogWarning( defaultTexPack + " not found" );
this.defaultTexPack = "default.zip";
}
}
}
public sealed class CpeListInfo {

View file

@ -10,6 +10,8 @@ namespace ClassicalSharp {
public InputHandler( Game game ) {
this.game = game;
RegisterInputHandlers();
LoadMouseToKeyMappings();
Keys = new KeyMap();
}
void RegisterInputHandlers() {
@ -21,13 +23,45 @@ namespace ClassicalSharp {
game.Mouse.ButtonDown += MouseButtonDown;
game.Mouse.ButtonUp += MouseButtonUp;
}
Key mapLeft, mapMiddle, mapRight;
void LoadMouseToKeyMappings() {
mapLeft = Options.GetKey( OptionsKey.MouseLeft, Key.Unknown );
mapMiddle = Options.GetKey( OptionsKey.MouseMiddle, Key.Unknown );
mapRight = Options.GetKey( OptionsKey.MouseRight, Key.Unknown );
}
public KeyMap Keys;
public bool IsKeyDown( Key key ) {
return game.Keyboard[key];
}
public bool IsKeyDown( KeyMapping mapping ) {
Key key = Keys[mapping];
return game.Keyboard[key];
}
public bool IsMousePressed( MouseButton button ) {
bool down = game.Mouse[button];
if( down ) return true;
// Key --> mouse mappings
if( button == MouseButton.Left && IsKeyDown( mapLeft ) ) return true;
if( button == MouseButton.Middle && IsKeyDown( mapMiddle ) ) return true;
if( button == MouseButton.Right && IsKeyDown( mapRight ) ) return true;
return false;
}
bool[] buttonsDown = new bool[3];
DateTime lastClick = DateTime.MinValue;
public void PickBlocks( bool cooldown, bool left, bool right, bool middle ) {
public void PickBlocks( bool cooldown ) {
bool left = game.IsMousePressed( MouseButton.Left );
bool right = game.IsMousePressed( MouseButton.Right );
bool middle = game.IsMousePressed( MouseButton.Middle );
DateTime now = DateTime.UtcNow;
double delta = ( now - lastClick ).TotalMilliseconds;
double delta = (now - lastClick).TotalMilliseconds;
if( cooldown && delta < 250 ) return; // 4 times per second
lastClick = now;
Inventory inv = game.Inventory;
@ -38,7 +72,7 @@ namespace ClassicalSharp {
ButtonStateChanged( MouseButton.Middle, middle, targetId );
}
int buttonsDown = ( left ? 1 : 0 ) + ( right ? 1 : 0 ) + ( middle ? 1 : 0 );
int buttonsDown = (left ? 1 : 0) + (right ? 1 : 0) + (middle ? 1 : 0);
if( !game.SelectedPos.Valid || buttonsDown > 1 || game.ScreenLockedInput || inv.HeldBlock == Block.Air ) return;
if( middle ) {
@ -131,10 +165,7 @@ namespace ClassicalSharp {
void MouseButtonDown( object sender, MouseButtonEventArgs e ) {
if( game.activeScreen == null || !game.activeScreen.HandlesMouseClick( e.X, e.Y, e.Button ) ) {
bool left = e.Button == MouseButton.Left;
bool right = e.Button == MouseButton.Right;
bool middle = e.Button == MouseButton.Middle;
PickBlocks( false, left, right, middle );
PickBlocks( false );
} else {
lastClick = DateTime.UtcNow;
}
@ -166,6 +197,8 @@ namespace ClassicalSharp {
void KeyUpHandler( object sender, KeyboardKeyEventArgs e ) {
Key key = e.Key;
if( SimulateMouse( key, false ) ) return;
if( game.activeScreen == null || !game.activeScreen.HandlesKeyUp( key ) ) {
}
}
@ -173,9 +206,11 @@ namespace ClassicalSharp {
static int[] viewDistances = { 16, 32, 64, 128, 256, 512 };
void KeyDownHandler( object sender, KeyboardKeyEventArgs e ) {
Key key = e.Key;
if( SimulateMouse( key, true ) ) return;
if( key == Key.F4 && (game.IsKeyDown( Key.AltLeft ) || game.IsKeyDown( Key.AltRight )) ) {
game.Exit();
} else if( key == game.Keys[KeyMapping.Screenshot] ) {
} else if( key == Keys[KeyMapping.Screenshot] ) {
game.screenshotRequested = true;
} else if( game.activeScreen == null || !game.activeScreen.HandlesKeyDown( key ) ) {
if( !HandleBuiltinKey( key ) ) {
@ -184,27 +219,45 @@ namespace ClassicalSharp {
}
}
MouseButtonEventArgs simArgs = new MouseButtonEventArgs();
bool SimulateMouse( Key key, bool pressed ) {
if( !(key == mapLeft || key == mapMiddle || key == mapRight ) )
return false;
simArgs.Button = key == mapLeft ? MouseButton.Left :
key == mapMiddle ? MouseButton.Middle : MouseButton.Right;
simArgs.X = game.Mouse.X;
simArgs.Y = game.Mouse.Y;
simArgs.IsPressed = pressed;
if( pressed ) {
MouseButtonDown( null, simArgs );
} else {
MouseButtonUp( null, simArgs );
}
return true;
}
bool HandleBuiltinKey( Key key ) {
if( key == game.Keys[KeyMapping.HideGui] ) {
if( key == Keys[KeyMapping.HideGui] ) {
game.HideGui = !game.HideGui;
} else if( key == game.Keys[KeyMapping.Fullscreen] ) {
} else if( key == Keys[KeyMapping.Fullscreen] ) {
WindowState state = game.WindowState;
if( state != WindowState.Minimized ) {
game.WindowState = state == WindowState.Fullscreen ?
WindowState.Normal : WindowState.Fullscreen;
}
} else if( key == game.Keys[KeyMapping.ThirdPersonCamera] ) {
} else if( key == Keys[KeyMapping.ThirdPersonCamera] ) {
bool useThirdPerson = !(game.Camera is ForwardThirdPersonCamera);
game.SetCamera( useThirdPerson );
} else if( key == game.Keys[KeyMapping.ViewDistance] ) {
} else if( key == Keys[KeyMapping.ViewDistance] ) {
if( game.IsKeyDown( Key.ShiftLeft ) || game.IsKeyDown( Key.ShiftRight ) ) {
CycleDistanceBackwards();
} else {
CycleDistanceForwards();
}
} else if( key == game.Keys[KeyMapping.PauseOrExit] && !game.Map.IsNotLoaded ) {
} else if( key == Keys[KeyMapping.PauseOrExit] && !game.Map.IsNotLoaded ) {
game.SetNewScreen( new PauseScreen( game ) );
} else if( key == game.Keys[KeyMapping.OpenInventory] ) {
} else if( key == Keys[KeyMapping.OpenInventory] ) {
game.SetNewScreen( new BlockSelectScreen( game ) );
} else {
return false;

View file

@ -64,19 +64,7 @@ namespace ClassicalSharp {
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] );
continue;
}
Key mapping;
try {
mapping = (Key)Enum.Parse( typeof( Key ), value, true );
} catch( ArgumentException ) {
Options.Set( key, Keys[i] );
continue;
}
Key mapping = Options.GetKey( key, Keys[i] );
if( !IsReservedKey( mapping ) )
Keys[i] = mapping;
}

View file

@ -1,6 +1,8 @@
using System;
using System.Collections.Generic;
using System.IO;
using OpenTK;
using OpenTK.Input;
namespace ClassicalSharp {
@ -9,6 +11,10 @@ namespace ClassicalSharp {
public const string FontSize = "chatfontsize";
public const string Sensitivity = "mousesensitivity";
public const string Speed = "speedmultiplier";
public const string MouseLeft = "mouseleft";
public const string MouseMiddle = "mousemiddle";
public const string MouseRight = "mouseright";
}
public static class Options {
@ -41,6 +47,23 @@ namespace ClassicalSharp {
return valueBool;
}
public static Key GetKey( string key, Key defValue ) {
string value = Options.Get( key );
if( value == null ) {
Set( key, defValue );
return defValue;
}
Key mapping;
try {
mapping = (Key)Enum.Parse( typeof( Key ), value, true );
} catch( ArgumentException ) {
Options.Set( key, defValue );
return defValue;
}
return mapping;
}
public static void Set( string key, string value ) {
OptionsSet[key] = value;
HasChanged = true;

View file

@ -19,6 +19,7 @@ namespace Launcher2 {
}
public override void Init() {
base.Init();
Resize();
using( drawer ) {
drawer.SetBitmap( game.Framebuffer );

View file

@ -13,11 +13,8 @@ namespace Launcher2 {
public ClassiCubeServersScreen( LauncherWindow game ) : base( game ) {
titleFont = new Font( "Arial", 16, FontStyle.Bold );
inputFont = new Font( "Arial", 13, FontStyle.Regular );
enterIndex = 4;
widgets = new LauncherWidget[7];
game.Window.Mouse.WheelChanged += MouseWheelChanged;
game.Window.Mouse.ButtonUp += MouseButtonUp;
enterIndex = 4;
widgets = new LauncherWidget[7];
}
public override void Tick() {
@ -49,7 +46,12 @@ namespace Launcher2 {
}
}
public override void Init() { Resize(); }
public override void Init() {
base.Init();
game.Window.Mouse.WheelChanged += MouseWheelChanged;
game.Window.Mouse.ButtonUp += MouseButtonUp;
Resize();
}
public override void Resize() {
using( drawer ) {
@ -135,14 +137,7 @@ namespace Launcher2 {
}
void ConnectToServer( int mouseX, int mouseY ) {
GameStartData data = null;
try {
data = game.Session.GetConnectInfo( Get( 3 ) );
} catch( WebException ex ) {
Program.LogException( ex );
return;
}
Client.Start( data, true );
game.ConnectToServer( Get( 3 ) );
}
void MouseWheelChanged( object sender, MouseWheelEventArgs e ) {

View file

@ -18,6 +18,7 @@ namespace Launcher2 {
}
public override void Init() {
base.Init();
Resize();
using( drawer ) {
drawer.SetBitmap( game.Framebuffer );

View file

@ -13,7 +13,10 @@ namespace Launcher2 {
protected Font titleFont, inputFont;
protected int enterIndex = -1;
public LauncherInputScreen( LauncherWindow game ) : base( game ) {
public LauncherInputScreen( LauncherWindow game ) : base( game ) {
}
public override void Init() {
game.Window.Mouse.Move += MouseMove;
game.Window.Mouse.ButtonDown += MouseButtonDown;
@ -41,7 +44,7 @@ namespace Launcher2 {
if( lastInput != null ) {
using( drawer ) {
drawer.SetBitmap( game.Framebuffer );
lastInput.AddChar( e.KeyChar, inputFont );
lastInput.AddChar( e.KeyChar, inputFont );
Dirty = true;
}
OnAddedChar();

View file

@ -27,6 +27,7 @@ namespace Launcher2 {
if( fetcher.Done ) {
ResourcePatcher patcher = new ResourcePatcher( fetcher );
patcher.Run();
game.SetScreen( new MainScreen( game ) );
fetcher = null;
}
}

View file

@ -12,7 +12,9 @@ namespace Launcher2 {
void HandleOnClick( int mouseX, int mouseY ) {
if( mouseX >= Window.Width - 10 ) {
ScrollbarClick( mouseY ); return;
ScrollbarClick( mouseY );
lastIndex = -10;
return;
}
if( mouseY >= HeaderStartY && mouseY < HeaderEndY ) {
@ -29,17 +31,23 @@ namespace Launcher2 {
} else {
DraggingWidth = true;
}
lastIndex = -10;
} else {
for( int i = 0; i < Count; i++ ) {
TableEntry entry = usedEntries[i];
if( mouseY >= entry.Y && mouseY < entry.Y + entry.Height ) {
if( lastIndex == i ) {
Window.ConnectToServer( entry.Hash );
}
SelectedChanged( entry.Hash );
lastIndex = i;
break;
}
}
}
}
int lastIndex = -10;
public void MouseMove( int deltaX, int deltaY ) {
if( DraggingWidth ) {
ColumnWidths[0] += deltaX;

View file

@ -5,6 +5,7 @@ using ClassicalSharp;
using ClassicalSharp.Network;
using OpenTK;
using OpenTK.Graphics;
using System.Net;
namespace Launcher2 {
@ -59,6 +60,18 @@ namespace Launcher2 {
screen.Init();
}
public bool ConnectToServer( string hash ) {
GameStartData data = null;
try {
data = Session.GetConnectInfo( hash );
} catch( WebException ex ) {
Program.LogException( ex );
return false;
}
Client.Start( data, true );
return true;
}
public void Run() {
Window = new NativeWindow( 480, 480, Program.AppName, 0,
GraphicsMode.Default, DisplayDevice.Default );