File: NS_Profile.cpp

package info (click to toggle)
clanlib 1.0~svn3827-7
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 24,632 kB
  • ctags: 16,580
  • sloc: cpp: 101,591; xml: 6,410; makefile: 1,743; ansic: 463; perl: 424; php: 247; sh: 53
file content (288 lines) | stat: -rw-r--r-- 8,140 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// NS_Profile.cpp: implementation of the NS_Profile class.
//
//////////////////////////////////////////////////////////////////////

#include "NS_Profile.h"
#include <ClanLib/signals.h>
#include <ClanLib/signals.h>
#include <ClanLib/display.h>
#include <ClanLib/display.h>
#include <ClanLib/core.h>
#include <ClanLib/core.h>

#include "NS_MessageBox.h"

#include <algorithm>
#include <functional>

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

NS_Profile::NS_Profile(CL_Component * parent)
    : CL_Window(parent, parent->get_style_manager()),
    success(false)
{
    CL_ListBox * lst_players = new CL_ListBox(CL_Rect(15, 35, 350, 255), get_client_area());
    CL_Button * btn_add = new CL_Button(CL_Point(15, 260), "New Player", get_client_area());
    CL_Button * btn_del = new CL_Button(CL_Point(140, 260), "Delete", get_client_area());
    CL_Button * btn_ok = new CL_Button(CL_Point(325, 260), "OK", get_client_area());

	// Calc dialog position
    set_position(CL_Rect(100, 100, 477, 400));
    set_title("Choose player"); 

    // Add players to list
    lst_players->set_multi_selection(false);

	// if we have players, then show them in dialog
	lst_players->insert_item("zz|sergant");
	lst_players->insert_item("Sphair");
	lst_players->insert_item("Judas");
	lst_players->insert_item("grumbel");
	lst_players->insert_item("Blacky Gray");
	lst_players->insert_item("carbon");
	lst_players->insert_item("Supaplex");
	lst_players->insert_item("Terminator");

	lst_players->set_current_item(0);

	lst_players->sort();        
    lst_players->set_tab_id(1);

    btn_ok->find_preferred_size();
    btn_del->find_preferred_size();
    btn_add->find_preferred_size();

    // Connect signals
    slots.connect(btn_ok->sig_clicked(), this, &NS_Profile::on_ok);
    slots.connect(btn_add->sig_clicked(), this, &NS_Profile::on_new_player);
    slots.connect(btn_del->sig_clicked(), this, &NS_Profile::on_delete_player);

    // Add components to component list
    components["btn_ok"]     = btn_ok;
    components["players"]    = lst_players;
    components["btn_add"]    = btn_add;
    components["btn_del"]    = btn_del;
}

bool NS_Profile::open(CL_Component * parent)
{
    NS_Profile dlg(parent);
    dlg.run();
    return dlg.is_success();
}

bool NS_Profile::is_success() const
{
    return success;
}

void NS_Profile::on_ok()
{
    // Select new default user
    // then quit
    quit();
}

NS_Profile::~NS_Profile()
{
	std::map<std::string, CL_Component * >::iterator it(components.begin());
	for(; it != components.end(); ++it)
		delete it->second;
}

// New Player
namespace {

class NS_New_Player : public CL_Window
{
public:
    NS_New_Player(CL_Component * parent, std::string const & player_name = "");
    ~NS_New_Player();
public:
    void on_ok();
    void on_cancel();
    static bool open(CL_Component * parent, std::string & player_name);
    std::string const & get_player_name() const;
    void set_focus_on_input();
    bool is_success() const;
    void on_key_down(CL_InputEvent const & key);
private:
    std::map<std::string, CL_Component * > components;
    CL_SlotContainer slots;
    std::string old_player_name;
    bool success;
};

NS_New_Player::NS_New_Player(CL_Component * parent,
        std::string const & player_name)
:
    CL_Window(parent, parent->get_style_manager()),
    old_player_name(player_name),
    success(false)
{
    CL_Button * btn_cancel = new CL_Button(CL_Point(220, 75), "Cancel", get_client_area());
    CL_Button * btn_ok = new CL_Button(CL_Point(180, 75), "OK", get_client_area());
    CL_InputBox * input_name = new CL_InputBox(CL_Rect(15, 40, 270, 65), get_client_area());

    // Calc dialog position
    set_position(CL_Rect(200, 150, 500, 265));
    set_title("Enter player name");

    // Connect signals
    slots.connect(btn_ok->sig_clicked(), this, &NS_New_Player::on_ok);
    slots.connect(btn_cancel->sig_clicked(), this, &NS_New_Player::on_cancel);
    slots.connect(CL_Keyboard::sig_key_down(), this, &NS_New_Player::on_key_down);

    // Add components to component list
    components["btn_ok"]     = btn_ok;
    components["btn_cancel"] = btn_cancel;
    components["input_name"] = input_name;

	input_name->set_focus();
}

NS_New_Player::~NS_New_Player()
{
	std::map<std::string, CL_Component * >::iterator it(components.begin());
	for(; it != components.end(); ++it)
		delete it->second;
}

void NS_New_Player::on_key_down(CL_InputEvent const & key)
{
    if (key.id == CL_KEY_ESCAPE)
        on_cancel();
    else
        if (key.id == CL_KEY_RETURN)
            on_ok();
}

std::string const & NS_New_Player::get_player_name() const
{
    return old_player_name;
}

bool NS_New_Player::is_success() const
{
    return success;
}

void NS_New_Player::set_focus_on_input()
{
    components["input_name"]->set_focus();
}

bool NS_New_Player::open(CL_Component * parent,
                         std::string & player_name)
{
    NS_New_Player dlg(parent, player_name);
    dlg.set_focus_on_input();
    dlg.run();
    player_name = dlg.get_player_name();
    return dlg.is_success();
}

struct CharRemover : std::unary_function<char, bool>
{
    CharRemover(std::string const & wildcard)
        : wildcard(wildcard)
    {
    }
    bool operator()(char ch)
    {
        return wildcard.find(ch) == std::string::npos;
    }
    std::string const & wildcard;
};

void NS_New_Player::on_ok()
{
    // Select new default user
    // then quit
    CL_InputBox * input_name = dynamic_cast<CL_InputBox*>(components["input_name"]);
    if (input_name)
    {
        std::string str = input_name->get_text();
        size_t pos = str.find_first_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890(),_");
        if (pos != std::string::npos)
            str.erase(str.begin(), str.begin() + pos);
        pos = str.find_last_of("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890(),_");
        if (pos != std::string::npos)
            str.erase(str.begin() + pos + 1, str.end());

        str.erase(std::remove_if(str.begin(), str.end(),
            CharRemover("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890(),_ ")),
            str.end());

        if (!str.empty())
            success = true;
        old_player_name = str;
    }
    quit();
}

void NS_New_Player::on_cancel()
{
    // Select new default user
    // then quit
    quit();
}

} // end New Player namespace

void NS_Profile::on_new_player()
{
    CL_ListBox * lst_players = dynamic_cast<CL_ListBox*>(components["players"]);
    if (lst_players)
    {
        std::string old_player_name;
        if (NS_New_Player::open(get_gui_manager(), old_player_name))
        // add player here
        {
            if (!old_player_name.empty())
			{
				lst_players->insert_item(old_player_name);
				lst_players->set_current_item(lst_players->get_count() - 1);
				lst_players->sort();
			}
        }
    }
}

void NS_Profile::on_delete_player()
{
    CL_ListBox * lst_players = dynamic_cast<CL_ListBox*>(components["players"]);
    if (lst_players)
    {
        if (lst_players->get_count() > 0)
		{
			if (lst_players->get_current_item() != -1)
            {
                int result = NS_MessageBox::info(get_gui_manager(), "Deleting Player",
                    "Are you sure ?\nInformation about all completed levels for player '" + lst_players->get_current_text() + "' will be lost!",
                    "OK", "Cancel", "");
                if (result == 1)
                {
                    // delete player
					lst_players->remove_item(lst_players->get_current_item());
                    if (lst_players->get_count())
                    {
                        lst_players->set_current_item(0);
                    }
                    else
                    {
                        lst_players->set_current_item(-1);
                    }
                }
            }
		}
		else
		{
			NS_MessageBox::info(this, "Deleting Player",
				"You should add some players first",
				"OK", "", "");
		}
    }
}