diff options
author | Joshua Langley <joshlangley[at]optusnet.com.au> | 2007-08-21 15:11:19 +0000 |
---|---|---|
committer | Joshua Langley <joshlangley[at]optusnet.com.au> | 2007-08-21 15:11:19 +0000 |
commit | 0f8e80bcfe92ca39803f0443cf4b30e3f9171eb9 (patch) | |
tree | a07d923f09e8c9aa4a478e2bd572ab94edf09ae4 /src | |
parent | 148b62c05337eaaaf3a1ead212b3792122812cee (diff) | |
download | mana-0f8e80bcfe92ca39803f0443cf4b30e3f9171eb9.tar.gz mana-0f8e80bcfe92ca39803f0443cf4b30e3f9171eb9.tar.bz2 mana-0f8e80bcfe92ca39803f0443cf4b30e3f9171eb9.tar.xz mana-0f8e80bcfe92ca39803f0443cf4b30e3f9171eb9.zip |
mantis id=118 - Added Item Shortcut Bar.
Diffstat (limited to 'src')
-rw-r--r-- | src/game.cpp | 21 | ||||
-rw-r--r-- | src/gui/gui.cpp | 5 | ||||
-rw-r--r-- | src/gui/itemcontainer.cpp | 18 | ||||
-rw-r--r-- | src/gui/itemshortcutcontainer.cpp | 222 | ||||
-rw-r--r-- | src/gui/itemshortcutcontainer.h | 116 | ||||
-rw-r--r-- | src/gui/itemshortcutwindow.cpp | 95 | ||||
-rw-r--r-- | src/gui/itemshortcutwindow.h | 69 | ||||
-rw-r--r-- | src/gui/menuwindow.cpp | 7 | ||||
-rw-r--r-- | src/gui/setup_keyboard.cpp | 6 | ||||
-rw-r--r-- | src/gui/window.h | 6 | ||||
-rw-r--r-- | src/itemshortcut.cpp | 86 | ||||
-rw-r--r-- | src/itemshortcut.h | 127 | ||||
-rw-r--r-- | src/keyboardconfig.cpp | 12 | ||||
-rw-r--r-- | src/keyboardconfig.h | 10 | ||||
-rw-r--r-- | src/localplayer.cpp | 11 | ||||
-rw-r--r-- | src/localplayer.h | 8 | ||||
-rw-r--r-- | src/main.cpp | 6 | ||||
-rw-r--r-- | src/net/inventoryhandler.cpp | 3 | ||||
-rw-r--r-- | src/utils/tostring.h | 2 |
19 files changed, 813 insertions, 17 deletions
diff --git a/src/game.cpp b/src/game.cpp index a2ea9af6..ec0f7d24 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -36,6 +36,7 @@ #include "engine.h" #include "flooritemmanager.h" #include "graphics.h" +#include "itemshortcut.h" #include "joystick.h" #include "keyboardconfig.h" #include "localplayer.h" @@ -53,6 +54,7 @@ #include "gui/gui.h" #include "gui/help.h" #include "gui/inventorywindow.h" +#include "gui/itemshortcutwindow.h" #include "gui/menuwindow.h" #include "gui/minimap.h" #include "gui/ministatus.h" @@ -116,6 +118,7 @@ TradeWindow *tradeWindow; //BuddyWindow *buddyWindow; HelpWindow *helpWindow; DebugWindow *debugWindow; +ItemShortcutWindow *itemShortcutWindow; BeingManager *beingManager = NULL; FloorItemManager *floorItemManager = NULL; @@ -195,7 +198,7 @@ void createGuiWindows(Network *network) //buddyWindow = new BuddyWindow(); helpWindow = new HelpWindow(); debugWindow = new DebugWindow(); - + itemShortcutWindow = new ItemShortcutWindow(); // Initialize window positions //chargeDialog->setPosition( // graphics->getWidth() - 5 - chargeDialog->getWidth(), @@ -207,6 +210,7 @@ void createGuiWindows(Network *network) chatWindow->setVisible(true); miniStatusWindow->setVisible(true); menuWindow->setVisible(true); + itemShortcutWindow->setVisible(true); } /** @@ -234,6 +238,7 @@ void destroyGuiWindows() //delete buddyWindow; delete helpWindow; delete debugWindow; + delete itemShortcutWindow; } Game::Game(Network *network): @@ -509,7 +514,19 @@ void Game::handleInput() if (keyboard.isEnabled() && !chatWindow->isFocused()) { - switch (keyboard.getKeyIndex(event.key.keysym.sym)) { + const int tKey = keyboard.getKeyIndex(event.key.keysym.sym); + // Checks if any item shortcut is pressed. + for (int i = KeyboardConfig::KEY_SHORTCUT_0; + i <= KeyboardConfig::KEY_SHORTCUT_9; + i++) + { + if (tKey == i) { + itemShortcut->useItem( + i - KeyboardConfig::KEY_SHORTCUT_0); + break; + } + } + switch (tKey) { case KeyboardConfig::KEY_PICKUP: { FloorItem *item = floorItemManager->findByCoordinates( diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 3f9dfd73..3e7541cf 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -207,10 +207,9 @@ Gui::draw() int mouseX, mouseY; Uint8 button = SDL_GetMouseState(&mouseX, &mouseY); - if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS || button & SDL_BUTTON(1)) - && mCustomCursor) + if ((SDL_GetAppState() & SDL_APPMOUSEFOCUS || button & SDL_BUTTON(1)) && + mCustomCursor) { - static_cast<Graphics*>(mGraphics)->drawImage( mMouseCursor->get(mCursorType), mouseX - 15, diff --git a/src/gui/itemcontainer.cpp b/src/gui/itemcontainer.cpp index 334770f8..43de8db3 100644 --- a/src/gui/itemcontainer.cpp +++ b/src/gui/itemcontainer.cpp @@ -30,6 +30,7 @@ #include "../graphics.h" #include "../inventory.h" #include "../item.h" +#include "../itemshortcut.h" #include "../log.h" #include "../resources/image.h" @@ -197,9 +198,18 @@ ItemContainer::mousePressed(gcn::MouseEvent &event) int my = event.getY(); int index = mx / gridWidth + ((my / gridHeight) * columns) + 2; - if (index > INVENTORY_SIZE) - index = INVENTORY_SIZE - 1; - - setSelectedItem(mInventory->getItem(index)); + itemShortcut->setItemSelected(NULL); + // Fix for old server, it should be: if (index >= mMaxItems) + if (index > mMaxItems + 1) + { + setSelectedItem(NULL); + return; + } + Item *item = mInventory->getItem(index); + setSelectedItem(item); + if (!item->isEquipment()) + { + itemShortcut->setItemSelected(item); + } } } diff --git a/src/gui/itemshortcutcontainer.cpp b/src/gui/itemshortcutcontainer.cpp new file mode 100644 index 00000000..d9ece8d0 --- /dev/null +++ b/src/gui/itemshortcutcontainer.cpp @@ -0,0 +1,222 @@ +/* + * 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 "itemshortcutcontainer.h" + +#include "../graphics.h" +#include "../item.h" +#include "../itemshortcut.h" +#include "../keyboardconfig.h" + +#include "../resources/image.h" +#include "../resources/resourcemanager.h" + +#include "../utils/tostring.h" + +ItemShortcutContainer::ItemShortcutContainer(): + mItemClicked(false), + mItemMoved(NULL) +{ + addMouseListener(this); + + ResourceManager *resman = ResourceManager::getInstance(); + + mBackgroundImg = resman->getImage("graphics/gui/item_shortcut_bgr.png"); + mMaxItems = itemShortcut->getItemCount(); + + mBoxHeight = 42; + mBoxWidth = 36; +} + +ItemShortcutContainer::~ItemShortcutContainer() +{ + mBackgroundImg->decRef(); +} + +void +ItemShortcutContainer::logic() +{ + gcn::Widget::logic(); + + int i = itemShortcut->getItemCount(); + + if (i != mMaxItems) + { + mMaxItems = i; + setWidth(getWidth()); + } +} + +void +ItemShortcutContainer::draw(gcn::Graphics *graphics) +{ + Graphics *g = static_cast<Graphics*>(graphics); + + 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)); + g->drawText(key, itemX + 2, itemY + 2, gcn::Graphics::LEFT); + + Item *item = itemShortcut->getItem(i); + if (item) { + // Draw item icon. + Image* image = item->getInfo().getImage(); + if (image) { + g->drawImage(image, itemX, itemY); + g->drawText( + toString(item->getQuantity()), + itemX + mBoxWidth / 2, + itemY + mBoxHeight - 14, + gcn::Graphics::CENTER); + } + } + } + if (mItemMoved) + { + // Draw the item image being dragged by the cursor. + Image* image = mItemMoved->getInfo().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 +ItemShortcutContainer::setWidth(int width) +{ + gcn::Widget::setWidth(width); + + 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 +ItemShortcutContainer::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; + } + Item *item = itemShortcut->getItem(index); + if (item) + { + mItemMoved = item; + itemShortcut->removeItem(index); + } + } + if (mItemMoved) { + mCursorPosX = event.getX(); + mCursorPosY = event.getY(); + } + } +} + +void +ItemShortcutContainer::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(NULL); + } + else if (itemShortcut->getItem(index)) { + mItemClicked = true; + } +} + +void +ItemShortcutContainer::mouseReleased(gcn::MouseEvent &event) +{ + if (event.getButton() == gcn::MouseEvent::LEFT) + { + if (itemShortcut->isItemSelected()) + { + itemShortcut->setItemSelected(NULL); + } + const int index = getIndexFromGrid(event.getX(), event.getY()); + if (index == -1) { + mItemMoved = NULL; + return; + } + if (mItemMoved) { + itemShortcut->setItems(index, mItemMoved); + mItemMoved = NULL; + } + else if (itemShortcut->getItem(index) && mItemClicked) + { + itemShortcut->useItem(index); + } + if (mItemClicked) { + mItemClicked = false; + } + } +} + +int +ItemShortcutContainer::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/itemshortcutcontainer.h b/src/gui/itemshortcutcontainer.h new file mode 100644 index 00000000..a1ad53b7 --- /dev/null +++ b/src/gui/itemshortcutcontainer.h @@ -0,0 +1,116 @@ +/* + * 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_ITEMSHORTCUTCONTAINER_H__ +#define _TMW_ITEMSHORTCUTCONTAINER_H__ + +#include <guichan/mouselistener.hpp> +#include <guichan/widget.hpp> + +#include <list> + +class Image; +class Item; + +/** + * An item shortcut container. Used to quickly use items. + * + * \ingroup GUI + */ +class ItemShortcutContainer : public gcn::Widget, public gcn::MouseListener +{ + public: + /** + * Constructor. Initializes the graphic. + */ + ItemShortcutContainer(); + + /** + * Destructor. + */ + virtual ~ItemShortcutContainer(); + + /** + * Handles the logic of the ItemContainer + */ + void logic(); + + /** + * Draws the items. + */ + void draw(gcn::Graphics *graphics); + + /** + * Sets the width of the container. This is used to determine the new + * height of the container. + */ + void setWidth(int width); + + /** + * 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 ItemShortcutContainer::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/itemshortcutwindow.cpp new file mode 100644 index 00000000..2ee2cfde --- /dev/null +++ b/src/gui/itemshortcutwindow.cpp @@ -0,0 +1,95 @@ +/* + * 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 "itemshortcutwindow.h" + +#include "itemshortcutcontainer.h" +#include "scrollarea.h" + +static const int SCROLL_PADDING = 0; + +ItemShortcutWindow::ItemShortcutWindow() +{ + setWindowName("itemShortcut"); + // 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(); + + int border = (SCROLL_PADDING * 2) + (getPadding() * 2); + setMinWidth(mItems->getBoxWidth() + border); + setMinHeight(mItems->getBoxHeight() + border); + setMaxWidth(mItems->getBoxWidth() * mItems->getMaxItems() + border); + setMaxHeight(mItems->getBoxHeight() * mItems->getMaxItems() + border); + + mInvenScroll = new ScrollArea(mItems); + mInvenScroll->setPosition(SCROLL_PADDING, SCROLL_PADDING); + mInvenScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER); + + add(mInvenScroll); + + loadWindowState(); +} + +ItemShortcutWindow::~ItemShortcutWindow() +{ + delete mItems; + delete mInvenScroll; +} + +void ItemShortcutWindow::logic() +{ + Window::logic(); +} + +void ItemShortcutWindow::mouseDragged(gcn::MouseEvent &event) +{ + const int tWidth = getWidth(), tHeight = getHeight(); + Window::mouseDragged(event); + if (getWidth() != tWidth || getHeight() != tHeight) + { + updateWidgets(); + } +} + +void ItemShortcutWindow::updateWidgets() +{ + const gcn::Rectangle area = getChildrenArea(); + + mInvenScroll->setSize(area.width - SCROLL_PADDING, + area.height - SCROLL_PADDING); +} + +void ItemShortcutWindow::loadWindowState() +{ + Window::loadWindowState(); + updateWidgets(); +} + +void ItemShortcutWindow::resetToDefaultSize() +{ + Window::resetToDefaultSize(); + updateWidgets(); +} diff --git a/src/gui/itemshortcutwindow.h b/src/gui/itemshortcutwindow.h new file mode 100644 index 00000000..54611efe --- /dev/null +++ b/src/gui/itemshortcutwindow.h @@ -0,0 +1,69 @@ +/* + * 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_ITEMSHORTCUTWINDOW_H +#define _TMW_ITEMSHORTCUTWINDOW_H + +#include "window.h" + +#include "../guichanfwd.h" + +class ItemShortcutContainer; + +/** + * Inventory dialog. + * + * \ingroup Interface + */ +class ItemShortcutWindow : public Window +{ + public: + /** + * Constructor. + */ + ItemShortcutWindow(); + + /** + * Destructor. + */ + ~ItemShortcutWindow(); + + /** + * Logic (updates buttons and weight information). + */ + void logic(); + + void mouseDragged(gcn::MouseEvent &event); + + void loadWindowState(); + + void resetToDefaultSize(); + + private: + void updateWidgets(); /**< Updates widgets size/position. */ + + ItemShortcutContainer *mItems; + + gcn::ScrollArea *mInvenScroll; +}; +extern ItemShortcutWindow *itemShortcutWindow; +#endif diff --git a/src/gui/menuwindow.cpp b/src/gui/menuwindow.cpp index 943cc6f0..c7505bae 100644 --- a/src/gui/menuwindow.cpp +++ b/src/gui/menuwindow.cpp @@ -35,6 +35,7 @@ extern Window *inventoryWindow; extern Window *equipmentWindow; extern Window *skillDialog; extern Window *statusWindow; +extern Window *itemShortcutWindow; namespace { struct MenuWindowListener : public gcn::ActionListener @@ -61,6 +62,7 @@ MenuWindow::MenuWindow(): "Equipment", "Inventory", "Skills", + "Shortcut", "Setup", 0 }; @@ -108,11 +110,14 @@ void MenuWindowListener::action(const gcn::ActionEvent &event) { window = skillDialog; } + else if (event.getId() == "Shortcut") + { + window = itemShortcutWindow; + } else if (event.getId() == "Setup") { window = setupWindow; } - if (window) { window->setVisible(!window->isVisible()); if (window->isVisible()) { diff --git a/src/gui/setup_keyboard.cpp b/src/gui/setup_keyboard.cpp index 3c8c8163..e88080b5 100644 --- a/src/gui/setup_keyboard.cpp +++ b/src/gui/setup_keyboard.cpp @@ -78,16 +78,16 @@ Setup_Keyboard::Setup_Keyboard(): refreshKeys(); - mKeyList->setDimension(gcn::Rectangle(0, 0, 180, 140)); + mKeyList->setDimension(gcn::Rectangle(0, 0, 185, 140)); mKeyList->addActionListener(this); mKeyList->setSelected(-1); ScrollArea *scrollArea = new ScrollArea(mKeyList); - scrollArea->setDimension(gcn::Rectangle(10, 10, 180, 140)); + scrollArea->setDimension(gcn::Rectangle(10, 10, 200, 140)); add(scrollArea); mAssignKeyButton = new Button("Assign", "assign", this); - mAssignKeyButton->setPosition(145, 155); + mAssignKeyButton->setPosition(165, 155); mAssignKeyButton->addActionListener(this); mAssignKeyButton->setEnabled(false); add(mAssignKeyButton); diff --git a/src/gui/window.h b/src/gui/window.h index a44a56d8..03dfafb2 100644 --- a/src/gui/window.h +++ b/src/gui/window.h @@ -155,6 +155,12 @@ class Window : public gcn::Window void setMaxHeight(unsigned int height); /** + * Sets flag to show a title or not. + */ + void setShowTitle(bool flag) + { mShowTitle = flag; } + + /** * Sets whether the window is sticky. * A sticky window will not have its visibility set to false * on a general setVisible(false) call. diff --git a/src/itemshortcut.cpp b/src/itemshortcut.cpp new file mode 100644 index 00000000..53d121f6 --- /dev/null +++ b/src/itemshortcut.cpp @@ -0,0 +1,86 @@ +/* + * 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 "itemshortcut.h" + +#include "localplayer.h" +#include "configuration.h" + +#include "utils/tostring.h" + +ItemShortcut::ItemShortcut *itemShortcut; + +ItemShortcut::ItemShortcut(): + mItemSelected(NULL) +{ + for (int i = 0; i < SHORTCUT_ITEMS; i++) + { + mItems[i] = NULL; + } +} + +ItemShortcut::~ItemShortcut() +{ + save(); +} + +void ItemShortcut::load() +{ + for(int i = 0; i < SHORTCUT_ITEMS; i++) + { + int itemId = (int) config.getValue("itemShortcut"+toString(i), -1.0); + + if (itemId != -1) + { + ItemPtr item = player_node->searchForItem(itemId); + if (item) + { + mItems[i] = item; + } + } + } +} + +void ItemShortcut::save() +{ + for(int i = 0; i < SHORTCUT_ITEMS; i++) + { + if (mItems[i]) + { + config.setValue("shortcut"+toString(i), mItems[i]->getId()); + } + else + { + config.setValue("shortcut"+toString(i), -1); + } + } +} + +void ItemShortcut::useItem(int index) +{ + if (mItems[index]) + { + if (mItems[index]->getQuantity()) { + player_node->useItem(mItems[index]); + } + } +} diff --git a/src/itemshortcut.h b/src/itemshortcut.h new file mode 100644 index 00000000..23835c3b --- /dev/null +++ b/src/itemshortcut.h @@ -0,0 +1,127 @@ +/* + * 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_ITEMSHORTCUT_H__ +#define _TMW_ITEMSHORTCUT_H__ + +#include "item.h" + +#define SHORTCUT_ITEMS 10 + +/** + * The item pointer + */ +typedef Item* ItemPtr; + +class ItemShortcut { + public: + /** + * Constructor. + */ + ItemShortcut(); + + /** + * Destructor. + */ + ~ItemShortcut(); + + /** + * Load the configuration information. + */ + void load(); + + /** + * Returns the shortcut item specified by the index. + * + * @param index Index of the shortcut item. + */ + ItemPtr getItem(int index) const + { return mItems[index]; } + + /** + * Returns the amount of shortcut items. + */ + int getItemCount() const + { return SHORTCUT_ITEMS; } + + /** + * Returns the item that is currently selected. + */ + ItemPtr getItemSelected() const + { return mItemSelected; } + + /** + * Adds the selected item to the items specified by the index. + * + * @param index Index of the items. + */ + void setItem(int index) + { mItems[index] = mItemSelected; } + + /** + * Adds an item to the items store specified by the index. + * + * @param index Index of the items. + * @param item Item to store. + */ + void setItems(int index, Item *item) + { mItems[index] = item; } + + /** + * Set the item that is selected. + * + * @param item The item that is to be assigned. + */ + void setItemSelected(ItemPtr item) + { mItemSelected = item; } + + /** + * A flag to check if the item is selected. + */ + bool isItemSelected() + { return (mItemSelected) ? true : false; } + + /** + * Remove a item from the shortcut. + */ + void removeItem(int index) + { mItems[index] = NULL; } + + /** + * Try to use the item specified by the index. + * + * @param index Index of the item shortcut. + */ + void useItem(int index); + + private: + /** + * Save the configuration information. + */ + void save(); + + ItemPtr mItems[SHORTCUT_ITEMS]; /**< the items stored */ + ItemPtr mItemSelected; /**< the item held by cursor */ + +}; +extern ItemShortcut *itemShortcut; +#endif diff --git a/src/keyboardconfig.cpp b/src/keyboardconfig.cpp index aea96942..649d60b6 100644 --- a/src/keyboardconfig.cpp +++ b/src/keyboardconfig.cpp @@ -47,7 +47,17 @@ static KeyData const keyData[KeyboardConfig::KEY_TOTAL] = { {"keyTargetClosest", SDLK_a, "Target Closest"}, {"keyPickup", SDLK_z, "Pickup"}, {"keyHideWindows", SDLK_h, "Hide Windows"}, - {"keyBeingSit", SDLK_g, "Sit"} + {"keyBeingSit", SDLK_g, "Sit"}, + {"keyShortcut0", SDLK_0, "Item Shortcut 0"}, + {"keyShortcut1", SDLK_1, "Item Shortcut 1"}, + {"keyShortcut2", SDLK_2, "Item Shortcut 2"}, + {"keyShortcut3", SDLK_3, "Item Shortcut 3"}, + {"keyShortcut4", SDLK_4, "Item Shortcut 4"}, + {"keyShortcut5", SDLK_5, "Item Shortcut 5"}, + {"keyShortcut6", SDLK_6, "Item Shortcut 6"}, + {"keyShortcut7", SDLK_7, "Item Shortcut 7"}, + {"keyShortcut8", SDLK_8, "Item Shortcut 8"}, + {"keyShortcut9", SDLK_9, "Item Shortcut 9"} }; void KeyboardConfig::init() diff --git a/src/keyboardconfig.h b/src/keyboardconfig.h index 1f186ce1..46394fa5 100644 --- a/src/keyboardconfig.h +++ b/src/keyboardconfig.h @@ -156,6 +156,16 @@ class KeyboardConfig KEY_PICKUP, KEY_HIDE_WINDOWS, KEY_SIT, + KEY_SHORTCUT_0, + KEY_SHORTCUT_1, + KEY_SHORTCUT_2, + KEY_SHORTCUT_3, + KEY_SHORTCUT_4, + KEY_SHORTCUT_5, + KEY_SHORTCUT_6, + KEY_SHORTCUT_7, + KEY_SHORTCUT_8, + KEY_SHORTCUT_9, KEY_TOTAL }; diff --git a/src/localplayer.cpp b/src/localplayer.cpp index fa8db881..96f3f8c3 100644 --- a/src/localplayer.cpp +++ b/src/localplayer.cpp @@ -128,6 +128,17 @@ Item* LocalPlayer::getInvItem(int index) return mInventory->getItem(index); } +Item* LocalPlayer::searchForItem(int itemId) +{ + for(int i = 0; i < INVENTORY_SIZE; i++) + { + if (itemId == mInventory->getItem(i)->getId()) { + return mInventory->getItem(i); + } + } + return NULL; +} + void LocalPlayer::equipItem(Item *item) { MessageOut outMsg(mNetwork); diff --git a/src/localplayer.h b/src/localplayer.h index b721e276..9be185bf 100644 --- a/src/localplayer.h +++ b/src/localplayer.h @@ -80,6 +80,14 @@ class LocalPlayer : public Player Item* getInvItem(int index); /** + * Searches for the specified item by it's identification. + * + * @param itemId The id of the item to be searched. + * @return Item found on success, NULL on failure. + */ + Item* searchForItem(int itemId); + + /** * Equips an item. */ void equipItem(Item *item); diff --git a/src/main.cpp b/src/main.cpp index 613d78a7..5b7737d9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -48,6 +48,7 @@ #include "keyboardconfig.h" #include "game.h" #include "graphics.h" +#include "itemshortcut.h" #include "lockedarray.h" #include "localplayer.h" #include "log.h" @@ -300,10 +301,12 @@ void init_engine(const Options &options) bpp << " video mode: " << SDL_GetError() << std::endl; exit(1); } - // Initialize for drawing graphics->_beginDraw(); + // Initialize the item shortcuts. + itemShortcut = new ItemShortcut(); + gui = new Gui(graphics); state = UPDATE_STATE; /**< Initial game state */ @@ -328,6 +331,7 @@ void init_engine(const Options &options) /** Clear the engine */ void exit_engine() { + delete itemShortcut; config.write(); delete gui; diff --git a/src/net/inventoryhandler.cpp b/src/net/inventoryhandler.cpp index 67038c99..400c570d 100644 --- a/src/net/inventoryhandler.cpp +++ b/src/net/inventoryhandler.cpp @@ -30,6 +30,7 @@ #include "../resources/iteminfo.h" #include "../item.h" +#include "../itemshortcut.h" #include "../localplayer.h" #include "../gui/chat.h" @@ -72,13 +73,13 @@ void InventoryHandler::handleMessage(MessageIn *msg) msg->skip(8); // card (4 shorts) player_node->addInvItem(index, itemId, amount, false); - // Trick because arrows are not considered equipment if (itemId == 1199 || itemId == 529) { player_node->getInvItem(index)->setEquipment(true); } } + itemShortcut->load(); break; case SMSG_PLAYER_INVENTORY_ADD: diff --git a/src/utils/tostring.h b/src/utils/tostring.h index 8fc6d105..95b8985f 100644 --- a/src/utils/tostring.h +++ b/src/utils/tostring.h @@ -22,7 +22,7 @@ */ #ifndef _TMW_UTILS_TOSTRING_H -#define _TMW_UTISL_TOSTRING_H +#define _TMW_UTILS_TOSTRING_H #include <sstream> |