Refactor into switch statement

This commit is contained in:
Kingsley Collie 2022-01-11 22:03:10 +00:00 committed by Alexander Harkness
parent f126e671f9
commit e1154ad070

View File

@ -1891,127 +1891,128 @@ void cSlotAreaFurnace::Clicked(cPlayer & a_Player, int a_SlotNum, eClickAction a
return; return;
} }
// Result slot clicked switch (a_SlotNum)
if (a_SlotNum == 2)
{ {
bool bAsync = false; case 1: // Fuel slot clicked
if (GetSlot(a_SlotNum, a_Player) == nullptr)
{ {
LOGWARNING("GetSlot(%d) returned nullptr! Ignoring click", a_SlotNum); cItem & DraggingItem = a_Player.GetDraggingItem();
return; cFurnaceRecipe * FurnaceRecipes = cRoot::Get()->GetFurnaceRecipe();
}
cItem Slot(*GetSlot(a_SlotNum, a_Player)); // Do not allow non-fuels to be placed in the fuel slot:
if (!Slot.IsSameType(a_ClickedItem)) if (!DraggingItem.IsEmpty() && !FurnaceRecipes->IsFuel(DraggingItem) && (a_ClickAction != caShiftLeftClick) && (a_ClickAction != caShiftRightClick))
{ {
LOGWARNING("*** Window lost sync at item %d in SlotArea with %d items ***", a_SlotNum, m_NumSlots); LOGD("Rejecting non fuel item being placed in fuel slot.");
LOGWARNING("My item: %s", ItemToFullString(Slot).c_str()); return;
LOGWARNING("Their item: %s", ItemToFullString(a_ClickedItem).c_str()); }
bAsync = true; break;
} }
case 2: // Result slot clicked
switch (a_ClickAction)
{ {
case caShiftLeftClick: bool bAsync = false;
case caShiftRightClick: if (GetSlot(a_SlotNum, a_Player) == nullptr)
{
HandleSmeltItem(Slot, a_Player);
ShiftClicked(a_Player, a_SlotNum, Slot);
return;
}
case caMiddleClick:
{
MiddleClicked(a_Player, a_SlotNum);
return;
}
case caDropKey:
case caCtrlDropKey:
{
DropClicked(a_Player, a_SlotNum, (a_SlotNum == caCtrlDropKey));
Slot.m_ItemCount = Slot.m_ItemCount - GetSlot(a_SlotNum, a_Player)->m_ItemCount;
HandleSmeltItem(Slot, a_Player);
return;
}
default:
{
break;
}
}
cItem & DraggingItem = a_Player.GetDraggingItem();
if (!DraggingItem.IsEmpty())
{
if (a_ClickAction == caDblClick)
{
return;
}
if (!DraggingItem.IsEqual(Slot))
{
return;
}
if ((DraggingItem.m_ItemCount + Slot.m_ItemCount) > Slot.GetMaxStackSize())
{ {
LOGWARNING("GetSlot(%d) returned nullptr! Ignoring click", a_SlotNum);
return; return;
} }
DraggingItem.m_ItemCount += Slot.m_ItemCount; cItem Slot(*GetSlot(a_SlotNum, a_Player));
HandleSmeltItem(Slot, a_Player); if (!Slot.IsSameType(a_ClickedItem))
Slot.Empty(); {
} LOGWARNING("*** Window lost sync at item %d in SlotArea with %d items ***", a_SlotNum, m_NumSlots);
else LOGWARNING("My item: %s", ItemToFullString(Slot).c_str());
{ LOGWARNING("Their item: %s", ItemToFullString(a_ClickedItem).c_str());
bAsync = true;
}
switch (a_ClickAction) switch (a_ClickAction)
{ {
case caDblClick: case caShiftLeftClick:
case caShiftRightClick:
{ {
DblClicked(a_Player, a_SlotNum); HandleSmeltItem(Slot, a_Player);
ShiftClicked(a_Player, a_SlotNum, Slot);
return; return;
} }
case caLeftClick: case caMiddleClick:
{ {
DraggingItem = Slot; MiddleClicked(a_Player, a_SlotNum);
HandleSmeltItem(Slot, a_Player); return;
Slot.Empty();
break;
} }
case caRightClick: case caDropKey:
case caCtrlDropKey:
{ {
DraggingItem = Slot.CopyOne(); DropClicked(a_Player, a_SlotNum, (a_SlotNum == caCtrlDropKey));
DraggingItem.m_ItemCount = static_cast<char>(static_cast<float>(Slot.m_ItemCount) / 2.f + 0.5f); Slot.m_ItemCount = Slot.m_ItemCount - GetSlot(a_SlotNum, a_Player)->m_ItemCount;
Slot.m_ItemCount -= DraggingItem.m_ItemCount; HandleSmeltItem(Slot, a_Player);
return;
if (Slot.m_ItemCount <= 0)
{
Slot.Empty();
}
HandleSmeltItem(DraggingItem, a_Player);
break;
} }
default: default:
{ {
ASSERT(!"Unhandled click type!"); break;
} }
} }
}
SetSlot(a_SlotNum, a_Player, Slot); cItem & DraggingItem = a_Player.GetDraggingItem();
if (bAsync) if (!DraggingItem.IsEmpty())
{ {
m_ParentWindow.BroadcastWholeWindow(); if (a_ClickAction == caDblClick)
} {
return; return;
} }
if (!DraggingItem.IsEqual(Slot))
{
return;
}
if ((DraggingItem.m_ItemCount + Slot.m_ItemCount) > Slot.GetMaxStackSize())
{
return;
}
// Fuel slot clicked DraggingItem.m_ItemCount += Slot.m_ItemCount;
if (a_SlotNum == 1) HandleSmeltItem(Slot, a_Player);
{ Slot.Empty();
cItem & DraggingItem = a_Player.GetDraggingItem(); }
cFurnaceRecipe * FurnaceRecipes = cRoot::Get()->GetFurnaceRecipe(); else
{
switch (a_ClickAction)
{
case caDblClick:
{
DblClicked(a_Player, a_SlotNum);
return;
}
case caLeftClick:
{
DraggingItem = Slot;
HandleSmeltItem(Slot, a_Player);
Slot.Empty();
break;
}
case caRightClick:
{
DraggingItem = Slot.CopyOne();
DraggingItem.m_ItemCount = static_cast<char>(static_cast<float>(Slot.m_ItemCount) / 2.f + 0.5f);
Slot.m_ItemCount -= DraggingItem.m_ItemCount;
// Do not allow non-fuels to be placed in the fuel slot: if (Slot.m_ItemCount <= 0)
if (!DraggingItem.IsEmpty() && !FurnaceRecipes->IsFuel(DraggingItem) && (a_ClickAction != caShiftLeftClick) && (a_ClickAction != caShiftRightClick)) {
{ Slot.Empty();
LOGD("Rejecting non fuel item being placed in fuel slot."); }
HandleSmeltItem(DraggingItem, a_Player);
break;
}
default:
{
ASSERT(!"Unhandled click type!");
}
}
}
SetSlot(a_SlotNum, a_Player, Slot);
if (bAsync)
{
m_ParentWindow.BroadcastWholeWindow();
}
return; return;
} }
} }