guiKeyChangeMenu.cpp 14 KB

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