guiFormSpecMenu.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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 const std::string &getForm() const = 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(translate_string(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::wstring &a_tooltip, irr::video::SColor a_bgcolor,
  207. irr::video::SColor a_color):
  208. tooltip(translate_string(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. static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
  253. JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest);
  254. void setFormSpec(const std::string &formspec_string,
  255. const InventoryLocation &current_inventory_location)
  256. {
  257. m_formspec_string = formspec_string;
  258. m_current_inventory_location = current_inventory_location;
  259. regenerateGui(m_screensize_old);
  260. }
  261. // form_src is deleted by this GUIFormSpecMenu
  262. void setFormSource(IFormSource *form_src)
  263. {
  264. delete m_form_src;
  265. m_form_src = form_src;
  266. }
  267. // text_dst is deleted by this GUIFormSpecMenu
  268. void setTextDest(TextDest *text_dst)
  269. {
  270. delete m_text_dst;
  271. m_text_dst = text_dst;
  272. }
  273. void allowClose(bool value)
  274. {
  275. m_allowclose = value;
  276. }
  277. void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
  278. {
  279. m_lock = lock;
  280. m_lockscreensize = basescreensize;
  281. }
  282. void removeChildren();
  283. void setInitialFocus();
  284. void setFocus(const std::string &elementname)
  285. {
  286. m_focused_element = elementname;
  287. }
  288. /*
  289. Remove and re-add (or reposition) stuff
  290. */
  291. void regenerateGui(v2u32 screensize);
  292. ItemSpec getItemAtPos(v2s32 p) const;
  293. void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
  294. void drawSelectedItem();
  295. void drawMenu();
  296. void updateSelectedItem();
  297. ItemStack verifySelectedItem();
  298. void acceptInput(FormspecQuitMode quitmode);
  299. bool preprocessEvent(const SEvent& event);
  300. bool OnEvent(const SEvent& event);
  301. bool doPause;
  302. bool pausesGame() { return doPause; }
  303. GUITable* getTable(const std::string &tablename);
  304. std::vector<std::string>* getDropDownValues(const std::string &name);
  305. #ifdef __ANDROID__
  306. bool getAndroidUIInput();
  307. #endif
  308. protected:
  309. v2s32 getBasePos() const
  310. {
  311. return padding + offset + AbsoluteRect.UpperLeftCorner;
  312. }
  313. v2s32 padding;
  314. v2s32 spacing;
  315. v2s32 imgsize;
  316. v2s32 offset;
  317. v2s32 pos_offset;
  318. std::stack<v2s32> container_stack;
  319. InventoryManager *m_invmgr;
  320. ISimpleTextureSource *m_tsrc;
  321. Client *m_client;
  322. std::string m_formspec_string;
  323. InventoryLocation m_current_inventory_location;
  324. std::vector<ListDrawSpec> m_inventorylists;
  325. std::vector<ListRingSpec> m_inventory_rings;
  326. std::vector<ImageDrawSpec> m_backgrounds;
  327. std::vector<ImageDrawSpec> m_images;
  328. std::vector<ImageDrawSpec> m_itemimages;
  329. std::vector<BoxDrawSpec> m_boxes;
  330. std::unordered_map<std::string, bool> field_close_on_enter;
  331. std::vector<FieldSpec> m_fields;
  332. std::vector<StaticTextSpec> m_static_texts;
  333. std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
  334. std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
  335. std::map<std::string, TooltipSpec> m_tooltips;
  336. std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
  337. std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
  338. ItemSpec *m_selected_item = nullptr;
  339. u32 m_selected_amount = 0;
  340. bool m_selected_dragging = false;
  341. // WARNING: BLACK MAGIC
  342. // Used to guess and keep up with some special things the server can do.
  343. // If name is "", no guess exists.
  344. ItemStack m_selected_content_guess;
  345. InventoryLocation m_selected_content_guess_inventory;
  346. v2s32 m_pointer;
  347. v2s32 m_old_pointer; // Mouse position after previous mouse event
  348. gui::IGUIStaticText *m_tooltip_element = nullptr;
  349. u64 m_tooltip_show_delay;
  350. bool m_tooltip_append_itemname;
  351. u64 m_hovered_time = 0;
  352. s32 m_old_tooltip_id = -1;
  353. bool m_auto_place = false;
  354. bool m_allowclose = true;
  355. bool m_lock = false;
  356. v2u32 m_lockscreensize;
  357. bool m_bgfullscreen;
  358. bool m_slotborder;
  359. video::SColor m_bgcolor;
  360. video::SColor m_fullscreen_bgcolor;
  361. video::SColor m_slotbg_n;
  362. video::SColor m_slotbg_h;
  363. video::SColor m_slotbordercolor;
  364. video::SColor m_default_tooltip_bgcolor;
  365. video::SColor m_default_tooltip_color;
  366. private:
  367. IFormSource *m_form_src;
  368. TextDest *m_text_dst;
  369. u32 m_formspec_version = 0;
  370. std::string m_focused_element = "";
  371. JoystickController *m_joystick;
  372. typedef struct {
  373. bool explicit_size;
  374. v2f invsize;
  375. v2s32 size;
  376. v2f32 offset;
  377. v2f32 anchor;
  378. core::rect<s32> rect;
  379. v2s32 basepos;
  380. v2u32 screensize;
  381. std::string focused_fieldname;
  382. GUITable::TableOptions table_options;
  383. GUITable::TableColumns table_columns;
  384. // used to restore table selection/scroll/treeview state
  385. std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
  386. } parserData;
  387. typedef struct {
  388. bool key_up;
  389. bool key_down;
  390. bool key_enter;
  391. bool key_escape;
  392. } fs_key_pendig;
  393. fs_key_pendig current_keys_pending;
  394. std::string current_field_enter_pending = "";
  395. void parseElement(parserData* data, const std::string &element);
  396. void parseSize(parserData* data, const std::string &element);
  397. void parseContainer(parserData* data, const std::string &element);
  398. void parseContainerEnd(parserData* data);
  399. void parseList(parserData* data, const std::string &element);
  400. void parseListRing(parserData* data, const std::string &element);
  401. void parseCheckbox(parserData* data, const std::string &element);
  402. void parseImage(parserData* data, const std::string &element);
  403. void parseItemImage(parserData* data, const std::string &element);
  404. void parseButton(parserData* data, const std::string &element,
  405. const std::string &typ);
  406. void parseBackground(parserData* data, const std::string &element);
  407. void parseTableOptions(parserData* data, const std::string &element);
  408. void parseTableColumns(parserData* data, const std::string &element);
  409. void parseTable(parserData* data, const std::string &element);
  410. void parseTextList(parserData* data, const std::string &element);
  411. void parseDropDown(parserData* data, const std::string &element);
  412. void parseFieldCloseOnEnter(parserData *data, const std::string &element);
  413. void parsePwdField(parserData* data, const std::string &element);
  414. void parseField(parserData* data, const std::string &element, const std::string &type);
  415. void createTextField(parserData *data, FieldSpec &spec,
  416. core::rect<s32> &rect, bool is_multiline);
  417. void parseSimpleField(parserData* data,std::vector<std::string> &parts);
  418. void parseTextArea(parserData* data,std::vector<std::string>& parts,
  419. const std::string &type);
  420. void parseLabel(parserData* data, const std::string &element);
  421. void parseVertLabel(parserData* data, const std::string &element);
  422. void parseImageButton(parserData* data, const std::string &element,
  423. const std::string &type);
  424. void parseItemImageButton(parserData* data, const std::string &element);
  425. void parseTabHeader(parserData* data, const std::string &element);
  426. void parseBox(parserData* data, const std::string &element);
  427. void parseBackgroundColor(parserData* data, const std::string &element);
  428. void parseListColors(parserData* data, const std::string &element);
  429. void parseTooltip(parserData* data, const std::string &element);
  430. bool parseVersionDirect(const std::string &data);
  431. bool parseSizeDirect(parserData* data, const std::string &element);
  432. void parseScrollBar(parserData* data, const std::string &element);
  433. bool parsePositionDirect(parserData *data, const std::string &element);
  434. void parsePosition(parserData *data, const std::string &element);
  435. bool parseAnchorDirect(parserData *data, const std::string &element);
  436. void parseAnchor(parserData *data, const std::string &element);
  437. void tryClose();
  438. void showTooltip(const std::wstring &text, const irr::video::SColor &color,
  439. const irr::video::SColor &bgcolor);
  440. /**
  441. * check if event is part of a double click
  442. * @param event event to evaluate
  443. * @return true/false if a doubleclick was detected
  444. */
  445. bool DoubleClickDetection(const SEvent event);
  446. struct clickpos
  447. {
  448. v2s32 pos;
  449. s64 time;
  450. };
  451. clickpos m_doubleclickdetect[2];
  452. int m_btn_height;
  453. gui::IGUIFont *m_font = nullptr;
  454. std::wstring getLabelByID(s32 id);
  455. std::string getNameByID(s32 id);
  456. #ifdef __ANDROID__
  457. v2s32 m_down_pos;
  458. std::string m_JavaDialogFieldName;
  459. #endif
  460. /* If true, remap a double-click (or double-tap) action to ESC. This is so
  461. * that, for example, Android users can double-tap to close a formspec.
  462. *
  463. * This value can (currently) only be set by the class constructor
  464. * and the default value for the setting is true.
  465. */
  466. bool m_remap_dbl_click;
  467. };
  468. class FormspecFormSource: public IFormSource
  469. {
  470. public:
  471. FormspecFormSource(const std::string &formspec):
  472. m_formspec(formspec)
  473. {
  474. }
  475. ~FormspecFormSource() = default;
  476. void setForm(const std::string &formspec)
  477. {
  478. m_formspec = FORMSPEC_VERSION_STRING + formspec;
  479. }
  480. const std::string &getForm() const
  481. {
  482. return m_formspec;
  483. }
  484. std::string m_formspec;
  485. };