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