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