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
|
/*
* The Mana Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2012 The Mana Developers
*
* This file is part of The Mana 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/skilldialog.h"
#include "log.h"
#include "playerinfo.h"
#include "configuration.h"
#include "gui/setup.h"
#include "gui/widgets/button.h"
#include "gui/widgets/label.h"
#include "gui/widgets/listbox.h"
#include "gui/widgets/progressbar.h"
#include "gui/widgets/scrollarea.h"
#include "gui/widgets/tab.h"
#include "gui/widgets/tabbedarea.h"
#include "gui/widgets/windowcontainer.h"
#include "net/net.h"
#include "net/playerhandler.h"
#include "resources/image.h"
#include "resources/resourcemanager.h"
#include "resources/theme.h"
#include "utils/dtor.h"
#include "utils/gettext.h"
#include "utils/stringutils.h"
#include "utils/xml.h"
#include <guichan/font.hpp>
#include <string>
#define SKILLS_FILE "skills.xml"
class SkillModel;
class SkillEntry;
struct SkillInfo
{
unsigned short id;
std::string name;
Image *icon = nullptr;
bool modifiable;
bool visible;
SkillModel *model = nullptr;
std::string skillLevel;
int skillLevelWidth;
std::string skillExp;
float progress;
gcn::Color color;
~SkillInfo()
{
if (icon)
icon->decRef();
}
void setIcon(const std::string &iconPath)
{
ResourceManager *res = ResourceManager::getInstance();
if (!iconPath.empty())
{
icon = res->getImage(iconPath);
}
if (!icon)
{
icon = Theme::getImageFromTheme(
paths.getStringValue("unknownItemFile"));
}
}
void update();
void draw(Graphics *graphics, int y, int width);
};
using SkillList = std::vector<SkillInfo *>;
class SkillModel : public gcn::ListModel
{
public:
int getNumberOfElements() override
{ return mVisibleSkills.size(); }
SkillInfo *getSkillAt(int i) const
{ return mVisibleSkills.at(i); }
std::string getElementAt(int i) override
{ return getSkillAt(i)->name; }
void updateVisibilities();
void addSkill(SkillInfo *info)
{ mSkills.push_back(info); }
private:
SkillList mSkills;
SkillList mVisibleSkills;
};
class SkillListBox : public ListBox
{
public:
SkillListBox(SkillModel *model):
ListBox(model)
{}
SkillInfo *getSelectedInfo()
{
const int selected = getSelected();
if (selected < 0 || selected > mListModel->getNumberOfElements())
return nullptr;
return static_cast<SkillModel*>(mListModel)->getSkillAt(selected);
}
void draw(gcn::Graphics *gcnGraphics) override
{
if (!mListModel)
return;
auto* model = static_cast<SkillModel*>(mListModel);
updateAlpha();
auto *graphics = static_cast<Graphics*>(gcnGraphics);
graphics->setColor(Theme::getThemeColor(Theme::HIGHLIGHT,
(int) (mAlpha * 255.0f)));
graphics->setFont(getFont());
// Draw filled rectangle around the selected list element
if (mSelected >= 0)
{
graphics->fillRectangle(gcn::Rectangle(0, getRowHeight() * mSelected,
getWidth(), getRowHeight()));
}
// Draw the list elements
graphics->setColor(Theme::getThemeColor(Theme::TEXT));
for (int i = 0, y = 1;
i < model->getNumberOfElements();
++i, y += getRowHeight())
{
SkillInfo *e = model->getSkillAt(i);
if (e)
{
e->draw(graphics, y, getWidth());
}
}
}
unsigned int getRowHeight() const override { return 34; }
};
class SkillTab : public Tab
{
public:
SkillTab(const std::string &name, SkillListBox *listBox):
mListBox(listBox)
{
setCaption(name);
}
~SkillTab() override
{
delete mListBox;
mListBox = nullptr;
}
SkillInfo *getSelectedInfo()
{
return mListBox->getSelectedInfo();
}
private:
SkillListBox *mListBox;
};
SkillDialog::SkillDialog():
Window(_("Skills"))
{
listen(Event::AttributesChannel);
setWindowName("Skills");
setCloseButton(true);
setResizable(true);
setSaveVisible(true);
setDefaultSize(windowContainer->getWidth() - 280, 30, 275, 425);
setMinHeight(113);
setMinWidth(240);
setupWindow->registerWindowForReset(this);
mTabs = new TabbedArea();
mPointsLabel = new Label("0");
mIncreaseButton = new Button(_("Up"), "inc", this);
place(0, 0, mTabs, 5, 5);
place(0, 5, mPointsLabel, 4);
place(4, 5, mIncreaseButton);
center();
loadWindowState();
}
SkillDialog::~SkillDialog()
{
clearSkills();
}
void SkillDialog::action(const gcn::ActionEvent &event)
{
if (event.getId() == "inc")
{
auto *tab = static_cast<SkillTab*>(mTabs->getSelectedTab());
if (SkillInfo *info = tab->getSelectedInfo())
Net::getPlayerHandler()->increaseSkill(info->id);
}
else if (event.getId() == "close")
{
setVisible(false);
}
}
std::string SkillDialog::update(int id)
{
auto i = mSkills.find(id);
if (i != mSkills.end())
{
SkillInfo *info = i->second;
info->update();
return info->name;
}
return std::string();
}
void SkillDialog::update()
{
mPointsLabel->setCaption(strprintf(_("Skill points available: %d"),
PlayerInfo::getAttribute(SKILL_POINTS)));
mPointsLabel->adjustSize();
for (auto &skill : mSkills)
{
skill.second->update();
}
}
void SkillDialog::event(Event::Channel channel, const Event &event)
{
if (event.getType() == Event::UpdateAttribute)
{
if (event.getInt("id") == SKILL_POINTS)
{
update();
}
}
else if (event.getType() == Event::UpdateStat)
{
auto it = mSkills.find(event.getInt("id"));
if (it != mSkills.end())
it->second->update();
}
}
void SkillDialog::clearSkills()
{
// Fixes issues with removing tabs
if (mTabs->getSelectedTabIndex() != -1)
{
mTabs->setSelectedTab((unsigned int) 0);
while (mTabs->getSelectedTabIndex() != -1)
{
gcn::Tab *tab = mTabs->getSelectedTab();
mTabs->removeTabWithIndex(mTabs->getSelectedTabIndex());
delete tab;
}
}
delete_all(mSkills);
mSkills.clear();
}
void SkillDialog::loadSkills()
{
clearSkills();
XML::Document doc(SKILLS_FILE);
xmlNodePtr root = doc.rootNode();
int setCount = 0;
std::string setName;
ScrollArea *scroll;
SkillListBox *listbox;
SkillTab *tab;
if (!root || !xmlStrEqual(root->name, BAD_CAST "skills"))
{
logger->log("Error loading skills file: %s", SKILLS_FILE);
if (Net::getNetworkType() == ServerInfo::TMWATHENA)
{
auto *model = new SkillModel();
auto *skill = new SkillInfo;
skill->id = 1;
skill->name = "basic";
skill->setIcon(std::string());
skill->modifiable = true;
skill->visible = true;
skill->model = model;
skill->update();
model->addSkill(skill);
mSkills[1] = skill;
model->updateVisibilities();
listbox = new SkillListBox(model);
scroll = new ScrollArea(listbox);
scroll->setOpaque(false);
scroll->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
scroll->setVerticalScrollPolicy(ScrollArea::SHOW_ALWAYS);
tab = new SkillTab("Skills", listbox);
mTabs->addTab(tab, scroll);
update();
}
return;
}
for_each_xml_child_node(set, root)
{
if (xmlStrEqual(set->name, BAD_CAST "set") ||
xmlStrEqual(set->name, BAD_CAST "skill-set"))
{
setCount++;
setName = XML::getProperty(set, "name", strprintf(_("Skill Set %d"), setCount));
auto *model = new SkillModel();
for_each_xml_child_node(node, set)
{
if (xmlStrEqual(node->name, BAD_CAST "skill"))
{
int id = atoi(XML::getProperty(node, "id", "-1").c_str());
std::string name = XML::getProperty(node, "name", strprintf(_("Skill %d"), id));
std::string icon = XML::getProperty(node, "icon", "");
auto *skill = new SkillInfo;
skill->id = id;
skill->name = name;
skill->setIcon(icon);
skill->modifiable = false;
skill->visible = false;
skill->model = model;
skill->update();
model->addSkill(skill);
mSkills[id] = skill;
}
}
model->updateVisibilities();
listbox = new SkillListBox(model);
scroll = new ScrollArea(listbox);
scroll->setOpaque(false);
scroll->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
scroll->setVerticalScrollPolicy(ScrollArea::SHOW_ALWAYS);
tab = new SkillTab(setName, listbox);
mTabs->addTab(tab, scroll);
}
}
update();
}
void SkillDialog::setModifiable(int id, bool modifiable)
{
auto it = mSkills.find(id);
if (it != mSkills.end())
{
SkillInfo *info = it->second;
info->modifiable = modifiable;
info->update();
}
}
void SkillModel::updateVisibilities()
{
mVisibleSkills.clear();
for (auto &skill : mSkills)
{
if (skill->visible)
{
mVisibleSkills.push_back(skill);
}
}
}
void SkillInfo::update()
{
int baseLevel = PlayerInfo::getStatBase(id);
int effLevel = PlayerInfo::getStatEffective(id);
std::pair<int, int> exp = PlayerInfo::getStatExperience(id);
if (!modifiable && baseLevel == 0 && effLevel == 0 && exp.second == 0)
{
if (visible)
{
visible = false;
model->updateVisibilities();
}
return;
}
bool updateVisibility = !visible;
visible = true;
if (effLevel != baseLevel)
{
skillLevel = strprintf(_("Lvl: %d (%+d)"), baseLevel,
effLevel - baseLevel);
}
else
{
if (baseLevel == 0)
{
skillLevel.clear();
}
else
{
skillLevel = strprintf(_("Lvl: %d"), baseLevel);
}
}
skillLevelWidth = -1;
if (exp.second)
{
progress = exp.second != 0 ? (float) exp.first / exp.second : 0;
skillExp = strprintf("%.2f%%", progress * 100);
}
else
{
skillExp.clear();
progress = 0.0f;
}
color = Theme::getProgressColor(Theme::PROG_EXP, progress);
if (updateVisibility)
{
model->updateVisibilities();
}
}
void SkillInfo::draw(Graphics *graphics, int y, int width)
{
graphics->drawImage(icon, 1, y);
graphics->drawText(name, 34, y);
if (skillLevelWidth < 0)
{
// Add one for padding
skillLevelWidth = graphics->getFont()->getWidth(skillLevel) + 1;
}
graphics->drawText(skillLevel, width - skillLevelWidth, y);
if (!skillExp.empty())
{
gcn::Rectangle rect(33, y + 15, width - 33, 17);
ProgressBar::render(graphics, rect, color, progress, skillExp);
}
}
|