summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/gui/shortcutcontainer.cpp237
-rw-r--r--src/gui/shortcutcontainer.h115
-rw-r--r--src/gui/shortcutwindow.cpp (renamed from src/gui/itemshortcutwindow.cpp)14
-rw-r--r--src/gui/shortcutwindow.h (renamed from src/gui/itemshortcutwindow.h)18
-rw-r--r--src/gui/smileycontainer.cpp164
-rw-r--r--src/gui/smileycontainer.h139
-rw-r--r--src/gui/smileyshortcutcontainer.cpp243
-rw-r--r--src/gui/smileyshortcutcontainer.h111
-rw-r--r--src/gui/smileywindow.cpp96
-rw-r--r--src/gui/smileywindow.h80
10 files changed, 1202 insertions, 15 deletions
diff --git a/src/gui/shortcutcontainer.cpp b/src/gui/shortcutcontainer.cpp
new file mode 100644
index 00000000..f1a60189
--- /dev/null
+++ b/src/gui/shortcutcontainer.cpp
@@ -0,0 +1,237 @@
+/*
+ * The Mana World
+ * Copyright 2007 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "shortcutcontainer.h"
+
+#include "../graphics.h"
+#include "../inventory.h"
+#include "../item.h"
+#include "../itemshortcut.h"
+#include "../keyboardconfig.h"
+#include "../localplayer.h"
+
+#include "../resources/image.h"
+#include "../resources/resourcemanager.h"
+
+#include "../utils/tostring.h"
+
+ShortcutContainer::ShortcutContainer():
+ mGridWidth(1),
+ mGridHeight(1),
+ mItemClicked(false),
+ mItemMoved(NULL)
+{
+ addMouseListener(this);
+ addWidgetListener(this);
+
+ ResourceManager *resman = ResourceManager::getInstance();
+
+ mBackgroundImg = resman->getImage("graphics/gui/item_shortcut_bgr.png");
+ mMaxItems = itemShortcut->getItemCount();
+
+ mBoxHeight = 42;
+ mBoxWidth = 36;
+}
+
+ShortcutContainer::~ShortcutContainer()
+{
+ mBackgroundImg->decRef();
+}
+
+void
+ShortcutContainer::logic()
+{
+ gcn::Widget::logic();
+
+ int i = itemShortcut->getItemCount();
+
+ if (i != mMaxItems)
+ {
+ mMaxItems = i;
+ setWidth(getWidth());
+ }
+}
+
+void
+ShortcutContainer::draw(gcn::Graphics *graphics)
+{
+ Graphics *g = static_cast<Graphics*>(graphics);
+
+ graphics->setFont(getFont());
+
+ for (int i = 0; i < mMaxItems; i++)
+ {
+ const int itemX = (i % mGridWidth) * mBoxWidth;
+ const int itemY = (i / mGridWidth) * mBoxHeight;
+
+ g->drawImage(mBackgroundImg, itemX, itemY);
+
+ // Draw item keyboard shortcut.
+ const char *key = SDL_GetKeyName(
+ (SDLKey) keyboard.getKeyValue(keyboard.KEY_SHORTCUT_0 + i));
+ graphics->setColor(0x000000);
+ g->drawText(key, itemX + 2, itemY + 2, gcn::Graphics::LEFT);
+
+ if (itemShortcut->getItem(i) < 0)
+ continue;
+
+ Item *item =
+ player_node->getInventory()->findItem(itemShortcut->getItem(i));
+ if (item) {
+ // Draw item icon.
+ const std::string label =
+ item->isEquipped() ? "Eq." : toString(item->getQuantity());
+ Image* image = item->getImage();
+ if (image) {
+ const std::string label =
+ item->isEquipped() ? "Eq." : toString(item->getQuantity());
+ g->drawImage(image, itemX, itemY);
+ g->drawText(
+ label,
+ itemX + mBoxWidth / 2,
+ itemY + mBoxHeight - 14,
+ gcn::Graphics::CENTER);
+ }
+ }
+ }
+ if (mItemMoved)
+ {
+ // Draw the item image being dragged by the cursor.
+ Image* image = mItemMoved->getImage();
+ if (image)
+ {
+ const int tPosX = mCursorPosX - (image->getWidth() / 2);
+ const int tPosY = mCursorPosY - (image->getHeight() / 2);
+
+ g->drawImage(image, tPosX, tPosY);
+ g->drawText(
+ toString(mItemMoved->getQuantity()),
+ tPosX + mBoxWidth / 2,
+ tPosY + mBoxHeight - 14,
+ gcn::Graphics::CENTER);
+ }
+ }
+}
+
+void ShortcutContainer::widgetResized(const gcn::Event &event)
+{
+ mGridWidth = getWidth() / mBoxWidth;
+ if (mGridWidth < 1) {
+ mGridWidth = 1;
+ }
+
+ setHeight((mMaxItems / mGridWidth +
+ (mMaxItems % mGridWidth > 0 ? 1 : 0)) * mBoxHeight);
+
+ mGridHeight = getHeight() / mBoxHeight;
+ if (mGridHeight < 1) {
+ mGridHeight = 1;
+ }
+}
+
+void
+ShortcutContainer::mouseDragged(gcn::MouseEvent &event)
+{
+ if (event.getButton() == gcn::MouseEvent::LEFT) {
+ if (!mItemMoved && mItemClicked) {
+ const int index = getIndexFromGrid(event.getX(), event.getY());
+ if (index == -1) {
+ return;
+ }
+ const int itemId = itemShortcut->getItem(index);
+ if (itemId < 0)
+ return;
+ Item *item = player_node->getInventory()->findItem(itemId);
+ if (item)
+ {
+ mItemMoved = item;
+ itemShortcut->removeItem(index);
+ }
+ }
+ if (mItemMoved) {
+ mCursorPosX = event.getX();
+ mCursorPosY = event.getY();
+ }
+ }
+}
+
+void
+ShortcutContainer::mousePressed(gcn::MouseEvent &event)
+{
+ const int index = getIndexFromGrid(event.getX(), event.getY());
+ if (index == -1) {
+ return;
+ }
+
+ // Stores the selected item if theirs one.
+ if (itemShortcut->isItemSelected()) {
+ itemShortcut->setItem(index);
+ itemShortcut->setItemSelected(-1);
+ }
+ else if (itemShortcut->getItem(index)) {
+ mItemClicked = true;
+ }
+}
+
+void
+ShortcutContainer::mouseReleased(gcn::MouseEvent &event)
+{
+ if (event.getButton() == gcn::MouseEvent::LEFT)
+ {
+ if (itemShortcut->isItemSelected())
+ {
+ itemShortcut->setItemSelected(-1);
+ }
+ const int index = getIndexFromGrid(event.getX(), event.getY());
+ if (index == -1) {
+ mItemMoved = NULL;
+ return;
+ }
+ if (mItemMoved) {
+ itemShortcut->setItems(index, mItemMoved->getId());
+ mItemMoved = NULL;
+ }
+ else if (itemShortcut->getItem(index) && mItemClicked)
+ {
+ itemShortcut->useItem(index);
+ }
+ if (mItemClicked) {
+ mItemClicked = false;
+ }
+ }
+}
+
+int
+ShortcutContainer::getIndexFromGrid(int pointX, int pointY) const
+{
+ const gcn::Rectangle tRect = gcn::Rectangle(
+ 0, 0, mGridWidth * mBoxWidth, mGridHeight * mBoxHeight);
+ if (!tRect.isPointInRect(pointX, pointY)) {
+ return -1;
+ }
+ const int index = ((pointY / mBoxHeight) * mGridWidth) +
+ pointX / mBoxWidth;
+ if (index >= mMaxItems)
+ {
+ return -1;
+ }
+ return index;
+}
diff --git a/src/gui/shortcutcontainer.h b/src/gui/shortcutcontainer.h
new file mode 100644
index 00000000..5ca56899
--- /dev/null
+++ b/src/gui/shortcutcontainer.h
@@ -0,0 +1,115 @@
+/*
+ * The Mana World
+ * Copyright 2007 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef _TMW_SHORTCUTCONTAINER_H__
+#define _TMW_SHORTCUTCONTAINER_H__
+
+#include <guichan/mouselistener.hpp>
+#include <guichan/widget.hpp>
+#include <guichan/widgetlistener.hpp>
+
+#include "../guichanfwd.h"
+
+class Image;
+class Item;
+
+/**
+ * An item shortcut container. Used to quickly use items.
+ *
+ * \ingroup GUI
+ */
+class ShortcutContainer : public gcn::Widget,
+ public gcn::WidgetListener,
+ public gcn::MouseListener
+{
+ public:
+ /**
+ * Constructor. Initializes the graphic.
+ */
+ ShortcutContainer();
+
+ /**
+ * Destructor.
+ */
+ virtual ~ShortcutContainer();
+
+ /**
+ * Handles the logic of the ItemContainer
+ */
+ virtual void logic();
+
+ /**
+ * Draws the items.
+ */
+ virtual void draw(gcn::Graphics *graphics);
+
+ /**
+ * Invoked when a widget changes its size. This is used to determine
+ * the new height of the container.
+ */
+ virtual void widgetResized(const gcn::Event &event);
+
+ /**
+ * Handles mouse when dragged.
+ */
+ virtual void mouseDragged(gcn::MouseEvent &event);
+
+ /**
+ * Handles mouse when pressed.
+ */
+ virtual void mousePressed(gcn::MouseEvent &event);
+
+ /**
+ * Handles mouse release.
+ */
+ virtual void mouseReleased(gcn::MouseEvent &event);
+
+ virtual int getMaxItems()
+ { return mMaxItems; }
+
+ virtual int getBoxWidth()
+ { return mBoxWidth; }
+
+ virtual int getBoxHeight()
+ { return mBoxHeight; }
+
+ protected:
+ /**
+ * Gets the index from the grid provided the point is in an item box.
+ *
+ * @param pointX X coordinate of the point.
+ * @param pointY Y coordinate of the point.
+ * @return index on success, -1 on failure.
+ */
+ int getIndexFromGrid(int pointX, int pointY) const;
+
+ Image *mBackgroundImg;
+
+ int mMaxItems;
+ int mBoxWidth;
+ int mBoxHeight;
+ int mCursorPosX, mCursorPosY;
+ int mGridWidth, mGridHeight;
+ bool mItemClicked;
+ Item *mItemMoved;
+};
+
+#endif
diff --git a/src/gui/itemshortcutwindow.cpp b/src/gui/shortcutwindow.cpp
index 08e76241..d2aa2c2c 100644
--- a/src/gui/itemshortcutwindow.cpp
+++ b/src/gui/shortcutwindow.cpp
@@ -19,22 +19,22 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#include "itemshortcutcontainer.h"
-#include "itemshortcutwindow.h"
+#include "shortcutcontainer.h"
+#include "shortcutwindow.h"
#include "scrollarea.h"
static const int SCROLL_PADDING = 0;
-ItemShortcutWindow::ItemShortcutWindow()
+ShortcutWindow::ShortcutWindow(const char * title,ShortcutContainer *content)
{
- setWindowName("ItemShortcut");
+ setWindowName(title);
// no title presented, title bar is padding so window can be moved.
gcn::Window::setTitleBarHeight(gcn::Window::getPadding());
setShowTitle(false);
setResizable(true);
setDefaultSize(758, 174, 42, 426);
- mItems = new ItemShortcutContainer;
+ mItems = content;
const int border = SCROLL_PADDING * 2 + getPadding() * 2;
setMinWidth(mItems->getBoxWidth() + border);
@@ -52,13 +52,13 @@ ItemShortcutWindow::ItemShortcutWindow()
loadWindowState();
}
-ItemShortcutWindow::~ItemShortcutWindow()
+ShortcutWindow::~ShortcutWindow()
{
delete mItems;
delete mScrollArea;
}
-void ItemShortcutWindow::widgetResized(const gcn::Event &event)
+void ShortcutWindow::widgetResized(const gcn::Event &event)
{
Window::widgetResized(event);
diff --git a/src/gui/itemshortcutwindow.h b/src/gui/shortcutwindow.h
index 587f15c8..c9ba9d2e 100644
--- a/src/gui/itemshortcutwindow.h
+++ b/src/gui/shortcutwindow.h
@@ -19,14 +19,14 @@
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
-#ifndef _TMW_ITEMSHORTCUTWINDOW_H
-#define _TMW_ITEMSHORTCUTWINDOW_H
+#ifndef _TMW_SHORTCUTWINDOW_H
+#define _TMW_SHORTCUTWINDOW_H
#include "window.h"
#include "../guichanfwd.h"
-class ItemShortcutContainer;
+class ShortcutContainer;
class ScrollArea;
/**
@@ -34,18 +34,18 @@ class ScrollArea;
*
* \ingroup Interface
*/
-class ItemShortcutWindow : public Window
+class ShortcutWindow : public Window
{
public:
/**
* Constructor.
*/
- ItemShortcutWindow();
+ ShortcutWindow(const char *title,ShortcutContainer *content);
/**
* Destructor.
*/
- ~ItemShortcutWindow();
+ ~ShortcutWindow();
/**
* Called whenever the widget changes size.
@@ -53,11 +53,13 @@ class ItemShortcutWindow : public Window
void widgetResized(const gcn::Event &event);
private:
- ItemShortcutContainer *mItems;
+ ShortcutWindow();
+ ShortcutContainer *mItems;
ScrollArea *mScrollArea;
};
-extern ItemShortcutWindow *itemShortcutWindow;
+extern ShortcutWindow *itemShortcutWindow;
+extern ShortcutWindow *smileyShortcutWindow;
#endif
diff --git a/src/gui/smileycontainer.cpp b/src/gui/smileycontainer.cpp
new file mode 100644
index 00000000..5eb99723
--- /dev/null
+++ b/src/gui/smileycontainer.cpp
@@ -0,0 +1,164 @@
+/*
+ * The Mana World
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "smileycontainer.h"
+
+#include <guichan/mouseinput.hpp>
+#include <guichan/selectionlistener.hpp>
+
+#include "../graphics.h"
+#include "../log.h"
+
+#include "../resources/image.h"
+#include "../resources/iteminfo.h"
+#include "../resources/resourcemanager.h"
+
+#include "../utils/tostring.h"
+
+const int SmileyContainer::gridWidth = 34; // item icon width + 4
+const int SmileyContainer::gridHeight = 36; // item icon height + 4
+
+static const int NO_ITEM = -1;
+
+SmileyContainer::SmileyContainer():
+ mSelectedItemIndex(NO_ITEM)
+{
+ ResourceManager *resman = ResourceManager::getInstance();
+
+ mSmileyImg = resman->getImageSet("graphics/gui/emotions.png",30,32);
+ if (!mSmileyImg) logger->error("Unable to load emotions");
+
+ mSelImg = resman->getImage("graphics/gui/selection.png");
+ if (!mSelImg) logger->error("Unable to load selection.png");
+
+ mMaxSmiley = mSmileyImg->size();
+
+ addMouseListener(this);
+ addWidgetListener(this);
+}
+
+SmileyContainer::~SmileyContainer()
+{
+ if (mSmileyImg)
+ {
+ mSmileyImg->decRef();
+ mSmileyImg=NULL;
+ }
+ if (!mSelImg)
+ {
+ mSelImg->decRef();
+ mSelImg=NULL;
+ }
+}
+
+void SmileyContainer::draw(gcn::Graphics *graphics)
+{
+ int columns = getWidth() / gridWidth;
+
+ // Have at least 1 column
+ if (columns < 1)
+ {
+ columns = 1;
+ }
+
+ for (int i = 0; i < mMaxSmiley ; i++)
+ {
+ int itemX = ((i) % columns) * gridWidth;
+ int itemY = ((i) / columns) * gridHeight;
+
+
+ // Draw item icon
+ static_cast<Graphics*>(graphics)->drawImage(
+ mSmileyImg->get(i), itemX, itemY);
+
+ // Draw selection image below selected item
+ if (mSelectedItemIndex == i)
+ {
+ static_cast<Graphics*>(graphics)->drawImage(
+ mSelImg, itemX, itemY);
+ }
+ }
+}
+
+void SmileyContainer::widgetResized(const gcn::Event &event)
+{
+ recalculateHeight();
+}
+
+void SmileyContainer::recalculateHeight()
+{
+ int cols = getWidth() / gridWidth;
+
+ if (cols < 1)
+ cols = 1;
+
+ const int rows = (mMaxSmiley / cols) + (mMaxSmiley % cols > 0 ? 1 : 0);
+ const int height = rows * gridHeight + 8;
+ if (height != getHeight())
+ setHeight(height);
+}
+
+int SmileyContainer::getSelectedSmiley()
+{
+ if (mSelectedItemIndex == NO_ITEM)
+ return 0;
+
+ return 1+mSelectedItemIndex;
+}
+
+void SmileyContainer::selectNone()
+{
+ setSelectedItemIndex(NO_ITEM);
+}
+
+void SmileyContainer::setSelectedItemIndex(int index)
+{
+ if (index < 0 || index >= mMaxSmiley )
+ mSelectedItemIndex = NO_ITEM;
+ else
+ mSelectedItemIndex = index;
+}
+
+void SmileyContainer::distributeValueChangedEvent()
+{
+ 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)
+ {
+ (*i)->valueChanged(event);
+ }
+}
+
+void SmileyContainer::mousePressed(gcn::MouseEvent &event)
+{
+ 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);
+
+ setSelectedItemIndex(index);
+ }
+}
diff --git a/src/gui/smileycontainer.h b/src/gui/smileycontainer.h
new file mode 100644
index 00000000..a6dadab6
--- /dev/null
+++ b/src/gui/smileycontainer.h
@@ -0,0 +1,139 @@
+/*
+ * The Mana World
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef _TMW_SMILEYCONTAINER_H__
+#define _TMW_SMILEYCONTAINER_H__
+
+#include <list>
+
+#include <resources/imageset.h>
+#include <guichan/mouselistener.hpp>
+#include <guichan/widget.hpp>
+#include <guichan/widgetlistener.hpp>
+
+#include "../guichanfwd.h"
+
+class Image;
+class Inventory;
+class Item;
+
+namespace gcn {
+ class SelectionListener;
+}
+
+/**
+ * An item container. Used to show items in inventory and trade dialog.
+ *
+ * \ingroup GUI
+ */
+class SmileyContainer : public gcn::Widget,
+ public gcn::MouseListener,
+ public gcn::WidgetListener
+{
+ public:
+ /**
+ * Constructor. Initializes the graphic.
+ */
+ SmileyContainer();
+
+ /**
+ * Destructor.
+ */
+ virtual ~SmileyContainer();
+
+ /**
+ * Draws the items.
+ */
+ void draw(gcn::Graphics *graphics);
+
+ /**
+ * Called whenever the widget changes size.
+ */
+ void widgetResized(const gcn::Event &event);
+
+ /**
+ * Handles mouse click.
+ */
+ void mousePressed(gcn::MouseEvent &event);
+
+ /**
+ * Returns the selected item.
+ */
+ int getSelectedSmiley();
+
+ /**
+ * Sets selected item to NULL.
+ */
+ void selectNone();
+
+ /**
+ * Adds a listener to the list that's notified each time a change to
+ * the selection occurs.
+ */
+ void addSelectionListener(gcn::SelectionListener *listener)
+ {
+ mListeners.push_back(listener);
+ }
+
+ /**
+ * Removes a listener from the list that's notified each time a change
+ * to the selection occurs.
+ */
+ void removeSelectionListener(gcn::SelectionListener *listener)
+ {
+ mListeners.remove(listener);
+ }
+
+ private:
+ /**
+
+ * Sets the currently selected item. Invalid (e.g., negative) indices set `no item'.
+ */
+ void setSelectedItemIndex(int index);
+
+ /**
+ * Find the current item index by the most recently used item ID
+ */
+ void refindSelectedItem(void);
+
+ /**
+ * Determine and set the height of the container.
+ */
+ void recalculateHeight(void);
+
+ /**
+ * Sends out selection events to the list of selection listeners.
+ */
+ void distributeValueChangedEvent(void);
+
+ ImageSet *mSmileyImg;
+ Image *mSelImg;
+ int mSelectedItemIndex;
+
+ int mMaxSmiley;
+
+ std::list<gcn::SelectionListener*> mListeners;
+
+ static const int gridWidth;
+ static const int gridHeight;
+};
+
+#endif
diff --git a/src/gui/smileyshortcutcontainer.cpp b/src/gui/smileyshortcutcontainer.cpp
new file mode 100644
index 00000000..5a9c3036
--- /dev/null
+++ b/src/gui/smileyshortcutcontainer.cpp
@@ -0,0 +1,243 @@
+/*
+ * The Mana World
+ * Copyright 2007 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include "smileyshortcutcontainer.h"
+
+#include "../graphics.h"
+#include "../inventory.h"
+#include "../item.h"
+#include "../itemshortcut.h"
+#include "../keyboardconfig.h"
+#include "../localplayer.h"
+#include "../log.h"
+
+
+#include "../resources/image.h"
+#include "../resources/resourcemanager.h"
+
+#include "../utils/tostring.h"
+
+SmileyShortcutContainer::SmileyShortcutContainer():
+ mGridWidth(1),
+ mGridHeight(1),
+ mItemClicked(false),
+ mItemMoved(NULL)
+{
+ addMouseListener(this);
+ addWidgetListener(this);
+
+ ResourceManager *resman = ResourceManager::getInstance();
+
+ mBackgroundImg = resman->getImage("graphics/gui/item_shortcut_bgr.png");
+ mSmileyImg = resman->getImageSet("graphics/gui/emotions.png",30,32);
+ if (!mSmileyImg) logger->error("Unable to load emotions");
+
+ mMaxItems = 12;
+
+ mBoxHeight = 42;
+ mBoxWidth = 36;
+}
+
+SmileyShortcutContainer::~SmileyShortcutContainer()
+{
+ mBackgroundImg->decRef();
+ if (mSmileyImg)
+ {
+ mSmileyImg->decRef();
+ mSmileyImg=NULL;
+ }
+
+}
+
+void
+SmileyShortcutContainer::draw(gcn::Graphics *graphics)
+{
+ Graphics *g = static_cast<Graphics*>(graphics);
+
+ graphics->setFont(getFont());
+
+ for (int i = 0; i < mMaxItems; i++)
+ {
+ const int itemX = (i % mGridWidth) * mBoxWidth;
+ const int itemY = (i / mGridWidth) * mBoxHeight;
+
+ g->drawImage(mBackgroundImg, itemX, itemY);
+
+ // Draw item keyboard shortcut.
+ const char *key = SDL_GetKeyName(
+ (SDLKey) keyboard.getKeyValue(keyboard.KEY_SMILEY_1 + i));
+ graphics->setColor(0x000000);
+ g->drawText(key, itemX + 2, itemY + 2, gcn::Graphics::LEFT);
+ static_cast<Graphics*>(graphics)->drawImage(
+ mSmileyImg->get(i), itemX+2, itemY+10);
+
+#if 0
+ if (itemShortcut->getItem(i) < 0)
+ continue;
+
+ Item *item =
+ player_node->getInventory()->findItem(itemShortcut->getItem(i));
+ if (item) {
+ // Draw item icon.
+ const std::string label =
+ item->isEquipped() ? "Eq." : toString(item->getQuantity());
+ Image* image = item->getImage();
+ if (image) {
+ const std::string label =
+ item->isEquipped() ? "Eq." : toString(item->getQuantity());
+ g->drawImage(image, itemX, itemY);
+ g->drawText(
+ label,
+ itemX + mBoxWidth / 2,
+ itemY + mBoxHeight - 14,
+ gcn::Graphics::CENTER);
+ }
+ }
+#endif
+ }
+ if (mItemMoved)
+ {
+ // Draw the item image being dragged by the cursor.
+ Image* image = mItemMoved->getImage();
+ if (image)
+ {
+ const int tPosX = mCursorPosX - (image->getWidth() / 2);
+ const int tPosY = mCursorPosY - (image->getHeight() / 2);
+
+ g->drawImage(image, tPosX, tPosY);
+ g->drawText(
+ toString(mItemMoved->getQuantity()),
+ tPosX + mBoxWidth / 2,
+ tPosY + mBoxHeight - 14,
+ gcn::Graphics::CENTER);
+ }
+ }
+}
+
+void SmileyShortcutContainer::widgetResized(const gcn::Event &event)
+{
+ mGridWidth = getWidth() / mBoxWidth;
+ if (mGridWidth < 1) {
+ mGridWidth = 1;
+ }
+
+ setHeight((mMaxItems / mGridWidth +
+ (mMaxItems % mGridWidth > 0 ? 1 : 0)) * mBoxHeight);
+
+ mGridHeight = getHeight() / mBoxHeight;
+ if (mGridHeight < 1) {
+ mGridHeight = 1;
+ }
+}
+
+void
+SmileyShortcutContainer::mouseDragged(gcn::MouseEvent &event)
+{
+ if (event.getButton() == gcn::MouseEvent::LEFT) {
+ if (!mItemMoved && mItemClicked) {
+ const int index = getIndexFromGrid(event.getX(), event.getY());
+ if (index == -1) {
+ return;
+ }
+#if 0
+ const int itemId = itemShortcut->getItem(index);
+ if (itemId < 0)
+ return;
+ Item *item = player_node->getInventory()->findItem(itemId);
+ if (item)
+ {
+ mItemMoved = item;
+ itemShortcut->removeItem(index);
+ }
+#endif
+ }
+ if (mItemMoved) {
+ mCursorPosX = event.getX();
+ mCursorPosY = event.getY();
+ }
+ }
+}
+
+void
+SmileyShortcutContainer::mousePressed(gcn::MouseEvent &event)
+{
+ const int index = getIndexFromGrid(event.getX(), event.getY());
+ if (index == -1) {
+ return;
+ }
+#if 0
+ // Stores the selected item if theirs one.
+ if (itemShortcut->isItemSelected()) {
+ itemShortcut->setItem(index);
+ itemShortcut->setItemSelected(-1);
+ }
+ else if (itemShortcut->getItem(index)) {
+ mItemClicked = true;
+ }
+#endif
+}
+
+void
+SmileyShortcutContainer::mouseReleased(gcn::MouseEvent &event)
+{
+ if (event.getButton() == gcn::MouseEvent::LEFT)
+ {
+#if 0
+ if (itemShortcut->isItemSelected())
+ {
+ itemShortcut->setItemSelected(-1);
+ }
+ const int index = getIndexFromGrid(event.getX(), event.getY());
+ if (index == -1) {
+ mItemMoved = NULL;
+ return;
+ }
+ if (mItemMoved) {
+ itemShortcut->setItems(index, mItemMoved->getId());
+ mItemMoved = NULL;
+ }
+ else if (itemShortcut->getItem(index) && mItemClicked)
+ {
+ itemShortcut->useItem(index);
+ }
+ if (mItemClicked) {
+ mItemClicked = false;
+ }
+#endif
+ }
+}
+
+int
+SmileyShortcutContainer::getIndexFromGrid(int pointX, int pointY) const
+{
+ const gcn::Rectangle tRect = gcn::Rectangle(
+ 0, 0, mGridWidth * mBoxWidth, mGridHeight * mBoxHeight);
+ if (!tRect.isPointInRect(pointX, pointY)) {
+ return -1;
+ }
+ const int index = ((pointY / mBoxHeight) * mGridWidth) +
+ pointX / mBoxWidth;
+ if (index >= mMaxItems)
+ {
+ return -1;
+ }
+ return index;
+}
diff --git a/src/gui/smileyshortcutcontainer.h b/src/gui/smileyshortcutcontainer.h
new file mode 100644
index 00000000..72ddd54a
--- /dev/null
+++ b/src/gui/smileyshortcutcontainer.h
@@ -0,0 +1,111 @@
+/*
+ * The Mana World
+ * Copyright 2007 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef _TMW_SMILEYSHORTCUTCONTAINER_H__
+#define _TMW_SMILEYSHORTCUTCONTAINER_H__
+
+#include <guichan/mouselistener.hpp>
+#include <guichan/widget.hpp>
+#include <guichan/widgetlistener.hpp>
+#include <resources/imageset.h>
+
+#include "../guichanfwd.h"
+#include "shortcutcontainer.h"
+
+class Image;
+class Item;
+
+/**
+ * A smiley shortcut container. Used to quickly use items.
+ *
+ * \ingroup GUI
+ */
+class SmileyShortcutContainer : public ShortcutContainer
+{
+ public:
+ /**
+ * Constructor. Initializes the graphic.
+ */
+ SmileyShortcutContainer();
+
+ /**
+ * Destructor.
+ */
+ virtual ~SmileyShortcutContainer();
+
+ /**
+ * Draws the items.
+ */
+ void draw(gcn::Graphics *graphics);
+
+ /**
+ * Invoked when a widget changes its size. This is used to determine
+ * the new height of the container.
+ */
+ void widgetResized(const gcn::Event &event);
+
+ /**
+ * Handles mouse when dragged.
+ */
+ void mouseDragged(gcn::MouseEvent &event);
+
+ /**
+ * Handles mouse when pressed.
+ */
+ void mousePressed(gcn::MouseEvent &event);
+
+ /**
+ * Handles mouse release.
+ */
+ void mouseReleased(gcn::MouseEvent &event);
+
+ int getMaxItems()
+ { return mMaxItems; }
+
+ int getBoxWidth()
+ { return mBoxWidth; }
+
+ int getBoxHeight()
+ { return mBoxHeight; }
+
+ private:
+ /**
+ * Gets the index from the grid provided the point is in an item box.
+ *
+ * @param pointX X coordinate of the point.
+ * @param pointY Y coordinate of the point.
+ * @return index on success, -1 on failure.
+ */
+ int getIndexFromGrid(int pointX, int pointY) const;
+
+ Image *mBackgroundImg;
+ ImageSet *mSmileyImg;
+
+ int mMaxItems;
+ int mBoxWidth;
+ int mBoxHeight;
+ int mCursorPosX, mCursorPosY;
+ int mGridWidth, mGridHeight;
+ bool mItemClicked;
+ Item *mItemMoved;
+};
+
+#endif
diff --git a/src/gui/smileywindow.cpp b/src/gui/smileywindow.cpp
new file mode 100644
index 00000000..c7c9d995
--- /dev/null
+++ b/src/gui/smileywindow.cpp
@@ -0,0 +1,96 @@
+/*
+ * The Mana World
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#include <string>
+
+#include <guichan/mouseinput.hpp>
+
+#include "button.h"
+#include "gui.h"
+#include "smileywindow.h"
+#include "smileycontainer.h"
+#include "scrollarea.h"
+#include "../localplayer.h"
+#include "../utils/tostring.h"
+
+SmileyWindow::SmileyWindow():
+ Window("Smiley")
+{
+ setWindowName("Smiley");
+ setResizable(true);
+ setCloseButton(true);
+ setMinWidth(80);
+ setDefaultSize(115, 25, 322, 200);
+
+ mUseButton = new Button("Use", "use", this);
+
+ mItems = new SmileyContainer();
+ mItems->addSelectionListener(this);
+
+ mInvenScroll = new ScrollArea(mItems);
+ mInvenScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
+
+ draw();
+
+ add(mUseButton);
+ add(mInvenScroll);
+
+ mUseButton->setSize(60, mUseButton->getHeight());
+
+ loadWindowState();
+}
+
+void SmileyWindow::action(const gcn::ActionEvent &event)
+{
+ int item = mItems->getSelectedSmiley();
+
+ if (!item)
+ return;
+
+ player_node->emote(item);
+}
+
+
+void SmileyWindow::draw()
+{
+ const gcn::Rectangle &area = getChildrenArea();
+ const int width = area.width;
+ const int height = area.height;
+
+ mUseButton->setPosition(8, height - 8 - mUseButton->getHeight());
+
+ mInvenScroll->setSize(width - 16, mUseButton->getY() - 18);
+ mInvenScroll->setPosition(8, 10);
+
+ setMinHeight(130);
+}
+
+void SmileyWindow::widgetResized(const gcn::Event &event)
+{
+ Window::widgetResized(event);
+ draw();
+}
+
+
+int SmileyWindow::getSelectedSmiley() const
+{
+ return mItems->getSelectedSmiley();
+}
diff --git a/src/gui/smileywindow.h b/src/gui/smileywindow.h
new file mode 100644
index 00000000..81ec6663
--- /dev/null
+++ b/src/gui/smileywindow.h
@@ -0,0 +1,80 @@
+/*
+ * The Mana World
+ * Copyright 2004 The Mana World Development Team
+ *
+ * This file is part of The Mana World.
+ *
+ * The Mana World 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.
+ *
+ * The Mana World 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 The Mana World; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+
+#ifndef _TMW_SMILEYWINDOW_H
+#define _TMW_SMILEYWINDOW_H
+
+#include <guichan/actionlistener.hpp>
+#include <guichan/selectionlistener.hpp>
+
+#include "textbox.h"
+#include "window.h"
+
+#include "../guichanfwd.h"
+
+class Item;
+class SmileyContainer;
+
+/**
+ * smiley dialog.
+ *
+ * \ingroup Interface
+ */
+class SmileyWindow : public Window, gcn::ActionListener,
+ gcn::SelectionListener
+{
+ public:
+ /**
+ * Constructor.
+ */
+ SmileyWindow();
+
+ /**
+ * Called when receiving actions from the widgets.
+ */
+ void action(const gcn::ActionEvent &event);
+
+ /**
+ * Returns the selected item.
+ */
+ int getSelectedSmiley() const;
+
+ /**
+ * Updates window drawing.
+ */
+ void draw();
+
+ /**
+ * Called whenever the widget changes size.
+ */
+ void widgetResized(const gcn::Event &event);
+
+ private:
+
+ SmileyContainer *mItems;
+
+ gcn::Button *mUseButton;
+ gcn::ScrollArea *mInvenScroll;
+};
+
+extern SmileyWindow *smileyWindow;
+
+#endif