aboutsummaryrefslogtreecommitdiff
path: root/src/guiInventoryMenu.cpp
diff options
context:
space:
mode:
authorKahrl <kahrl@gmx.net>2012-01-21 21:21:41 +0100
committerPerttu Ahola <celeron55@gmail.com>2012-01-22 17:31:12 +0200
commit1efdc36b22532807d21a0beac94524e3eacfe7bc (patch)
tree9828ccdb11ec7b4e21e9e4558bbb202865aafa14 /src/guiInventoryMenu.cpp
parent157a4cf18cb9c098f465b8baecd7d2cd5705f2dd (diff)
downloadminetest-1efdc36b22532807d21a0beac94524e3eacfe7bc.tar.gz
minetest-1efdc36b22532807d21a0beac94524e3eacfe7bc.tar.bz2
minetest-1efdc36b22532807d21a0beac94524e3eacfe7bc.zip
Inventory menu (with dragging) improved. Crafting is now handled via a IACTION_CRAFT inventory action.
Diffstat (limited to 'src/guiInventoryMenu.cpp')
-rw-r--r--src/guiInventoryMenu.cpp425
1 files changed, 311 insertions, 114 deletions
diff --git a/src/guiInventoryMenu.cpp b/src/guiInventoryMenu.cpp
index 6e46bd49c..a4b16b251 100644
--- a/src/guiInventoryMenu.cpp
+++ b/src/guiInventoryMenu.cpp
@@ -133,6 +133,8 @@ GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
m_gamedef(gamedef)
{
m_selected_item = NULL;
+ m_selected_amount = 0;
+ m_selected_dragging = false;
m_tooltip_element = NULL;
}
@@ -317,28 +319,17 @@ void GUIInventoryMenu::drawList(const ListDrawSpec &s, int phase)
}
}
- if(phase == 1 && !item.empty())
+ if(phase == 1)
{
- // Draw item at the normal position if
- // - the item is not being dragged or
- // /*- the item is in the crafting result slot*/
- if(!selected /*|| s.listname == "craftresult"*/)
+ // Draw item stack
+ if(selected)
{
- drawItemStack(driver, font, item,
- rect, &AbsoluteClippingRect, m_gamedef);
+ item.takeItem(m_selected_amount);
}
- }
-
- if(phase ==2 && !item.empty())
- {
- // Draw dragged item
- if(selected)
+ if(!item.empty())
{
- v2s32 offset = m_pointer - rect.getCenter();
- rect.UpperLeftCorner += offset;
- rect.LowerRightCorner += offset;
drawItemStack(driver, font, item,
- rect, NULL, m_gamedef);
+ rect, &AbsoluteClippingRect, m_gamedef);
}
// Draw tooltip
@@ -359,12 +350,38 @@ void GUIInventoryMenu::drawList(const ListDrawSpec &s, int phase)
core::dimension2d<s32>(tooltip_width, tooltip_height)));
}
}
-
}
}
+void GUIInventoryMenu::drawSelectedItem()
+{
+ if(!m_selected_item)
+ return;
+
+ video::IVideoDriver* driver = Environment->getVideoDriver();
+
+ // Get font
+ gui::IGUIFont *font = NULL;
+ gui::IGUISkin* skin = Environment->getSkin();
+ if (skin)
+ font = skin->getFont();
+
+ Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
+ assert(inv);
+ InventoryList *list = inv->getList(m_selected_item->listname);
+ assert(list);
+ ItemStack stack = list->getItem(m_selected_item->i);
+ stack.count = m_selected_amount;
+
+ core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
+ core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
+ drawItemStack(driver, font, stack, rect, NULL, m_gamedef);
+}
+
void GUIInventoryMenu::drawMenu()
{
+ updateSelectedItem();
+
gui::IGUISkin* skin = Environment->getSkin();
if (!skin)
return;
@@ -378,22 +395,93 @@ void GUIInventoryMenu::drawMenu()
/*
Draw items
Phase 0: Item slot rectangles
- Phase 1: Item images
- Phase 2: Dragged item image; tooltip
+ Phase 1: Item images; prepare tooltip
*/
- for(int phase=0; phase<=2; phase++)
+ for(int phase=0; phase<=1; phase++)
for(u32 i=0; i<m_draw_spec.size(); i++)
{
drawList(m_draw_spec[i], phase);
}
/*
+ Draw dragged item stack
+ */
+ drawSelectedItem();
+
+ /*
Call base class
*/
gui::IGUIElement::draw();
}
+void GUIInventoryMenu::updateSelectedItem()
+{
+ // If the selected stack has become empty for some reason, deselect it.
+ // If the selected stack has become smaller, adjust m_selected_amount.
+ if(m_selected_item)
+ {
+ bool selection_valid = false;
+ if(m_selected_item->isValid())
+ {
+ Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
+ if(inv)
+ {
+ InventoryList *list = inv->getList(m_selected_item->listname);
+ if(list && (u32) m_selected_item->i < list->getSize())
+ {
+ ItemStack stack = list->getItem(m_selected_item->i);
+ if(m_selected_amount > stack.count)
+ m_selected_amount = stack.count;
+ if(!stack.empty())
+ selection_valid = true;
+ }
+ }
+ }
+ if(!selection_valid)
+ {
+ delete m_selected_item;
+ m_selected_item = NULL;
+ m_selected_amount = 0;
+ m_selected_dragging = false;
+ }
+ }
+
+ // If craftresult is nonempty and nothing else is selected, select it now.
+ if(!m_selected_item)
+ {
+ for(u32 i=0; i<m_draw_spec.size(); i++)
+ {
+ const ListDrawSpec &s = m_draw_spec[i];
+ if(s.listname == "craftpreview")
+ {
+ Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
+ InventoryList *list = inv->getList("craftresult");
+ if(list && list->getSize() >= 1 && !list->getItem(0).empty())
+ {
+ m_selected_item = new ItemSpec;
+ m_selected_item->inventoryloc = s.inventoryloc;
+ m_selected_item->listname = "craftresult";
+ m_selected_item->i = 0;
+ m_selected_amount = 0;
+ m_selected_dragging = false;
+ break;
+ }
+ }
+ }
+ }
+
+ // If craftresult is selected, keep the whole stack selected
+ if(m_selected_item && m_selected_item->listname == "craftresult")
+ {
+ Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
+ assert(inv);
+ InventoryList *list = inv->getList(m_selected_item->listname);
+ assert(list);
+ m_selected_amount = list->getItem(m_selected_item->i).count;
+ }
+}
+
bool GUIInventoryMenu::OnEvent(const SEvent& event)
{
if(event.EventType==EET_KEY_INPUT_EVENT)
@@ -418,133 +506,242 @@ bool GUIInventoryMenu::OnEvent(const SEvent& event)
// Mouse event other than movement
v2s32 p(event.MouseInput.X, event.MouseInput.Y);
+ m_pointer = p;
+
+ // Get selected item and hovered/clicked item (s)
+
+ updateSelectedItem();
ItemSpec s = getItemAtPos(p);
Inventory *inv_selected = NULL;
Inventory *inv_s = NULL;
+
if(m_selected_item)
{
- assert(m_selected_item->isValid());
inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
assert(inv_selected);
+ assert(inv_selected->getList(m_selected_item->listname) != NULL);
}
+
+ u32 s_count = 0;
+
if(s.isValid())
{
inv_s = m_invmgr->getInventory(s.inventoryloc);
assert(inv_s);
+
+ InventoryList *list = inv_s->getList(s.listname);
+ if(list != NULL && (u32) s.i < list->getSize())
+ s_count = list->getItem(s.i).count;
+ else
+ s.i = -1; // make it invalid again
}
- bool different_item = m_selected_item
- && ((inv_selected != inv_s)
- || (m_selected_item->listname != s.listname)
- || (m_selected_item->i != s.i));
- int amount = -1;
+ // buttons: 0 = left, 1 = right, 2 = middle
+ // up/down: 0 = down (press), 1 = up (release), 2 = unknown event
+ int button = 0;
+ int updown = 2;
if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
- amount = 0;
+ { button = 0; updown = 0; }
else if(event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
- amount = 1;
+ { button = 1; updown = 0; }
else if(event.MouseInput.Event == EMIE_MMOUSE_PRESSED_DOWN)
- amount = 10;
- else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP && different_item)
- amount = 0;
- //else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP && different_item)
- // amount = 1;
- //else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP && different_item)
- // amount = 10;
-
- if(amount >= 0)
+ { button = 2; updown = 0; }
+ else if(event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
+ { button = 0; updown = 1; }
+ else if(event.MouseInput.Event == EMIE_RMOUSE_LEFT_UP)
+ { button = 1; updown = 1; }
+ else if(event.MouseInput.Event == EMIE_MMOUSE_LEFT_UP)
+ { button = 2; updown = 1; }
+
+ // Set this number to a positive value to generate a move action
+ // from m_selected_item to s.
+ u32 move_amount = 0;
+
+ // Set this number to a positive value to generate a drop action
+ // from m_selected_item.
+ u32 drop_amount = 0;
+
+ // Set this number to a positive value to generate a craft action at s.
+ u32 craft_amount = 0;
+
+ if(updown == 0)
{
- // Indicates whether source slot should be deselected
- bool remove_selection = false;
+ // Some mouse button has been pressed
- //infostream<<"Mouse action at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
- if(s.isValid())
+ //infostream<<"Mouse button "<<button<<" pressed at p=("
+ // <<p.X<<","<<p.Y<<")"<<std::endl;
+
+ m_selected_dragging = false;
+
+ if(s.isValid() && s.listname == "craftpreview")
{
- infostream<<"Mouse action on "<<s.inventoryloc.dump()
- <<"/"<<s.listname<<" "<<s.i<<std::endl;
- if(m_selected_item)
- {
- Inventory *inv_from = inv_selected;
- Inventory *inv_to = inv_s;
- assert(inv_from);
- assert(inv_to);
- InventoryList *list_from =
- inv_from->getList(m_selected_item->listname);
- InventoryList *list_to =
- inv_to->getList(s.listname);
- if(list_from == NULL)
- infostream<<"from list doesn't exist"<<std::endl;
- if(list_to == NULL)
- infostream<<"to list doesn't exist"<<std::endl;
- if(list_from && list_to
- && !list_from->getItem(m_selected_item->i).empty())
- {
- infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
- IMoveAction *a = new IMoveAction();
- a->count = amount;
- a->from_inv = m_selected_item->inventoryloc;
- a->from_list = m_selected_item->listname;
- a->from_i = m_selected_item->i;
- a->to_inv = s.inventoryloc;
- a->to_list = s.listname;
- a->to_i = s.i;
- m_invmgr->inventoryAction(a);
-
- if(amount == 0 || list_from->getItem(m_selected_item->i).count<=amount)
- remove_selection = true;
- }
- }
- else
+ // Craft preview has been clicked: craft
+ craft_amount = (button == 2 ? 10 : 1);
+ }
+ else if(m_selected_item == NULL)
+ {
+ if(s_count != 0)
{
- /*
- Select if nonempty
- */
- assert(inv_s);
- InventoryList *list = inv_s->getList(s.listname);
- if(list && !list->getItem(s.i).empty())
- {
- m_selected_item = new ItemSpec(s);
- }
+ // Non-empty stack has been clicked: select it
+ m_selected_item = new ItemSpec(s);
+
+ if(button == 1) // right
+ m_selected_amount = (s_count + 1) / 2;
+ else if(button == 2) // middle
+ m_selected_amount = MYMIN(s_count, 10);
+ else // left
+ m_selected_amount = s_count;
+
+ m_selected_dragging = true;
}
}
- else if(m_selected_item)
+ else // m_selected_item != NULL
{
- // If moved outside the menu, drop.
- // (Otherwise abort inventory action.)
- if(getAbsoluteClippingRect().isPointInside(m_pointer))
+ assert(m_selected_amount >= 1);
+
+ if(s.isValid())
{
- // Inside menu
- remove_selection = true;
+ // Clicked another slot: move
+ if(button == 1) // right
+ move_amount = 1;
+ else if(button == 2) // middle
+ move_amount = MYMIN(m_selected_amount, 10);
+ else // left
+ move_amount = m_selected_amount;
+ }
+ else if(getAbsoluteClippingRect().isPointInside(m_pointer))
+ {
+ // Clicked somewhere else: deselect
+ m_selected_amount = 0;
}
else
{
- // Outside of menu
- Inventory *inv_from = inv_selected;
- assert(inv_from);
- InventoryList *list_from =
- inv_from->getList(m_selected_item->listname);
- if(list_from == NULL)
- infostream<<"from list doesn't exist"<<std::endl;
- if(list_from && !list_from->getItem(m_selected_item->i).empty())
- {
- infostream<<"Handing IACTION_DROP to manager"<<std::endl;
- IDropAction *a = new IDropAction();
- a->count = amount;
- a->from_inv = m_selected_item->inventoryloc;
- a->from_list = m_selected_item->listname;
- a->from_i = m_selected_item->i;
- m_invmgr->inventoryAction(a);
- if(amount == 0 || list_from->getItem(m_selected_item->i).count<=amount)
- remove_selection = true;
- }
+ // Clicked outside of the window: drop
+ if(button == 1) // right
+ drop_amount = 1;
+ else if(button == 2) // middle
+ drop_amount = MYMIN(m_selected_amount, 10);
+ else // left
+ drop_amount = m_selected_amount;
+ }
+ }
+ }
+ else if(updown == 1)
+ {
+ // Some mouse button has been released
+
+ //infostream<<"Mouse button "<<button<<" released at p=("
+ // <<p.X<<","<<p.Y<<")"<<std::endl;
+
+ if(m_selected_item != NULL && m_selected_dragging && s.isValid())
+ {
+ if((inv_selected != inv_s) ||
+ (m_selected_item->listname != s.listname) ||
+ (m_selected_item->i != s.i))
+ {
+ // Dragged to different slot: move all selected
+ move_amount = m_selected_amount;
}
}
+ else if(m_selected_item != NULL && m_selected_dragging &&
+ !(getAbsoluteClippingRect().isPointInside(m_pointer)))
+ {
+ // Dragged outside of window: drop all selected
+ drop_amount = m_selected_amount;
+ }
- if(remove_selection)
+ m_selected_dragging = false;
+ }
+
+ // Possibly send inventory action to server
+ if(move_amount > 0)
+ {
+ // Send IACTION_MOVE
+
+ assert(m_selected_item && m_selected_item->isValid());
+ assert(s.isValid());
+
+ assert(inv_selected && inv_s);
+ InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
+ InventoryList *list_to = inv_s->getList(s.listname);
+ assert(list_from && list_to);
+ ItemStack stack_from = list_from->getItem(m_selected_item->i);
+ ItemStack stack_to = list_to->getItem(s.i);
+
+ // Check how many items can be moved
+ move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
+ ItemStack leftover = stack_to.addItem(stack_from, m_gamedef->idef());
+ if(leftover.count == stack_from.count)
+ {
+ // Swap the stacks
+ }
+ else if(leftover.empty())
+ {
+ // Item fits
+ }
+ else
{
- delete m_selected_item;
- m_selected_item = NULL;
+ // Item only fits partially
+ move_amount -= leftover.count;
}
+ assert(move_amount > 0 && move_amount <= m_selected_amount);
+ m_selected_amount -= move_amount;
+
+ infostream<<"Handing IACTION_MOVE to manager"<<std::endl;
+ IMoveAction *a = new IMoveAction();
+ a->count = move_amount;
+ a->from_inv = m_selected_item->inventoryloc;
+ a->from_list = m_selected_item->listname;
+ a->from_i = m_selected_item->i;
+ a->to_inv = s.inventoryloc;
+ a->to_list = s.listname;
+ a->to_i = s.i;
+ m_invmgr->inventoryAction(a);
+ }
+ else if(drop_amount > 0)
+ {
+ // Send IACTION_DROP
+
+ assert(m_selected_item && m_selected_item->isValid());
+ assert(inv_selected);
+ InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
+ assert(list_from);
+ ItemStack stack_from = list_from->getItem(m_selected_item->i);
+
+ // Check how many items can be dropped
+ drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
+ assert(drop_amount > 0 && drop_amount <= m_selected_amount);
+ m_selected_amount -= drop_amount;
+
+ infostream<<"Handing IACTION_DROP to manager"<<std::endl;
+ IDropAction *a = new IDropAction();
+ a->count = drop_amount;
+ a->from_inv = m_selected_item->inventoryloc;
+ a->from_list = m_selected_item->listname;
+ a->from_i = m_selected_item->i;
+ m_invmgr->inventoryAction(a);
+ }
+ else if(craft_amount > 0)
+ {
+ // Send IACTION_CRAFT
+
+ assert(s.isValid());
+ assert(inv_s);
+
+ infostream<<"Handing IACTION_CRAFT to manager"<<std::endl;
+ ICraftAction *a = new ICraftAction();
+ a->count = craft_amount;
+ a->craft_inv = s.inventoryloc;
+ m_invmgr->inventoryAction(a);
+ }
+
+ // If m_selected_amount has been decreased to zero, deselect
+ if(m_selected_amount == 0)
+ {
+ delete m_selected_item;
+ m_selected_item = NULL;
+ m_selected_dragging = false;
}
}
if(event.EventType==EET_GUI_EVENT)