00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gui/itemshortcutcontainer.h"
00023
00024 #include "gui/inventorywindow.h"
00025 #include "gui/itempopup.h"
00026 #include "gui/palette.h"
00027 #include "gui/viewport.h"
00028
00029 #include "configuration.h"
00030 #include "graphics.h"
00031 #include "inventory.h"
00032 #include "item.h"
00033 #include "itemshortcut.h"
00034 #include "keyboardconfig.h"
00035 #include "localplayer.h"
00036
00037 #include "resources/image.h"
00038 #include "resources/iteminfo.h"
00039 #include "resources/resourcemanager.h"
00040
00041 #include "utils/stringutils.h"
00042
00043 ItemShortcutContainer::ItemShortcutContainer():
00044 ShortcutContainer(),
00045 mItemClicked(false),
00046 mItemMoved(NULL)
00047 {
00048 addMouseListener(this);
00049 addWidgetListener(this);
00050
00051 mItemPopup = new ItemPopup;
00052
00053 ResourceManager *resman = ResourceManager::getInstance();
00054
00055 mBackgroundImg = resman->getImage("graphics/gui/item_shortcut_bgr.png");
00056 mMaxItems = itemShortcut->getItemCount();
00057
00058 mBackgroundImg->setAlpha(config.getValue("guialpha", 0.8));
00059
00060 mBoxHeight = mBackgroundImg->getHeight();
00061 mBoxWidth = mBackgroundImg->getWidth();
00062 }
00063
00064 ItemShortcutContainer::~ItemShortcutContainer()
00065 {
00066 mBackgroundImg->decRef();
00067 delete mItemPopup;
00068 }
00069
00070 void ItemShortcutContainer::draw(gcn::Graphics *graphics)
00071 {
00072 if (config.getValue("guialpha", 0.8) != mAlpha)
00073 {
00074 mAlpha = config.getValue("guialpha", 0.8);
00075 mBackgroundImg->setAlpha(mAlpha);
00076 }
00077
00078 Graphics *g = static_cast<Graphics*>(graphics);
00079
00080 graphics->setFont(getFont());
00081
00082 for (int i = 0; i < mMaxItems; i++)
00083 {
00084 const int itemX = (i % mGridWidth) * mBoxWidth;
00085 const int itemY = (i / mGridWidth) * mBoxHeight;
00086
00087 g->drawImage(mBackgroundImg, itemX, itemY);
00088
00089
00090 const char *key = SDL_GetKeyName(
00091 (SDLKey) keyboard.getKeyValue(keyboard.KEY_SHORTCUT_1 + i));
00092 graphics->setColor(guiPalette->getColor(Palette::TEXT));
00093 g->drawText(key, itemX + 2, itemY + 2, gcn::Graphics::LEFT);
00094
00095 if (itemShortcut->getItem(i) < 0)
00096 continue;
00097
00098 Item *item =
00099 player_node->getInventory()->findItem(itemShortcut->getItem(i));
00100
00101 if (item)
00102 {
00103
00104 Image* image = item->getImage();
00105
00106 if (image)
00107 {
00108 std::string caption;
00109 if (item->getQuantity() > 1)
00110 caption = toString(item->getQuantity());
00111 else if (item->isEquipped())
00112 caption = "(Eq)";
00113
00114 g->drawImage(image, itemX, itemY);
00115 if (item->isEquipped())
00116 g->setColor(guiPalette->getColor(Palette::ITEM_EQUIPPED));
00117 g->drawText(caption, itemX + mBoxWidth / 2,
00118 itemY + mBoxHeight - 14, gcn::Graphics::CENTER);
00119 }
00120 }
00121 }
00122
00123 if (mItemMoved)
00124 {
00125
00126 Image* image = mItemMoved->getImage();
00127 if (image)
00128 {
00129 const int tPosX = mCursorPosX - (image->getWidth() / 2);
00130 const int tPosY = mCursorPosY - (image->getHeight() / 2);
00131
00132 g->drawImage(image, tPosX, tPosY);
00133 g->drawText(toString(mItemMoved->getQuantity()),
00134 tPosX + mBoxWidth / 2, tPosY + mBoxHeight - 14,
00135 gcn::Graphics::CENTER);
00136 }
00137 }
00138 }
00139
00140 void ItemShortcutContainer::mouseDragged(gcn::MouseEvent &event)
00141 {
00142 if (event.getButton() == gcn::MouseEvent::LEFT)
00143 {
00144 if (!mItemMoved && mItemClicked)
00145 {
00146 const int index = getIndexFromGrid(event.getX(), event.getY());
00147 const int itemId = itemShortcut->getItem(index);
00148
00149 if (index == -1 || itemId < 0)
00150 return;
00151
00152 Item *item = player_node->getInventory()->findItem(itemId);
00153
00154 if (item)
00155 {
00156 mItemMoved = item;
00157 itemShortcut->removeItem(index);
00158 }
00159 }
00160 if (mItemMoved)
00161 {
00162 mCursorPosX = event.getX();
00163 mCursorPosY = event.getY();
00164 }
00165 }
00166 }
00167
00168 void ItemShortcutContainer::mousePressed(gcn::MouseEvent &event)
00169 {
00170 const int index = getIndexFromGrid(event.getX(), event.getY());
00171
00172 if (index == -1)
00173 return;
00174
00175 if (event.getButton() == gcn::MouseEvent::LEFT)
00176 {
00177
00178 if (itemShortcut->isItemSelected() && inventoryWindow->isVisible())
00179 {
00180 itemShortcut->setItem(index);
00181 itemShortcut->setItemSelected(-1);
00182 }
00183 else if (itemShortcut->getItem(index))
00184 mItemClicked = true;
00185 }
00186 else if (event.getButton() == gcn::MouseEvent::RIGHT)
00187 {
00188 Item *item = player_node->getInventory()->
00189 findItem(itemShortcut->getItem(index));
00190
00191 if (!item)
00192 return;
00193
00194
00195
00196 viewport->showPopup(viewport->getMouseX(), viewport->getMouseY(), item);
00197 }
00198 }
00199
00200 void ItemShortcutContainer::mouseReleased(gcn::MouseEvent &event)
00201 {
00202 if (event.getButton() == gcn::MouseEvent::LEFT)
00203 {
00204 if (itemShortcut->isItemSelected())
00205 itemShortcut->setItemSelected(-1);
00206
00207 const int index = getIndexFromGrid(event.getX(), event.getY());
00208 if (index == -1)
00209 {
00210 mItemMoved = NULL;
00211 return;
00212 }
00213 if (mItemMoved)
00214 {
00215 itemShortcut->setItems(index, mItemMoved->getId());
00216 mItemMoved = NULL;
00217 }
00218 else if (itemShortcut->getItem(index) && mItemClicked)
00219 {
00220 itemShortcut->useItem(index);
00221 }
00222
00223 if (mItemClicked)
00224 mItemClicked = false;
00225 }
00226 }
00227
00228
00229 void ItemShortcutContainer::mouseMoved(gcn::MouseEvent &event)
00230 {
00231 const int index = getIndexFromGrid(event.getX(), event.getY());
00232 const int itemId = itemShortcut->getItem(index);
00233
00234 if (index == -1 || itemId < 0)
00235 return;
00236
00237 Item *item = player_node->getInventory()->findItem(itemId);
00238
00239 if (item && inventoryWindow->isVisible())
00240 {
00241 mItemPopup->setItem(item->getInfo());
00242 mItemPopup->view(viewport->getMouseX(), viewport->getMouseY());
00243 }
00244 else
00245 {
00246 mItemPopup->setVisible(false);
00247 }
00248 }
00249
00250
00251 void ItemShortcutContainer::mouseExited(gcn::MouseEvent &event)
00252 {
00253 mItemPopup->setVisible(false);
00254 }