From 1cafda1147c06a647e3d3f1e3f986d7296ccbd08 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Mon, 1 Jul 2013 23:18:52 +0300 Subject: add support for drag and drop in spells window. --- src/gui/widgets/dropshortcutcontainer.cpp | 3 +- src/gui/widgets/spellshortcutcontainer.cpp | 110 +++++++++++++++++++---------- src/gui/widgets/spellshortcutcontainer.h | 2 + 3 files changed, 76 insertions(+), 39 deletions(-) (limited to 'src/gui/widgets') diff --git a/src/gui/widgets/dropshortcutcontainer.cpp b/src/gui/widgets/dropshortcutcontainer.cpp index ac2916c1d..8078347f7 100644 --- a/src/gui/widgets/dropshortcutcontainer.cpp +++ b/src/gui/widgets/dropshortcutcontainer.cpp @@ -252,7 +252,8 @@ void DropShortcutContainer::mouseReleased(gcn::MouseEvent &event) dragDrop.clear(); return; } - if (!dragDrop.isEmpty()) + if (!dragDrop.isEmpty() && dragDrop.getSource() + == DRAGDROP_SOURCE_INVENTORY) { dropShortcut->setItems(index, dragDrop.getItem(), dragDrop.getItemColor()); diff --git a/src/gui/widgets/spellshortcutcontainer.cpp b/src/gui/widgets/spellshortcutcontainer.cpp index ef92ba09c..df9f421b8 100644 --- a/src/gui/widgets/spellshortcutcontainer.cpp +++ b/src/gui/widgets/spellshortcutcontainer.cpp @@ -22,6 +22,13 @@ #include "gui/widgets/spellshortcutcontainer.h" +#include "client.h" +#include "dragdrop.h" +#include "itemshortcut.h" +#include "keyboardconfig.h" +#include "localplayer.h" +#include "spellshortcut.h" + #include "gui/inventorywindow.h" #include "gui/okdialog.h" #include "gui/shortcutwindow.h" @@ -29,12 +36,6 @@ #include "gui/viewport.h" #include "gui/textcommandeditor.h" -#include "client.h" -#include "spellshortcut.h" -#include "itemshortcut.h" -#include "keyboardconfig.h" -#include "localplayer.h" - #include "resources/image.h" #include "resources/resourcemanager.h" @@ -117,8 +118,7 @@ void SpellShortcutContainer::draw(gcn::Graphics *graphics) const int itemX = (i % mGridWidth) * mBoxWidth; const int itemY = (i / mGridWidth) * mBoxHeight; - const int itemId = spellShortcut->getItem( - (mNumber * SPELL_SHORTCUT_ITEMS) + i); + const int itemId = getItemByIndex(i); if (selectedId >= 0 && itemId == selectedId) { g->drawRectangle(gcn::Rectangle(itemX + 1, itemY + 1, @@ -154,23 +154,28 @@ void SpellShortcutContainer::mouseDragged(gcn::MouseEvent &event) { if (event.getButton() == gcn::MouseEvent::LEFT) { - if (!mSpellMoved && mSpellClicked) + if (dragDrop.isEmpty() && mSpellClicked) { + mSpellClicked = false; const int index = getIndexFromGrid(event.getX(), event.getY()); if (index == -1) return; - const int itemId = spellShortcut->getItem( - (mNumber * SPELL_SHORTCUT_ITEMS) + index); - + const int itemId = getItemByIndex(index); if (itemId < 0) return; - } - if (mSpellMoved) - { - mCursorPosX = event.getX(); - mCursorPosY = event.getY(); + event.consume(); + TextCommand *const spell = spellManager->getSpell(itemId); + if (spell) + { + dragDrop.dragCommand(spell, DRAGDROP_SOURCE_SPELLS, index); + } + else + { + dragDrop.clear(); + mSpellClicked = false; + } } } } @@ -185,6 +190,9 @@ void SpellShortcutContainer::mousePressed(gcn::MouseEvent &event) const unsigned int eventButton = event.getButton(); if (eventButton == gcn::MouseEvent::LEFT) { + const int itemId = getItemByIndex(index); + if (itemId > 0) + mSpellClicked = true; } else if (eventButton == gcn::MouseEvent::RIGHT) { @@ -194,8 +202,7 @@ void SpellShortcutContainer::mousePressed(gcn::MouseEvent &event) if (!spellShortcut || !spellManager) return; - const int itemId = spellShortcut->getItem( - (mNumber * SPELL_SHORTCUT_ITEMS) + index); + const int itemId = getItemByIndex(index); spellManager->invoke(itemId); } } @@ -208,43 +215,66 @@ void SpellShortcutContainer::mouseReleased(gcn::MouseEvent &event) const int index = getIndexFromGrid(event.getX(), event.getY()); if (index == -1) + { + dragDrop.clear(); return; + } - const int itemId = spellShortcut->getItem( - (mNumber * SPELL_SHORTCUT_ITEMS) + index); + const int itemId = getItemByIndex(index); const unsigned int eventButton = event.getButton(); if (eventButton == gcn::MouseEvent::LEFT) { + mSpellClicked = false; + if (itemId < 0) return; const int selectedId = spellShortcut->getSelectedItem(); + event.consume(); - if (selectedId != itemId) + if (!dragDrop.isEmpty()) { - const TextCommand *const spell = spellManager->getSpell(itemId); - if (spell && !spell->isEmpty()) + if (dragDrop.getSource() == DRAGDROP_SOURCE_SPELLS) { - const int num = itemShortcutWindow->getTabIndex(); - if (num >= 0 && num < static_cast(SHORTCUT_TABS) - && itemShortcut[num]) + const int oldIndex = dragDrop.getTag(); + const int oldItemId = getItemByIndex(oldIndex); + const int idx = mNumber * SPELL_SHORTCUT_ITEMS; + if (spellManager) { - itemShortcut[num]->setItemSelected( - spell->getId() + SPELL_MIN_ID); + spellManager->swap(idx + index, idx + oldIndex); + spellManager->save(); } - spellShortcut->setItemSelected(spell->getId()); } } else { - const int num = itemShortcutWindow->getTabIndex(); - if (num >= 0 && num < static_cast(SHORTCUT_TABS) - && itemShortcut[num]) + if (selectedId != itemId) { - itemShortcut[num]->setItemSelected(-1); + const TextCommand *const + spell = spellManager->getSpell(itemId); + if (spell && !spell->isEmpty()) + { + const int num = itemShortcutWindow->getTabIndex(); + if (num >= 0 && num < static_cast(SHORTCUT_TABS) + && itemShortcut[num]) + { + itemShortcut[num]->setItemSelected( + spell->getId() + SPELL_MIN_ID); + } + spellShortcut->setItemSelected(spell->getId()); + } + } + else + { + const int num = itemShortcutWindow->getTabIndex(); + if (num >= 0 && num < static_cast(SHORTCUT_TABS) + && itemShortcut[num]) + { + itemShortcut[num]->setItemSelected(-1); + } + spellShortcut->setItemSelected(-1); } - spellShortcut->setItemSelected(-1); } } else if (eventButton == gcn::MouseEvent::RIGHT) @@ -269,9 +299,7 @@ void SpellShortcutContainer::mouseMoved(gcn::MouseEvent &event) if (index == -1) return; - const int itemId = spellShortcut->getItem( - (mNumber * SPELL_SHORTCUT_ITEMS) + index); - + const int itemId = getItemByIndex(index); mSpellPopup->setVisible(false); const TextCommand *const spell = spellManager->getSpell(itemId); if (spell && !spell->isEmpty()) @@ -296,3 +324,9 @@ void SpellShortcutContainer::widgetHidden(const gcn::Event &event A_UNUSED) if (mSpellPopup) mSpellPopup->setVisible(false); } + +int SpellShortcutContainer::getItemByIndex(const int index) +{ + return spellShortcut->getItem( + (mNumber * SPELL_SHORTCUT_ITEMS) + index); +} diff --git a/src/gui/widgets/spellshortcutcontainer.h b/src/gui/widgets/spellshortcutcontainer.h index c880a2d0c..509b96c61 100644 --- a/src/gui/widgets/spellshortcutcontainer.h +++ b/src/gui/widgets/spellshortcutcontainer.h @@ -79,6 +79,8 @@ class SpellShortcutContainer final : public ShortcutContainer void setWidget2(const Widget2 *const widget) override; + int getItemByIndex(const int index); + private: TextCommand *mSpellMoved; SpellPopup *mSpellPopup; -- cgit v1.2.3-60-g2f50