| 1 |
// NS_Profile.cpp: implementation of the NS_Profile class. |
| 2 |
// |
| 3 |
////////////////////////////////////////////////////////////////////// |
| 4 |
|
| 5 |
#include "NS_Profile.h" |
| 6 |
#include <ClanLib/signals.h> |
| 7 |
#include <ClanLib/signals.h> |
| 8 |
#include <ClanLib/display.h> |
| 9 |
#include <ClanLib/display.h> |
| 10 |
#include <ClanLib/core.h> |
| 11 |
#include <ClanLib/core.h> |
| 12 |
|
| 13 |
#include "NS_MessageBox.h" |
| 14 |
|
| 15 |
#include <algorithm> |
| 16 |
#include <functional> |
| 17 |
|
| 18 |
////////////////////////////////////////////////////////////////////// |
| 19 |
// Construction/Destruction |
| 20 |
////////////////////////////////////////////////////////////////////// |
| 21 |
|
| 22 |
NS_Profile::NS_Profile(CL_Component * parent) |
| 23 |
: CL_Window(parent, parent->get_style_manager()), |
| 24 |
success(false) |
| 25 |
{ |
| 26 |
CL_ListBox * lst_players = new CL_ListBox(CL_Rect(15, 35, 350, 255), get_client_area()); |
| 27 |
CL_Button * btn_add = new CL_Button(CL_Point(15, 260), "New Player", get_client_area()); |
| 28 |
CL_Button * btn_del = new CL_Button(CL_Point(140, 260), "Delete", get_client_area()); |
| 29 |
CL_Button * btn_ok = new CL_Button(CL_Point(325, 260), "OK", get_client_area()); |
| 30 |
|
| 31 |
// Calc dialog position |
| 32 |
set_position(CL_Rect(100, 100, 477, 400)); |
| 33 |
set_title("Choose player"); |
| 34 |
|
| 35 |
// Add players to list |
| 36 |
lst_players->set_multi_selection(false); |
| 37 |
|
| 38 |
// if we have players, then show them in dialog |
| 39 |
lst_players->insert_item("zz|sergant"); |
| 40 |
lst_players->insert_item("Sphair"); |
| 41 |
lst_players->insert_item("Judas"); |
| 42 |
lst_players->insert_item("grumbel"); |
| 43 |
lst_players->insert_item("Blacky Gray"); |
| 44 |
lst_players->insert_item("carbon"); |
| 45 |
lst_players->insert_item("Supaplex"); |
| 46 |
lst_players->insert_item("Terminator"); |
| 47 |
|
| 48 |
lst_players->set_current_item(0); |
| 49 |
|
| 50 |
lst_players->sort(); |
| 51 |
lst_players->set_tab_id(1); |
| 52 |
|
| 53 |
btn_ok->find_preferred_size(); |
| 54 |
btn_del->find_preferred_size(); |
| 55 |
btn_add->find_preferred_size(); |
| 56 |
|
| 57 |
// Connect signals |
| 58 |
slots.connect(btn_ok->sig_clicked(), this, &NS_Profile::on_ok); |
| 59 |
slots.connect(btn_add->sig_clicked(), this, &NS_Profile::on_new_player); |
| 60 |
slots.connect(btn_del->sig_clicked(), this, &NS_Profile::on_delete_player); |
| 61 |
|
| 62 |
// Add components to component list |
| 63 |
components["btn_ok"] = btn_ok; |
| 64 |
components["players"] = lst_players; |
| 65 |
components["btn_add"] = btn_add; |
| 66 |
components["btn_del"] = btn_del; |
| 67 |
} |
| 68 |
|
| 69 |
bool NS_Profile::open(CL_Component * parent) |
| 70 |
{ |
| 71 |
NS_Profile dlg(parent); |
| 72 |
dlg.run(); |
| 73 |
return dlg.is_success(); |
| 74 |
} |
| 75 |
|
| 76 |
bool NS_Profile::is_success() const |
| 77 |
{ |
| 78 |
return success; |
| 79 |
} |
| 80 |
|
| 81 |
void NS_Profile::on_ok() |
| 82 |
{ |
| 83 |
// Select new default user |
| 84 |
// then quit |
| 85 |
quit(); |
| 86 |
} |
| 87 |
|
| 88 |
NS_Profile::~NS_Profile() |
| 89 |
{ |
| 90 |
std::map<std::string, CL_Component * >::iterator it(components.begin()); |
| 91 |
for(; it != components.end(); ++it) |
| 92 |
delete it->second; |
| 93 |
} |
| 94 |
|
| 95 |
// New Player |
| 96 |
namespace { |
| 97 |
|
| 98 |
class NS_New_Player : public CL_Window |
| 99 |
{ |
| 100 |
public: |
| 101 |
NS_New_Player(CL_Component * parent, std::string const & player_name = ""); |
| 102 |
~NS_New_Player(); |
| 103 |
public: |
| 104 |
void on_ok(); |
| 105 |
void on_cancel(); |
| 106 |
static bool open(CL_Component * parent, std::string & player_name); |
| 107 |
std::string const & get_player_name() const; |
| 108 |
void set_focus_on_input(); |
| 109 |
bool is_success() const; |
| 110 |
void on_key_down(CL_InputEvent const & key); |
| 111 |
private: |
| 112 |
std::map<std::string, CL_Component * > components; |
| 113 |
CL_SlotContainer slots; |
| 114 |
std::string old_player_name; |
| 115 |
bool success; |
| 116 |
}; |
| 117 |
|
| 118 |
NS_New_Player::NS_New_Player(CL_Component * parent, |
| 119 |
std::string const & player_name) |
| 120 |
: |
| 121 |
CL_Window(parent, parent->get_style_manager()), |
| 122 |
old_player_name(player_name), |
| 123 |
success(false) |
| 124 |
{ |
| 125 |
CL_Button * btn_cancel = new CL_Button(CL_Point(220, 75), "Cancel", get_client_area()); |
| 126 |
CL_Button * btn_ok = new CL_Button(CL_Point(180, 75), "OK", get_client_area()); |
| 127 |
CL_InputBox * input_name = new CL_InputBox(CL_Rect(15, 40, 270, 65), get_client_area()); |
| 128 |
|
| 129 |
// Calc dialog position |
| 130 |
set_position(CL_Rect(200, 150, 500, 265)); |
| 131 |
set_title("Enter player name"); |
| 132 |
|
| 133 |
// Connect signals |
| 134 |
slots.connect(btn_ok->sig_clicked(), this, &NS_New_Player::on_ok); |
| 135 |
slots.connect(btn_cancel->sig_clicked(), this, &NS_New_Player::on_cancel); |
| 136 |
slots.connect(CL_Keyboard::sig_key_down(), this, &NS_New_Player::on_key_down); |
| 137 |
|
| 138 |
// Add components to component list |
| 139 |
components["btn_ok"] = btn_ok; |
| 140 |
components["btn_cancel"] = btn_cancel; |
| 141 |
components["input_name"] = input_name; |
| 142 |
|
| 143 |
input_name->set_focus(); |
| 144 |
} |
| 145 |
|
| 146 |
NS_New_Player::~NS_New_Player() |
| 147 |
{ |
| 148 |
std::map<std::string, CL_Component * >::iterator it(components.begin()); |
| 149 |
for(; it != components.end(); ++it) |
| 150 |
delete it->second; |
| 151 |
} |
| 152 |
|
| 153 |
void NS_New_Player::on_key_down(CL_InputEvent const & key) |
| 154 |
{ |
| 155 |
if (key.id == CL_KEY_ESCAPE) |
| 156 |
on_cancel(); |
| 157 |
else |
| 158 |
if (key.id == CL_KEY_RETURN) |
| 159 |
on_ok(); |
| 160 |
} |
| 161 |
|
| 162 |
std::string const & NS_New_Player::get_player_name() const |
| 163 |
{ |
| 164 |
return old_player_name; |
| 165 |
} |
| 166 |
|
| 167 |
bool NS_New_Player::is_success() const |
| 168 |
{ |
| 169 |
return success; |
| 170 |
} |
| 171 |
|
| 172 |
void NS_New_Player::set_focus_on_input() |
| 173 |
{ |
| 174 |
components["input_name"]->set_focus(); |
| 175 |
} |
| 176 |
|
| 177 |
bool NS_New_Player::open(CL_Component * parent, |
| 178 |
std::string & player_name) |
| 179 |
{ |
| 180 |
NS_New_Player dlg(parent, player_name); |
| 181 |
dlg.set_focus_on_input(); |
| 182 |
dlg.run(); |
| 183 |
player_name = dlg.get_player_name(); |
| 184 |
return dlg.is_success(); |
| 185 |
} |
| 186 |
|
| 187 |
struct CharRemover : std::unary_function<char, bool> |
| 188 |
{ |
| 189 |
CharRemover(std::string const & wildcard) |
| 190 |
: wildcard(wildcard) |
| 191 |
{ |
| 192 |
} |
| 193 |
bool operator()(char ch) |
| 194 |
{ |
| 195 |
return wildcard.find(ch) == std::string::npos; |
| 196 |
} |
| 197 |
std::string const & wildcard; |
| 198 |
}; |
| 199 |
|
| 200 |
void NS_New_Player::on_ok() |
| 201 |
{ |
| 202 |
// Select new default user |
| 203 |
// then quit |
| 204 |
CL_InputBox * input_name = dynamic_cast<CL_InputBox*>(components["input_name"]); |
| 205 |
if (input_name) |
| 206 |
{ |
| 207 |
std::string str = input_name->get_text(); |
| 208 |
size_t pos = str.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890(),_"); |
| 209 |
if (pos != std::string::npos) |
| 210 |
str.erase(str.begin(), str.begin() + pos); |
| 211 |
pos = str.find_last_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890(),_"); |
| 212 |
if (pos != std::string::npos) |
| 213 |
str.erase(str.begin() + pos + 1, str.end()); |
| 214 |
|
| 215 |
str.erase(std::remove_if(str.begin(), str.end(), |
| 216 |
CharRemover("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890(),_ ")), |
| 217 |
str.end()); |
| 218 |
|
| 219 |
if (!str.empty()) |
| 220 |
success = true; |
| 221 |
old_player_name = str; |
| 222 |
} |
| 223 |
quit(); |
| 224 |
} |
| 225 |
|
| 226 |
void NS_New_Player::on_cancel() |
| 227 |
{ |
| 228 |
// Select new default user |
| 229 |
// then quit |
| 230 |
quit(); |
| 231 |
} |
| 232 |
|
| 233 |
} // end New Player namespace |
| 234 |
|
| 235 |
void NS_Profile::on_new_player() |
| 236 |
{ |
| 237 |
CL_ListBox * lst_players = dynamic_cast<CL_ListBox*>(components["players"]); |
| 238 |
if (lst_players) |
| 239 |
{ |
| 240 |
std::string old_player_name; |
| 241 |
if (NS_New_Player::open(get_gui_manager(), old_player_name)) |
| 242 |
// add player here |
| 243 |
{ |
| 244 |
if (!old_player_name.empty()) |
| 245 |
{ |
| 246 |
lst_players->insert_item(old_player_name); |
| 247 |
lst_players->set_current_item(lst_players->get_count() - 1); |
| 248 |
lst_players->sort(); |
| 249 |
} |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
void NS_Profile::on_delete_player() |
| 255 |
{ |
| 256 |
CL_ListBox * lst_players = dynamic_cast<CL_ListBox*>(components["players"]); |
| 257 |
if (lst_players) |
| 258 |
{ |
| 259 |
if (lst_players->get_count() > 0) |
| 260 |
{ |
| 261 |
if (lst_players->get_current_item() != -1) |
| 262 |
{ |
| 263 |
int result = NS_MessageBox::info(get_gui_manager(), "Deleting Player", |
| 264 |
"Are you sure ?\nInformation about all completed levels for player '" + lst_players->get_current_text() + "' will be lost!", |
| 265 |
"OK", "Cancel", ""); |
| 266 |
if (result == 1) |
| 267 |
{ |
| 268 |
// delete player |
| 269 |
lst_players->remove_item(lst_players->get_current_item()); |
| 270 |
if (lst_players->get_count()) |
| 271 |
{ |
| 272 |
lst_players->set_current_item(0); |
| 273 |
} |
| 274 |
else |
| 275 |
{ |
| 276 |
lst_players->set_current_item(-1); |
| 277 |
} |
| 278 |
} |
| 279 |
} |
| 280 |
} |
| 281 |
else |
| 282 |
{ |
| 283 |
NS_MessageBox::info(this, "Deleting Player", |
| 284 |
"You should add some players first", |
| 285 |
"OK", "", ""); |
| 286 |
} |
| 287 |
} |
| 288 |
} |