Refactored window clicking code to use different click actions
First part of solving FS #371; should fix #370. git-svn-id: http://mc-server.googlecode.com/svn/trunk@1459 0a769ca7-a7f5-676a-18bf-c427514a06d6
This commit is contained in:
@@ -31,7 +31,7 @@ cSlotArea::cSlotArea(int a_NumSlots, cWindow & a_ParentWindow) :
|
||||
|
||||
|
||||
|
||||
void cSlotArea::Clicked(cPlayer & a_Player, int a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed, const cItem & a_ClickedItem)
|
||||
void cSlotArea::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem)
|
||||
{
|
||||
/*
|
||||
LOGD("Slot area with %d slots clicked at slot number %d, clicked item %s, slot item %s",
|
||||
@@ -50,94 +50,105 @@ void cSlotArea::Clicked(cPlayer & a_Player, int a_SlotNum, bool a_IsRightClick,
|
||||
return;
|
||||
}
|
||||
|
||||
if (a_IsShiftPressed)
|
||||
if ((a_ClickAction == caShiftLeftClick) || (a_ClickAction == caShiftRightClick))
|
||||
{
|
||||
if (!a_Player.IsDraggingItem())
|
||||
{
|
||||
ShiftClicked(a_Player, a_SlotNum, a_ClickedItem);
|
||||
return;
|
||||
}
|
||||
LOGD("Shift clicked, but the player is draggint an item: %s", ItemToFullString(a_Player.GetDraggingItem()).c_str());
|
||||
LOGD("Shift clicked, but the player is dragging an item: %s", ItemToFullString(a_Player.GetDraggingItem()).c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
cItem Slot(*GetSlot(a_SlotNum, a_Player));
|
||||
if (!Slot.IsSameType(a_ClickedItem))
|
||||
{
|
||||
LOGD("*** Window lost sync at item %d in SlotArea with %d items ***", a_SlotNum, m_NumSlots);
|
||||
LOGD("My item: %s", ItemToFullString(Slot).c_str());
|
||||
LOGD("Their item: %s", ItemToFullString(a_ClickedItem).c_str());
|
||||
LOGWARNING("*** Window lost sync at item %d in SlotArea with %d items ***", a_SlotNum, m_NumSlots);
|
||||
LOGWARNING("My item: %s", ItemToFullString(Slot).c_str());
|
||||
LOGWARNING("Their item: %s", ItemToFullString(a_ClickedItem).c_str());
|
||||
bAsync = true;
|
||||
}
|
||||
cItem & DraggingItem = a_Player.GetDraggingItem();
|
||||
if (a_IsRightClick)
|
||||
switch (a_ClickAction)
|
||||
{
|
||||
// Right clicked
|
||||
if (DraggingItem.m_ItemType <= 0) // Empty-handed?
|
||||
case caRightClick:
|
||||
{
|
||||
DraggingItem.m_ItemCount = (char)(((float)Slot.m_ItemCount) / 2.f + 0.5f);
|
||||
Slot.m_ItemCount -= DraggingItem.m_ItemCount;
|
||||
DraggingItem.m_ItemType = Slot.m_ItemType;
|
||||
DraggingItem.m_ItemDamage = Slot.m_ItemDamage;
|
||||
if (DraggingItem.m_ItemType <= 0) // Empty-handed?
|
||||
{
|
||||
DraggingItem.m_ItemCount = (char)(((float)Slot.m_ItemCount) / 2.f + 0.5f);
|
||||
Slot.m_ItemCount -= DraggingItem.m_ItemCount;
|
||||
DraggingItem.m_ItemType = Slot.m_ItemType;
|
||||
DraggingItem.m_ItemDamage = Slot.m_ItemDamage;
|
||||
|
||||
if (Slot.m_ItemCount <= 0)
|
||||
{
|
||||
Slot.Empty();
|
||||
if (Slot.m_ItemCount <= 0)
|
||||
{
|
||||
Slot.Empty();
|
||||
}
|
||||
}
|
||||
else if ((Slot.m_ItemType <= 0) || DraggingItem.IsEqual(Slot))
|
||||
{
|
||||
// Drop one item in slot
|
||||
cItemHandler * Handler = ItemHandler(Slot.m_ItemType);
|
||||
if ((DraggingItem.m_ItemCount > 0) && (Slot.m_ItemCount < Handler->GetMaxStackSize()))
|
||||
{
|
||||
Slot.m_ItemType = DraggingItem.m_ItemType;
|
||||
Slot.m_ItemCount++;
|
||||
Slot.m_ItemDamage = DraggingItem.m_ItemDamage;
|
||||
DraggingItem.m_ItemCount--;
|
||||
}
|
||||
if (DraggingItem.m_ItemCount <= 0)
|
||||
{
|
||||
DraggingItem.Empty();
|
||||
}
|
||||
}
|
||||
else if (!DraggingItem.IsEqual(Slot))
|
||||
{
|
||||
// Swap contents
|
||||
cItem tmp(DraggingItem);
|
||||
DraggingItem = Slot;
|
||||
Slot = tmp;
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if ((Slot.m_ItemType <= 0) || DraggingItem.IsEqual(Slot))
|
||||
|
||||
case caLeftClick:
|
||||
{
|
||||
// Drop one item in slot
|
||||
cItemHandler * Handler = ItemHandler(Slot.m_ItemType);
|
||||
if ((DraggingItem.m_ItemCount > 0) && (Slot.m_ItemCount < Handler->GetMaxStackSize()))
|
||||
// Left-clicked
|
||||
if (!DraggingItem.IsEqual(Slot))
|
||||
{
|
||||
Slot.m_ItemType = DraggingItem.m_ItemType;
|
||||
Slot.m_ItemCount++;
|
||||
Slot.m_ItemDamage = DraggingItem.m_ItemDamage;
|
||||
DraggingItem.m_ItemCount--;
|
||||
// Switch contents
|
||||
cItem tmp(DraggingItem);
|
||||
DraggingItem = Slot;
|
||||
Slot = tmp;
|
||||
}
|
||||
if (DraggingItem.m_ItemCount <= 0)
|
||||
else
|
||||
{
|
||||
DraggingItem.Empty();
|
||||
// Same type, add items:
|
||||
cItemHandler * Handler = ItemHandler(DraggingItem.m_ItemType);
|
||||
int FreeSlots = Handler->GetMaxStackSize() - Slot.m_ItemCount;
|
||||
if (FreeSlots < 0)
|
||||
{
|
||||
ASSERT(!"Bad item stack size - where did we get more items in a slot than allowed?");
|
||||
FreeSlots = 0;
|
||||
}
|
||||
int Filling = (FreeSlots > DraggingItem.m_ItemCount) ? DraggingItem.m_ItemCount : FreeSlots;
|
||||
Slot.m_ItemCount += (char)Filling;
|
||||
DraggingItem.m_ItemCount -= (char)Filling;
|
||||
if (DraggingItem.m_ItemCount <= 0)
|
||||
{
|
||||
DraggingItem.Empty();
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
else if (!DraggingItem.IsEqual(Slot))
|
||||
default:
|
||||
{
|
||||
// Swap contents
|
||||
cItem tmp(DraggingItem);
|
||||
DraggingItem = Slot;
|
||||
Slot = tmp;
|
||||
LOGWARNING("SlotArea: Unhandled click action: %d (%s)", a_ClickAction, ClickActionToString(a_ClickAction));
|
||||
m_ParentWindow.BroadcastWholeWindow();
|
||||
return;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Left-clicked
|
||||
if (!DraggingItem.IsEqual(Slot))
|
||||
{
|
||||
// Switch contents
|
||||
cItem tmp(DraggingItem);
|
||||
DraggingItem = Slot;
|
||||
Slot = tmp;
|
||||
}
|
||||
else
|
||||
{
|
||||
// Same type, add items:
|
||||
cItemHandler * Handler = ItemHandler(DraggingItem.m_ItemType);
|
||||
int FreeSlots = Handler->GetMaxStackSize() - Slot.m_ItemCount;
|
||||
if (FreeSlots < 0)
|
||||
{
|
||||
ASSERT(!"Bad item stack size - where did we get more items in a slot than allowed?");
|
||||
FreeSlots = 0;
|
||||
}
|
||||
int Filling = (FreeSlots > DraggingItem.m_ItemCount) ? DraggingItem.m_ItemCount : FreeSlots;
|
||||
Slot.m_ItemCount += (char)Filling;
|
||||
DraggingItem.m_ItemCount -= (char)Filling;
|
||||
if (DraggingItem.m_ItemCount <= 0)
|
||||
{
|
||||
DraggingItem.Empty();
|
||||
}
|
||||
}
|
||||
}
|
||||
} // switch (a_ClickAction
|
||||
|
||||
SetSlot(a_SlotNum, a_Player, Slot);
|
||||
if (bAsync)
|
||||
@@ -301,12 +312,12 @@ cSlotAreaCrafting::cSlotAreaCrafting(int a_GridSize, cWindow & a_ParentWindow) :
|
||||
|
||||
|
||||
|
||||
void cSlotAreaCrafting::Clicked(cPlayer & a_Player, int a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed, const cItem & a_ClickedItem)
|
||||
void cSlotAreaCrafting::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem)
|
||||
{
|
||||
// Override for craft result slot
|
||||
if (a_SlotNum == 0)
|
||||
{
|
||||
if (a_IsShiftPressed)
|
||||
if ((a_ClickAction == caShiftLeftClick) || (a_ClickAction == caShiftRightClick))
|
||||
{
|
||||
ShiftClickedResult(a_Player);
|
||||
}
|
||||
@@ -316,7 +327,7 @@ void cSlotAreaCrafting::Clicked(cPlayer & a_Player, int a_SlotNum, bool a_IsRigh
|
||||
}
|
||||
return;
|
||||
}
|
||||
super::Clicked(a_Player, a_SlotNum, a_IsRightClick, a_IsShiftPressed, a_ClickedItem);
|
||||
super::Clicked(a_Player, a_SlotNum, a_ClickAction, a_ClickedItem);
|
||||
UpdateRecipe(a_Player);
|
||||
}
|
||||
|
||||
@@ -475,9 +486,9 @@ cSlotAreaDispenser::cSlotAreaDispenser(cDispenserEntity * a_Dispenser, cWindow &
|
||||
|
||||
|
||||
|
||||
void cSlotAreaDispenser::Clicked(cPlayer & a_Player, int a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed, const cItem & a_ClickedItem)
|
||||
void cSlotAreaDispenser::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem)
|
||||
{
|
||||
super::Clicked(a_Player, a_SlotNum, a_IsRightClick, a_IsShiftPressed, a_ClickedItem);
|
||||
super::Clicked(a_Player, a_SlotNum, a_ClickAction, a_ClickedItem);
|
||||
|
||||
if (m_Dispenser == NULL)
|
||||
{
|
||||
@@ -522,11 +533,11 @@ cSlotAreaFurnace::cSlotAreaFurnace(cFurnaceEntity * a_Furnace, cWindow & a_Paren
|
||||
|
||||
|
||||
|
||||
void cSlotAreaFurnace::Clicked(cPlayer & a_Player, int a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed, const cItem & a_ClickedItem)
|
||||
void cSlotAreaFurnace::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem)
|
||||
{
|
||||
cItem Fuel = *GetSlot(0, a_Player);
|
||||
|
||||
super::Clicked(a_Player, a_SlotNum, a_IsRightClick, a_IsShiftPressed, a_ClickedItem);
|
||||
super::Clicked(a_Player, a_SlotNum, a_ClickAction, a_ClickedItem);
|
||||
|
||||
if (m_Furnace == NULL)
|
||||
{
|
||||
@@ -582,7 +593,7 @@ cSlotAreaInventoryBase::cSlotAreaInventoryBase(int a_NumSlots, int a_SlotOffset,
|
||||
|
||||
|
||||
|
||||
void cSlotAreaInventoryBase::Clicked(cPlayer & a_Player, int a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed, const cItem & a_ClickedItem)
|
||||
void cSlotAreaInventoryBase::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem)
|
||||
{
|
||||
if ((a_Player.GetGameMode() == eGameMode_Creative) && (m_ParentWindow.GetWindowType() == cWindow::Inventory))
|
||||
{
|
||||
@@ -592,7 +603,7 @@ void cSlotAreaInventoryBase::Clicked(cPlayer & a_Player, int a_SlotNum, bool a_I
|
||||
}
|
||||
|
||||
// Survival inventory and all other windows' inventory has the same handling as normal slot areas
|
||||
super::Clicked(a_Player, a_SlotNum, a_IsRightClick, a_IsShiftPressed, a_ClickedItem);
|
||||
super::Clicked(a_Player, a_SlotNum, a_ClickAction, a_ClickedItem);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ public:
|
||||
virtual void SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) = 0;
|
||||
|
||||
/// Called when a player clicks in the window. Parameters taken from the click packet.
|
||||
virtual void Clicked(cPlayer & a_Player, int a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed, const cItem & a_ClickedItem);
|
||||
virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem);
|
||||
|
||||
/// Called from Clicked if it is a valid shiftclick
|
||||
virtual void ShiftClicked(cPlayer & a_Player, int a_SlotNum, const cItem & a_ClickedItem);
|
||||
@@ -76,7 +76,7 @@ public:
|
||||
cSlotAreaInventoryBase(int a_NumSlots, int a_SlotOffset, cWindow & a_ParentWindow);
|
||||
|
||||
// Creative inventory's click handling is somewhat different from survival inventory's, handle that here:
|
||||
virtual void Clicked(cPlayer & a_Player, int a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed, const cItem & a_ClickedItem) override;
|
||||
virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override;
|
||||
|
||||
virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) override;
|
||||
virtual void SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;
|
||||
@@ -185,7 +185,7 @@ public:
|
||||
cSlotAreaCrafting(int a_GridSize, cWindow & a_ParentWindow);
|
||||
|
||||
// cSlotAreaTemporary overrides:
|
||||
virtual void Clicked (cPlayer & a_Player, int a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed, const cItem & a_ClickedItem) override;
|
||||
virtual void Clicked (cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override;
|
||||
virtual void OnPlayerRemoved(cPlayer & a_Player) override;
|
||||
|
||||
// Distributing items into this area is completely disabled
|
||||
@@ -258,7 +258,7 @@ class cSlotAreaDispenser :
|
||||
public:
|
||||
cSlotAreaDispenser(cDispenserEntity * a_Dispenser, cWindow & a_ParentWindow);
|
||||
|
||||
virtual void Clicked(cPlayer & a_Player, int a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed, const cItem & a_ClickedItem) override;
|
||||
virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override;
|
||||
virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) override;
|
||||
virtual void SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;
|
||||
|
||||
@@ -278,7 +278,7 @@ class cSlotAreaFurnace :
|
||||
public:
|
||||
cSlotAreaFurnace(cFurnaceEntity * a_Furnace, cWindow & a_ParentWindow);
|
||||
|
||||
virtual void Clicked(cPlayer & a_Player, int a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed, const cItem & a_ClickedItem) override;
|
||||
virtual void Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a_ClickAction, const cItem & a_ClickedItem) override;
|
||||
virtual const cItem * GetSlot(int a_SlotNum, cPlayer & a_Player) override;
|
||||
virtual void SetSlot(int a_SlotNum, cPlayer & a_Player, const cItem & a_Item) override;
|
||||
|
||||
|
||||
@@ -96,7 +96,7 @@ void cWindow::GetSlots(cPlayer & a_Player, cItems & a_Slots) const
|
||||
|
||||
void cWindow::Clicked(
|
||||
cPlayer & a_Player,
|
||||
int a_WindowID, short a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed,
|
||||
int a_WindowID, short a_SlotNum, eClickAction a_ClickAction,
|
||||
const cItem & a_ClickedItem
|
||||
)
|
||||
{
|
||||
@@ -106,16 +106,31 @@ void cWindow::Clicked(
|
||||
return;
|
||||
}
|
||||
|
||||
if (a_SlotNum < 0) // Outside window click
|
||||
switch (a_ClickAction)
|
||||
{
|
||||
if (a_IsRightClick)
|
||||
case caRightClickOutside:
|
||||
{
|
||||
// Toss one of the dragged items:
|
||||
a_Player.TossItem(true);
|
||||
return;
|
||||
}
|
||||
else
|
||||
case caLeftClickOutside:
|
||||
{
|
||||
// Toss all dragged items:
|
||||
a_Player.TossItem(true, a_Player.GetDraggingItem().m_ItemCount);
|
||||
return;
|
||||
}
|
||||
case caLeftClickOutsideHoldNothing:
|
||||
case caRightClickOutsideHoldNothing:
|
||||
{
|
||||
// Nothing needed
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (a_SlotNum < 0)
|
||||
{
|
||||
// TODO: Other click actions with irrelevant slot number (FS #371)
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -125,7 +140,7 @@ void cWindow::Clicked(
|
||||
{
|
||||
if (LocalSlotNum < (*itr)->GetNumSlots())
|
||||
{
|
||||
(*itr)->Clicked(a_Player, LocalSlotNum, a_IsRightClick, a_IsShiftPressed, a_ClickedItem);
|
||||
(*itr)->Clicked(a_Player, LocalSlotNum, a_ClickAction, a_ClickedItem);
|
||||
return;
|
||||
}
|
||||
LocalSlotNum -= (*itr)->GetNumSlots();
|
||||
|
||||
@@ -72,7 +72,7 @@ public:
|
||||
|
||||
void Clicked(
|
||||
cPlayer & a_Player, int a_WindowID,
|
||||
short a_SlotNum, bool a_IsRightClick, bool a_IsShiftPressed,
|
||||
short a_SlotNum, eClickAction a_ClickAction,
|
||||
const cItem & a_ClickedItem
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user