GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/worldselectdialog.cpp Lines: 29 48 60.4 %
Date: 2017-11-29 Branches: 35 86 40.7 %

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


8
    mWorldList(CREATEWIDGETR(ListBox, this, mWorldListModel, "")),
52
    // TRANSLATORS: world dialog button
53

4
    mChangeLoginButton(new Button(this, _("Change Login"), "login", this)),
54
    // TRANSLATORS: world dialog button
55




50
    mChooseWorld(new Button(this, _("Choose World"), "world", this))
56
{
57
2
    ScrollArea *const worldsScroll = new ScrollArea(this, mWorldList,
58

8
        fromBool(getOptionBool("showbackground"), Opaque),
59

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

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

6
}