1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
/*
* The ManaPlus Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2010 The Mana Developers
* Copyright (C) 2011-2012 The ManaPlus Developers
*
* This file is part of The ManaPlus Client.
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "gui/equipmentwindow.h"
#include "gui/widgets/button.h"
#include "being.h"
#include "equipment.h"
#include "graphics.h"
#include "inventory.h"
#include "item.h"
#include "localplayer.h"
#include "gui/itempopup.h"
#include "gui/theme.h"
#include "gui/setup.h"
#include "gui/viewport.h"
#include "gui/widgets/playerbox.h"
#include "net/inventoryhandler.h"
#include "net/net.h"
#include "resources/image.h"
#include "resources/imageset.h"
#include "resources/iteminfo.h"
#include "resources/resourcemanager.h"
#include "utils/dtor.h"
#include "utils/gettext.h"
#include "utils/stringutils.h"
#include <guichan/font.hpp>
#include "debug.h"
static const int BOX_WIDTH = 36;
static const int BOX_HEIGHT = 36;
static const int BOX_X_PAD = (BOX_WIDTH - 32) / 2;
static const int BOX_Y_PAD = (BOX_HEIGHT - 32) / 2;
EquipmentWindow::EquipmentWindow(Equipment *equipment, Being *being,
bool foring):
Window(_("Equipment"), false, nullptr, "equipment.xml"),
mEquipment(equipment),
mSelected(-1),
mForing(foring),
mImageSet(0)
{
mBeing = being;
mItemPopup = new ItemPopup;
if (setupWindow)
setupWindow->registerWindowForReset(this);
// Control that shows the Player
mPlayerBox = new PlayerBox;
mPlayerBox->setDimension(gcn::Rectangle(50, 80, 74, 168));
mPlayerBox->setPlayer(being);
if (foring)
setWindowName("Being equipment");
else
setWindowName("Equipment");
setCloseButton(true);
setSaveVisible(true);
setStickyButtonLock(true);
setDefaultSize(180, 345, ImageRect::CENTER);
mBoxes.reserve(13);
for (int f = 0; f < 13; f ++)
mBoxes.push_back(nullptr);
fillBoxes();
loadWindowState();
mUnequip = new Button(_("Unequip"), "unequip", this);
const gcn::Rectangle &area = getChildrenArea();
mUnequip->setPosition(area.width - mUnequip->getWidth() - 5,
area.height - mUnequip->getHeight() - 5);
mUnequip->setEnabled(false);
add(mPlayerBox);
add(mUnequip);
mHighlightColor = Theme::getThemeColor(Theme::HIGHLIGHT);
mBorderColor = Theme::getThemeColor(Theme::BORDER);
setForegroundColor(Theme::getThemeColor(Theme::TEXT));
}
EquipmentWindow::~EquipmentWindow()
{
delete mItemPopup;
mItemPopup = nullptr;
if (this == beingEquipmentWindow)
{
if (mEquipment)
delete mEquipment->getBackend();
delete mEquipment;
mEquipment = nullptr;
}
delete_all(mBoxes);
mBoxes.clear();
if (mImageSet)
{
mImageSet->decRef();
mImageSet = nullptr;
}
}
void EquipmentWindow::draw(gcn::Graphics *graphics)
{
// Draw window graphics
Window::draw(graphics);
Graphics *g = static_cast<Graphics*>(graphics);
Window::drawChildren(graphics);
int i = 0;
const int fontHeight = getFont()->getHeight();
std::vector<EquipmentBox*>::const_iterator it;
std::vector<EquipmentBox*>::const_iterator it_end = mBoxes.end();
for (it = mBoxes.begin(); it != it_end; ++ it, ++ i)
{
EquipmentBox *box = *it;
if (!box)
continue;
if (i == mSelected)
{
mHighlightColor.a = getGuiAlpha();
// Set color to the highlight color
g->setColor(mHighlightColor);
g->fillRectangle(gcn::Rectangle(box->x, box->y,
BOX_WIDTH, BOX_HEIGHT));
}
// Set color black
g->setColor(mBorderColor);
// Draw box border
g->drawRectangle(gcn::Rectangle(box->x, box->y,
BOX_WIDTH, BOX_HEIGHT));
if (!mEquipment)
continue;
Item *item = mEquipment->getEquipment(i);
if (item)
{
// Draw Item.
Image *image = item->getImage();
if (image)
{
image->setAlpha(1.0f); // Ensure the image is drawn
// with maximum opacity
g->drawImage(image, box->x + 2, box->y + 2);
if (i == EQUIP_PROJECTILE_SLOT)
{
g->setColor(getForegroundColor());
graphics->drawText(toString(item->getQuantity()),
box->x + (BOX_WIDTH / 2), box->y - fontHeight,
gcn::Graphics::CENTER);
}
}
}
else if (box->image)
{
g->drawImage(box->image, box->x + BOX_X_PAD,
box->y + BOX_Y_PAD);
}
}
}
void EquipmentWindow::action(const gcn::ActionEvent &event)
{
if (!mEquipment)
return;
if (event.getId() == "unequip" && mSelected > -1)
{
Item *item = mEquipment->getEquipment(mSelected);
Net::getInventoryHandler()->unequipItem(item);
setSelected(-1);
}
}
Item *EquipmentWindow::getItem(int x, int y) const
{
if (!mEquipment)
return nullptr;
std::vector<EquipmentBox*>::const_iterator it;
std::vector<EquipmentBox*>::const_iterator it_end = mBoxes.end();
int i = 0;
for (it = mBoxes.begin(); it != it_end; ++ it, ++ i)
{
EquipmentBox *box = *it;
if (!box)
continue;
const gcn::Rectangle tRect(box->x, box->y,
BOX_WIDTH, BOX_HEIGHT);
if (tRect.isPointInRect(x, y))
return mEquipment->getEquipment(i);
}
return nullptr;
}
void EquipmentWindow::mousePressed(gcn::MouseEvent& mouseEvent)
{
Window::mousePressed(mouseEvent);
if (!mEquipment)
return;
const int x = mouseEvent.getX();
const int y = mouseEvent.getY();
if (mouseEvent.getButton() == gcn::MouseEvent::LEFT)
{
if (mForing)
return;
// Checks if any of the presses were in the equip boxes.
std::vector<EquipmentBox*>::const_iterator it;
std::vector<EquipmentBox*>::const_iterator it_end = mBoxes.end();
int i = 0;
for (it = mBoxes.begin(); it != it_end; ++ it, ++ i)
{
EquipmentBox *box = *it;
if (!box)
continue;
Item *item = mEquipment->getEquipment(i);
const gcn::Rectangle tRect(box->x, box->y,
BOX_WIDTH, BOX_HEIGHT);
if (tRect.isPointInRect(x, y) && item)
setSelected(i);
}
}
else if (mouseEvent.getButton() == gcn::MouseEvent::RIGHT)
{
if (Item *item = getItem(x, y))
{
if (mItemPopup)
mItemPopup->setVisible(false);
/* Convert relative to the window coordinates to absolute screen
* coordinates.
*/
const int mx = x + getX();
const int my = y + getY();
if (viewport)
{
if (mForing)
viewport->showUndressPopup(mx, my, mBeing, item);
else
viewport->showPopup(this, mx, my, item, true);
}
}
}
}
// Show ItemTooltip
void EquipmentWindow::mouseMoved(gcn::MouseEvent &event)
{
if (!mItemPopup)
return;
const int x = event.getX();
const int y = event.getY();
Item *item = getItem(x, y);
if (item)
{
int mouseX, mouseY;
SDL_GetMouseState(&mouseX, &mouseY);
mItemPopup->setItem(item);
mItemPopup->position(x + getX(), y + getY());
}
else
{
mItemPopup->setVisible(false);
}
}
// Hide ItemTooltip
void EquipmentWindow::mouseExited(gcn::MouseEvent &event A_UNUSED)
{
if (mItemPopup)
mItemPopup->setVisible(false);
}
void EquipmentWindow::setSelected(int index)
{
mSelected = index;
if (mUnequip)
mUnequip->setEnabled(mSelected != -1);
}
void EquipmentWindow::setBeing(Being *being)
{
mPlayerBox->setPlayer(being);
mBeing = being;
if (mEquipment)
delete mEquipment->getBackend();
delete mEquipment;
if (!being)
{
mEquipment = nullptr;
return;
}
mEquipment = being->getEquipment();
}
void EquipmentWindow::updateBeing(Being *being)
{
if (being == mBeing)
setBeing(being);
}
void EquipmentWindow::resetBeing(Being *being)
{
if (being == mBeing)
setBeing(nullptr);
}
void EquipmentWindow::fillBoxes()
{
XML::Document *doc = new XML::Document("equipmentwindow.xml");
XmlNodePtr root = doc->rootNode();
if (!root)
{
delete doc;
fillDefault();
return;
}
if (mImageSet)
mImageSet->decRef();
mImageSet = Theme::getImageSetFromTheme(XML::getProperty(
root, "image", "equipmentbox.png"), 32, 32);
for_each_xml_child_node(node, root)
{
if (xmlNameEqual(node, "window"))
loadWindow(node);
else if (xmlNameEqual(node, "playerbox"))
loadPlayerBox(node);
else if (xmlNameEqual(node, "slot"))
loadSlot(node, mImageSet);
}
delete doc;
}
void EquipmentWindow::loadWindow(XmlNodePtr windowNode)
{
setDefaultSize(XML::getProperty(windowNode, "width", 180),
XML::getProperty(windowNode, "height", 345), ImageRect::CENTER);
}
void EquipmentWindow::loadPlayerBox(XmlNodePtr playerBoxNode)
{
mPlayerBox->setDimension(gcn::Rectangle(
XML::getProperty(playerBoxNode, "x", 50),
XML::getProperty(playerBoxNode, "y", 80),
XML::getProperty(playerBoxNode, "width", 74),
XML::getProperty(playerBoxNode, "height", 168)));
}
void EquipmentWindow::loadSlot(XmlNodePtr slotNode, ImageSet *imageset)
{
int slot = parseSlotName(XML::getProperty(slotNode, "name", ""));
if (slot < 0)
return;
const int x = XML::getProperty(slotNode, "x", 0) + getPadding();
const int y = XML::getProperty(slotNode, "y", 0) + getTitleBarHeight();
const int imageIndex = XML::getProperty(slotNode, "image", -1);
Image *image = nullptr;
if (imageset && imageIndex >= 0 && imageIndex < (signed)imageset->size())
image = imageset->get(imageIndex);
if (mBoxes[slot])
{
EquipmentBox *box = mBoxes[slot];
box->x = x;
box->y = y;
box->image = image;
}
else
{
mBoxes[slot] = new EquipmentBox(x, y, image);
}
}
int EquipmentWindow::parseSlotName(std::string name)
{
int id = -1;
if (name == "shoes" || name == "boot" || name == "boots")
{
id = 4;
}
else if (name == "bottomclothes" || name == "bottom" || name == "pants")
{
id = 3;
}
else if (name == "topclothes" || name == "top"
|| name == "torso" || name == "body")
{
id = 0;
}
else if (name == "free" || name == "misc1")
{
id = 5;
}
else if (name == "misc2" || name == "scarf" || name == "scarfs")
{
id = 7;
}
else if (name == "hat" || name == "hats")
{
id = 2;
}
else if (name == "wings")
{
id = 6;
}
else if (name == "glove" || name == "gloves")
{
id = 1;
}
else if (name == "weapon" || name == "weapons")
{
id = 8;
}
else if (name == "shield" || name == "shields")
{
id = 9;
}
else if (name == "amulet" || name == "amulets")
{
id = 11;
}
else if (name == "ring" || name == "rings")
{
id = 12;
}
else if (name == "arrow" || name == "arrows" || name == "ammo")
{
id = 10;
}
return id;
}
void EquipmentWindow::fillDefault()
{
if (mImageSet)
mImageSet->decRef();
mImageSet = Theme::getImageSetFromTheme(
"equipmentbox.png", 32, 32);
addBox(0, 90, 40, 0); // torso
addBox(1, 8, 78, 1); // gloves
addBox(2, 70, 0, 2); // hat
addBox(3, 50, 253, 3); // pants
addBox(4, 90, 253, 4); // boots
addBox(5, 8, 213, 5); // FREE
addBox(6, 129, 213, 6); // wings
addBox(7, 50, 40, 5); // scarf
addBox(8, 8, 168, 7); // weapon
addBox(9, 129, 168, 8); // shield
addBox(10, 129, 78, 9); // ammo
addBox(11, 8, 123, 5); // amulet
addBox(12, 129, 123, 5); // ring
}
void EquipmentWindow::addBox(int idx, int x, int y, int imageIndex)
{
Image *image = nullptr;
if (mImageSet && imageIndex >= 0 && imageIndex < (signed)mImageSet->size())
image = mImageSet->get(imageIndex);
mBoxes[idx] = new EquipmentBox(x + getPadding(), y + getTitleBarHeight(),
image);
}
|