guiKeyChangeMenu.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 <string>
  22. #include <IGUICheckBox.h>
  23. #include <IGUIEditBox.h>
  24. #include <IGUIButton.h>
  25. #include <IGUIStaticText.h>
  26. #include <IGUIFont.h>
  27. #include "settings.h"
  28. #include <algorithm>
  29. #include "mainmenumanager.h" // for g_gamecallback
  30. #define KMaxButtonPerColumns 12
  31. extern MainGameCallback *g_gamecallback;
  32. enum
  33. {
  34. GUI_ID_BACK_BUTTON = 101, GUI_ID_ABORT_BUTTON, GUI_ID_SCROLL_BAR,
  35. // buttons
  36. GUI_ID_KEY_FORWARD_BUTTON,
  37. GUI_ID_KEY_BACKWARD_BUTTON,
  38. GUI_ID_KEY_LEFT_BUTTON,
  39. GUI_ID_KEY_RIGHT_BUTTON,
  40. GUI_ID_KEY_USE_BUTTON,
  41. GUI_ID_KEY_FLY_BUTTON,
  42. GUI_ID_KEY_FAST_BUTTON,
  43. GUI_ID_KEY_JUMP_BUTTON,
  44. GUI_ID_KEY_NOCLIP_BUTTON,
  45. GUI_ID_KEY_CINEMATIC_BUTTON,
  46. GUI_ID_KEY_CHAT_BUTTON,
  47. GUI_ID_KEY_CMD_BUTTON,
  48. GUI_ID_KEY_CMD_LOCAL_BUTTON,
  49. GUI_ID_KEY_CONSOLE_BUTTON,
  50. GUI_ID_KEY_SNEAK_BUTTON,
  51. GUI_ID_KEY_DROP_BUTTON,
  52. GUI_ID_KEY_INVENTORY_BUTTON,
  53. GUI_ID_KEY_HOTBAR_PREV_BUTTON,
  54. GUI_ID_KEY_HOTBAR_NEXT_BUTTON,
  55. GUI_ID_KEY_MUTE_BUTTON,
  56. GUI_ID_KEY_DEC_VOLUME_BUTTON,
  57. GUI_ID_KEY_INC_VOLUME_BUTTON,
  58. GUI_ID_KEY_RANGE_BUTTON,
  59. GUI_ID_KEY_ZOOM_BUTTON,
  60. GUI_ID_KEY_CAMERA_BUTTON,
  61. GUI_ID_KEY_MINIMAP_BUTTON,
  62. GUI_ID_KEY_SCREENSHOT_BUTTON,
  63. GUI_ID_KEY_CHATLOG_BUTTON,
  64. GUI_ID_KEY_HUD_BUTTON,
  65. GUI_ID_KEY_FOG_BUTTON,
  66. GUI_ID_KEY_DEC_RANGE_BUTTON,
  67. GUI_ID_KEY_INC_RANGE_BUTTON,
  68. GUI_ID_KEY_AUTOFWD_BUTTON,
  69. // other
  70. GUI_ID_CB_AUX1_DESCENDS,
  71. GUI_ID_CB_DOUBLETAP_JUMP,
  72. };
  73. GUIKeyChangeMenu::GUIKeyChangeMenu(gui::IGUIEnvironment* env,
  74. gui::IGUIElement* parent, s32 id, IMenuManager *menumgr) :
  75. GUIModalMenu(env, parent, id, menumgr)
  76. {
  77. init_keys();
  78. for (key_setting *ks : key_settings)
  79. key_used.push_back(ks->key);
  80. }
  81. GUIKeyChangeMenu::~GUIKeyChangeMenu()
  82. {
  83. removeChildren();
  84. for (key_setting *ks : key_settings) {
  85. delete[] ks->button_name;
  86. delete ks;
  87. }
  88. key_settings.clear();
  89. }
  90. void GUIKeyChangeMenu::removeChildren()
  91. {
  92. const core::list<gui::IGUIElement*> &children = getChildren();
  93. core::list<gui::IGUIElement*> children_copy;
  94. for (gui::IGUIElement*i : children) {
  95. children_copy.push_back(i);
  96. }
  97. for (gui::IGUIElement *i : children_copy) {
  98. i->remove();
  99. }
  100. }
  101. void GUIKeyChangeMenu::regenerateGui(v2u32 screensize)
  102. {
  103. removeChildren();
  104. v2s32 size(745, 430);
  105. core::rect < s32 > rect(screensize.X / 2 - size.X / 2,
  106. screensize.Y / 2 - size.Y / 2, screensize.X / 2 + size.X / 2,
  107. screensize.Y / 2 + size.Y / 2);
  108. DesiredRect = rect;
  109. recalculateAbsolutePosition(false);
  110. v2s32 topleft(0, 0);
  111. {
  112. core::rect < s32 > rect(0, 0, 600, 40);
  113. rect += topleft + v2s32(25, 3);
  114. //gui::IGUIStaticText *t =
  115. const wchar_t *text = wgettext("Keybindings. (If this menu screws up, remove stuff from minetest.conf)");
  116. Environment->addStaticText(text,
  117. rect, false, true, this, -1);
  118. delete[] text;
  119. //t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_UPPERLEFT);
  120. }
  121. // Build buttons
  122. v2s32 offset(25, 60);
  123. for(size_t i = 0; i < key_settings.size(); i++)
  124. {
  125. key_setting *k = key_settings.at(i);
  126. {
  127. core::rect < s32 > rect(0, 0, 150, 20);
  128. rect += topleft + v2s32(offset.X, offset.Y);
  129. Environment->addStaticText(k->button_name, rect, false, true, this, -1);
  130. }
  131. {
  132. core::rect < s32 > rect(0, 0, 100, 30);
  133. rect += topleft + v2s32(offset.X + 120, offset.Y - 5);
  134. const wchar_t *text = wgettext(k->key.name());
  135. k->button = Environment->addButton(rect, this, k->id, text);
  136. delete[] text;
  137. }
  138. if ((i + 1) % KMaxButtonPerColumns == 0) {
  139. offset.X += 230;
  140. offset.Y = 60;
  141. } else {
  142. offset += v2s32(0, 25);
  143. }
  144. }
  145. {
  146. s32 option_x = offset.X;
  147. s32 option_y = offset.Y + 5;
  148. u32 option_w = 180;
  149. {
  150. core::rect<s32> rect(0, 0, option_w, 30);
  151. rect += topleft + v2s32(option_x, option_y);
  152. const wchar_t *text = wgettext("\"Special\" = climb down");
  153. Environment->addCheckBox(g_settings->getBool("aux1_descends"), rect, this,
  154. GUI_ID_CB_AUX1_DESCENDS, text);
  155. delete[] text;
  156. }
  157. offset += v2s32(0, 25);
  158. }
  159. {
  160. s32 option_x = offset.X;
  161. s32 option_y = offset.Y + 5;
  162. u32 option_w = 280;
  163. {
  164. core::rect<s32> rect(0, 0, option_w, 30);
  165. rect += topleft + v2s32(option_x, option_y);
  166. const wchar_t *text = wgettext("Double tap \"jump\" to toggle fly");
  167. Environment->addCheckBox(g_settings->getBool("doubletap_jump"), rect, this,
  168. GUI_ID_CB_DOUBLETAP_JUMP, text);
  169. delete[] text;
  170. }
  171. offset += v2s32(0, 25);
  172. }
  173. {
  174. core::rect < s32 > rect(0, 0, 100, 30);
  175. rect += topleft + v2s32(size.X / 2 - 105, size.Y - 40);
  176. const wchar_t *text = wgettext("Save");
  177. Environment->addButton(rect, this, GUI_ID_BACK_BUTTON,
  178. text);
  179. delete[] text;
  180. }
  181. {
  182. core::rect < s32 > rect(0, 0, 100, 30);
  183. rect += topleft + v2s32(size.X / 2 + 5, size.Y - 40);
  184. const wchar_t *text = wgettext("Cancel");
  185. Environment->addButton(rect, this, GUI_ID_ABORT_BUTTON,
  186. text);
  187. delete[] text;
  188. }
  189. }
  190. void GUIKeyChangeMenu::drawMenu()
  191. {
  192. gui::IGUISkin* skin = Environment->getSkin();
  193. if (!skin)
  194. return;
  195. video::IVideoDriver* driver = Environment->getVideoDriver();
  196. video::SColor bgcolor(140, 0, 0, 0);
  197. {
  198. core::rect < s32 > rect(0, 0, 745, 620);
  199. rect += AbsoluteRect.UpperLeftCorner;
  200. driver->draw2DRectangle(bgcolor, rect, &AbsoluteClippingRect);
  201. }
  202. gui::IGUIElement::draw();
  203. }
  204. bool GUIKeyChangeMenu::acceptInput()
  205. {
  206. for (key_setting *k : key_settings) {
  207. g_settings->set(k->setting_name, k->key.sym());
  208. }
  209. {
  210. gui::IGUIElement *e = getElementFromId(GUI_ID_CB_AUX1_DESCENDS);
  211. if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
  212. g_settings->setBool("aux1_descends", ((gui::IGUICheckBox*)e)->isChecked());
  213. }
  214. {
  215. gui::IGUIElement *e = getElementFromId(GUI_ID_CB_DOUBLETAP_JUMP);
  216. if(e != NULL && e->getType() == gui::EGUIET_CHECK_BOX)
  217. g_settings->setBool("doubletap_jump", ((gui::IGUICheckBox*)e)->isChecked());
  218. }
  219. clearKeyCache();
  220. g_gamecallback->signalKeyConfigChange();
  221. return true;
  222. }
  223. bool GUIKeyChangeMenu::resetMenu()
  224. {
  225. if (activeKey >= 0)
  226. {
  227. for (key_setting *k : key_settings) {
  228. if (k->id == activeKey) {
  229. const wchar_t *text = wgettext(k->key.name());
  230. k->button->setText(text);
  231. delete[] text;
  232. break;
  233. }
  234. }
  235. activeKey = -1;
  236. return false;
  237. }
  238. return true;
  239. }
  240. bool GUIKeyChangeMenu::OnEvent(const SEvent& event)
  241. {
  242. if (event.EventType == EET_KEY_INPUT_EVENT && activeKey >= 0
  243. && event.KeyInput.PressedDown) {
  244. bool prefer_character = shift_down;
  245. KeyPress kp(event.KeyInput, prefer_character);
  246. bool shift_went_down = false;
  247. if(!shift_down &&
  248. (event.KeyInput.Key == irr::KEY_SHIFT ||
  249. event.KeyInput.Key == irr::KEY_LSHIFT ||
  250. event.KeyInput.Key == irr::KEY_RSHIFT))
  251. shift_went_down = true;
  252. // Remove Key already in use message
  253. if(this->key_used_text)
  254. {
  255. this->key_used_text->remove();
  256. this->key_used_text = NULL;
  257. }
  258. // Display Key already in use message
  259. if (std::find(this->key_used.begin(), this->key_used.end(), kp) != this->key_used.end())
  260. {
  261. core::rect < s32 > rect(0, 0, 600, 40);
  262. rect += v2s32(0, 0) + v2s32(25, 30);
  263. const wchar_t *text = wgettext("Key already in use");
  264. this->key_used_text = Environment->addStaticText(text,
  265. rect, false, true, this, -1);
  266. delete[] text;
  267. //infostream << "Key already in use" << std::endl;
  268. }
  269. // But go on
  270. {
  271. key_setting *k = NULL;
  272. for (key_setting *ks : key_settings) {
  273. if (ks->id == activeKey) {
  274. k = ks;
  275. break;
  276. }
  277. }
  278. FATAL_ERROR_IF(k == NULL, "Key setting not found");
  279. k->key = kp;
  280. const wchar_t *text = wgettext(k->key.name());
  281. k->button->setText(text);
  282. delete[] text;
  283. this->key_used.push_back(kp);
  284. // Allow characters made with shift
  285. if(shift_went_down){
  286. shift_down = true;
  287. return false;
  288. }
  289. activeKey = -1;
  290. return true;
  291. }
  292. } else if (event.EventType == EET_KEY_INPUT_EVENT && activeKey < 0
  293. && event.KeyInput.PressedDown
  294. && event.KeyInput.Key == irr::KEY_ESCAPE) {
  295. quitMenu();
  296. return true;
  297. } else if (event.EventType == EET_GUI_EVENT) {
  298. if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
  299. && isVisible())
  300. {
  301. if (!canTakeFocus(event.GUIEvent.Element))
  302. {
  303. dstream << "GUIMainMenu: Not allowing focus change."
  304. << std::endl;
  305. // Returning true disables focus change
  306. return true;
  307. }
  308. }
  309. if (event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED)
  310. {
  311. switch (event.GUIEvent.Caller->getID())
  312. {
  313. case GUI_ID_BACK_BUTTON: //back
  314. acceptInput();
  315. quitMenu();
  316. return true;
  317. case GUI_ID_ABORT_BUTTON: //abort
  318. quitMenu();
  319. return true;
  320. default:
  321. key_setting *k = NULL;
  322. for (key_setting *ks : key_settings) {
  323. if (ks->id == event.GUIEvent.Caller->getID()) {
  324. k = ks;
  325. break;
  326. }
  327. }
  328. FATAL_ERROR_IF(k == NULL, "Key setting not found");
  329. resetMenu();
  330. shift_down = false;
  331. activeKey = event.GUIEvent.Caller->getID();
  332. const wchar_t *text = wgettext("press key");
  333. k->button->setText(text);
  334. delete[] text;
  335. this->key_used.erase(std::remove(this->key_used.begin(),
  336. this->key_used.end(), k->key), this->key_used.end());
  337. break;
  338. }
  339. Environment->setFocus(this);
  340. }
  341. }
  342. return Parent ? Parent->OnEvent(event) : false;
  343. }
  344. void GUIKeyChangeMenu::add_key(int id, const wchar_t *button_name, const std::string &setting_name)
  345. {
  346. key_setting *k = new key_setting;
  347. k->id = id;
  348. k->button_name = button_name;
  349. k->setting_name = setting_name;
  350. k->key = getKeySetting(k->setting_name.c_str());
  351. key_settings.push_back(k);
  352. }
  353. void GUIKeyChangeMenu::init_keys()
  354. {
  355. this->add_key(GUI_ID_KEY_FORWARD_BUTTON, wgettext("Forward"), "keymap_forward");
  356. this->add_key(GUI_ID_KEY_BACKWARD_BUTTON, wgettext("Backward"), "keymap_backward");
  357. this->add_key(GUI_ID_KEY_LEFT_BUTTON, wgettext("Left"), "keymap_left");
  358. this->add_key(GUI_ID_KEY_RIGHT_BUTTON, wgettext("Right"), "keymap_right");
  359. this->add_key(GUI_ID_KEY_USE_BUTTON, wgettext("Special"), "keymap_special1");
  360. this->add_key(GUI_ID_KEY_JUMP_BUTTON, wgettext("Jump"), "keymap_jump");
  361. this->add_key(GUI_ID_KEY_SNEAK_BUTTON, wgettext("Sneak"), "keymap_sneak");
  362. this->add_key(GUI_ID_KEY_DROP_BUTTON, wgettext("Drop"), "keymap_drop");
  363. this->add_key(GUI_ID_KEY_INVENTORY_BUTTON, wgettext("Inventory"), "keymap_inventory");
  364. this->add_key(GUI_ID_KEY_HOTBAR_PREV_BUTTON,wgettext("Prev. item"), "keymap_hotbar_previous");
  365. this->add_key(GUI_ID_KEY_HOTBAR_NEXT_BUTTON,wgettext("Next item"), "keymap_hotbar_next");
  366. this->add_key(GUI_ID_KEY_ZOOM_BUTTON, wgettext("Zoom"), "keymap_zoom");
  367. this->add_key(GUI_ID_KEY_CAMERA_BUTTON, wgettext("Change camera"), "keymap_camera_mode");
  368. this->add_key(GUI_ID_KEY_CINEMATIC_BUTTON, wgettext("Toggle Cinematic"), "keymap_cinematic");
  369. this->add_key(GUI_ID_KEY_MINIMAP_BUTTON, wgettext("Toggle minimap"), "keymap_minimap");
  370. this->add_key(GUI_ID_KEY_FLY_BUTTON, wgettext("Toggle fly"), "keymap_freemove");
  371. this->add_key(GUI_ID_KEY_FAST_BUTTON, wgettext("Toggle fast"), "keymap_fastmove");
  372. this->add_key(GUI_ID_KEY_NOCLIP_BUTTON, wgettext("Toggle noclip"), "keymap_noclip");
  373. this->add_key(GUI_ID_KEY_MUTE_BUTTON, wgettext("Mute"), "keymap_mute");
  374. this->add_key(GUI_ID_KEY_DEC_VOLUME_BUTTON,wgettext("Dec. volume"), "keymap_decrease_volume");
  375. this->add_key(GUI_ID_KEY_INC_VOLUME_BUTTON,wgettext("Inc. volume"), "keymap_increase_volume");
  376. this->add_key(GUI_ID_KEY_AUTOFWD_BUTTON, wgettext("Autoforward"), "keymap_autoforward");
  377. this->add_key(GUI_ID_KEY_CHAT_BUTTON, wgettext("Chat"), "keymap_chat");
  378. this->add_key(GUI_ID_KEY_SCREENSHOT_BUTTON,wgettext("Screenshot"), "keymap_screenshot");
  379. this->add_key(GUI_ID_KEY_RANGE_BUTTON, wgettext("Range select"), "keymap_rangeselect");
  380. this->add_key(GUI_ID_KEY_DEC_RANGE_BUTTON, wgettext("Dec. range"), "keymap_decrease_viewing_range_min");
  381. this->add_key(GUI_ID_KEY_INC_RANGE_BUTTON, wgettext("Inc. range"), "keymap_increase_viewing_range_min");
  382. this->add_key(GUI_ID_KEY_CONSOLE_BUTTON, wgettext("Console"), "keymap_console");
  383. this->add_key(GUI_ID_KEY_CMD_BUTTON, wgettext("Command"), "keymap_cmd");
  384. this->add_key(GUI_ID_KEY_CMD_LOCAL_BUTTON, wgettext("Local command"), "keymap_cmd_local");
  385. this->add_key(GUI_ID_KEY_HUD_BUTTON, wgettext("Toggle HUD"), "keymap_toggle_hud");
  386. this->add_key(GUI_ID_KEY_CHATLOG_BUTTON, wgettext("Toggle chat log"), "keymap_toggle_chat");
  387. this->add_key(GUI_ID_KEY_FOG_BUTTON, wgettext("Toggle fog"), "keymap_toggle_force_fog_off");
  388. }