Mana
Loading...
Searching...
No Matches
worldselectdialog.cpp
Go to the documentation of this file.
1/*
2 * The Mana Client
3 * Copyright (C) 2004-2009 The Mana World Development Team
4 * Copyright (C) 2009-2012 The Mana Developers
5 *
6 * This file is part of The Mana Client.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include "client.h"
25
26#include "gui/sdlinput.h"
27
28#include "gui/widgets/button.h"
29#include "gui/widgets/layout.h"
30#include "gui/widgets/listbox.h"
32
33#include "net/loginhandler.h"
34#include "net/net.h"
35#include "net/worldinfo.h"
36
37#include "utils/gettext.h"
38#include "utils/stringutils.h"
39
40extern WorldInfo **server_info;
41
45class WorldListModel : public gcn::ListModel
46{
47 public:
49 mWorlds(std::move(worlds))
50 {
51 }
52
53 int getNumberOfElements() override
54 {
55 return mWorlds.size();
56 }
57
58 std::string getElementAt(int i) override
59 {
60 const WorldInfo *si = mWorlds[i];
61 return si->name + " (" + toString(si->online_users) + ")";
62 }
63 private:
65};
66
68 Window(_("Select World"))
69{
70 mWorldListModel = std::make_unique<WorldListModel>(worlds);
72 auto *worldsScroll = new ScrollArea(mWorldList);
73 mChangeLoginButton = new Button(_("Change Login"), "login", this);
74 mChooseWorld = new Button(_("Choose World"), "world", this);
75
76 worldsScroll->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
77
78 place(0, 0, worldsScroll, 3, 5).setPadding(2);
80 place(2, 5, mChooseWorld);
81
82 // Make sure the list has enough height
83 getLayout().setRowHeight(0, 60);
84
85 reflowLayout(0, 0);
86
87 if (worlds.empty())
88 // Disable Ok button
89 mChooseWorld->setEnabled(false);
90 else
91 // Select first server
92 mWorldList->setSelected(0);
93
94 addKeyListener(this);
95
96 center();
97 setVisible(true);
98 mChooseWorld->requestFocus();
99}
100
102
103void WorldSelectDialog::action(const gcn::ActionEvent &event)
104{
105 if (event.getId() == "world")
106 {
107 mChangeLoginButton->setEnabled(false);
108 mChooseWorld->setEnabled(false);
110
111 // Check in case netcode moves us forward
114 }
115 else if (event.getId() == "login")
116 {
118 }
119}
120
121void WorldSelectDialog::keyPressed(gcn::KeyEvent &keyEvent)
122{
123 gcn::Key key = keyEvent.getKey();
124
125 if (key.getValue() == Key::ESCAPE)
126 {
127 action(gcn::ActionEvent(nullptr, mChangeLoginButton->getActionEventId()));
128 }
129 else if (key.getValue() == Key::ENTER)
130 {
131 action(gcn::ActionEvent(nullptr, mChooseWorld->getActionEventId()));
132 }
133}
134
135void WorldSelectDialog::mouseClicked(gcn::MouseEvent &mouseEvent)
136{
137 if (mouseEvent.getSource() == mWorldList &&
138 isDoubleClick(mWorldList->getSelected()))
139 {
140 action(gcn::ActionEvent(mChooseWorld,
141 mChooseWorld->getActionEventId()));
142 }
143}
Button widget.
Definition button.h:38
static void setState(State state)
Definition client.h:169
static State getState()
Definition client.h:172
void setRowHeight(int n, int h)
Definition layout.h:221
LayoutCell & setPadding(int p)
Sets the padding around the cell content.
Definition layout.h:179
A list box, meant to be used inside a scroll area.
Definition listbox.h:36
virtual void chooseServer(unsigned int server)=0
A scroll area.
Definition scrollarea.h:38
A window.
Definition window.h:59
void center()
Positions the window in the center of it's parent.
Definition window.cpp:768
virtual void setVisible(bool visible)
Overloads window setVisible by Guichan to allow sticky window handling.
Definition window.cpp:282
Layout & getLayout()
Gets the layout handler for this window.
Definition window.cpp:714
void reflowLayout(int w=0, int h=0)
Computes the position of the widgets according to the current layout.
Definition window.cpp:748
LayoutCell & place(int x, int y, gcn::Widget *, int w=1, int h=1)
Adds a widget to the window and sets it at given cell.
Definition window.cpp:737
The list model for the server list.
std::string getElementAt(int i) override
WorldListModel(Worlds worlds)
int getNumberOfElements() override
void action(const gcn::ActionEvent &event) override
Called when receiving actions from the widgets.
gcn::Button * mChangeLoginButton
void keyPressed(gcn::KeyEvent &keyEvent) override
~WorldSelectDialog() override
WorldSelectDialog(Worlds worlds)
void mouseClicked(gcn::MouseEvent &mouseEvent) override
std::unique_ptr< WorldListModel > mWorldListModel
gcn::ListBox * mWorldList
gcn::Button * mChooseWorld
bool isDoubleClick(int selected)
Returns whether this call and the last call were done for the same selected index and within a short ...
Definition client.cpp:126
@ STATE_WORLD_SELECT
Definition client.h:66
@ STATE_WORLD_SELECT_ATTEMPT
Definition client.h:67
@ STATE_LOGIN
Definition client.h:64
#define _(s)
Definition gettext.h:38
@ ENTER
Definition sdlinput.h:78
@ ESCAPE
Definition sdlinput.h:96
LoginHandler * getLoginHandler()
Definition net.cpp:95
std::string toString(const T &arg)
Converts the given value to a string using std::stringstream.
Definition stringutils.h:68
std::string name
Definition worldinfo.h:29
short online_users
Definition worldinfo.h:31
std::vector< WorldInfo * > Worlds
Definition worldinfo.h:35
WorldInfo ** server_info