]> git.lizzy.rs Git - minetest.git/blob - src/gui/guiFormSpecMenu.h
Fix memory leak in GUIHyperText (#9489)
[minetest.git] / src / gui / guiFormSpecMenu.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #pragma once
21
22 #include <utility>
23 #include <stack>
24 #include <unordered_set>
25
26 #include "irrlichttypes_extrabloated.h"
27 #include "inventorymanager.h"
28 #include "modalMenu.h"
29 #include "guiInventoryList.h"
30 #include "guiTable.h"
31 #include "network/networkprotocol.h"
32 #include "client/joystick_controller.h"
33 #include "util/string.h"
34 #include "util/enriched_string.h"
35 #include "StyleSpec.h"
36
37 class InventoryManager;
38 class ISimpleTextureSource;
39 class Client;
40 class GUIScrollBar;
41 class TexturePool;
42
43 typedef enum {
44         f_Button,
45         f_Table,
46         f_TabHeader,
47         f_CheckBox,
48         f_DropDown,
49         f_ScrollBar,
50         f_Box,
51         f_ItemImage,
52         f_Unknown
53 } FormspecFieldType;
54
55 typedef enum {
56         quit_mode_no,
57         quit_mode_accept,
58         quit_mode_cancel
59 } FormspecQuitMode;
60
61 struct TextDest
62 {
63         virtual ~TextDest() = default;
64
65         // This is deprecated I guess? -celeron55
66         virtual void gotText(const std::wstring &text) {}
67         virtual void gotText(const StringMap &fields) = 0;
68
69         std::string m_formname;
70 };
71
72 class IFormSource
73 {
74 public:
75         virtual ~IFormSource() = default;
76         virtual const std::string &getForm() const = 0;
77         // Fill in variables in field text
78         virtual std::string resolveText(const std::string &str) { return str; }
79 };
80
81 class GUIFormSpecMenu : public GUIModalMenu
82 {
83         struct ListRingSpec
84         {
85                 ListRingSpec() = default;
86
87                 ListRingSpec(const InventoryLocation &a_inventoryloc,
88                                 const std::string &a_listname):
89                         inventoryloc(a_inventoryloc),
90                         listname(a_listname)
91                 {
92                 }
93
94                 InventoryLocation inventoryloc;
95                 std::string listname;
96         };
97
98         struct FieldSpec
99         {
100                 FieldSpec() = default;
101
102                 FieldSpec(const std::string &name, const std::wstring &label,
103                                 const std::wstring &default_text, s32 id, int priority = 0,
104                                 gui::ECURSOR_ICON cursor_icon = ECI_NORMAL) :
105                         fname(name),
106                         flabel(label),
107                         fdefault(unescape_enriched(translate_string(default_text))),
108                         fid(id),
109                         send(false),
110                         ftype(f_Unknown),
111                         is_exit(false),
112                         priority(priority),
113                         fcursor_icon(cursor_icon)
114                 {
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         };
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
141                 std::wstring tooltip;
142                 irr::video::SColor bgcolor;
143                 irr::video::SColor color;
144         };
145
146 public:
147         GUIFormSpecMenu(JoystickController *joystick,
148                         gui::IGUIElement* parent, s32 id,
149                         IMenuManager *menumgr,
150                         Client *client,
151                         ISimpleTextureSource *tsrc,
152                         IFormSource* fs_src,
153                         TextDest* txt_dst,
154                         const std::string &formspecPrepend,
155                         bool remap_dbl_click = true);
156
157         ~GUIFormSpecMenu();
158
159         static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
160                 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
161                 const std::string &formspecPrepend);
162
163         void setFormSpec(const std::string &formspec_string,
164                         const InventoryLocation &current_inventory_location)
165         {
166                 m_formspec_string = formspec_string;
167                 m_current_inventory_location = current_inventory_location;
168                 regenerateGui(m_screensize_old);
169         }
170
171         const InventoryLocation &getFormspecLocation()
172         {
173                 return m_current_inventory_location;
174         }
175
176         void setFormspecPrepend(const std::string &formspecPrepend)
177         {
178                 m_formspec_prepend = formspecPrepend;
179         }
180
181         // form_src is deleted by this GUIFormSpecMenu
182         void setFormSource(IFormSource *form_src)
183         {
184                 delete m_form_src;
185                 m_form_src = form_src;
186         }
187
188         // text_dst is deleted by this GUIFormSpecMenu
189         void setTextDest(TextDest *text_dst)
190         {
191                 delete m_text_dst;
192                 m_text_dst = text_dst;
193         }
194
195         void allowClose(bool value)
196         {
197                 m_allowclose = value;
198         }
199
200         void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
201         {
202                 m_lock = lock;
203                 m_lockscreensize = basescreensize;
204         }
205
206         void removeChildren();
207         void setInitialFocus();
208
209         void setFocus(const std::string &elementname)
210         {
211                 m_focused_element = elementname;
212         }
213
214         Client *getClient() const
215         {
216                 return m_client;
217         }
218
219         const GUIInventoryList::ItemSpec *getSelectedItem() const
220         {
221                 return m_selected_item;
222         }
223
224         const u16 getSelectedAmount() const
225         {
226                 return m_selected_amount;
227         }
228
229         bool doTooltipAppendItemname() const
230         {
231                 return m_tooltip_append_itemname;
232         }
233
234         void addHoveredItemTooltip(const std::string &name)
235         {
236                 m_hovered_item_tooltips.emplace_back(name);
237         }
238
239         /*
240                 Remove and re-add (or reposition) stuff
241         */
242         void regenerateGui(v2u32 screensize);
243
244         GUIInventoryList::ItemSpec getItemAtPos(v2s32 p) const;
245         void drawSelectedItem();
246         void drawMenu();
247         void updateSelectedItem();
248         ItemStack verifySelectedItem();
249
250         void acceptInput(FormspecQuitMode quitmode);
251         bool preprocessEvent(const SEvent& event);
252         bool OnEvent(const SEvent& event);
253         bool doPause;
254         bool pausesGame() { return doPause; }
255
256         GUITable* getTable(const std::string &tablename);
257         std::vector<std::string>* getDropDownValues(const std::string &name);
258
259 #ifdef __ANDROID__
260         bool getAndroidUIInput();
261 #endif
262
263 protected:
264         v2s32 getBasePos() const
265         {
266                         return padding + offset + AbsoluteRect.UpperLeftCorner;
267         }
268         std::wstring getLabelByID(s32 id);
269         std::string getNameByID(s32 id);
270         const FieldSpec *getSpecByID(s32 id);
271         v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
272         v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
273         v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
274
275         std::unordered_map<std::string, StyleSpec> theme_by_type;
276         std::unordered_map<std::string, StyleSpec> theme_by_name;
277         std::unordered_set<std::string> property_warned;
278
279         StyleSpec getStyleForElement(const std::string &type,
280                         const std::string &name="", const std::string &parent_type="");
281
282         v2s32 padding;
283         v2f32 spacing;
284         v2s32 imgsize;
285         v2s32 offset;
286         v2f32 pos_offset;
287         std::stack<v2f32> container_stack;
288
289         InventoryManager *m_invmgr;
290         ISimpleTextureSource *m_tsrc;
291         Client *m_client;
292
293         std::string m_formspec_string;
294         std::string m_formspec_prepend;
295         InventoryLocation m_current_inventory_location;
296
297         std::vector<GUIInventoryList *> m_inventorylists;
298         std::vector<ListRingSpec> m_inventory_rings;
299         std::vector<gui::IGUIElement *> m_backgrounds;
300         std::unordered_map<std::string, bool> field_close_on_enter;
301         std::vector<FieldSpec> m_fields;
302         std::vector<std::pair<FieldSpec, GUITable *>> m_tables;
303         std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes;
304         std::map<std::string, TooltipSpec> m_tooltips;
305         std::vector<std::pair<gui::IGUIElement *, TooltipSpec>> m_tooltip_rects;
306         std::vector<std::pair<FieldSpec, GUIScrollBar *>> m_scrollbars;
307         std::vector<std::pair<FieldSpec, std::vector<std::string>>> m_dropdowns;
308
309         GUIInventoryList::ItemSpec *m_selected_item = nullptr;
310         u16 m_selected_amount = 0;
311         bool m_selected_dragging = false;
312         ItemStack m_selected_swap;
313
314         gui::IGUIStaticText *m_tooltip_element = nullptr;
315
316         u64 m_tooltip_show_delay;
317         bool m_tooltip_append_itemname;
318         u64 m_hovered_time = 0;
319         s32 m_old_tooltip_id = -1;
320
321         bool m_auto_place = false;
322
323         bool m_allowclose = true;
324         bool m_lock = false;
325         v2u32 m_lockscreensize;
326
327         bool m_bgnonfullscreen;
328         bool m_bgfullscreen;
329         video::SColor m_bgcolor;
330         video::SColor m_fullscreen_bgcolor;
331         video::SColor m_default_tooltip_bgcolor;
332         video::SColor m_default_tooltip_color;
333
334
335 private:
336         IFormSource        *m_form_src;
337         TextDest           *m_text_dst;
338         u16                 m_formspec_version = 1;
339         std::string         m_focused_element = "";
340         JoystickController *m_joystick;
341
342         typedef struct {
343                 bool explicit_size;
344                 bool real_coordinates;
345                 u8 simple_field_count;
346                 v2f invsize;
347                 v2s32 size;
348                 v2f32 offset;
349                 v2f32 anchor;
350                 core::rect<s32> rect;
351                 v2s32 basepos;
352                 v2u32 screensize;
353                 std::string focused_fieldname;
354                 GUITable::TableOptions table_options;
355                 GUITable::TableColumns table_columns;
356
357                 GUIInventoryList::Options inventorylist_options;
358
359                 struct {
360                         s32 max = 1000;
361                         s32 min = 0;
362                         s32 small_step = 10;
363                         s32 large_step = 100;
364                         s32 thumb_size = 1;
365                         GUIScrollBar::ArrowVisibility arrow_visiblity = GUIScrollBar::DEFAULT;
366                 } scrollbar_options;
367
368                 // used to restore table selection/scroll/treeview state
369                 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
370         } parserData;
371
372         typedef struct {
373                 bool key_up;
374                 bool key_down;
375                 bool key_enter;
376                 bool key_escape;
377         } fs_key_pendig;
378
379         fs_key_pendig current_keys_pending;
380         std::string current_field_enter_pending = "";
381         std::vector<std::string> m_hovered_item_tooltips;
382
383         void parseElement(parserData* data, const std::string &element);
384
385         void parseSize(parserData* data, const std::string &element);
386         void parseContainer(parserData* data, const std::string &element);
387         void parseContainerEnd(parserData* data);
388         void parseList(parserData* data, const std::string &element);
389         void parseListRing(parserData* data, const std::string &element);
390         void parseCheckbox(parserData* data, const std::string &element);
391         void parseImage(parserData* data, const std::string &element);
392         void parseAnimatedImage(parserData *data, const std::string &element);
393         void parseItemImage(parserData* data, const std::string &element);
394         void parseButton(parserData* data, const std::string &element,
395                         const std::string &typ);
396         void parseBackground(parserData* data, const std::string &element);
397         void parseTableOptions(parserData* data, const std::string &element);
398         void parseTableColumns(parserData* data, const std::string &element);
399         void parseTable(parserData* data, const std::string &element);
400         void parseTextList(parserData* data, const std::string &element);
401         void parseDropDown(parserData* data, const std::string &element);
402         void parseFieldCloseOnEnter(parserData *data, const std::string &element);
403         void parsePwdField(parserData* data, const std::string &element);
404         void parseField(parserData* data, const std::string &element, const std::string &type);
405         void createTextField(parserData *data, FieldSpec &spec,
406                 core::rect<s32> &rect, bool is_multiline);
407         void parseSimpleField(parserData* data,std::vector<std::string> &parts);
408         void parseTextArea(parserData* data,std::vector<std::string>& parts,
409                         const std::string &type);
410         void parseHyperText(parserData *data, const std::string &element);
411         void parseLabel(parserData* data, const std::string &element);
412         void parseVertLabel(parserData* data, const std::string &element);
413         void parseImageButton(parserData* data, const std::string &element,
414                         const std::string &type);
415         void parseItemImageButton(parserData* data, const std::string &element);
416         void parseTabHeader(parserData* data, const std::string &element);
417         void parseBox(parserData* data, const std::string &element);
418         void parseBackgroundColor(parserData* data, const std::string &element);
419         void parseListColors(parserData* data, const std::string &element);
420         void parseTooltip(parserData* data, const std::string &element);
421         bool parseVersionDirect(const std::string &data);
422         bool parseSizeDirect(parserData* data, const std::string &element);
423         void parseScrollBar(parserData* data, const std::string &element);
424         void parseScrollBarOptions(parserData *data, const std::string &element);
425         bool parsePositionDirect(parserData *data, const std::string &element);
426         void parsePosition(parserData *data, const std::string &element);
427         bool parseAnchorDirect(parserData *data, const std::string &element);
428         void parseAnchor(parserData *data, const std::string &element);
429         bool parseStyle(parserData *data, const std::string &element, bool style_type);
430
431         void tryClose();
432
433         void showTooltip(const std::wstring &text, const irr::video::SColor &color,
434                 const irr::video::SColor &bgcolor);
435
436         /**
437          * In formspec version < 2 the elements were not ordered properly. Some element
438          * types were drawn before others.
439          * This function sorts the elements in the old order for backwards compatibility.
440          */
441         void legacySortElements(core::list<IGUIElement *>::Iterator from);
442
443         /**
444          * check if event is part of a double click
445          * @param event event to evaluate
446          * @return true/false if a doubleclick was detected
447          */
448         bool DoubleClickDetection(const SEvent event);
449
450         struct clickpos
451         {
452                 v2s32 pos;
453                 s64 time;
454         };
455         clickpos m_doubleclickdetect[2];
456
457         int m_btn_height;
458         gui::IGUIFont *m_font = nullptr;
459
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
469 class FormspecFormSource: public IFormSource
470 {
471 public:
472         FormspecFormSource(const std::string &formspec):
473                 m_formspec(formspec)
474         {
475         }
476
477         ~FormspecFormSource() = default;
478
479         void setForm(const std::string &formspec)
480         {
481                 m_formspec = formspec;
482         }
483
484         const std::string &getForm() const
485         {
486                 return m_formspec;
487         }
488
489         std::string m_formspec;
490 };