Mana
Loading...
Searching...
No Matches
quitdialog.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
22#include "gui/quitdialog.h"
23
24#include "client.h"
25
26#include "gui/sdlinput.h"
27
28#include "gui/widgets/layout.h"
29#include "gui/widgets/button.h"
31
32#include "net/charhandler.h"
33#include "net/net.h"
34
35#include "utils/gettext.h"
36
37#include <cassert>
38
40 Window(_("Quit"), true, nullptr), mMyPointer(pointerToMe)
41{
42 mForceQuit = new RadioButton(_("Quit"), "quitdialog");
43 mLogoutQuit = new RadioButton(_("Quit"), "quitdialog");
44 mSwitchAccountServer = new RadioButton(_("Switch server"), "quitdialog");
45 mSwitchCharacter = new RadioButton(_("Switch character"), "quitdialog");
46 mOkButton = new Button(_("OK"), "ok", this);
47 mOkButton->setButtonIcon("button-icon-confirm.png");
48 mCancelButton = new Button(_("Cancel"), "cancel", this);
49 mCancelButton->setButtonIcon("button-icon-cancel.png");
50
51 addKeyListener(this);
52
54
55 const State state = Client::getState();
56
57 // All states, when we're not logged in to someone.
58 if (state == STATE_CHOOSE_SERVER ||
59 state == STATE_CONNECT_SERVER ||
60 state == STATE_LOGIN ||
61 state == STATE_LOGIN_ATTEMPT ||
62 state == STATE_UPDATE ||
63 state == STATE_LOAD_DATA)
64 {
66 }
67 else
68 {
69 // Only added if we are connected to an accountserver or gameserver
72
73 // Only added if we are connected to a gameserver
74 if (state == STATE_GAME)
76 }
77
78 mOptions[0]->setSelected(true);
79
80 place = getPlacer(0, 1);
81
82 place(1, 0, mOkButton);
83 place(2, 0, mCancelButton);
84
85 reflowLayout(150, 0);
86 center();
87 setVisible(true);
88 requestModalFocus();
89 mOkButton->requestFocus();
90}
91
93{
94 if (mMyPointer)
95 *mMyPointer = nullptr;
96
97 // Optional widgets, so delete them by hand.
98 delete mForceQuit;
99 delete mLogoutQuit;
101 delete mSwitchCharacter;
102}
103
104void QuitDialog::placeOption(ContainerPlacer &place, gcn::RadioButton *option)
105{
106 place(0, mOptions.size(), option, 3);
107 mOptions.push_back(option);
108}
109
110void QuitDialog::action(const gcn::ActionEvent &event)
111{
112 if (event.getId() == "ok")
113 {
114 if (mForceQuit->isSelected())
115 {
117 }
118 else if (mLogoutQuit->isSelected())
119 {
121 }
122 else if (mSwitchAccountServer->isSelected())
123 {
125 }
126 else if (mSwitchCharacter->isSelected())
127 {
128 assert(Client::getState() == STATE_GAME);
129
131 }
132 }
134}
135
136void QuitDialog::keyPressed(gcn::KeyEvent &keyEvent)
137{
138 const gcn::Key &key = keyEvent.getKey();
139 int dir = 0;
140
141 switch (key.getValue())
142 {
143 case Key::ENTER:
144 action(gcn::ActionEvent(nullptr, mOkButton->getActionEventId()));
145 break;
146 case Key::ESCAPE:
147 action(gcn::ActionEvent(nullptr, mCancelButton->getActionEventId()));
148 break;
149 case Key::UP:
150 dir = -1;
151 break;
152 case Key::DOWN:
153 dir = 1;
154 break;
155 }
156
157 if (dir != 0)
158 {
159 auto it = mOptions.begin();
160
161 for (; it < mOptions.end(); it++)
162 {
163 if ((*it)->isSelected())
164 break;
165 }
166
167 if (it == mOptions.end())
168 {
169 mOptions[0]->setSelected(true);
170 return;
171 }
172
173 if (it == mOptions.begin() && dir < 0)
174 it = mOptions.end();
175
176 it += dir;
177
178 if (it == mOptions.end())
179 it = mOptions.begin();
180
181 (*it)->setSelected(true);
182 }
183}
Button widget.
Definition button.h:38
bool setButtonIcon(const std::string &iconFile)
Set the icons available next to the text.
Definition button.cpp:66
static void setState(State state)
Definition client.h:169
static State getState()
Definition client.h:172
This class is a helper for adding widgets to nested tables in a window.
Definition layout.h:34
virtual void switchCharacter()=0
The quit dialog.
Definition quitdialog.h:40
gcn::RadioButton * mSwitchAccountServer
Definition quitdialog.h:64
void action(const gcn::ActionEvent &event) override
Called when receiving actions from the widgets.
QuitDialog ** mMyPointer
Definition quitdialog.h:69
std::vector< gcn::RadioButton * > mOptions
Definition quitdialog.h:60
Button * mOkButton
Definition quitdialog.h:66
Button * mCancelButton
Definition quitdialog.h:67
void placeOption(ContainerPlacer &place, gcn::RadioButton *option)
QuitDialog(QuitDialog **pointerToMe)
Constructor.
gcn::RadioButton * mForceQuit
Definition quitdialog.h:63
void keyPressed(gcn::KeyEvent &keyEvent) override
~QuitDialog() override
gcn::RadioButton * mSwitchCharacter
Definition quitdialog.h:65
gcn::RadioButton * mLogoutQuit
Definition quitdialog.h:62
Guichan based RadioButton with custom look.
Definition radiobutton.h:30
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
ContainerPlacer getPlacer(int x, int y)
Returns a proxy for adding widgets in an inner table of the layout.
Definition window.cpp:743
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
void scheduleDelete()
Schedule this window for deletion.
Definition window.cpp:299
State
All client states.
Definition client.h:59
@ STATE_CONNECT_SERVER
Definition client.h:63
@ STATE_CHOOSE_SERVER
Definition client.h:62
@ STATE_LOAD_DATA
Definition client.h:69
@ STATE_SWITCH_SERVER
Definition client.h:89
@ STATE_EXIT
Definition client.h:94
@ STATE_LOGIN_ATTEMPT
Definition client.h:65
@ STATE_UPDATE
Definition client.h:68
@ STATE_GAME
Definition client.h:73
@ STATE_FORCE_QUIT
Definition client.h:95
@ STATE_LOGIN
Definition client.h:64
#define _(s)
Definition gettext.h:38
@ ENTER
Definition sdlinput.h:78
@ UP
Definition sdlinput.h:121
@ ESCAPE
Definition sdlinput.h:96
@ DOWN
Definition sdlinput.h:122
CharHandler * getCharHandler()
Definition net.cpp:65