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