From 3f146a15e6a6d9e75fd46f7afd3f02b7abefb574 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Fri, 1 Apr 2016 10:24:47 +1100 Subject: [PATCH] Don't clamp the cursor to the centre of the block when scrolling in the inventory screen. (Thanks FabTheZen) --- .../Inventory/InventoryScreen.Input.cs | 89 +++++++++++++++ .../InventoryScreen.Scrolling.cs} | 8 +- .../InventoryScreen.cs} | 102 +++--------------- .../2D/Widgets/BlockHotbarWidget.cs | 2 +- ClassicalSharp/ClassicalSharp.csproj | 6 +- ClassicalSharp/Entities/Entity.Bounds.cs | 3 + ClassicalSharp/Game/InputHandler.cs | 2 +- 7 files changed, 117 insertions(+), 95 deletions(-) create mode 100644 ClassicalSharp/2D/Screens/Inventory/InventoryScreen.Input.cs rename ClassicalSharp/2D/Screens/{BlockSelectScreen.Scrolling.cs => Inventory/InventoryScreen.Scrolling.cs} (90%) rename ClassicalSharp/2D/Screens/{BlockSelectScreen.cs => Inventory/InventoryScreen.cs} (71%) diff --git a/ClassicalSharp/2D/Screens/Inventory/InventoryScreen.Input.cs b/ClassicalSharp/2D/Screens/Inventory/InventoryScreen.Input.cs new file mode 100644 index 000000000..5d059c4bf --- /dev/null +++ b/ClassicalSharp/2D/Screens/Inventory/InventoryScreen.Input.cs @@ -0,0 +1,89 @@ +// ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT +using System; +using System.Drawing; +using ClassicalSharp.GraphicsAPI; +using OpenTK.Input; + +namespace ClassicalSharp.Gui { + + public partial class InventoryScreen : Screen { + + public override bool HandlesAllInput { get { return true; } } + + public override bool HandlesMouseMove( int mouseX, int mouseY ) { + if( draggingMouse ) { + mouseY -= TableY; + scrollY = (int)((mouseY - mouseOffset) / ScrollbarScale); + ClampScrollY(); + return true; + } + + selIndex = -1; + if( Contains( startX, startY, blocksPerRow * blockSize, + maxRows * blockSize, mouseX, mouseY ) ) { + for( int i = 0; i < blocksTable.Length; i++ ) { + int x, y; + GetCoords( i, out x, out y ); + + if( Contains( x, y, blockSize, blockSize, mouseX, mouseY ) ) { + selIndex = i; + break; + } + } + } + RecreateBlockInfoTexture(); + return true; + } + + public override bool HandlesMouseClick( int mouseX, int mouseY, MouseButton button ) { + if( draggingMouse || game.hudScreen.hotbar.HandlesMouseClick( mouseX, mouseY, button ) ) + return true; + if( button == MouseButton.Left && mouseX >= TableX && mouseX < TableX + scrollbarWidth ) { + ScrollbarClick( mouseY ); + } else if( button == MouseButton.Left ) { + if( selIndex != -1 ) + game.Inventory.HeldBlock = blocksTable[selIndex]; + else if( Contains( TableX, TableY, TableWidth, TableHeight, mouseX, mouseY ) ) + return true; + game.SetNewScreen( null ); + } + return true; + } + + public override bool HandlesKeyDown( Key key ) { + if( key == game.Mapping( KeyBinding.PauseOrExit ) || + key == game.Mapping( KeyBinding.OpenInventory ) ) { + game.SetNewScreen( null ); + } else if( key == Key.Enter && selIndex != -1 ) { + game.Inventory.HeldBlock = blocksTable[selIndex]; + game.SetNewScreen( null ); + } else if( (key == Key.Left || key == Key.Keypad4) && selIndex != -1 ) { + ArrowKeyMove( -1 ); + } else if( (key == Key.Right || key == Key.Keypad6) && selIndex != -1 ) { + ArrowKeyMove( 1 ); + } else if( (key == Key.Up || key == Key.Keypad8) && selIndex != -1 ) { + ArrowKeyMove( -blocksPerRow ); + } else if( (key == Key.Down || key == Key.Keypad2) && selIndex != -1 ) { + ArrowKeyMove( blocksPerRow ); + } else if( key >= Key.Number1 && key <= Key.Number9 ) { + game.Inventory.HeldBlockIndex = (int)key - (int)Key.Number1; + } + return true; + } + + void ArrowKeyMove( int delta ) { + int startIndex = selIndex; + selIndex += delta; + if( selIndex < 0 ) + selIndex -= delta; + if( selIndex >= blocksTable.Length ) + selIndex -= delta; + + int scrollDelta = (selIndex / blocksPerRow) - (startIndex / blocksPerRow); + scrollY += scrollDelta; + ClampScrollY(); + RecreateBlockInfoTexture(); + MoveCursorToSelected(); + } + } +} diff --git a/ClassicalSharp/2D/Screens/BlockSelectScreen.Scrolling.cs b/ClassicalSharp/2D/Screens/Inventory/InventoryScreen.Scrolling.cs similarity index 90% rename from ClassicalSharp/2D/Screens/BlockSelectScreen.Scrolling.cs rename to ClassicalSharp/2D/Screens/Inventory/InventoryScreen.Scrolling.cs index 840f955d7..3c0b6457e 100644 --- a/ClassicalSharp/2D/Screens/BlockSelectScreen.Scrolling.cs +++ b/ClassicalSharp/2D/Screens/Inventory/InventoryScreen.Scrolling.cs @@ -1,10 +1,11 @@ // ClassicalSharp copyright 2014-2016 UnknownShadow200 | Licensed under MIT using System; +using System.Drawing; using OpenTK.Input; namespace ClassicalSharp.Gui { - public partial class BlockSelectScreen : Screen { + public partial class InventoryScreen : Screen { const int scrollbarWidth = 10; static FastColour scrollCol = new FastColour( 10, 10, 10, 220 ); @@ -36,10 +37,11 @@ namespace ClassicalSharp.Gui { selIndex = scrollY * blocksPerRow + (selIndex % blocksPerRow); for( int row = 0; row < rowY; row++ ) { - if( selIndex + blocksPerRow >= blocksTable.Length ) break; selIndex += blocksPerRow; } - MoveCursorToSelected(); + + if( selIndex >= blocksTable.Length ) + selIndex = -1; RecreateBlockInfoTexture(); return true; } diff --git a/ClassicalSharp/2D/Screens/BlockSelectScreen.cs b/ClassicalSharp/2D/Screens/Inventory/InventoryScreen.cs similarity index 71% rename from ClassicalSharp/2D/Screens/BlockSelectScreen.cs rename to ClassicalSharp/2D/Screens/Inventory/InventoryScreen.cs index d9f43b962..08ab6774c 100644 --- a/ClassicalSharp/2D/Screens/BlockSelectScreen.cs +++ b/ClassicalSharp/2D/Screens/Inventory/InventoryScreen.cs @@ -6,15 +6,15 @@ using OpenTK.Input; namespace ClassicalSharp.Gui { - public partial class BlockSelectScreen : Screen { + public partial class InventoryScreen : Screen { - public BlockSelectScreen( Game game ) : base( game ) { + public InventoryScreen( Game game ) : base( game ) { font = new Font( game.FontName, 13 ); } Block[] blocksTable; Texture blockInfoTexture; - const int blocksPerRow = 10, maxRows = 8; + const int blocksPerRow = 10, maxRows = 4; int selIndex, rows; int startX, startY, blockSize; float selBlockExpand; @@ -69,6 +69,16 @@ namespace ClassicalSharp.Gui { return row >= 0 && row < maxRows; } + Point GetMouseCoords( int i ) { + int x, y; + GetCoords( i, out x, out y ); + x += blockSize / 2; y += blockSize / 2; + + Point topLeft = game.PointToScreen( Point.Empty ); + x += topLeft.X; y += topLeft.Y; + return new Point( x, y ); + } + public override void Dispose() { font.Dispose(); api.DeleteTexture( ref blockInfoTexture ); @@ -107,13 +117,7 @@ namespace ClassicalSharp.Gui { void MoveCursorToSelected() { if( selIndex == -1 ) return; - int x, y; - GetCoords( selIndex, out x, out y ); - x += blockSize / 2; y += blockSize / 2; - - Point topLeft = game.PointToScreen( Point.Empty ); - x += topLeft.X; y += topLeft.Y; - game.DesktopCursorPos = new Point( x, y ); + game.DesktopCursorPos = GetMouseCoords( selIndex ); } void BlockPermissionsChanged( object sender, EventArgs e ) { @@ -220,83 +224,5 @@ namespace ClassicalSharp.Gui { return false; return tile < BlockInfo.CpeBlocksCount || game.BlockInfo.Name[tile] != "Invalid"; } - - public override bool HandlesAllInput { get { return true; } } - - public override bool HandlesMouseMove( int mouseX, int mouseY ) { - if( draggingMouse ) { - mouseY -= TableY; - scrollY = (int)((mouseY - mouseOffset) / ScrollbarScale); - ClampScrollY(); - return true; - } - - selIndex = -1; - if( Contains( startX, startY, blocksPerRow * blockSize, - maxRows * blockSize, mouseX, mouseY ) ) { - for( int i = 0; i < blocksTable.Length; i++ ) { - int x, y; - GetCoords( i, out x, out y ); - - if( Contains( x, y, blockSize, blockSize, mouseX, mouseY ) ) { - selIndex = i; - break; - } - } - } - RecreateBlockInfoTexture(); - return true; - } - - public override bool HandlesMouseClick( int mouseX, int mouseY, MouseButton button ) { - if( draggingMouse || game.hudScreen.hotbar.HandlesMouseClick( mouseX, mouseY, button ) ) - return true; - if( button == MouseButton.Left && mouseX >= TableX && mouseX < TableX + scrollbarWidth ) { - ScrollbarClick( mouseY ); - } else if( button == MouseButton.Left ) { - if( selIndex != -1 ) - game.Inventory.HeldBlock = blocksTable[selIndex]; - else if( Contains( TableX, TableY, TableWidth, TableHeight, mouseX, mouseY ) ) - return true; - game.SetNewScreen( null ); - } - return true; - } - - public override bool HandlesKeyDown( Key key ) { - if( key == game.Mapping( KeyBinding.PauseOrExit ) || - key == game.Mapping( KeyBinding.OpenInventory ) ) { - game.SetNewScreen( null ); - } else if( key == Key.Enter && selIndex != -1 ) { - game.Inventory.HeldBlock = blocksTable[selIndex]; - game.SetNewScreen( null ); - } else if( (key == Key.Left || key == Key.Keypad4) && selIndex != -1 ) { - ArrowKeyMove( -1 ); - } else if( (key == Key.Right || key == Key.Keypad6) && selIndex != -1 ) { - ArrowKeyMove( 1 ); - } else if( (key == Key.Up || key == Key.Keypad8) && selIndex != -1 ) { - ArrowKeyMove( -blocksPerRow ); - } else if( (key == Key.Down || key == Key.Keypad2) && selIndex != -1 ) { - ArrowKeyMove( blocksPerRow ); - } else if( key >= Key.Number1 && key <= Key.Number9 ) { - game.Inventory.HeldBlockIndex = (int)key - (int)Key.Number1; - } - return true; - } - - void ArrowKeyMove( int delta ) { - int startIndex = selIndex; - selIndex += delta; - if( selIndex < 0 ) - selIndex -= delta; - if( selIndex >= blocksTable.Length ) - selIndex -= delta; - - int scrollDelta = (selIndex / blocksPerRow) - (startIndex / blocksPerRow); - scrollY += scrollDelta; - ClampScrollY(); - RecreateBlockInfoTexture(); - MoveCursorToSelected(); - } } } diff --git a/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs b/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs index 2fa3b67f1..b2dec36f1 100644 --- a/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs +++ b/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs @@ -97,7 +97,7 @@ namespace ClassicalSharp.Gui { public override bool HandlesMouseClick( int mouseX, int mouseY, MouseButton button ) { if( button != MouseButton.Left || !Bounds.Contains( mouseX, mouseY ) ) return false; - BlockSelectScreen screen = game.GetActiveScreen as BlockSelectScreen; + InventoryScreen screen = game.GetActiveScreen as InventoryScreen; if( screen == null ) return false; for( int i = 0; i < hotbarCount; i++ ) { diff --git a/ClassicalSharp/ClassicalSharp.csproj b/ClassicalSharp/ClassicalSharp.csproj index 555c6969d..f931cb577 100644 --- a/ClassicalSharp/ClassicalSharp.csproj +++ b/ClassicalSharp/ClassicalSharp.csproj @@ -86,14 +86,15 @@ - - + + + @@ -291,6 +292,7 @@ + diff --git a/ClassicalSharp/Entities/Entity.Bounds.cs b/ClassicalSharp/Entities/Entity.Bounds.cs index ef7b6c553..1932417fb 100644 --- a/ClassicalSharp/Entities/Entity.Bounds.cs +++ b/ClassicalSharp/Entities/Entity.Bounds.cs @@ -78,6 +78,9 @@ namespace ClassicalSharp.Entities { /// Determines whether any of the blocks that intersect the /// bounding box of this entity are water or still water. public bool TouchesAnyWater() { + // TODO: proper way to check seems to be: + // If liquid block above, leave height same + // otherwise reduce water BB height by 0.5 blocks BoundingBox bounds = CollisionBounds.Expand( liqExpand ); AdjustLiquidTestBounds( ref bounds ); return TouchesAny( bounds, diff --git a/ClassicalSharp/Game/InputHandler.cs b/ClassicalSharp/Game/InputHandler.cs index b00de305b..0fcb6b992 100644 --- a/ClassicalSharp/Game/InputHandler.cs +++ b/ClassicalSharp/Game/InputHandler.cs @@ -350,7 +350,7 @@ namespace ClassicalSharp { } else if( key == Keys[KeyBinding.PauseOrExit] && !game.World.IsNotLoaded ) { game.SetNewScreen( new PauseScreen( game ) ); } else if( key == Keys[KeyBinding.OpenInventory] ) { - game.SetNewScreen( new BlockSelectScreen( game ) ); + game.SetNewScreen( new InventoryScreen( game ) ); } else if( key == Key.F9 ) { game.ShowClock = !game.ShowClock; } else {