diff options
author | Andrei Karas <akaras@inbox.ru> | 2013-05-11 18:39:15 +0300 |
---|---|---|
committer | Andrei Karas <akaras@inbox.ru> | 2013-06-01 12:52:16 +0300 |
commit | f45b10598c4a128510d7c469a6fe669587c01a3e (patch) | |
tree | 6a35598bcf52ee3e380890dbac3c2d013b83a2d2 | |
parent | 9f85b059aff7c54043d85c3422f02be3570d5050 (diff) | |
download | manaplus-f45b10598c4a128510d7c469a6fe669587c01a3e.tar.gz manaplus-f45b10598c4a128510d7c469a6fe669587c01a3e.tar.bz2 manaplus-f45b10598c4a128510d7c469a6fe669587c01a3e.tar.xz manaplus-f45b10598c4a128510d7c469a6fe669587c01a3e.zip |
add dragdrop between inventory and outfits window.
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/Makefile.am | 1 | ||||
-rw-r--r-- | src/dragdrop.h | 79 | ||||
-rw-r--r-- | src/gui/gui.cpp | 37 | ||||
-rw-r--r-- | src/gui/outfitwindow.cpp | 60 | ||||
-rw-r--r-- | src/gui/outfitwindow.h | 10 | ||||
-rw-r--r-- | src/gui/widgets/itemcontainer.cpp | 25 | ||||
-rw-r--r-- | src/item.cpp | 4 |
8 files changed, 142 insertions, 75 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 1349ea994..4f5fa4ee3 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -576,6 +576,7 @@ SET(SRCS depricatedevent.h depricatedlistener.cpp depricatedlistener.h + dragdrop.h effectmanager.cpp effectmanager.h emoteshortcut.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 211164a0c..b7584a950 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -579,6 +579,7 @@ manaplus_SOURCES += gui/widgets/avatarlistbox.cpp \ depricatedevent.h \ depricatedlistener.cpp \ depricatedlistener.h \ + dragdrop.h \ effectmanager.cpp \ effectmanager.h \ emoteshortcut.cpp \ diff --git a/src/dragdrop.h b/src/dragdrop.h new file mode 100644 index 000000000..e65c76a9b --- /dev/null +++ b/src/dragdrop.h @@ -0,0 +1,79 @@ +/* + * The ManaPlus Client + * Copyright (C) 2013 The ManaPlus Developers + * + * This file is part of The ManaPlus Client. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#ifndef DRAGDROP_H +#define DRAGDROP_H + +#include "item.h" + +#include "localconsts.h" + +enum DragDropSource +{ + DRAGDROP_SOURCE_EMPTY = 0, + DRAGDROP_SOURCE_INVENTORY, + DRAGDROP_SOURCE_STORAGE, + DRAGDROP_SOURCE_CART, + DRAGDROP_SOURCE_TRADE, + DRAGDROP_SOURCE_OUTFIT, + DRAGDROP_SOURCE_SPELLS, + DRAGDROP_SOURCE_GROUND, + DRAGDROP_SOURCE_SHORTCUTS, + DRAGDROP_SOURCE_CRAFT +}; + +class DragDrop +{ + public: + DragDrop(Item *const item, const DragDropSource source) : + mItem(item), + mSource(source) + { + } + + Item *getItem() + { return mItem; } + + DragDropSource getSource() const + { return mSource; } + + void dragItem(Item *const item, const DragDropSource source) + { + mItem = item; + mSource = source; + } + + void clear() + { + mItem = nullptr; + mSource = DRAGDROP_SOURCE_EMPTY; + } + + bool isEmpty() const + { return mSource == DRAGDROP_SOURCE_EMPTY; } + + private: + Item *mItem; + DragDropSource mSource; +}; + +extern DragDrop dragDrop; + +#endif // DRAGDROP_H diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 9418d3e4c..a12163d60 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -33,6 +33,7 @@ #include "gui/widgets/window.h" #include "configuration.h" +#include "dragdrop.h" #include "keydata.h" #include "keyevent.h" #include "keyinput.h" @@ -433,14 +434,24 @@ void Gui::draw() if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS || button & SDL_BUTTON(1)) && mMouseCursors && mCustomCursor && mMouseCursorAlpha > 0.0f) { + Graphics *g2 = static_cast<Graphics*>(mGraphics); + Item *const item = dragDrop.getItem(); + if (item) + { + const Image *const image = item->getImage(); + if (image) + { + const int tPosX = mouseX - (image->mBounds.w / 2); + const int tPosY = mouseY - (image->mBounds.h / 2); + g2->drawImage(image, tPosX, tPosY); + } + } + Image *const mouseCursor = mMouseCursors->get(mCursorType); if (mouseCursor) { mouseCursor->setAlpha(mMouseCursorAlpha); - static_cast<Graphics*>(mGraphics)->drawImage( - mouseCursor, - mouseX - 15, - mouseY - 17); + g2->drawImage(mouseCursor, mouseX - 15, mouseY - 17); } } @@ -598,6 +609,7 @@ void Gui::distributeMouseEvent(gcn::Widget* source, int type, int button, (*it)->mousePressed(mouseEvent); break; case gcn::MouseEvent::RELEASED: + case 100: // manual hack for release on target after drag (*it)->mouseReleased(mouseEvent); break; case gcn::MouseEvent::WHEEL_MOVED_UP: @@ -625,6 +637,9 @@ void Gui::distributeMouseEvent(gcn::Widget* source, int type, int button, widget = parent; parent = swap->getParent(); + if (type == gcn::MouseEvent::RELEASED) + dragDrop.clear(); + // If a non modal focused widget has been reach // and we have modal focus cancel the distribution. if (mFocusHandler->getModalFocused() @@ -734,17 +749,27 @@ void Gui::handleMouseReleased(const gcn::MouseInput &mouseInput) gcn::Widget *sourceWidget = getMouseEventSource( mouseInput.getX(), mouseInput.getY()); + int sourceWidgetX, sourceWidgetY; if (mFocusHandler->getDraggedWidget()) { if (sourceWidget != mFocusHandler->getLastWidgetPressed()) mFocusHandler->setLastWidgetPressed(nullptr); + gcn::Widget *oldWidget = sourceWidget; sourceWidget = mFocusHandler->getDraggedWidget(); + if (oldWidget != sourceWidget) + { + oldWidget->getAbsolutePosition(sourceWidgetX, sourceWidgetY); + distributeMouseEvent(oldWidget, + 100, + mouseInput.getButton(), + mouseInput.getX(), + mouseInput.getY()); + + } } - int sourceWidgetX, sourceWidgetY; sourceWidget->getAbsolutePosition(sourceWidgetX, sourceWidgetY); - distributeMouseEvent(sourceWidget, MouseEvent::RELEASED, mouseInput.getButton(), diff --git a/src/gui/outfitwindow.cpp b/src/gui/outfitwindow.cpp index 2b9f57825..04320049c 100644 --- a/src/gui/outfitwindow.cpp +++ b/src/gui/outfitwindow.cpp @@ -23,6 +23,7 @@ #include "gui/outfitwindow.h" #include "configuration.h" +#include "dragdrop.h" #include "emoteshortcut.h" #include "equipment.h" #include "game.h" @@ -78,13 +79,9 @@ OutfitWindow::OutfitWindow(): keyName(mCurrentOutfit).c_str()))), mBoxWidth(33), mBoxHeight(33), - mCursorPosX(0), - mCursorPosY(0), mGridWidth(4), mGridHeight(4), - mItemMoved(nullptr), mItems(), - mItemSelected(-1), mAwayOutfit(0), mBorderColor(getThemeColor(Theme::BORDER, 64)), mBackgroundColor(getThemeColor(Theme::BACKGROUND, 32)), @@ -374,6 +371,7 @@ void OutfitWindow::draw(gcn::Graphics *graphics) } } } +/* if (mItemMoved) { // Draw the item image being dragged by the cursor. @@ -386,6 +384,7 @@ void OutfitWindow::draw(gcn::Graphics *graphics) g->drawImage(image, tPosX, tPosY); } } +*/ BLOCK_END("OutfitWindow::draw") } @@ -394,7 +393,7 @@ void OutfitWindow::mouseDragged(gcn::MouseEvent &event) { if (event.getButton() == gcn::MouseEvent::LEFT) { - if (!mItemMoved && mItemClicked) + if (dragDrop.isEmpty() && mItemClicked) { if (mCurrentOutfit < 0 || mCurrentOutfit >= static_cast<signed int>(OUTFITS_COUNT)) @@ -423,17 +422,12 @@ void OutfitWindow::mouseDragged(gcn::MouseEvent &event) { Item *const item = inv->findItem(itemId, itemColor); if (item) - mItemMoved = item; + dragDrop.dragItem(item, DragDropSource::DRAGDROP_SOURCE_OUTFIT); else - mItemMoved = nullptr; + dragDrop.clear(); mItems[mCurrentOutfit][index] = -1; } } - if (mItemMoved) - { - mCursorPosX = event.getX(); - mCursorPosY = event.getY(); - } } Window::mouseDragged(event); } @@ -457,19 +451,9 @@ void OutfitWindow::mousePressed(gcn::MouseEvent &event) mMoved = false; event.consume(); - // Stores the selected item if there is one. - if (isItemSelected()) - { - mItems[mCurrentOutfit][index] = mItemSelected; - mItemColors[mCurrentOutfit][index] = mItemColorSelected; - - if (inventoryWindow) - inventoryWindow->unselectItem(); - } - else if (mItems[mCurrentOutfit][index]) - { + if (mItems[mCurrentOutfit][index]) mItemClicked = true; - } + Window::mousePressed(event); } @@ -485,17 +469,21 @@ void OutfitWindow::mouseReleased(gcn::MouseEvent &event) const int index = getIndexFromGrid(event.getX(), event.getY()); if (index == -1) { - mItemMoved = nullptr; + dragDrop.clear(); Window::mouseReleased(event); return; } mMoved = false; event.consume(); - if (mItemMoved) + if (!dragDrop.isEmpty()) { - mItems[mCurrentOutfit][index] = mItemMoved->getId(); - mItemColors[mCurrentOutfit][index] = mItemMoved->getColor(); - mItemMoved = nullptr; + Item *const item = dragDrop.getItem(); + if (item) + { + mItems[mCurrentOutfit][index] = item->getId(); + mItemColors[mCurrentOutfit][index] = item->getColor(); + dragDrop.clear(); + } } if (mItemClicked) mItemClicked = false; @@ -665,20 +653,6 @@ void OutfitWindow::unwearAwayOutfit() wearOutfit(OUTFITS_COUNT); } -void OutfitWindow::setItemSelected(const Item *const item) -{ - if (item) - { - mItemSelected = item->getId(); - mItemColorSelected = item->getColor(); - } - else - { - mItemSelected = -1; - mItemColorSelected = 1; - } -} - void OutfitWindow::clearCurrentOutfit() { if (mCurrentOutfit < 0 || mCurrentOutfit diff --git a/src/gui/outfitwindow.h b/src/gui/outfitwindow.h index 7a5638f01..efb59346a 100644 --- a/src/gui/outfitwindow.h +++ b/src/gui/outfitwindow.h @@ -64,14 +64,6 @@ class OutfitWindow final : public Window, void load(const bool oldConfig = false); - void setItemSelected(const int itemId) - { mItemSelected = itemId; } - - void setItemSelected(const Item *const item); - - bool isItemSelected() const A_WARN_UNUSED - { return mItemSelected > 0; } - void wearOutfit(const int outfit, const bool unwearEmpty = true, const bool select = false); @@ -123,10 +115,8 @@ class OutfitWindow final : public Window, int mCursorPosY; int mGridWidth; int mGridHeight; - Item *mItemMoved; int mItems[OUTFITS_COUNT + 1][OUTFIT_ITEM_COUNT]; - int mItemSelected; int mAwayOutfit; gcn::Color mBorderColor; diff --git a/src/gui/widgets/itemcontainer.cpp b/src/gui/widgets/itemcontainer.cpp index f65624eae..2246280fa 100644 --- a/src/gui/widgets/itemcontainer.cpp +++ b/src/gui/widgets/itemcontainer.cpp @@ -22,6 +22,7 @@ #include "gui/widgets/itemcontainer.h" +#include "dragdrop.h" #include "inventory.h" #include "item.h" #include "itemshortcut.h" @@ -267,18 +268,8 @@ void ItemContainer::draw(gcn::Graphics *graphics) { if (mShowMatrix[itemIndex] == mSelectedIndex) { - if (mSelectionStatus == SEL_DRAGGING) - { - // Reposition the coords to that of the cursor. - itemX = mDragPosX - (mBoxWidth / 2); - itemY = mDragPosY - (mBoxHeight / 2); - } - else - { - // Draw selection border image. - if (mSelImg) - g->drawImage(mSelImg, itemX, itemY); - } + if (mSelImg) + g->drawImage(mSelImg, itemX, itemY); } image->setAlpha(1.0f); // ensure the image if fully drawn... g->drawImage(image, itemX + mPaddingItemX, @@ -332,14 +323,16 @@ void ItemContainer::draw(gcn::Graphics *graphics) void ItemContainer::selectNone() { + dragDrop.clear(); + setSelectedIndex(-1); mSelectionStatus = SEL_NONE; +/* if (outfitWindow) outfitWindow->setItemSelected(-1); if (shopWindow) shopWindow->setItemSelected(-1); -// if (skillDialog) -// skillDialog->setItemSelected(-1); +*/ } void ItemContainer::setSelectedIndex(const int newIndex) @@ -407,10 +400,12 @@ void ItemContainer::mousePressed(gcn::MouseEvent &event) if (mSelectedIndex == index && mClicks != 2) { + dragDrop.dragItem(item, DragDropSource::DRAGDROP_SOURCE_INVENTORY); mSelectionStatus = SEL_DESELECTING; } else if (item && item->getId()) { + dragDrop.dragItem(item, DragDropSource::DRAGDROP_SOURCE_INVENTORY); setSelectedIndex(index); mSelectionStatus = SEL_SELECTING; @@ -422,8 +417,6 @@ void ItemContainer::mousePressed(gcn::MouseEvent &event) } if (dropShortcut) dropShortcut->setItemSelected(item); - if (item->isEquipment() && outfitWindow) - outfitWindow->setItemSelected(item); if (shopWindow) shopWindow->setItemSelected(item->getId()); } diff --git a/src/item.cpp b/src/item.cpp index d6c804314..90ca9ea35 100644 --- a/src/item.cpp +++ b/src/item.cpp @@ -22,6 +22,8 @@ #include "item.h" +#include "dragdrop.h" + #include "gui/theme.h" #include "resources/image.h" @@ -33,6 +35,8 @@ extern int serverVersion; +DragDrop dragDrop(nullptr, DRAGDROP_SOURCE_EMPTY); + Item::Item(const int id, const int quantity, const int refine, const unsigned char color, const bool equipment, const bool equipped): |