00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "gui/setup_players.h"
00023
00024 #include "gui/ok_dialog.h"
00025 #include "gui/table.h"
00026
00027 #include "gui/widgets/button.h"
00028 #include "gui/widgets/checkbox.h"
00029 #include "gui/widgets/dropdown.h"
00030 #include "gui/widgets/label.h"
00031 #include "gui/widgets/layouthelper.h"
00032 #include "gui/widgets/scrollarea.h"
00033
00034 #include "configuration.h"
00035 #include "log.h"
00036
00037 #include "utils/gettext.h"
00038
00039 #include <string>
00040 #include <vector>
00041
00042 #define COLUMNS_NR 2 // name plus listbox
00043 #define NAME_COLUMN 0
00044 #define RELATION_CHOICE_COLUMN 1
00045
00046 #define ROW_HEIGHT 12
00047
00048
00049 #define NAME_COLUMN_WIDTH 230
00050 #define RELATION_CHOICE_COLUMN_WIDTH 80
00051
00052 #define WIDGET_AT(row, column) (((row) * COLUMNS_NR) + column)
00053
00054 static const char *table_titles[COLUMNS_NR] =
00055 {
00056 N_("Name"),
00057 N_("Relation")
00058 };
00059
00060 static const char *RELATION_NAMES[PlayerRelation::RELATIONS_NR] =
00061 {
00062 N_("Neutral"),
00063 N_("Friend"),
00064 N_("Disregarded"),
00065 N_("Ignored")
00066 };
00067
00068 class PlayerRelationListModel : public gcn::ListModel
00069 {
00070 public:
00071 virtual ~PlayerRelationListModel() { }
00072
00073 virtual int getNumberOfElements()
00074 {
00075 return PlayerRelation::RELATIONS_NR;
00076 }
00077
00078 virtual std::string getElementAt(int i)
00079 {
00080 if (i >= getNumberOfElements() || i < 0)
00081 return "";
00082 return gettext(RELATION_NAMES[i]);
00083 }
00084 };
00085
00086 class PlayerTableModel : public TableModel
00087 {
00088 public:
00089 PlayerTableModel() :
00090 mPlayers(NULL)
00091 {
00092 playerRelationsUpdated();
00093 }
00094
00095 virtual ~PlayerTableModel()
00096 {
00097 freeWidgets();
00098 if (mPlayers)
00099 delete mPlayers;
00100 }
00101
00102 virtual int getRows()
00103 {
00104 return mPlayers->size();
00105 }
00106
00107 virtual int getColumns()
00108 {
00109 return COLUMNS_NR;
00110 }
00111
00112 virtual int getRowHeight()
00113 {
00114 return ROW_HEIGHT;
00115 }
00116
00117 virtual int getColumnWidth(int index)
00118 {
00119 if (index == NAME_COLUMN)
00120 return NAME_COLUMN_WIDTH;
00121 else
00122 return RELATION_CHOICE_COLUMN_WIDTH;
00123 }
00124
00125 virtual void playerRelationsUpdated()
00126 {
00127 signalBeforeUpdate();
00128
00129 freeWidgets();
00130 std::vector<std::string> *player_names = player_relations.getPlayers();
00131 if (mPlayers)
00132 delete mPlayers;
00133 mPlayers = player_names;
00134
00135
00136 for (unsigned int r = 0; r < player_names->size(); ++r)
00137 {
00138 std::string name = (*player_names)[r];
00139 gcn::Widget *widget = new Label(name);
00140 mWidgets.push_back(widget);
00141 gcn::ListModel *playerRelation = new PlayerRelationListModel;
00142
00143 gcn::DropDown *choicebox = new DropDown(playerRelation);
00144 choicebox->setSelected(player_relations.getRelation(name));
00145 mWidgets.push_back(choicebox);
00146 }
00147
00148 signalAfterUpdate();
00149 }
00150
00151 virtual void updateModelInRow(int row)
00152 {
00153 gcn::DropDown *choicebox = dynamic_cast<gcn::DropDown *>(
00154 getElementAt(row, RELATION_CHOICE_COLUMN));
00155 player_relations.setRelation(getPlayerAt(row),
00156 static_cast<PlayerRelation::Relation>(
00157 choicebox->getSelected()));
00158 }
00159
00160
00161 virtual gcn::Widget *getElementAt(int row, int column)
00162 {
00163 return mWidgets[WIDGET_AT(row, column)];
00164 }
00165
00166 virtual void freeWidgets()
00167 {
00168 if (mPlayers)
00169 delete mPlayers;
00170 mPlayers = NULL;
00171
00172 for (std::vector<gcn::Widget *>::const_iterator it = mWidgets.begin();
00173 it != mWidgets.end(); it++)
00174 {
00175 delete *it;
00176 }
00177
00178 mWidgets.clear();
00179 }
00180
00181 std::string getPlayerAt(int index)
00182 {
00183 return (*mPlayers)[index];
00184 }
00185
00186 protected:
00187 std::vector<std::string> *mPlayers;
00188 std::vector<gcn::Widget *> mWidgets;
00189 };
00190
00194 class IgnoreChoicesListModel : public gcn::ListModel
00195 {
00196 public:
00197 virtual ~IgnoreChoicesListModel() { }
00198
00199 virtual int getNumberOfElements()
00200 {
00201 return player_relations.getPlayerIgnoreStrategies()->size();
00202 }
00203
00204 virtual std::string getElementAt(int i)
00205 {
00206 if (i >= getNumberOfElements())
00207 return _("???");
00208
00209 return (*player_relations.getPlayerIgnoreStrategies())[i]->mDescription;
00210 }
00211 };
00212
00213 #define ACTION_DELETE "delete"
00214 #define ACTION_TABLE "table"
00215 #define ACTION_STRATEGY "strategy"
00216 #define ACTION_WHISPER_TAB "whisper tab"
00217
00218 Setup_Players::Setup_Players():
00219 mPlayerTableTitleModel(new StaticTableModel(1, COLUMNS_NR)),
00220 mPlayerTableModel(new PlayerTableModel),
00221 mPlayerTable(new GuiTable(mPlayerTableModel)),
00222 mPlayerTitleTable(new GuiTable(mPlayerTableTitleModel)),
00223 mPlayerScrollArea(new ScrollArea(mPlayerTable)),
00224 mPersistIgnores(new CheckBox(_("Save player list"),
00225 player_relations.getPersistIgnores())),
00226 mDefaultTrading(new CheckBox(_("Allow trading"),
00227 player_relations.getDefault() & PlayerRelation::TRADE)),
00228 mDefaultWhisper(new CheckBox(_("Allow whispers"),
00229 player_relations.getDefault() & PlayerRelation::WHISPER)),
00230 mDeleteButton(new Button(_("Delete"), ACTION_DELETE, this)),
00231 mWhisperTab(config.getValue("whispertab", false)),
00232 mWhisperTabCheckBox(new CheckBox(_("Put all whispers in tabs"), mWhisperTab))
00233 {
00234 setName(_("Players"));
00235
00236 mPlayerTable->setOpaque(false);
00237
00238 mPlayerTableTitleModel->fixColumnWidth(NAME_COLUMN, NAME_COLUMN_WIDTH);
00239 mPlayerTableTitleModel->fixColumnWidth(RELATION_CHOICE_COLUMN,
00240 RELATION_CHOICE_COLUMN_WIDTH);
00241 mPlayerTitleTable->setBackgroundColor(gcn::Color(0xbf, 0xbf, 0xbf));
00242
00243 mIgnoreActionChoicesModel = new IgnoreChoicesListModel;
00244 mIgnoreActionChoicesBox = new DropDown(mIgnoreActionChoicesModel);
00245
00246 for (int i = 0; i < COLUMNS_NR; i++)
00247 {
00248 mPlayerTableTitleModel->set(0, i,
00249 new Label(gettext(table_titles[i])));
00250 }
00251
00252 mPlayerTitleTable->setLinewiseSelection(true);
00253
00254 mPlayerScrollArea->setHorizontalScrollPolicy(gcn::ScrollArea::SHOW_NEVER);
00255 mPlayerTable->setActionEventId(ACTION_TABLE);
00256 mPlayerTable->setLinewiseSelection(true);
00257 mPlayerTable->addActionListener(this);
00258
00259 gcn::Label *ignore_action_label = new Label(_("When ignoring:"));
00260
00261 mIgnoreActionChoicesBox->setActionEventId(ACTION_STRATEGY);
00262 mIgnoreActionChoicesBox->addActionListener(this);
00263
00264 int ignore_strategy_index = 0;
00265
00266 if (player_relations.getPlayerIgnoreStrategy())
00267 {
00268 ignore_strategy_index = player_relations.getPlayerIgnoreStrategyIndex(
00269 player_relations.getPlayerIgnoreStrategy()->mShortName);
00270 if (ignore_strategy_index < 0)
00271 ignore_strategy_index = 0;
00272 }
00273 mIgnoreActionChoicesBox->setSelected(ignore_strategy_index);
00274 mIgnoreActionChoicesBox->adjustHeight();
00275
00276 mWhisperTabCheckBox->setActionEventId(ACTION_WHISPER_TAB);
00277 mWhisperTabCheckBox->addActionListener(this);
00278
00279 reset();
00280
00281
00282 LayoutHelper h(this);
00283 ContainerPlacer place = h.getPlacer(0, 0);
00284
00285 place(0, 0, mPlayerTitleTable, 4);
00286 place(0, 1, mPlayerScrollArea, 4, 4).setPadding(2);
00287 place(0, 5, mDeleteButton);
00288 place(0, 6, mWhisperTabCheckBox);
00289 place(2, 5, ignore_action_label);
00290 place(2, 6, mIgnoreActionChoicesBox, 2).setPadding(2);
00291 place(2, 7, mPersistIgnores);
00292 place(2, 8, mDefaultTrading);
00293 place(2, 9, mDefaultWhisper);
00294
00295 player_relations.addListener(this);
00296
00297 setDimension(gcn::Rectangle(0, 0, 325, 280));
00298 }
00299
00300 Setup_Players::~Setup_Players()
00301 {
00302 player_relations.removeListener(this);
00303 delete mIgnoreActionChoicesModel;
00304 }
00305
00306
00307 void Setup_Players::reset()
00308 {
00309
00310
00311
00312 int selection = 0;
00313 for (unsigned int i = 0;
00314 i < player_relations.getPlayerIgnoreStrategies()->size();
00315 ++i)
00316 if ((*player_relations.getPlayerIgnoreStrategies())[i] ==
00317 player_relations.getPlayerIgnoreStrategy())
00318 {
00319
00320 selection = i;
00321 break;
00322 }
00323
00324 mIgnoreActionChoicesBox->setSelected(selection);
00325 }
00326
00327 void Setup_Players::apply()
00328 {
00329 player_relations.setPersistIgnores(mPersistIgnores->isSelected());
00330 player_relations.store();
00331
00332 unsigned int old_default_relations = player_relations.getDefault() &
00333 ~(PlayerRelation::TRADE |
00334 PlayerRelation::WHISPER);
00335 player_relations.setDefault(old_default_relations
00336 | (mDefaultTrading->isSelected() ?
00337 PlayerRelation::TRADE : 0)
00338 | (mDefaultWhisper->isSelected() ?
00339 PlayerRelation::WHISPER : 0));
00340 config.setValue("whispertab", mWhisperTab);
00341 }
00342
00343 void Setup_Players::cancel()
00344 {
00345 mWhisperTab = config.getValue("whispertab", false);
00346 mWhisperTabCheckBox->setSelected(mWhisperTab);
00347 }
00348
00349 void Setup_Players::action(const gcn::ActionEvent &event)
00350 {
00351 if (event.getId() == ACTION_TABLE)
00352 {
00353
00354
00355
00356
00357 player_relations.removeListener(this);
00358
00359 int row = mPlayerTable->getSelectedRow();
00360 if (row >= 0)
00361 mPlayerTableModel->updateModelInRow(row);
00362
00363 player_relations.addListener(this);
00364
00365 }
00366 else if (event.getId() == ACTION_DELETE)
00367 {
00368 int player_index = mPlayerTable->getSelectedRow();
00369
00370 if (player_index < 0)
00371 return;
00372
00373 std::string name = mPlayerTableModel->getPlayerAt(player_index);
00374
00375 player_relations.removePlayer(name);
00376
00377 }
00378 else if (event.getId() == ACTION_STRATEGY)
00379 {
00380 PlayerIgnoreStrategy *s =
00381 (*player_relations.getPlayerIgnoreStrategies())[
00382 mIgnoreActionChoicesBox->getSelected()];
00383
00384 player_relations.setPlayerIgnoreStrategy(s);
00385 }
00386 else if (event.getId() == ACTION_WHISPER_TAB)
00387 {
00388 mWhisperTab = mWhisperTabCheckBox->isSelected();
00389 }
00390 }
00391
00392 void Setup_Players::updatedPlayer(const std::string &name)
00393 {
00394 mPlayerTableModel->playerRelationsUpdated();
00395 mDefaultTrading->setSelected(
00396 player_relations.getDefault() & PlayerRelation::TRADE);
00397 mDefaultWhisper->setSelected(
00398 player_relations.getDefault() & PlayerRelation::WHISPER);
00399 }