1 /*
2 guiKeyChangeMenu.cpp
3 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2013 Ciaran Gultnieks <ciaran@ciarang.com>
5 Copyright (C) 2013 teddydestodes <derkomtur@schattengang.net>
6 */
7 
8 /*
9 This file is part of Freeminer.
10 
11 Freeminer is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, either version 3 of the License, or
14 (at your option) any later version.
15 
16 Freeminer  is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 GNU General Public License for more details.
20 
21 You should have received a copy of the GNU General Public License
22 along with Freeminer.  If not, see <http://www.gnu.org/licenses/>.
23 */
24 
25 #include "guiKeyChangeMenu.h"
26 #include "debug.h"
27 #include "serialization.h"
28 #include "main.h"
29 #include <string>
30 #include <IGUICheckBox.h>
31 #include <IGUIEditBox.h>
32 #include <IGUIButton.h>
33 #include <IGUIStaticText.h>
34 #include <IGUIFont.h>
35 #include "settings.h"
36 #include <algorithm>
37 
38 #include "mainmenumanager.h"  // for g_gamecallback
39 
40 #define KMaxButtonPerColumns 12
41 
42 extern MainGameCallback *g_gamecallback;
43 
44 enum
45 {
46 	GUI_ID_BACK_BUTTON = 101, GUI_ID_ABORT_BUTTON, GUI_ID_SCROLL_BAR,
47 	// buttons
48 	GUI_ID_KEY_FORWARD_BUTTON,
49 	GUI_ID_KEY_BACKWARD_BUTTON,
50 	GUI_ID_KEY_LEFT_BUTTON,
51 	GUI_ID_KEY_RIGHT_BUTTON,
52 	GUI_ID_KEY_USE_BUTTON,
53 	GUI_ID_KEY_FLY_BUTTON,
54 	GUI_ID_KEY_FAST_BUTTON,
55 	GUI_ID_KEY_JUMP_BUTTON,
56 	GUI_ID_KEY_NOCLIP_BUTTON,
57 	GUI_ID_KEY_CHAT_BUTTON,
58 	GUI_ID_KEY_PL_LIST_BUTTON,
59 	GUI_ID_KEY_CMD_BUTTON,
60 	GUI_ID_KEY_CONSOLE_BUTTON,
61 	GUI_ID_KEY_SNEAK_BUTTON,
62 	GUI_ID_KEY_DROP_BUTTON,
63 	GUI_ID_KEY_INVENTORY_BUTTON,
64 	GUI_ID_KEY_DUMP_BUTTON,
65 	GUI_ID_KEY_RANGE_BUTTON,
66 	GUI_ID_KEY_ZOOM_BUTTON,
67 	// other
68 	GUI_ID_CB_AUX1_DESCENDS,
69 	GUI_ID_CB_DOUBLETAP_JUMP,
70 };
71 
GUIKeyChangeMenu(gui::IGUIEnvironment * env,gui::IGUIElement * parent,s32 id,IMenuManager * menumgr)72 GUIKeyChangeMenu::GUIKeyChangeMenu(gui::IGUIEnvironment* env,
73 				gui::IGUIElement* parent, s32 id, IMenuManager *menumgr) :
74 GUIModalMenu(env, parent, id, menumgr)
75 {
76 	shift_down = false;
77 	activeKey = -1;
78 	this->key_used_text = NULL;
79 	init_keys();
80 	for(size_t i=0; i<key_settings.size(); i++)
81 		this->key_used.push_back(key_settings.at(i)->key);
82 }
83 
~GUIKeyChangeMenu()84 GUIKeyChangeMenu::~GUIKeyChangeMenu()
85 {
86 	removeChildren();
87 
88 	for (std::vector<key_setting*>::iterator iter = key_settings.begin();
89 			iter != key_settings.end(); iter ++) {
90 		delete[] (*iter)->button_name;
91 		delete (*iter);
92 	}
93 	key_settings.clear();
94 }
95 
removeChildren()96 void GUIKeyChangeMenu::removeChildren()
97 {
98 	const core::list<gui::IGUIElement*> &children = getChildren();
99 	core::list<gui::IGUIElement*> children_copy;
100 	for (core::list<gui::IGUIElement*>::ConstIterator i = children.begin(); i
101 		 != children.end(); i++)
102 	{
103 		children_copy.push_back(*i);
104 	}
105 	for (core::list<gui::IGUIElement*>::Iterator i = children_copy.begin(); i
106 		 != children_copy.end(); i++)
107 	{
108 		(*i)->remove();
109 	}
110 }
111 
regenerateGui(v2u32 screensize)112 void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
113 {
114 	removeChildren();
115 	v2s32 size(620, 430);
116 
117 	core::rect < s32 > rect(screensize.X / 2 - size.X / 2,
118 							screensize.Y / 2 - size.Y / 2, screensize.X / 2 + size.X / 2,
119 							screensize.Y / 2 + size.Y / 2);
120 
121 	DesiredRect = rect;
122 	recalculateAbsolutePosition(false);
123 
124 	v2s32 topleft(0, 0);
125 
126 	{
127 		core::rect < s32 > rect(0, 0, 600, 40);
128 		rect += topleft + v2s32(25, 3);
129 		//gui::IGUIStaticText *t =
130 		wchar_t* text = wgettext("Keybindings. (If this menu screws up, remove stuff from minetest.conf)");
131 		Environment->addStaticText(text,
132 								   rect, false, true, this, -1);
133 		delete[] text;
134 		//t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
135 	}
136 
137 	// Build buttons
138 
139 	v2s32 offset(25, 60);
140 
141 	for(size_t i = 0; i < key_settings.size(); i++)
142 	{
143 		key_setting *k = key_settings.at(i);
144 		{
145 			core::rect < s32 > rect(0, 0, 100, 20);
146 			rect += topleft + v2s32(offset.X, offset.Y);
147 			Environment->addStaticText(k->button_name, rect, false, true, this, -1);
148 		}
149 
150 		{
151 			core::rect < s32 > rect(0, 0, 100, 30);
152 			rect += topleft + v2s32(offset.X + 105, offset.Y - 5);
153 			wchar_t* text = wgettext(k->key.name());
154 			k->button = Environment->addButton(rect, this, k->id, text );
155 			delete[] text;
156 		}
157 		if(i + 1 == KMaxButtonPerColumns)
158 			offset = v2s32(250, 60);
159 		else
160 			offset += v2s32(0, 25);
161 	}
162 
163 	{
164 		s32 option_x = offset.X + 10;
165 		s32 option_y = offset.Y;
166 		u32 option_w = 180;
167 		{
168 			core::rect<s32> rect(0, 0, option_w, 30);
169 			rect += topleft + v2s32(option_x, option_y);
170 			wchar_t* text = wgettext("\"Use\" = climb down");
171 			Environment->addCheckBox(g_settings->getBool("aux1_descends"), rect, this,
172 					GUI_ID_CB_AUX1_DESCENDS, text);
173 			delete[] text;
174 		}
175 		offset += v2s32(0, 25);
176 	}
177 
178 	{
179 		s32 option_x = offset.X + 10;
180 		s32 option_y = offset.Y;
181 		u32 option_w = 220;
182 		{
183 			core::rect<s32> rect(0, 0, option_w, 30);
184 			rect += topleft + v2s32(option_x, option_y);
185 			wchar_t* text = wgettext("Double tap \"jump\" to toggle fly");
186 			Environment->addCheckBox(g_settings->getBool("doubletap_jump"), rect, this,
187 					GUI_ID_CB_DOUBLETAP_JUMP, text);
188 			delete[] text;
189 		}
190 		offset += v2s32(0, 25);
191 	}
192 
193 	{
194 		core::rect < s32 > rect(0, 0, 100, 30);
195 		rect += topleft + v2s32(size.X - 100 - 20, size.Y - 40);
196 		wchar_t* text =  wgettext("Save");
197 		Environment->addButton(rect, this, GUI_ID_BACK_BUTTON,
198 							 text);
199 		delete[] text;
200 	}
201 	{
202 		core::rect < s32 > rect(0, 0, 100, 30);
203 		rect += topleft + v2s32(size.X - 100 - 20 - 100 - 20, size.Y - 40);
204 		wchar_t* text = wgettext("Cancel");
205 		Environment->addButton(rect, this, GUI_ID_ABORT_BUTTON,
206 							 text );
207 		delete[] text;
208 	}
209 }
210 
drawMenu()211 void GUIKeyChangeMenu::drawMenu()
212 {
213 	gui::IGUISkin* skin = Environment->getSkin();
214 	if (!skin)
215 		return;
216 	video::IVideoDriver* driver = Environment->getVideoDriver();
217 
218 	video::SColor bgcolor(140, 0, 0, 0);
219 
220 	{
221 		core::rect < s32 > rect(0, 0, 620, 620);
222 		rect += AbsoluteRect.UpperLeftCorner;
223 		driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
224 	}
225 
226 	gui::IGUIElement::draw();
227 }
228 
acceptInput()229 bool GUIKeyChangeMenu::acceptInput()
230 {
231 	for(size_t i = 0; i < key_settings.size(); i++)
232 	{
233 		key_setting *k = key_settings.at(i);
234 		g_settings->set(k->setting_name, k->key.sym());
235 	}
236 	{
237 		gui::IGUIElement *e = getElementFromId(GUI_ID_CB_AUX1_DESCENDS);
238 		if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
239 			g_settings->setBool("aux1_descends", ((gui::IGUICheckBox*)e)->isChecked());
240 	}
241 	{
242 		gui::IGUIElement *e = getElementFromId(GUI_ID_CB_DOUBLETAP_JUMP);
243 		if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
244 			g_settings->setBool("doubletap_jump", ((gui::IGUICheckBox*)e)->isChecked());
245 	}
246 
247 	clearKeyCache();
248 
249 	g_gamecallback->signalKeyConfigChange();
250 
251 	return true;
252 }
253 
resetMenu()254 bool GUIKeyChangeMenu::resetMenu()
255 {
256 	if (activeKey >= 0)
257 	{
258 		for(size_t i = 0; i < key_settings.size(); i++)
259 		{
260 			key_setting *k = key_settings.at(i);
261 			if(k->id == activeKey)
262 			{
263 				wchar_t* text = wgettext(k->key.name());
264 				k->button->setText(text);
265 				delete[] text;
266 				break;
267 			}
268 		}
269 		activeKey = -1;
270 		return false;
271 	}
272 	return true;
273 }
OnEvent(const SEvent & event)274 bool GUIKeyChangeMenu::OnEvent(const SEvent& event)
275 {
276 	if (event.EventType == EET_KEY_INPUT_EVENT && activeKey >= 0
277 		&& event.KeyInput.PressedDown)
278 	{
279 
280 		bool prefer_character = shift_down;
281 		KeyPress kp(event.KeyInput, prefer_character);
282 
283 		bool shift_went_down = false;
284 		if(!shift_down &&
285 				(event.KeyInput.Key == irr::KEY_SHIFT ||
286 				event.KeyInput.Key == irr::KEY_LSHIFT ||
287 				event.KeyInput.Key == irr::KEY_RSHIFT))
288 			shift_went_down = true;
289 
290 		// Remove Key already in use message
291 		if(this->key_used_text)
292 		{
293 			this->key_used_text->remove();
294 			this->key_used_text = NULL;
295 		}
296 		// Display Key already in use message
297 		if (std::find(this->key_used.begin(), this->key_used.end(), kp) != this->key_used.end())
298 		{
299 			core::rect < s32 > rect(0, 0, 600, 40);
300 			rect += v2s32(0, 0) + v2s32(25, 30);
301 			wchar_t* text = wgettext("Key already in use");
302 			this->key_used_text = Environment->addStaticText(text,
303 									rect, false, true, this, -1);
304 			delete[] text;
305 			//infostream << "Key already in use" << std::endl;
306 		}
307 
308 		// But go on
309 		{
310 			key_setting *k=NULL;
311 			for(size_t i = 0; i < key_settings.size(); i++)
312 			{
313 				if(key_settings.at(i)->id == activeKey)
314 				{
315 					k = key_settings.at(i);
316 					break;
317 				}
318 			}
319 			assert(k);
320 			k->key = kp;
321 			wchar_t* text = wgettext(k->key.name());
322 			k->button->setText(text);
323 			delete[] text;
324 
325 			this->key_used.push_back(kp);
326 
327 			// Allow characters made with shift
328 			if(shift_went_down){
329 				shift_down = true;
330 				return false;
331 			}else{
332 				activeKey = -1;
333 				return true;
334 			}
335 		}
336 	}
337 	if (event.EventType == EET_GUI_EVENT)
338 	{
339 		if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
340 			&& isVisible())
341 		{
342 			if (!canTakeFocus(event.GUIEvent.Element))
343 			{
344 				dstream << "GUIMainMenu: Not allowing focus change."
345 				<< std::endl;
346 				// Returning true disables focus change
347 				return true;
348 			}
349 		}
350 		if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED)
351 		{
352 			switch (event.GUIEvent.Caller->getID())
353 			{
354 				case GUI_ID_BACK_BUTTON: //back
355 					acceptInput();
356 					quitMenu();
357 					return true;
358 				case GUI_ID_ABORT_BUTTON: //abort
359 					quitMenu();
360 					return true;
361 				default:
362 					key_setting *k = NULL;
363 					for(size_t i = 0; i < key_settings.size(); i++)
364 					{
365 						if(key_settings.at(i)->id == event.GUIEvent.Caller->getID())
366 						{
367 							k = key_settings.at(i);
368 							break;
369 						}
370 					}
371 					assert(k);
372 
373 					resetMenu();
374 					shift_down = false;
375 					activeKey = event.GUIEvent.Caller->getID();
376 					wchar_t* text = wgettext("press key");
377 					k->button->setText(text);
378 					delete[] text;
379 					this->key_used.erase(std::remove(this->key_used.begin(),
380 							this->key_used.end(), k->key), this->key_used.end());
381 					break;
382 			}
383 			Environment->setFocus(this);
384 		}
385 	}
386 	return Parent ? Parent->OnEvent(event) : false;
387 }
388 
add_key(int id,wchar_t * button_name,std::string setting_name)389 void GUIKeyChangeMenu::add_key(int id, wchar_t* button_name, std::string setting_name)
390 {
391 	key_setting *k = new key_setting;
392 	k->id = id;
393 
394 	k->button_name = button_name;
395 	k->setting_name = setting_name;
396 	k->key = getKeySetting(k->setting_name.c_str());
397 	key_settings.push_back(k);
398 }
399 
init_keys()400 void GUIKeyChangeMenu::init_keys()
401 {
402 	this->add_key(GUI_ID_KEY_FORWARD_BUTTON,   wgettext("Forward"),       "keymap_forward");
403 	this->add_key(GUI_ID_KEY_BACKWARD_BUTTON,  wgettext("Backward"),      "keymap_backward");
404 	this->add_key(GUI_ID_KEY_LEFT_BUTTON,      wgettext("Left"),          "keymap_left");
405 	this->add_key(GUI_ID_KEY_RIGHT_BUTTON,     wgettext("Right"),         "keymap_right");
406 	this->add_key(GUI_ID_KEY_USE_BUTTON,       wgettext("Use"),           "keymap_special1");
407 	this->add_key(GUI_ID_KEY_JUMP_BUTTON,      wgettext("Jump"),          "keymap_jump");
408 	this->add_key(GUI_ID_KEY_SNEAK_BUTTON,     wgettext("Sneak"),         "keymap_sneak");
409 	this->add_key(GUI_ID_KEY_DROP_BUTTON,      wgettext("Drop"),          "keymap_drop");
410 	this->add_key(GUI_ID_KEY_ZOOM_BUTTON,      wgettext("Zoom"),          "keymap_zoom");
411 	this->add_key(GUI_ID_KEY_INVENTORY_BUTTON, wgettext("Inventory"),     "keymap_inventory");
412 	this->add_key(GUI_ID_KEY_CHAT_BUTTON,      wgettext("Chat"),          "keymap_chat");
413 	this->add_key(GUI_ID_KEY_PL_LIST_BUTTON,   wgettext("Player list"),   "keymap_playerlist");
414 	this->add_key(GUI_ID_KEY_CMD_BUTTON,       wgettext("Command"),       "keymap_cmd");
415 	this->add_key(GUI_ID_KEY_CONSOLE_BUTTON,   wgettext("Console"),       "keymap_console");
416 	this->add_key(GUI_ID_KEY_FLY_BUTTON,       wgettext("Toggle fly"),    "keymap_freemove");
417 	this->add_key(GUI_ID_KEY_FAST_BUTTON,      wgettext("Toggle fast"),   "keymap_fastmove");
418 	this->add_key(GUI_ID_KEY_NOCLIP_BUTTON,    wgettext("Toggle noclip"), "keymap_noclip");
419 	this->add_key(GUI_ID_KEY_RANGE_BUTTON,     wgettext("Range select"),  "keymap_rangeselect");
420 	this->add_key(GUI_ID_KEY_DUMP_BUTTON,      wgettext("Print stacks"),  "keymap_print_debug_stacks");
421 }
422