diff --git a/2D/Screens/PauseScreen.cs b/2D/Screens/PauseScreen.cs index 9c53be4ae..fe76a8098 100644 --- a/2D/Screens/PauseScreen.cs +++ b/2D/Screens/PauseScreen.cs @@ -9,14 +9,16 @@ namespace ClassicalSharp { public PauseScreen( Game window ) : base( window ) { } - TextWidget controlsWidget, gameWidget, exitWidget; - TextWidget[] keysLeft, keysRight; + TextWidget controlsWidget, gameWidget, exitWidget, keyStatusWidget; + KeyMapWidget[] keysLeft, keysRight; + KeyMapWidget widgetToChange; public override void Render( double delta ) { GraphicsApi.Draw2DQuad( 0, 0, Window.Width, Window.Height, new FastColour( 255, 255, 255, 100 ) ); controlsWidget.Render( delta ); gameWidget.Render( delta ); exitWidget.Render( delta ); + keyStatusWidget.Render( delta ); for( int i = 0; i < keysLeft.Length; i++ ) { keysLeft[i].Render( delta ); } @@ -26,9 +28,10 @@ namespace ClassicalSharp { } public override void Init() { - controlsWidget = CreateTextWidget( 0, 30, "&eControls list", Docking.LeftOrTop, 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 ); + controlsWidget = CreateTextWidget( 0, 30, "&eControls list", Docking.Centre, Docking.LeftOrTop, 16, FontStyle.Bold ); + keyStatusWidget = CreateTextWidget( 0, -80, "", Docking.Centre, Docking.BottomOrRight, 13, FontStyle.Italic ); + gameWidget = CreateTextWidget( 0, -50, "&eBack to game", Docking.Centre, Docking.BottomOrRight, 16, FontStyle.Bold ); + exitWidget = CreateTextWidget( 0, -10, "&eExit", Docking.Centre, Docking.BottomOrRight, 16, FontStyle.Bold ); KeyMapping[] mappingsLeft = { KeyMapping.Forward, KeyMapping.Back, KeyMapping.Left, KeyMapping.Right, KeyMapping.Jump, KeyMapping.Respawn, KeyMapping.SetSpawn, KeyMapping.OpenChat, KeyMapping.SendChat, @@ -37,36 +40,38 @@ namespace ClassicalSharp { "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[] 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 ); } + int leftEnd; void MakeKeysLeft( KeyMapping[] mappings, string[] descriptions ) { int startY = controlsWidget.BottomRight.Y + 10; - keysLeft = new TextWidget[mappings.Length]; + keysLeft = new KeyMapWidget[mappings.Length]; for( int i = 0; i < keysLeft.Length; i++ ) { 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; + TextWidget widget = CreateTextWidget( 0, startY, text, Docking.LeftOrTop, Docking.LeftOrTop, 14, FontStyle.Bold ); + widget.XOffset = 10; widget.MoveTo( widget.X + widget.XOffset, widget.Y ); - keysLeft[i] = widget; + keysLeft[i] = new KeyMapWidget( widget, mappings[i], descriptions[i] ); startY += widget.Height + 5; } + leftEnd = CalculateMaxWidth( keysLeft ); } void MakeKeysRight( KeyMapping[] mappings, string[] descriptions ) { int startY = controlsWidget.BottomRight.Y + 10; - keysRight = new TextWidget[mappings.Length]; + keysRight = new KeyMapWidget[mappings.Length]; for( int i = 0; i < keysRight.Length; i++ ) { 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; + TextWidget widget = CreateTextWidget( 0, startY, text, Docking.LeftOrTop, Docking.LeftOrTop, 14, FontStyle.Bold ); + widget.XOffset = leftEnd + 30; widget.MoveTo( widget.X + widget.XOffset, widget.Y ); - keysRight[i] = widget; + keysRight[i] = new KeyMapWidget( widget, mappings[i], descriptions[i] ); startY += widget.Height + 5; } } @@ -83,11 +88,11 @@ namespace ClassicalSharp { } } - TextWidget CreateTextWidget( int x, int y, string text, Docking vertical, int fontSize, FontStyle style ) { + TextWidget CreateTextWidget( int x, int y, string text, Docking horizontal, Docking vertical, int fontSize, FontStyle style ) { TextWidget widget = new TextWidget( Window, fontSize ); widget.Style = style; widget.Init(); - widget.HorizontalDocking = Docking.Centre; + widget.HorizontalDocking = horizontal; widget.VerticalDocking = vertical; widget.XOffset = x; widget.YOffset = y; @@ -99,6 +104,7 @@ namespace ClassicalSharp { gameWidget.OnResize( oldWidth, oldHeight, width, height ); controlsWidget.OnResize( oldWidth, oldHeight, width, height ); exitWidget.OnResize( oldWidth, oldHeight, width, height ); + keyStatusWidget.OnResize( oldWidth, oldHeight, width, height ); for( int i = 0; i < keysLeft.Length; i++ ) { keysLeft[i].OnResize( oldWidth, oldHeight, width, height ); } @@ -111,6 +117,49 @@ namespace ClassicalSharp { get { return true; } } + public override bool HandlesKeyDown( Key key ) { + if( widgetToChange != null ) { + KeyMapWidget widget = widgetToChange; + widgetToChange = null; + string reason; + if( !Window.Keys.IsKeyOkay( key, out reason ) ) { + const string format = "&eFailed to change mapping \"{0}\". &c({1})"; + keyStatusWidget.SetText( String.Format( format, widget.Description, reason ) ); + } else { + Key oldKey = Window.Keys[widget.Mapping]; + const string format = "&eChanged mapping \"{0}\" from &7{1} &eto &7{2}&e."; + keyStatusWidget.SetText( String.Format( format, widget.Description, oldKey, key ) ); + Window.Keys[widget.Mapping] = key; + widget.Widget.SetText( widget.Description + ": " + key ); + if( Array.IndexOf( keysLeft, widget ) >= 0 ) { + ResizeKeysRight(); + } + } + } + return true; + } + + void ResizeKeysRight() { + int newLeftEnd = CalculateMaxWidth( keysLeft ); + if( newLeftEnd != leftEnd ) { + int diff = newLeftEnd - leftEnd; + for( int i = 0; i < keysRight.Length; i++ ) { + TextWidget textWidget = keysRight[i].Widget; + textWidget.XOffset = newLeftEnd + 30; + textWidget.MoveTo( textWidget.X + diff, textWidget.Y ); + } + } + leftEnd = newLeftEnd; + } + + int CalculateMaxWidth( KeyMapWidget[] widgets ) { + int maxWidth = 0; + for( int i = 0; i < widgets.Length; i++ ) { + maxWidth = Math.Max( widgets[i].Widget.Width, maxWidth ); + } + return maxWidth; + } + public override bool HandlesMouseClick( int mouseX, int mouseY, MouseButton button ) { if( button != MouseButton.Left ) return false; if( exitWidget.ContainsPoint( mouseX, mouseY ) ) { @@ -119,8 +168,59 @@ namespace ClassicalSharp { } else if( gameWidget.ContainsPoint( mouseX, mouseY ) ) { Window.SetNewScreen( new NormalScreen( Window ) ); return true; + } else if( widgetToChange == null ) { + for( int i = 0; i < keysLeft.Length; i++ ) { + KeyMapWidget widget = keysLeft[i]; + if( widget.Widget.ContainsPoint( mouseX, mouseY ) ) { + SetWidgetToChange( widget ); + return true; + } + } + for( int i = 0; i < keysRight.Length; i++ ) { + KeyMapWidget widget = keysRight[i]; + if( widget.Widget.ContainsPoint( mouseX, mouseY ) ) { + SetWidgetToChange( widget ); + return true; + } + } } return false; } + + void SetWidgetToChange( KeyMapWidget widget ) { + Key oldKey = Window.Keys[widget.Mapping]; + if( !Window.Keys.IsLockedKey( oldKey ) ) { + const string format = "&ePress new key for \"{0}\"."; + keyStatusWidget.SetText( String.Format( format, widget.Description ) ); + widgetToChange = widget; + } else { + const string format = "&cCannot change mapping of &e\"{0}\"."; + keyStatusWidget.SetText( String.Format( format, widget.Description ) ); + } + } + + class KeyMapWidget { + public TextWidget Widget; + public KeyMapping Mapping; + public string Description; + + public KeyMapWidget( TextWidget widget, KeyMapping mapping, string desc ) { + Widget = widget; + Mapping = mapping; + Description = desc; + } + + public void Render( double delta ) { + Widget.Render( delta ); + } + + public void OnResize( int oldWidth, int oldHeight, int width, int height ) { + Widget.OnResize( oldWidth, oldHeight, width, height ); + } + + public void Dispose() { + Widget.Dispose(); + } + } } } diff --git a/ClassicalSharp.csproj b/ClassicalSharp.csproj index d8f51801b..5675566a4 100644 --- a/ClassicalSharp.csproj +++ b/ClassicalSharp.csproj @@ -1,184 +1,184 @@ - - - - {BEB1C785-5CAD-48FF-A886-876BF0A318D4} - Debug - AnyCPU - Exe - ClassicalSharp - ClassicalSharp - v2.0 - Properties - True - False - 4 - false - - - - - - 3.5 - publish\ - true - Disk - false - Foreground - 7 - Days - false - false - true - 0 - 1.0.0.%2a - false - false - true - False - - - AnyCPU - False - Auto - 4194304 - 4096 - - - output\debug\ - true - Full - False - False - DEBUG;TRACE - Project - wwwf null 127.0.0.1 25566 - - - output\release\ - false - None - True - False - TRACE - - - - - OpenTK.dll - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - False - Microsoft .NET Framework 4 %28x86 and x64%29 - true - - - False - .NET Framework 3.5 SP1 Client Profile - false - - - False - .NET Framework 3.5 SP1 - false - - - False - Windows Installer 3.1 - true - - - - - - - - - - - - - - - + + + + {BEB1C785-5CAD-48FF-A886-876BF0A318D4} + Debug + AnyCPU + Exe + ClassicalSharp + ClassicalSharp + v2.0 + Properties + True + False + 4 + false + + + + + + 3.5 + publish\ + true + Disk + false + Foreground + 7 + Days + false + false + true + 0 + 1.0.0.%2a + false + false + true + False + + + AnyCPU + False + Auto + 4194304 + 4096 + + + output\debug\ + true + Full + False + False + DEBUG;TRACE + Project + wwwf null 127.0.0.1 25566 + + + output\release\ + false + None + True + False + TRACE + + + + + OpenTK.dll + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + False + Microsoft .NET Framework 4 %28x86 and x64%29 + true + + + False + .NET Framework 3.5 SP1 Client Profile + false + + + False + .NET Framework 3.5 SP1 + false + + + False + Windows Installer 3.1 + true + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Game/Game.InputHandling.cs b/Game/Game.InputHandling.cs index 72b1a7881..d41423a2c 100644 --- a/Game/Game.InputHandling.cs +++ b/Game/Game.InputHandling.cs @@ -79,7 +79,7 @@ namespace ClassicalSharp { } else if( key == Keys[KeyMapping.Fullscreen] ) { WindowState state = WindowState; if( state != WindowState.Minimized ) { - WindowState = state == WindowState.Fullscreen ? + WindowState = state == WindowState.Fullscreen ? WindowState.Normal : WindowState.Fullscreen; } } else if( key == Keys[KeyMapping.ThirdPersonCamera] ) { @@ -147,58 +147,54 @@ namespace ClassicalSharp { return block == 0 || ( !CanPlace[block] && !CanDelete[block] && BlockInfo.IsLiquid( block ) ); } - public KeyMap Keys = new KeyMap(); + public KeyMap Keys = new KeyMap(); } public class KeyMap { - - public Key this[KeyMapping key] { - get { return Keys[(int)key]; } - set { Keys[(int)key] = 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 Key this[KeyMapping key] { + get { return Keys[(int)key]; } + set { Keys[(int)key] = value; } } - 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, + 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 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 IsLockedKey( Key key ) { + return key == Key.Escape || ( key >= Key.F1 && key <= Key.F35 ); + } + + public bool IsKeyOkay( Key key, out string reason ) { + 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, + } } \ No newline at end of file