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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
|
/*
* The ManaPlus Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2010 The Mana Developers
* Copyright (C) 2011-2014 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/windows/statuswindow.h"
#include "configuration.h"
#include "equipment.h"
#include "gamemodifiers.h"
#include "inventory.h"
#include "item.h"
#include "settings.h"
#include "units.h"
#include "gui/windows/chatwindow.h"
#include "being/attributes.h"
#include "being/localplayer.h"
#include "being/playerinfo.h"
#include "gui/windows/equipmentwindow.h"
#include "gui/windows/setupwindow.h"
#include "gui/widgets/button.h"
#include "gui/widgets/containerplacer.h"
#include "gui/widgets/label.h"
#include "gui/widgets/layouthelper.h"
#include "gui/widgets/layouttype.h"
#include "gui/widgets/progressbar.h"
#include "gui/widgets/scrollarea.h"
#include "gui/widgets/statuswindowattrs.h"
#include "gui/widgets/vertcontainer.h"
#include "gui/widgets/windowcontainer.h"
#include "net/net.h"
#include "net/playerhandler.h"
#include "net/gamehandler.h"
#include "utils/delete2.h"
#include "utils/gettext.h"
#include <SDL_timer.h>
#include "debug.h"
StatusWindow *statusWindow = nullptr;
StatusWindow::StatusWindow() :
Window(localPlayer ? localPlayer->getName() :
"?", false, nullptr, "status.xml"),
ActionListener(),
AttributeListener(),
StatListener(),
// TRANSLATORS: status window label
mLvlLabel(new Label(this, strprintf(_("Level: %d"), 0))),
// TRANSLATORS: status window label
mMoneyLabel(new Label(this, strprintf(_("Money: %s"), ""))),
// TRANSLATORS: status window label
mHpLabel(new Label(this, _("HP:"))),
mMpLabel(nullptr),
// TRANSLATORS: status window label
mXpLabel(new Label(this, _("Exp:"))),
mHpBar(nullptr),
mMpBar(nullptr),
mXpBar(nullptr),
mJobLvlLabel(nullptr),
mJobLabel(nullptr),
mJobBar(nullptr),
mAttrCont(new VertContainer(this, 32)),
mAttrScroll(new ScrollArea(this, mAttrCont, false)),
mDAttrCont(new VertContainer(this, 32)),
mDAttrScroll(new ScrollArea(this, mDAttrCont, false)),
mCharacterPointsLabel(new Label(this, "C")),
mCorrectionPointsLabel(nullptr),
// TRANSLATORS: status window button
mCopyButton(new Button(this, _("Copy to chat"), "copy", this)),
mAttrs()
{
setWindowName("Status");
if (setupWindow)
setupWindow->registerWindowForReset(this);
setResizable(true);
setCloseButton(true);
setSaveVisible(true);
setStickyButtonLock(true);
setDefaultSize((windowContainer->getWidth() - 480) / 2,
(windowContainer->getHeight() - 500) / 2, 480, 500);
if (localPlayer && !localPlayer->getRaceName().empty())
{
setCaption(strprintf("%s (%s)", localPlayer->getName().c_str(),
localPlayer->getRaceName().c_str()));
}
int max = PlayerInfo::getAttribute(Attributes::MAX_HP);
if (!max)
max = 1;
mHpBar = new ProgressBar(this, static_cast<float>(PlayerInfo::getAttribute(
Attributes::HP)) / static_cast<float>(max), 80, 0, Theme::PROG_HP,
"hpprogressbar.xml", "hpprogressbar_fill.xml");
mHpBar->setColor(getThemeColor(Theme::HP_BAR),
getThemeColor(Theme::HP_BAR_OUTLINE));
max = PlayerInfo::getAttribute(Attributes::EXP_NEEDED);
mXpBar = new ProgressBar(this, max ?
static_cast<float>(PlayerInfo::getAttribute(Attributes::EXP))
/ static_cast<float>(max) : static_cast<float>(0), 80, 0,
Theme::PROG_EXP, "xpprogressbar.xml", "xpprogressbar_fill.xml");
mXpBar->setColor(getThemeColor(Theme::XP_BAR),
getThemeColor(Theme::XP_BAR_OUTLINE));
const bool magicBar = gameHandler->canUseMagicBar();
const int job = Net::getPlayerHandler()->getJobLocation()
&& serverConfig.getValueBool("showJob", true);
if (magicBar)
{
max = PlayerInfo::getAttribute(Attributes::MAX_MP);
// TRANSLATORS: status window label
mMpLabel = new Label(this, _("MP:"));
const bool useMagic = Net::getPlayerHandler()->canUseMagic();
mMpBar = new ProgressBar(this, max ? static_cast<float>(
PlayerInfo::getAttribute(Attributes::MAX_MP))
/ static_cast<float>(max) : static_cast<float>(0),
80, 0, useMagic ? Theme::PROG_MP : Theme::PROG_NO_MP,
useMagic ? "mpprogressbar.xml" : "nompprogressbar.xml",
useMagic ? "mpprogressbar_fill.xml" : "nompprogressbar_fill.xml");
if (useMagic)
{
mMpBar->setColor(getThemeColor(Theme::MP_BAR),
getThemeColor(Theme::MP_BAR_OUTLINE));
}
else
{
mMpBar->setColor(getThemeColor(Theme::NO_MP_BAR),
getThemeColor(Theme::NO_MP_BAR_OUTLINE));
}
}
else
{
mMpLabel = nullptr;
mMpBar = nullptr;
}
place(0, 0, mLvlLabel, 3);
place(0, 1, mHpLabel).setPadding(3);
place(1, 1, mHpBar, 4);
place(5, 1, mXpLabel).setPadding(3);
place(6, 1, mXpBar, 5);
if (magicBar)
{
place(0, 2, mMpLabel).setPadding(3);
// 5, 2 and 6, 2 Job Progress Bar
if (job)
place(1, 2, mMpBar, 4);
else
place(1, 2, mMpBar, 10);
}
if (job)
{
// TRANSLATORS: status window label
mJobLvlLabel = new Label(this, strprintf(_("Job: %d"), 0));
// TRANSLATORS: status window label
mJobLabel = new Label(this, _("Job:"));
mJobBar = new ProgressBar(this, 0.0F, 80, 0, Theme::PROG_JOB,
"jobprogressbar.xml", "jobprogressbar_fill.xml");
mJobBar->setColor(getThemeColor(Theme::JOB_BAR),
getThemeColor(Theme::JOB_BAR_OUTLINE));
place(3, 0, mJobLvlLabel, 3);
place(5, 2, mJobLabel).setPadding(3);
place(6, 2, mJobBar, 5);
place(6, 0, mMoneyLabel, 3);
}
else
{
mJobLvlLabel = nullptr;
mJobLabel = nullptr;
mJobBar = nullptr;
place(3, 0, mMoneyLabel, 3);
}
// ----------------------
// Stats Part
// ----------------------
mAttrScroll->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
mAttrScroll->setVerticalScrollPolicy(ScrollArea::SHOW_AUTO);
place(0, 3, mAttrScroll, 5, 3);
mDAttrScroll->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
mDAttrScroll->setVerticalScrollPolicy(ScrollArea::SHOW_AUTO);
place(6, 3, mDAttrScroll, 5, 3);
getLayout().setRowHeight(3, LayoutType::SET);
place(0, 6, mCharacterPointsLabel, 5);
place(0, 5, mCopyButton);
if (Net::getPlayerHandler()->canCorrectAttributes())
{
mCorrectionPointsLabel = new Label(this, "C");
place(0, 7, mCorrectionPointsLabel, 5);
}
loadWindowState();
enableVisibleSound(true);
// Update bars
updateHPBar(mHpBar, true);
if (magicBar)
updateMPBar(mMpBar, true);
updateXPBar(mXpBar, false);
// TRANSLATORS: status window label
mMoneyLabel->setCaption(strprintf(_("Money: %s"), Units::formatCurrency(
PlayerInfo::getAttribute(Attributes::MONEY)).c_str()));
mMoneyLabel->adjustSize();
// TRANSLATORS: status window label
mCharacterPointsLabel->setCaption(strprintf(_("Character points: %d"),
PlayerInfo::getAttribute(Attributes::CHAR_POINTS)));
mCharacterPointsLabel->adjustSize();
if (localPlayer && localPlayer->isGM())
{
// TRANSLATORS: status window label
mLvlLabel->setCaption(strprintf(_("Level: %d (GM %d)"),
PlayerInfo::getAttribute(Attributes::LEVEL),
localPlayer->getGMLevel()));
}
else
{
// TRANSLATORS: status window label
mLvlLabel->setCaption(strprintf(_("Level: %d"),
PlayerInfo::getAttribute(Attributes::LEVEL)));
}
mLvlLabel->adjustSize();
}
void StatusWindow::statChanged(const int id,
const int oldVal1,
const int oldVal2 A_UNUSED)
{
static bool blocked = false;
if (blocked)
return;
if (id == Net::getPlayerHandler()->getJobLocation())
{
if (mJobLvlLabel)
{
int lvl = PlayerInfo::getStatBase(id);
const int oldExp = oldVal1;
const std::pair<int, int> exp = PlayerInfo::getStatExperience(id);
if (!lvl)
{
// possible server broken and don't send job level,
// then we fixing it :)
if (exp.second < 20000)
{
lvl = 0;
}
else
{
lvl = (exp.second - 20000) / 150;
blocked = true;
PlayerInfo::setStatBase(id, lvl);
blocked = false;
}
}
if (exp.first < oldExp && exp.second >= 20000)
{ // possible job level up. but server broken and don't send
// new job exp limit, we fixing it
lvl ++;
blocked = true;
PlayerInfo::setStatExperience(
id, exp.first, 20000 + lvl * 150);
PlayerInfo::setStatBase(id, lvl);
blocked = false;
}
// TRANSLATORS: status window label
mJobLvlLabel->setCaption(strprintf(_("Job: %d"), lvl));
mJobLvlLabel->adjustSize();
updateProgressBar(mJobBar, id, false);
}
}
else
{
updateMPBar(mMpBar, true);
const Attrs::const_iterator it = mAttrs.find(id);
if (it != mAttrs.end() && it->second)
it->second->update();
}
}
void StatusWindow::attributeChanged(const int id,
const int oldVal A_UNUSED,
const int newVal)
{
switch (id)
{
case Attributes::HP:
case Attributes::MAX_HP:
updateHPBar(mHpBar, true);
break;
case Attributes::MP:
case Attributes::MAX_MP:
updateMPBar(mMpBar, true);
break;
case Attributes::EXP:
case Attributes::EXP_NEEDED:
updateXPBar(mXpBar, false);
break;
case Attributes::MONEY:
// TRANSLATORS: status window label
mMoneyLabel->setCaption(strprintf(_("Money: %s"),
Units::formatCurrency(newVal).c_str()));
mMoneyLabel->adjustSize();
break;
case Attributes::CHAR_POINTS:
mCharacterPointsLabel->setCaption(strprintf(
// TRANSLATORS: status window label
_("Character points: %d"), newVal));
mCharacterPointsLabel->adjustSize();
// Update all attributes
for (Attrs::const_iterator it = mAttrs.begin();
it != mAttrs.end(); ++it)
{
if (it->second)
it->second->update();
}
break;
case Attributes::CORR_POINTS:
mCorrectionPointsLabel->setCaption(strprintf(
// TRANSLATORS: status window label
_("Correction points: %d"), newVal));
mCorrectionPointsLabel->adjustSize();
// Update all attributes
for (Attrs::const_iterator it = mAttrs.begin();
it != mAttrs.end(); ++it)
{
if (it->second)
it->second->update();
}
break;
case Attributes::LEVEL:
// TRANSLATORS: status window label
mLvlLabel->setCaption(strprintf(_("Level: %d"), newVal));
mLvlLabel->adjustSize();
break;
default:
break;
}
}
void StatusWindow::setPointsNeeded(const int id, const int needed)
{
const Attrs::const_iterator it = mAttrs.find(id);
if (it != mAttrs.end())
{
AttrDisplay *const disp = it->second;
if (disp && disp->getType() == AttrDisplay::CHANGEABLE)
static_cast<ChangeDisplay*>(disp)->setPointsNeeded(needed);
}
}
void StatusWindow::addAttribute(const int id, const std::string &restrict name,
const std::string &restrict shortName,
const bool modifiable)
{
AttrDisplay *disp;
if (modifiable)
{
disp = new ChangeDisplay(this, id, name, shortName);
disp->update();
mAttrCont->add1(disp);
}
else
{
disp = new DerDisplay(this, id, name, shortName);
disp->update();
mDAttrCont->add1(disp);
}
mAttrs[id] = disp;
}
void StatusWindow::clearAttributes()
{
mAttrCont->clear();
mDAttrCont->clear();
FOR_EACH (Attrs::iterator, it, mAttrs)
delete (*it).second;
mAttrs.clear();
}
void StatusWindow::updateHPBar(ProgressBar *const bar, const bool showMax)
{
if (!bar)
return;
const int hp = PlayerInfo::getAttribute(Attributes::HP);
const int maxHp = PlayerInfo::getAttribute(Attributes::MAX_HP);
if (showMax)
bar->setText(toString(hp).append("/").append(toString(maxHp)));
else
bar->setText(toString(hp));
float prog = 1.0;
if (maxHp > 0)
prog = static_cast<float>(hp) / static_cast<float>(maxHp);
bar->setProgress(prog);
}
void StatusWindow::updateMPBar(ProgressBar *const bar,
const bool showMax) const
{
if (!bar)
return;
const int mp = PlayerInfo::getAttribute(Attributes::MP);
const int maxMp = PlayerInfo::getAttribute(Attributes::MAX_MP);
if (showMax)
bar->setText(toString(mp).append("/").append(toString(maxMp)));
else
bar->setText(toString(mp));
float prog = 1.0F;
if (maxMp > 0)
prog = static_cast<float>(mp) / static_cast<float>(maxMp);
if (Net::getPlayerHandler()->canUseMagic())
{
bar->setColor(getThemeColor(Theme::MP_BAR),
getThemeColor(Theme::MP_BAR_OUTLINE));
bar->setProgressPalette(Theme::PROG_MP);
}
else
{
bar->setColor(getThemeColor(Theme::NO_MP_BAR),
getThemeColor(Theme::NO_MP_BAR_OUTLINE));
bar->setProgressPalette(Theme::PROG_NO_MP);
}
bar->setProgress(prog);
}
void StatusWindow::updateProgressBar(ProgressBar *const bar, const int value,
const int max, const bool percent)
{
if (!bar)
return;
if (max == 0)
{
// TRANSLATORS: status bar label
bar->setText(_("Max"));
bar->setProgress(1);
bar->setText(toString(value));
}
else
{
const float progress = static_cast<float>(value)
/ static_cast<float>(max);
if (percent)
{
bar->setText(strprintf("%2.5f%%",
static_cast<double>(100 * progress)));
}
else
{
bar->setText(toString(value).append("/").append(toString(max)));
}
bar->setProgress(progress);
}
}
void StatusWindow::updateXPBar(ProgressBar *const bar, const bool percent)
{
if (!bar)
return;
updateProgressBar(bar, PlayerInfo::getAttribute(Attributes::EXP),
PlayerInfo::getAttribute(Attributes::EXP_NEEDED), percent);
}
void StatusWindow::updateJobBar(ProgressBar *const bar, const bool percent)
{
if (!bar)
return;
const std::pair<int, int> exp = PlayerInfo::getStatExperience(
Net::getPlayerHandler()->getJobLocation());
updateProgressBar(bar, exp.first, exp.second, percent);
}
void StatusWindow::updateProgressBar(ProgressBar *const bar, const int id,
const bool percent)
{
const std::pair<int, int> exp = PlayerInfo::getStatExperience(id);
updateProgressBar(bar, exp.first, exp.second, percent);
}
void StatusWindow::updateWeightBar(ProgressBar *const bar)
{
if (!bar)
return;
if (PlayerInfo::getAttribute(Attributes::MAX_WEIGHT) == 0)
{
// TRANSLATORS: status bar label
bar->setText(_("Max"));
bar->setProgress(1.0);
}
else
{
const int totalWeight = PlayerInfo::getAttribute(
Attributes::TOTAL_WEIGHT);
const int maxWeight = PlayerInfo::getAttribute(Attributes::MAX_WEIGHT);
float progress = 1.0F;
if (maxWeight)
{
progress = static_cast<float>(totalWeight)
/ static_cast<float>(maxWeight);
}
bar->setText(strprintf("%s/%s", Units::formatWeight(
totalWeight).c_str(), Units::formatWeight(maxWeight).c_str()));
bar->setProgress(progress);
}
}
void StatusWindow::updateMoneyBar(ProgressBar *const bar)
{
if (!bar)
return;
const int money = PlayerInfo::getAttribute(Attributes::MONEY);
bar->setText(Units::formatCurrency(money));
if (money > 0)
{
const float progress = static_cast<float>(money)
/ static_cast<float>(1000000000);
bar->setProgress(progress);
}
else
{
bar->setProgress(1.0);
}
}
void StatusWindow::updateArrowsBar(ProgressBar *const bar)
{
if (!bar || !equipmentWindow)
return;
const Item *const item = equipmentWindow->getEquipment(
Equipment::EQUIP_PROJECTILE_SLOT);
if (item && item->getQuantity() > 0)
bar->setText(toString(item->getQuantity()));
else
bar->setText("0");
}
void StatusWindow::updateInvSlotsBar(ProgressBar *const bar)
{
if (!bar)
return;
const Inventory *const inv = PlayerInfo::getInventory();
if (!inv)
return;
const int usedSlots = inv->getNumberOfSlotsUsed();
const int maxSlots = inv->getSize();
if (maxSlots)
{
bar->setProgress(static_cast<float>(usedSlots)
/ static_cast<float>(maxSlots));
}
bar->setText(strprintf("%d", usedSlots));
}
std::string StatusWindow::translateLetter(const char *const letters)
{
char buf[2];
char *const str = gettext(letters);
if (strlen(str) != 3)
return letters;
buf[0] = str[1];
buf[1] = 0;
return std::string(buf);
}
std::string StatusWindow::translateLetter2(const std::string &letters)
{
if (letters.size() < 5)
return "";
return std::string(gettext(letters.substr(1, 1).c_str()));
}
void StatusWindow::updateStatusBar(ProgressBar *const bar,
const bool percent A_UNUSED) const
{
bar->setText(translateLetter2(GameModifiers::getMoveTypeString())
.append(translateLetter2(GameModifiers::getCrazyMoveTypeString()))
.append(translateLetter2(GameModifiers::getMoveToTargetTypeString()))
.append(translateLetter2(GameModifiers::getFollowModeString()))
.append(" ").append(translateLetter2(
GameModifiers::getAttackWeaponTypeString()))
.append(translateLetter2(GameModifiers::getAttackTypeString()))
.append(translateLetter2(GameModifiers::getMagicAttackTypeString()))
.append(translateLetter2(GameModifiers::getPvpAttackTypeString()))
.append(" ").append(translateLetter2(
GameModifiers::getQuickDropCounterString()))
.append(translateLetter2(GameModifiers::getPickUpTypeString()))
.append(" ").append(translateLetter2(
GameModifiers::getMapDrawTypeString()))
.append(" ").append(translateLetter2(
GameModifiers::getImitationModeString()))
.append(translateLetter2(GameModifiers::getCameraModeString()))
.append(translateLetter2(GameModifiers::getAwayModeString())));
bar->setProgress(50);
if (settings.disableGameModifiers)
bar->setBackgroundColor(getThemeColor(Theme::STATUSBAR_ON));
else
bar->setBackgroundColor(getThemeColor(Theme::STATUSBAR_OFF));
}
void StatusWindow::action(const ActionEvent &event)
{
if (!chatWindow)
return;
if (event.getId() == "copy")
{
Attrs::const_iterator it = mAttrs.begin();
const Attrs::const_iterator it_end = mAttrs.end();
std::string str;
while (it != it_end)
{
const ChangeDisplay *const attr = dynamic_cast<ChangeDisplay*>(
(*it).second);
if (attr)
{
str.append(strprintf("%s:%s ", attr->getShortName().c_str(),
attr->getValue().c_str()));
}
++ it;
}
chatWindow->addInputText(str);
}
}
AttrDisplay::AttrDisplay(const Widget2 *const widget,
const int id, const std::string &restrict name,
const std::string &restrict shortName) :
Container(widget),
mId(id),
mName(name),
mShortName(shortName),
mLayout(new LayoutHelper(this)),
mLabel(new Label(this, name)),
mValue(new Label(this, "1 "))
{
setSize(100, 32);
mLabel->setAlignment(Graphics::CENTER);
mValue->setAlignment(Graphics::CENTER);
}
AttrDisplay::~AttrDisplay()
{
delete2(mLayout);
}
std::string AttrDisplay::update()
{
const int base = PlayerInfo::getStatBase(mId);
const int bonus = PlayerInfo::getStatMod(mId);
std::string value = toString(base + bonus);
if (bonus)
value.append(strprintf("=%d%+d", base, bonus));
mValue->setCaption(value);
return mName;
}
DerDisplay::DerDisplay(const Widget2 *const widget,
const int id, const std::string &restrict name,
const std::string &restrict shortName) :
AttrDisplay(widget, id, name, shortName)
{
ContainerPlacer place = mLayout->getPlacer(0, 0);
place(0, 0, mLabel, 3);
place(3, 0, mValue, 2);
}
ChangeDisplay::ChangeDisplay(const Widget2 *const widget,
const int id, const std::string &restrict name,
const std::string &restrict shortName) :
AttrDisplay(widget, id, name, shortName),
ActionListener(),
mNeeded(1),
// TRANSLATORS: status window label
mPoints(new Label(this, _("Max"))),
mDec(nullptr),
// TRANSLATORS: status window label (plus sign)
mInc(new Button(this, _("+"), "inc", this))
{
// Do the layout
ContainerPlacer place = mLayout->getPlacer(0, 0);
place(0, 0, mLabel, 3);
place(4, 0, mValue, 2);
place(6, 0, mInc);
place(7, 0, mPoints);
if (Net::getPlayerHandler()->canCorrectAttributes())
{
// TRANSLATORS: status window label (minus sign)
mDec = new Button(this, _("-"), "dec", this);
mDec->setWidth(mInc->getWidth());
place(3, 0, mDec);
}
}
std::string ChangeDisplay::update()
{
if (mNeeded > 0)
{
mPoints->setCaption(toString(mNeeded));
}
else
{
// TRANSLATORS: status bar label
mPoints->setCaption(_("Max"));
}
if (mDec)
mDec->setEnabled(PlayerInfo::getAttribute(Attributes::CORR_POINTS));
mInc->setEnabled(PlayerInfo::getAttribute(Attributes::CHAR_POINTS)
>= mNeeded && mNeeded > 0);
return AttrDisplay::update();
}
void ChangeDisplay::setPointsNeeded(const int needed)
{
mNeeded = needed;
update();
}
void ChangeDisplay::action(const ActionEvent &event)
{
if (Net::getPlayerHandler()->canCorrectAttributes() &&
event.getSource() == mDec)
{
const int newcorrpoints = PlayerInfo::getAttribute(
Attributes::CORR_POINTS);
PlayerInfo::setAttribute(Attributes::CORR_POINTS, newcorrpoints - 1);
const int newpoints = PlayerInfo::getAttribute(
Attributes::CHAR_POINTS) + 1;
PlayerInfo::setAttribute(Attributes::CHAR_POINTS, newpoints);
const int newbase = PlayerInfo::getStatBase(mId) - 1;
PlayerInfo::setStatBase(mId, newbase);
Net::getPlayerHandler()->decreaseAttribute(mId);
}
else if (event.getSource() == mInc)
{
int cnt = 1;
if (config.getBoolValue("quickStats"))
{
cnt = mInc->getClickCount();
if (cnt > 10)
cnt = 10;
}
const int newpoints = PlayerInfo::getAttribute(
Attributes::CHAR_POINTS) - cnt;
PlayerInfo::setAttribute(Attributes::CHAR_POINTS, newpoints);
const int newbase = PlayerInfo::getStatBase(mId) + cnt;
PlayerInfo::setStatBase(mId, newbase);
for (unsigned f = 0; f < mInc->getClickCount(); f ++)
{
Net::getPlayerHandler()->increaseAttribute(mId);
if (cnt != 1)
SDL_Delay(100);
}
}
}
|