ManaPlus
quitdialog.cpp
Go to the documentation of this file.
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/quitdialog.h"
25 
26 #include "client.h"
27 #include "configuration.h"
28 #include "soundmanager.h"
29 
30 #include "const/sound.h"
31 
32 #include "gui/dialogsmanager.h"
33 #include "gui/viewport.h"
34 
36 #include "gui/widgets/button.h"
38 
39 #include "net/charserverhandler.h"
40 #include "net/gamehandler.h"
41 
42 #include "resources/map/map.h"
43 
44 #include "utils/delete2.h"
45 #include "utils/gettext.h"
46 #include "utils/process.h"
47 
48 #include "debug.h"
49 
50 QuitDialog::QuitDialog(QuitDialog **const pointerToMe) :
51  // TRANSLATORS: quit dialog name
52  Window(_("Quit"), Modal_true, nullptr, "quit.xml"),
54  KeyListener(),
55  mOptions(),
56  // TRANSLATORS: quit dialog button
57  mLogoutQuit(new RadioButton(this, _("Quit"), "quitdialog", false)),
58  // TRANSLATORS: quit dialog button
59  mForceQuit(new RadioButton(this, _("Quit"), "quitdialog", false)),
60  mSwitchAccountServer(new RadioButton(this,
61  // TRANSLATORS: quit dialog button
62  _("Switch server"), "quitdialog", false)),
63  mSwitchCharacter(new RadioButton(this,
64  // TRANSLATORS: quit dialog button
65  _("Switch character"), "quitdialog", false)),
66  mRate(nullptr),
67  // TRANSLATORS: quit dialog button
68  mOkButton(new Button(this, _("OK"), "ok", BUTTON_SKIN, this)),
69  // TRANSLATORS: quit dialog button
70  mCancelButton(new Button(this, _("Cancel"), "cancel", BUTTON_SKIN, this)),
71  mMyPointer(pointerToMe),
72  mNeedForceQuit(false)
73 {
74  addKeyListener(this);
75 
76  ContainerPlacer placer = getPlacer(0, 0);
77  const StateT state = client->getState();
79  || state == State::CONNECT_SERVER || state == State::LOGIN
80  || state == State::PRE_LOGIN || state == State::LOGIN_ATTEMPT
81  || state == State::UPDATE || state == State::LOAD_DATA);
82 
83  // All states, when we're not logged in to someone.
84  if (mNeedForceQuit)
85  {
86  placeOption(placer, mForceQuit);
87  }
88  else
89  {
90  // Only added if we are connected to an accountserver or gameserver
91  placeOption(placer, mLogoutQuit);
93 
94  // Only added if we are connected to a gameserver
95  if (state == State::GAME)
97  }
98 
99 /*
100 #ifdef ANDROID
101  if (config.getBoolValue("rated") == false
102  && config.getIntValue("gamecount") > 3)
103  {
104  // TRANSLATORS: rate button
105  mRate = new RadioButton(this, _("Rate in google play"), "quitdialog",
106  false);
107  placeOption(placer, mRate);
108  mOptions[mOptions.size() - 1]->setSelected(true);
109  }
110  else
111 #endif
112 */
113  {
114  mOptions[0]->setSelected(true);
115  }
116 
117  placer = getPlacer(0, 1);
118  placer(1, 0, mOkButton, 1, 1);
119  placer(2, 0, mCancelButton, 1, 1);
120  reflowLayout(200, 0);
121 }
122 
124 {
131 }
132 
134 {
135  if (mMyPointer != nullptr)
136  *mMyPointer = nullptr;
141 }
142 
144  RadioButton *const option)
145 {
146  placer(0, CAST_S32(mOptions.size()), option, 3, 1);
147  mOptions.push_back(option);
148 }
149 
150 void QuitDialog::action(const ActionEvent &event)
151 {
153  if (event.getId() == "ok")
154  {
155  if (viewport != nullptr)
156  {
157  const Map *const map = viewport->getMap();
158  if (map != nullptr)
159  map->saveExtraLayer();
160  }
161 
162  if (mForceQuit->isSelected())
163  {
165  }
166  else if (mLogoutQuit->isSelected())
167  {
170  }
171  else if ((mRate != nullptr) && mRate->isSelected())
172  {
173  openBrowser("https://play.google.com/store/apps/details?"
174  "id=org.evolonline.beta.manaplus");
175  config.setValue("rated", true);
176  if (mNeedForceQuit)
177  {
179  }
180  else
181  {
184  }
185  }
186  else if (gameHandler->isConnected()
188  {
191  }
192  else if (mSwitchCharacter->isSelected())
193  {
194  if (client->getState() == State::GAME)
195  {
199  }
200  }
201  }
202  scheduleDelete();
203 }
204 
206 {
207  const InputActionT actionId = event.getActionId();
208  int dir = 0;
209 
210  PRAGMA45(GCC diagnostic push)
211  PRAGMA45(GCC diagnostic ignored "-Wswitch-enum")
212  switch (actionId)
213  {
217  break;
220  break;
221  case InputAction::GUI_UP:
222  dir = -1;
223  break;
225  dir = 1;
226  break;
227  default:
228  break;
229  }
230  PRAGMA45(GCC diagnostic pop)
231 
232  if (dir != 0)
233  {
234  STD_VECTOR<RadioButton*>::const_iterator it = mOptions.begin();
235  const STD_VECTOR<RadioButton*>::const_iterator
236  it_end = mOptions.end();
237 
238  for (; it < it_end; ++it)
239  {
240  if ((*it)->isSelected())
241  break;
242  }
243 
244  if (it == mOptions.end())
245  {
246  if (mOptions[0] != nullptr)
247  mOptions[0]->setSelected(true);
248  return;
249  }
250  else if (it == mOptions.begin() && dir < 0)
251  {
252  it = mOptions.end();
253  }
254 
255  it += dir;
256 
257  if (it == mOptions.end())
258  it = mOptions.begin();
259 
260  (*it)->setSelected(true);
261  }
262 }
const std::string BUTTON_SKIN
Definition: button.h:89
#define CAST_S32
Definition: cast.h:30
Net::CharServerHandler * charServerHandler
Definition: net.cpp:85
const std::string & getId() const
Definition: actionevent.h:122
Definition: button.h:102
void setState(const StateT state)
Definition: client.h:66
StateT getState() const
Definition: client.h:69
void setValue(const std::string &key, const std::string &value)
static void closeDialogs()
Definition: map.h:75
void saveExtraLayer() const
Definition: map.cpp:1226
virtual void switchCharacter() const =0
virtual bool isConnected() const =0
RadioButton * mSwitchAccountServer
Definition: quitdialog.h:75
QuitDialog ** mMyPointer
Definition: quitdialog.h:81
bool mNeedForceQuit
Definition: quitdialog.h:82
RadioButton * mSwitchCharacter
Definition: quitdialog.h:76
void postInit()
Definition: quitdialog.cpp:123
Button * mOkButton
Definition: quitdialog.h:78
RadioButton * mForceQuit
Definition: quitdialog.h:74
void placeOption(ContainerPlacer &placer, RadioButton *const option)
Definition: quitdialog.cpp:143
Button * mCancelButton
Definition: quitdialog.h:79
RadioButton * mRate
Definition: quitdialog.h:77
void keyPressed(KeyEvent &event)
Definition: quitdialog.cpp:205
std::vector< RadioButton * > mOptions
Definition: quitdialog.h:71
void action(const ActionEvent &event)
Definition: quitdialog.cpp:150
QuitDialog(QuitDialog **const pointerToMe)
Definition: quitdialog.cpp:50
RadioButton * mLogoutQuit
Definition: quitdialog.h:73
bool isSelected() const
Definition: radiobutton.h:142
void playGuiSound(const std::string &name)
Map * getMap() const
Definition: viewport.h:135
void requestModalFocus()
Definition: widget.cpp:380
void addKeyListener(KeyListener *const keyListener)
Definition: widget.cpp:272
virtual void requestFocus()
Definition: widget.cpp:204
Widget * getParent() const
Definition: widget.h:202
const std::string & getActionEventId() const
Definition: widget.h:605
Definition: window.h:102
ContainerPlacer getPlacer(const int x, const int y)
Definition: window.cpp:1391
virtual void setVisible(Visible visible)
Definition: window.cpp:778
void reflowLayout(int w, int h)
Definition: window.cpp:1396
void postInit()
Definition: window.cpp:249
virtual void scheduleDelete()
Definition: window.cpp:831
void setLocationRelativeTo(const Widget *const widget)
Definition: window.cpp:509
Configuration config
Configuration serverConfig
#define new
Definition: debug_new.h:147
#define delete2(var)
Definition: delete2.h:25
Client * client
Definition: client.cpp:118
Viewport * viewport
Definition: viewport.cpp:36
Net::GameHandler * gameHandler
Definition: net.cpp:91
#define _(s)
Definition: gettext.h:35
PRAGMA45(GCC diagnostic push) PRAGMA45(GCC diagnostic ignored "-Wredundant-decls") PRAGMA45(GCC diagnostic pop) class TestMain
InputAction ::T InputActionT
Definition: inputaction.h:717
#define nullptr
Definition: localconsts.h:45
const bool Modal_true
Definition: modal.h:30
@ CHOOSE_SERVER
Definition: state.h:37
@ GAME
Definition: state.h:49
@ UPDATE
Definition: state.h:44
@ LOGIN_ATTEMPT
Definition: state.h:41
@ FORCE_QUIT
Definition: state.h:68
@ SWITCH_SERVER
Definition: state.h:62
@ LOAD_DATA
Definition: state.h:45
@ PRE_LOGIN
Definition: state.h:39
@ CONNECT_SERVER
Definition: state.h:38
@ EXIT
Definition: state.h:67
@ LOGIN
Definition: state.h:40
ItemOptionDb::OptionInfos mOptions
bool openBrowser(std::string url)
Definition: process.cpp:310
static const std::string SOUND_HIDE_WINDOW
Definition: sound.h:38
static const std::string SOUND_SHOW_WINDOW
Definition: sound.h:37
SoundManager soundManager
State ::T StateT
Definition: state.h:71
const bool Visible_true
Definition: visible.h:30