From 85accb7f6f5fdfa9f5861ef364be51be37b34513 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Tue, 3 Sep 2013 13:41:09 +0300 Subject: add drag and drop support for equipment window. --- src/gui/equipmentwindow.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 4 deletions(-) (limited to 'src/gui/equipmentwindow.cpp') diff --git a/src/gui/equipmentwindow.cpp b/src/gui/equipmentwindow.cpp index 8e51dae72..1dbc0a0ff 100644 --- a/src/gui/equipmentwindow.cpp +++ b/src/gui/equipmentwindow.cpp @@ -22,12 +22,14 @@ #include "gui/equipmentwindow.h" +#include "dragdrop.h" #include "graphicsvertexes.h" #include "inventory.h" #include "item.h" #include "being/being.h" #include "being/localplayer.h" +#include "being/playerinfo.h" #include "gui/itempopup.h" #include "gui/setup.h" @@ -278,10 +280,11 @@ Item *EquipmentWindow::getItem(const int x, const int y) const void EquipmentWindow::mousePressed(gcn::MouseEvent& mouseEvent) { - Window::mousePressed(mouseEvent); - if (!mEquipment) + { + Window::mousePressed(mouseEvent); return; + } const int x = mouseEvent.getX(); const int y = mouseEvent.getY(); @@ -293,6 +296,8 @@ void EquipmentWindow::mousePressed(gcn::MouseEvent& mouseEvent) // Checks if any of the presses were in the equip boxes. int i = 0; + bool inBox(false); + for (std::vector::const_iterator it = mBoxes.begin(), it_end = mBoxes.end(); it != it_end; ++ it, ++ i) { @@ -302,8 +307,18 @@ void EquipmentWindow::mousePressed(gcn::MouseEvent& mouseEvent) const Item *const item = mEquipment->getEquipment(i); const gcn::Rectangle tRect(box->x, box->y, mBoxSize, mBoxSize); - if (tRect.isPointInRect(x, y) && item) - setSelected(i); + if (tRect.isPointInRect(x, y)) + { + inBox = true; + if (item) + { + setSelected(i); + dragDrop.dragItem(item, DRAGDROP_SOURCE_EQUIPMENT); + return; + } + } + if (inBox) + return; } } else if (mouseEvent.getButton() == gcn::MouseEvent::RIGHT) @@ -327,6 +342,62 @@ void EquipmentWindow::mousePressed(gcn::MouseEvent& mouseEvent) } } } + Window::mousePressed(mouseEvent); +} + +void EquipmentWindow::mouseReleased(gcn::MouseEvent &mouseEvent) +{ + Window::mouseReleased(mouseEvent); + const DragDropSource src = dragDrop.getSource(); + if (dragDrop.isEmpty() || (src != DRAGDROP_SOURCE_INVENTORY + && src != DRAGDROP_SOURCE_EQUIPMENT)) + { + return; + } + Inventory *const inventory = player_node + ? PlayerInfo::getInventory() : nullptr; + if (!inventory) + return; + + Item *const item = inventory->findItem(dragDrop.getItem(), + dragDrop.getItemColor()); + if (!item) + return; + + if (dragDrop.getSource() == DRAGDROP_SOURCE_INVENTORY) + { + if (item->isEquipment()) + { + if (!item->isEquipped()) + Net::getInventoryHandler()->equipItem(item); + } + } + else if (dragDrop.getSource() == DRAGDROP_SOURCE_EQUIPMENT) + { + if (item->isEquipment()) + { + const int x = mouseEvent.getX(); + const int y = mouseEvent.getY(); + int i = 0; + for (std::vector::const_iterator + it = mBoxes.begin(), it_end = mBoxes.end(); + it != it_end; ++ it, ++ i) + { + const EquipmentBox *const box = *it; + if (!box) + continue; + const gcn::Rectangle tRect(box->x, box->y, mBoxSize, mBoxSize); + + if (tRect.isPointInRect(x, y)) + return; + } + + if (item->isEquipped()) + Net::getInventoryHandler()->unequipItem(item); + } + } + dragDrop.clear(); + dragDrop.deselect(); } // Show ItemTooltip -- cgit v1.2.3-60-g2f50