diff options
author | Chuck Miller <shadowmil@gmail.com> | 2009-05-12 05:24:26 -0400 |
---|---|---|
committer | Chuck Miller <shadowmil@gmail.com> | 2009-05-12 06:07:59 -0400 |
commit | 5d1c423648bc138fe4a67908a0f16247a80e5967 (patch) | |
tree | da883e1f0c57613e441d579cb786a58462711c31 /src | |
parent | 260e22a429311e597e91e2d9a149c8fdbf5df5b3 (diff) | |
download | mana-5d1c423648bc138fe4a67908a0f16247a80e5967.tar.gz mana-5d1c423648bc138fe4a67908a0f16247a80e5967.tar.bz2 mana-5d1c423648bc138fe4a67908a0f16247a80e5967.tar.xz mana-5d1c423648bc138fe4a67908a0f16247a80e5967.zip |
Makes ItemContainer use indexes for selected and highlighted items, rather then Item pointers.
As suggested by Bjorn
Diffstat (limited to 'src')
-rw-r--r-- | src/gui/itemcontainer.cpp | 129 | ||||
-rw-r--r-- | src/gui/itemcontainer.h | 6 | ||||
-rw-r--r-- | src/localplayer.cpp | 5 | ||||
-rw-r--r-- | src/localplayer.h | 5 |
4 files changed, 61 insertions, 84 deletions
diff --git a/src/gui/itemcontainer.cpp b/src/gui/itemcontainer.cpp index d2712cba..6cbdabb2 100644 --- a/src/gui/itemcontainer.cpp +++ b/src/gui/itemcontainer.cpp @@ -27,11 +27,13 @@ #include "gui/sdlinput.h" #include "gui/viewport.h" +#include "net/net.h" +#include "net/inventoryhandler.h" + #include "graphics.h" #include "inventory.h" #include "item.h" #include "itemshortcut.h" -#include "localplayer.h" #include "log.h" #include "resources/image.h" @@ -53,8 +55,8 @@ ItemContainer::ItemContainer(Inventory *inventory, bool forceQuantity): mInventory(inventory), mGridColumns(1), mGridRows(1), - mSelectedItem(NULL), - mHighlightedItem(NULL), + mSelectedIndex(-1), + mHighlightedIndex(-1), mSelectionStatus(SEL_NONE), mForceQuantity(forceQuantity), mSwapItems(false), @@ -92,8 +94,8 @@ void ItemContainer::draw(gcn::Graphics *graphics) { int itemX = i * BOX_WIDTH; int itemY = j * BOX_HEIGHT; - - Item *item = mInventory->getItem((j * mGridColumns) + i); + int itemIndex = (j * mGridColumns) + i; + Item *item = mInventory->getItem(itemIndex); if (!item || item->getId() == 0) continue; @@ -101,7 +103,7 @@ void ItemContainer::draw(gcn::Graphics *graphics) Image *image = item->getImage(); if (image) { - if (item == mSelectedItem) + if (itemIndex == mSelectedIndex) { if (mSelectionStatus == SEL_DRAGGING) { // Reposition the coords to that of the cursor. @@ -133,11 +135,10 @@ void ItemContainer::draw(gcn::Graphics *graphics) } // Draw an orange box around the selected item - if (isFocused() && mHighlightedItem) + if (isFocused() && mHighlightedIndex != -1) { - const int i = mHighlightedItem->getInvIndex(); - const int itemX = (i % mGridColumns) * BOX_WIDTH; - const int itemY = (i / mGridColumns) * BOX_HEIGHT; + const int itemX = (mHighlightedIndex % mGridColumns) * BOX_WIDTH; + const int itemY = (mHighlightedIndex / mGridColumns) * BOX_HEIGHT; g->setColor(gcn::Color(255, 128, 0)); g->drawRectangle(gcn::Rectangle(itemX, itemY, BOX_WIDTH, BOX_HEIGHT)); } @@ -145,18 +146,24 @@ void ItemContainer::draw(gcn::Graphics *graphics) void ItemContainer::selectNone() { - setSelectedItem(NULL); + setSelectedIndex(-1); + mSelectionStatus = SEL_NONE; } -void ItemContainer::setSelectedItem(Item *item) +void ItemContainer::setSelectedIndex(int newIndex) { - if (mSelectedItem != item) + if (mSelectedIndex != newIndex) { - mSelectedItem = item; + mSelectedIndex = newIndex; distributeValueChangedEvent(); } } +Item *ItemContainer::getSelectedItem() const +{ + return mInventory->getItem(mSelectedIndex); +} + void ItemContainer::distributeValueChangedEvent() { SelectionListenerIterator i, i_end; @@ -229,21 +236,20 @@ void ItemContainer::mousePressed(gcn::MouseEvent &event) chatWindow->addItemText(item->getInfo().getName()); } - if (mSelectedItem && mSelectedItem == item) + if (mSelectedIndex == index) { mSelectionStatus = SEL_DESELECTING; } else if (item && item->getId()) { - setSelectedItem(item); + setSelectedIndex(index); mSelectionStatus = SEL_SELECTING; itemShortcut->setItemSelected(item->getId()); } else { - setSelectedItem(NULL); - mSelectionStatus = SEL_NONE; + selectNone(); } } } @@ -266,8 +272,7 @@ void ItemContainer::mouseReleased(gcn::MouseEvent &event) mSelectionStatus = SEL_SELECTED; return; case SEL_DESELECTING: - setSelectedItem(NULL); - mSelectionStatus = SEL_NONE; + selectNone(); return; case SEL_DRAGGING: mSelectionStatus = SEL_SELECTED; @@ -279,12 +284,10 @@ void ItemContainer::mouseReleased(gcn::MouseEvent &event) int index = getSlotIndex(event.getX(), event.getY()); if (index == Inventory::NO_SLOT_INDEX) return; - Item *item = mInventory->getItem(index); - if (item == mSelectedItem) + if (index == mSelectedIndex || mSelectedIndex == -1) return; - player_node->moveInvItem(mSelectedItem, index); - setSelectedItem(NULL); - mSelectionStatus = SEL_NONE; + Net::getInventoryHandler()->moveItem(mSelectedIndex, index); + selectNone(); } @@ -320,20 +323,6 @@ void ItemContainer::widgetResized(const gcn::Event &event) setHeight(mGridRows * BOX_HEIGHT); } -Item *ItemContainer::getSelectedItem() -{ - if (!mSelectedItem) - return NULL; - - if (!mInventory->contains(mSelectedItem)) - { - mSelectedItem = NULL; - return NULL; - } - - return mSelectedItem; -} - int ItemContainer::getSlotIndex(int x, int y) const { if (x < getWidth() && y < getHeight()) @@ -346,49 +335,47 @@ int ItemContainer::getSlotIndex(int x, int y) const void ItemContainer::keyAction() { // If there is no highlight then return. - if (!mHighlightedItem) + if (mHighlightedIndex == -1) return; // If the highlight is on the selected item, then deselect it. - if (mHighlightedItem == mSelectedItem) + if (mHighlightedIndex == mSelectedIndex) { - setSelectedItem(NULL); - mSelectionStatus = SEL_NONE; + selectNone(); } // Check and swap items if necessary. else if (mSwapItems && - mSelectedItem && - mHighlightedItem->getId()) + mSelectedIndex != -1 && + mHighlightedIndex != -1) { - player_node->moveInvItem( - mSelectedItem, mHighlightedItem->getInvIndex()); - setSelectedItem(mHighlightedItem); + Net::getInventoryHandler()->moveItem( + mSelectedIndex, mHighlightedIndex); + setSelectedIndex(mHighlightedIndex); } // If the highlight is on an item then select it. - else if (mHighlightedItem->getId()) + else if (mHighlightedIndex != -1) { - setSelectedItem(mHighlightedItem); + setSelectedIndex(mHighlightedIndex); mSelectionStatus = SEL_SELECTED; } // If the highlight is on a blank space then move it. - else if (mSelectedItem) + else if (mSelectedIndex != -1) { - player_node->moveInvItem( - mSelectedItem, mHighlightedItem->getInvIndex()); - setSelectedItem(NULL); - mSelectionStatus = SEL_NONE; + Net::getInventoryHandler()->moveItem( + mSelectedIndex, mHighlightedIndex); + selectNone(); } } void ItemContainer::moveHighlight(Direction direction) { - if (!mHighlightedItem) + if (mHighlightedIndex == -1) { - if (mSelectedItem) { - mHighlightedItem = mSelectedItem; + if (mSelectedIndex != -1) { + mHighlightedIndex = mSelectedIndex; } else { - mHighlightedItem = mInventory->getItem(0); + mHighlightedIndex = 0; } return; } @@ -396,34 +383,34 @@ void ItemContainer::moveHighlight(Direction direction) switch (direction) { case Left: - if (mHighlightedItem->getInvIndex() % mGridColumns == 0) + if (mHighlightedIndex % mGridColumns == 0) { - mHighlightedItem += mGridColumns; + mHighlightedIndex += mGridColumns; } - mHighlightedItem--; + mHighlightedIndex--; break; case Right: - if ((mHighlightedItem->getInvIndex() % mGridColumns) == + if ((mHighlightedIndex % mGridColumns) == (mGridColumns - 1)) { - mHighlightedItem -= mGridColumns; + mHighlightedIndex -= mGridColumns; } - mHighlightedItem++; + mHighlightedIndex++; break; case Up: - if (mHighlightedItem->getInvIndex() / mGridColumns == 0) + if (mHighlightedIndex / mGridColumns == 0) { - mHighlightedItem += (mGridColumns * mGridRows); + mHighlightedIndex += (mGridColumns * mGridRows); } - mHighlightedItem -= mGridColumns; + mHighlightedIndex -= mGridColumns; break; case Down: - if ((mHighlightedItem->getInvIndex() / mGridColumns) == + if ((mHighlightedIndex / mGridColumns) == (mGridRows - 1)) { - mHighlightedItem -= (mGridColumns * mGridRows); + mHighlightedIndex -= (mGridColumns * mGridRows); } - mHighlightedItem += mGridColumns; + mHighlightedIndex += mGridColumns; break; } } diff --git a/src/gui/itemcontainer.h b/src/gui/itemcontainer.h index 54d7c35e..f446a647 100644 --- a/src/gui/itemcontainer.h +++ b/src/gui/itemcontainer.h @@ -86,7 +86,7 @@ class ItemContainer : public gcn::Widget, /** * Returns the selected item. */ - Item *getSelectedItem(); + Item *getSelectedItem() const; /** * Sets selected item to NULL. @@ -144,7 +144,7 @@ class ItemContainer : public gcn::Widget, /** * Sets the currently selected item. */ - void setSelectedItem(Item *item); + void setSelectedIndex(int index); /** * Find the current item index by the most recently used item ID @@ -173,7 +173,7 @@ class ItemContainer : public gcn::Widget, Inventory *mInventory; int mGridColumns, mGridRows; Image *mSelImg; - Item *mSelectedItem, *mHighlightedItem; + int mSelectedIndex, mHighlightedIndex; SelectionState mSelectionStatus; bool mForceQuantity; bool mSwapItems; diff --git a/src/localplayer.cpp b/src/localplayer.cpp index 8a60a4b3..b571c4b0 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -346,11 +346,6 @@ void LocalPlayer::inviteToParty(Player *player) Net::getPartyHandler()->invite(player); } -void LocalPlayer::moveInvItem(Item *item, int newIndex) -{ - Net::getInventoryHandler()->moveItem(item->getInvIndex(), newIndex); -} - void LocalPlayer::equipItem(Item *item) { Net::getInventoryHandler()->equipItem(item); diff --git a/src/localplayer.h b/src/localplayer.h index 930020db..acf2ee63 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -198,11 +198,6 @@ class LocalPlayer : public Player void inviteToParty(Player *player); /** - * Move the Inventory item from the old slot to the new slot. - */ - void moveInvItem(Item *item, int newIndex); - - /** * Equips an item. */ void equipItem(Item *item); |