Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
blob: 4417bf3e5afc9723f7c7c3d0e259fde60f58cd2e (plain)
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
   client/controlsettingsmenu.cc
   This file is part of the Osirion project and is distributed under
   the terms of the GNU General Public License version 2
*/

#include "client/controlsettingsmenu.h"
#include "client/input.h"
#include "ui/button.h"
#include "ui/iconbutton.h"
#include "ui/label.h"
#include "ui/listview.h"
#include "ui/ui.h"

namespace client {
	
class ControlListItem : public ui::ListItem
{
public:
	ControlListItem(ui::ListView *parent, Control *control) : ui::ListItem(parent, "")
	{
		set_border(false);
		
		_namelabel = new ui::Label(this);
		_namelabel->set_border(true);
		_namelabel->set_background(false);
		_namelabel->set_alignment(ui::AlignVCenter | ui::AlignLeft);
		
		_keylabel = new ui::Button(this);
		_keylabel->set_border(true);
		_keylabel->set_background(false);
		_keylabel->set_alignment(ui::AlignCenter);
		
		set_control(control);
		
	}
	
	virtual ~ControlListItem()
	{
	}
	
	inline Control *control()
	{
		return _control;
	}
	
	void refresh()
	{
		if (_control)
		{
			_namelabel->set_text(_control->name());
			_keylabel->set_text(_control->keyname());
		}
		else 
		{
			_namelabel->clear();
			_keylabel->clear();
		}
	}
	
	void set_control(Control *control)
	{
		_control = control;
		refresh();
	}
	
protected:	
	virtual void resize()
	{
		const float w = width() * 0.5f;
		const float h = height();
		
		_namelabel->set_size(w, h);
		_namelabel->set_location(0.0f, 0.0f);
		
		_keylabel->set_size(width() - w, h);
		_keylabel->set_location(width() - w, 0.0f);
		
	}
	
	virtual bool on_emit(Widget *sender, const Event event, void *data)
	{
		if (event == ui::Widget::EventButtonClicked)
		{
			if (sender == _keylabel)
			{
				emit(EventListItemClicked);
				return true;
			}
		}
		return ui::ListItem::on_emit(sender, event, data);
	}
	
private:
	ui::Label 	*_namelabel;
	ui::Button 	*_keylabel;
	
	Control		*_control;
	
};

class ControlKeyWindow : public ui::Window
{
public:
	ControlKeyWindow(ui::Widget *parent = 0) : ui::Window(parent)
	{
		set_border(false);
		set_background(false);
		set_label("controlkeywindow");
	
		_frame = new ui::Window(this);
		_frame->set_border(true);
		_frame->set_background(true);
	
		_namelabel = new ui::Label(_frame);
		_namelabel->set_border(false);
		_namelabel->set_background(false);
		_namelabel->set_alignment(ui::AlignCenter);
		
		_keylabel = new ui::Label(_frame);
		_keylabel->set_border(false);
		_keylabel->set_background(false);
		_keylabel->set_alignment(ui::AlignCenter);
	
		_okbutton = new ui::Button(_frame, "OK");
		_cancelbutton = new ui::Button(_frame, "Cancel");
	}
	
	virtual ~ControlKeyWindow()
	{
	}
	
	inline const std::string &key() const
	{
		return _keylabel->text();
	}
	
	void set_control(Control *control)
	{
		_control = control;
		_namelabel->set_text("Press key ^F" + _control->name() );
		_keylabel->set_text(_control->keyname());
	}
	
protected:
	virtual void resize()
	{
		const float padding =  ui::UI::padding;
		
		set_size(parent()->size());
		
		_frame->set_size(
			ui::UI::elementsize.width() * 3.0f, 
			ui::UI::elementsize.width() * 1.5f
		);	
		_frame->set_location(
			(width() - _frame->width()) * 0.5f,
			(height() - _frame->height()) * 0.5f
		);

		const float h =  ui::root()->font_large()->height() * 2.0f;
		_namelabel->set_size(_frame->width() - padding * 2.0f, h * 2.0f);
		_namelabel->set_location(padding, padding);
		
		_keylabel->set_size(_frame->width() - padding * 2.0f, h);
		_keylabel->set_location(padding, _namelabel->bottom() + padding);
		
		_okbutton->set_size(ui::UI::elementsize);
		_cancelbutton->set_size(ui::UI::elementsize);
		
		const float l = (_frame->width() - _okbutton->width() - _cancelbutton->width() - padding * 2.0f) * 0.5f;
		_okbutton->set_location(l, _frame->height() - _okbutton->height() - padding);
		_cancelbutton->set_location(_okbutton->right() + padding, _frame->height() - _cancelbutton->height() - padding);

	}
	
	void capture_keypress()
	{
	std::string keyname;
		if (input::last_key_pressed()) {
			Key::Modifier mod = input::modifier();
			if (mod == Key::Shift)
				keyname.assign("shift+");
			else if (mod == Key::Ctrl)
				keyname.assign("ctrl+");
			else if (mod == Key::Alt)
				keyname.assign("alt+");
			keyname.append(input::last_key_pressed()->name());
		}
		_keylabel->set_text(keyname);
	}
	
	virtual bool on_mousepress(const unsigned int button)
	{
		capture_keypress();
		return true;
	
	}
	
	virtual bool on_mousewheel(const math::Vector2f & direction)
	{
		capture_keypress();
		return true;
	}
	
	virtual bool on_keypress(const int key, const unsigned int modifier)
	{
		if (key == SDLK_ESCAPE)
		{
			hide();
			return true;
		}
		else 
		{
			capture_keypress();
			return true;
		}
		return false;
	}
	
	virtual bool on_emit(Widget *sender, const Event event, void *data)
	{
		if (event == Widget::EventButtonClicked)
		{
			if (sender == _okbutton) 
			{
				if (_control->keyname().size())
				{
					input::keyboard->unbind(_control->keyname());
				}
				
				_control->set_keyname(_keylabel->text());
				
				if (_control->keyname().size())
				{
					input::keyboard->bind(_control->keyname(), _control->command());
				}
				
				input::keyboard->load_controls();
				
				hide();
				return true;
			}
			else if (sender == _cancelbutton)
			{
				hide();
				return true;
			}
		}
		return ui::Window::on_emit(sender, event, data);
	}
	
private:	
	ui::Window	*_frame;
	
	ui::Label	*_namelabel;
	ui::Label	*_keylabel;
	ui::Button	*_okbutton;
	ui::Button	*_cancelbutton;
	
	Control		*_control;
};
	
ControlSettingsMenu::ControlSettingsMenu(ui::Widget *parent, const char *label) : ui::Window(parent)
{
	set_label(label);
	set_border(true);
	set_background(true);
	set_font(ui::root()->font_small());
	
	// window title
	_titlelabel = new ui::Label(this);
	_titlelabel->set_label("title");
	_titlelabel->set_background(false);
	_titlelabel->set_border(false);
	_titlelabel->set_font(ui::root()->font_large());
	_titlelabel->set_alignment(ui::AlignCenter);
	_titlelabel->set_text("CONTROLS");
	
	// close button
	_closebutton = new ui::IconButton(_titlelabel, "bitmaps/icons/window_close");
	
	// command listview
	_controlslistview = new ui::ListView(this);
	_controlslistview->set_background(true);
	
	_controlkeywindow = new ControlKeyWindow(this);
	_controlkeywindow->hide();
}

ControlSettingsMenu::~ControlSettingsMenu()
{
}

void ControlSettingsMenu::refresh()
{
	_controlslistview->clear();
	
	input::keyboard->load_controls();
	
	for (Keyboard::Controls::iterator cit = input::keyboard->controls().begin(); cit != input::keyboard->controls().end(); ++cit)
	{
		ui::ListItem *listitem = new ControlListItem(_controlslistview, (*cit));
		listitem->set_font(ui::root()->font_tiny());
		listitem->set_height(listitem->font()->height() * 2.0f);
	}
	
	_controlslistview->event_resize();
}

void ControlSettingsMenu::show()
{
	refresh();
	ui::Window::show();
}

void ControlSettingsMenu::apply()
{
}

void ControlSettingsMenu::resize()
{
	const float padding =  ui::UI::padding;
	
	// resize title label
	_titlelabel->set_size(width() - padding * 2.0f, _titlelabel->font()->height());	
	_titlelabel->set_location(padding, padding);

	// resize close button
	_closebutton->set_size(_titlelabel->font()->height(), _titlelabel->font()->height());
	_closebutton->set_location(_titlelabel->width() - _closebutton->width(), 0);
	
	// resize listview
	_controlslistview->set_location(_titlelabel->left(), _titlelabel->bottom() + padding);	
	_controlslistview->set_size(_titlelabel->width(), height() - _titlelabel->bottom() - padding * 2.0f);
}

bool ControlSettingsMenu::on_emit(ui::Widget *sender, const ui::Widget::Event event, void *data)
{
	if (sender == _closebutton)
	{
		if (event == ui::Widget::EventButtonClicked)
		{
			parent()->hide();
			return true;
		}
	}
	else if (sender == _controlslistview)
	{
		if (event == ui::Widget::EventListViewChanged)
		{
			if (_controlslistview->selected())
			{
				ControlListItem *listitem = static_cast<ControlListItem *>(_controlslistview->selected());
				_controlkeywindow->set_control(listitem->control());
				_controlkeywindow->show();
			}
			return true;
		}
	}
	else if (sender == _controlkeywindow)
	{
		if (event == ui::Widget::EventWindowHide)
		{
			for (ui::Widget::Children::iterator it = _controlslistview->children().begin(); it != _controlslistview->children().end(); ++it)
			{	
				ControlListItem *listitem = dynamic_cast<ControlListItem *>(*it);				
				if  (listitem)
				{
					listitem->refresh();
				}
				
			}
			return true;
		}
		
	}
	return Window::on_emit(sender, event, data);
}

} // namespace client