]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiFormSpecMenu.h
Formspec: Make dropdowns optionally return event based on index, not value (#9496)
[dragonfireclient.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 class GUIScrollContainer;
43
44 typedef enum {
45         f_Button,
46         f_Table,
47         f_TabHeader,
48         f_CheckBox,
49         f_DropDown,
50         f_ScrollBar,
51         f_Box,
52         f_ItemImage,
53         f_HyperText,
54         f_AnimatedImage,
55         f_Unknown
56 } FormspecFieldType;
57
58 typedef enum {
59         quit_mode_no,
60         quit_mode_accept,
61         quit_mode_cancel
62 } FormspecQuitMode;
63
64 struct TextDest
65 {
66         virtual ~TextDest() = default;
67
68         // This is deprecated I guess? -celeron55
69         virtual void gotText(const std::wstring &text) {}
70         virtual void gotText(const StringMap &fields) = 0;
71
72         std::string m_formname;
73 };
74
75 class IFormSource
76 {
77 public:
78         virtual ~IFormSource() = default;
79         virtual const std::string &getForm() const = 0;
80         // Fill in variables in field text
81         virtual std::string resolveText(const std::string &str) { return str; }
82 };
83
84 class GUIFormSpecMenu : public GUIModalMenu
85 {
86         struct ListRingSpec
87         {
88                 ListRingSpec() = default;
89
90                 ListRingSpec(const InventoryLocation &a_inventoryloc,
91                                 const std::string &a_listname):
92                         inventoryloc(a_inventoryloc),
93                         listname(a_listname)
94                 {
95                 }
96
97                 InventoryLocation inventoryloc;
98                 std::string listname;
99         };
100
101         struct FieldSpec
102         {
103                 FieldSpec() = default;
104
105                 FieldSpec(const std::string &name, const std::wstring &label,
106                                 const std::wstring &default_text, s32 id, int priority = 0,
107                                 gui::ECURSOR_ICON cursor_icon = ECI_NORMAL) :
108                         fname(name),
109                         flabel(label),
110                         fdefault(unescape_enriched(translate_string(default_text))),
111                         fid(id),
112                         send(false),
113                         ftype(f_Unknown),
114                         is_exit(false),
115                         priority(priority),
116                         fcursor_icon(cursor_icon)
117                 {
118                 }
119
120                 std::string fname;
121                 std::wstring flabel;
122                 std::wstring fdefault;
123                 s32 fid;
124                 bool send;
125                 FormspecFieldType ftype;
126                 bool is_exit;
127                 // Draw priority for formspec version < 3
128                 int priority;
129                 core::rect<s32> rect;
130                 gui::ECURSOR_ICON fcursor_icon;
131         };
132
133         struct TooltipSpec
134         {
135                 TooltipSpec() = default;
136                 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
137                                 irr::video::SColor a_color):
138                         tooltip(translate_string(a_tooltip)),
139                         bgcolor(a_bgcolor),
140                         color(a_color)
141                 {
142                 }
143
144                 std::wstring tooltip;
145                 irr::video::SColor bgcolor;
146                 irr::video::SColor color;
147         };
148
149 public:
150         GUIFormSpecMenu(JoystickController *joystick,
151                         gui::IGUIElement* parent, s32 id,
152                         IMenuManager *menumgr,
153                         Client *client,
154                         ISimpleTextureSource *tsrc,
155                         IFormSource* fs_src,
156                         TextDest* txt_dst,
157                         const std::string &formspecPrepend,
158                         bool remap_dbl_click = true);
159
160         ~GUIFormSpecMenu();
161
162         static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
163                 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
164                 const std::string &formspecPrepend);
165
166         void setFormSpec(const std::string &formspec_string,
167                         const InventoryLocation &current_inventory_location)
168         {
169                 m_formspec_string = formspec_string;
170                 m_current_inventory_location = current_inventory_location;
171                 regenerateGui(m_screensize_old);
172         }
173
174         const InventoryLocation &getFormspecLocation()
175         {
176                 return m_current_inventory_location;
177         }
178
179         void setFormspecPrepend(const std::string &formspecPrepend)
180         {
181                 m_formspec_prepend = formspecPrepend;
182         }
183
184         // form_src is deleted by this GUIFormSpecMenu
185         void setFormSource(IFormSource *form_src)
186         {
187                 delete m_form_src;
188                 m_form_src = form_src;
189         }
190
191         // text_dst is deleted by this GUIFormSpecMenu
192         void setTextDest(TextDest *text_dst)
193         {
194                 delete m_text_dst;
195                 m_text_dst = text_dst;
196         }
197
198         void allowClose(bool value)
199         {
200                 m_allowclose = value;
201         }
202
203         void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
204         {
205                 m_lock = lock;
206                 m_lockscreensize = basescreensize;
207         }
208
209         void removeChildren();
210         void setInitialFocus();
211
212         void setFocus(const std::string &elementname)
213         {
214                 m_focused_element = elementname;
215         }
216
217         Client *getClient() const
218         {
219                 return m_client;
220         }
221
222         const GUIInventoryList::ItemSpec *getSelectedItem() const
223         {
224                 return m_selected_item;
225         }
226
227         const u16 getSelectedAmount() const
228         {
229                 return m_selected_amount;
230         }
231
232         bool doTooltipAppendItemname() const
233         {
234                 return m_tooltip_append_itemname;
235         }
236
237         void addHoveredItemTooltip(const std::string &name)
238         {
239                 m_hovered_item_tooltips.emplace_back(name);
240         }
241
242         /*
243                 Remove and re-add (or reposition) stuff
244         */
245         void regenerateGui(v2u32 screensize);
246
247         GUIInventoryList::ItemSpec getItemAtPos(v2s32 p) const;
248         void drawSelectedItem();
249         void drawMenu();
250         void updateSelectedItem();
251         ItemStack verifySelectedItem();
252
253         void acceptInput(FormspecQuitMode quitmode);
254         bool preprocessEvent(const SEvent& event);
255         bool OnEvent(const SEvent& event);
256         bool doPause;
257         bool pausesGame() { return doPause; }
258
259         GUITable* getTable(const std::string &tablename);
260         std::vector<std::string>* getDropDownValues(const std::string &name);
261
262 #ifdef __ANDROID__
263         bool getAndroidUIInput();
264 #endif
265
266 protected:
267         v2s32 getBasePos() const
268         {
269                         return padding + offset + AbsoluteRect.UpperLeftCorner;
270         }
271         std::wstring getLabelByID(s32 id);
272         std::string getNameByID(s32 id);
273         const FieldSpec *getSpecByID(s32 id);
274         v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
275         v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
276         v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
277
278         std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_type;
279         std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_name;
280         std::unordered_set<std::string> property_warned;
281
282         StyleSpec getDefaultStyleForElement(const std::string &type,
283                         const std::string &name="", const std::string &parent_type="");
284         std::array<StyleSpec, StyleSpec::NUM_STATES> getStyleForElement(const std::string &type,
285                         const std::string &name="", const std::string &parent_type="");
286
287         v2s32 padding;
288         v2f32 spacing;
289         v2s32 imgsize;
290         v2s32 offset;
291         v2f32 pos_offset;
292         std::stack<v2f32> container_stack;
293
294         InventoryManager *m_invmgr;
295         ISimpleTextureSource *m_tsrc;
296         Client *m_client;
297
298         std::string m_formspec_string;
299         std::string m_formspec_prepend;
300         InventoryLocation m_current_inventory_location;
301
302         std::vector<GUIInventoryList *> m_inventorylists;
303         std::vector<ListRingSpec> m_inventory_rings;
304         std::vector<gui::IGUIElement *> m_backgrounds;
305         std::unordered_map<std::string, bool> field_close_on_enter;
306         std::unordered_map<std::string, bool> m_dropdown_index_event;
307         std::vector<FieldSpec> m_fields;
308         std::vector<std::pair<FieldSpec, GUITable *>> m_tables;
309         std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes;
310         std::map<std::string, TooltipSpec> m_tooltips;
311         std::vector<std::pair<gui::IGUIElement *, TooltipSpec>> m_tooltip_rects;
312         std::vector<std::pair<FieldSpec, GUIScrollBar *>> m_scrollbars;
313         std::vector<std::pair<FieldSpec, std::vector<std::string>>> m_dropdowns;
314         std::vector<gui::IGUIElement *> m_clickthrough_elements;
315         std::vector<std::pair<std::string, GUIScrollContainer *>> m_scroll_containers;
316
317         GUIInventoryList::ItemSpec *m_selected_item = nullptr;
318         u16 m_selected_amount = 0;
319         bool m_selected_dragging = false;
320         ItemStack m_selected_swap;
321
322         gui::IGUIStaticText *m_tooltip_element = nullptr;
323
324         u64 m_tooltip_show_delay;
325         bool m_tooltip_append_itemname;
326         u64 m_hovered_time = 0;
327         s32 m_old_tooltip_id = -1;
328
329         bool m_auto_place = false;
330
331         bool m_allowclose = true;
332         bool m_lock = false;
333         v2u32 m_lockscreensize;
334
335         bool m_bgnonfullscreen;
336         bool m_bgfullscreen;
337         video::SColor m_bgcolor;
338         video::SColor m_fullscreen_bgcolor;
339         video::SColor m_default_tooltip_bgcolor;
340         video::SColor m_default_tooltip_color;
341
342
343 private:
344         IFormSource        *m_form_src;
345         TextDest           *m_text_dst;
346         u16                 m_formspec_version = 1;
347         std::string         m_focused_element = "";
348         JoystickController *m_joystick;
349         bool m_show_debug = false;
350
351         typedef struct {
352                 bool explicit_size;
353                 bool real_coordinates;
354                 u8 simple_field_count;
355                 v2f invsize;
356                 v2s32 size;
357                 v2f32 offset;
358                 v2f32 anchor;
359                 core::rect<s32> rect;
360                 v2s32 basepos;
361                 v2u32 screensize;
362                 std::string focused_fieldname;
363                 GUITable::TableOptions table_options;
364                 GUITable::TableColumns table_columns;
365                 gui::IGUIElement *current_parent = nullptr;
366
367                 GUIInventoryList::Options inventorylist_options;
368
369                 struct {
370                         s32 max = 1000;
371                         s32 min = 0;
372                         s32 small_step = 10;
373                         s32 large_step = 100;
374                         s32 thumb_size = 1;
375                         GUIScrollBar::ArrowVisibility arrow_visiblity = GUIScrollBar::DEFAULT;
376                 } scrollbar_options;
377
378                 // used to restore table selection/scroll/treeview state
379                 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
380         } parserData;
381
382         typedef struct {
383                 bool key_up;
384                 bool key_down;
385                 bool key_enter;
386                 bool key_escape;
387         } fs_key_pendig;
388
389         fs_key_pendig current_keys_pending;
390         std::string current_field_enter_pending = "";
391         std::vector<std::string> m_hovered_item_tooltips;
392
393         void parseElement(parserData* data, const std::string &element);
394
395         void parseSize(parserData* data, const std::string &element);
396         void parseContainer(parserData* data, const std::string &element);
397         void parseContainerEnd(parserData* data);
398         void parseScrollContainer(parserData *data, const std::string &element);
399         void parseScrollContainerEnd(parserData *data);
400         void parseList(parserData* data, const std::string &element);
401         void parseListRing(parserData* data, const std::string &element);
402         void parseCheckbox(parserData* data, const std::string &element);
403         void parseImage(parserData* data, const std::string &element);
404         void parseAnimatedImage(parserData *data, const std::string &element);
405         void parseItemImage(parserData* data, const std::string &element);
406         void parseButton(parserData* data, const std::string &element,
407                         const std::string &typ);
408         void parseBackground(parserData* data, const std::string &element);
409         void parseTableOptions(parserData* data, const std::string &element);
410         void parseTableColumns(parserData* data, const std::string &element);
411         void parseTable(parserData* data, const std::string &element);
412         void parseTextList(parserData* data, const std::string &element);
413         void parseDropDown(parserData* data, const std::string &element);
414         void parseFieldCloseOnEnter(parserData *data, const std::string &element);
415         void parsePwdField(parserData* data, const std::string &element);
416         void parseField(parserData* data, const std::string &element, const std::string &type);
417         void createTextField(parserData *data, FieldSpec &spec,
418                 core::rect<s32> &rect, bool is_multiline);
419         void parseSimpleField(parserData* data,std::vector<std::string> &parts);
420         void parseTextArea(parserData* data,std::vector<std::string>& parts,
421                         const std::string &type);
422         void parseHyperText(parserData *data, const std::string &element);
423         void parseLabel(parserData* data, const std::string &element);
424         void parseVertLabel(parserData* data, const std::string &element);
425         void parseImageButton(parserData* data, const std::string &element,
426                         const std::string &type);
427         void parseItemImageButton(parserData* data, const std::string &element);
428         void parseTabHeader(parserData* data, const std::string &element);
429         void parseBox(parserData* data, const std::string &element);
430         void parseBackgroundColor(parserData* data, const std::string &element);
431         void parseListColors(parserData* data, const std::string &element);
432         void parseTooltip(parserData* data, const std::string &element);
433         bool parseVersionDirect(const std::string &data);
434         bool parseSizeDirect(parserData* data, const std::string &element);
435         void parseScrollBar(parserData* data, const std::string &element);
436         void parseScrollBarOptions(parserData *data, const std::string &element);
437         bool parsePositionDirect(parserData *data, const std::string &element);
438         void parsePosition(parserData *data, const std::string &element);
439         bool parseAnchorDirect(parserData *data, const std::string &element);
440         void parseAnchor(parserData *data, const std::string &element);
441         bool parseStyle(parserData *data, const std::string &element, bool style_type);
442
443         void tryClose();
444
445         void showTooltip(const std::wstring &text, const irr::video::SColor &color,
446                 const irr::video::SColor &bgcolor);
447
448         /**
449          * In formspec version < 2 the elements were not ordered properly. Some element
450          * types were drawn before others.
451          * This function sorts the elements in the old order for backwards compatibility.
452          */
453         void legacySortElements(core::list<IGUIElement *>::Iterator from);
454
455         /**
456          * check if event is part of a double click
457          * @param event event to evaluate
458          * @return true/false if a doubleclick was detected
459          */
460         bool DoubleClickDetection(const SEvent event);
461
462         struct clickpos
463         {
464                 v2s32 pos;
465                 s64 time;
466         };
467         clickpos m_doubleclickdetect[2];
468
469         int m_btn_height;
470         gui::IGUIFont *m_font = nullptr;
471
472         /* If true, remap a double-click (or double-tap) action to ESC. This is so
473          * that, for example, Android users can double-tap to close a formspec.
474         *
475          * This value can (currently) only be set by the class constructor
476          * and the default value for the setting is true.
477          */
478         bool m_remap_dbl_click;
479 };
480
481 class FormspecFormSource: public IFormSource
482 {
483 public:
484         FormspecFormSource(const std::string &formspec):
485                 m_formspec(formspec)
486         {
487         }
488
489         ~FormspecFormSource() = default;
490
491         void setForm(const std::string &formspec)
492         {
493                 m_formspec = formspec;
494         }
495
496         const std::string &getForm() const
497         {
498                 return m_formspec;
499         }
500
501         std::string m_formspec;
502 };