1
0
mirror of https://github.com/moparisthebest/minetest synced 2025-03-10 06:39:44 -04:00

GUIFormSpecMenu::OnEvent code style update and small refactor

* Make method (more) consistent with current code stlye
* Move index into loop constructor after @rubenwardy's suggestion
* Cache inv_s->getList(s.listname), which removes a possibly bad scenario
of inv_s being null.
* Properly check for validity
This commit is contained in:
est31 2015-06-16 20:33:07 +02:00
parent 7b7f8b7225
commit 7a90b31b30

View File

@ -3116,6 +3116,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
Inventory *inv_selected = NULL; Inventory *inv_selected = NULL;
Inventory *inv_s = NULL; Inventory *inv_s = NULL;
InventoryList *list_s = NULL;
if (m_selected_item) { if (m_selected_item) {
inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc); inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
@ -3137,23 +3138,23 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
break; break;
} }
InventoryList *list = inv_s->getList(s.listname); list_s = inv_s->getList(s.listname);
if(list == NULL) { if (list_s == NULL) {
verbosestream << "InventoryMenu: The selected inventory list \"" verbosestream << "InventoryMenu: The selected inventory list \""
<< s.listname << "\" does not exist" << std::endl; << s.listname << "\" does not exist" << std::endl;
s.i = -1; // make it invalid again s.i = -1; // make it invalid again
break; break;
} }
if((u32)s.i >= list->getSize()) { if ((u32)s.i >= list_s->getSize()) {
infostream << "InventoryMenu: The selected inventory list \"" infostream << "InventoryMenu: The selected inventory list \""
<< s.listname << "\" is too small (i=" << s.i << ", size=" << s.listname << "\" is too small (i=" << s.i << ", size="
<<list->getSize()<<")"<<std::endl; << list_s->getSize() << ")" << std::endl;
s.i = -1; // make it invalid again s.i = -1; // make it invalid again
break; break;
} }
s_count = list->getItem(s.i).count; s_count = list_s->getItem(s.i).count;
} while(0); } while(0);
bool identical = (m_selected_item != NULL) && s.isValid() && bool identical = (m_selected_item != NULL) && s.isValid() &&
@ -3206,8 +3207,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
if (s.isValid() && s.listname == "craftpreview") { if (s.isValid() && s.listname == "craftpreview") {
// Craft preview has been clicked: craft // Craft preview has been clicked: craft
craft_amount = (button == 2 ? 10 : 1); craft_amount = (button == 2 ? 10 : 1);
} } else if (m_selected_item == NULL) {
else if(m_selected_item == NULL) {
if (s_count != 0) { if (s_count != 0) {
// Non-empty stack has been clicked: select or shift-move it // Non-empty stack has been clicked: select or shift-move it
m_selected_item = new ItemSpec(s); m_selected_item = new ItemSpec(s);
@ -3233,8 +3233,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
shift_move_amount = 1; shift_move_amount = 1;
} }
} }
} } else { // m_selected_item != NULL
else { // m_selected_item != NULL
assert(m_selected_amount >= 1); assert(m_selected_amount >= 1);
if (s.isValid()) { if (s.isValid()) {
@ -3276,8 +3275,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
// Dragged to different slot: move all selected // Dragged to different slot: move all selected
move_amount = m_selected_amount; move_amount = m_selected_amount;
} }
} } else if (m_selected_item != NULL && m_selected_dragging &&
else if(m_selected_item != NULL && m_selected_dragging &&
!(getAbsoluteClippingRect().isPointInside(m_pointer))) { !(getAbsoluteClippingRect().isPointInside(m_pointer))) {
// Dragged outside of window: drop all selected // Dragged outside of window: drop all selected
drop_amount = m_selected_amount; drop_amount = m_selected_amount;
@ -3289,8 +3287,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
// + click changes to drop one item when moved mode // + click changes to drop one item when moved mode
if (button == 1 && m_selected_item != NULL) if (button == 1 && m_selected_item != NULL)
m_rmouse_auto_place = !m_rmouse_auto_place; m_rmouse_auto_place = !m_rmouse_auto_place;
} } else if (updown == -1) {
else if(updown == -1) {
// Mouse has been moved and rmb is down and mouse pointer just // Mouse has been moved and rmb is down and mouse pointer just
// entered a new inventory field (checked in the entry-if, this // entered a new inventory field (checked in the entry-if, this
// is the only action here that is generated by mouse movement) // is the only action here that is generated by mouse movement)
@ -3302,7 +3299,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
// or contains the same item type as what is going to be // or contains the same item type as what is going to be
// moved // moved
InventoryList *list_from = inv_selected->getList(m_selected_item->listname); InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
InventoryList *list_to = inv_s->getList(s.listname); InventoryList *list_to = list_s;
assert(list_from && list_to); assert(list_from && list_to);
ItemStack stack_from = list_from->getItem(m_selected_item->i); ItemStack stack_from = list_from->getItem(m_selected_item->i);
ItemStack stack_to = list_to->getItem(s.i); ItemStack stack_to = list_to->getItem(s.i);
@ -3321,7 +3318,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
assert(inv_selected && inv_s); assert(inv_selected && inv_s);
InventoryList *list_from = inv_selected->getList(m_selected_item->listname); InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
InventoryList *list_to = inv_s->getList(s.listname); InventoryList *list_to = list_s;
assert(list_from && list_to); assert(list_from && list_to);
ItemStack stack_from = list_from->getItem(m_selected_item->i); ItemStack stack_from = list_from->getItem(m_selected_item->i);
ItemStack stack_to = list_to->getItem(s.i); ItemStack stack_to = list_to->getItem(s.i);
@ -3375,8 +3372,8 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
break; break;
u32 to_inv_ind = (i + 1) % mis; u32 to_inv_ind = (i + 1) % mis;
const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind]; const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
InventoryList *list_from = inv_s->getList(s.listname); InventoryList *list_from = list_s;
if (!list_from) if (!s.isValid())
break; break;
Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc); Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
if (!inv_to) if (!inv_to)
@ -3388,10 +3385,10 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
assert(shift_move_amount <= stack_from.count); assert(shift_move_amount <= stack_from.count);
// find a place (or more than one) to add the new item // find a place (or more than one) to add the new item
u32 slot_to = 0;
u32 ilt_size = list_to->getSize(); u32 ilt_size = list_to->getSize();
ItemStack leftover; ItemStack leftover;
for (; slot_to < ilt_size && shift_move_amount > 0; slot_to++) { for (u32 slot_to = 0; slot_to < ilt_size
&& shift_move_amount > 0; slot_to++) {
list_to->itemFits(slot_to, stack_from, &leftover); list_to->itemFits(slot_to, stack_from, &leftover);
if (leftover.count < stack_from.count) { if (leftover.count < stack_from.count) {
infostream << "Handing IACTION_MOVE to manager" << std::endl; infostream << "Handing IACTION_MOVE to manager" << std::endl;
@ -3434,8 +3431,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
a->from_list = m_selected_item->listname; a->from_list = m_selected_item->listname;
a->from_i = m_selected_item->i; a->from_i = m_selected_item->i;
m_invmgr->inventoryAction(a); m_invmgr->inventoryAction(a);
} } else if (craft_amount > 0) {
else if(craft_amount > 0) {
m_selected_content_guess = ItemStack(); // Clear m_selected_content_guess = ItemStack(); // Clear
// Send IACTION_CRAFT // Send IACTION_CRAFT
@ -3524,8 +3520,7 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
s.send = false; s.send = false;
return true; return true;
} }
} } else if ((s.ftype == f_DropDown) &&
else if ((s.ftype == f_DropDown) &&
(s.fid == event.GUIEvent.Caller->getID())) { (s.fid == event.GUIEvent.Caller->getID())) {
// only send the changed dropdown // only send the changed dropdown
for (u32 i = 0; i < m_fields.size(); i++) { for (u32 i = 0; i < m_fields.size(); i++) {
@ -3546,10 +3541,8 @@ bool GUIFormSpecMenu::OnEvent(const SEvent& event)
} }
} }
return true; return true;
} } else if ((s.ftype == f_ScrollBar) &&
else if ((s.ftype == f_ScrollBar) && (s.fid == event.GUIEvent.Caller->getID())) {
(s.fid == event.GUIEvent.Caller->getID()))
{
s.fdefault = L"Changed"; s.fdefault = L"Changed";
acceptInput(quit_mode_no); acceptInput(quit_mode_no);
s.fdefault = L""; s.fdefault = L"";