guiKeyChangeMenu.cpp 11 KB

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