]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiFormSpecMenu.h
0b4d3879dc700dc012fba3a3894cf26bd13f3ea2
[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 "guiScrollBar.h"
31 #include "guiTable.h"
32 #include "network/networkprotocol.h"
33 #include "client/joystick_controller.h"
34 #include "util/string.h"
35 #include "util/enriched_string.h"
36 #include "StyleSpec.h"
37
38 class InventoryManager;
39 class ISimpleTextureSource;
40 class Client;
41 class GUIScrollContainer;
42 class ISoundManager;
43
44 enum FormspecFieldType {
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 };
57
58 enum FormspecQuitMode {
59         quit_mode_no,
60         quit_mode_accept,
61         quit_mode_cancel
62 };
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                 std::string sound;
132         };
133
134         struct TooltipSpec
135         {
136                 TooltipSpec() = default;
137                 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
138                                 irr::video::SColor a_color):
139                         tooltip(translate_string(a_tooltip)),
140                         bgcolor(a_bgcolor),
141                         color(a_color)
142                 {
143                 }
144
145                 std::wstring tooltip;
146                 irr::video::SColor bgcolor;
147                 irr::video::SColor color;
148         };
149
150 public:
151         GUIFormSpecMenu(JoystickController *joystick,
152                         gui::IGUIElement* parent, s32 id,
153                         IMenuManager *menumgr,
154                         Client *client,
155                         gui::IGUIEnvironment *guienv,
156                         ISimpleTextureSource *tsrc,
157                         ISoundManager *sound_manager,
158                         IFormSource* fs_src,
159                         TextDest* txt_dst,
160                         const std::string &formspecPrepend,
161                         bool remap_dbl_click = true);
162
163         ~GUIFormSpecMenu();
164
165         static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
166                 gui::IGUIEnvironment *guienv, JoystickController *joystick, IFormSource *fs_src,
167                 TextDest *txt_dest, const std::string &formspecPrepend,
168                 ISoundManager *sound_manager);
169
170         void setFormSpec(const std::string &formspec_string,
171                         const InventoryLocation &current_inventory_location)
172         {
173                 m_formspec_string = formspec_string;
174                 m_current_inventory_location = current_inventory_location;
175                 m_is_form_regenerated = false;
176                 regenerateGui(m_screensize_old);
177         }
178
179         const InventoryLocation &getFormspecLocation()
180         {
181                 return m_current_inventory_location;
182         }
183
184         void setFormspecPrepend(const std::string &formspecPrepend)
185         {
186                 m_formspec_prepend = formspecPrepend;
187         }
188
189         // form_src is deleted by this GUIFormSpecMenu
190         void setFormSource(IFormSource *form_src)
191         {
192                 delete m_form_src;
193                 m_form_src = form_src;
194         }
195
196         // text_dst is deleted by this GUIFormSpecMenu
197         void setTextDest(TextDest *text_dst)
198         {
199                 delete m_text_dst;
200                 m_text_dst = text_dst;
201         }
202
203         void allowClose(bool value)
204         {
205                 m_allowclose = value;
206         }
207
208         void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
209         {
210                 m_lock = lock;
211                 m_lockscreensize = basescreensize;
212         }
213
214         void removeChildren();
215         void setInitialFocus();
216
217         void setFocus(const std::string &elementname)
218         {
219                 m_focused_element = elementname;
220         }
221
222         Client *getClient() const
223         {
224                 return m_client;
225         }
226
227         const GUIInventoryList::ItemSpec *getSelectedItem() const
228         {
229                 return m_selected_item;
230         }
231
232         u16 getSelectedAmount() const
233         {
234                 return m_selected_amount;
235         }
236
237         bool doTooltipAppendItemname() const
238         {
239                 return m_tooltip_append_itemname;
240         }
241
242         void addHoveredItemTooltip(const std::string &name)
243         {
244                 m_hovered_item_tooltips.emplace_back(name);
245         }
246
247         /*
248                 Remove and re-add (or reposition) stuff
249         */
250         void regenerateGui(v2u32 screensize);
251
252         GUIInventoryList::ItemSpec getItemAtPos(v2s32 p) const;
253         void drawSelectedItem();
254         void drawMenu();
255         void updateSelectedItem();
256         ItemStack verifySelectedItem();
257
258         void acceptInput(FormspecQuitMode quitmode=quit_mode_no);
259         bool preprocessEvent(const SEvent& event);
260         bool OnEvent(const SEvent& event);
261         bool doPause;
262         bool pausesGame() { return doPause; }
263
264         GUITable* getTable(const std::string &tablename);
265         std::vector<std::string>* getDropDownValues(const std::string &name);
266
267 #ifdef __ANDROID__
268         bool getAndroidUIInput();
269 #endif
270
271 protected:
272         v2s32 getBasePos() const
273         {
274                         return padding + offset + AbsoluteRect.UpperLeftCorner;
275         }
276         std::wstring getLabelByID(s32 id);
277         std::string getNameByID(s32 id);
278         const FieldSpec *getSpecByID(s32 id);
279         v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
280         v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
281         v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
282         bool precheckElement(const std::string &name, const std::string &element,
283                 size_t args_min, size_t args_max, std::vector<std::string> &parts);
284
285         std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_type;
286         std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_name;
287         std::unordered_set<std::string> property_warned;
288
289         StyleSpec getDefaultStyleForElement(const std::string &type,
290                         const std::string &name="", const std::string &parent_type="");
291         std::array<StyleSpec, StyleSpec::NUM_STATES> getStyleForElement(const std::string &type,
292                         const std::string &name="", const std::string &parent_type="");
293
294         v2s32 padding;
295         v2f32 spacing;
296         v2s32 imgsize;
297         v2s32 offset;
298         v2f32 pos_offset;
299         std::stack<v2f32> container_stack;
300
301         InventoryManager *m_invmgr;
302         ISimpleTextureSource *m_tsrc;
303         ISoundManager *m_sound_manager;
304         Client *m_client;
305
306         std::string m_formspec_string;
307         std::string m_formspec_prepend;
308         InventoryLocation m_current_inventory_location;
309
310         // Default true because we can't control regeneration on resizing, but
311         // we can control cases when the formspec is shown intentionally.
312         bool m_is_form_regenerated = true;
313
314         std::vector<GUIInventoryList *> m_inventorylists;
315         std::vector<ListRingSpec> m_inventory_rings;
316         std::vector<gui::IGUIElement *> m_backgrounds;
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
379                 GUIInventoryList::Options inventorylist_options;
380
381                 struct {
382                         s32 max = 1000;
383                         s32 min = 0;
384                         s32 small_step = 10;
385                         s32 large_step = 100;
386                         s32 thumb_size = 1;
387                         GUIScrollBar::ArrowVisibility arrow_visiblity = GUIScrollBar::DEFAULT;
388                 } scrollbar_options;
389
390                 // used to restore table selection/scroll/treeview state
391                 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
392         };
393
394         struct fs_key_pending {
395                 bool key_up;
396                 bool key_down;
397                 bool key_enter;
398                 bool key_escape;
399         };
400
401         fs_key_pending current_keys_pending;
402         std::string current_field_enter_pending = "";
403         std::vector<std::string> m_hovered_item_tooltips;
404
405         void parseElement(parserData* data, const std::string &element);
406
407         void parseSize(parserData* data, const std::string &element);
408         void parseContainer(parserData* data, const std::string &element);
409         void parseContainerEnd(parserData* data);
410         void parseScrollContainer(parserData *data, const std::string &element);
411         void parseScrollContainerEnd(parserData *data);
412         void parseList(parserData* data, const std::string &element);
413         void parseListRing(parserData* data, const std::string &element);
414         void parseCheckbox(parserData* data, const std::string &element);
415         void parseImage(parserData* data, const std::string &element);
416         void parseAnimatedImage(parserData *data, const std::string &element);
417         void parseItemImage(parserData* data, const std::string &element);
418         void parseButton(parserData* data, const std::string &element,
419                         const std::string &typ);
420         void parseBackground(parserData* data, const std::string &element);
421         void parseTableOptions(parserData* data, const std::string &element);
422         void parseTableColumns(parserData* data, const std::string &element);
423         void parseTable(parserData* data, const std::string &element);
424         void parseTextList(parserData* data, const std::string &element);
425         void parseDropDown(parserData* data, const std::string &element);
426         void parseFieldCloseOnEnter(parserData *data, const std::string &element);
427         void parsePwdField(parserData* data, const std::string &element);
428         void parseField(parserData* data, const std::string &element, const std::string &type);
429         void createTextField(parserData *data, FieldSpec &spec,
430                 core::rect<s32> &rect, bool is_multiline);
431         void parseSimpleField(parserData* data,std::vector<std::string> &parts);
432         void parseTextArea(parserData* data,std::vector<std::string>& parts,
433                         const std::string &type);
434         void parseHyperText(parserData *data, const std::string &element);
435         void parseLabel(parserData* data, const std::string &element);
436         void parseVertLabel(parserData* data, const std::string &element);
437         void parseImageButton(parserData* data, const std::string &element,
438                         const std::string &type);
439         void parseItemImageButton(parserData* data, const std::string &element);
440         void parseTabHeader(parserData* data, const std::string &element);
441         void parseBox(parserData* data, const std::string &element);
442         void parseBackgroundColor(parserData* data, const std::string &element);
443         void parseListColors(parserData* data, const std::string &element);
444         void parseTooltip(parserData* data, const std::string &element);
445         bool parseVersionDirect(const std::string &data);
446         bool parseSizeDirect(parserData* data, const std::string &element);
447         void parseScrollBar(parserData* data, const std::string &element);
448         void parseScrollBarOptions(parserData *data, const std::string &element);
449         bool parsePositionDirect(parserData *data, const std::string &element);
450         void parsePosition(parserData *data, const std::string &element);
451         bool parseAnchorDirect(parserData *data, const std::string &element);
452         void parseAnchor(parserData *data, const std::string &element);
453         bool parsePaddingDirect(parserData *data, const std::string &element);
454         void parsePadding(parserData *data, const std::string &element);
455         bool parseStyle(parserData *data, const std::string &element, bool style_type);
456         void parseSetFocus(const std::string &element);
457         void parseModel(parserData *data, const std::string &element);
458
459         void tryClose();
460
461         void showTooltip(const std::wstring &text, const irr::video::SColor &color,
462                 const irr::video::SColor &bgcolor);
463
464         /**
465          * In formspec version < 2 the elements were not ordered properly. Some element
466          * types were drawn before others.
467          * This function sorts the elements in the old order for backwards compatibility.
468          */
469         void legacySortElements(core::list<IGUIElement *>::Iterator from);
470
471         int m_btn_height;
472         gui::IGUIFont *m_font = nullptr;
473 };
474
475 class FormspecFormSource: public IFormSource
476 {
477 public:
478         FormspecFormSource(const std::string &formspec):
479                 m_formspec(formspec)
480         {
481         }
482
483         ~FormspecFormSource() = default;
484
485         void setForm(const std::string &formspec)
486         {
487                 m_formspec = formspec;
488         }
489
490         const std::string &getForm() const
491         {
492                 return m_formspec;
493         }
494
495         std::string m_formspec;
496 };