From 2eb15d389f85e300cb124b990ebbf2663acaa637 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Wed, 3 Jul 2013 15:48:02 +0300 Subject: add support for drag and drop from/to shortcuts window. --- src/dragdrop.h | 9 ++- src/gui/skilldialog.cpp | 6 ++ src/gui/skilldialog.h | 2 + src/gui/widgets/itemshortcutcontainer.cpp | 108 ++++++++++++++++++----------- src/gui/widgets/itemshortcutcontainer.h | 1 - src/gui/widgets/spellshortcutcontainer.cpp | 1 + src/itemshortcut.cpp | 27 +++++++- src/itemshortcut.h | 5 ++ 8 files changed, 112 insertions(+), 47 deletions(-) (limited to 'src') diff --git a/src/dragdrop.h b/src/dragdrop.h index 2405b151e..3c7167aaf 100644 --- a/src/dragdrop.h +++ b/src/dragdrop.h @@ -83,7 +83,7 @@ class DragDrop DragDropSource getSource() const { return mSource; } - void dragItem(Item *const item, + void dragItem(const Item *const item, const DragDropSource source, const int tag = 0) { @@ -111,7 +111,7 @@ class DragDrop } } - void dragCommand(TextCommand *const command, + void dragCommand(const TextCommand *const command, const DragDropSource source, const int tag = 0) { @@ -134,6 +134,7 @@ class DragDrop mTag = 0; return; } + mItem = command->getId(); } else { @@ -165,6 +166,7 @@ class DragDrop mItemImage = data->icon; if (mItemImage) mItemImage->incRef(); + mItem = info->id; mSource = source; mTag = tag; } @@ -224,6 +226,9 @@ class DragDrop int getTag() { return mTag; } + void setItem(const int item) + { mItem = item; } + private: int mItem; uint8_t mItemColor; diff --git a/src/gui/skilldialog.cpp b/src/gui/skilldialog.cpp index d6fc3656c..0653a5e0d 100644 --- a/src/gui/skilldialog.cpp +++ b/src/gui/skilldialog.cpp @@ -168,6 +168,7 @@ class SkillListBox final : public ListBox if (!skill) return; dragDrop.dragSkill(skill, DRAGDROP_SOURCE_SKILLS); + dragDrop.setItem(skill->id + SKILL_MIN_ID); } ListBox::mouseDragged(event); } @@ -609,6 +610,11 @@ SkillInfo* SkillDialog::getSkill(const int id) return mSkills[id]; } +SkillInfo* SkillDialog::getSkillByItem(const int itemId) +{ + return mSkills[itemId - SKILL_MIN_ID]; +} + void SkillDialog::widgetResized(const gcn::Event &event) { Window::widgetResized(event); diff --git a/src/gui/skilldialog.h b/src/gui/skilldialog.h index fd8728f2d..c6d491056 100644 --- a/src/gui/skilldialog.h +++ b/src/gui/skilldialog.h @@ -79,6 +79,8 @@ class SkillDialog final : public Window, public gcn::ActionListener SkillInfo* getSkill(const int id) A_WARN_UNUSED; + SkillInfo* getSkillByItem(const int itemId) A_WARN_UNUSED; + bool hasSkills() const A_WARN_UNUSED { return !mSkills.empty(); } diff --git a/src/gui/widgets/itemshortcutcontainer.cpp b/src/gui/widgets/itemshortcutcontainer.cpp index 416f2092c..ec0776f36 100644 --- a/src/gui/widgets/itemshortcutcontainer.cpp +++ b/src/gui/widgets/itemshortcutcontainer.cpp @@ -24,6 +24,7 @@ #include "client.h" #include "configuration.h" +#include "dragdrop.h" #include "inventory.h" #include "inputmanager.h" #include "item.h" @@ -50,7 +51,6 @@ ItemShortcutContainer::ItemShortcutContainer(const unsigned number) : ShortcutContainer(), mItemClicked(false), - mItemMoved(nullptr), mNumber(number), mItemPopup(new ItemPopup), mSpellPopup(new SpellPopup), @@ -222,23 +222,6 @@ void ItemShortcutContainer::draw(gcn::Graphics *graphics) } } } - - if (mItemMoved) - { - // Draw the item image being dragged by the cursor. - const Image *const image = mItemMoved->getImage(); - if (image) - { - const int tPosX = mCursorPosX - (image->mBounds.w / 2); - const int tPosY = mCursorPosY - (image->mBounds.h / 2); - const std::string str = toString(mItemMoved->getQuantity()); - - g->drawImage(image, tPosX, tPosY); - font->drawString(g, str, - tPosX + (mBoxWidth - font->getWidth(str)) / 2, - tPosY + mBoxHeight - 14); - } - } BLOCK_END("ItemShortcutContainer::draw") } @@ -250,8 +233,10 @@ void ItemShortcutContainer::mouseDragged(gcn::MouseEvent &event) if (event.getButton() == gcn::MouseEvent::LEFT) { - if (!mItemMoved && mItemClicked) + if (dragDrop.isEmpty() && mItemClicked) { + mItemClicked = false; + const int index = getIndexFromGrid(event.getX(), event.getY()); if (index == -1) return; @@ -262,37 +247,69 @@ void ItemShortcutContainer::mouseDragged(gcn::MouseEvent &event) if (itemId < 0) return; + event.consume(); if (itemId < SPELL_MIN_ID) - { + { // items if (!PlayerInfo::getInventory()) return; - Item *const item = PlayerInfo::getInventory()->findItem( + const Item *const item = PlayerInfo::getInventory()->findItem( itemId, itemColor); if (item) { - mItemMoved = item; selShortcut->removeItem(index); + dragDrop.dragItem(item, DRAGDROP_SOURCE_SHORTCUTS, index); + } + else + { + dragDrop.clear(); } } - else if (itemId < SKILL_MIN_ID && spellManager) - { + else if (itemId < SKILL_MIN_ID) + { // spells/commands + if (!spellManager) + { + dragDrop.clear(); + return; + } + const TextCommand *const spell = spellManager->getSpellByItem( itemId); if (spell) + { selShortcut->removeItem(index); + dragDrop.dragCommand(spell, + DRAGDROP_SOURCE_SHORTCUTS, index); + dragDrop.setItem(itemId); + } + else + { + dragDrop.clear(); + } } else - { - selShortcut->removeItem(index); + { // skills + if (!skillDialog) + { + dragDrop.clear(); + return; + } + const SkillInfo *const skill + = skillDialog->getSkillByItem(itemId); + if (skill) + { + selShortcut->removeItem(index); + dragDrop.dragSkill(skill, + DRAGDROP_SOURCE_SHORTCUTS, index); + dragDrop.setItem(itemId); + } + else + { + dragDrop.clear(); + } } } - if (mItemMoved) - { - mCursorPosX = event.getX(); - mCursorPosY = event.getY(); - } } } @@ -348,23 +365,30 @@ void ItemShortcutContainer::mouseReleased(gcn::MouseEvent &event) const int index = getIndexFromGrid(event.getX(), event.getY()); if (index == -1) - { - mItemMoved = nullptr; return; - } - if (mItemMoved) + + if (dragDrop.isEmpty()) { - selShortcut->setItems(index, mItemMoved->getId(), - mItemMoved->getColor()); - mItemMoved = nullptr; + if (selShortcut->getItem(index) && mItemClicked) + selShortcut->useItem(index); } - else if (selShortcut->getItem(index) && mItemClicked) + else { - selShortcut->useItem(index); + if (dragDrop.getSource() == DRAGDROP_SOURCE_SHORTCUTS) + { + const int oldIndex = dragDrop.getTag(); + selShortcut->setItem(oldIndex, dragDrop.getItem(), + dragDrop.getItemColor()); + selShortcut->swap(oldIndex, index); + } + else + { + selShortcut->setItem(index, dragDrop.getItem(), + dragDrop.getItemColor()); + } } - if (mItemClicked) - mItemClicked = false; + mItemClicked = false; } } diff --git a/src/gui/widgets/itemshortcutcontainer.h b/src/gui/widgets/itemshortcutcontainer.h index afd421b40..5f08cdd56 100644 --- a/src/gui/widgets/itemshortcutcontainer.h +++ b/src/gui/widgets/itemshortcutcontainer.h @@ -82,7 +82,6 @@ class ItemShortcutContainer final : public ShortcutContainer private: bool mItemClicked; - Item *mItemMoved; unsigned mNumber; ItemPopup *mItemPopup; diff --git a/src/gui/widgets/spellshortcutcontainer.cpp b/src/gui/widgets/spellshortcutcontainer.cpp index df9f421b8..dad7f5dcf 100644 --- a/src/gui/widgets/spellshortcutcontainer.cpp +++ b/src/gui/widgets/spellshortcutcontainer.cpp @@ -170,6 +170,7 @@ void SpellShortcutContainer::mouseDragged(gcn::MouseEvent &event) if (spell) { dragDrop.dragCommand(spell, DRAGDROP_SOURCE_SPELLS, index); + dragDrop.setItem(spell->getId() + SPELL_MIN_ID); } else { diff --git a/src/itemshortcut.cpp b/src/itemshortcut.cpp index 499b8e5b8..0ca90e9be 100644 --- a/src/itemshortcut.cpp +++ b/src/itemshortcut.cpp @@ -202,8 +202,6 @@ void ItemShortcut::setItemSelected(const Item *const item) { if (item) { -// logger->log("set selected id: %d", item->getId()); -// logger->log("set selected color: %d", item->getColor()); mItemSelected = item->getId(); mItemColorSelected = item->getColor(); } @@ -220,3 +218,28 @@ void ItemShortcut::setItem(const int index) mItemColors[index] = mItemColorSelected; save(); } + +void ItemShortcut::setItem(const int index, const int item, + const unsigned char color) +{ + mItems[index] = item; + mItemColors[index] = color; + save(); +} + +void ItemShortcut::swap(const int index1, const int index2) +{ + if (index1 < 0 || index2 < 0 || (unsigned int)index1 >= SHORTCUT_ITEMS + || (unsigned int)index2 >= SHORTCUT_ITEMS) + { + return; + } + + const int tmpItem = mItems[index1]; + mItems[index1] = mItems[index2]; + mItems[index2] = tmpItem; + const unsigned char tmpColor = mItemColors[index1]; + mItemColors[index1] = mItemColors[index2]; + mItemColors[index2] = tmpColor; + save(); +} diff --git a/src/itemshortcut.h b/src/itemshortcut.h index 78fc23558..1121007fa 100644 --- a/src/itemshortcut.h +++ b/src/itemshortcut.h @@ -88,6 +88,9 @@ class ItemShortcut final */ void setItem(const int index); + void setItem(const int index, const int item, + const unsigned char color); + /** * Adds an item to the items store specified by the index. * @@ -143,6 +146,8 @@ class ItemShortcut final */ void unequipItem(const int index) const; + void swap(const int index1, const int index2); + private: int mItems[SHORTCUT_ITEMS]; /**< The items. */ unsigned char mItemColors[SHORTCUT_ITEMS]; /**< The item colors. */ -- cgit v1.2.3-60-g2f50