summaryrefslogtreecommitdiff
path: root/src/gui/itemcontainer.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/gui/itemcontainer.cpp')
-rw-r--r--src/gui/itemcontainer.cpp433
1 files changed, 285 insertions, 148 deletions
diff --git a/src/gui/itemcontainer.cpp b/src/gui/itemcontainer.cpp
index 8a780eb4..38a41e0e 100644
--- a/src/gui/itemcontainer.cpp
+++ b/src/gui/itemcontainer.cpp
@@ -19,12 +19,15 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+
#include <guichan/mouseinput.hpp>
#include <guichan/selectionlistener.hpp>
+#include "chat.h"
#include "itemcontainer.h"
#include "itempopup.h"
#include "palette.h"
+#include "sdlinput.h"
#include "viewport.h"
#include "../graphics.h"
@@ -39,29 +42,48 @@
#include "../utils/stringutils.h"
-const int ItemContainer::gridWidth = 36; // item icon width + 4
-const int ItemContainer::gridHeight = 42; // item icon height + 10
+// TODO: Add support for adding items to the item shortcut window (global
+// itemShortcut).
-static const int NO_ITEM = -1;
+static const int BOX_WIDTH = 36;
+static const int BOX_HEIGHT = 44;
-ItemContainer::ItemContainer(Inventory *inventory, int offset):
+enum
+{
+ SEL_NONE = 0,
+ SEL_SELECTED,
+ SEL_SELECTING,
+ SEL_DESELECTING,
+ SEL_DRAGGING
+};
+
+ItemContainer::ItemContainer(Inventory *inventory,
+ int gridColumns,
+ int gridRows,
+ int offset):
mInventory(inventory),
- mSelectedItemIndex(NO_ITEM),
- mLastSelectedItemId(NO_ITEM),
- mOffset(offset)
+ mGridColumns(gridColumns),
+ mGridRows(gridRows),
+ mOffset(offset),
+ mSelectedItem(NULL),
+ mHighlightedItem(NULL),
+ mSelectionStatus(SEL_NONE),
+ mSwapItems(false),
+ mDescItems(false)
{
- mItemPopup = new ItemPopup;
- mItemPopup->setOpaque(false);
+ mItemPopup = new ItemPopup();
+ setFocusable(true);
ResourceManager *resman = ResourceManager::getInstance();
mSelImg = resman->getImage("graphics/gui/selection.png");
if (!mSelImg) logger->error("Unable to load selection.png");
- mMaxItems = mInventory->getLastUsedSlot() - (mOffset - 1); // Count from 0, usage from 2
-
+ addKeyListener(this);
addMouseListener(this);
- addWidgetListener(this);
+
+ setSize((BOX_WIDTH - 1) * mGridColumns + 1,
+ (BOX_HEIGHT - 1) * mGridRows + 1);
}
ItemContainer::~ItemContainer()
@@ -70,185 +92,214 @@ ItemContainer::~ItemContainer()
delete mItemPopup;
}
-void ItemContainer::logic()
-{
- if (!isVisible())
- return;
-
- gcn::Widget::logic();
-
- int i = mInventory->getLastUsedSlot() - (mOffset - 1); // Count from 0, usage from 2
-
- if (i != mMaxItems)
- {
- mMaxItems = i;
- recalculateHeight();
- }
-}
-
void ItemContainer::draw(gcn::Graphics *graphics)
{
- if (!isVisible())
- return;
-
- int columns = getWidth() / gridWidth;
+ Graphics *g = static_cast<Graphics*>(graphics);
- // Have at least 1 column
- if (columns < 1)
- columns = 1;
-
- /*
- * mOffset is used to compensate for some weirdness that eAthena inherited
- * from Ragnarok Online. Inventory slots and cart slots are +2 from their
- * actual index, while storage slots are +1.
- */
- for (int i = mOffset; i < mInventory->getSize(); i++)
+ for (int i = 0; i < mGridColumns; i++)
{
- Item *item = mInventory->getItem(i);
+ for (int j = 0; j < mGridRows; j++)
+ {
+ // Items positions made to overlap on another.
+ int itemX = i * (BOX_WIDTH - 1);
+ int itemY = j * (BOX_HEIGHT - 1);
- if (!item || item->getQuantity() <= 0)
- continue;
+ // Set color to black.
+ g->setColor(gcn::Color(0, 0, 0));
+ // Draw box border.
+ g->drawRectangle(
+ gcn::Rectangle(itemX, itemY, BOX_WIDTH, BOX_HEIGHT));
- int itemX = ((i - mOffset) % columns) * gridWidth;
- int itemY = ((i - mOffset) / columns) * gridHeight;
+ Item *item = mInventory->getItem((j * mGridColumns) + i);
- // Draw selection image below selected item
- if (mSelectedItemIndex == i)
- static_cast<Graphics*>(graphics)->drawImage(mSelImg, itemX, itemY);
+ if (!item || item->getId() == 0)
+ continue;
- // Draw item icon
- Image* image = item->getImage();
+ Image *image = item->getImage();
+ if (image)
+ {
+ if (item == mSelectedItem)
+ {
+ if (mSelectionStatus == SEL_DRAGGING) {
+ // Reposition the coords to that of the cursor.
+ itemX = mDragPosX - (BOX_WIDTH / 2);
+ itemY = mDragPosY - (BOX_HEIGHT / 2);
+ }
+ else {
+ // Draw selected image.
+ g->drawImage(mSelImg, itemX, itemY);
+ }
+ }
+ g->drawImage(image, itemX, itemY);
+ }
+ if (item->getQuantity() > 1) {
+ // Draw item caption
+ g->drawText(
+ toString(item->getQuantity()),
+ itemX + BOX_WIDTH / 2,
+ itemY + BOX_HEIGHT - 14,
+ gcn::Graphics::CENTER);
+ }
- if (image)
- static_cast<Graphics*>(graphics)->drawImage(image, itemX, itemY);
+ }
+ }
- // Draw item caption
- graphics->setFont(getFont());
- graphics->setColor(guiPalette->getColor(Palette::TEXT));
- graphics->drawText(
- (item->isEquipped() ? "Eq." : toString(item->getQuantity())),
- itemX + gridWidth / 2, itemY + gridHeight - 11,
- gcn::Graphics::CENTER);
+ if (isFocused() && mHighlightedItem) {
+ // Items positions made to overlap on another.
+ const int i = mHighlightedItem->getInvIndex();
+ const int itemX = (i % mGridColumns) * (BOX_WIDTH - 1);
+ const int itemY = (i / mGridColumns) * (BOX_HEIGHT - 1);
+ // Set color to orange.
+ g->setColor(gcn::Color(255, 128, 0));
+ // Draw box border.
+ g->drawRectangle(gcn::Rectangle(itemX, itemY, BOX_WIDTH, BOX_HEIGHT));
}
}
-void ItemContainer::widgetResized(const gcn::Event &event)
+void ItemContainer::selectNone()
{
- recalculateHeight();
+ setSelectedItem(NULL);
}
-void ItemContainer::recalculateHeight()
+void ItemContainer::setSelectedItem(Item *item)
{
- int cols = getWidth() / gridWidth;
-
- if (cols < 1)
- cols = 1;
-
- const int rows = (mMaxItems / cols) + (mMaxItems % cols > 0 ? 1 : 0);
- const int height = rows * gridHeight + 8;
-
- if (height != getHeight())
- setHeight(height);
-}
-
-Item *ItemContainer::getSelectedItem()
-{
- refindSelectedItem(); // Make sure that we're still current
-
- if (mSelectedItemIndex == NO_ITEM)
- return NULL;
-
- return mInventory->getItem(mSelectedItemIndex);
+ if (mSelectedItem != item)
+ {
+ mSelectedItem = item;
+ distributeValueChangedEvent();
+ }
}
-void ItemContainer::selectNone()
+void ItemContainer::distributeValueChangedEvent()
{
- setSelectedItemIndex(NO_ITEM);
-}
+ SelectionListenerIterator iter;
-void ItemContainer::refindSelectedItem()
-{
- if (mSelectedItemIndex != NO_ITEM)
+ for (iter = mSelectionListeners.begin(); iter != mSelectionListeners.end();
+ ++iter)
{
- if (mInventory->getItem(mSelectedItemIndex) &&
- mInventory->getItem(mSelectedItemIndex)->getId() == mLastSelectedItemId)
- return; // we're already fine
-
- // Otherwise ensure the invariant: we must point to an item of the specified last ID,
- // or nowhere at all.
-
- for (int i = 0; i <= mMaxItems + 1; i++)
- if (mInventory->getItem(i) &&
- mInventory->getItem(i)->getId() == mLastSelectedItemId)
- {
- mSelectedItemIndex = i;
- return;
- }
+ gcn::SelectionEvent event(this);
+ (*iter)->valueChanged(event);
}
-
- mLastSelectedItemId = mSelectedItemIndex = NO_ITEM;
}
-void ItemContainer::setSelectedItemIndex(int index)
+void ItemContainer::keyPressed(gcn::KeyEvent &event)
{
- int newSelectedItemIndex;
-
- /*
- * mOffset is used to compensate for some weirdness that eAthena inherited from
- * Ragnarok Online. Inventory slots and cart slots are +2 from their actual index,
- * while storage slots are +1.
- */
- if (index < 0 || index > mMaxItems + mOffset || mInventory->getItem(index) == NULL)
- newSelectedItemIndex = NO_ITEM;
- else
- newSelectedItemIndex = index;
-
- if (mSelectedItemIndex != newSelectedItemIndex)
+ switch (event.getKey().getValue())
{
- mSelectedItemIndex = newSelectedItemIndex;
-
- if (mSelectedItemIndex == NO_ITEM)
- mLastSelectedItemId = NO_ITEM;
- else
- mLastSelectedItemId = mInventory->getItem(index)->getId();
-
- distributeValueChangedEvent();
+ case Key::LEFT:
+ moveHighlight(MOVE_SELECTED_LEFT);
+ break;
+ case Key::RIGHT:
+ moveHighlight(MOVE_SELECTED_RIGHT);
+ break;
+ case Key::UP:
+ moveHighlight(MOVE_SELECTED_UP);
+ break;
+ case Key::DOWN:
+ moveHighlight(MOVE_SELECTED_DOWN);
+ break;
+ case Key::SPACE:
+ keyAction();
+ break;
+ case Key::LEFT_ALT:
+ case Key::RIGHT_ALT:
+ mSwapItems = true;
+ break;
+ case Key::RIGHT_CONTROL:
+ mDescItems = true;
+ break;
}
}
-void ItemContainer::distributeValueChangedEvent()
+void ItemContainer::keyReleased(gcn::KeyEvent &event)
{
- gcn::SelectionEvent event(this);
- std::list<gcn::SelectionListener*>::iterator i_end = mListeners.end();
- std::list<gcn::SelectionListener*>::iterator i;
-
- for (i = mListeners.begin(); i != i_end; ++i)
+ switch (event.getKey().getValue())
{
- (*i)->valueChanged(event);
+ case Key::LEFT_ALT:
+ case Key::RIGHT_ALT:
+ mSwapItems = false;
+ break;
+ case Key::RIGHT_CONTROL:
+ mDescItems = false;
+ break;
}
}
void ItemContainer::mousePressed(gcn::MouseEvent &event)
{
const int button = event.getButton();
-
if (button == gcn::MouseEvent::LEFT || button == gcn::MouseEvent::RIGHT)
{
- int columns = getWidth() / gridWidth;
- int mx = event.getX();
- int my = event.getY();
- int index = mx / gridWidth + ((my / gridHeight) * columns) + mOffset;
-
- itemShortcut->setItemSelected(-1);
- setSelectedItemIndex(index);
+ const int index = getSlotIndex(event.getX(), event.getY());
+ if (index == Inventory::NO_SLOT_INDEX) {
+ return;
+ }
Item *item = mInventory->getItem(index);
- if (item)
+ // put item name into chat window
+ if (mDescItems)
+ {
+ chatWindow->addItemText(item->getInfo().getName());
+ }
+
+ if (mSelectedItem && mSelectedItem == item)
+ {
+ mSelectionStatus = SEL_DESELECTING;
+ }
+ else if (item && item->getId())
+ {
+ setSelectedItem(item);
+ mSelectionStatus = SEL_SELECTING;
+
itemShortcut->setItemSelected(item->getId());
+ }
+ else
+ {
+ setSelectedItem(NULL);
+ mSelectionStatus = SEL_NONE;
+ }
}
}
+void ItemContainer::mouseDragged(gcn::MouseEvent &event)
+{
+ if (mSelectionStatus != SEL_NONE)
+ {
+ mSelectionStatus = SEL_DRAGGING;
+ mDragPosX = event.getX();
+ mDragPosY = event.getY();
+ }
+}
+
+void ItemContainer::mouseReleased(gcn::MouseEvent &event)
+{
+ switch (mSelectionStatus)
+ {
+ case SEL_SELECTING:
+ mSelectionStatus = SEL_SELECTED;
+ return;
+ case SEL_DESELECTING:
+ setSelectedItem(NULL);
+ mSelectionStatus = SEL_NONE;
+ return;
+ case SEL_DRAGGING:
+ mSelectionStatus = SEL_SELECTED;
+ break;
+ default:
+ return;
+ };
+
+ int index = getSlotIndex(event.getX(), event.getY());
+ if (index == Inventory::NO_SLOT_INDEX) return;
+ Item *item = mInventory->getItem(index);
+ if (item == mSelectedItem) return;
+ player_node->moveInvItem(mSelectedItem, index);
+ setSelectedItem(NULL);
+ mSelectionStatus = SEL_NONE;
+}
+
+
// Show ItemTooltip
void ItemContainer::mouseMoved(gcn::MouseEvent &event)
{
@@ -273,11 +324,97 @@ void ItemContainer::mouseExited(gcn::MouseEvent &event)
mItemPopup->setVisible(false);
}
-int ItemContainer::getSlotIndex(int posX, int posY) const
+int ItemContainer::getSlotIndex(const int posX, const int posY) const
{
- int columns = getWidth() / gridWidth;
- int index = posX / gridWidth + ((posY / gridHeight) * columns) + mOffset;
+ if (getDimension().isPointInRect(posX, posY))
+ {
+ // Takes into account, boxes are overlapping each other.
+ return (posY / (BOX_HEIGHT - 1)) * mGridColumns + (posX / (BOX_WIDTH - 1));
+ }
+ return Inventory::NO_SLOT_INDEX;
+}
- return (index);
+void ItemContainer::keyAction()
+{
+ // If there is no highlight then return.
+ if (!mHighlightedItem)
+ return;
+
+ // If the highlight is on the selected item, then deselect it.
+ if (mHighlightedItem == mSelectedItem)
+ {
+ setSelectedItem(NULL);
+ mSelectionStatus = SEL_NONE;
+ }
+ // Check and swap items if necessary.
+ else if (mSwapItems &&
+ mSelectedItem &&
+ mHighlightedItem->getId())
+ {
+ player_node->moveInvItem(
+ mSelectedItem, mHighlightedItem->getInvIndex());
+ setSelectedItem(mHighlightedItem);
+ }
+ // If the highlight is on an item then select it.
+ else if (mHighlightedItem->getId())
+ {
+ setSelectedItem(mHighlightedItem);
+ mSelectionStatus = SEL_SELECTED;
+ }
+ // If the highlight is on a blank space then move it.
+ else if (mSelectedItem)
+ {
+ player_node->moveInvItem(
+ mSelectedItem, mHighlightedItem->getInvIndex());
+ setSelectedItem(NULL);
+ mSelectionStatus = SEL_NONE;
+ }
}
+void ItemContainer::moveHighlight(int direction)
+{
+ if (!mHighlightedItem)
+ {
+ if (mSelectedItem) {
+ mHighlightedItem = mSelectedItem;
+ }
+ else {
+ mHighlightedItem = mInventory->getItem(0);
+ }
+ return;
+ }
+
+ switch (direction)
+ {
+ case MOVE_SELECTED_LEFT:
+ if (mHighlightedItem->getInvIndex() % mGridColumns == 0)
+ {
+ mHighlightedItem += mGridColumns;
+ }
+ mHighlightedItem--;
+ break;
+ case MOVE_SELECTED_RIGHT:
+ if ((mHighlightedItem->getInvIndex() % mGridColumns) ==
+ (mGridColumns - 1))
+ {
+ mHighlightedItem -= mGridColumns;
+ }
+ mHighlightedItem++;
+ break;
+ case MOVE_SELECTED_UP:
+ if (mHighlightedItem->getInvIndex() / mGridColumns == 0)
+ {
+ mHighlightedItem += (mGridColumns * mGridRows);
+ }
+ mHighlightedItem -= mGridColumns;
+ break;
+ case MOVE_SELECTED_DOWN:
+ if ((mHighlightedItem->getInvIndex() / mGridColumns) ==
+ (mGridRows - 1))
+ {
+ mHighlightedItem -= (mGridColumns * mGridRows);
+ }
+ mHighlightedItem += mGridColumns;
+ break;
+ }
+}