@ -654,10 +654,10 @@ Point InvGetEquipSlotCoordFromInvSlot(const inv_xy_slot slot)
Point GetSlotCoord ( int slot )
Point GetSlotCoord ( int slot )
{
{
if ( slot > = SLOTXY_BELT_FIRST & & slot < = SLOTXY_BELT_LAST ) {
if ( slot > = SLOTXY_BELT_FIRST & & slot < = SLOTXY_BELT_LAST ) {
return GetPanelPosition ( UiPanels : : Main , InvRect [ slot ] . position ) ;
return GetPanelPosition ( UiPanels : : Main , InvRect [ slot ] . Center ( ) ) ;
}
}
return GetPanelPosition ( UiPanels : : Inventory , InvRect [ slot ] . position ) ;
return GetPanelPosition ( UiPanels : : Inventory , InvRect [ slot ] . Center ( ) ) ;
}
}
/**
/**
@ -733,25 +733,12 @@ void ResetInvCursorPosition()
} else {
} else {
mousePos = GetSlotCoord ( Slot ) ;
mousePos = GetSlotCoord ( Slot ) ;
}
}
if ( ! MyPlayer - > HoldItem . isEmpty ( ) ) {
mousePos + = Displacement { - INV_SLOT_HALF_SIZE_PX , - INV_SLOT_HALF_SIZE_PX } ;
}
} else if ( Slot > = SLOTXY_BELT_FIRST & & Slot < = SLOTXY_BELT_LAST ) {
} else if ( Slot > = SLOTXY_BELT_FIRST & & Slot < = SLOTXY_BELT_LAST ) {
mousePos = GetSlotCoord ( Slot ) ;
mousePos = GetSlotCoord ( Slot ) ;
if ( ! MyPlayer - > HoldItem . isEmpty ( ) )
mousePos + = Displacement { - INV_SLOT_HALF_SIZE_PX , - INV_SLOT_HALF_SIZE_PX } ;
} else {
} else {
mousePos = InvGetEquipSlotCoordFromInvSlot ( ( inv_xy_slot ) Slot ) ;
mousePos = InvGetEquipSlotCoordFromInvSlot ( ( inv_xy_slot ) Slot ) ;
if ( ! MyPlayer - > HoldItem . isEmpty ( ) ) {
Size itemSize = GetInventorySize ( MyPlayer - > HoldItem ) ;
mousePos + = Displacement { - INV_SLOT_HALF_SIZE_PX , - INV_SLOT_HALF_SIZE_PX * itemSize . height } ;
}
}
}
mousePos . x + = ( InventorySlotSizeInPixels . width / 2 ) ;
mousePos . y - = ( InventorySlotSizeInPixels . height / 2 ) ;
SetCursorPos ( mousePos ) ;
SetCursorPos ( mousePos ) ;
}
}
@ -759,7 +746,6 @@ int FindClosestInventorySlot(Point mousePos)
{
{
int shortestDistance = std : : numeric_limits < int > : : max ( ) ;
int shortestDistance = std : : numeric_limits < int > : : max ( ) ;
int bestSlot = 0 ;
int bestSlot = 0 ;
mousePos + = Displacement { - INV_SLOT_HALF_SIZE_PX , INV_SLOT_HALF_SIZE_PX } ;
for ( int i = 0 ; i < NUM_XY_SLOTS ; i + + ) {
for ( int i = 0 ; i < NUM_XY_SLOTS ; i + + ) {
int distance = mousePos . ManhattanDistance ( GetSlotCoord ( i ) ) ;
int distance = mousePos . ManhattanDistance ( GetSlotCoord ( i ) ) ;
@ -776,7 +762,6 @@ Point FindClosestStashSlot(Point mousePos)
{
{
int shortestDistance = std : : numeric_limits < int > : : max ( ) ;
int shortestDistance = std : : numeric_limits < int > : : max ( ) ;
Point bestSlot = { } ;
Point bestSlot = { } ;
mousePos + = Displacement { - INV_SLOT_HALF_SIZE_PX , - INV_SLOT_HALF_SIZE_PX } ;
for ( Point point : PointsInRectangle ( Rectangle { { 0 , 0 } , Size { 10 , 10 } } ) ) {
for ( Point point : PointsInRectangle ( Rectangle { { 0 , 0 } , Size { 10 , 10 } } ) ) {
int distance = mousePos . ManhattanDistance ( GetStashSlotCoord ( point ) ) ;
int distance = mousePos . ManhattanDistance ( GetStashSlotCoord ( point ) ) ;
@ -819,7 +804,7 @@ void InventoryMove(AxisDirection dir)
const Item & heldItem = MyPlayer - > HoldItem ;
const Item & heldItem = MyPlayer - > HoldItem ;
const bool isHoldingItem = ! heldItem . isEmpty ( ) ;
const bool isHoldingItem = ! heldItem . isEmpty ( ) ;
Size itemSize = GetInventorySize ( heldItem ) ;
Size itemSize = isHoldingItem ? GetInventorySize ( heldItem ) : Size { 1 } ;
// when item is on cursor (pcurs > 1), this is the real cursor XY
// when item is on cursor (pcurs > 1), this is the real cursor XY
if ( dir . x = = AxisDirectionX_LEFT ) {
if ( dir . x = = AxisDirectionX_LEFT ) {
@ -1006,28 +991,27 @@ void InventoryMove(AxisDirection dir)
} else {
} else {
mousePos = GetSlotCoord ( Slot ) ;
mousePos = GetSlotCoord ( Slot ) ;
}
}
// move cursor to the center of the slot if not holding anything or top left is holding an object
// If we're in the inventory we may need to move the cursor to an area that doesn't line up with the center of a cell
if ( isHoldingItem ) {
if ( Slot > = SLOTXY_INV_FIRST & & Slot < = SLOTXY_INV_LAST ) {
if ( Slot < SLOTXY_INV_FIRST ) {
if ( ! isHoldingItem ) {
// The coordinates we get for body slots are based on the centre of the region relative to the hand cursor
// If we're not holding an item
// Need to adjust the position for items larger than 1x1 so they're aligned as expected
mousePos . x - = itemSize . width * INV_SLOT_HALF_SIZE_PX ;
mousePos . y - = itemSize . height * INV_SLOT_HALF_SIZE_PX ;
}
} else {
// get item under new slot if navigating on the inventory
if ( Slot > = SLOTXY_INV_FIRST & & Slot < = SLOTXY_BELT_LAST ) {
int8_t itemInvId = GetItemIdOnSlot ( Slot ) ;
int8_t itemInvId = GetItemIdOnSlot ( Slot ) ;
int itemSlot = FindFirstSlotOnItem ( itemInvId ) ;
if ( itemInvId ! = 0 ) {
if ( itemSlot < 0 )
// but the cursor moved over an item
itemSlot = Slot ;
int itemSlot = FindFirstSlotOnItem ( itemInvId ) ;
if ( itemSlot < 0 )
// offset the cursor so it shows over the center of the item
itemSlot = Slot ;
mousePos = GetSlotCoord ( itemSlot ) ;
itemSize = GetItemSizeOnSlot ( itemSlot ) ;
// then we need to offset the cursor so it shows over the center of the item
mousePos . x + = ( itemSize . width * InventorySlotSizeInPixels . width ) / 2 ;
mousePos = GetSlotCoord ( itemSlot ) ;
mousePos . y + = ( itemSize . height * InventorySlotSizeInPixels . height ) / 2 ;
itemSize = GetItemSizeOnSlot ( itemSlot ) ;
}
}
}
// At this point itemSize is either the size of the cell/item the hand cursor is over, or the size of the item we're currently holding.
// mousePos is the center of the top left cell of the item under the hand cursor, or the top left cell of the region that could fit the item we're holding.
// either way we need to offset the mouse position to account for items (we're holding or hovering over) with a dimension larger than a single cell.
mousePos . x + = ( ( itemSize . width - 1 ) * InventorySlotSizeInPixels . width ) / 2 ;
mousePos . y + = ( ( itemSize . height - 1 ) * InventorySlotSizeInPixels . height ) / 2 ;
}
}
if ( mousePos = = MousePosition ) {
if ( mousePos = = MousePosition ) {
@ -1826,36 +1810,39 @@ void PerformPrimaryAction()
} else if ( GetRightPanel ( ) . contains ( MousePosition ) | | GetMainPanel ( ) . contains ( MousePosition ) ) {
} else if ( GetRightPanel ( ) . contains ( MousePosition ) | | GetMainPanel ( ) . contains ( MousePosition ) ) {
int inventorySlot = ( Slot > = 0 ) ? Slot : FindClosestInventorySlot ( MousePosition ) ;
int inventorySlot = ( Slot > = 0 ) ? Slot : FindClosestInventorySlot ( MousePosition ) ;
const Size cursorSizeInCells = MyPlayer - > HoldItem . isEmpty ( ) ? Size { 1 , 1 } : GetInventorySize ( MyPlayer - > HoldItem ) ;
int jumpSlot = inventorySlot ; // If the cursor is over an inventory slot we may need to adjust it due to pasting items of different sizes over each other
if ( inventorySlot > = SLOTXY_INV_FIRST & & inventorySlot < = SLOTXY_INV_LAST ) {
// Find any item occupying a slot that is currently under the cursor
const Size cursorSizeInCells = MyPlayer - > HoldItem . isEmpty ( ) ? Size { 1 , 1 } : GetInventorySize ( MyPlayer - > HoldItem ) ;
int8_t itemUnderCursor = [ ] ( int inventorySlot , Size cursorSizeInCells ) {
if ( inventorySlot < SLOTXY_INV_FIRST | | inventorySlot > SLOTXY_INV_LAST )
// Find any item occupying a slot that is currently under the cursor
return 0 ;
int8_t itemUnderCursor = [ ] ( int inventorySlot , Size cursorSizeInCells ) {
for ( int x = 0 ; x < cursorSizeInCells . width ; x + + ) {
if ( inventorySlot < SLOTXY_INV_FIRST | | inventorySlot > SLOTXY_INV_LAST )
for ( int y = 0 ; y < cursorSizeInCells . height ; y + + ) {
return 0 ;
int slotUnderCursor = inventorySlot + x + y * INV_ROW_SLOT_SIZE ;
for ( int x = 0 ; x < cursorSizeInCells . width ; x + + ) {
if ( slotUnderCursor > SLOTXY_INV_LAST )
for ( int y = 0 ; y < cursorSizeInCells . height ; y + + ) {
continue ;
int slotUnderCursor = inventorySlot + x + y * INV_ROW_SLOT_SIZE ;
int itemId = GetItemIdOnSlot ( slotUnderCursor ) ;
if ( slotUnderCursor > SLOTXY_INV_LAST )
if ( itemId ! = 0 )
continue ;
return itemId ;
int itemId = GetItemIdOnSlot ( slotUnderCursor ) ;
if ( itemId ! = 0 )
return itemId ;
}
}
}
}
return 0 ;
return 0 ;
} ( inventorySlot , cursorSizeInCells ) ;
} ( inventorySlot , cursorSizeInCells ) ;
// The cursor will need to be shifted to
// Capture the first slot of the first item (if any) under the cursor
// this slot if the item is swapped or lifted
if ( itemUnderCursor > 0 )
int jumpSlot = FindFirstSlotOnItem ( itemUnderCursor ) ;
jumpSlot = FindFirstSlotOnItem ( itemUnderCursor ) ;
}
CheckInvItem ( ) ;
CheckInvItem ( ) ;
// If we don't find the item in the same position as before,
if ( inventorySlot > = SLOTXY_INV_FIRST & & inventorySlot < = SLOTXY_INV_LAST ) {
// it suggests that the item was swapped or lifted
int newSlot = FindFirstSlotOnItem ( itemUnderCursor ) ;
if ( jumpSlot > = 0 & & jumpSlot ! = newSlot ) {
Point mousePos = GetSlotCoord ( jumpSlot ) ;
Point mousePos = GetSlotCoord ( jumpSlot ) ;
Slot = jumpSlot ;
Slot = jumpSlot ;
const Size newCursorSizeInCells = MyPlayer - > HoldItem . isEmpty ( ) ? GetItemSizeOnSlot ( jumpSlot ) : GetInventorySize ( MyPlayer - > HoldItem ) ;
mousePos . x + = ( ( newCursorSizeInCells . width - 1 ) * InventorySlotSizeInPixels . width ) / 2 ;
mousePos . y + = ( ( newCursorSizeInCells . height - 1 ) * InventorySlotSizeInPixels . height ) / 2 ;
SetCursorPos ( mousePos ) ;
SetCursorPos ( mousePos ) ;
}
}
} else if ( IsStashOpen & & GetLeftPanel ( ) . contains ( MousePosition ) ) {
} else if ( IsStashOpen & & GetLeftPanel ( ) . contains ( MousePosition ) ) {