GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/worldselectdialog.cpp Lines: 31 50 62.0 %
Date: 2021-03-17 Branches: 34 84 40.5 %

Line Branch Exec Source
1
/*
2
 *  The ManaPlus Client
3
 *  Copyright (C) 2004-2009  The Mana World Development Team
4
 *  Copyright (C) 2009-2010  The Mana Developers
5
 *  Copyright (C) 2011-2019  The ManaPlus Developers
6
 *  Copyright (C) 2019-2021  Andrei Karas
7
 *
8
 *  This file is part of The ManaPlus Client.
9
 *
10
 *  This program is free software; you can redistribute it and/or modify
11
 *  it under the terms of the GNU General Public License as published by
12
 *  the Free Software Foundation; either version 2 of the License, or
13
 *  any later version.
14
 *
15
 *  This program is distributed in the hope that it will be useful,
16
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 *  GNU General Public License for more details.
19
 *
20
 *  You should have received a copy of the GNU General Public License
21
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 */
23
24
#include "gui/windows/worldselectdialog.h"
25
26
#include "client.h"
27
#include "settings.h"
28
29
#include "gui/widgets/button.h"
30
#include "gui/widgets/createwidget.h"
31
#include "gui/widgets/layout.h"
32
#include "gui/widgets/listbox.h"
33
#include "gui/widgets/scrollarea.h"
34
35
#include "gui/models/worldlistmodel.h"
36
37
#include "net/loginhandler.h"
38
39
#include "utils/delete2.h"
40
#include "utils/gettext.h"
41
42
#include "debug.h"
43
44
extern WorldInfo **server_info;
45
46
1
WorldSelectDialog::WorldSelectDialog(const Worlds &worlds) :
47
    // TRANSLATORS: world select dialog name
48
1
    Window(_("Select World"), Modal_false, nullptr, "world.xml"),
49
    ActionListener(),
50
    KeyListener(),
51
2
    mWorldListModel(new WorldListModel(worlds)),
52


4
    mWorldList(CREATEWIDGETR(ListBox, this, mWorldListModel, "")),
53
    // TRANSLATORS: world dialog button
54
1
    mChangeLoginButton(new Button(this, _("Change Login"), "login",
55

1
        BUTTON_SKIN, this)),
56
    // TRANSLATORS: world dialog button
57
1
    mChooseWorld(new Button(this, _("Choose World"), "world",
58




25
        BUTTON_SKIN, this))
59
{
60
1
    ScrollArea *const worldsScroll = new ScrollArea(this, mWorldList,
61

4
        fromBool(getOptionBool("showbackground", false), Opaque),
62

5
         "world_background.xml");
63
64
1
    worldsScroll->setHorizontalScrollPolicy(ScrollArea::SHOW_NEVER);
65
66
2
    place(0, 0, worldsScroll, 3, 5).setPadding(2);
67
1
    place(1, 5, mChangeLoginButton, 1, 1);
68
1
    place(2, 5, mChooseWorld, 1, 1);
69
70
    // Make sure the list has enough height
71

1
    getLayout().setRowHeight(0, 60);
72
73
1
    reflowLayout(0, 0);
74
75
1
    if (worlds.empty())
76
    {
77
        // Disable Ok button
78
1
        mChooseWorld->setEnabled(false);
79
    }
80
    else
81
    {
82
        // Select first server
83
        mWorldList->setSelected(0);
84
    }
85
86
1
    addKeyListener(this);
87
88
1
    center();
89
1
}
90
91
1
void WorldSelectDialog::postInit()
92
{
93
1
    Window::postInit();
94
1
    setVisible(Visible_true);
95
1
    mChooseWorld->requestFocus();
96
1
}
97
98
5
WorldSelectDialog::~WorldSelectDialog()
99
{
100
1
    delete2(mWorldListModel)
101
2
}
102
103
void WorldSelectDialog::action(const ActionEvent &event)
104
{
105
    const std::string &eventId = event.getId();
106
    if (eventId == "world")
107
    {
108
        mChangeLoginButton->setEnabled(false);
109
        mChooseWorld->setEnabled(false);
110
        loginHandler->chooseServer(mWorldList->getSelected(),
111
            settings.persistentIp);
112
113
        // Check in case netcode moves us forward
114
        if (client->getState() == State::WORLD_SELECT)
115
            client->setState(State::WORLD_SELECT_ATTEMPT);
116
    }
117
    else if (eventId == "login")
118
    {
119
        client->setState(State::SWITCH_LOGIN);
120
    }
121
}
122
123
void WorldSelectDialog::keyPressed(KeyEvent &event)
124
{
125
    const InputActionT actionId = event.getActionId();
126
127
    if (actionId == InputAction::GUI_CANCEL)
128
    {
129
        action(ActionEvent(nullptr,
130
            mChangeLoginButton->getActionEventId()));
131
    }
132
    else if (actionId == InputAction::GUI_SELECT ||
133
             actionId == InputAction::GUI_SELECT2)
134
    {
135
        action(ActionEvent(nullptr, mChooseWorld->getActionEventId()));
136
    }
137

3
}