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
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
|
/*
* The ManaPlus Client
* Copyright (C) 2004-2009 The Mana World Development Team
* Copyright (C) 2009-2010 The Mana Developers
* Copyright (C) 2011-2015 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/npcdialog.h"
#include "actormanager.h"
#include "configuration.h"
#include "inventory.h"
#include "item.h"
#include "settings.h"
#include "soundconsts.h"
#include "soundmanager.h"
#include "being/being.h"
#include "enums/net/packettypes.h"
#include "gui/gui.h"
#include "gui/viewport.h"
#include "gui/fonts/font.h"
#include "gui/popups/popupmenu.h"
#include "gui/windows/inventorywindow.h"
#include "gui/widgets/browserbox.h"
#include "gui/widgets/button.h"
#include "gui/widgets/inttextfield.h"
#include "gui/widgets/itemcontainer.h"
#include "gui/widgets/itemlinkhandler.h"
#include "gui/widgets/layout.h"
#include "gui/widgets/layouttype.h"
#include "gui/widgets/extendedlistbox.h"
#include "gui/widgets/playerbox.h"
#include "gui/widgets/scrollarea.h"
#include "resources/resourcemanager.h"
#include "resources/db/avatardb.h"
#include "resources/db/npcdb.h"
#include "net/npchandler.h"
#include "net/packetlimiter.h"
#include "utils/copynpaste.h"
#include "utils/delete2.h"
#include "utils/gettext.h"
#include "debug.h"
// TRANSLATORS: npc dialog button
#define CAPTION_WAITING _("Stop waiting")
// TRANSLATORS: npc dialog button
#define CAPTION_NEXT _("Next")
// TRANSLATORS: npc dialog button
#define CAPTION_CLOSE _("Close")
// TRANSLATORS: npc dialog button
#define CAPTION_SUBMIT _("Submit")
NpcDialog::DialogList NpcDialog::instances;
NpcDialogs NpcDialog::mNpcDialogs;
typedef std::vector<Image *>::iterator ImageVectorIter;
NpcDialog::NpcDialog(const int npcId) :
// TRANSLATORS: npc dialog name
Window(_("NPC"), false, nullptr, "npc.xml"),
ActionListener(),
mNpcId(npcId),
mDefaultInt(0),
mDefaultString(),
mTextBox(new BrowserBox(this, BrowserBox::AUTO_WRAP, true,
"browserbox.xml")),
mScrollArea(new ScrollArea(this, mTextBox,
getOptionBool("showtextbackground"), "npc_textbackground.xml")),
mText(),
mNewText(),
mItemList(new ExtendedListBox(this, this, "extendedlistbox.xml")),
mListScrollArea(new ScrollArea(this, mItemList,
getOptionBool("showlistbackground"), "npc_listbackground.xml")),
mItems(),
mImages(),
mItemLinkHandler(new ItemLinkHandler),
mTextField(new TextField(this, "")),
mIntField(new IntTextField(this)),
// TRANSLATORS: npc dialog button
mPlusButton(new Button(this, _("+"), "inc", this)),
// TRANSLATORS: npc dialog button
mMinusButton(new Button(this, _("-"), "dec", this)),
// TRANSLATORS: npc dialog button
mClearButton(new Button(this, _("Clear"), "clear", this)),
mButton(new Button(this, "", "ok", this)),
// TRANSLATORS: npc dialog button
mButton2(new Button(this, _("Close"), "close", this)),
// TRANSLATORS: npc dialog button
mButton3(new Button(this, _("Add"), "add", this)),
// TRANSLATORS: npc dialog button
mResetButton(new Button(this, _("Reset"), "reset", this)),
mInventory(new Inventory(InventoryType::NPC, 1)),
mItemContainer(new ItemContainer(this, mInventory)),
mItemScrollArea(new ScrollArea(this, mItemContainer,
getOptionBool("showitemsbackground"), "npc_listbackground.xml")),
mInputState(NPC_INPUT_NONE),
mActionState(NPC_ACTION_WAIT),
mPlayerBox(new PlayerBox(nullptr)),
mAvatarBeing(nullptr),
mLastNextTime(0),
mCameraMode(-1),
mCameraX(0),
mCameraY(0),
mShowAvatar(false),
mLogInteraction(config.getBoolValue("logNpcInGui"))
{
mItemList->postInit();
// Basic Window Setup
setWindowName("NpcText");
setResizable(true);
setFocusable(true);
setStickyButtonLock(true);
setMinWidth(200);
setMinHeight(150);
setDefaultSize(300, 578, ImageRect::LOWER_LEFT);
mPlayerBox->setWidth(70);
mPlayerBox->setHeight(100);
// Setup output text box
mTextBox->setOpaque(false);
mTextBox->setMaxRow(config.getIntValue("ChatLogLength"));
mTextBox->setLinkHandler(mItemLinkHandler);
mTextBox->setProcessVars(true);
mTextBox->setFont(gui->getNpcFont());
mTextBox->setEnableKeys(true);
mTextBox->setEnableTabs(true);
mScrollArea->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
mScrollArea->setVerticalScrollPolicy(ScrollArea::SHOW_ALWAYS);
// Setup listbox
mItemList->setWrappingEnabled(true);
mItemList->setActionEventId("ok");
mItemList->addActionListener(this);
mItemList->setDistributeMousePressed(false);
mItemList->setFont(gui->getNpcFont());
if (gui->getNpcFont()->getHeight() < 20)
mItemList->setRowHeight(20);
else
mItemList->setRowHeight(gui->getNpcFont()->getHeight());
setContentSize(260, 175);
mListScrollArea->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
mItemScrollArea->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
mItemList->setVisible(true);
mTextField->setVisible(true);
mIntField->setVisible(true);
const Font *const fnt = mButton->getFont();
int width = std::max(fnt->getWidth(CAPTION_WAITING),
fnt->getWidth(CAPTION_NEXT));
width = std::max(width, fnt->getWidth(CAPTION_CLOSE));
width = std::max(width, fnt->getWidth(CAPTION_SUBMIT));
mButton->setWidth(8 + width);
// Place widgets
buildLayout();
center();
loadWindowState();
instances.push_back(this);
}
void NpcDialog::postInit()
{
setVisible(true);
requestFocus();
enableVisibleSound(true);
soundManager.playGuiSound(SOUND_SHOW_WINDOW);
if (actorManager)
{
const Being *const being = actorManager->findBeing(mNpcId);
if (being)
{
showAvatar(NPCDB::getAvatarFor(being->getSubType()));
setCaption(being->getName());
}
}
config.addListener("logNpcInGui", this);
}
NpcDialog::~NpcDialog()
{
config.removeListeners(this);
CHECKLISTENERS
clearLayout();
if (mPlayerBox)
{
delete mPlayerBox->getBeing();
delete mPlayerBox;
}
delete2(mTextBox);
delete2(mClearButton);
delete2(mButton);
delete2(mButton2);
delete2(mButton3);
delete2(mScrollArea);
delete2(mItemList);
delete2(mTextField);
delete2(mIntField);
delete2(mResetButton);
delete2(mPlusButton);
delete2(mMinusButton);
delete2(mItemLinkHandler);
delete2(mItemContainer);
delete2(mInventory);
delete2(mItemScrollArea);
delete2(mListScrollArea);
FOR_EACH (ImageVectorIter, it, mImages)
{
if (*it)
(*it)->decRef();
}
mImages.clear();
instances.remove(this);
}
void NpcDialog::addText(const std::string &text, const bool save)
{
if (save || mLogInteraction)
{
if (mText.size() > 5000)
mText.clear();
mNewText.append(text);
mTextBox->addRow(text);
}
mScrollArea->setVerticalScrollAmount(mScrollArea->getVerticalMaxScroll());
mActionState = NPC_ACTION_WAIT;
buildLayout();
}
void NpcDialog::showNextButton()
{
mActionState = NPC_ACTION_NEXT;
buildLayout();
}
void NpcDialog::showCloseButton()
{
mActionState = NPC_ACTION_CLOSE;
buildLayout();
}
void NpcDialog::action(const ActionEvent &event)
{
const std::string &eventId = event.getId();
if (eventId == "ok")
{
if (mActionState == NPC_ACTION_NEXT)
{
if (!PacketLimiter::limitPackets(PACKET_NPC_NEXT))
return;
nextDialog();
addText(std::string(), false);
}
else if (mActionState == NPC_ACTION_CLOSE
|| mActionState == NPC_ACTION_WAIT)
{
closeDialog();
}
else if (mActionState == NPC_ACTION_INPUT)
{
std::string printText; // Text that will get printed
// in the textbox
switch (mInputState)
{
case NPC_INPUT_LIST:
{
if (gui)
gui->resetClickCount();
const int selectedIndex = mItemList->getSelected();
if (selectedIndex >= static_cast<int>(mItems.size())
|| selectedIndex < 0
|| !PacketLimiter::limitPackets(PACKET_NPC_INPUT))
{
return;
}
unsigned char choice = static_cast<unsigned char>(
selectedIndex + 1);
printText = mItems[selectedIndex];
npcHandler->listInput(mNpcId, choice);
break;
}
case NPC_INPUT_STRING:
{
if (!PacketLimiter::limitPackets(PACKET_NPC_INPUT))
return;
printText = mTextField->getText();
npcHandler->stringInput(mNpcId, printText);
break;
}
case NPC_INPUT_INTEGER:
{
if (!PacketLimiter::limitPackets(PACKET_NPC_INPUT))
return;
printText = strprintf("%d", mIntField->getValue());
npcHandler->integerInput(
mNpcId, mIntField->getValue());
break;
}
case NPC_INPUT_ITEM:
{
if (!PacketLimiter::limitPackets(PACKET_NPC_INPUT))
return;
std::string str;
const int sz = mInventory->getNumberOfSlotsUsed();
if (sz == 0)
{
str = "0,0";
}
else
{
const Item *item = mInventory->getItem(0);
if (item)
{
str = strprintf("%d,%d", item->getId(),
item->getColor());
}
else
{
str = "0,0";
}
for (int f = 1; f < sz; f ++)
{
str.append(";");
item = mInventory->getItem(f);
if (item)
{
str.append(strprintf("%d,%d", item->getId(),
item->getColor()));
}
else
{
str.append("0,0");
}
}
}
// need send selected item
npcHandler->stringInput(mNpcId, str);
mInventory->clear();
break;
}
case NPC_INPUT_NONE:
default:
break;
}
if (mInputState != NPC_INPUT_ITEM)
{
// addText will auto remove the input layout
addText(strprintf("> \"%s\"", printText.c_str()), false);
}
mNewText.clear();
}
if (!mLogInteraction)
mTextBox->clearRows();
}
else if (eventId == "reset")
{
switch (mInputState)
{
case NPC_INPUT_STRING:
mTextField->setText(mDefaultString);
break;
case NPC_INPUT_INTEGER:
mIntField->setValue(mDefaultInt);
break;
case NPC_INPUT_ITEM:
mInventory->clear();
break;
case NPC_INPUT_NONE:
case NPC_INPUT_LIST:
default:
break;
}
}
else if (eventId == "inc")
{
mIntField->setValue(mIntField->getValue() + 1);
}
else if (eventId == "dec")
{
mIntField->setValue(mIntField->getValue() - 1);
}
else if (eventId == "clear")
{
switch (mInputState)
{
case NPC_INPUT_ITEM:
mInventory->clear();
break;
case NPC_INPUT_STRING:
case NPC_INPUT_INTEGER:
case NPC_INPUT_LIST:
case NPC_INPUT_NONE:
default:
clearRows();
break;
}
}
else if (eventId == "close")
{
if (mActionState == NPC_ACTION_INPUT)
{
switch (mInputState)
{
case NPC_INPUT_ITEM:
npcHandler->stringInput(mNpcId, "0,0");
break;
case NPC_INPUT_STRING:
case NPC_INPUT_INTEGER:
case NPC_INPUT_NONE:
case NPC_INPUT_LIST:
default:
npcHandler->listInput(mNpcId, 255);
break;
}
closeDialog();
}
}
else if (eventId == "add")
{
if (inventoryWindow)
{
const Item *const item = inventoryWindow->getSelectedItem();
if (item)
{
mInventory->addItem(item->getId(),
item->getType(),
1,
1,
item->getColor(),
item->getIdentified(),
item->getDamaged(),
item->getFavorite(),
Equipm_false,
false);
}
}
}
}
void NpcDialog::nextDialog()
{
npcHandler->nextDialog(mNpcId);
}
void NpcDialog::closeDialog()
{
restoreCamera();
npcHandler->closeDialog(mNpcId);
}
int NpcDialog::getNumberOfElements()
{
return static_cast<int>(mItems.size());
}
std::string NpcDialog::getElementAt(int i)
{
return mItems[i];
}
const Image *NpcDialog::getImageAt(int i)
{
return mImages[i];
}
void NpcDialog::choiceRequest()
{
mItems.clear();
FOR_EACH (ImageVectorIter, it, mImages)
{
if (*it)
(*it)->decRef();
}
mImages.clear();
mActionState = NPC_ACTION_INPUT;
mInputState = NPC_INPUT_LIST;
buildLayout();
}
void NpcDialog::addChoice(const std::string &choice)
{
mItems.push_back(choice);
mImages.push_back(nullptr);
}
void NpcDialog::parseListItems(const std::string &itemString)
{
std::istringstream iss(itemString);
ResourceManager *const resman = ResourceManager::getInstance();
std::string tmp;
const std::string path = paths.getStringValue("guiIcons");
while (getline(iss, tmp, ':'))
{
if (tmp.empty())
continue;
const size_t pos = tmp.find("|");
if (pos == std::string::npos)
{
mItems.push_back(tmp);
mImages.push_back(nullptr);
}
else
{
mItems.push_back(tmp.substr(pos + 1));
Image *const img = resman->getImage(std::string(
path).append(tmp.substr(0, pos)).append(".png"));
mImages.push_back(img);
}
}
if (!mItems.empty())
{
mItemList->setSelected(0);
mItemList->requestFocus();
}
else
{
mItemList->setSelected(-1);
}
}
void NpcDialog::refocus()
{
if (!mItems.empty())
mItemList->refocus();
}
void NpcDialog::textRequest(const std::string &defaultText)
{
mActionState = NPC_ACTION_INPUT;
mInputState = NPC_INPUT_STRING;
mDefaultString = defaultText;
mTextField->setText(defaultText);
buildLayout();
}
bool NpcDialog::isTextInputFocused() const
{
return mTextField->isFocused();
}
bool NpcDialog::isInputFocused() const
{
return mTextField->isFocused() || mIntField->isFocused()
|| mItemList->isFocused();
}
bool NpcDialog::isAnyInputFocused()
{
FOR_EACH (DialogList::const_iterator, it, instances)
{
if ((*it) && (*it)->isInputFocused())
return true;
}
return false;
}
void NpcDialog::integerRequest(const int defaultValue, const int min,
const int max)
{
mActionState = NPC_ACTION_INPUT;
mInputState = NPC_INPUT_INTEGER;
mDefaultInt = defaultValue;
mIntField->setRange(min, max);
mIntField->setValue(defaultValue);
buildLayout();
}
void NpcDialog::itemRequest(const int size)
{
mActionState = NPC_ACTION_INPUT;
mInputState = NPC_INPUT_ITEM;
mInventory->resize(size);
buildLayout();
}
void NpcDialog::move(const int amount)
{
if (mActionState != NPC_ACTION_INPUT)
return;
switch (mInputState)
{
case NPC_INPUT_INTEGER:
mIntField->setValue(mIntField->getValue() + amount);
break;
case NPC_INPUT_LIST:
mItemList->setSelected(mItemList->getSelected() - amount);
break;
case NPC_INPUT_NONE:
case NPC_INPUT_STRING:
case NPC_INPUT_ITEM:
default:
break;
}
}
void NpcDialog::setVisible(bool visible)
{
Window::setVisible(visible);
if (!visible)
scheduleDelete();
}
void NpcDialog::optionChanged(const std::string &name)
{
if (name == "logNpcInGui")
mLogInteraction = config.getBoolValue("logNpcInGui");
}
NpcDialog *NpcDialog::getActive()
{
if (instances.size() == 1)
return instances.front();
FOR_EACH (DialogList::const_iterator, it, instances)
{
if ((*it) && (*it)->isFocused())
return (*it);
}
return nullptr;
}
void NpcDialog::closeAll()
{
FOR_EACH (DialogList::const_iterator, it, instances)
{
if (*it)
(*it)->close();
}
}
void NpcDialog::placeNormalControls()
{
if (mShowAvatar)
{
place(0, 0, mPlayerBox);
place(1, 0, mScrollArea, 5, 3);
place(4, 3, mClearButton);
place(5, 3, mButton);
}
else
{
place(0, 0, mScrollArea, 5, 3);
place(3, 3, mClearButton);
place(4, 3, mButton);
}
}
void NpcDialog::placeMenuControls()
{
if (mShowAvatar)
{
place(0, 0, mPlayerBox);
place(1, 0, mScrollArea, 6, 3);
place(0, 3, mListScrollArea, 7, 3);
place(1, 6, mButton2, 2);
place(3, 6, mClearButton, 2);
place(5, 6, mButton, 2);
}
else
{
place(0, 0, mScrollArea, 6, 3);
place(0, 3, mListScrollArea, 6, 3);
place(0, 6, mButton2, 2);
place(2, 6, mClearButton, 2);
place(4, 6, mButton, 2);
}
}
void NpcDialog::placeTextInputControls()
{
if (mShowAvatar)
{
place(0, 0, mPlayerBox);
place(1, 0, mScrollArea, 6, 3);
place(0, 3, mTextField, 6);
place(0, 4, mResetButton, 2);
place(4, 4, mClearButton, 2);
place(5, 4, mButton, 2);
}
else
{
place(0, 0, mScrollArea, 6, 3);
place(0, 3, mTextField, 6);
place(0, 4, mResetButton, 2);
place(2, 4, mClearButton, 2);
place(4, 4, mButton, 2);
}
}
void NpcDialog::placeIntInputControls()
{
if (mShowAvatar)
{
place(0, 0, mPlayerBox);
place(1, 0, mScrollArea, 6, 3);
place(1, 3, mMinusButton, 1);
place(2, 3, mIntField, 4);
place(6, 3, mPlusButton, 1);
place(0, 4, mResetButton, 2);
place(3, 4, mClearButton, 2);
place(5, 4, mButton, 2);
}
else
{
place(0, 0, mScrollArea, 6, 3);
place(0, 3, mMinusButton, 1);
place(1, 3, mIntField, 4);
place(5, 3, mPlusButton, 1);
place(0, 4, mResetButton, 2);
place(2, 4, mClearButton, 2);
place(4, 4, mButton, 2);
}
}
void NpcDialog::placeItemInputControls()
{
if (mShowAvatar)
{
place(0, 0, mPlayerBox);
place(1, 0, mScrollArea, 6, 3);
place(0, 3, mItemScrollArea, 7, 3);
place(1, 6, mButton3, 2);
place(3, 6, mClearButton, 2);
place(5, 6, mButton, 2);
}
else
{
place(0, 0, mScrollArea, 6, 3);
place(0, 3, mItemScrollArea, 6, 3);
place(0, 6, mButton3, 2);
place(2, 6, mClearButton, 2);
place(4, 6, mButton, 2);
}
}
void NpcDialog::buildLayout()
{
clearLayout();
if (mActionState != NPC_ACTION_INPUT)
{
if (mActionState == NPC_ACTION_WAIT)
mButton->setCaption(CAPTION_WAITING);
else if (mActionState == NPC_ACTION_NEXT)
mButton->setCaption(CAPTION_NEXT);
else if (mActionState == NPC_ACTION_CLOSE)
mButton->setCaption(CAPTION_CLOSE);
placeNormalControls();
}
else if (mInputState != NPC_INPUT_NONE)
{
mButton->setCaption(CAPTION_SUBMIT);
switch (mInputState)
{
case NPC_INPUT_LIST:
placeMenuControls();
mItemList->setSelected(-1);
break;
case NPC_INPUT_STRING:
placeTextInputControls();
break;
case NPC_INPUT_INTEGER:
placeIntInputControls();
break;
case NPC_INPUT_ITEM:
placeItemInputControls();
break;
case NPC_INPUT_NONE:
default:
break;
}
}
Layout &layout = getLayout();
layout.setRowHeight(1, LayoutType::SET);
redraw();
mScrollArea->setVerticalScrollAmount(mScrollArea->getVerticalMaxScroll());
}
void NpcDialog::saveCamera()
{
if (!viewport || mCameraMode >= 0)
return;
mCameraMode = static_cast<int>(settings.cameraMode);
mCameraX = viewport->getCameraRelativeX();
mCameraY = viewport->getCameraRelativeY();
}
void NpcDialog::restoreCamera()
{
if (!viewport || mCameraMode == -1)
return;
if (static_cast<int>(settings.cameraMode) != mCameraMode)
viewport->toggleCameraMode();
if (mCameraMode)
{
viewport->setCameraRelativeX(mCameraX);
viewport->setCameraRelativeY(mCameraY);
}
mCameraMode = -1;
}
void NpcDialog::showAvatar(const uint16_t avatarId)
{
const bool needShow = (avatarId != 0);
if (needShow)
{
delete mAvatarBeing;
mAvatarBeing = new Being(0, ActorType::Avatar, avatarId, nullptr);
mPlayerBox->setPlayer(mAvatarBeing);
if (!mAvatarBeing->empty())
{
mAvatarBeing->logic();
const BeingInfo *const info = AvatarDB::get(avatarId);
const int pad2 = 2 * mPadding;
int width = 0;
if (info)
{
width = info->getWidth();
mPlayerBox->setWidth(width + pad2);
mPlayerBox->setHeight(info->getHeight() + pad2);
}
const Sprite *const sprite = mAvatarBeing->getSprite(0);
if (sprite && !width)
{
mPlayerBox->setWidth(sprite->getWidth() + pad2);
mPlayerBox->setHeight(sprite->getHeight() + pad2);
}
}
}
else
{
delete2(mAvatarBeing)
mPlayerBox->setPlayer(nullptr);
}
if (needShow != mShowAvatar)
{
mShowAvatar = needShow;
buildLayout();
}
else
{
mShowAvatar = needShow;
}
}
void NpcDialog::setAvatarDirection(const uint8_t direction)
{
Being *const being = mPlayerBox->getBeing();
if (being)
being->setDirection(direction);
}
void NpcDialog::setAvatarAction(const int actionId)
{
Being *const being = mPlayerBox->getBeing();
if (being)
being->setAction(static_cast<BeingAction::Action>(actionId), 0);
}
void NpcDialog::logic()
{
BLOCK_START("NpcDialog::logic")
Window::logic();
if (mShowAvatar && mAvatarBeing)
{
mAvatarBeing->logic();
if (mPlayerBox->getWidth() < static_cast<signed>(3 * getPadding()))
{
const Sprite *const sprite = mAvatarBeing->getSprite(0);
if (sprite)
{
mPlayerBox->setWidth(sprite->getWidth() + 2 * getPadding());
mPlayerBox->setHeight(sprite->getHeight() + 2 * getPadding());
buildLayout();
}
}
}
BLOCK_END("NpcDialog::logic")
}
void NpcDialog::clearRows()
{
mTextBox->clearRows();
}
void NpcDialog::clearDialogs()
{
NpcDialogs::iterator it = mNpcDialogs.begin();
const NpcDialogs::iterator it_end = mNpcDialogs.end();
while (it != it_end)
{
delete (*it).second;
++ it;
}
mNpcDialogs.clear();
}
void NpcDialog::mousePressed(MouseEvent &event)
{
Window::mousePressed(event);
if (event.getButton() == MouseButton::RIGHT
&& event.getSource() == mTextBox)
{
event.consume();
if (popupMenu)
{
popupMenu->showNpcDialogPopup(mNpcId,
viewport->mMouseX,
viewport->mMouseY);
}
}
}
void NpcDialog::copyToClipboard(const int npcId, const int x, const int y)
{
NpcDialogs::iterator it = mNpcDialogs.find(npcId);
if (it != mNpcDialogs.end())
{
const BrowserBox *const text = (*it).second->mTextBox;
if (!text)
return;
std::string str = text->getTextAtPos(x, y);
sendBuffer(str);
}
}
|