GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/gui/windows/worldselectdialog.cpp Lines: 31 50 62.0 %
Date: 2018-05-19 03:07:18 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-2018  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
2
    mChangeLoginButton(new Button(this, _("Change Login"), "login",
54

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




50
        BUTTON_SKIN, this))
58
{
59
2
    ScrollArea *const worldsScroll = new ScrollArea(this, mWorldList,
60

8
        fromBool(getOptionBool("showbackground", false), Opaque),
61

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

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

6
}