Don't clamp the cursor to the centre of the block when scrolling in the inventory screen. (Thanks FabTheZen)

This commit is contained in:
UnknownShadow200 2016-04-01 10:24:47 +11:00
parent 785222cc8f
commit 3f146a15e6
7 changed files with 117 additions and 95 deletions

View File

@ -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();
}
}
}

View File

@ -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;
}

View File

@ -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();
}
}
}

View File

@ -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++ ) {

View File

@ -86,14 +86,15 @@
<Compile Include="2D\Drawing\DrawTextArgs.cs" />
<Compile Include="2D\Drawing\GdiPlusDrawer2D.cs" />
<Compile Include="2D\Drawing\IDrawer2D.cs" />
<Compile Include="2D\Screens\BlockSelectScreen.cs" />
<Compile Include="2D\Screens\BlockSelectScreen.Scrolling.cs" />
<Compile Include="2D\Screens\ChatScreen.cs" />
<Compile Include="2D\Screens\ClickableScreen.cs" />
<Compile Include="2D\Screens\ErrorScreen.cs" />
<Compile Include="2D\Screens\FilesScreen.cs" />
<Compile Include="2D\Screens\FpsScreen.cs" />
<Compile Include="2D\Screens\HotkeyScreen.cs" />
<Compile Include="2D\Screens\Inventory\InventoryScreen.cs" />
<Compile Include="2D\Screens\Inventory\InventoryScreen.Input.cs" />
<Compile Include="2D\Screens\Inventory\InventoryScreen.Scrolling.cs" />
<Compile Include="2D\Screens\LoadingMapScreen.cs" />
<Compile Include="2D\Screens\Menu\ClassicOptionsScreen.cs" />
<Compile Include="2D\Screens\Menu\EnvSettingsScreen.cs" />
@ -291,6 +292,7 @@
</ItemGroup>
<ItemGroup>
<Folder Include="2D\Drawing" />
<Folder Include="2D\Screens\Inventory" />
<Folder Include="2D\Utils" />
<Folder Include="2D\Screens" />
<Folder Include="2D\Screens\Menu" />

View File

@ -78,6 +78,9 @@ namespace ClassicalSharp.Entities {
/// <summary> Determines whether any of the blocks that intersect the
/// bounding box of this entity are water or still water. </summary>
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,

View File

@ -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 {