guiFormSpecMenu.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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 <optional>
  18. #include <utility>
  19. #include <stack>
  20. #include <unordered_set>
  21. #include "irrlichttypes_extrabloated.h"
  22. #include "irr_ptr.h"
  23. #include "inventory.h"
  24. #include "inventorymanager.h"
  25. #include "modalMenu.h"
  26. #include "guiInventoryList.h"
  27. #include "guiScrollBar.h"
  28. #include "guiTable.h"
  29. #include "network/networkprotocol.h"
  30. #include "client/joystick_controller.h"
  31. #include "util/string.h"
  32. #include "util/enriched_string.h"
  33. #include "StyleSpec.h"
  34. #include <IGUIStaticText.h>
  35. class InventoryManager;
  36. class ISimpleTextureSource;
  37. class Client;
  38. class GUIScrollContainer;
  39. class ISoundManager;
  40. enum FormspecFieldType {
  41. f_Button,
  42. f_Table,
  43. f_TabHeader,
  44. f_CheckBox,
  45. f_DropDown,
  46. f_ScrollBar,
  47. f_Box,
  48. f_ItemImage,
  49. f_HyperText,
  50. f_AnimatedImage,
  51. f_Unknown
  52. };
  53. enum FormspecQuitMode {
  54. quit_mode_no,
  55. quit_mode_accept,
  56. quit_mode_cancel
  57. };
  58. enum ButtonEventType : u8
  59. {
  60. BET_LEFT,
  61. BET_RIGHT,
  62. BET_MIDDLE,
  63. BET_WHEEL_UP,
  64. BET_WHEEL_DOWN,
  65. BET_UP,
  66. BET_DOWN,
  67. BET_MOVE,
  68. BET_OTHER
  69. };
  70. struct TextDest
  71. {
  72. virtual ~TextDest() = default;
  73. // This is deprecated I guess? -celeron55
  74. virtual void gotText(const std::wstring &text) {}
  75. virtual void gotText(const StringMap &fields) = 0;
  76. std::string m_formname;
  77. };
  78. class IFormSource
  79. {
  80. public:
  81. virtual ~IFormSource() = default;
  82. virtual const std::string &getForm() const = 0;
  83. // Fill in variables in field text
  84. virtual std::string resolveText(const std::string &str) { return str; }
  85. };
  86. class GUIFormSpecMenu : public GUIModalMenu
  87. {
  88. struct ListRingSpec
  89. {
  90. ListRingSpec() = default;
  91. ListRingSpec(const InventoryLocation &a_inventoryloc,
  92. const std::string &a_listname):
  93. inventoryloc(a_inventoryloc),
  94. listname(a_listname)
  95. {
  96. }
  97. InventoryLocation inventoryloc;
  98. std::string listname;
  99. };
  100. struct FieldSpec
  101. {
  102. FieldSpec() = default;
  103. FieldSpec(const std::string &name, const std::wstring &label,
  104. const std::wstring &default_text, s32 id, int priority = 0,
  105. gui::ECURSOR_ICON cursor_icon = ECI_NORMAL) :
  106. fname(name),
  107. flabel(label),
  108. fdefault(unescape_enriched(translate_string(default_text))),
  109. fid(id),
  110. send(false),
  111. ftype(f_Unknown),
  112. is_exit(false),
  113. priority(priority),
  114. fcursor_icon(cursor_icon)
  115. {
  116. }
  117. std::string fname;
  118. std::wstring flabel;
  119. std::wstring fdefault;
  120. s32 fid;
  121. bool send;
  122. FormspecFieldType ftype;
  123. bool is_exit;
  124. // Draw priority for formspec version < 3
  125. int priority;
  126. core::rect<s32> rect;
  127. gui::ECURSOR_ICON fcursor_icon;
  128. std::string sound;
  129. };
  130. struct TooltipSpec
  131. {
  132. TooltipSpec() = default;
  133. TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
  134. irr::video::SColor a_color):
  135. tooltip(translate_string(a_tooltip)),
  136. bgcolor(a_bgcolor),
  137. color(a_color)
  138. {
  139. }
  140. std::wstring tooltip;
  141. irr::video::SColor bgcolor;
  142. irr::video::SColor color;
  143. };
  144. public:
  145. GUIFormSpecMenu(JoystickController *joystick,
  146. gui::IGUIElement* parent, s32 id,
  147. IMenuManager *menumgr,
  148. Client *client,
  149. gui::IGUIEnvironment *guienv,
  150. ISimpleTextureSource *tsrc,
  151. ISoundManager *sound_manager,
  152. IFormSource* fs_src,
  153. TextDest* txt_dst,
  154. const std::string &formspecPrepend,
  155. bool remap_dbl_click = true);
  156. ~GUIFormSpecMenu();
  157. static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
  158. gui::IGUIEnvironment *guienv, JoystickController *joystick, IFormSource *fs_src,
  159. TextDest *txt_dest, const std::string &formspecPrepend,
  160. ISoundManager *sound_manager);
  161. void setFormSpec(const std::string &formspec_string,
  162. const InventoryLocation &current_inventory_location)
  163. {
  164. m_formspec_string = formspec_string;
  165. m_current_inventory_location = current_inventory_location;
  166. m_is_form_regenerated = false;
  167. regenerateGui(m_screensize_old);
  168. }
  169. const InventoryLocation &getFormspecLocation()
  170. {
  171. return m_current_inventory_location;
  172. }
  173. void setFormspecPrepend(const std::string &formspecPrepend)
  174. {
  175. m_formspec_prepend = formspecPrepend;
  176. }
  177. // form_src is deleted by this GUIFormSpecMenu
  178. void setFormSource(IFormSource *form_src)
  179. {
  180. delete m_form_src;
  181. m_form_src = form_src;
  182. }
  183. // text_dst is deleted by this GUIFormSpecMenu
  184. void setTextDest(TextDest *text_dst)
  185. {
  186. delete m_text_dst;
  187. m_text_dst = text_dst;
  188. }
  189. void allowClose(bool value)
  190. {
  191. m_allowclose = value;
  192. }
  193. void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
  194. {
  195. m_lock = lock;
  196. m_lockscreensize = basescreensize;
  197. }
  198. void removeTooltip();
  199. void setInitialFocus();
  200. void setFocus(const std::string &elementname)
  201. {
  202. m_focused_element = elementname;
  203. }
  204. Client *getClient() const
  205. {
  206. return m_client;
  207. }
  208. const GUIInventoryList::ItemSpec *getSelectedItem() const
  209. {
  210. return m_selected_item;
  211. }
  212. u16 getSelectedAmount() const
  213. {
  214. return m_selected_amount;
  215. }
  216. bool doTooltipAppendItemname() const
  217. {
  218. return m_tooltip_append_itemname;
  219. }
  220. void addHoveredItemTooltip(const std::string &name)
  221. {
  222. m_hovered_item_tooltips.emplace_back(name);
  223. }
  224. /*
  225. Remove and re-add (or reposition) stuff
  226. */
  227. void regenerateGui(v2u32 screensize);
  228. GUIInventoryList::ItemSpec getItemAtPos(v2s32 p) const;
  229. void drawSelectedItem();
  230. void drawMenu();
  231. void updateSelectedItem();
  232. ItemStack verifySelectedItem();
  233. s16 getNextInventoryRing(const InventoryLocation &inventoryloc, const std::string &listname);
  234. void acceptInput(FormspecQuitMode quitmode=quit_mode_no);
  235. bool preprocessEvent(const SEvent& event);
  236. bool OnEvent(const SEvent& event);
  237. bool doPause;
  238. bool pausesGame() { return doPause; }
  239. GUITable* getTable(const std::string &tablename);
  240. std::vector<std::string>* getDropDownValues(const std::string &name);
  241. // This will only return a meaningful value if called after drawMenu().
  242. core::rect<s32> getAbsoluteRect();
  243. #ifdef __ANDROID__
  244. void getAndroidUIInput();
  245. #endif
  246. protected:
  247. v2s32 getBasePos() const
  248. {
  249. return padding + offset + AbsoluteRect.UpperLeftCorner;
  250. }
  251. std::wstring getLabelByID(s32 id);
  252. std::string getNameByID(s32 id);
  253. const FieldSpec *getSpecByID(s32 id);
  254. v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
  255. v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
  256. v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
  257. bool precheckElement(const std::string &name, const std::string &element,
  258. size_t args_min, size_t args_max, std::vector<std::string> &parts);
  259. std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_type;
  260. std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_name;
  261. std::unordered_set<std::string> property_warned;
  262. StyleSpec getDefaultStyleForElement(const std::string &type,
  263. const std::string &name="", const std::string &parent_type="");
  264. std::array<StyleSpec, StyleSpec::NUM_STATES> getStyleForElement(const std::string &type,
  265. const std::string &name="", const std::string &parent_type="");
  266. v2s32 padding;
  267. v2f32 spacing;
  268. v2s32 imgsize;
  269. v2s32 offset;
  270. v2f32 pos_offset;
  271. std::stack<v2f32> container_stack;
  272. InventoryManager *m_invmgr;
  273. ISimpleTextureSource *m_tsrc;
  274. ISoundManager *m_sound_manager;
  275. Client *m_client;
  276. std::string m_formspec_string;
  277. std::string m_formspec_prepend;
  278. InventoryLocation m_current_inventory_location;
  279. // Default true because we can't control regeneration on resizing, but
  280. // we can control cases when the formspec is shown intentionally.
  281. bool m_is_form_regenerated = true;
  282. std::vector<GUIInventoryList *> m_inventorylists;
  283. std::vector<ListRingSpec> m_inventory_rings;
  284. std::unordered_map<std::string, bool> field_enter_after_edit;
  285. std::unordered_map<std::string, bool> field_close_on_enter;
  286. std::unordered_map<std::string, bool> m_dropdown_index_event;
  287. std::vector<FieldSpec> m_fields;
  288. std::vector<std::pair<FieldSpec, GUITable *>> m_tables;
  289. std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes;
  290. std::map<std::string, TooltipSpec> m_tooltips;
  291. std::vector<std::pair<gui::IGUIElement *, TooltipSpec>> m_tooltip_rects;
  292. std::vector<std::pair<FieldSpec, GUIScrollBar *>> m_scrollbars;
  293. std::vector<std::pair<FieldSpec, std::vector<std::string>>> m_dropdowns;
  294. std::vector<gui::IGUIElement *> m_clickthrough_elements;
  295. std::vector<std::pair<std::string, GUIScrollContainer *>> m_scroll_containers;
  296. GUIInventoryList::ItemSpec *m_selected_item = nullptr;
  297. u16 m_selected_amount = 0;
  298. bool m_selected_dragging = false;
  299. ItemStack m_selected_swap;
  300. ButtonEventType m_held_mouse_button = BET_OTHER;
  301. bool m_shift_move_after_craft = false;
  302. u16 m_left_drag_amount = 0;
  303. ItemStack m_left_drag_stack;
  304. std::vector<std::pair<GUIInventoryList::ItemSpec, ItemStack>> m_left_drag_stacks;
  305. bool m_left_dragging = false;
  306. gui::IGUIStaticText *m_tooltip_element = nullptr;
  307. u64 m_tooltip_show_delay;
  308. bool m_tooltip_append_itemname;
  309. u64 m_hovered_time = 0;
  310. s32 m_old_tooltip_id = -1;
  311. bool m_allowclose = true;
  312. bool m_lock = false;
  313. v2u32 m_lockscreensize;
  314. bool m_bgnonfullscreen;
  315. bool m_bgfullscreen;
  316. video::SColor m_bgcolor;
  317. video::SColor m_fullscreen_bgcolor;
  318. video::SColor m_default_tooltip_bgcolor;
  319. video::SColor m_default_tooltip_color;
  320. private:
  321. IFormSource *m_form_src;
  322. TextDest *m_text_dst;
  323. std::string m_last_formname;
  324. u16 m_formspec_version = 1;
  325. std::optional<std::string> m_focused_element = std::nullopt;
  326. JoystickController *m_joystick;
  327. bool m_show_debug = false;
  328. struct parserData {
  329. bool explicit_size;
  330. bool real_coordinates;
  331. u8 simple_field_count;
  332. v2f invsize;
  333. v2s32 size;
  334. v2f32 offset;
  335. v2f32 anchor;
  336. v2f32 padding;
  337. core::rect<s32> rect;
  338. v2s32 basepos;
  339. v2u32 screensize;
  340. GUITable::TableOptions table_options;
  341. GUITable::TableColumns table_columns;
  342. gui::IGUIElement *current_parent = nullptr;
  343. irr_ptr<gui::IGUIElement> background_parent;
  344. GUIInventoryList::Options inventorylist_options;
  345. struct {
  346. s32 max = 1000;
  347. s32 min = 0;
  348. s32 small_step = 10;
  349. s32 large_step = 100;
  350. s32 thumb_size = 1;
  351. GUIScrollBar::ArrowVisibility arrow_visiblity = GUIScrollBar::DEFAULT;
  352. } scrollbar_options;
  353. // used to restore table selection/scroll/treeview state
  354. std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
  355. };
  356. struct fs_key_pending {
  357. bool key_up;
  358. bool key_down;
  359. bool key_enter;
  360. bool key_escape;
  361. };
  362. fs_key_pending current_keys_pending;
  363. std::string current_field_enter_pending = "";
  364. std::vector<std::string> m_hovered_item_tooltips;
  365. void removeAll();
  366. void parseElement(parserData* data, const std::string &element);
  367. void parseSize(parserData* data, const std::string &element);
  368. void parseContainer(parserData* data, const std::string &element);
  369. void parseContainerEnd(parserData* data);
  370. void parseScrollContainer(parserData *data, const std::string &element);
  371. void parseScrollContainerEnd(parserData *data);
  372. void parseList(parserData* data, const std::string &element);
  373. void parseListRing(parserData* data, const std::string &element);
  374. void parseCheckbox(parserData* data, const std::string &element);
  375. void parseImage(parserData* data, const std::string &element);
  376. void parseAnimatedImage(parserData *data, const std::string &element);
  377. void parseItemImage(parserData* data, const std::string &element);
  378. void parseButton(parserData* data, const std::string &element,
  379. const std::string &typ);
  380. void parseBackground(parserData* data, const std::string &element);
  381. void parseTableOptions(parserData* data, const std::string &element);
  382. void parseTableColumns(parserData* data, const std::string &element);
  383. void parseTable(parserData* data, const std::string &element);
  384. void parseTextList(parserData* data, const std::string &element);
  385. void parseDropDown(parserData* data, const std::string &element);
  386. void parseFieldEnterAfterEdit(parserData *data, const std::string &element);
  387. void parseFieldCloseOnEnter(parserData *data, const std::string &element);
  388. void parsePwdField(parserData* data, const std::string &element);
  389. void parseField(parserData* data, const std::string &element, const std::string &type);
  390. void createTextField(parserData *data, FieldSpec &spec,
  391. core::rect<s32> &rect, bool is_multiline);
  392. void parseSimpleField(parserData* data,std::vector<std::string> &parts);
  393. void parseTextArea(parserData* data,std::vector<std::string>& parts,
  394. const std::string &type);
  395. void parseHyperText(parserData *data, const std::string &element);
  396. void parseLabel(parserData* data, const std::string &element);
  397. void parseVertLabel(parserData* data, const std::string &element);
  398. void parseImageButton(parserData* data, const std::string &element,
  399. const std::string &type);
  400. void parseItemImageButton(parserData* data, const std::string &element);
  401. void parseTabHeader(parserData* data, const std::string &element);
  402. void parseBox(parserData* data, const std::string &element);
  403. void parseBackgroundColor(parserData* data, const std::string &element);
  404. void parseListColors(parserData* data, const std::string &element);
  405. void parseTooltip(parserData* data, const std::string &element);
  406. bool parseVersionDirect(const std::string &data);
  407. bool parseSizeDirect(parserData* data, const std::string &element);
  408. void parseScrollBar(parserData* data, const std::string &element);
  409. void parseScrollBarOptions(parserData *data, const std::string &element);
  410. bool parsePositionDirect(parserData *data, const std::string &element);
  411. void parsePosition(parserData *data, const std::string &element);
  412. bool parseAnchorDirect(parserData *data, const std::string &element);
  413. void parseAnchor(parserData *data, const std::string &element);
  414. bool parsePaddingDirect(parserData *data, const std::string &element);
  415. void parsePadding(parserData *data, const std::string &element);
  416. bool parseStyle(parserData *data, const std::string &element, bool style_type);
  417. void parseSetFocus(const std::string &element);
  418. void parseModel(parserData *data, const std::string &element);
  419. bool parseMiddleRect(const std::string &value, core::rect<s32> *parsed_rect);
  420. void tryClose();
  421. void showTooltip(const std::wstring &text, const irr::video::SColor &color,
  422. const irr::video::SColor &bgcolor);
  423. /**
  424. * In formspec version < 2 the elements were not ordered properly. Some element
  425. * types were drawn before others.
  426. * This function sorts the elements in the old order for backwards compatibility.
  427. */
  428. void legacySortElements(std::list<IGUIElement *>::iterator from);
  429. int m_btn_height;
  430. gui::IGUIFont *m_font = nullptr;
  431. // used by getAbsoluteRect
  432. s32 m_tabheader_upper_edge = 0;
  433. };
  434. class FormspecFormSource: public IFormSource
  435. {
  436. public:
  437. FormspecFormSource(const std::string &formspec):
  438. m_formspec(formspec)
  439. {
  440. }
  441. ~FormspecFormSource() = default;
  442. void setForm(const std::string &formspec)
  443. {
  444. m_formspec = formspec;
  445. }
  446. const std::string &getForm() const
  447. {
  448. return m_formspec;
  449. }
  450. std::string m_formspec;
  451. };