You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

3911 lines
133 KiB

  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include <cstdlib>
  17. #include <algorithm>
  18. #include <iterator>
  19. #include <sstream>
  20. #include <limits>
  21. #include "guiFormSpecMenu.h"
  22. #include "guiTable.h"
  23. #include "constants.h"
  24. #include "gamedef.h"
  25. #include "keycode.h"
  26. #include "util/strfnd.h"
  27. #include <IGUICheckBox.h>
  28. #include <IGUIEditBox.h>
  29. #include <IGUIButton.h>
  30. #include <IGUIStaticText.h>
  31. #include <IGUIFont.h>
  32. #include <IGUITabControl.h>
  33. #include <IGUIComboBox.h>
  34. #include "client/renderingengine.h"
  35. #include "log.h"
  36. #include "client/tile.h" // ITextureSource
  37. #include "hud.h" // drawItemStack
  38. #include "filesys.h"
  39. #include "gettime.h"
  40. #include "gettext.h"
  41. #include "scripting_server.h"
  42. #include "porting.h"
  43. #include "settings.h"
  44. #include "client.h"
  45. #include "fontengine.h"
  46. #include "util/hex.h"
  47. #include "util/numeric.h"
  48. #include "util/string.h" // for parseColorString()
  49. #include "irrlicht_changes/static_text.h"
  50. #include "guiscalingfilter.h"
  51. #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
  52. #include "intlGUIEditBox.h"
  53. #endif
  54. #define MY_CHECKPOS(a,b) \
  55. if (v_pos.size() != 2) { \
  56. errorstream<< "Invalid pos for element " << a << "specified: \"" \
  57. << parts[b] << "\"" << std::endl; \
  58. return; \
  59. }
  60. #define MY_CHECKGEOM(a,b) \
  61. if (v_geom.size() != 2) { \
  62. errorstream<< "Invalid pos for element " << a << "specified: \"" \
  63. << parts[b] << "\"" << std::endl; \
  64. return; \
  65. }
  66. /*
  67. GUIFormSpecMenu
  68. */
  69. static unsigned int font_line_height(gui::IGUIFont *font)
  70. {
  71. return font->getDimension(L"Ay").Height + font->getKerningHeight();
  72. }
  73. GUIFormSpecMenu::GUIFormSpecMenu(JoystickController *joystick,
  74. gui::IGUIElement *parent, s32 id, IMenuManager *menumgr,
  75. Client *client, ISimpleTextureSource *tsrc, IFormSource *fsrc, TextDest *tdst,
  76. bool remap_dbl_click) :
  77. GUIModalMenu(RenderingEngine::get_gui_env(), parent, id, menumgr),
  78. m_invmgr(client),
  79. m_tsrc(tsrc),
  80. m_client(client),
  81. m_form_src(fsrc),
  82. m_text_dst(tdst),
  83. m_joystick(joystick),
  84. m_remap_dbl_click(remap_dbl_click)
  85. #ifdef __ANDROID__
  86. , m_JavaDialogFieldName("")
  87. #endif
  88. {
  89. current_keys_pending.key_down = false;
  90. current_keys_pending.key_up = false;
  91. current_keys_pending.key_enter = false;
  92. current_keys_pending.key_escape = false;
  93. m_doubleclickdetect[0].time = 0;
  94. m_doubleclickdetect[1].time = 0;
  95. m_doubleclickdetect[0].pos = v2s32(0, 0);
  96. m_doubleclickdetect[1].pos = v2s32(0, 0);
  97. m_tooltip_show_delay = (u32)g_settings->getS32("tooltip_show_delay");
  98. m_tooltip_append_itemname = g_settings->getBool("tooltip_append_itemname");
  99. }
  100. GUIFormSpecMenu::~GUIFormSpecMenu()
  101. {
  102. removeChildren();
  103. for (u32 i = 0; i < m_tables.size(); ++i) {
  104. GUITable *table = m_tables[i].second;
  105. table->drop();
  106. }
  107. delete m_selected_item;
  108. delete m_form_src;
  109. delete m_text_dst;
  110. }
  111. void GUIFormSpecMenu::removeChildren()
  112. {
  113. const core::list<gui::IGUIElement*> &children = getChildren();
  114. while(!children.empty()) {
  115. (*children.getLast())->remove();
  116. }
  117. if(m_tooltip_element) {
  118. m_tooltip_element->remove();
  119. m_tooltip_element->drop();
  120. m_tooltip_element = NULL;
  121. }
  122. }
  123. void GUIFormSpecMenu::setInitialFocus()
  124. {
  125. // Set initial focus according to following order of precedence:
  126. // 1. first empty editbox
  127. // 2. first editbox
  128. // 3. first table
  129. // 4. last button
  130. // 5. first focusable (not statictext, not tabheader)
  131. // 6. first child element
  132. core::list<gui::IGUIElement*> children = getChildren();
  133. // in case "children" contains any NULL elements, remove them
  134. for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
  135. it != children.end();) {
  136. if (*it)
  137. ++it;
  138. else
  139. it = children.erase(it);
  140. }
  141. // 1. first empty editbox
  142. for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
  143. it != children.end(); ++it) {
  144. if ((*it)->getType() == gui::EGUIET_EDIT_BOX
  145. && (*it)->getText()[0] == 0) {
  146. Environment->setFocus(*it);
  147. return;
  148. }
  149. }
  150. // 2. first editbox
  151. for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
  152. it != children.end(); ++it) {
  153. if ((*it)->getType() == gui::EGUIET_EDIT_BOX) {
  154. Environment->setFocus(*it);
  155. return;
  156. }
  157. }
  158. // 3. first table
  159. for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
  160. it != children.end(); ++it) {
  161. if ((*it)->getTypeName() == std::string("GUITable")) {
  162. Environment->setFocus(*it);
  163. return;
  164. }
  165. }
  166. // 4. last button
  167. for (core::list<gui::IGUIElement*>::Iterator it = children.getLast();
  168. it != children.end(); --it) {
  169. if ((*it)->getType() == gui::EGUIET_BUTTON) {
  170. Environment->setFocus(*it);
  171. return;
  172. }
  173. }
  174. // 5. first focusable (not statictext, not tabheader)
  175. for (core::list<gui::IGUIElement*>::Iterator it = children.begin();
  176. it != children.end(); ++it) {
  177. if ((*it)->getType() != gui::EGUIET_STATIC_TEXT &&
  178. (*it)->getType() != gui::EGUIET_TAB_CONTROL) {
  179. Environment->setFocus(*it);
  180. return;
  181. }
  182. }
  183. // 6. first child element
  184. if (children.empty())
  185. Environment->setFocus(this);
  186. else
  187. Environment->setFocus(*(children.begin()));
  188. }
  189. GUITable* GUIFormSpecMenu::getTable(const std::string &tablename)
  190. {
  191. for (u32 i = 0; i < m_tables.size(); ++i) {
  192. if (tablename == m_tables[i].first.fname)
  193. return m_tables[i].second;
  194. }
  195. return 0;
  196. }
  197. std::vector<std::string>* GUIFormSpecMenu::getDropDownValues(const std::string &name)
  198. {
  199. for (u32 i = 0; i < m_dropdowns.size(); ++i) {
  200. if (name == m_dropdowns[i].first.fname)
  201. return &m_dropdowns[i].second;
  202. }
  203. return NULL;
  204. }
  205. void GUIFormSpecMenu::parseSize(parserData* data, const std::string &element)
  206. {
  207. std::vector<std::string> parts = split(element,',');
  208. if (((parts.size() == 2) || parts.size() == 3) ||
  209. ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
  210. {
  211. if (parts[1].find(';') != std::string::npos)
  212. parts[1] = parts[1].substr(0,parts[1].find(';'));
  213. data->invsize.X = MYMAX(0, stof(parts[0]));
  214. data->invsize.Y = MYMAX(0, stof(parts[1]));
  215. lockSize(false);
  216. if (parts.size() == 3) {
  217. if (parts[2] == "true") {
  218. lockSize(true,v2u32(800,600));
  219. }
  220. }
  221. data->explicit_size = true;
  222. return;
  223. }
  224. errorstream<< "Invalid size element (" << parts.size() << "): '" << element << "'" << std::endl;
  225. }
  226. void GUIFormSpecMenu::parseContainer(parserData* data, const std::string &element)
  227. {
  228. std::vector<std::string> parts = split(element, ',');
  229. if (parts.size() >= 2) {
  230. if (parts[1].find(';') != std::string::npos)
  231. parts[1] = parts[1].substr(0, parts[1].find(';'));
  232. container_stack.push(pos_offset);
  233. pos_offset.X += stof(parts[0]);
  234. pos_offset.Y += stof(parts[1]);
  235. return;
  236. }
  237. errorstream<< "Invalid container start element (" << parts.size() << "): '" << element << "'" << std::endl;
  238. }
  239. void GUIFormSpecMenu::parseContainerEnd(parserData* data)
  240. {
  241. if (container_stack.empty()) {
  242. errorstream<< "Invalid container end element, no matching container start element" << std::endl;
  243. } else {
  244. pos_offset = container_stack.top();
  245. container_stack.pop();
  246. }
  247. }
  248. void GUIFormSpecMenu::parseList(parserData* data, const std::string &element)
  249. {
  250. if (m_client == 0) {
  251. warningstream<<"invalid use of 'list' with m_client==0"<<std::endl;
  252. return;
  253. }
  254. std::vector<std::string> parts = split(element,';');
  255. if (((parts.size() == 4) || (parts.size() == 5)) ||
  256. ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
  257. {
  258. std::string location = parts[0];
  259. std::string listname = parts[1];
  260. std::vector<std::string> v_pos = split(parts[2],',');
  261. std::vector<std::string> v_geom = split(parts[3],',');
  262. std::string startindex = "";
  263. if (parts.size() == 5)
  264. startindex = parts[4];
  265. MY_CHECKPOS("list",2);
  266. MY_CHECKGEOM("list",3);
  267. InventoryLocation loc;
  268. if(location == "context" || location == "current_name")
  269. loc = m_current_inventory_location;
  270. else
  271. loc.deSerialize(location);
  272. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  273. pos.X += stof(v_pos[0]) * (float)spacing.X;
  274. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  275. v2s32 geom;
  276. geom.X = stoi(v_geom[0]);
  277. geom.Y = stoi(v_geom[1]);
  278. s32 start_i = 0;
  279. if(startindex != "")
  280. start_i = stoi(startindex);
  281. if (geom.X < 0 || geom.Y < 0 || start_i < 0) {
  282. errorstream<< "Invalid list element: '" << element << "'" << std::endl;
  283. return;
  284. }
  285. if(!data->explicit_size)
  286. warningstream<<"invalid use of list without a size[] element"<<std::endl;
  287. m_inventorylists.push_back(ListDrawSpec(loc, listname, pos, geom, start_i));
  288. return;
  289. }
  290. errorstream<< "Invalid list element(" << parts.size() << "): '" << element << "'" << std::endl;
  291. }
  292. void GUIFormSpecMenu::parseListRing(parserData* data, const std::string &element)
  293. {
  294. if (m_client == 0) {
  295. errorstream << "WARNING: invalid use of 'listring' with m_client==0" << std::endl;
  296. return;
  297. }
  298. std::vector<std::string> parts = split(element, ';');
  299. if (parts.size() == 2) {
  300. std::string location = parts[0];
  301. std::string listname = parts[1];
  302. InventoryLocation loc;
  303. if (location == "context" || location == "current_name")
  304. loc = m_current_inventory_location;
  305. else
  306. loc.deSerialize(location);
  307. m_inventory_rings.push_back(ListRingSpec(loc, listname));
  308. return;
  309. } else if ((element == "") && (m_inventorylists.size() > 1)) {
  310. size_t siz = m_inventorylists.size();
  311. // insert the last two inv list elements into the list ring
  312. const ListDrawSpec &spa = m_inventorylists[siz - 2];
  313. const ListDrawSpec &spb = m_inventorylists[siz - 1];
  314. m_inventory_rings.push_back(ListRingSpec(spa.inventoryloc, spa.listname));
  315. m_inventory_rings.push_back(ListRingSpec(spb.inventoryloc, spb.listname));
  316. return;
  317. }
  318. errorstream<< "Invalid list ring element(" << parts.size() << ", "
  319. << m_inventorylists.size() << "): '" << element << "'" << std::endl;
  320. }
  321. void GUIFormSpecMenu::parseCheckbox(parserData* data, const std::string &element)
  322. {
  323. std::vector<std::string> parts = split(element,';');
  324. if (((parts.size() >= 3) && (parts.size() <= 4)) ||
  325. ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
  326. {
  327. std::vector<std::string> v_pos = split(parts[0],',');
  328. std::string name = parts[1];
  329. std::string label = parts[2];
  330. std::string selected = "";
  331. if (parts.size() >= 4)
  332. selected = parts[3];
  333. MY_CHECKPOS("checkbox",0);
  334. v2s32 pos = padding + pos_offset * spacing;
  335. pos.X += stof(v_pos[0]) * (float) spacing.X;
  336. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  337. bool fselected = false;
  338. if (selected == "true")
  339. fselected = true;
  340. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  341. core::rect<s32> rect = core::rect<s32>(
  342. pos.X, pos.Y + ((imgsize.Y/2) - m_btn_height),
  343. pos.X + m_font->getDimension(wlabel.c_str()).Width + 25, // text size + size of checkbox
  344. pos.Y + ((imgsize.Y/2) + m_btn_height));
  345. FieldSpec spec(
  346. name,
  347. wlabel, //Needed for displaying text on MSVC
  348. wlabel,
  349. 258+m_fields.size()
  350. );
  351. spec.ftype = f_CheckBox;
  352. gui::IGUICheckBox* e = Environment->addCheckBox(fselected, rect, this,
  353. spec.fid, spec.flabel.c_str());
  354. if (spec.fname == data->focused_fieldname) {
  355. Environment->setFocus(e);
  356. }
  357. m_checkboxes.push_back(std::pair<FieldSpec,gui::IGUICheckBox*>(spec,e));
  358. m_fields.push_back(spec);
  359. return;
  360. }
  361. errorstream<< "Invalid checkbox element(" << parts.size() << "): '" << element << "'" << std::endl;
  362. }
  363. void GUIFormSpecMenu::parseScrollBar(parserData* data, const std::string &element)
  364. {
  365. std::vector<std::string> parts = split(element,';');
  366. if (parts.size() >= 5) {
  367. std::vector<std::string> v_pos = split(parts[0],',');
  368. std::vector<std::string> v_dim = split(parts[1],',');
  369. std::string name = parts[3];
  370. std::string value = parts[4];
  371. MY_CHECKPOS("scrollbar",0);
  372. v2s32 pos = padding + pos_offset * spacing;
  373. pos.X += stof(v_pos[0]) * (float) spacing.X;
  374. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  375. if (v_dim.size() != 2) {
  376. errorstream<< "Invalid size for element " << "scrollbar"
  377. << "specified: \"" << parts[1] << "\"" << std::endl;
  378. return;
  379. }
  380. v2s32 dim;
  381. dim.X = stof(v_dim[0]) * (float) spacing.X;
  382. dim.Y = stof(v_dim[1]) * (float) spacing.Y;
  383. core::rect<s32> rect =
  384. core::rect<s32>(pos.X, pos.Y, pos.X + dim.X, pos.Y + dim.Y);
  385. FieldSpec spec(
  386. name,
  387. L"",
  388. L"",
  389. 258+m_fields.size()
  390. );
  391. bool is_horizontal = true;
  392. if (parts[2] == "vertical")
  393. is_horizontal = false;
  394. spec.ftype = f_ScrollBar;
  395. spec.send = true;
  396. gui::IGUIScrollBar* e =
  397. Environment->addScrollBar(is_horizontal,rect,this,spec.fid);
  398. e->setMax(1000);
  399. e->setMin(0);
  400. e->setPos(stoi(parts[4]));
  401. e->setSmallStep(10);
  402. e->setLargeStep(100);
  403. m_scrollbars.push_back(std::pair<FieldSpec,gui::IGUIScrollBar*>(spec,e));
  404. m_fields.push_back(spec);
  405. return;
  406. }
  407. errorstream<< "Invalid scrollbar element(" << parts.size() << "): '" << element << "'" << std::endl;
  408. }
  409. void GUIFormSpecMenu::parseImage(parserData* data, const std::string &element)
  410. {
  411. std::vector<std::string> parts = split(element,';');
  412. if ((parts.size() == 3) ||
  413. ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
  414. {
  415. std::vector<std::string> v_pos = split(parts[0],',');
  416. std::vector<std::string> v_geom = split(parts[1],',');
  417. std::string name = unescape_string(parts[2]);
  418. MY_CHECKPOS("image", 0);
  419. MY_CHECKGEOM("image", 1);
  420. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  421. pos.X += stof(v_pos[0]) * (float) spacing.X;
  422. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  423. v2s32 geom;
  424. geom.X = stof(v_geom[0]) * (float)imgsize.X;
  425. geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
  426. if (!data->explicit_size)
  427. warningstream<<"invalid use of image without a size[] element"<<std::endl;
  428. m_images.push_back(ImageDrawSpec(name, pos, geom));
  429. return;
  430. } else if (parts.size() == 2) {
  431. std::vector<std::string> v_pos = split(parts[0],',');
  432. std::string name = unescape_string(parts[1]);
  433. MY_CHECKPOS("image", 0);
  434. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  435. pos.X += stof(v_pos[0]) * (float) spacing.X;
  436. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  437. if (!data->explicit_size)
  438. warningstream<<"invalid use of image without a size[] element"<<std::endl;
  439. m_images.push_back(ImageDrawSpec(name, pos));
  440. return;
  441. }
  442. errorstream<< "Invalid image element(" << parts.size() << "): '" << element << "'" << std::endl;
  443. }
  444. void GUIFormSpecMenu::parseItemImage(parserData* data, const std::string &element)
  445. {
  446. std::vector<std::string> parts = split(element,';');
  447. if ((parts.size() == 3) ||
  448. ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
  449. {
  450. std::vector<std::string> v_pos = split(parts[0],',');
  451. std::vector<std::string> v_geom = split(parts[1],',');
  452. std::string name = parts[2];
  453. MY_CHECKPOS("itemimage",0);
  454. MY_CHECKGEOM("itemimage",1);
  455. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  456. pos.X += stof(v_pos[0]) * (float) spacing.X;
  457. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  458. v2s32 geom;
  459. geom.X = stof(v_geom[0]) * (float)imgsize.X;
  460. geom.Y = stof(v_geom[1]) * (float)imgsize.Y;
  461. if(!data->explicit_size)
  462. warningstream<<"invalid use of item_image without a size[] element"<<std::endl;
  463. m_itemimages.push_back(ImageDrawSpec("", name, pos, geom));
  464. return;
  465. }
  466. errorstream<< "Invalid ItemImage element(" << parts.size() << "): '" << element << "'" << std::endl;
  467. }
  468. void GUIFormSpecMenu::parseButton(parserData* data, const std::string &element,
  469. const std::string &type)
  470. {
  471. std::vector<std::string> parts = split(element,';');
  472. if ((parts.size() == 4) ||
  473. ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
  474. {
  475. std::vector<std::string> v_pos = split(parts[0],',');
  476. std::vector<std::string> v_geom = split(parts[1],',');
  477. std::string name = parts[2];
  478. std::string label = parts[3];
  479. MY_CHECKPOS("button",0);
  480. MY_CHECKGEOM("button",1);
  481. v2s32 pos = padding + pos_offset * spacing;
  482. pos.X += stof(v_pos[0]) * (float)spacing.X;
  483. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  484. v2s32 geom;
  485. geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
  486. pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
  487. core::rect<s32> rect =
  488. core::rect<s32>(pos.X, pos.Y - m_btn_height,
  489. pos.X + geom.X, pos.Y + m_btn_height);
  490. if(!data->explicit_size)
  491. warningstream<<"invalid use of button without a size[] element"<<std::endl;
  492. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  493. FieldSpec spec(
  494. name,
  495. wlabel,
  496. L"",
  497. 258+m_fields.size()
  498. );
  499. spec.ftype = f_Button;
  500. if(type == "button_exit")
  501. spec.is_exit = true;
  502. gui::IGUIButton* e = Environment->addButton(rect, this, spec.fid,
  503. spec.flabel.c_str());
  504. if (spec.fname == data->focused_fieldname) {
  505. Environment->setFocus(e);
  506. }
  507. m_fields.push_back(spec);
  508. return;
  509. }
  510. errorstream<< "Invalid button element(" << parts.size() << "): '" << element << "'" << std::endl;
  511. }
  512. void GUIFormSpecMenu::parseBackground(parserData* data, const std::string &element)
  513. {
  514. std::vector<std::string> parts = split(element,';');
  515. if (((parts.size() == 3) || (parts.size() == 4)) ||
  516. ((parts.size() > 4) && (m_formspec_version > FORMSPEC_API_VERSION)))
  517. {
  518. std::vector<std::string> v_pos = split(parts[0],',');
  519. std::vector<std::string> v_geom = split(parts[1],',');
  520. std::string name = unescape_string(parts[2]);
  521. MY_CHECKPOS("background",0);
  522. MY_CHECKGEOM("background",1);
  523. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  524. pos.X += stof(v_pos[0]) * (float)spacing.X - ((float)spacing.X - (float)imgsize.X)/2;
  525. pos.Y += stof(v_pos[1]) * (float)spacing.Y - ((float)spacing.Y - (float)imgsize.Y)/2;
  526. v2s32 geom;
  527. geom.X = stof(v_geom[0]) * (float)spacing.X;
  528. geom.Y = stof(v_geom[1]) * (float)spacing.Y;
  529. if (!data->explicit_size)
  530. warningstream<<"invalid use of background without a size[] element"<<std::endl;
  531. bool clip = false;
  532. if (parts.size() == 4 && is_yes(parts[3])) {
  533. pos.X = stoi(v_pos[0]); //acts as offset
  534. pos.Y = stoi(v_pos[1]); //acts as offset
  535. clip = true;
  536. }
  537. m_backgrounds.push_back(ImageDrawSpec(name, pos, geom, clip));
  538. return;
  539. }
  540. errorstream<< "Invalid background element(" << parts.size() << "): '" << element << "'" << std::endl;
  541. }
  542. void GUIFormSpecMenu::parseTableOptions(parserData* data, const std::string &element)
  543. {
  544. std::vector<std::string> parts = split(element,';');
  545. data->table_options.clear();
  546. for (size_t i = 0; i < parts.size(); ++i) {
  547. // Parse table option
  548. std::string opt = unescape_string(parts[i]);
  549. data->table_options.push_back(GUITable::splitOption(opt));
  550. }
  551. }
  552. void GUIFormSpecMenu::parseTableColumns(parserData* data, const std::string &element)
  553. {
  554. std::vector<std::string> parts = split(element,';');
  555. data->table_columns.clear();
  556. for (size_t i = 0; i < parts.size(); ++i) {
  557. std::vector<std::string> col_parts = split(parts[i],',');
  558. GUITable::TableColumn column;
  559. // Parse column type
  560. if (!col_parts.empty())
  561. column.type = col_parts[0];
  562. // Parse column options
  563. for (size_t j = 1; j < col_parts.size(); ++j) {
  564. std::string opt = unescape_string(col_parts[j]);
  565. column.options.push_back(GUITable::splitOption(opt));
  566. }
  567. data->table_columns.push_back(column);
  568. }
  569. }
  570. void GUIFormSpecMenu::parseTable(parserData* data, const std::string &element)
  571. {
  572. std::vector<std::string> parts = split(element,';');
  573. if (((parts.size() == 4) || (parts.size() == 5)) ||
  574. ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
  575. {
  576. std::vector<std::string> v_pos = split(parts[0],',');
  577. std::vector<std::string> v_geom = split(parts[1],',');
  578. std::string name = parts[2];
  579. std::vector<std::string> items = split(parts[3],',');
  580. std::string str_initial_selection = "";
  581. std::string str_transparent = "false";
  582. if (parts.size() >= 5)
  583. str_initial_selection = parts[4];
  584. MY_CHECKPOS("table",0);
  585. MY_CHECKGEOM("table",1);
  586. v2s32 pos = padding + pos_offset * spacing;
  587. pos.X += stof(v_pos[0]) * (float)spacing.X;
  588. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  589. v2s32 geom;
  590. geom.X = stof(v_geom[0]) * (float)spacing.X;
  591. geom.Y = stof(v_geom[1]) * (float)spacing.Y;
  592. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  593. FieldSpec spec(
  594. name,
  595. L"",
  596. L"",
  597. 258+m_fields.size()
  598. );
  599. spec.ftype = f_Table;
  600. for (unsigned int i = 0; i < items.size(); ++i) {
  601. items[i] = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(items[i]))));
  602. }
  603. //now really show table
  604. GUITable *e = new GUITable(Environment, this, spec.fid, rect,
  605. m_tsrc);
  606. if (spec.fname == data->focused_fieldname) {
  607. Environment->setFocus(e);
  608. }
  609. e->setTable(data->table_options, data->table_columns, items);
  610. if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
  611. e->setDynamicData(data->table_dyndata[name]);
  612. }
  613. if ((str_initial_selection != "") &&
  614. (str_initial_selection != "0"))
  615. e->setSelected(stoi(str_initial_selection.c_str()));
  616. m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
  617. m_fields.push_back(spec);
  618. return;
  619. }
  620. errorstream<< "Invalid table element(" << parts.size() << "): '" << element << "'" << std::endl;
  621. }
  622. void GUIFormSpecMenu::parseTextList(parserData* data, const std::string &element)
  623. {
  624. std::vector<std::string> parts = split(element,';');
  625. if (((parts.size() == 4) || (parts.size() == 5) || (parts.size() == 6)) ||
  626. ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
  627. {
  628. std::vector<std::string> v_pos = split(parts[0],',');
  629. std::vector<std::string> v_geom = split(parts[1],',');
  630. std::string name = parts[2];
  631. std::vector<std::string> items = split(parts[3],',');
  632. std::string str_initial_selection = "";
  633. std::string str_transparent = "false";
  634. if (parts.size() >= 5)
  635. str_initial_selection = parts[4];
  636. if (parts.size() >= 6)
  637. str_transparent = parts[5];
  638. MY_CHECKPOS("textlist",0);
  639. MY_CHECKGEOM("textlist",1);
  640. v2s32 pos = padding + pos_offset * spacing;
  641. pos.X += stof(v_pos[0]) * (float)spacing.X;
  642. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  643. v2s32 geom;
  644. geom.X = stof(v_geom[0]) * (float)spacing.X;
  645. geom.Y = stof(v_geom[1]) * (float)spacing.Y;
  646. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  647. FieldSpec spec(
  648. name,
  649. L"",
  650. L"",
  651. 258+m_fields.size()
  652. );
  653. spec.ftype = f_Table;
  654. for (unsigned int i = 0; i < items.size(); ++i) {
  655. items[i] = wide_to_utf8(unescape_translate(utf8_to_wide(unescape_string(items[i]))));
  656. }
  657. //now really show list
  658. GUITable *e = new GUITable(Environment, this, spec.fid, rect,
  659. m_tsrc);
  660. if (spec.fname == data->focused_fieldname) {
  661. Environment->setFocus(e);
  662. }
  663. e->setTextList(items, is_yes(str_transparent));
  664. if (data->table_dyndata.find(name) != data->table_dyndata.end()) {
  665. e->setDynamicData(data->table_dyndata[name]);
  666. }
  667. if ((str_initial_selection != "") &&
  668. (str_initial_selection != "0"))
  669. e->setSelected(stoi(str_initial_selection.c_str()));
  670. m_tables.push_back(std::pair<FieldSpec,GUITable*>(spec, e));
  671. m_fields.push_back(spec);
  672. return;
  673. }
  674. errorstream<< "Invalid textlist element(" << parts.size() << "): '" << element << "'" << std::endl;
  675. }
  676. void GUIFormSpecMenu::parseDropDown(parserData* data, const std::string &element)
  677. {
  678. std::vector<std::string> parts = split(element,';');
  679. if ((parts.size() == 5) ||
  680. ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
  681. {
  682. std::vector<std::string> v_pos = split(parts[0],',');
  683. std::string name = parts[2];
  684. std::vector<std::string> items = split(parts[3],',');
  685. std::string str_initial_selection = "";
  686. str_initial_selection = parts[4];
  687. MY_CHECKPOS("dropdown",0);
  688. v2s32 pos = padding + pos_offset * spacing;
  689. pos.X += stof(v_pos[0]) * (float)spacing.X;
  690. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  691. s32 width = stof(parts[1]) * (float)spacing.Y;
  692. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y,
  693. pos.X + width, pos.Y + (m_btn_height * 2));
  694. FieldSpec spec(
  695. name,
  696. L"",
  697. L"",
  698. 258+m_fields.size()
  699. );
  700. spec.ftype = f_DropDown;
  701. spec.send = true;
  702. //now really show list
  703. gui::IGUIComboBox *e = Environment->addComboBox(rect, this,spec.fid);
  704. if (spec.fname == data->focused_fieldname) {
  705. Environment->setFocus(e);
  706. }
  707. for (unsigned int i=0; i < items.size(); i++) {
  708. e->addItem(unescape_translate(unescape_string(
  709. utf8_to_wide(items[i]))).c_str());
  710. }
  711. if (str_initial_selection != "")
  712. e->setSelected(stoi(str_initial_selection.c_str())-1);
  713. m_fields.push_back(spec);
  714. m_dropdowns.push_back(std::pair<FieldSpec,
  715. std::vector<std::string> >(spec, std::vector<std::string>()));
  716. std::vector<std::string> &values = m_dropdowns.back().second;
  717. for (unsigned int i = 0; i < items.size(); i++) {
  718. values.push_back(unescape_string(items[i]));
  719. }
  720. return;
  721. }
  722. errorstream << "Invalid dropdown element(" << parts.size() << "): '"
  723. << element << "'" << std::endl;
  724. }
  725. void GUIFormSpecMenu::parseFieldCloseOnEnter(parserData *data, const std::string &element)
  726. {
  727. std::vector<std::string> parts = split(element,';');
  728. if (parts.size() == 2 ||
  729. (parts.size() > 2 && m_formspec_version > FORMSPEC_API_VERSION)) {
  730. field_close_on_enter[parts[0]] = is_yes(parts[1]);
  731. }
  732. }
  733. void GUIFormSpecMenu::parsePwdField(parserData* data, const std::string &element)
  734. {
  735. std::vector<std::string> parts = split(element,';');
  736. if ((parts.size() == 4) || (parts.size() == 5) ||
  737. ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
  738. {
  739. std::vector<std::string> v_pos = split(parts[0],',');
  740. std::vector<std::string> v_geom = split(parts[1],',');
  741. std::string name = parts[2];
  742. std::string label = parts[3];
  743. MY_CHECKPOS("pwdfield",0);
  744. MY_CHECKGEOM("pwdfield",1);
  745. v2s32 pos = pos_offset * spacing;
  746. pos.X += stof(v_pos[0]) * (float)spacing.X;
  747. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  748. v2s32 geom;
  749. geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
  750. pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
  751. pos.Y -= m_btn_height;
  752. geom.Y = m_btn_height*2;
  753. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  754. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  755. FieldSpec spec(
  756. name,
  757. wlabel,
  758. L"",
  759. 258+m_fields.size()
  760. );
  761. spec.send = true;
  762. gui::IGUIEditBox * e = Environment->addEditBox(0, rect, true, this, spec.fid);
  763. if (spec.fname == data->focused_fieldname) {
  764. Environment->setFocus(e);
  765. }
  766. if (label.length() >= 1)
  767. {
  768. int font_height = g_fontengine->getTextHeight();
  769. rect.UpperLeftCorner.Y -= font_height;
  770. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
  771. addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
  772. }
  773. e->setPasswordBox(true,L'*');
  774. irr::SEvent evt;
  775. evt.EventType = EET_KEY_INPUT_EVENT;
  776. evt.KeyInput.Key = KEY_END;
  777. evt.KeyInput.Char = 0;
  778. evt.KeyInput.Control = 0;
  779. evt.KeyInput.Shift = 0;
  780. evt.KeyInput.PressedDown = true;
  781. e->OnEvent(evt);
  782. if (parts.size() >= 5) {
  783. // TODO: remove after 2016-11-03
  784. warningstream << "pwdfield: use field_close_on_enter[name, enabled]" <<
  785. " instead of the 5th param" << std::endl;
  786. field_close_on_enter[name] = is_yes(parts[4]);
  787. }
  788. m_fields.push_back(spec);
  789. return;
  790. }
  791. errorstream<< "Invalid pwdfield element(" << parts.size() << "): '" << element << "'" << std::endl;
  792. }
  793. void GUIFormSpecMenu::parseSimpleField(parserData* data,
  794. std::vector<std::string> &parts)
  795. {
  796. std::string name = parts[0];
  797. std::string label = parts[1];
  798. std::string default_val = parts[2];
  799. core::rect<s32> rect;
  800. if(data->explicit_size)
  801. warningstream<<"invalid use of unpositioned \"field\" in inventory"<<std::endl;
  802. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  803. pos.Y = ((m_fields.size()+2)*60);
  804. v2s32 size = DesiredRect.getSize();
  805. rect = core::rect<s32>(size.X / 2 - 150, pos.Y,
  806. (size.X / 2 - 150) + 300, pos.Y + (m_btn_height*2));
  807. if(m_form_src)
  808. default_val = m_form_src->resolveText(default_val);
  809. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  810. FieldSpec spec(
  811. name,
  812. wlabel,
  813. utf8_to_wide(unescape_string(default_val)),
  814. 258+m_fields.size()
  815. );
  816. if (name == "")
  817. {
  818. // spec field id to 0, this stops submit searching for a value that isn't there
  819. addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
  820. }
  821. else
  822. {
  823. spec.send = true;
  824. gui::IGUIElement *e;
  825. #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
  826. if (g_settings->getBool("freetype")) {
  827. e = (gui::IGUIElement *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
  828. true, Environment, this, spec.fid, rect);
  829. e->drop();
  830. } else {
  831. #else
  832. {
  833. #endif
  834. e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
  835. }
  836. if (spec.fname == data->focused_fieldname) {
  837. Environment->setFocus(e);
  838. }
  839. irr::SEvent evt;
  840. evt.EventType = EET_KEY_INPUT_EVENT;
  841. evt.KeyInput.Key = KEY_END;
  842. evt.KeyInput.Char = 0;
  843. evt.KeyInput.Control = 0;
  844. evt.KeyInput.Shift = 0;
  845. evt.KeyInput.PressedDown = true;
  846. e->OnEvent(evt);
  847. if (label.length() >= 1)
  848. {
  849. int font_height = g_fontengine->getTextHeight();
  850. rect.UpperLeftCorner.Y -= font_height;
  851. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
  852. addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
  853. }
  854. }
  855. if (parts.size() >= 4) {
  856. // TODO: remove after 2016-11-03
  857. warningstream << "field/simple: use field_close_on_enter[name, enabled]" <<
  858. " instead of the 4th param" << std::endl;
  859. field_close_on_enter[name] = is_yes(parts[3]);
  860. }
  861. m_fields.push_back(spec);
  862. }
  863. void GUIFormSpecMenu::parseTextArea(parserData* data, std::vector<std::string>& parts,
  864. const std::string &type)
  865. {
  866. std::vector<std::string> v_pos = split(parts[0],',');
  867. std::vector<std::string> v_geom = split(parts[1],',');
  868. std::string name = parts[2];
  869. std::string label = parts[3];
  870. std::string default_val = parts[4];
  871. MY_CHECKPOS(type,0);
  872. MY_CHECKGEOM(type,1);
  873. v2s32 pos = pos_offset * spacing;
  874. pos.X += stof(v_pos[0]) * (float) spacing.X;
  875. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  876. v2s32 geom;
  877. geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
  878. if (type == "textarea")
  879. {
  880. geom.Y = (stof(v_geom[1]) * (float)imgsize.Y) - (spacing.Y-imgsize.Y);
  881. pos.Y += m_btn_height;
  882. }
  883. else
  884. {
  885. pos.Y += (stof(v_geom[1]) * (float)imgsize.Y)/2;
  886. pos.Y -= m_btn_height;
  887. geom.Y = m_btn_height*2;
  888. }
  889. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  890. if(!data->explicit_size)
  891. warningstream<<"invalid use of positioned "<<type<<" without a size[] element"<<std::endl;
  892. if(m_form_src)
  893. default_val = m_form_src->resolveText(default_val);
  894. std::wstring wlabel = translate_string(utf8_to_wide(unescape_string(label)));
  895. FieldSpec spec(
  896. name,
  897. wlabel,
  898. utf8_to_wide(unescape_string(default_val)),
  899. 258+m_fields.size()
  900. );
  901. if (name == "")
  902. {
  903. // spec field id to 0, this stops submit searching for a value that isn't there
  904. addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, spec.fid);
  905. }
  906. else
  907. {
  908. spec.send = true;
  909. gui::IGUIEditBox *e;
  910. #if USE_FREETYPE && IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 9
  911. if (g_settings->getBool("freetype")) {
  912. e = (gui::IGUIEditBox *) new gui::intlGUIEditBox(spec.fdefault.c_str(),
  913. true, Environment, this, spec.fid, rect);
  914. e->drop();
  915. } else {
  916. #else
  917. {
  918. #endif
  919. e = Environment->addEditBox(spec.fdefault.c_str(), rect, true, this, spec.fid);
  920. }
  921. if (spec.fname == data->focused_fieldname) {
  922. Environment->setFocus(e);
  923. }
  924. if (type == "textarea")
  925. {
  926. e->setMultiLine(true);
  927. e->setWordWrap(true);
  928. e->setTextAlignment(gui::EGUIA_UPPERLEFT, gui::EGUIA_UPPERLEFT);
  929. } else {
  930. irr::SEvent evt;
  931. evt.EventType = EET_KEY_INPUT_EVENT;
  932. evt.KeyInput.Key = KEY_END;
  933. evt.KeyInput.Char = 0;
  934. evt.KeyInput.Control = 0;
  935. evt.KeyInput.Shift = 0;
  936. evt.KeyInput.PressedDown = true;
  937. e->OnEvent(evt);
  938. }
  939. if (label.length() >= 1)
  940. {
  941. int font_height = g_fontengine->getTextHeight();
  942. rect.UpperLeftCorner.Y -= font_height;
  943. rect.LowerRightCorner.Y = rect.UpperLeftCorner.Y + font_height;
  944. addStaticText(Environment, spec.flabel.c_str(), rect, false, true, this, 0);
  945. }
  946. }
  947. if (parts.size() >= 6) {
  948. // TODO: remove after 2016-11-03
  949. warningstream << "field/textarea: use field_close_on_enter[name, enabled]" <<
  950. " instead of the 6th param" << std::endl;
  951. field_close_on_enter[name] = is_yes(parts[5]);
  952. }
  953. m_fields.push_back(spec);
  954. }
  955. void GUIFormSpecMenu::parseField(parserData* data, const std::string &element,
  956. const std::string &type)
  957. {
  958. std::vector<std::string> parts = split(element,';');
  959. if (parts.size() == 3 || parts.size() == 4) {
  960. parseSimpleField(data,parts);
  961. return;
  962. }
  963. if ((parts.size() == 5) || (parts.size() == 6) ||
  964. ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
  965. {
  966. parseTextArea(data,parts,type);
  967. return;
  968. }
  969. errorstream<< "Invalid field element(" << parts.size() << "): '" << element << "'" << std::endl;
  970. }
  971. void GUIFormSpecMenu::parseLabel(parserData* data, const std::string &element)
  972. {
  973. std::vector<std::string> parts = split(element,';');
  974. if ((parts.size() == 2) ||
  975. ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
  976. {
  977. std::vector<std::string> v_pos = split(parts[0],',');
  978. std::string text = parts[1];
  979. MY_CHECKPOS("label",0);
  980. v2s32 pos = padding + pos_offset * spacing;
  981. pos.X += stof(v_pos[0]) * (float)spacing.X;
  982. pos.Y += (stof(v_pos[1]) + 7.0/30.0) * (float)spacing.Y;
  983. if(!data->explicit_size)
  984. warningstream<<"invalid use of label without a size[] element"<<std::endl;
  985. std::vector<std::string> lines = split(text, '\n');
  986. for (unsigned int i = 0; i != lines.size(); i++)
  987. {
  988. // Lines are spaced at the nominal distance of
  989. // 2/5 inventory slot, even if the font doesn't
  990. // quite match that. This provides consistent
  991. // form layout, at the expense of sometimes
  992. // having sub-optimal spacing for the font.
  993. // We multiply by 2 and then divide by 5, rather
  994. // than multiply by 0.4, to get exact results
  995. // in the integer cases: 0.4 is not exactly
  996. // representable in binary floating point.
  997. s32 posy = pos.Y + ((float)i) * spacing.Y * 2.0 / 5.0;
  998. std::wstring wlabel = unescape_translate
  999. (unescape_string (utf8_to_wide (lines [i])));
  1000. core::rect<s32> rect = core::rect<s32>(
  1001. pos.X, posy - m_btn_height,
  1002. pos.X + m_font->getDimension(wlabel.c_str()).Width,
  1003. posy + m_btn_height);
  1004. FieldSpec spec(
  1005. "",
  1006. wlabel,
  1007. L"",
  1008. 258+m_fields.size()
  1009. );
  1010. gui::IGUIStaticText *e =
  1011. addStaticText(Environment, spec.flabel.c_str(),
  1012. rect, false, false, this, spec.fid);
  1013. e->setTextAlignment(gui::EGUIA_UPPERLEFT,
  1014. gui::EGUIA_CENTER);
  1015. m_fields.push_back(spec);
  1016. }
  1017. return;
  1018. }
  1019. errorstream<< "Invalid label element(" << parts.size() << "): '" << element << "'" << std::endl;
  1020. }
  1021. void GUIFormSpecMenu::parseVertLabel(parserData* data, const std::string &element)
  1022. {
  1023. std::vector<std::string> parts = split(element,';');
  1024. if ((parts.size() == 2) ||
  1025. ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
  1026. {
  1027. std::vector<std::string> v_pos = split(parts[0],',');
  1028. std::wstring text = unescape_translate(
  1029. unescape_string(utf8_to_wide(parts[1])));
  1030. MY_CHECKPOS("vertlabel",1);
  1031. v2s32 pos = padding + pos_offset * spacing;
  1032. pos.X += stof(v_pos[0]) * (float)spacing.X;
  1033. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  1034. core::rect<s32> rect = core::rect<s32>(
  1035. pos.X, pos.Y+((imgsize.Y/2)- m_btn_height),
  1036. pos.X+15, pos.Y +
  1037. font_line_height(m_font)
  1038. * (text.length()+1)
  1039. +((imgsize.Y/2)- m_btn_height));
  1040. //actually text.length() would be correct but adding +1 avoids to break all mods
  1041. if(!data->explicit_size)
  1042. warningstream<<"invalid use of label without a size[] element"<<std::endl;
  1043. std::wstring label = L"";
  1044. for (unsigned int i=0; i < text.length(); i++) {
  1045. label += text[i];
  1046. label += L"\n";
  1047. }
  1048. FieldSpec spec(
  1049. "",
  1050. label,
  1051. L"",
  1052. 258+m_fields.size()
  1053. );
  1054. gui::IGUIStaticText *t =
  1055. addStaticText(Environment, spec.flabel.c_str(), rect, false, false, this, spec.fid);
  1056. t->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
  1057. m_fields.push_back(spec);
  1058. return;
  1059. }
  1060. errorstream<< "Invalid vertlabel element(" << parts.size() << "): '" << element << "'" << std::endl;
  1061. }
  1062. void GUIFormSpecMenu::parseImageButton(parserData* data, const std::string &element,
  1063. const std::string &type)
  1064. {
  1065. std::vector<std::string> parts = split(element,';');
  1066. if ((((parts.size() >= 5) && (parts.size() <= 8)) && (parts.size() != 6)) ||
  1067. ((parts.size() > 8) && (m_formspec_version > FORMSPEC_API_VERSION)))
  1068. {
  1069. std::vector<std::string> v_pos = split(parts[0],',');
  1070. std::vector<std::string> v_geom = split(parts[1],',');
  1071. std::string image_name = parts[2];
  1072. std::string name = parts[3];
  1073. std::string label = parts[4];
  1074. MY_CHECKPOS("imagebutton",0);
  1075. MY_CHECKGEOM("imagebutton",1);
  1076. v2s32 pos = padding + pos_offset * spacing;
  1077. pos.X += stof(v_pos[0]) * (float)spacing.X;
  1078. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  1079. v2s32 geom;
  1080. geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
  1081. geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
  1082. bool noclip = false;
  1083. bool drawborder = true;
  1084. std::string pressed_image_name = "";
  1085. if (parts.size() >= 7) {
  1086. if (parts[5] == "true")
  1087. noclip = true;
  1088. if (parts[6] == "false")
  1089. drawborder = false;
  1090. }
  1091. if (parts.size() >= 8) {
  1092. pressed_image_name = parts[7];
  1093. }
  1094. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  1095. if(!data->explicit_size)
  1096. warningstream<<"invalid use of image_button without a size[] element"<<std::endl;
  1097. image_name = unescape_string(image_name);
  1098. pressed_image_name = unescape_string(pressed_image_name);
  1099. std::wstring wlabel = utf8_to_wide(unescape_string(label));
  1100. FieldSpec spec(
  1101. name,
  1102. wlabel,
  1103. utf8_to_wide(image_name),
  1104. 258+m_fields.size()
  1105. );
  1106. spec.ftype = f_Button;
  1107. if(type == "image_button_exit")
  1108. spec.is_exit = true;
  1109. video::ITexture *texture = 0;
  1110. video::ITexture *pressed_texture = 0;
  1111. texture = m_tsrc->getTexture(image_name);
  1112. if (pressed_image_name != "")
  1113. pressed_texture = m_tsrc->getTexture(pressed_image_name);
  1114. else
  1115. pressed_texture = texture;
  1116. gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, spec.flabel.c_str());
  1117. if (spec.fname == data->focused_fieldname) {
  1118. Environment->setFocus(e);
  1119. }
  1120. e->setUseAlphaChannel(true);
  1121. e->setImage(guiScalingImageButton(
  1122. Environment->getVideoDriver(), texture, geom.X, geom.Y));
  1123. e->setPressedImage(guiScalingImageButton(
  1124. Environment->getVideoDriver(), pressed_texture, geom.X, geom.Y));
  1125. e->setScaleImage(true);
  1126. e->setNotClipped(noclip);
  1127. e->setDrawBorder(drawborder);
  1128. m_fields.push_back(spec);
  1129. return;
  1130. }
  1131. errorstream<< "Invalid imagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
  1132. }
  1133. void GUIFormSpecMenu::parseTabHeader(parserData* data, const std::string &element)
  1134. {
  1135. std::vector<std::string> parts = split(element,';');
  1136. if (((parts.size() == 4) || (parts.size() == 6)) ||
  1137. ((parts.size() > 6) && (m_formspec_version > FORMSPEC_API_VERSION)))
  1138. {
  1139. std::vector<std::string> v_pos = split(parts[0],',');
  1140. std::string name = parts[1];
  1141. std::vector<std::string> buttons = split(parts[2],',');
  1142. std::string str_index = parts[3];
  1143. bool show_background = true;
  1144. bool show_border = true;
  1145. int tab_index = stoi(str_index) -1;
  1146. MY_CHECKPOS("tabheader",0);
  1147. if (parts.size() == 6) {
  1148. if (parts[4] == "true")
  1149. show_background = false;
  1150. if (parts[5] == "false")
  1151. show_border = false;
  1152. }
  1153. FieldSpec spec(
  1154. name,
  1155. L"",
  1156. L"",
  1157. 258+m_fields.size()
  1158. );
  1159. spec.ftype = f_TabHeader;
  1160. v2s32 pos = pos_offset * spacing;
  1161. pos.X += stof(v_pos[0]) * (float)spacing.X;
  1162. pos.Y += stof(v_pos[1]) * (float)spacing.Y - m_btn_height * 2;
  1163. v2s32 geom;
  1164. geom.X = DesiredRect.getWidth();
  1165. geom.Y = m_btn_height*2;
  1166. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X,
  1167. pos.Y+geom.Y);
  1168. gui::IGUITabControl *e = Environment->addTabControl(rect, this,
  1169. show_background, show_border, spec.fid);
  1170. e->setAlignment(irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_UPPERLEFT,
  1171. irr::gui::EGUIA_UPPERLEFT, irr::gui::EGUIA_LOWERRIGHT);
  1172. e->setTabHeight(m_btn_height*2);
  1173. if (spec.fname == data->focused_fieldname) {
  1174. Environment->setFocus(e);
  1175. }
  1176. e->setNotClipped(true);
  1177. for (unsigned int i = 0; i < buttons.size(); i++) {
  1178. e->addTab(unescape_translate(unescape_string(
  1179. utf8_to_wide(buttons[i]))).c_str(), -1);
  1180. }
  1181. if ((tab_index >= 0) &&
  1182. (buttons.size() < INT_MAX) &&
  1183. (tab_index < (int) buttons.size()))
  1184. e->setActiveTab(tab_index);
  1185. m_fields.push_back(spec);
  1186. return;
  1187. }
  1188. errorstream << "Invalid TabHeader element(" << parts.size() << "): '"
  1189. << element << "'" << std::endl;
  1190. }
  1191. void GUIFormSpecMenu::parseItemImageButton(parserData* data, const std::string &element)
  1192. {
  1193. if (m_client == 0) {
  1194. warningstream << "invalid use of item_image_button with m_client==0"
  1195. << std::endl;
  1196. return;
  1197. }
  1198. std::vector<std::string> parts = split(element,';');
  1199. if ((parts.size() == 5) ||
  1200. ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
  1201. {
  1202. std::vector<std::string> v_pos = split(parts[0],',');
  1203. std::vector<std::string> v_geom = split(parts[1],',');
  1204. std::string item_name = parts[2];
  1205. std::string name = parts[3];
  1206. std::string label = parts[4];
  1207. label = unescape_string(label);
  1208. item_name = unescape_string(item_name);
  1209. MY_CHECKPOS("itemimagebutton",0);
  1210. MY_CHECKGEOM("itemimagebutton",1);
  1211. v2s32 pos = padding + pos_offset * spacing;
  1212. pos.X += stof(v_pos[0]) * (float)spacing.X;
  1213. pos.Y += stof(v_pos[1]) * (float)spacing.Y;
  1214. v2s32 geom;
  1215. geom.X = (stof(v_geom[0]) * (float)spacing.X)-(spacing.X-imgsize.X);
  1216. geom.Y = (stof(v_geom[1]) * (float)spacing.Y)-(spacing.Y-imgsize.Y);
  1217. core::rect<s32> rect = core::rect<s32>(pos.X, pos.Y, pos.X+geom.X, pos.Y+geom.Y);
  1218. if(!data->explicit_size)
  1219. warningstream<<"invalid use of item_image_button without a size[] element"<<std::endl;
  1220. IItemDefManager *idef = m_client->idef();
  1221. ItemStack item;
  1222. item.deSerialize(item_name, idef);
  1223. m_tooltips[name] =
  1224. TooltipSpec(utf8_to_wide(item.getDefinition(idef).description),
  1225. m_default_tooltip_bgcolor,
  1226. m_default_tooltip_color);
  1227. FieldSpec spec(
  1228. name,
  1229. utf8_to_wide(label),
  1230. utf8_to_wide(item_name),
  1231. 258 + m_fields.size()
  1232. );
  1233. gui::IGUIButton *e = Environment->addButton(rect, this, spec.fid, L"");
  1234. if (spec.fname == data->focused_fieldname) {
  1235. Environment->setFocus(e);
  1236. }
  1237. spec.ftype = f_Button;
  1238. rect+=data->basepos-padding;
  1239. spec.rect=rect;
  1240. m_fields.push_back(spec);
  1241. pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  1242. pos.X += stof(v_pos[0]) * (float) spacing.X;
  1243. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  1244. m_itemimages.push_back(ImageDrawSpec("", item_name, e, pos, geom));
  1245. m_static_texts.push_back(StaticTextSpec(utf8_to_wide(label), rect, e));
  1246. return;
  1247. }
  1248. errorstream<< "Invalid ItemImagebutton element(" << parts.size() << "): '" << element << "'" << std::endl;
  1249. }
  1250. void GUIFormSpecMenu::parseBox(parserData* data, const std::string &element)
  1251. {
  1252. std::vector<std::string> parts = split(element,';');
  1253. if ((parts.size() == 3) ||
  1254. ((parts.size() > 3) && (m_formspec_version > FORMSPEC_API_VERSION)))
  1255. {
  1256. std::vector<std::string> v_pos = split(parts[0],',');
  1257. std::vector<std::string> v_geom = split(parts[1],',');
  1258. MY_CHECKPOS("box",0);
  1259. MY_CHECKGEOM("box",1);
  1260. v2s32 pos = padding + AbsoluteRect.UpperLeftCorner + pos_offset * spacing;
  1261. pos.X += stof(v_pos[0]) * (float) spacing.X;
  1262. pos.Y += stof(v_pos[1]) * (float) spacing.Y;
  1263. v2s32 geom;
  1264. geom.X = stof(v_geom[0]) * (float)spacing.X;
  1265. geom.Y = stof(v_geom[1]) * (float)spacing.Y;
  1266. video::SColor tmp_color;
  1267. if (parseColorString(parts[2], tmp_color, false, 0x8C)) {
  1268. BoxDrawSpec spec(pos, geom, tmp_color);
  1269. m_boxes.push_back(spec);
  1270. }
  1271. else {
  1272. errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "' INVALID COLOR" << std::endl;
  1273. }
  1274. return;
  1275. }
  1276. errorstream<< "Invalid Box element(" << parts.size() << "): '" << element << "'" << std::endl;
  1277. }
  1278. void GUIFormSpecMenu::parseBackgroundColor(parserData* data, const std::string &element)
  1279. {
  1280. std::vector<std::string> parts = split(element,';');
  1281. if (((parts.size() == 1) || (parts.size() == 2)) ||
  1282. ((parts.size() > 2) && (m_formspec_version > FORMSPEC_API_VERSION)))
  1283. {
  1284. parseColorString(parts[0],m_bgcolor,false);
  1285. if (parts.size() == 2) {
  1286. std::string fullscreen = parts[1];
  1287. m_bgfullscreen = is_yes(fullscreen);
  1288. }
  1289. return;
  1290. }
  1291. errorstream<< "Invalid bgcolor element(" << parts.size() << "): '" << element << "'" << std::endl;
  1292. }
  1293. void GUIFormSpecMenu::parseListColors(parserData* data, const std::string &element)
  1294. {
  1295. std::vector<std::string> parts = split(element,';');
  1296. if (((parts.size() == 2) || (parts.size() == 3) || (parts.size() == 5)) ||
  1297. ((parts.size() > 5) && (m_formspec_version > FORMSPEC_API_VERSION)))
  1298. {
  1299. parseColorString(parts[0], m_slotbg_n, false);
  1300. parseColorString(parts[1], m_slotbg_h, false);
  1301. if (parts.size() >= 3) {
  1302. if (parseColorString(parts[2], m_slotbordercolor, false)) {
  1303. m_slotborder = true;
  1304. }
  1305. }
  1306. if (parts.size() == 5) {
  1307. video::SColor tmp_color;
  1308. if (parseColorString(parts[3], tmp_color, false))
  1309. m_default_tooltip_bgcolor = tmp_color;
  1310. if (parseColorString(parts[4], tmp_color, false))
  1311. m_default_tooltip_color = tmp_color;
  1312. }
  1313. return;
  1314. }
  1315. errorstream<< "Invalid listcolors element(" << parts.size() << "): '" << element << "'" << std::endl;
  1316. }
  1317. void GUIFormSpecMenu::parseTooltip(parserData* data, const std::string &element)
  1318. {
  1319. std::vector<std::string> parts = split(element,';');
  1320. if (parts.size() == 2) {
  1321. std::string name = parts[0];
  1322. m_tooltips[name] = TooltipSpec(utf8_to_wide(unescape_string(parts[1])),
  1323. m_default_tooltip_bgcolor, m_default_tooltip_color);
  1324. return;
  1325. } else if (parts.size() == 4) {
  1326. std::string name = parts[0];
  1327. video::SColor tmp_color1, tmp_color2;
  1328. if ( parseColorString(parts[2], tmp_color1, false) && parseColorString(parts[3], tmp_color2, false) ) {
  1329. m_tooltips[name] = TooltipSpec(utf8_to_wide(unescape_string(parts[1])),
  1330. tmp_color1, tmp_color2);
  1331. return;
  1332. }
  1333. }
  1334. errorstream<< "Invalid tooltip element(" << parts.size() << "): '" << element << "'" << std::endl;
  1335. }
  1336. bool GUIFormSpecMenu::parseVersionDirect(const std::string &data)
  1337. {
  1338. //some prechecks
  1339. if (data == "")
  1340. return false;
  1341. std::vector<std::string> parts = split(data,'[');
  1342. if (parts.size() < 2) {
  1343. return false;
  1344. }
  1345. if (trim(parts[0]) != "formspec_version") {
  1346. return false;
  1347. }
  1348. if (is_number(parts[1])) {
  1349. m_formspec_version = mystoi(parts[1]);
  1350. return true;
  1351. }
  1352. return false;
  1353. }
  1354. bool GUIFormSpecMenu::parseSizeDirect(parserData* data, const std::string &element)
  1355. {
  1356. if (element == "")
  1357. return false;
  1358. std::vector<std::string> parts = split(element,'[');
  1359. if (parts.size() < 2)
  1360. return false;
  1361. std::string type = trim(parts[0]);
  1362. std::string description = trim(parts[1]);
  1363. if (type != "size" && type != "invsize")
  1364. return false;
  1365. if (type == "invsize")
  1366. log_deprecated("Deprecated formspec element \"invsize\" is used");
  1367. parseSize(data, description);
  1368. return true;
  1369. }
  1370. bool GUIFormSpecMenu::parsePositionDirect(parserData *data, const std::string &element)
  1371. {
  1372. if (element.empty())
  1373. return false;
  1374. std::vector<std::string> parts = split(element, '[');
  1375. if (parts.size() != 2)
  1376. return false;
  1377. std::string type = trim(parts[0]);
  1378. std::string description = trim(parts[1]);
  1379. if (type != "position")
  1380. return false;
  1381. parsePosition(data, description);
  1382. return true;
  1383. }
  1384. void GUIFormSpecMenu::parsePosition(parserData *data, const std::string &element)
  1385. {
  1386. std::vector<std::string> parts = split(element, ',');
  1387. if (parts.size() == 2) {
  1388. data->offset.X = stof(parts[0]);
  1389. data->offset.Y = stof(parts[1]);
  1390. return;
  1391. }
  1392. errorstream << "Invalid position element (" << parts.size() << "): '" << element << "'" << std::endl;
  1393. }
  1394. bool GUIFormSpecMenu::parseAnchorDirect(parserData *data, const std::string &element)
  1395. {
  1396. if (element.empty())
  1397. return false;
  1398. std::vector<std::string> parts = split(element, '[');
  1399. if (parts.size() != 2)
  1400. return false;
  1401. std::string type = trim(parts[0]);
  1402. std::string description = trim(parts[1]);
  1403. if (type != "anchor")
  1404. return false;
  1405. parseAnchor(data, description);
  1406. return true;
  1407. }
  1408. void GUIFormSpecMenu::parseAnchor(parserData *data, const std::string &element)
  1409. {
  1410. std::vector<std::string> parts = split(element, ',');
  1411. if (parts.size() == 2) {
  1412. data->anchor.X = stof(parts[0]);
  1413. data->anchor.Y = stof(parts[1]);
  1414. return;
  1415. }
  1416. errorstream << "Invalid anchor element (" << parts.size() << "): '" << element
  1417. << "'" << std::endl;
  1418. }
  1419. void GUIFormSpecMenu::parseElement(parserData* data, const std::string &element)
  1420. {
  1421. //some prechecks
  1422. if (element == "")
  1423. return;
  1424. std::vector<std::string> parts = split(element,'[');
  1425. // ugly workaround to keep compatibility
  1426. if (parts.size() > 2) {
  1427. if (trim(parts[0]) == "image") {
  1428. for (unsigned int i=2;i< parts.size(); i++) {
  1429. parts[1] += "[" + parts[i];
  1430. }
  1431. }
  1432. else { return; }
  1433. }
  1434. if (parts.size() < 2) {
  1435. return;
  1436. }
  1437. std::string type = trim(parts[0]);
  1438. std::string description = trim(parts[1]);
  1439. if (type == "container") {
  1440. parseContainer(data, description);
  1441. return;
  1442. }
  1443. if (type == "container_end") {
  1444. parseContainerEnd(data);
  1445. return;
  1446. }
  1447. if (type == "list") {
  1448. parseList(data, description);
  1449. return;
  1450. }
  1451. if (type == "listring") {
  1452. parseListRing(data, description);
  1453. return;
  1454. }
  1455. if (type == "checkbox") {
  1456. parseCheckbox(data, description);
  1457. return;
  1458. }
  1459. if (type == "image") {
  1460. parseImage(data, description);
  1461. return;
  1462. }
  1463. if (type == "item_image") {
  1464. parseItemImage(data, description);
  1465. return;
  1466. }
  1467. if (type == "button" || type == "button_exit") {
  1468. parseButton(data, description, type);
  1469. return;
  1470. }
  1471. if (type == "background") {
  1472. parseBackground(data,description);
  1473. return;
  1474. }
  1475. if (type == "tableoptions"){
  1476. parseTableOptions(data,description);
  1477. return;
  1478. }
  1479. if (type == "tablecolumns"){
  1480. parseTableColumns(data,description);
  1481. return;
  1482. }
  1483. if (type == "table"){
  1484. parseTable(data,description);
  1485. return;
  1486. }
  1487. if (type == "textlist"){
  1488. parseTextList(data,description);
  1489. return;
  1490. }
  1491. if (type == "dropdown"){
  1492. parseDropDown(data,description);
  1493. return;
  1494. }
  1495. if (type == "field_close_on_enter") {
  1496. parseFieldCloseOnEnter(data, description);
  1497. return;
  1498. }
  1499. if (type == "pwdfield") {
  1500. parsePwdField(data,description);
  1501. return;
  1502. }
  1503. if ((type == "field") || (type == "textarea")){
  1504. parseField(data,description,type);
  1505. return;
  1506. }
  1507. if (type == "label") {
  1508. parseLabel(data,description);
  1509. return;
  1510. }
  1511. if (type == "vertlabel") {
  1512. parseVertLabel(data,description);
  1513. return;
  1514. }
  1515. if (type == "item_image_button") {
  1516. parseItemImageButton(data,description);
  1517. return;
  1518. }
  1519. if ((type == "image_button") || (type == "image_button_exit")) {
  1520. parseImageButton(data,description,type);
  1521. return;
  1522. }
  1523. if (type == "tabheader") {
  1524. parseTabHeader(data,description);
  1525. return;
  1526. }
  1527. if (type == "box") {
  1528. parseBox(data,description);
  1529. return;
  1530. }
  1531. if (type == "bgcolor") {
  1532. parseBackgroundColor(data,description);
  1533. return;
  1534. }
  1535. if (type == "listcolors") {
  1536. parseListColors(data,description);
  1537. return;
  1538. }
  1539. if (type == "tooltip") {
  1540. parseTooltip(data,description);
  1541. return;
  1542. }
  1543. if (type == "scrollbar") {
  1544. parseScrollBar(data, description);
  1545. return;
  1546. }
  1547. // Ignore others
  1548. infostream
  1549. << "Unknown DrawSpec: type="<<type<<", data=\""<<description<<"\""
  1550. <<std::endl;
  1551. }
  1552. void GUIFormSpecMenu::regenerateGui(v2u32 screensize)
  1553. {
  1554. /* useless to regenerate without a screensize */
  1555. if ((screensize.X <= 0) || (screensize.Y <= 0)) {
  1556. return;
  1557. }
  1558. parserData mydata;
  1559. //preserve tables
  1560. for (u32 i = 0; i < m_tables.size(); ++i) {
  1561. std::string tablename = m_tables[i].first.fname;
  1562. GUITable *table = m_tables[i].second;
  1563. mydata.table_dyndata[tablename] = table->getDynamicData();
  1564. }
  1565. //set focus
  1566. if (!m_focused_element.empty())
  1567. mydata.focused_fieldname = m_focused_element;
  1568. //preserve focus
  1569. gui::IGUIElement *focused_element = Environment->getFocus();
  1570. if (focused_element && focused_element->getParent() == this) {
  1571. s32 focused_id = focused_element->getID();
  1572. if (focused_id > 257) {
  1573. for (u32 i=0; i<m_fields.size(); i++) {
  1574. if (m_fields[i].fid == focused_id) {
  1575. mydata.focused_fieldname =
  1576. m_fields[i].fname;
  1577. break;
  1578. }
  1579. }
  1580. }
  1581. }
  1582. // Remove children
  1583. removeChildren();
  1584. for (u32 i = 0; i < m_tables.size(); ++i) {
  1585. GUITable *table = m_tables[i].second;
  1586. table->drop();
  1587. }
  1588. mydata.size= v2s32(100,100);
  1589. mydata.screensize = screensize;
  1590. mydata.offset = v2f32(0.5f, 0.5f);
  1591. mydata.anchor = v2f32(0.5f, 0.5f);
  1592. // Base position of contents of form
  1593. mydata.basepos = getBasePos();
  1594. /* Convert m_init_draw_spec to m_inventorylists */
  1595. m_inventorylists.clear();
  1596. m_images.clear();
  1597. m_backgrounds.clear();
  1598. m_itemimages.clear();
  1599. m_tables.clear();
  1600. m_checkboxes.clear();
  1601. m_scrollbars.clear();
  1602. m_fields.clear();
  1603. m_boxes.clear();
  1604. m_tooltips.clear();
  1605. m_inventory_rings.clear();
  1606. m_static_texts.clear();
  1607. m_dropdowns.clear();
  1608. // Set default values (fits old formspec values)
  1609. m_bgcolor = video::SColor(140,0,0,0);
  1610. m_bgfullscreen = false;
  1611. m_slotbg_n = video::SColor(255,128,128,128);
  1612. m_slotbg_h = video::SColor(255,192,192,192);
  1613. m_default_tooltip_bgcolor = video::SColor(255,110,130,60);
  1614. m_default_tooltip_color = video::SColor(255,255,255,255);
  1615. m_slotbordercolor = video::SColor(200,0,0,0);
  1616. m_slotborder = false;
  1617. // Add tooltip
  1618. {
  1619. assert(!m_tooltip_element);
  1620. // Note: parent != this so that the tooltip isn't clipped by the menu rectangle
  1621. m_tooltip_element = addStaticText(Environment, L"",core::rect<s32>(0,0,110,18));
  1622. m_tooltip_element->enableOverrideColor(true);
  1623. m_tooltip_element->setBackgroundColor(m_default_tooltip_bgcolor);
  1624. m_tooltip_element->setDrawBackground(true);
  1625. m_tooltip_element->setDrawBorder(true);
  1626. m_tooltip_element->setOverrideColor(m_default_tooltip_color);
  1627. m_tooltip_element->setTextAlignment(gui::EGUIA_CENTER, gui::EGUIA_CENTER);
  1628. m_tooltip_element->setWordWrap(false);
  1629. //we're not parent so no autograb for this one!
  1630. m_tooltip_element->grab();
  1631. }
  1632. std::vector<std::string> elements = split(m_formspec_string,']');
  1633. unsigned int i = 0;
  1634. /* try to read version from first element only */
  1635. if (elements.size() >= 1) {
  1636. if ( parseVersionDirect(elements[0]) ) {
  1637. i++;
  1638. }
  1639. }
  1640. /* we need size first in order to calculate image scale */
  1641. mydata.explicit_size = false;
  1642. for (; i< elements.size(); i++) {
  1643. if (!parseSizeDirect(&mydata, elements[i])) {
  1644. break;
  1645. }
  1646. }
  1647. /* "position" element is always after "size" element if it used */
  1648. for (; i< elements.size(); i++) {
  1649. if (!parsePositionDirect(&mydata, elements[i])) {
  1650. break;
  1651. }
  1652. }
  1653. /* "anchor" element is always after "position" (or "size" element) if it used */
  1654. for (; i< elements.size(); i++) {
  1655. if (!parseAnchorDirect(&mydata, elements[i])) {
  1656. break;
  1657. }
  1658. }
  1659. if (mydata.explicit_size) {
  1660. // compute scaling for specified form size
  1661. if (m_lock) {
  1662. v2u32 current_screensize = RenderingEngine::get_video_driver()->getScreenSize();
  1663. v2u32 delta = current_screensize - m_lockscreensize;
  1664. if (current_screensize.Y > m_lockscreensize.Y)
  1665. delta.Y /= 2;
  1666. else
  1667. delta.Y = 0;
  1668. if (current_screensize.X > m_lockscreensize.X)
  1669. delta.X /= 2;
  1670. else
  1671. delta.X = 0;
  1672. offset = v2s32(delta.X,delta.Y);
  1673. mydata.screensize = m_lockscreensize;
  1674. } else {
  1675. offset = v2s32(0,0);
  1676. }
  1677. double gui_scaling = g_settings->getFloat("gui_scaling");
  1678. double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
  1679. double use_imgsize;
  1680. if (m_lock) {
  1681. // In fixed-size mode, inventory image size
  1682. // is 0.53 inch multiplied by the gui_scaling
  1683. // config parameter. This magic size is chosen
  1684. // to make the main menu (15.5 inventory images
  1685. // wide, including border) just fit into the
  1686. // default window (800 pixels wide) at 96 DPI
  1687. // and default scaling (1.00).
  1688. use_imgsize = 0.5555 * screen_dpi * gui_scaling;
  1689. } else {
  1690. // In variable-size mode, we prefer to make the
  1691. // inventory image size 1/15 of screen height,
  1692. // multiplied by the gui_scaling config parameter.
  1693. // If the preferred size won't fit the whole
  1694. // form on the screen, either horizontally or
  1695. // vertically, then we scale it down to fit.
  1696. // (The magic numbers in the computation of what
  1697. // fits arise from the scaling factors in the
  1698. // following stanza, including the form border,
  1699. // help text space, and 0.1 inventory slot spare.)
  1700. // However, a minimum size is also set, that
  1701. // the image size can't be less than 0.3 inch
  1702. // multiplied by gui_scaling, even if this means
  1703. // the form doesn't fit the screen.
  1704. double prefer_imgsize = mydata.screensize.Y / 15 *
  1705. gui_scaling;
  1706. double fitx_imgsize = mydata.screensize.X /
  1707. ((5.0/4.0) * (0.5 + mydata.invsize.X));
  1708. double fity_imgsize = mydata.screensize.Y /
  1709. ((15.0/13.0) * (0.85 * mydata.invsize.Y));
  1710. double screen_dpi = RenderingEngine::getDisplayDensity() * 96;
  1711. double min_imgsize = 0.3 * screen_dpi * gui_scaling;
  1712. use_imgsize = MYMAX(min_imgsize, MYMIN(prefer_imgsize,
  1713. MYMIN(fitx_imgsize, fity_imgsize)));
  1714. }
  1715. // Everything else is scaled in proportion to the
  1716. // inventory image size. The inventory slot spacing
  1717. // is 5/4 image size horizontally and 15/13 image size
  1718. // vertically. The padding around the form (incorporating
  1719. // the border of the outer inventory slots) is 3/8
  1720. // image size. Font height (baseline to baseline)
  1721. // is 2/5 vertical inventory slot spacing, and button
  1722. // half-height is 7/8 of font height.
  1723. imgsize = v2s32(use_imgsize, use_imgsize);
  1724. spacing = v2s32(use_imgsize*5.0/4, use_imgsize*15.0/13);
  1725. padding = v2s32(use_imgsize*3.0/8, use_imgsize*3.0/8);
  1726. m_btn_height = use_imgsize*15.0/13 * 0.35;
  1727. m_font = g_fontengine->getFont();
  1728. mydata.size = v2s32(
  1729. padding.X*2+spacing.X*(mydata.invsize.X-1.0)+imgsize.X,
  1730. padding.Y*2+spacing.Y*(mydata.invsize.Y-1.0)+imgsize.Y + m_btn_height*2.0/3.0
  1731. );
  1732. DesiredRect = mydata.rect = core::rect<s32>(
  1733. (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * (f32)mydata.size.X) + offset.X,
  1734. (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * (f32)mydata.size.Y) + offset.Y,
  1735. (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * (f32)mydata.size.X) + offset.X,
  1736. (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * (f32)mydata.size.Y) + offset.Y
  1737. );
  1738. } else {
  1739. // Non-size[] form must consist only of text fields and
  1740. // implicit "Proceed" button. Use default font, and
  1741. // temporary form size which will be recalculated below.
  1742. m_font = g_fontengine->getFont();
  1743. m_btn_height = font_line_height(m_font) * 0.875;
  1744. DesiredRect = core::rect<s32>(
  1745. (s32)((f32)mydata.screensize.X * mydata.offset.X) - (s32)(mydata.anchor.X * 580.0),
  1746. (s32)((f32)mydata.screensize.Y * mydata.offset.Y) - (s32)(mydata.anchor.Y * 300.0),
  1747. (s32)((f32)mydata.screensize.X * mydata.offset.X) + (s32)((1.0 - mydata.anchor.X) * 580.0),
  1748. (s32)((f32)mydata.screensize.Y * mydata.offset.Y) + (s32)((1.0 - mydata.anchor.Y) * 300.0)
  1749. );
  1750. }
  1751. recalculateAbsolutePosition(false);
  1752. mydata.basepos = getBasePos();
  1753. m_tooltip_element->setOverrideFont(m_font);
  1754. gui::IGUISkin* skin = Environment->getSkin();
  1755. sanity_check(skin);
  1756. gui::IGUIFont *old_font = skin->getFont();
  1757. skin->setFont(m_font);
  1758. pos_offset = v2s32();
  1759. for (; i< elements.size(); i++) {
  1760. parseElement(&mydata, elements[i]);
  1761. }
  1762. if (!container_stack.empty()) {
  1763. errorstream << "Invalid formspec string: container was never closed!"
  1764. << std::endl;
  1765. }
  1766. // If there are fields without explicit size[], add a "Proceed"
  1767. // button and adjust size to fit all the fields.
  1768. if (m_fields.size() && !mydata.explicit_size) {
  1769. mydata.rect = core::rect<s32>(
  1770. mydata.screensize.X/2 - 580/2,
  1771. mydata.screensize.Y/2 - 300/2,
  1772. mydata.screensize.X/2 + 580/2,
  1773. mydata.screensize.Y/2 + 240/2+(m_fields.size()*60)
  1774. );
  1775. DesiredRect = mydata.rect;
  1776. recalculateAbsolutePosition(false);
  1777. mydata.basepos = getBasePos();
  1778. {
  1779. v2s32 pos = mydata.basepos;
  1780. pos.Y = ((m_fields.size()+2)*60);
  1781. v2s32 size = DesiredRect.getSize();
  1782. mydata.rect =
  1783. core::rect<s32>(size.X/2-70, pos.Y,
  1784. (size.X/2-70)+140, pos.Y + (m_btn_height*2));
  1785. const wchar_t *text = wgettext("Proceed");
  1786. Environment->addButton(mydata.rect, this, 257, text);
  1787. delete[] text;
  1788. }
  1789. }
  1790. //set initial focus if parser didn't set it
  1791. focused_element = Environment->getFocus();
  1792. if (!focused_element
  1793. || !isMyChild(focused_element)
  1794. || focused_element->getType() == gui::EGUIET_TAB_CONTROL)
  1795. setInitialFocus();
  1796. skin->setFont(old_font);
  1797. }
  1798. #ifdef __ANDROID__
  1799. bool GUIFormSpecMenu::getAndroidUIInput()
  1800. {
  1801. /* no dialog shown */
  1802. if (m_JavaDialogFieldName == "") {
  1803. return false;
  1804. }
  1805. /* still waiting */
  1806. if (porting::getInputDialogState() == -1) {
  1807. return true;
  1808. }
  1809. std::string fieldname = m_JavaDialogFieldName;
  1810. m_JavaDialogFieldName = "";
  1811. /* no value abort dialog processing */
  1812. if (porting::getInputDialogState() != 0) {
  1813. return false;
  1814. }
  1815. for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
  1816. iter != m_fields.end(); ++iter) {
  1817. if (iter->fname != fieldname) {
  1818. continue;
  1819. }
  1820. IGUIElement* tochange = getElementFromId(iter->fid);
  1821. if (tochange == 0) {
  1822. return false;
  1823. }
  1824. if (tochange->getType() != irr::gui::EGUIET_EDIT_BOX) {
  1825. return false;
  1826. }
  1827. std::string text = porting::getInputDialogValue();
  1828. ((gui::IGUIEditBox*) tochange)->
  1829. setText(utf8_to_wide(text).c_str());
  1830. }
  1831. return false;
  1832. }
  1833. #endif
  1834. GUIFormSpecMenu::ItemSpec GUIFormSpecMenu::getItemAtPos(v2s32 p) const
  1835. {
  1836. core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
  1837. for(u32 i=0; i<m_inventorylists.size(); i++)
  1838. {
  1839. const ListDrawSpec &s = m_inventorylists[i];
  1840. for(s32 i=0; i<s.geom.X*s.geom.Y; i++) {
  1841. s32 item_i = i + s.start_item_i;
  1842. s32 x = (i%s.geom.X) * spacing.X;
  1843. s32 y = (i/s.geom.X) * spacing.Y;
  1844. v2s32 p0(x,y);
  1845. core::rect<s32> rect = imgrect + s.pos + p0;
  1846. if(rect.isPointInside(p))
  1847. {
  1848. return ItemSpec(s.inventoryloc, s.listname, item_i);
  1849. }
  1850. }
  1851. }
  1852. return ItemSpec(InventoryLocation(), "", -1);
  1853. }
  1854. void GUIFormSpecMenu::drawList(const ListDrawSpec &s, int phase,
  1855. bool &item_hovered)
  1856. {
  1857. video::IVideoDriver* driver = Environment->getVideoDriver();
  1858. Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
  1859. if (!inv)
  1860. {
  1861. #ifdef NOTDEF // Prevent message flood
  1862. warningstream<<"GUIFormSpecMenu::drawList(): "
  1863. <<"The inventory location "
  1864. <<"\""<<s.inventoryloc.dump()<<"\" doesn't exist"
  1865. <<std::endl;
  1866. #endif
  1867. return;
  1868. }
  1869. InventoryList *ilist = inv->getList (s.listname);
  1870. if (!ilist)
  1871. {
  1872. #ifdef NOTDEF // Prevent message flood
  1873. warningstream<<"GUIFormSpecMenu::drawList(): "
  1874. <<"The inventory list \""<<s.listname<<"\" @ \""
  1875. <<s.inventoryloc.dump()<<"\" doesn't exist"
  1876. <<std::endl;
  1877. #endif
  1878. return;
  1879. }
  1880. core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
  1881. for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
  1882. {
  1883. s32 item_i = i + s.start_item_i;
  1884. if(item_i >= (s32) ilist->getSize())
  1885. break;
  1886. s32 x = (i%s.geom.X) * spacing.X;
  1887. s32 y = (i/s.geom.X) * spacing.Y;
  1888. v2s32 p(x,y);
  1889. core::rect<s32> rect = imgrect + s.pos + p;
  1890. ItemStack item;
  1891. if(ilist)
  1892. item = ilist->getItem(item_i);
  1893. bool selected = m_selected_item
  1894. && m_invmgr->getInventory(m_selected_item->inventoryloc) == inv
  1895. && m_selected_item->listname == s.listname
  1896. && m_selected_item->i == item_i;
  1897. bool hovering = rect.isPointInside(m_pointer);
  1898. ItemRotationKind rotation_kind = selected ? IT_ROT_SELECTED :
  1899. (hovering ? IT_ROT_HOVERED : IT_ROT_NONE);
  1900. if (phase == 0) {
  1901. if (hovering) {
  1902. item_hovered = true;
  1903. driver->draw2DRectangle(m_slotbg_h, rect, &AbsoluteClippingRect);
  1904. } else {
  1905. driver->draw2DRectangle(m_slotbg_n, rect, &AbsoluteClippingRect);
  1906. }
  1907. }
  1908. //Draw inv slot borders
  1909. if (m_slotborder) {
  1910. s32 x1 = rect.UpperLeftCorner.X;
  1911. s32 y1 = rect.UpperLeftCorner.Y;
  1912. s32 x2 = rect.LowerRightCorner.X;
  1913. s32 y2 = rect.LowerRightCorner.Y;
  1914. s32 border = 1;
  1915. driver->draw2DRectangle(m_slotbordercolor,
  1916. core::rect<s32>(v2s32(x1 - border, y1 - border),
  1917. v2s32(x2 + border, y1)), NULL);
  1918. driver->draw2DRectangle(m_slotbordercolor,
  1919. core::rect<s32>(v2s32(x1 - border, y2),
  1920. v2s32(x2 + border, y2 + border)), NULL);
  1921. driver->draw2DRectangle(m_slotbordercolor,
  1922. core::rect<s32>(v2s32(x1 - border, y1),
  1923. v2s32(x1, y2)), NULL);
  1924. driver->draw2DRectangle(m_slotbordercolor,
  1925. core::rect<s32>(v2s32(x2, y1),
  1926. v2s32(x2 + border, y2)), NULL);
  1927. }
  1928. if(phase == 1)
  1929. {
  1930. // Draw item stack
  1931. if(selected)
  1932. {
  1933. item.takeItem(m_selected_amount);
  1934. }
  1935. if(!item.empty())
  1936. {
  1937. drawItemStack(driver, m_font, item,
  1938. rect, &AbsoluteClippingRect, m_client,
  1939. rotation_kind);
  1940. }
  1941. // Draw tooltip
  1942. std::wstring tooltip_text = L"";
  1943. if (hovering && !m_selected_item) {
  1944. const std::string &desc = item.metadata.getString("description");
  1945. if (desc.empty())
  1946. tooltip_text =
  1947. utf8_to_wide(item.getDefinition(m_client->idef()).description);
  1948. else
  1949. tooltip_text = utf8_to_wide(desc);
  1950. if (!item.name.empty()) {
  1951. if (tooltip_text.empty())
  1952. tooltip_text = utf8_to_wide(item.name);
  1953. if (m_tooltip_append_itemname)
  1954. tooltip_text += utf8_to_wide(" [" + item.name + "]");
  1955. }
  1956. }
  1957. if (!tooltip_text.empty()) {
  1958. showTooltip(tooltip_text, m_default_tooltip_color,
  1959. m_default_tooltip_bgcolor);
  1960. }
  1961. }
  1962. }
  1963. }
  1964. void GUIFormSpecMenu::drawSelectedItem()
  1965. {
  1966. video::IVideoDriver* driver = Environment->getVideoDriver();
  1967. if (!m_selected_item) {
  1968. drawItemStack(driver, m_font, ItemStack(),
  1969. core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
  1970. NULL, m_client, IT_ROT_DRAGGED);
  1971. return;
  1972. }
  1973. Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
  1974. sanity_check(inv);
  1975. InventoryList *list = inv->getList(m_selected_item->listname);
  1976. sanity_check(list);
  1977. ItemStack stack = list->getItem(m_selected_item->i);
  1978. stack.count = m_selected_amount;
  1979. core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
  1980. core::rect<s32> rect = imgrect + (m_pointer - imgrect.getCenter());
  1981. rect.constrainTo(driver->getViewPort());
  1982. drawItemStack(driver, m_font, stack, rect, NULL, m_client, IT_ROT_DRAGGED);
  1983. }
  1984. void GUIFormSpecMenu::drawMenu()
  1985. {
  1986. if(m_form_src){
  1987. std::string newform = m_form_src->getForm();
  1988. if(newform != m_formspec_string){
  1989. m_formspec_string = newform;
  1990. regenerateGui(m_screensize_old);
  1991. }
  1992. }
  1993. gui::IGUISkin* skin = Environment->getSkin();
  1994. sanity_check(skin != NULL);
  1995. gui::IGUIFont *old_font = skin->getFont();
  1996. skin->setFont(m_font);
  1997. updateSelectedItem();
  1998. video::IVideoDriver* driver = Environment->getVideoDriver();
  1999. v2u32 screenSize = driver->getScreenSize();
  2000. core::rect<s32> allbg(0, 0, screenSize.X , screenSize.Y);
  2001. if (m_bgfullscreen)
  2002. driver->draw2DRectangle(m_bgcolor, allbg, &allbg);
  2003. else
  2004. driver->draw2DRectangle(m_bgcolor, AbsoluteRect, &AbsoluteClippingRect);
  2005. m_tooltip_element->setVisible(false);
  2006. /*
  2007. Draw backgrounds
  2008. */
  2009. for(u32 i=0; i<m_backgrounds.size(); i++)
  2010. {
  2011. const ImageDrawSpec &spec = m_backgrounds[i];
  2012. video::ITexture *texture = m_tsrc->getTexture(spec.name);
  2013. if (texture != 0) {
  2014. // Image size on screen
  2015. core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
  2016. // Image rectangle on screen
  2017. core::rect<s32> rect = imgrect + spec.pos;
  2018. if (spec.clip) {
  2019. core::dimension2d<s32> absrec_size = AbsoluteRect.getSize();
  2020. rect = core::rect<s32>(AbsoluteRect.UpperLeftCorner.X - spec.pos.X,
  2021. AbsoluteRect.UpperLeftCorner.Y - spec.pos.Y,
  2022. AbsoluteRect.UpperLeftCorner.X + absrec_size.Width + spec.pos.X,
  2023. AbsoluteRect.UpperLeftCorner.Y + absrec_size.Height + spec.pos.Y);
  2024. }
  2025. const video::SColor color(255,255,255,255);
  2026. const video::SColor colors[] = {color,color,color,color};
  2027. draw2DImageFilterScaled(driver, texture, rect,
  2028. core::rect<s32>(core::position2d<s32>(0,0),
  2029. core::dimension2di(texture->getOriginalSize())),
  2030. 0, (video::SColor *const) colors, true);
  2031. } else {
  2032. errorstream << "GUIFormSpecMenu::drawMenu() Draw backgrounds unable to load texture:" << std::endl;
  2033. errorstream << "\t" << spec.name << std::endl;
  2034. }
  2035. }
  2036. /*
  2037. Draw Boxes
  2038. */
  2039. for(u32 i=0; i<m_boxes.size(); i++)
  2040. {
  2041. const BoxDrawSpec &spec = m_boxes[i];
  2042. irr::video::SColor todraw = spec.color;
  2043. core::rect<s32> rect(spec.pos.X,spec.pos.Y,
  2044. spec.pos.X + spec.geom.X,spec.pos.Y + spec.geom.Y);
  2045. driver->draw2DRectangle(todraw, rect, 0);
  2046. }
  2047. /*
  2048. Call base class
  2049. */
  2050. gui::IGUIElement::draw();
  2051. /*
  2052. Draw images
  2053. */
  2054. for(u32 i=0; i<m_images.size(); i++)
  2055. {
  2056. const ImageDrawSpec &spec = m_images[i];
  2057. video::ITexture *texture = m_tsrc->getTexture(spec.name);
  2058. if (texture != 0) {
  2059. const core::dimension2d<u32>& img_origsize = texture->getOriginalSize();
  2060. // Image size on screen
  2061. core::rect<s32> imgrect;
  2062. if (spec.scale)
  2063. imgrect = core::rect<s32>(0,0,spec.geom.X, spec.geom.Y);
  2064. else {
  2065. imgrect = core::rect<s32>(0,0,img_origsize.Width,img_origsize.Height);
  2066. }
  2067. // Image rectangle on screen
  2068. core::rect<s32> rect = imgrect + spec.pos;
  2069. const video::SColor color(255,255,255,255);
  2070. const video::SColor colors[] = {color,color,color,color};
  2071. draw2DImageFilterScaled(driver, texture, rect,
  2072. core::rect<s32>(core::position2d<s32>(0,0),img_origsize),
  2073. 0, (video::SColor *const) colors, true);
  2074. }
  2075. else {
  2076. errorstream << "GUIFormSpecMenu::drawMenu() Draw images unable to load texture:" << std::endl;
  2077. errorstream << "\t" << spec.name << std::endl;
  2078. }
  2079. }
  2080. /*
  2081. Draw item images
  2082. */
  2083. for(u32 i=0; i<m_itemimages.size(); i++)
  2084. {
  2085. if (m_client == 0)
  2086. break;
  2087. const ImageDrawSpec &spec = m_itemimages[i];
  2088. IItemDefManager *idef = m_client->idef();
  2089. ItemStack item;
  2090. item.deSerialize(spec.item_name, idef);
  2091. core::rect<s32> imgrect(0, 0, spec.geom.X, spec.geom.Y);
  2092. // Viewport rectangle on screen
  2093. core::rect<s32> rect = imgrect + spec.pos;
  2094. if (spec.parent_button && spec.parent_button->isPressed()) {
  2095. #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
  2096. rect += core::dimension2d<s32>(
  2097. 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
  2098. #else
  2099. rect += core::dimension2d<s32>(
  2100. skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
  2101. skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
  2102. #endif
  2103. }
  2104. drawItemStack(driver, m_font, item, rect, &AbsoluteClippingRect,
  2105. m_client, IT_ROT_NONE);
  2106. }
  2107. /*
  2108. Draw items
  2109. Phase 0: Item slot rectangles
  2110. Phase 1: Item images; prepare tooltip
  2111. */
  2112. bool item_hovered = false;
  2113. int start_phase = 0;
  2114. for (int phase = start_phase; phase <= 1; phase++) {
  2115. for (u32 i = 0; i < m_inventorylists.size(); i++) {
  2116. drawList(m_inventorylists[i], phase, item_hovered);
  2117. }
  2118. }
  2119. if (!item_hovered) {
  2120. drawItemStack(driver, m_font, ItemStack(),
  2121. core::rect<s32>(v2s32(0, 0), v2s32(0, 0)),
  2122. NULL, m_client, IT_ROT_HOVERED);
  2123. }
  2124. /* TODO find way to show tooltips on touchscreen */
  2125. #ifndef HAVE_TOUCHSCREENGUI
  2126. m_pointer = RenderingEngine::get_raw_device()->getCursorControl()->getPosition();
  2127. #endif
  2128. /*
  2129. Draw static text elements
  2130. */
  2131. for (u32 i = 0; i < m_static_texts.size(); i++) {
  2132. const StaticTextSpec &spec = m_static_texts[i];
  2133. core::rect<s32> rect = spec.rect;
  2134. if (spec.parent_button && spec.parent_button->isPressed()) {
  2135. #if (IRRLICHT_VERSION_MAJOR == 1 && IRRLICHT_VERSION_MINOR < 8)
  2136. rect += core::dimension2d<s32>(
  2137. 0.05 * (float)rect.getWidth(), 0.05 * (float)rect.getHeight());
  2138. #else
  2139. // Use image offset instead of text's because its a bit smaller
  2140. // and fits better, also TEXT_OFFSET_X is always 0
  2141. rect += core::dimension2d<s32>(
  2142. skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_X),
  2143. skin->getSize(irr::gui::EGDS_BUTTON_PRESSED_IMAGE_OFFSET_Y));
  2144. #endif
  2145. }
  2146. video::SColor color(255, 255, 255, 255);
  2147. m_font->draw(spec.text.c_str(), rect, color, true, true, &rect);
  2148. }
  2149. /*
  2150. Draw fields/buttons tooltips
  2151. */
  2152. gui::IGUIElement *hovered =
  2153. Environment->getRootGUIElement()->getElementFromPoint(m_pointer);
  2154. if (hovered != NULL) {
  2155. s32 id = hovered->getID();
  2156. u64 delta = 0;
  2157. if (id == -1) {
  2158. m_old_tooltip_id = id;
  2159. } else {
  2160. if (id == m_old_tooltip_id) {
  2161. delta = porting::getDeltaMs(m_hovered_time, porting::getTimeMs());
  2162. } else {
  2163. m_hovered_time = porting::getTimeMs();
  2164. m_old_tooltip_id = id;
  2165. }
  2166. }
  2167. // Find and update the current tooltip
  2168. if (id != -1 && delta >= m_tooltip_show_delay) {
  2169. for (std::vector<FieldSpec>::iterator iter = m_fields.begin();
  2170. iter != m_fields.end(); ++iter) {
  2171. if (iter->fid != id)
  2172. continue;
  2173. const std::wstring &text = m_tooltips[iter->fname].tooltip;
  2174. if (!text.empty())
  2175. showTooltip(text, m_tooltips[iter->fname].color,
  2176. m_tooltips[iter->fname].bgcolor);
  2177. break;
  2178. }
  2179. }
  2180. }
  2181. m_tooltip_element->draw();
  2182. /*
  2183. Draw dragged item stack
  2184. */
  2185. drawSelectedItem();
  2186. skin->setFont(old_font);
  2187. }
  2188. void GUIFormSpecMenu::showTooltip(const std::wstring &text,
  2189. const irr::video::SColor &color, const irr::video::SColor &bgcolor)
  2190. {
  2191. const std::wstring ntext = translate_string(text);
  2192. m_tooltip_element->setOverrideColor(color);
  2193. m_tooltip_element->setBackgroundColor(bgcolor);
  2194. setStaticText(m_tooltip_element, ntext.c_str());
  2195. // Tooltip size and offset
  2196. s32 tooltip_width = m_tooltip_element->getTextWidth() + m_btn_height;
  2197. #if (IRRLICHT_VERSION_MAJOR <= 1 && IRRLICHT_VERSION_MINOR <= 8 && IRRLICHT_VERSION_REVISION < 2) || USE_FREETYPE == 1
  2198. std::vector<std::wstring> text_rows = str_split(ntext, L'\n');
  2199. s32 tooltip_height = m_tooltip_element->getTextHeight() * text_rows.size() + 5;
  2200. #else
  2201. s32 tooltip_height = m_tooltip_element->getTextHeight() + 5;
  2202. #endif
  2203. v2u32 screenSize = Environment->getVideoDriver()->getScreenSize();
  2204. int tooltip_offset_x = m_btn_height;
  2205. int tooltip_offset_y = m_btn_height;
  2206. #ifdef __ANDROID__
  2207. tooltip_offset_x *= 3;
  2208. tooltip_offset_y = 0;
  2209. if (m_pointer.X > (s32)screenSize.X / 2)
  2210. tooltip_offset_x = -(tooltip_offset_x + tooltip_width);
  2211. #endif
  2212. // Calculate and set the tooltip position
  2213. s32 tooltip_x = m_pointer.X + tooltip_offset_x;
  2214. s32 tooltip_y = m_pointer.Y + tooltip_offset_y;
  2215. if (tooltip_x + tooltip_width > (s32)screenSize.X)
  2216. tooltip_x = (s32)screenSize.X - tooltip_width - m_btn_height;
  2217. if (tooltip_y + tooltip_height > (s32)screenSize.Y)
  2218. tooltip_y = (s32)screenSize.Y - tooltip_height - m_btn_height;
  2219. m_tooltip_element->setRelativePosition(
  2220. core::rect<s32>(
  2221. core::position2d<s32>(tooltip_x, tooltip_y),
  2222. core::dimension2d<s32>(tooltip_width, tooltip_height)
  2223. )
  2224. );
  2225. // Display the tooltip
  2226. m_tooltip_element->setVisible(true);
  2227. bringToFront(m_tooltip_element);
  2228. }
  2229. void GUIFormSpecMenu::updateSelectedItem()
  2230. {
  2231. // If the selected stack has become empty for some reason, deselect it.
  2232. // If the selected stack has become inaccessible, deselect it.
  2233. // If the selected stack has become smaller, adjust m_selected_amount.
  2234. ItemStack selected = verifySelectedItem();
  2235. // WARNING: BLACK MAGIC
  2236. // See if there is a stack suited for our current guess.
  2237. // If such stack does not exist, clear the guess.
  2238. if(m_selected_content_guess.name != "" &&
  2239. selected.name == m_selected_content_guess.name &&
  2240. selected.count == m_selected_content_guess.count){
  2241. // Selected item fits the guess. Skip the black magic.
  2242. }
  2243. else if(m_selected_content_guess.name != ""){
  2244. bool found = false;
  2245. for(u32 i=0; i<m_inventorylists.size() && !found; i++){
  2246. const ListDrawSpec &s = m_inventorylists[i];
  2247. Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
  2248. if(!inv)
  2249. continue;
  2250. InventoryList *list = inv->getList(s.listname);
  2251. if(!list)
  2252. continue;
  2253. for(s32 i=0; i<s.geom.X*s.geom.Y && !found; i++){
  2254. u32 item_i = i + s.start_item_i;
  2255. if(item_i >= list->getSize())
  2256. continue;
  2257. ItemStack stack = list->getItem(item_i);
  2258. if(stack.name == m_selected_content_guess.name &&
  2259. stack.count == m_selected_content_guess.count){
  2260. found = true;
  2261. infostream<<"Client: Changing selected content guess to "
  2262. <<s.inventoryloc.dump()<<" "<<s.listname
  2263. <<" "<<item_i<<std::endl;
  2264. delete m_selected_item;
  2265. m_selected_item = new ItemSpec(s.inventoryloc, s.listname, item_i);
  2266. m_selected_amount = stack.count;
  2267. }
  2268. }
  2269. }
  2270. if(!found){
  2271. infostream<<"Client: Discarding selected content guess: "
  2272. <<m_selected_content_guess.getItemString()<<std::endl;
  2273. m_selected_content_guess.name = "";
  2274. }
  2275. }
  2276. // If craftresult is nonempty and nothing else is selected, select it now.
  2277. if(!m_selected_item)
  2278. {
  2279. for(u32 i=0; i<m_inventorylists.size(); i++)
  2280. {
  2281. const ListDrawSpec &s = m_inventorylists[i];
  2282. if(s.listname == "craftpreview")
  2283. {
  2284. Inventory *inv = m_invmgr->getInventory(s.inventoryloc);
  2285. InventoryList *list = inv->getList("craftresult");
  2286. if(list && list->getSize() >= 1 && !list->getItem(0).empty())
  2287. {
  2288. m_selected_item = new ItemSpec;
  2289. m_selected_item->inventoryloc = s.inventoryloc;
  2290. m_selected_item->listname = "craftresult";
  2291. m_selected_item->i = 0;
  2292. m_selected_amount = 0;
  2293. m_selected_dragging = false;
  2294. break;
  2295. }
  2296. }
  2297. }
  2298. }
  2299. // If craftresult is selected, keep the whole stack selected
  2300. if(m_selected_item && m_selected_item->listname == "craftresult")
  2301. {
  2302. m_selected_amount = verifySelectedItem().count;
  2303. }
  2304. }
  2305. ItemStack GUIFormSpecMenu::verifySelectedItem()
  2306. {
  2307. // If the selected stack has become empty for some reason, deselect it.
  2308. // If the selected stack has become inaccessible, deselect it.
  2309. // If the selected stack has become smaller, adjust m_selected_amount.
  2310. // Return the selected stack.
  2311. if(m_selected_item)
  2312. {
  2313. if(m_selected_item->isValid())
  2314. {
  2315. Inventory *inv = m_invmgr->getInventory(m_selected_item->inventoryloc);
  2316. if(inv)
  2317. {
  2318. InventoryList *list = inv->getList(m_selected_item->listname);
  2319. if(list && (u32) m_selected_item->i < list->getSize())
  2320. {
  2321. ItemStack stack = list->getItem(m_selected_item->i);
  2322. if(m_selected_amount > stack.count)
  2323. m_selected_amount = stack.count;
  2324. if(!stack.empty())
  2325. return stack;
  2326. }
  2327. }
  2328. }
  2329. // selection was not valid
  2330. delete m_selected_item;
  2331. m_selected_item = NULL;
  2332. m_selected_amount = 0;
  2333. m_selected_dragging = false;
  2334. }
  2335. return ItemStack();
  2336. }
  2337. void GUIFormSpecMenu::acceptInput(FormspecQuitMode quitmode=quit_mode_no)
  2338. {
  2339. if(m_text_dst)
  2340. {
  2341. StringMap fields;
  2342. if (quitmode == quit_mode_accept) {
  2343. fields["quit"] = "true";
  2344. }
  2345. if (quitmode == quit_mode_cancel) {
  2346. fields["quit"] = "true";
  2347. m_text_dst->gotText(fields);
  2348. return;
  2349. }
  2350. if (current_keys_pending.key_down) {
  2351. fields["key_down"] = "true";
  2352. current_keys_pending.key_down = false;
  2353. }
  2354. if (current_keys_pending.key_up) {
  2355. fields["key_up"] = "true";
  2356. current_keys_pending.key_up = false;
  2357. }
  2358. if (current_keys_pending.key_enter) {
  2359. fields["key_enter"] = "true";
  2360. current_keys_pending.key_enter = false;
  2361. }
  2362. if (!current_field_enter_pending.empty()) {
  2363. fields["key_enter_field"] = current_field_enter_pending;
  2364. current_field_enter_pending = "";
  2365. }
  2366. if (current_keys_pending.key_escape) {
  2367. fields["key_escape"] = "true";
  2368. current_keys_pending.key_escape = false;
  2369. }
  2370. for(unsigned int i=0; i<m_fields.size(); i++) {
  2371. const FieldSpec &s = m_fields[i];
  2372. if(s.send) {
  2373. std::string name = s.fname;
  2374. if (s.ftype == f_Button) {
  2375. fields[name] = wide_to_utf8(s.flabel);
  2376. } else if (s.ftype == f_Table) {
  2377. GUITable *table = getTable(s.fname);
  2378. if (table) {
  2379. fields[name] = table->checkEvent();
  2380. }
  2381. }
  2382. else if(s.ftype == f_DropDown) {
  2383. // no dynamic cast possible due to some distributions shipped
  2384. // without rtti support in irrlicht
  2385. IGUIElement * element = getElementFromId(s.fid);
  2386. gui::IGUIComboBox *e = NULL;
  2387. if ((element) && (element->getType() == gui::EGUIET_COMBO_BOX)) {
  2388. e = static_cast<gui::IGUIComboBox*>(element);
  2389. }
  2390. s32 selected = e->getSelected();
  2391. if (selected >= 0) {
  2392. std::vector<std::string> *dropdown_values =
  2393. getDropDownValues(s.fname);
  2394. if (dropdown_values && selected < (s32)dropdown_values->size()) {
  2395. fields[name] = (*dropdown_values)[selected];
  2396. }
  2397. }
  2398. }
  2399. else if (s.ftype == f_TabHeader) {
  2400. // no dynamic cast possible due to some distributions shipped
  2401. // without rtti support in irrlicht
  2402. IGUIElement * element = getElementFromId(s.fid);
  2403. gui::IGUITabControl *e = NULL;
  2404. if ((element) && (element->getType() == gui::EGUIET_TAB_CONTROL)) {
  2405. e = static_cast<gui::IGUITabControl*>(element);
  2406. }
  2407. if (e != 0) {
  2408. std::stringstream ss;
  2409. ss << (e->getActiveTab() +1);
  2410. fields[name] = ss.str();
  2411. }
  2412. }
  2413. else if (s.ftype == f_CheckBox) {
  2414. // no dynamic cast possible due to some distributions shipped
  2415. // without rtti support in irrlicht
  2416. IGUIElement * element = getElementFromId(s.fid);
  2417. gui::IGUICheckBox *e = NULL;
  2418. if ((element) && (element->getType() == gui::EGUIET_CHECK_BOX)) {
  2419. e = static_cast<gui::IGUICheckBox*>(element);
  2420. }
  2421. if (e != 0) {
  2422. if (e->isChecked())
  2423. fields[name] = "true";
  2424. else
  2425. fields[name] = "false";
  2426. }
  2427. }
  2428. else if (s.ftype == f_ScrollBar) {
  2429. // no dynamic cast possible due to some distributions shipped
  2430. // without rtti support in irrlicht
  2431. IGUIElement * element = getElementFromId(s.fid);
  2432. gui::IGUIScrollBar *e = NULL;
  2433. if ((element) && (element->getType() == gui::EGUIET_SCROLL_BAR)) {
  2434. e = static_cast<gui::IGUIScrollBar*>(element);
  2435. }
  2436. if (e != 0) {
  2437. std::stringstream os;
  2438. os << e->getPos();
  2439. if (s.fdefault == L"Changed")
  2440. fields[name] = "CHG:" + os.str();
  2441. else
  2442. fields[name] = "VAL:" + os.str();
  2443. }
  2444. }
  2445. else
  2446. {
  2447. IGUIElement* e = getElementFromId(s.fid);
  2448. if(e != NULL) {
  2449. fields[name] = wide_to_utf8(e->getText());
  2450. }
  2451. }
  2452. }
  2453. }
  2454. m_text_dst->gotText(fields);
  2455. }
  2456. }
  2457. static bool isChild(gui::IGUIElement * tocheck, gui::IGUIElement * parent)
  2458. {
  2459. while(tocheck != NULL) {
  2460. if (tocheck == parent) {
  2461. return true;
  2462. }
  2463. tocheck = tocheck->getParent();
  2464. }
  2465. return false;
  2466. }
  2467. bool GUIFormSpecMenu::preprocessEvent(const SEvent& event)
  2468. {
  2469. // The IGUITabControl renders visually using the skin's selected
  2470. // font, which we override for the duration of form drawing,
  2471. // but computes tab hotspots based on how it would have rendered
  2472. // using the font that is selected at the time of button release.
  2473. // To make these two consistent, temporarily override the skin's
  2474. // font while the IGUITabControl is processing the event.
  2475. if (event.EventType == EET_MOUSE_INPUT_EVENT &&
  2476. event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
  2477. s32 x = event.MouseInput.X;
  2478. s32 y = event.MouseInput.Y;
  2479. gui::IGUIElement *hovered =
  2480. Environment->getRootGUIElement()->getElementFromPoint(
  2481. core::position2d<s32>(x, y));
  2482. if (hovered && isMyChild(hovered) &&
  2483. hovered->getType() == gui::EGUIET_TAB_CONTROL) {
  2484. gui::IGUISkin* skin = Environment->getSkin();
  2485. sanity_check(skin != NULL);
  2486. gui::IGUIFont *old_font = skin->getFont();
  2487. skin->setFont(m_font);
  2488. bool retval = hovered->OnEvent(event);
  2489. skin->setFont(old_font);
  2490. return retval;
  2491. }
  2492. }
  2493. // Fix Esc/Return key being eaten by checkboxen and tables
  2494. if(event.EventType==EET_KEY_INPUT_EVENT) {
  2495. KeyPress kp(event.KeyInput);
  2496. if (kp == EscapeKey || kp == CancelKey
  2497. || kp == getKeySetting("keymap_inventory")
  2498. || event.KeyInput.Key==KEY_RETURN) {
  2499. gui::IGUIElement *focused = Environment->getFocus();
  2500. if (focused && isMyChild(focused) &&
  2501. (focused->getType() == gui::EGUIET_LIST_BOX ||
  2502. focused->getType() == gui::EGUIET_CHECK_BOX) &&
  2503. (focused->getParent()->getType() != gui::EGUIET_COMBO_BOX ||
  2504. event.KeyInput.Key != KEY_RETURN)) {
  2505. OnEvent(event);
  2506. return true;
  2507. }
  2508. }
  2509. }
  2510. // Mouse wheel events: send to hovered element instead of focused
  2511. if(event.EventType==EET_MOUSE_INPUT_EVENT
  2512. && event.MouseInput.Event == EMIE_MOUSE_WHEEL) {
  2513. s32 x = event.MouseInput.X;
  2514. s32 y = event.MouseInput.Y;
  2515. gui::IGUIElement *hovered =
  2516. Environment->getRootGUIElement()->getElementFromPoint(
  2517. core::position2d<s32>(x, y));
  2518. if (hovered && isMyChild(hovered)) {
  2519. hovered->OnEvent(event);
  2520. return true;
  2521. }
  2522. }
  2523. if (event.EventType == EET_MOUSE_INPUT_EVENT) {
  2524. s32 x = event.MouseInput.X;
  2525. s32 y = event.MouseInput.Y;
  2526. gui::IGUIElement *hovered =
  2527. Environment->getRootGUIElement()->getElementFromPoint(
  2528. core::position2d<s32>(x, y));
  2529. if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
  2530. m_old_tooltip_id = -1;
  2531. }
  2532. if (!isChild(hovered,this)) {
  2533. if (DoubleClickDetection(event)) {
  2534. return true;
  2535. }
  2536. }
  2537. }
  2538. #ifdef __ANDROID__
  2539. // display software keyboard when clicking edit boxes
  2540. if (event.EventType == EET_MOUSE_INPUT_EVENT
  2541. && event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
  2542. gui::IGUIElement *hovered =
  2543. Environment->getRootGUIElement()->getElementFromPoint(
  2544. core::position2d<s32>(event.MouseInput.X, event.MouseInput.Y));
  2545. if ((hovered) && (hovered->getType() == irr::gui::EGUIET_EDIT_BOX)) {
  2546. bool retval = hovered->OnEvent(event);
  2547. if (retval)
  2548. Environment->setFocus(hovered);
  2549. std::string field_name = getNameByID(hovered->getID());
  2550. /* read-only field */
  2551. if (field_name.empty())
  2552. return retval;
  2553. m_JavaDialogFieldName = field_name;
  2554. std::string message = gettext("Enter ");
  2555. std::string label = wide_to_utf8(getLabelByID(hovered->getID()));
  2556. if (label.empty())
  2557. label = "text";
  2558. message += gettext(label) + ":";
  2559. /* single line text input */
  2560. int type = 2;
  2561. /* multi line text input */
  2562. if (((gui::IGUIEditBox*) hovered)->isMultiLineEnabled())
  2563. type = 1;
  2564. /* passwords are always single line */
  2565. if (((gui::IGUIEditBox*) hovered)->isPasswordBox())
  2566. type = 3;
  2567. porting::showInputDialog(gettext("ok"), "",
  2568. wide_to_utf8(((gui::IGUIEditBox*) hovered)->getText()),
  2569. type);
  2570. return retval;
  2571. }
  2572. }
  2573. if (event.EventType == EET_TOUCH_INPUT_EVENT)
  2574. {
  2575. SEvent translated;
  2576. memset(&translated, 0, sizeof(SEvent));
  2577. translated.EventType = EET_MOUSE_INPUT_EVENT;
  2578. gui::IGUIElement* root = Environment->getRootGUIElement();
  2579. if (!root) {
  2580. errorstream
  2581. << "GUIFormSpecMenu::preprocessEvent unable to get root element"
  2582. << std::endl;
  2583. return false;
  2584. }
  2585. gui::IGUIElement* hovered = root->getElementFromPoint(
  2586. core::position2d<s32>(
  2587. event.TouchInput.X,
  2588. event.TouchInput.Y));
  2589. translated.MouseInput.X = event.TouchInput.X;
  2590. translated.MouseInput.Y = event.TouchInput.Y;
  2591. translated.MouseInput.Control = false;
  2592. bool dont_send_event = false;
  2593. if (event.TouchInput.touchedCount == 1) {
  2594. switch (event.TouchInput.Event) {
  2595. case ETIE_PRESSED_DOWN:
  2596. m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
  2597. translated.MouseInput.Event = EMIE_LMOUSE_PRESSED_DOWN;
  2598. translated.MouseInput.ButtonStates = EMBSM_LEFT;
  2599. m_down_pos = m_pointer;
  2600. break;
  2601. case ETIE_MOVED:
  2602. m_pointer = v2s32(event.TouchInput.X,event.TouchInput.Y);
  2603. translated.MouseInput.Event = EMIE_MOUSE_MOVED;
  2604. translated.MouseInput.ButtonStates = EMBSM_LEFT;
  2605. break;
  2606. case ETIE_LEFT_UP:
  2607. translated.MouseInput.Event = EMIE_LMOUSE_LEFT_UP;
  2608. translated.MouseInput.ButtonStates = 0;
  2609. hovered = root->getElementFromPoint(m_down_pos);
  2610. /* we don't have a valid pointer element use last
  2611. * known pointer pos */
  2612. translated.MouseInput.X = m_pointer.X;
  2613. translated.MouseInput.Y = m_pointer.Y;
  2614. /* reset down pos */
  2615. m_down_pos = v2s32(0,0);
  2616. break;
  2617. default:
  2618. dont_send_event = true;
  2619. //this is not supposed to happen
  2620. errorstream
  2621. << "GUIFormSpecMenu::preprocessEvent unexpected usecase Event="
  2622. << event.TouchInput.Event << std::endl;
  2623. }
  2624. } else if ( (event.TouchInput.touchedCount == 2) &&
  2625. (event.TouchInput.Event == ETIE_PRESSED_DOWN) ) {
  2626. hovered = root->getElementFromPoint(m_down_pos);
  2627. translated.MouseInput.Event = EMIE_RMOUSE_PRESSED_DOWN;
  2628. translated.MouseInput.ButtonStates = EMBSM_LEFT | EMBSM_RIGHT;
  2629. translated.MouseInput.X = m_pointer.X;
  2630. translated.MouseInput.Y = m_pointer.Y;
  2631. if (hovered) {
  2632. hovered->OnEvent(translated);
  2633. }
  2634. translated.MouseInput.Event = EMIE_RMOUSE_LEFT_UP;
  2635. translated.MouseInput.ButtonStates = EMBSM_LEFT;
  2636. if (hovered) {
  2637. hovered->OnEvent(translated);
  2638. }
  2639. dont_send_event = true;
  2640. }
  2641. /* ignore unhandled 2 touch events ... accidental moving for example */
  2642. else if (event.TouchInput.touchedCount == 2) {
  2643. dont_send_event = true;
  2644. }
  2645. else if (event.TouchInput.touchedCount > 2) {
  2646. errorstream
  2647. << "GUIFormSpecMenu::preprocessEvent to many multitouch events "
  2648. << event.TouchInput.touchedCount << " ignoring them" << std::endl;
  2649. }
  2650. if (dont_send_event) {
  2651. return true;
  2652. }
  2653. /* check if translated event needs to be preprocessed again */
  2654. if (preprocessEvent(translated)) {
  2655. return true;
  2656. }
  2657. if (hovered) {
  2658. grab();
  2659. bool retval = hovered->OnEvent(translated);
  2660. if (event.TouchInput.Event == ETIE_LEFT_UP) {
  2661. /* reset pointer */
  2662. m_pointer = v2s32(0,0);
  2663. }
  2664. drop();
  2665. return retval;
  2666. }
  2667. }
  2668. #endif
  2669. if (event.EventType == irr::EET_JOYSTICK_INPUT_EVENT) {
  2670. /* TODO add a check like:
  2671. if (event.JoystickEvent != joystick_we_listen_for)
  2672. return false;
  2673. */
  2674. bool handled = m_joystick->handleEvent(event.JoystickEvent);
  2675. if (handled) {
  2676. if (m_joystick->wasKeyDown(KeyType::ESC)) {
  2677. tryClose();
  2678. } else if (m_joystick->wasKeyDown(KeyType::JUMP)) {
  2679. if (m_allowclose) {
  2680. acceptInput(quit_mode_accept);
  2681. quitMenu();
  2682. }
  2683. }
  2684. }
  2685. return handled;
  2686. }
  2687. return false;
  2688. }
  2689. /******************************************************************************/
  2690. bool GUIFormSpecMenu::DoubleClickDetection(const SEvent event)
  2691. {
  2692. /* The following code is for capturing double-clicks of the mouse button
  2693. * and translating the double-click into an EET_KEY_INPUT_EVENT event
  2694. * -- which closes the form -- under some circumstances.
  2695. *
  2696. * There have been many github issues reporting this as a bug even though it
  2697. * was an intended feature. For this reason, remapping the double-click as
  2698. * an ESC must be explicitly set when creating this class via the
  2699. * /p remap_dbl_click parameter of the constructor.
  2700. */
  2701. if (!m_remap_dbl_click)
  2702. return false;
  2703. if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN) {
  2704. m_doubleclickdetect[0].pos = m_doubleclickdetect[1].pos;
  2705. m_doubleclickdetect[0].time = m_doubleclickdetect[1].time;
  2706. m_doubleclickdetect[1].pos = m_pointer;
  2707. m_doubleclickdetect[1].time = porting::getTimeMs();
  2708. }
  2709. else if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP) {
  2710. u64 delta = porting::getDeltaMs(m_doubleclickdetect[0].time, porting::getTimeMs());
  2711. if (delta > 400) {
  2712. return false;
  2713. }
  2714. double squaredistance =
  2715. m_doubleclickdetect[0].pos
  2716. .getDistanceFromSQ(m_doubleclickdetect[1].pos);
  2717. if (squaredistance > (30*30)) {
  2718. return false;
  2719. }
  2720. SEvent* translated = new SEvent();
  2721. assert(translated != 0);
  2722. //translate doubleclick to escape
  2723. memset(translated, 0, sizeof(SEvent));
  2724. translated->EventType = irr::EET_KEY_INPUT_EVENT;
  2725. translated->KeyInput.Key = KEY_ESCAPE;
  2726. translated->KeyInput.Control = false;
  2727. translated->KeyInput.Shift = false;
  2728. translated->KeyInput.PressedDown = true;
  2729. translated->KeyInput.Char = 0;
  2730. OnEvent(*translated);
  2731. // no need to send the key up event as we're already deleted
  2732. // and no one else did notice this event
  2733. delete translated;
  2734. return true;
  2735. }
  2736. return false;
  2737. }
  2738. void GUIFormSpecMenu::tryClose()
  2739. {
  2740. if (m_allowclose) {
  2741. doPause = false;
  2742. acceptInput(quit_mode_cancel);
  2743. quitMenu();
  2744. } else {
  2745. m_text_dst->gotText(L"MenuQuit");
  2746. }
  2747. }
  2748. bool GUIFormSpecMenu::OnEvent(const SEvent& event)
  2749. {
  2750. if (event.EventType==EET_KEY_INPUT_EVENT) {
  2751. KeyPress kp(event.KeyInput);
  2752. if (event.KeyInput.PressedDown && (
  2753. (kp == EscapeKey) || (kp == CancelKey) ||
  2754. ((m_client != NULL) && (kp == getKeySetting("keymap_inventory"))))) {
  2755. tryClose();
  2756. return true;
  2757. } else if (m_client != NULL && event.KeyInput.PressedDown &&
  2758. (kp == getKeySetting("keymap_screenshot"))) {
  2759. m_client->makeScreenshot();
  2760. }
  2761. if (event.KeyInput.PressedDown &&
  2762. (event.KeyInput.Key==KEY_RETURN ||
  2763. event.KeyInput.Key==KEY_UP ||
  2764. event.KeyInput.Key==KEY_DOWN)
  2765. ) {
  2766. switch (event.KeyInput.Key) {
  2767. case KEY_RETURN:
  2768. current_keys_pending.key_enter = true;
  2769. break;
  2770. case KEY_UP:
  2771. current_keys_pending.key_up = true;
  2772. break;
  2773. case KEY_DOWN:
  2774. current_keys_pending.key_down = true;
  2775. break;
  2776. break;
  2777. default:
  2778. //can't happen at all!
  2779. FATAL_ERROR("Reached a source line that can't ever been reached");
  2780. break;
  2781. }
  2782. if (current_keys_pending.key_enter && m_allowclose) {
  2783. acceptInput(quit_mode_accept);
  2784. quitMenu();
  2785. } else {
  2786. acceptInput();
  2787. }
  2788. return true;
  2789. }
  2790. }
  2791. /* Mouse event other than movement, or crossing the border of inventory
  2792. field while holding right mouse button
  2793. */
  2794. if (event.EventType == EET_MOUSE_INPUT_EVENT &&
  2795. (event.MouseInput.Event != EMIE_MOUSE_MOVED ||
  2796. (event.MouseInput.Event == EMIE_MOUSE_MOVED &&
  2797. event.MouseInput.isRightPressed() &&
  2798. getItemAtPos(m_pointer).i != getItemAtPos(m_old_pointer).i))) {
  2799. // Get selected item and hovered/clicked item (s)
  2800. m_old_tooltip_id = -1;
  2801. updateSelectedItem();
  2802. ItemSpec s = getItemAtPos(m_pointer);
  2803. Inventory *inv_selected = NULL;
  2804. Inventory *inv_s = NULL;
  2805. InventoryList *list_s = NULL;
  2806. if (m_selected_item) {
  2807. inv_selected = m_invmgr->getInventory(m_selected_item->inventoryloc);
  2808. sanity_check(inv_selected);
  2809. sanity_check(inv_selected->getList(m_selected_item->listname) != NULL);
  2810. }
  2811. u32 s_count = 0;
  2812. if (s.isValid())
  2813. do { // breakable
  2814. inv_s = m_invmgr->getInventory(s.inventoryloc);
  2815. if (!inv_s) {
  2816. errorstream << "InventoryMenu: The selected inventory location "
  2817. << "\"" << s.inventoryloc.dump() << "\" doesn't exist"
  2818. << std::endl;
  2819. s.i = -1; // make it invalid again
  2820. break;
  2821. }
  2822. list_s = inv_s->getList(s.listname);
  2823. if (list_s == NULL) {
  2824. verbosestream << "InventoryMenu: The selected inventory list \""
  2825. << s.listname << "\" does not exist" << std::endl;
  2826. s.i = -1; // make it invalid again
  2827. break;
  2828. }
  2829. if ((u32)s.i >= list_s->getSize()) {
  2830. infostream << "InventoryMenu: The selected inventory list \""
  2831. << s.listname << "\" is too small (i=" << s.i << ", size="
  2832. << list_s->getSize() << ")" << std::endl;
  2833. s.i = -1; // make it invalid again
  2834. break;
  2835. }
  2836. s_count = list_s->getItem(s.i).count;
  2837. } while(0);
  2838. bool identical = (m_selected_item != NULL) && s.isValid() &&
  2839. (inv_selected == inv_s) &&
  2840. (m_selected_item->listname == s.listname) &&
  2841. (m_selected_item->i == s.i);
  2842. // buttons: 0 = left, 1 = right, 2 = middle, 3 = wheeldown, 4 = wheelup
  2843. // up/down: 0 = down (press or wheel), 1 = up (release),
  2844. // 2 = unknown event, -1 = movement
  2845. int button = 0;
  2846. int updown = 2;
  2847. switch (event.MouseInput.Event) {
  2848. case EMIE_LMOUSE_PRESSED_DOWN:
  2849. button = 0; updown = 0;
  2850. break;
  2851. case EMIE_RMOUSE_PRESSED_DOWN:
  2852. button = 1; updown = 0;
  2853. break;
  2854. case EMIE_MMOUSE_PRESSED_DOWN:
  2855. button = 2; updown = 0;
  2856. break;
  2857. case EMIE_MOUSE_WHEEL:
  2858. button = 3 + (event.MouseInput.Wheel > 0); updown = 0;
  2859. break;
  2860. case EMIE_LMOUSE_LEFT_UP:
  2861. button = 0; updown = 1;
  2862. break;
  2863. case EMIE_RMOUSE_LEFT_UP:
  2864. button = 1; updown = 1;
  2865. break;
  2866. case EMIE_MMOUSE_LEFT_UP:
  2867. button = 2; updown = 1;
  2868. break;
  2869. case EMIE_MOUSE_MOVED:
  2870. updown = -1;
  2871. break;
  2872. default:
  2873. break;
  2874. }
  2875. // Set this number to a positive value to generate a move action
  2876. // from m_selected_item to s.
  2877. u32 move_amount = 0;
  2878. // Set this number to a positive value to generate a move action
  2879. // from s to the next inventory ring.
  2880. u32 shift_move_amount = 0;
  2881. // Set this number to a positive value to generate a drop action
  2882. // from m_selected_item.
  2883. u32 drop_amount = 0;
  2884. // Set this number to a positive value to generate a craft action at s.
  2885. u32 craft_amount = 0;
  2886. if (!updown) {
  2887. // Some mouse button has been pressed
  2888. //infostream<<"Mouse button "<<button<<" pressed at p=("
  2889. // <<p.X<<","<<p.Y<<")"<<std::endl;
  2890. m_selected_dragging = false;
  2891. if (s.isValid() && s.listname == "craftpreview") {
  2892. // Craft preview has been clicked: craft
  2893. craft_amount = (button == 2 ? 10 : 1);
  2894. } else if (m_selected_item == NULL) {
  2895. if (s_count && button != 4) {
  2896. // Non-empty stack has been clicked: select or shift-move it
  2897. m_selected_item = new ItemSpec(s);
  2898. u32 count;
  2899. if (button == 1) // right
  2900. count = (s_count + 1) / 2;
  2901. else if (button == 2) // middle
  2902. count = MYMIN(s_count, 10);
  2903. else if (button == 3) // wheeldown
  2904. count = 1;
  2905. else // left
  2906. count = s_count;
  2907. if (!event.MouseInput.Shift) {
  2908. // no shift: select item
  2909. m_selected_amount = count;
  2910. m_selected_dragging = button != 3;
  2911. m_auto_place = false;
  2912. } else {
  2913. // shift pressed: move item, right click moves 1
  2914. shift_move_amount = button == 1 ? 1 : count;
  2915. }
  2916. }
  2917. } else { // m_selected_item != NULL
  2918. assert(m_selected_amount >= 1);
  2919. if (s.isValid()) {
  2920. // Clicked a slot: move
  2921. if (button == 1 || button == 4) // right or wheelup
  2922. move_amount = 1;
  2923. else if (button == 2) // middle
  2924. move_amount = MYMIN(m_selected_amount, 10);
  2925. else if (!button) // left
  2926. move_amount = m_selected_amount;
  2927. // else wheeldown
  2928. if (identical) {
  2929. if (button == 3) { // wheeldown
  2930. if (m_selected_amount < s_count)
  2931. ++m_selected_amount;
  2932. } else {
  2933. if (move_amount >= m_selected_amount)
  2934. m_selected_amount = 0;
  2935. else
  2936. m_selected_amount -= move_amount;
  2937. move_amount = 0;
  2938. }
  2939. }
  2940. }
  2941. else if (!getAbsoluteClippingRect().isPointInside(m_pointer)
  2942. && button != 3) {
  2943. // Clicked outside of the window: drop
  2944. if (button == 1 || button == 4) // right or wheelup
  2945. drop_amount = 1;
  2946. else if (button == 2) // middle
  2947. drop_amount = MYMIN(m_selected_amount, 10);
  2948. else // left
  2949. drop_amount = m_selected_amount;
  2950. }
  2951. }
  2952. } else if (updown == 1) {
  2953. // Some mouse button has been released
  2954. //infostream<<"Mouse button "<<button<<" released at p=("
  2955. // <<p.X<<","<<p.Y<<")"<<std::endl;
  2956. if (m_selected_dragging && m_selected_item != NULL) {
  2957. if (s.isValid()) {
  2958. if (!identical) {
  2959. // Dragged to different slot: move all selected
  2960. move_amount = m_selected_amount;
  2961. }
  2962. } else if (!getAbsoluteClippingRect().isPointInside(m_pointer)) {
  2963. // Dragged outside of window: drop all selected
  2964. drop_amount = m_selected_amount;
  2965. }
  2966. }
  2967. m_selected_dragging = false;
  2968. // Keep track of whether the mouse button be released
  2969. // One click is drag without dropping. Click + release
  2970. // + click changes to drop item when moved mode
  2971. if (m_selected_item)
  2972. m_auto_place = true;
  2973. } else if (updown == -1) {
  2974. // Mouse has been moved and rmb is down and mouse pointer just
  2975. // entered a new inventory field (checked in the entry-if, this
  2976. // is the only action here that is generated by mouse movement)
  2977. if (m_selected_item != NULL && s.isValid()) {
  2978. // Move 1 item
  2979. // TODO: middle mouse to move 10 items might be handy
  2980. if (m_auto_place) {
  2981. // Only move an item if the destination slot is empty
  2982. // or contains the same item type as what is going to be
  2983. // moved
  2984. InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
  2985. InventoryList *list_to = list_s;
  2986. assert(list_from && list_to);
  2987. ItemStack stack_from = list_from->getItem(m_selected_item->i);
  2988. ItemStack stack_to = list_to->getItem(s.i);
  2989. if (stack_to.empty() || stack_to.name == stack_from.name)
  2990. move_amount = 1;
  2991. }
  2992. }
  2993. }
  2994. // Possibly send inventory action to server
  2995. if (move_amount > 0) {
  2996. // Send IAction::Move
  2997. assert(m_selected_item && m_selected_item->isValid());
  2998. assert(s.isValid());
  2999. assert(inv_selected && inv_s);
  3000. InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
  3001. InventoryList *list_to = list_s;
  3002. assert(list_from && list_to);
  3003. ItemStack stack_from = list_from->getItem(m_selected_item->i);
  3004. ItemStack stack_to = list_to->getItem(s.i);
  3005. // Check how many items can be moved
  3006. move_amount = stack_from.count = MYMIN(move_amount, stack_from.count);
  3007. ItemStack leftover = stack_to.addItem(stack_from, m_client->idef());
  3008. // If source stack cannot be added to destination stack at all,
  3009. // they are swapped
  3010. if ((leftover.count == stack_from.count) &&
  3011. (leftover.name == stack_from.name)) {
  3012. m_selected_amount = stack_to.count;
  3013. // In case the server doesn't directly swap them but instead
  3014. // moves stack_to somewhere else, set this
  3015. m_selected_content_guess = stack_to;
  3016. m_selected_content_guess_inventory = s.inventoryloc;
  3017. }
  3018. // Source stack goes fully into destination stack
  3019. else if (leftover.empty()) {
  3020. m_selected_amount -= move_amount;
  3021. m_selected_content_guess = ItemStack(); // Clear
  3022. }
  3023. // Source stack goes partly into destination stack
  3024. else {
  3025. move_amount -= leftover.count;
  3026. m_selected_amount -= move_amount;
  3027. m_selected_content_guess = ItemStack(); // Clear
  3028. }
  3029. infostream << "Handing IAction::Move to manager" << std::endl;
  3030. IMoveAction *a = new IMoveAction();
  3031. a->count = move_amount;
  3032. a->from_inv = m_selected_item->inventoryloc;
  3033. a->from_list = m_selected_item->listname;
  3034. a->from_i = m_selected_item->i;
  3035. a->to_inv = s.inventoryloc;
  3036. a->to_list = s.listname;
  3037. a->to_i = s.i;
  3038. m_invmgr->inventoryAction(a);
  3039. } else if (shift_move_amount > 0) {
  3040. u32 mis = m_inventory_rings.size();
  3041. u32 i = 0;
  3042. for (; i < mis; i++) {
  3043. const ListRingSpec &sp = m_inventory_rings[i];
  3044. if (sp.inventoryloc == s.inventoryloc
  3045. && sp.listname == s.listname)
  3046. break;
  3047. }
  3048. do {
  3049. if (i >= mis) // if not found
  3050. break;
  3051. u32 to_inv_ind = (i + 1) % mis;
  3052. const ListRingSpec &to_inv_sp = m_inventory_rings[to_inv_ind];
  3053. InventoryList *list_from = list_s;
  3054. if (!s.isValid())
  3055. break;
  3056. Inventory *inv_to = m_invmgr->getInventory(to_inv_sp.inventoryloc);
  3057. if (!inv_to)
  3058. break;
  3059. InventoryList *list_to = inv_to->getList(to_inv_sp.listname);
  3060. if (!list_to)
  3061. break;
  3062. ItemStack stack_from = list_from->getItem(s.i);
  3063. assert(shift_move_amount <= stack_from.count);
  3064. if (m_client->getProtoVersion() >= 25) {
  3065. infostream << "Handing IAction::Move to manager" << std::endl;
  3066. IMoveAction *a = new IMoveAction();
  3067. a->count = shift_move_amount;
  3068. a->from_inv = s.inventoryloc;
  3069. a->from_list = s.listname;
  3070. a->from_i = s.i;
  3071. a->to_inv = to_inv_sp.inventoryloc;
  3072. a->to_list = to_inv_sp.listname;
  3073. a->move_somewhere = true;
  3074. m_invmgr->inventoryAction(a);
  3075. } else {
  3076. // find a place (or more than one) to add the new item
  3077. u32 ilt_size = list_to->getSize();
  3078. ItemStack leftover;
  3079. for (u32 slot_to = 0; slot_to < ilt_size
  3080. && shift_move_amount > 0; slot_to++) {
  3081. list_to->itemFits(slot_to, stack_from, &leftover);
  3082. if (leftover.count < stack_from.count) {
  3083. infostream << "Handing IAction::Move to manager" << std::endl;
  3084. IMoveAction *a = new IMoveAction();
  3085. a->count = MYMIN(shift_move_amount,
  3086. (u32) (stack_from.count - leftover.count));
  3087. shift_move_amount -= a->count;
  3088. a->from_inv = s.inventoryloc;
  3089. a->from_list = s.listname;
  3090. a->from_i = s.i;
  3091. a->to_inv = to_inv_sp.inventoryloc;
  3092. a->to_list = to_inv_sp.listname;
  3093. a->to_i = slot_to;
  3094. m_invmgr->inventoryAction(a);
  3095. stack_from = leftover;
  3096. }
  3097. }
  3098. }
  3099. } while (0);
  3100. } else if (drop_amount > 0) {
  3101. m_selected_content_guess = ItemStack(); // Clear
  3102. // Send IAction::Drop
  3103. assert(m_selected_item && m_selected_item->isValid());
  3104. assert(inv_selected);
  3105. InventoryList *list_from = inv_selected->getList(m_selected_item->listname);
  3106. assert(list_from);
  3107. ItemStack stack_from = list_from->getItem(m_selected_item->i);
  3108. // Check how many items can be dropped
  3109. drop_amount = stack_from.count = MYMIN(drop_amount, stack_from.count);
  3110. assert(drop_amount > 0 && drop_amount <= m_selected_amount);
  3111. m_selected_amount -= drop_amount;
  3112. infostream << "Handing IAction::Drop to manager" << std::endl;
  3113. IDropAction *a = new IDropAction();
  3114. a->count = drop_amount;
  3115. a->from_inv = m_selected_item->inventoryloc;
  3116. a->from_list = m_selected_item->listname;
  3117. a->from_i = m_selected_item->i;
  3118. m_invmgr->inventoryAction(a);
  3119. } else if (craft_amount > 0) {
  3120. assert(s.isValid());
  3121. // if there are no items selected or the selected item
  3122. // belongs to craftresult list, proceed with crafting
  3123. if (m_selected_item == NULL ||
  3124. !m_selected_item->isValid() || m_selected_item->listname == "craftresult") {
  3125. m_selected_content_guess = ItemStack(); // Clear
  3126. assert(inv_s);
  3127. // Send IACTION_CRAFT
  3128. infostream << "Handing IACTION_CRAFT to manager" << std::endl;
  3129. ICraftAction *a = new ICraftAction();
  3130. a->count = craft_amount;
  3131. a->craft_inv = s.inventoryloc;
  3132. m_invmgr->inventoryAction(a);
  3133. }
  3134. }
  3135. // If m_selected_amount has been decreased to zero, deselect
  3136. if (m_selected_amount == 0) {
  3137. delete m_selected_item;
  3138. m_selected_item = NULL;
  3139. m_selected_amount = 0;
  3140. m_selected_dragging = false;
  3141. m_selected_content_guess = ItemStack();
  3142. }
  3143. m_old_pointer = m_pointer;
  3144. }
  3145. if (event.EventType == EET_GUI_EVENT) {
  3146. if (event.GUIEvent.EventType == gui::EGET_TAB_CHANGED
  3147. && isVisible()) {
  3148. // find the element that was clicked
  3149. for (unsigned int i=0; i<m_fields.size(); i++) {
  3150. FieldSpec &s = m_fields[i];
  3151. if ((s.ftype == f_TabHeader) &&
  3152. (s.fid == event.GUIEvent.Caller->getID())) {
  3153. s.send = true;
  3154. acceptInput();
  3155. s.send = false;
  3156. return true;
  3157. }
  3158. }
  3159. }
  3160. if (event.GUIEvent.EventType == gui::EGET_ELEMENT_FOCUS_LOST
  3161. && isVisible()) {
  3162. if (!canTakeFocus(event.GUIEvent.Element)) {
  3163. infostream<<"GUIFormSpecMenu: Not allowing focus change."
  3164. <<std::endl;
  3165. // Returning true disables focus change
  3166. return true;
  3167. }
  3168. }
  3169. if ((event.GUIEvent.EventType == gui::EGET_BUTTON_CLICKED) ||
  3170. (event.GUIEvent.EventType == gui::EGET_CHECKBOX_CHANGED) ||
  3171. (event.GUIEvent.EventType == gui::EGET_COMBO_BOX_CHANGED) ||
  3172. (event.GUIEvent.EventType == gui::EGET_SCROLL_BAR_CHANGED)) {
  3173. unsigned int btn_id = event.GUIEvent.Caller->getID();
  3174. if (btn_id == 257) {
  3175. if (m_allowclose) {
  3176. acceptInput(quit_mode_accept);
  3177. quitMenu();
  3178. } else {
  3179. acceptInput();
  3180. m_text_dst->gotText(L"ExitButton");
  3181. }
  3182. // quitMenu deallocates menu
  3183. return true;
  3184. }
  3185. // find the element that was clicked
  3186. for (u32 i = 0; i < m_fields.size(); i++) {
  3187. FieldSpec &s = m_fields[i];
  3188. // if its a button, set the send field so
  3189. // lua knows which button was pressed
  3190. if (((s.ftype == f_Button) || (s.ftype == f_CheckBox)) &&
  3191. (s.fid == event.GUIEvent.Caller->getID())) {
  3192. s.send = true;
  3193. if (s.is_exit) {
  3194. if (m_allowclose) {
  3195. acceptInput(quit_mode_accept);
  3196. quitMenu();
  3197. } else {
  3198. m_text_dst->gotText(L"ExitButton");
  3199. }
  3200. return true;
  3201. } else {
  3202. acceptInput(quit_mode_no);
  3203. s.send = false;
  3204. return true;
  3205. }
  3206. } else if ((s.ftype == f_DropDown) &&
  3207. (s.fid == event.GUIEvent.Caller->getID())) {
  3208. // only send the changed dropdown
  3209. for (u32 i = 0; i < m_fields.size(); i++) {
  3210. FieldSpec &s2 = m_fields[i];
  3211. if (s2.ftype == f_DropDown) {
  3212. s2.send = false;
  3213. }
  3214. }
  3215. s.send = true;
  3216. acceptInput(quit_mode_no);
  3217. // revert configuration to make sure dropdowns are sent on
  3218. // regular button click
  3219. for (u32 i = 0; i < m_fields.size(); i++) {
  3220. FieldSpec &s2 = m_fields[i];
  3221. if (s2.ftype == f_DropDown) {
  3222. s2.send = true;
  3223. }
  3224. }
  3225. return true;
  3226. } else if ((s.ftype == f_ScrollBar) &&
  3227. (s.fid == event.GUIEvent.Caller->getID())) {
  3228. s.fdefault = L"Changed";
  3229. acceptInput(quit_mode_no);
  3230. s.fdefault = L"";
  3231. }
  3232. }
  3233. }
  3234. if (event.GUIEvent.EventType == gui::EGET_EDITBOX_ENTER) {
  3235. if (event.GUIEvent.Caller->getID() > 257) {
  3236. bool close_on_enter = true;
  3237. for (u32 i = 0; i < m_fields.size(); i++) {
  3238. FieldSpec &s = m_fields[i];
  3239. if (s.ftype == f_Unknown &&
  3240. s.fid == event.GUIEvent.Caller->getID()) {
  3241. current_field_enter_pending = s.fname;
  3242. std::unordered_map<std::string, bool>::const_iterator it =
  3243. field_close_on_enter.find(s.fname);
  3244. if (it != field_close_on_enter.end())
  3245. close_on_enter = (*it).second;
  3246. break;
  3247. }
  3248. }
  3249. if (m_allowclose && close_on_enter) {
  3250. current_keys_pending.key_enter = true;
  3251. acceptInput(quit_mode_accept);
  3252. quitMenu();
  3253. } else {
  3254. current_keys_pending.key_enter = true;
  3255. acceptInput();
  3256. }
  3257. // quitMenu deallocates menu
  3258. return true;
  3259. }
  3260. }
  3261. if (event.GUIEvent.EventType == gui::EGET_TABLE_CHANGED) {
  3262. int current_id = event.GUIEvent.Caller->getID();
  3263. if (current_id > 257) {
  3264. // find the element that was clicked
  3265. for (u32 i = 0; i < m_fields.size(); i++) {
  3266. FieldSpec &s = m_fields[i];
  3267. // if it's a table, set the send field
  3268. // so lua knows which table was changed
  3269. if ((s.ftype == f_Table) && (s.fid == current_id)) {
  3270. s.send = true;
  3271. acceptInput();
  3272. s.send=false;
  3273. }
  3274. }
  3275. return true;
  3276. }
  3277. }
  3278. }
  3279. return Parent ? Parent->OnEvent(event) : false;
  3280. }
  3281. /**
  3282. * get name of element by element id
  3283. * @param id of element
  3284. * @return name string or empty string
  3285. */
  3286. std::string GUIFormSpecMenu::getNameByID(s32 id)
  3287. {
  3288. for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
  3289. iter != m_fields.end(); ++iter) {
  3290. if (iter->fid == id) {
  3291. return iter->fname;
  3292. }
  3293. }
  3294. return "";
  3295. }
  3296. /**
  3297. * get label of element by id
  3298. * @param id of element
  3299. * @return label string or empty string
  3300. */
  3301. std::wstring GUIFormSpecMenu::getLabelByID(s32 id)
  3302. {
  3303. for(std::vector<FieldSpec>::iterator iter = m_fields.begin();
  3304. iter != m_fields.end(); ++iter) {
  3305. if (iter->fid == id) {
  3306. return iter->flabel;
  3307. }
  3308. }
  3309. return L"";
  3310. }