00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gui/widgets/button.h"
00023
00024 #include "gui/equipmentwindow.h"
00025 #include "gui/itempopup.h"
00026 #include "gui/palette.h"
00027 #include "gui/playerbox.h"
00028 #include "gui/viewport.h"
00029
00030 #include "equipment.h"
00031 #include "graphics.h"
00032 #include "inventory.h"
00033 #include "item.h"
00034 #include "localplayer.h"
00035
00036 #include "resources/image.h"
00037 #include "resources/iteminfo.h"
00038 #include "resources/resourcemanager.h"
00039
00040 #include "utils/gettext.h"
00041 #include "utils/stringutils.h"
00042
00043 #include <guichan/font.hpp>
00044
00045 static const int BOX_WIDTH = 36;
00046 static const int BOX_HEIGHT = 36;
00047
00048
00049 static const int boxPosition[][2] = {
00050 { 50, 208 },
00051 { 8, 123 },
00052 { 8, 78 },
00053 { 129, 168 },
00054 { 8, 168 },
00055 { 129, 123 },
00056 { 90, 208 },
00057 { 50, 40 },
00058 { 70, 0 },
00059 { 90, 40 },
00060 { 129, 78 }
00061 };
00062
00063 #ifdef TMWSERV_SUPPORT
00064 EquipmentWindow::EquipmentWindow(Equipment *equipment):
00065 #else
00066 EquipmentWindow::EquipmentWindow():
00067 #endif
00068 Window(_("Equipment")),
00069 #ifdef TMWSERV_SUPPORT
00070 mEquipment(equipment),
00071 #endif
00072 mSelected(-1)
00073 {
00074 mItemPopup = new ItemPopup;
00075
00076
00077 PlayerBox *playerBox = new PlayerBox;
00078 playerBox->setDimension(gcn::Rectangle(50, 80, 74, 123));
00079 playerBox->setPlayer(player_node);
00080
00081 setWindowName("Equipment");
00082 setCloseButton(true);
00083 setSaveVisible(true);
00084 setDefaultSize(180, 300, ImageRect::CENTER);
00085 loadWindowState();
00086
00087 gcn::Button *unequip = new Button(_("Unequip"), "unequip", this);
00088 gcn::Rectangle const &area = getChildrenArea();
00089 unequip->setPosition(area.width - unequip->getWidth() - 5,
00090 area.height - unequip->getHeight() - 5);
00091
00092 add(playerBox);
00093 add(unequip);
00094
00095 for (int i = 0; i < EQUIP_VECTOREND; i++)
00096 {
00097 mEquipBox[i].posX = boxPosition[i][0] + getPadding();
00098 mEquipBox[i].posY = boxPosition[i][1] + getTitleBarHeight();
00099 }
00100
00101 #ifdef EATHENA_SUPPORT
00102 mEquipment = player_node->mEquipment.get();
00103 mInventory = player_node->getInventory();
00104 #endif
00105 }
00106
00107 EquipmentWindow::~EquipmentWindow()
00108 {
00109 delete mItemPopup;
00110 }
00111
00112 void EquipmentWindow::draw(gcn::Graphics *graphics)
00113 {
00114
00115 Window::draw(graphics);
00116
00117 Graphics *g = static_cast<Graphics*>(graphics);
00118
00119 Window::drawChildren(graphics);
00120
00121 for (int i = 0; i < EQUIP_VECTOREND; i++)
00122 {
00123 if (i == mSelected)
00124 {
00125 const gcn::Color color = guiPalette->getColor(Palette::HIGHLIGHT);
00126
00127
00128 g->setColor(gcn::Color(color.r, color.g, color.b, getGuiAlpha()));
00129 g->fillRectangle(gcn::Rectangle(mEquipBox[i].posX, mEquipBox[i].posY,
00130 BOX_WIDTH, BOX_HEIGHT));
00131 }
00132
00133
00134 g->setColor(gcn::Color(0, 0, 0));
00135
00136 g->drawRectangle(gcn::Rectangle(mEquipBox[i].posX, mEquipBox[i].posY,
00137 BOX_WIDTH, BOX_HEIGHT));
00138
00139 #ifdef TMWSERV_SUPPORT
00140 Item *item = mEquipment->getEquipment(i);
00141 #else
00142 Item *item = (i != EQUIP_AMMO_SLOT) ?
00143 mInventory->getItem(mEquipment->getEquipment(i)) :
00144 mInventory->getItem(mEquipment->getArrows());
00145 #endif
00146 if (item)
00147 {
00148
00149 Image *image = item->getImage();
00150 g->drawImage(image, mEquipBox[i].posX, mEquipBox[i].posY);
00151 #ifdef EATHENA_SUPPORT
00152 if (i == EQUIP_AMMO_SLOT)
00153 {
00154 g->setColor(guiPalette->getColor(Palette::TEXT));
00155 graphics->drawText(toString(item->getQuantity()),
00156 mEquipBox[i].posX + (BOX_WIDTH / 2),
00157 mEquipBox[i].posY - getFont()->getHeight(),
00158 gcn::Graphics::CENTER);
00159 }
00160 #endif
00161 }
00162 }
00163 }
00164
00165 void EquipmentWindow::action(const gcn::ActionEvent &event)
00166 {
00167 if (event.getId() == "unequip" && mSelected > -1)
00168 {
00169 #ifdef TMWSERV_SUPPORT // TODO: merge these!
00170 Item *item = mEquipment->getEquipment(mSelected);
00171 #else
00172 Item *item = (mSelected != EQUIP_AMMO_SLOT) ?
00173 mInventory->getItem(mEquipment->getEquipment(mSelected)) :
00174 mInventory->getItem(mEquipment->getArrows());
00175 #endif
00176 player_node->unequipItem(item);
00177 mSelected = -1;
00178 }
00179 }
00180
00181 Item* EquipmentWindow::getItem(int x, int y) const
00182 {
00183 for (int i = 0; i < EQUIP_VECTOREND; i++)
00184 {
00185 gcn::Rectangle tRect(mEquipBox[i].posX, mEquipBox[i].posY,
00186 BOX_WIDTH, BOX_HEIGHT);
00187
00188 if (tRect.isPointInRect(x, y))
00189 {
00190 #ifdef TMWSERV_SUPPORT
00191 return mEquipment->getEquipment(i);
00192 #else
00193 return (i != EQUIP_AMMO_SLOT) ?
00194 mInventory->getItem(mEquipment->getEquipment(i)) :
00195 mInventory->getItem(mEquipment->getArrows());
00196 #endif
00197 }
00198 }
00199 return NULL;
00200 }
00201
00202 void EquipmentWindow::mousePressed(gcn::MouseEvent& mouseEvent)
00203 {
00204 Window::mousePressed(mouseEvent);
00205
00206 const int x = mouseEvent.getX();
00207 const int y = mouseEvent.getY();
00208
00209 if (mouseEvent.getButton() == gcn::MouseEvent::LEFT)
00210 {
00211
00212 for (int i = 0; i < EQUIP_VECTOREND; i++)
00213 {
00214 #ifdef TMWSERV_SUPPORT
00215 Item *item = mEquipment->getEquipment(i);
00216 #else
00217 Item *item = (i != EQUIP_AMMO_SLOT) ?
00218 mInventory->getItem(mEquipment->getEquipment(i)) :
00219 mInventory->getItem(mEquipment->getArrows());
00220 #endif
00221 gcn::Rectangle tRect(mEquipBox[i].posX, mEquipBox[i].posY,
00222 BOX_WIDTH, BOX_HEIGHT);
00223
00224 if (tRect.isPointInRect(x, y) && item)
00225 mSelected = i;
00226 }
00227 }
00228 else if (mouseEvent.getButton() == gcn::MouseEvent::RIGHT)
00229 {
00230 if (Item *item = getItem(x, y))
00231 {
00232
00233
00234
00235 const int mx = x + getX();
00236 const int my = y + getY();
00237 viewport->showPopup(mx, my, item);
00238 }
00239 }
00240 }
00241
00242
00243 void EquipmentWindow::mouseMoved(gcn::MouseEvent &event)
00244 {
00245 const int x = event.getX();
00246 const int y = event.getY();
00247
00248 Item *item = getItem(x, y);
00249
00250 if (item)
00251 {
00252 int mouseX, mouseY;
00253 SDL_GetMouseState(&mouseX, &mouseY);
00254
00255 mItemPopup->setItem(item->getInfo());
00256 mItemPopup->view(x + getX(), y + getY());
00257 }
00258 else
00259 {
00260 mItemPopup->setVisible(false);
00261 }
00262 }
00263
00264
00265 void EquipmentWindow::mouseExited(gcn::MouseEvent &event)
00266 {
00267 mItemPopup->setVisible(false);
00268 }