summaryrefslogtreecommitdiff
path: root/src/gui/windows/charselectdialog.cpp
blob: 524be16ccb4230ed5023f76801c175ce98cf0e22 (plain) (blame)
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
/*
 *  The ManaPlus Client
 *  Copyright (C) 2004-2009  The Mana World Development Team
 *  Copyright (C) 2009-2010  The Mana Developers
 *  Copyright (C) 2011-2017  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/charselectdialog.h"

#include "client.h"
#include "configuration.h"

#include "listeners/charrenamelistener.h"
#include "listeners/pincodelistener.h"

#include "gui/windows/charcreatedialog.h"
#include "gui/windows/chardeleteconfirm.h"
#include "gui/windows/editdialog.h"
#include "gui/windows/logindialog.h"
#include "gui/windows/okdialog.h"
#include "gui/windows/textdialog.h"

#include "gui/widgets/button.h"
#include "gui/widgets/characterdisplay.h"
#include "gui/widgets/characterviewnormal.h"
#include "gui/widgets/characterviewsmall.h"
#include "gui/widgets/containerplacer.h"
#include "gui/widgets/createwidget.h"

#include "net/character.h"
#include "net/charserverhandler.h"
#include "net/logindata.h"
#ifdef TMWA_SUPPORT
#include "net/net.h"
#endif  // TMWA_SUPPORT
#include "net/serverfeatures.h"

#include "utils/foreach.h"

#include "resources/db/unitsdb.h"

#include "debug.h"

// Character slots per row in the dialog
static const int SLOTS_PER_ROW = 5;

CharSelectDialog::CharSelectDialog(LoginData &data) :
    // TRANSLATORS: char select dialog name
    Window(strprintf(_("Account %s (last login time %s)"),
        data.username.c_str(), data.lastLogin.c_str()),
        Modal_false,
        nullptr,
        "char.xml"),
    ActionListener(),
    KeyListener(),
    mLoginData(&data),
    // TRANSLATORS: char select dialog. button.
    mSwitchLoginButton(new Button(this, _("Switch"), "switch", this)),
    // TRANSLATORS: char select dialog. button.
    mChangePasswordButton(new Button(this, _("Password"),
                          "change_password", this)),
    // TRANSLATORS: char select dialog. button.
    mPlayButton(new Button(this, _("Play"), "use", this)),
    // TRANSLATORS: char select dialog. button.
    mInfoButton(new Button(this, _("Info"), "info", this)),
    // TRANSLATORS: char select dialog. button.
    mDeleteButton(new Button(this, _("Delete"), "delete", this)),
    // TRANSLATORS: char select dialog. button.
    mRenameButton(nullptr),
    mCharacterView(nullptr),
    mCharacterEntries(0),
    mCharServerHandler(charServerHandler),
    mDeleteDialog(nullptr),
    mDeleteIndex(-1),
    mLocked(false),
    mSmallScreen(mainGraphics->getWidth() < 470
                 || mainGraphics->getHeight() < 370)
{
    setCloseButton(true);
    setFocusable(true);

    ContainerPlacer placer(nullptr, nullptr);
    placer = getPlacer(0, 0);

    placer(0, 0, mSwitchLoginButton, 1, 1);

    int n = 1;
    placer(n, 0, mChangePasswordButton, 1, 1);
    n ++;
    placer(n, 0, mDeleteButton, 1, 1);
    n ++;
#ifdef TMWA_SUPPORT
    if (Net::getNetworkType() != ServerType::TMWATHENA)
#endif
    {
        mRenameButton = new Button(this,
            // TRANSLATORS: character rename button
            _("Rename"),
            "rename",
            this);
        placer(n, 0, mRenameButton, 1, 1);
        n ++;
    }
    placer(n, 0, mInfoButton, 1, 1);
    n ++;

    for (int i = 0; i < CAST_S32(mLoginData->characterSlots); i++)
    {
        CharacterDisplay *const character = new CharacterDisplay(this, this);
        character->setVisible(Visible_false);
        mCharacterEntries.push_back(character);
    }

    placer(0, 2, mPlayButton, 1, 1);

    if (!mSmallScreen)
    {
        mCharacterView = new CharacterViewNormal(
            this, &mCharacterEntries, mPadding);
        placer(0, 1, mCharacterView, 10, 1);
        int sz = 410 + 2 * mPadding;
        if (config.getIntValue("fontSize") > 18)
            sz = 500 + 2 * mPadding;
        const int width = mCharacterView->getWidth() + 2 * mPadding;
        if (sz < width)
            sz = width;
        if (sz > mainGraphics->getWidth())
            sz = mainGraphics->getWidth();
        reflowLayout(sz, 0);
    }
    else
    {
        // TRANSLATORS: char select dialog name
        setCaption(strprintf(_("Account %s"), mLoginData->username.c_str()));
        mCharacterView = new CharacterViewSmall(
            this, &mCharacterEntries, mPadding);
        mCharacterView->setWidth(mainGraphics->getWidth()
            - 2 * getPadding());
        placer(0, 1, mCharacterView, 10, 1);
        reflowLayout(0, 0);
    }
    addKeyListener(this);
    center();

    charServerHandler->setCharSelectDialog(this);
    mCharacterView->show(0);
    updateState();
}

CharSelectDialog::~CharSelectDialog()
{
}

void CharSelectDialog::postInit()
{
    Window::postInit();
    setVisible(Visible_true);
    requestFocus();
    if (charServerHandler->isNeedCreatePin())
    {
        EditDialog *const dialog = CREATEWIDGETR(EditDialog,
            // TRANSLATORS: pin code dialog header.
            _("Please set new pincode"),
            "",
            "OK",
            300,
            nullptr,
            Modal_true);
        dialog->addActionListener(&pincodeListener);
    }
}

void CharSelectDialog::action(const ActionEvent &event)
{
    // Check if a button of a character was pressed
    const Widget *const sourceParent = event.getSource()->getParent();
    int selected = -1;
    for (unsigned int i = 0, fsz = CAST_U32(
         mCharacterEntries.size());
         i < fsz;
         ++i)
    {
        if (mCharacterEntries[i] == sourceParent)
        {
            selected = i;
            mCharacterView->show(i);
            updateState();
            break;
        }
    }
    if (selected == -1)
        selected = mCharacterView->getSelected();

    const std::string &eventId = event.getId();

    if (selected >= 0)
    {
        if (eventId == "use")
        {
            use(selected);
            return;
        }
        else if (eventId == "delete"
                 && (mCharacterEntries[selected]->getCharacter() != nullptr))
        {
            CREATEWIDGET(CharDeleteConfirm, this, selected);
            return;
        }
        else if (eventId == "rename"
                 && (mCharacterEntries[selected]->getCharacter() != nullptr))
        {
            const LocalPlayer *const player = mCharacterEntries[
                selected]->getCharacter()->dummy;
            EditDialog *const dialog = CREATEWIDGETR(EditDialog,
                // TRANSLATORS: character rename dialog header.
                _("Please enter new name"),
                player->getName(),
                "OK",
                300,
                nullptr,
                Modal_true);
            charRenameListener.setId(player->getId());
            charRenameListener.setDialog(dialog);
            dialog->addActionListener(&charRenameListener);
        }
        else if (eventId == "info")
        {
            Net::Character *const character = mCharacterEntries[
                selected]->getCharacter();
            if (character == nullptr)
                return;

            const LocalPlayer *const data = character->dummy;
            if (data == nullptr)
                return;

            const std::string strExp = toString(CAST_U64(
                character->data.mAttributes[Attributes::PLAYER_EXP]));
            const std::string msg = strprintf(
                // TRANSLATORS: char select dialog. player info message.
                _("Hp: %u/%u\nMp: %u/%u\nLevel: %u\n"
                "Experience: %s\nMoney: %s"),
                CAST_U32(
                character->data.mAttributes[Attributes::PLAYER_HP]),
                CAST_U32(
                character->data.mAttributes[Attributes::PLAYER_MAX_HP]),
                CAST_U32(
                character->data.mAttributes[Attributes::PLAYER_MP]),
                CAST_U32(
                character->data.mAttributes[Attributes::PLAYER_MAX_MP]),
                CAST_U32(
                character->data.mAttributes[Attributes::PLAYER_BASE_LEVEL]),
                strExp.c_str(),
                UnitsDb::formatCurrency(CAST_S32(
                character->data.mAttributes[Attributes::MONEY])).c_str());
            CREATEWIDGET(OkDialog, data->getName(), msg,
                // TRANSLATORS: ok dialog button
                _("OK"),
                DialogType::SILENCE,
                Modal_true,
                ShowCenter_true,
                nullptr,
                260);
        }
    }
    if (eventId == "switch")
    {
        charServerHandler->clear();
        close();
    }
    else if (eventId == "change_password")
    {
        client->setState(State::CHANGEPASSWORD);
    }
    else if (eventId == "change_email")
    {
        client->setState(State::CHANGEEMAIL);
    }
    else if (eventId == "try delete character")
    {
        if ((mDeleteDialog != nullptr) && mDeleteIndex != -1)
        {
            if (serverFeatures->haveEmailOnDelete())
            {
                attemptCharacterDelete(mDeleteIndex, mDeleteDialog->getText());
                mDeleteDialog = nullptr;
            }
            else if (mDeleteDialog->getText() == LoginDialog::savedPassword)
            {
                attemptCharacterDelete(mDeleteIndex, "");
                mDeleteDialog = nullptr;
            }
            else
            {
                CREATEWIDGET(OkDialog,
                    // TRANSLATORS: error header
                    _("Error"),
                    // TRANSLATORS: error message
                    _("Incorrect password"),
                    // TRANSLATORS: ok dialog button
                    _("OK"),
                    DialogType::ERROR,
                    Modal_true,
                    ShowCenter_true,
                    nullptr,
                    260);
            }
        }
        mDeleteIndex = -1;
    }
}

void CharSelectDialog::use(const int selected)
{
    if ((mCharacterEntries[selected] != nullptr)
        && (mCharacterEntries[selected]->getCharacter() != nullptr))
    {
        attemptCharacterSelect(selected);
    }
    else
    {
        CharCreateDialog *const charCreateDialog =
            CREATEWIDGETR(CharCreateDialog, this, selected);
        mCharServerHandler->setCharCreateDialog(charCreateDialog);
    }
}

void CharSelectDialog::keyPressed(KeyEvent &event)
{
    const InputActionT actionId = event.getActionId();
    PRAGMA45(GCC diagnostic push)
    PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
    switch (actionId)
    {
        case InputAction::GUI_CANCEL:
            event.consume();
            action(ActionEvent(mSwitchLoginButton,
                mSwitchLoginButton->getActionEventId()));
            break;

        case InputAction::GUI_RIGHT:
        {
            event.consume();
            int idx = mCharacterView->getSelected();
            if (idx >= 0)
            {
                idx ++;
                if (idx == SLOTS_PER_ROW)
                    break;
                mCharacterView->show(idx);
                updateState();
            }
            break;
        }

        case InputAction::GUI_LEFT:
        {
            event.consume();
            int idx = mCharacterView->getSelected();
            if (idx >= 0)
            {
                if ((idx == 0) || idx == SLOTS_PER_ROW)
                    break;
                idx --;
                mCharacterView->show(idx);
                updateState();
            }
            break;
        }

        case InputAction::GUI_UP:
        {
            event.consume();
            int idx = mCharacterView->getSelected();
            if (idx >= 0)
            {
                if (idx < SLOTS_PER_ROW)
                    break;
                idx -= SLOTS_PER_ROW;
                mCharacterView->show(idx);
                updateState();
            }
            break;
        }

        case InputAction::GUI_DOWN:
        {
            event.consume();
            int idx = mCharacterView->getSelected();
            if (idx >= 0)
            {
                if (idx >= SLOTS_PER_ROW)
                    break;
                idx += SLOTS_PER_ROW;
                mCharacterView->show(idx);
                updateState();
            }
            break;
        }

        case InputAction::GUI_DELETE:
        {
            event.consume();
            const int idx = mCharacterView->getSelected();
            if (idx >= 0 && (mCharacterEntries[idx] != nullptr)
                && (mCharacterEntries[idx]->getCharacter() != nullptr))
            {
                CREATEWIDGET(CharDeleteConfirm, this, idx);
            }
            break;
        }

        case InputAction::GUI_SELECT:
        {
            event.consume();
            use(mCharacterView->getSelected());
            break;
        }
        default:
            break;
    }
    PRAGMA45(GCC diagnostic pop)
}

/**
 * Communicate character deletion to the server.
 */
void CharSelectDialog::attemptCharacterDelete(const int index,
                                              const std::string &email)
{
    if (mLocked)
        return;

    if (mCharacterEntries[index] != nullptr)
    {
        mCharServerHandler->deleteCharacter(
            mCharacterEntries[index]->getCharacter(),
            email);
    }
    lock();
}

void CharSelectDialog::askPasswordForDeletion(const int index)
{
    mDeleteIndex = index;
    if (serverFeatures->haveEmailOnDelete())
    {
        CREATEWIDGETV(mDeleteDialog, TextDialog,
            // TRANSLATORS: char deletion question.
            _("Enter your email for deleting character"),
            // TRANSLATORS: email label.
            _("Enter email:"),
            this,
            false);
    }
    else
    {
        CREATEWIDGETV(mDeleteDialog, TextDialog,
            // TRANSLATORS: char deletion question.
            _("Enter password for deleting character"),
            // TRANSLATORS: email label.
            _("Enter password:"),
            this,
            true);
    }
    mDeleteDialog->setActionEventId("try delete character");
    mDeleteDialog->addActionListener(this);
}

/**
 * Communicate character selection to the server.
 */
void CharSelectDialog::attemptCharacterSelect(const int index)
{
    if (mLocked || (mCharacterEntries[index] == nullptr))
        return;

    setVisible(Visible_false);
    if (mCharServerHandler != nullptr)
    {
        mCharServerHandler->chooseCharacter(
            mCharacterEntries[index]->getCharacter());
    }
    lock();
}

void CharSelectDialog::setCharacters(const Net::Characters &characters)
{
    // Reset previous characters
    FOR_EACH (STD_VECTOR<CharacterDisplay*>::const_iterator,
              iter, mCharacterEntries)
    {
        if (*iter != nullptr)
            (*iter)->setCharacter(nullptr);
    }

    FOR_EACH (Net::Characters::const_iterator, i, characters)
        setCharacter(*i);
    updateState();
}

void CharSelectDialog::setCharacter(Net::Character *const character)
{
    if (character == nullptr)
        return;
    const int characterSlot = character->slot;
    if (characterSlot >= CAST_S32(mCharacterEntries.size()))
    {
        logger->log("Warning: slot out of range: %d", character->slot);
        return;
    }

    if (mCharacterEntries[characterSlot] != nullptr)
        mCharacterEntries[characterSlot]->setCharacter(character);
}

void CharSelectDialog::lock()
{
    if (!mLocked)
        setLocked(true);
}

void CharSelectDialog::unlock()
{
    setLocked(false);
}

void CharSelectDialog::setLocked(const bool locked)
{
    mLocked = locked;

    if (mSwitchLoginButton != nullptr)
        mSwitchLoginButton->setEnabled(!locked);
    if (mChangePasswordButton != nullptr)
        mChangePasswordButton->setEnabled(!locked);
    mPlayButton->setEnabled(!locked);
    if (mDeleteButton != nullptr)
        mDeleteButton->setEnabled(!locked);

    for (size_t i = 0, sz = mCharacterEntries.size(); i < sz; ++i)
    {
        if (mCharacterEntries[i] != nullptr)
            mCharacterEntries[i]->setActive(!mLocked);
    }
}

bool CharSelectDialog::selectByName(const std::string &name,
                                    const SelectAction selAction)
{
    if (mLocked)
        return false;

    for (size_t i = 0, sz = mCharacterEntries.size(); i < sz; ++i)
    {
        if (mCharacterEntries[i] != nullptr)
        {
            const Net::Character *const character
                = mCharacterEntries[i]->getCharacter();
            if (character != nullptr)
            {
                if (character->dummy != nullptr &&
                    character->dummy->getName() == name)
                {
                    mCharacterView->show(CAST_S32(i));
                    updateState();
                    if (selAction == Choose)
                        attemptCharacterSelect(CAST_S32(i));
                    return true;
                }
            }
        }
    }

    return false;
}

void CharSelectDialog::close()
{
    client->setState(State::SWITCH_LOGIN);
    Window::close();
}

void CharSelectDialog::widgetResized(const Event &event)
{
    Window::widgetResized(event);
    mCharacterView->resize();
}

void CharSelectDialog::updateState()
{
    const int idx = mCharacterView->getSelected();
    if (idx == -1)
    {
        mPlayButton->setEnabled(false);
        return;
    }
    mPlayButton->setEnabled(true);

    if (mCharacterEntries[idx] != nullptr &&
        mCharacterEntries[idx]->getCharacter() != nullptr)
    {
        // TRANSLATORS: char select dialog. button.
        mPlayButton->setCaption(_("Play"));

        const LocalPlayer *const player = mCharacterEntries[
            idx]->getCharacter()->dummy;
        if ((player != nullptr) && (mRenameButton != nullptr))
            mRenameButton->setEnabled(player->getRename() ? true : false);
    }
    else
    {
        // TRANSLATORS: char select dialog. button.
        mPlayButton->setCaption(_("Create"));
    }
}

void CharSelectDialog::setName(const BeingId id, const std::string &newName)
{
    for (unsigned int i = 0, fsz = CAST_U32(
         mCharacterEntries.size());
         i < fsz;
         ++i)
    {
        if (mCharacterEntries[i] == nullptr)
            continue;
        CharacterDisplay *const character = mCharacterEntries[i];
        if (character == nullptr)
            continue;
        LocalPlayer *const player = character->getCharacter()->dummy;
        if ((player != nullptr) && player->getId() == id)
        {
            player->setName(newName);
            character->update();
            return;
        }
    }
}