guiFormSpecMenu.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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. #pragma once
  17. #include <utility>
  18. #include <stack>
  19. #include "irrlichttypes_extrabloated.h"
  20. #include "inventorymanager.h"
  21. #include "modalMenu.h"
  22. #include "guiTable.h"
  23. #include "network/networkprotocol.h"
  24. #include "client/joystick_controller.h"
  25. #include "util/string.h"
  26. #include "util/enriched_string.h"
  27. class InventoryManager;
  28. class ISimpleTextureSource;
  29. class Client;
  30. typedef enum {
  31. f_Button,
  32. f_Table,
  33. f_TabHeader,
  34. f_CheckBox,
  35. f_DropDown,
  36. f_ScrollBar,
  37. f_Unknown
  38. } FormspecFieldType;
  39. typedef enum {
  40. quit_mode_no,
  41. quit_mode_accept,
  42. quit_mode_cancel
  43. } FormspecQuitMode;
  44. struct TextDest
  45. {
  46. virtual ~TextDest() = default;
  47. // This is deprecated I guess? -celeron55
  48. virtual void gotText(const std::wstring &text) {}
  49. virtual void gotText(const StringMap &fields) = 0;
  50. std::string m_formname;
  51. };
  52. class IFormSource
  53. {
  54. public:
  55. virtual ~IFormSource() = default;
  56. virtual std::string getForm() = 0;
  57. // Fill in variables in field text
  58. virtual std::string resolveText(const std::string &str) { return str; }
  59. };
  60. class GUIFormSpecMenu : public GUIModalMenu
  61. {
  62. struct ItemSpec
  63. {
  64. ItemSpec() = default;
  65. ItemSpec(const InventoryLocation &a_inventoryloc,
  66. const std::string &a_listname,
  67. s32 a_i) :
  68. inventoryloc(a_inventoryloc),
  69. listname(a_listname),
  70. i(a_i)
  71. {
  72. }
  73. bool isValid() const { return i != -1; }
  74. InventoryLocation inventoryloc;
  75. std::string listname;
  76. s32 i = -1;
  77. };
  78. struct ListDrawSpec
  79. {
  80. ListDrawSpec() = default;
  81. ListDrawSpec(const InventoryLocation &a_inventoryloc,
  82. const std::string &a_listname,
  83. v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
  84. inventoryloc(a_inventoryloc),
  85. listname(a_listname),
  86. pos(a_pos),
  87. geom(a_geom),
  88. start_item_i(a_start_item_i)
  89. {
  90. }
  91. InventoryLocation inventoryloc;
  92. std::string listname;
  93. v2s32 pos;
  94. v2s32 geom;
  95. s32 start_item_i;
  96. };
  97. struct ListRingSpec
  98. {
  99. ListRingSpec() = default;
  100. ListRingSpec(const InventoryLocation &a_inventoryloc,
  101. const std::string &a_listname):
  102. inventoryloc(a_inventoryloc),
  103. listname(a_listname)
  104. {
  105. }
  106. InventoryLocation inventoryloc;
  107. std::string listname;
  108. };
  109. struct ImageDrawSpec
  110. {
  111. ImageDrawSpec():
  112. parent_button(NULL),
  113. clip(false)
  114. {
  115. }
  116. ImageDrawSpec(const std::string &a_name,
  117. const std::string &a_item_name,
  118. gui::IGUIButton *a_parent_button,
  119. const v2s32 &a_pos, const v2s32 &a_geom):
  120. name(a_name),
  121. item_name(a_item_name),
  122. parent_button(a_parent_button),
  123. pos(a_pos),
  124. geom(a_geom),
  125. scale(true),
  126. clip(false)
  127. {
  128. }
  129. ImageDrawSpec(const std::string &a_name,
  130. const std::string &a_item_name,
  131. const v2s32 &a_pos, const v2s32 &a_geom):
  132. name(a_name),
  133. item_name(a_item_name),
  134. parent_button(NULL),
  135. pos(a_pos),
  136. geom(a_geom),
  137. scale(true),
  138. clip(false)
  139. {
  140. }
  141. ImageDrawSpec(const std::string &a_name,
  142. const v2s32 &a_pos, const v2s32 &a_geom, bool clip=false):
  143. name(a_name),
  144. parent_button(NULL),
  145. pos(a_pos),
  146. geom(a_geom),
  147. scale(true),
  148. clip(clip)
  149. {
  150. }
  151. ImageDrawSpec(const std::string &a_name,
  152. const v2s32 &a_pos):
  153. name(a_name),
  154. parent_button(NULL),
  155. pos(a_pos),
  156. scale(false),
  157. clip(false)
  158. {
  159. }
  160. std::string name;
  161. std::string item_name;
  162. gui::IGUIButton *parent_button;
  163. v2s32 pos;
  164. v2s32 geom;
  165. bool scale;
  166. bool clip;
  167. };
  168. struct FieldSpec
  169. {
  170. FieldSpec() = default;
  171. FieldSpec(const std::string &name, const std::wstring &label,
  172. const std::wstring &default_text, int id) :
  173. fname(name),
  174. flabel(label),
  175. fdefault(unescape_enriched(default_text)),
  176. fid(id),
  177. send(false),
  178. ftype(f_Unknown),
  179. is_exit(false)
  180. {
  181. }
  182. std::string fname;
  183. std::wstring flabel;
  184. std::wstring fdefault;
  185. int fid;
  186. bool send;
  187. FormspecFieldType ftype;
  188. bool is_exit;
  189. core::rect<s32> rect;
  190. };
  191. struct BoxDrawSpec
  192. {
  193. BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
  194. pos(a_pos),
  195. geom(a_geom),
  196. color(a_color)
  197. {
  198. }
  199. v2s32 pos;
  200. v2s32 geom;
  201. irr::video::SColor color;
  202. };
  203. struct TooltipSpec
  204. {
  205. TooltipSpec() = default;
  206. TooltipSpec(const std::string &a_tooltip, irr::video::SColor a_bgcolor,
  207. irr::video::SColor a_color):
  208. tooltip(utf8_to_wide(a_tooltip)),
  209. bgcolor(a_bgcolor),
  210. color(a_color)
  211. {
  212. }
  213. std::wstring tooltip;
  214. irr::video::SColor bgcolor;
  215. irr::video::SColor color;
  216. };
  217. struct StaticTextSpec
  218. {
  219. StaticTextSpec():
  220. parent_button(NULL)
  221. {
  222. }
  223. StaticTextSpec(const std::wstring &a_text,
  224. const core::rect<s32> &a_rect):
  225. text(a_text),
  226. rect(a_rect),
  227. parent_button(NULL)
  228. {
  229. }
  230. StaticTextSpec(const std::wstring &a_text,
  231. const core::rect<s32> &a_rect,
  232. gui::IGUIButton *a_parent_button):
  233. text(a_text),
  234. rect(a_rect),
  235. parent_button(a_parent_button)
  236. {
  237. }
  238. std::wstring text;
  239. core::rect<s32> rect;
  240. gui::IGUIButton *parent_button;
  241. };
  242. public:
  243. GUIFormSpecMenu(JoystickController *joystick,
  244. gui::IGUIElement* parent, s32 id,
  245. IMenuManager *menumgr,
  246. Client *client,
  247. ISimpleTextureSource *tsrc,
  248. IFormSource* fs_src,
  249. TextDest* txt_dst,
  250. bool remap_dbl_click = true);
  251. ~GUIFormSpecMenu();
  252. void setFormSpec(const std::string &formspec_string,
  253. const InventoryLocation &current_inventory_location)
  254. {
  255. m_formspec_string = formspec_string;
  256. m_current_inventory_location = current_inventory_location;
  257. regenerateGui(m_screensize_old);
  258. }
  259. // form_src is deleted by this GUIFormSpecMenu
  260. void setFormSource(IFormSource *form_src)
  261. {
  262. delete m_form_src;
  263. m_form_src = form_src;
  264. }
  265. // text_dst is deleted by this GUIFormSpecMenu
  266. void setTextDest(TextDest *text_dst)
  267. {
  268. delete m_text_dst;
  269. m_text_dst = text_dst;
  270. }
  271. void allowClose(bool value)
  272. {
  273. m_allowclose = value;
  274. }
  275. void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
  276. {
  277. m_lock = lock;
  278. m_lockscreensize = basescreensize;
  279. }
  280. void removeChildren();
  281. void setInitialFocus();
  282. void setFocus(const std::string &elementname)
  283. {
  284. m_focused_element = elementname;
  285. }
  286. /*
  287. Remove and re-add (or reposition) stuff
  288. */
  289. void regenerateGui(v2u32 screensize);
  290. ItemSpec getItemAtPos(v2s32 p) const;
  291. void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
  292. void drawSelectedItem();
  293. void drawMenu();
  294. void updateSelectedItem();
  295. ItemStack verifySelectedItem();
  296. void acceptInput(FormspecQuitMode quitmode);
  297. bool preprocessEvent(const SEvent& event);
  298. bool OnEvent(const SEvent& event);
  299. bool doPause;
  300. bool pausesGame() { return doPause; }
  301. GUITable* getTable(const std::string &tablename);
  302. std::vector<std::string>* getDropDownValues(const std::string &name);
  303. #ifdef __ANDROID__
  304. bool getAndroidUIInput();
  305. #endif
  306. protected:
  307. v2s32 getBasePos() const
  308. {
  309. return padding + offset + AbsoluteRect.UpperLeftCorner;
  310. }
  311. v2s32 padding;
  312. v2s32 spacing;
  313. v2s32 imgsize;
  314. v2s32 offset;
  315. v2s32 pos_offset;
  316. std::stack<v2s32> container_stack;
  317. InventoryManager *m_invmgr;
  318. ISimpleTextureSource *m_tsrc;
  319. Client *m_client;
  320. std::string m_formspec_string;
  321. InventoryLocation m_current_inventory_location;
  322. std::vector<ListDrawSpec> m_inventorylists;
  323. std::vector<ListRingSpec> m_inventory_rings;
  324. std::vector<ImageDrawSpec> m_backgrounds;
  325. std::vector<ImageDrawSpec> m_images;
  326. std::vector<ImageDrawSpec> m_itemimages;
  327. std::vector<BoxDrawSpec> m_boxes;
  328. std::unordered_map<std::string, bool> field_close_on_enter;
  329. std::vector<FieldSpec> m_fields;
  330. std::vector<StaticTextSpec> m_static_texts;
  331. std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
  332. std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
  333. std::map<std::string, TooltipSpec> m_tooltips;
  334. std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
  335. std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
  336. ItemSpec *m_selected_item = nullptr;
  337. u32 m_selected_amount = 0;
  338. bool m_selected_dragging = false;
  339. // WARNING: BLACK MAGIC
  340. // Used to guess and keep up with some special things the server can do.
  341. // If name is "", no guess exists.
  342. ItemStack m_selected_content_guess;
  343. InventoryLocation m_selected_content_guess_inventory;
  344. v2s32 m_pointer;
  345. v2s32 m_old_pointer; // Mouse position after previous mouse event
  346. gui::IGUIStaticText *m_tooltip_element = nullptr;
  347. u64 m_tooltip_show_delay;
  348. u64 m_hovered_time = 0;
  349. s32 m_old_tooltip_id = -1;
  350. bool m_rmouse_auto_place = false;
  351. bool m_allowclose = true;
  352. bool m_lock = false;
  353. v2u32 m_lockscreensize;
  354. bool m_bgfullscreen;
  355. bool m_slotborder;
  356. video::SColor m_bgcolor;
  357. video::SColor m_slotbg_n;
  358. video::SColor m_slotbg_h;
  359. video::SColor m_slotbordercolor;
  360. video::SColor m_default_tooltip_bgcolor;
  361. video::SColor m_default_tooltip_color;
  362. private:
  363. IFormSource *m_form_src;
  364. TextDest *m_text_dst;
  365. u32 m_formspec_version = 0;
  366. std::string m_focused_element = "";
  367. JoystickController *m_joystick;
  368. typedef struct {
  369. bool explicit_size;
  370. v2f invsize;
  371. v2s32 size;
  372. v2f32 offset;
  373. v2f32 anchor;
  374. core::rect<s32> rect;
  375. v2s32 basepos;
  376. v2u32 screensize;
  377. std::string focused_fieldname;
  378. GUITable::TableOptions table_options;
  379. GUITable::TableColumns table_columns;
  380. // used to restore table selection/scroll/treeview state
  381. std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
  382. } parserData;
  383. typedef struct {
  384. bool key_up;
  385. bool key_down;
  386. bool key_enter;
  387. bool key_escape;
  388. } fs_key_pendig;
  389. fs_key_pendig current_keys_pending;
  390. std::string current_field_enter_pending = "";
  391. void parseElement(parserData* data, const std::string &element);
  392. void parseSize(parserData* data, const std::string &element);
  393. void parseContainer(parserData* data, const std::string &element);
  394. void parseContainerEnd(parserData* data);
  395. void parseList(parserData* data, const std::string &element);
  396. void parseListRing(parserData* data, const std::string &element);
  397. void parseCheckbox(parserData* data, const std::string &element);
  398. void parseImage(parserData* data, const std::string &element);
  399. void parseItemImage(parserData* data, const std::string &element);
  400. void parseButton(parserData* data, const std::string &element,
  401. const std::string &typ);
  402. void parseBackground(parserData* data, const std::string &element);
  403. void parseTableOptions(parserData* data, const std::string &element);
  404. void parseTableColumns(parserData* data, const std::string &element);
  405. void parseTable(parserData* data, const std::string &element);
  406. void parseTextList(parserData* data, const std::string &element);
  407. void parseDropDown(parserData* data, const std::string &element);
  408. void parseFieldCloseOnEnter(parserData *data, const std::string &element);
  409. void parsePwdField(parserData* data, const std::string &element);
  410. void parseField(parserData* data, const std::string &element, const std::string &type);
  411. void parseSimpleField(parserData* data,std::vector<std::string> &parts);
  412. void parseTextArea(parserData* data,std::vector<std::string>& parts,
  413. const std::string &type);
  414. void parseLabel(parserData* data, const std::string &element);
  415. void parseVertLabel(parserData* data, const std::string &element);
  416. void parseImageButton(parserData* data, const std::string &element,
  417. const std::string &type);
  418. void parseItemImageButton(parserData* data, const std::string &element);
  419. void parseTabHeader(parserData* data, const std::string &element);
  420. void parseBox(parserData* data, const std::string &element);
  421. void parseBackgroundColor(parserData* data, const std::string &element);
  422. void parseListColors(parserData* data, const std::string &element);
  423. void parseTooltip(parserData* data, const std::string &element);
  424. bool parseVersionDirect(const std::string &data);
  425. bool parseSizeDirect(parserData* data, const std::string &element);
  426. void parseScrollBar(parserData* data, const std::string &element);
  427. bool parsePositionDirect(parserData *data, const std::string &element);
  428. void parsePosition(parserData *data, const std::string &element);
  429. bool parseAnchorDirect(parserData *data, const std::string &element);
  430. void parseAnchor(parserData *data, const std::string &element);
  431. void tryClose();
  432. void showTooltip(const std::wstring &text, const irr::video::SColor &color,
  433. const irr::video::SColor &bgcolor);
  434. /**
  435. * check if event is part of a double click
  436. * @param event event to evaluate
  437. * @return true/false if a doubleclick was detected
  438. */
  439. bool DoubleClickDetection(const SEvent event);
  440. struct clickpos
  441. {
  442. v2s32 pos;
  443. s64 time;
  444. };
  445. clickpos m_doubleclickdetect[2];
  446. int m_btn_height;
  447. gui::IGUIFont *m_font = nullptr;
  448. std::wstring getLabelByID(s32 id);
  449. std::string getNameByID(s32 id);
  450. #ifdef __ANDROID__
  451. v2s32 m_down_pos;
  452. std::string m_JavaDialogFieldName;
  453. #endif
  454. /* If true, remap a double-click (or double-tap) action to ESC. This is so
  455. * that, for example, Android users can double-tap to close a formspec.
  456. *
  457. * This value can (currently) only be set by the class constructor
  458. * and the default value for the setting is true.
  459. */
  460. bool m_remap_dbl_click;
  461. };
  462. class FormspecFormSource: public IFormSource
  463. {
  464. public:
  465. FormspecFormSource(const std::string &formspec):
  466. m_formspec(formspec)
  467. {
  468. }
  469. ~FormspecFormSource() = default;
  470. void setForm(const std::string &formspec)
  471. {
  472. m_formspec = FORMSPEC_VERSION_STRING + formspec;
  473. }
  474. std::string getForm() { return m_formspec; }
  475. std::string m_formspec;
  476. };