00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gui/char_select.h"
00023
00024 #include "gui/charcreatedialog.h"
00025 #include "gui/confirm_dialog.h"
00026 #include "gui/ok_dialog.h"
00027 #include "gui/playerbox.h"
00028
00029 #ifdef TMWSERV_SUPPORT
00030 #include "gui/widgets/radiobutton.h"
00031 #include "gui/widgets/slider.h"
00032
00033 #include "gui/unregisterdialog.h"
00034 #include "gui/changepassworddialog.h"
00035 #include "gui/changeemaildialog.h"
00036
00037 #include "logindata.h"
00038
00039 #include "net/tmwserv/accountserver/account.h"
00040 #endif
00041
00042 #include "gui/widgets/button.h"
00043 #include "gui/widgets/label.h"
00044 #include "gui/widgets/layout.h"
00045 #include "gui/widgets/textfield.h"
00046
00047 #include "game.h"
00048 #include "localplayer.h"
00049 #include "main.h"
00050 #include "units.h"
00051
00052 #include "net/charhandler.h"
00053 #include "net/messageout.h"
00054 #include "net/net.h"
00055
00056 #include "resources/colordb.h"
00057
00058 #include "utils/gettext.h"
00059 #include "utils/strprintf.h"
00060 #include "utils/stringutils.h"
00061
00062 #include <guichan/font.hpp>
00063
00064 #include <string>
00065
00069 class CharDeleteConfirm : public ConfirmDialog
00070 {
00071 public:
00072 CharDeleteConfirm(CharSelectDialog *master);
00073 void action(const gcn::ActionEvent &event);
00074 private:
00075 CharSelectDialog *master;
00076 };
00077
00078 CharDeleteConfirm::CharDeleteConfirm(CharSelectDialog *m):
00079 ConfirmDialog(_("Confirm Character Delete"),
00080 _("Are you sure you want to delete this character?"), m),
00081 master(m)
00082 {
00083 }
00084
00085 void CharDeleteConfirm::action(const gcn::ActionEvent &event)
00086 {
00087
00088 if (event.getId() == "yes")
00089 {
00090 master->attemptCharDelete();
00091 }
00092 ConfirmDialog::action(event);
00093 }
00094
00095 #ifdef TMWSERV_SUPPORT
00096 CharSelectDialog::CharSelectDialog(LockedArray<LocalPlayer*> *charInfo,
00097 LoginData *loginData):
00098 Window(_("Account and Character Management")),
00099 mCharInfo(charInfo),
00100 mCharSelected(false),
00101 mLoginData(loginData)
00102 #else
00103 CharSelectDialog::CharSelectDialog(LockedArray<LocalPlayer*> *charInfo,
00104 Gender gender):
00105 Window(_("Select Character")),
00106 mCharInfo(charInfo),
00107 mCharSelected(false),
00108 mGender(gender)
00109 #endif
00110 {
00111 mSelectButton = new Button(_("Ok"), "ok", this);
00112 mCancelButton = new Button(_("Cancel"), "cancel", this);
00113 mPreviousButton = new Button(_("Previous"), "previous", this);
00114 mNextButton = new Button(_("Next"), "next", this);
00115 mNameLabel = new Label(strprintf(_("Name: %s"), ""));
00116 mLevelLabel = new Label(strprintf(_("Level: %d"), 0));
00117 #ifdef TMWSERV_SUPPORT
00118 mNewCharButton = new Button(_("New"), "new", this);
00119 mDelCharButton = new Button(_("Delete"), "delete", this);
00120 mUnRegisterButton = new Button(_("Unregister"), "unregister", this);
00121 mChangePasswordButton = new Button(_("Change Password"), "change_password", this);
00122 mChangeEmailButton = new Button(_("Change Email Address"), "change_email", this);
00123
00124 mAccountNameLabel = new Label(strprintf(_("Account: %s"), mLoginData->username.c_str()));
00125 mNameLabel = new Label(strprintf(_("Name: %s"), ""));
00126 mLevelLabel = new Label(strprintf(_("Level: %d"), 0));
00127 mMoneyLabel = new Label(strprintf(_("Money: %d"), 0));
00128
00129
00130 mPlayerBox = new PlayerBox;
00131 mPlayerBox->setWidth(74);
00132
00133 ContainerPlacer place;
00134 place = getPlacer(0, 0);
00135 place(0, 0, mAccountNameLabel);
00136 place(0, 1, mUnRegisterButton);
00137 place(0, 2, mChangePasswordButton);
00138 place(1, 2, mChangeEmailButton);
00139 place = getPlacer(0, 1);
00140 place(0, 0, mPlayerBox, 1, 5).setPadding(3);
00141 place(1, 0, mNameLabel, 3);
00142 place(1, 1, mLevelLabel, 3);
00143 place(1, 2, mMoneyLabel, 3);
00144 place(1, 3, mPreviousButton);
00145 place(2, 3, mNextButton);
00146 place(1, 4, mNewCharButton);
00147 place(2, 4, mDelCharButton);
00148 place.getCell().matchColWidth(1, 2);
00149 place = getPlacer(0, 2);
00150 place(0, 0, mSelectButton);
00151 place(1, 0, mCancelButton);
00152 reflowLayout(265, 0);
00153 #else
00154 mCharInfo->select(0);
00155 LocalPlayer *pi = mCharInfo->getEntry();
00156 if (pi)
00157 mMoney = Units::formatCurrency(pi->getMoney());
00158
00159 mPlayerBox = new PlayerBox;
00160 mPlayerBox->setWidth(74);
00161
00162 mJobLevelLabel = new Label(strprintf(_("Job Level: %d"), 0));
00163 mMoneyLabel = new Label(strprintf(_("Money: %s"), mMoney.c_str()));
00164
00165 const std::string tempString = getFont()->getWidth(_("New")) <
00166 getFont()->getWidth(_("Delete")) ?
00167 _("Delete") : _("New");
00168
00169 mNewDelCharButton = new Button(tempString, "newdel", this);
00170
00171 ContainerPlacer place;
00172 place = getPlacer(0, 0);
00173
00174 place(0, 0, mPlayerBox, 1, 6).setPadding(3);
00175 place(1, 0, mNewDelCharButton);
00176 place(1, 1, mNameLabel, 5);
00177 place(1, 2, mLevelLabel, 5);
00178 place(1, 3, mJobLevelLabel, 5);
00179 place(1, 4, mMoneyLabel, 5);
00180 place.getCell().matchColWidth(1, 4);
00181 place = getPlacer(0, 2);
00182 place(0, 0, mPreviousButton);
00183 place(1, 0, mNextButton);
00184 place(4, 0, mCancelButton);
00185 place(5, 0, mSelectButton);
00186
00187 reflowLayout(250, 0);
00188 #endif
00189
00190 center();
00191 setVisible(true);
00192 mSelectButton->requestFocus();
00193 updatePlayerInfo();
00194 }
00195
00196 void CharSelectDialog::action(const gcn::ActionEvent &event)
00197 {
00198 #ifdef TMWSERV_SUPPORT
00199
00200 if (event.getId() == "ok" && (mCharInfo->getEntry()))
00201 #else
00202 if (event.getId() == "ok" && n_character > 0)
00203 #endif
00204 {
00205
00206 #ifdef TMWSERV_SUPPORT
00207 mNewCharButton->setEnabled(false);
00208 mDelCharButton->setEnabled(false);
00209 mUnRegisterButton->setEnabled(false);
00210 #else
00211 mNewDelCharButton->setEnabled(false);
00212 #endif
00213 mSelectButton->setEnabled(false);
00214 mPreviousButton->setEnabled(false);
00215 mNextButton->setEnabled(false);
00216 mCharSelected = true;
00217 attemptCharSelect();
00218 }
00219 else if (event.getId() == "cancel")
00220 {
00221 #ifdef TMWSERV_SUPPORT
00222 mCharInfo->clear();
00223 state = STATE_SWITCH_ACCOUNTSERVER_ATTEMPT;
00224 #else
00225 state = STATE_EXIT;
00226 #endif
00227 }
00228 #ifdef TMWSERV_SUPPORT
00229 else if (event.getId() == "new")
00230 {
00231
00232
00233 if (!(mCharInfo->getEntry()))
00234 {
00235
00236 CharCreateDialog *charCreateDialog =
00237 new CharCreateDialog(this, mCharInfo->getPos());
00238 Net::getCharHandler()->setCharCreateDialog(charCreateDialog);
00239 }
00240 }
00241 else if (event.getId() == "delete")
00242 {
00243
00244 if (mCharInfo->getEntry())
00245 {
00246 new CharDeleteConfirm(this);
00247 }
00248 }
00249 #else
00250 else if (event.getId() == "newdel")
00251 {
00252
00253 if (mCharInfo->getEntry())
00254 {
00255 new CharDeleteConfirm(this);
00256 }
00257 else if (n_character <= maxSlot)
00258 {
00259
00260 CharCreateDialog *charCreateDialog =
00261 new CharCreateDialog(this, mCharInfo->getPos());
00262 Net::getCharHandler()->setCharCreateDialog(charCreateDialog);
00263 }
00264 }
00265 #endif
00266 else if (event.getId() == "previous")
00267 {
00268 mCharInfo->prev();
00269 LocalPlayer *pi = mCharInfo->getEntry();
00270 if (pi)
00271 mMoney = Units::formatCurrency(pi->getMoney());
00272 }
00273 else if (event.getId() == "next")
00274 {
00275 mCharInfo->next();
00276 LocalPlayer *pi = mCharInfo->getEntry();
00277 if (pi)
00278 mMoney = Units::formatCurrency(pi->getMoney());
00279 }
00280 #ifdef TMWSERV_SUPPORT
00281 else if (event.getId() == "unregister")
00282 {
00283 new UnRegisterDialog(this, mLoginData);
00284 }
00285 else if (event.getId() == "change_password")
00286 {
00287 new ChangePasswordDialog(this, mLoginData);
00288 }
00289 else if (event.getId() == "change_email")
00290 {
00291 new ChangeEmailDialog(this, mLoginData);
00292 }
00293 #endif
00294 }
00295
00296 void CharSelectDialog::updatePlayerInfo()
00297 {
00298 LocalPlayer *pi = mCharInfo->getEntry();
00299
00300 if (pi)
00301 {
00302 mNameLabel->setCaption(strprintf(_("Name: %s"),
00303 pi->getName().c_str()));
00304 mLevelLabel->setCaption(strprintf(_("Level: %d"), pi->getLevel()));
00305 #ifndef TMWSERV_SUPPORT
00306 mJobLevelLabel->setCaption(strprintf(_("Job Level: %d"),
00307 pi->mJobLevel));
00308 #endif
00309 mMoneyLabel->setCaption(strprintf(_("Money: %s"), mMoney.c_str()));
00310 if (!mCharSelected)
00311 {
00312 #ifdef TMWSERV_SUPPORT
00313 mNewCharButton->setEnabled(false);
00314 mDelCharButton->setEnabled(true);
00315 #else
00316 mNewDelCharButton->setCaption(_("Delete"));
00317 #endif
00318 mSelectButton->setEnabled(true);
00319 }
00320 }
00321 else
00322 {
00323 mNameLabel->setCaption(strprintf(_("Name: %s"), ""));
00324 mLevelLabel->setCaption(strprintf(_("Level: %d"), 0));
00325 #ifndef TMWSERV_SUPPORT
00326 mJobLevelLabel->setCaption(strprintf(_("Job Level: %d"), 0));
00327 #endif
00328 mMoneyLabel->setCaption(strprintf(_("Money: %s"), ""));
00329 #ifdef TMWSERV_SUPPORT
00330 mNewCharButton->setEnabled(true);
00331 mDelCharButton->setEnabled(false);
00332 #else
00333 mNewDelCharButton->setCaption(_("New"));
00334 #endif
00335 mSelectButton->setEnabled(false);
00336 }
00337
00338 mPlayerBox->setPlayer(pi);
00339 }
00340
00341 void CharSelectDialog::attemptCharDelete()
00342 {
00343 Net::getCharHandler()->deleteCharacter(mCharInfo->getPos(), mCharInfo->getEntry());
00344 mCharInfo->lock();
00345 }
00346
00347 void CharSelectDialog::attemptCharSelect()
00348 {
00349 Net::getCharHandler()->chooseCharacter(mCharInfo->getPos(), mCharInfo->getEntry());
00350 mCharInfo->lock();
00351 }
00352
00353 void CharSelectDialog::logic()
00354 {
00355 updatePlayerInfo();
00356 }
00357
00358 bool CharSelectDialog::selectByName(const std::string &name)
00359 {
00360 if (mCharInfo->isLocked())
00361 return false;
00362
00363 unsigned int oldPos = mCharInfo->getPos();
00364
00365 mCharInfo->select(0);
00366 do
00367 {
00368 LocalPlayer *player = mCharInfo->getEntry();
00369
00370 if (player && player->getName() == name)
00371 return true;
00372
00373 mCharInfo->next();
00374 } while (mCharInfo->getPos());
00375
00376 mCharInfo->select(oldPos);
00377
00378 return false;
00379 }