]> git.lizzy.rs Git - dragonfireclient.git/blob - src/gui/guiFormSpecMenu.h
guiFormspecMenu: Allow fraction values for container[] (#7497)
[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
25 #include "irrlichttypes_extrabloated.h"
26 #include "inventorymanager.h"
27 #include "modalMenu.h"
28 #include "guiTable.h"
29 #include "network/networkprotocol.h"
30 #include "client/joystick_controller.h"
31 #include "util/string.h"
32 #include "util/enriched_string.h"
33
34 class InventoryManager;
35 class ISimpleTextureSource;
36 class Client;
37
38 typedef enum {
39         f_Button,
40         f_Table,
41         f_TabHeader,
42         f_CheckBox,
43         f_DropDown,
44         f_ScrollBar,
45         f_Unknown
46 } FormspecFieldType;
47
48 typedef enum {
49         quit_mode_no,
50         quit_mode_accept,
51         quit_mode_cancel
52 } FormspecQuitMode;
53
54 struct TextDest
55 {
56         virtual ~TextDest() = default;
57
58         // This is deprecated I guess? -celeron55
59         virtual void gotText(const std::wstring &text) {}
60         virtual void gotText(const StringMap &fields) = 0;
61
62         std::string m_formname;
63 };
64
65 class IFormSource
66 {
67 public:
68         virtual ~IFormSource() = default;
69         virtual const std::string &getForm() const = 0;
70         // Fill in variables in field text
71         virtual std::string resolveText(const std::string &str) { return str; }
72 };
73
74 class GUIFormSpecMenu : public GUIModalMenu
75 {
76         struct ItemSpec
77         {
78                 ItemSpec() = default;
79
80                 ItemSpec(const InventoryLocation &a_inventoryloc,
81                                 const std::string &a_listname,
82                                 s32 a_i) :
83                         inventoryloc(a_inventoryloc),
84                         listname(a_listname),
85                         i(a_i)
86                 {
87                 }
88
89                 bool isValid() const { return i != -1; }
90
91                 InventoryLocation inventoryloc;
92                 std::string listname;
93                 s32 i = -1;
94         };
95
96         struct ListDrawSpec
97         {
98                 ListDrawSpec() = default;
99
100                 ListDrawSpec(const InventoryLocation &a_inventoryloc,
101                                 const std::string &a_listname,
102                                 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
103                         inventoryloc(a_inventoryloc),
104                         listname(a_listname),
105                         pos(a_pos),
106                         geom(a_geom),
107                         start_item_i(a_start_item_i)
108                 {
109                 }
110
111                 InventoryLocation inventoryloc;
112                 std::string listname;
113                 v2s32 pos;
114                 v2s32 geom;
115                 s32 start_item_i;
116         };
117
118         struct ListRingSpec
119         {
120                 ListRingSpec() = default;
121
122                 ListRingSpec(const InventoryLocation &a_inventoryloc,
123                                 const std::string &a_listname):
124                         inventoryloc(a_inventoryloc),
125                         listname(a_listname)
126                 {
127                 }
128
129                 InventoryLocation inventoryloc;
130                 std::string listname;
131         };
132
133         struct ImageDrawSpec
134         {
135                 ImageDrawSpec():
136                         parent_button(NULL),
137                         clip(false)
138                 {
139                 }
140
141                 ImageDrawSpec(const std::string &a_name,
142                                 const std::string &a_item_name,
143                                 gui::IGUIButton *a_parent_button,
144                                 const v2s32 &a_pos, const v2s32 &a_geom):
145                         name(a_name),
146                         item_name(a_item_name),
147                         parent_button(a_parent_button),
148                         pos(a_pos),
149                         geom(a_geom),
150                         scale(true),
151                         clip(false)
152                 {
153                 }
154
155                 ImageDrawSpec(const std::string &a_name,
156                                 const std::string &a_item_name,
157                                 const v2s32 &a_pos, const v2s32 &a_geom):
158                         name(a_name),
159                         item_name(a_item_name),
160                         parent_button(NULL),
161                         pos(a_pos),
162                         geom(a_geom),
163                         scale(true),
164                         clip(false)
165                 {
166                 }
167
168                 ImageDrawSpec(const std::string &a_name,
169                                 const v2s32 &a_pos, const v2s32 &a_geom, bool clip=false):
170                         name(a_name),
171                         parent_button(NULL),
172                         pos(a_pos),
173                         geom(a_geom),
174                         scale(true),
175                         clip(clip)
176                 {
177                 }
178
179                 ImageDrawSpec(const std::string &a_name,
180                                 const v2s32 &a_pos):
181                         name(a_name),
182                         parent_button(NULL),
183                         pos(a_pos),
184                         scale(false),
185                         clip(false)
186                 {
187                 }
188
189                 std::string name;
190                 std::string item_name;
191                 gui::IGUIButton *parent_button;
192                 v2s32 pos;
193                 v2s32 geom;
194                 bool scale;
195                 bool clip;
196         };
197
198         struct FieldSpec
199         {
200                 FieldSpec() = default;
201
202                 FieldSpec(const std::string &name, const std::wstring &label,
203                                 const std::wstring &default_text, int id) :
204                         fname(name),
205                         flabel(label),
206                         fdefault(unescape_enriched(translate_string(default_text))),
207                         fid(id),
208                         send(false),
209                         ftype(f_Unknown),
210                         is_exit(false)
211                 {
212                 }
213
214                 std::string fname;
215                 std::wstring flabel;
216                 std::wstring fdefault;
217                 int fid;
218                 bool send;
219                 FormspecFieldType ftype;
220                 bool is_exit;
221                 core::rect<s32> rect;
222         };
223
224         struct BoxDrawSpec
225         {
226                 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom, irr::video::SColor a_color):
227                         pos(a_pos),
228                         geom(a_geom),
229                         color(a_color)
230                 {
231                 }
232                 v2s32 pos;
233                 v2s32 geom;
234                 irr::video::SColor color;
235         };
236
237         struct TooltipSpec
238         {
239                 TooltipSpec() = default;
240                 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
241                                 irr::video::SColor a_color):
242                         tooltip(translate_string(a_tooltip)),
243                         bgcolor(a_bgcolor),
244                         color(a_color)
245                 {
246                 }
247
248                 std::wstring tooltip;
249                 irr::video::SColor bgcolor;
250                 irr::video::SColor color;
251         };
252
253         struct StaticTextSpec
254         {
255                 StaticTextSpec():
256                         parent_button(NULL)
257                 {
258                 }
259
260                 StaticTextSpec(const std::wstring &a_text,
261                                 const core::rect<s32> &a_rect):
262                         text(a_text),
263                         rect(a_rect),
264                         parent_button(NULL)
265                 {
266                 }
267
268                 StaticTextSpec(const std::wstring &a_text,
269                                 const core::rect<s32> &a_rect,
270                                 gui::IGUIButton *a_parent_button):
271                         text(a_text),
272                         rect(a_rect),
273                         parent_button(a_parent_button)
274                 {
275                 }
276
277                 std::wstring text;
278                 core::rect<s32> rect;
279                 gui::IGUIButton *parent_button;
280         };
281
282 public:
283         GUIFormSpecMenu(JoystickController *joystick,
284                         gui::IGUIElement* parent, s32 id,
285                         IMenuManager *menumgr,
286                         Client *client,
287                         ISimpleTextureSource *tsrc,
288                         IFormSource* fs_src,
289                         TextDest* txt_dst,
290                         std::string formspecPrepend,
291                         bool remap_dbl_click = true);
292
293         ~GUIFormSpecMenu();
294
295         static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
296                 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
297                 const std::string &formspecPrepend);
298
299         void setFormSpec(const std::string &formspec_string,
300                         const InventoryLocation &current_inventory_location)
301         {
302                 m_formspec_string = formspec_string;
303                 m_current_inventory_location = current_inventory_location;
304                 regenerateGui(m_screensize_old);
305         }
306
307         void setFormspecPrepend(const std::string &formspecPrepend)
308         {
309                 m_formspec_prepend = formspecPrepend;
310         }
311
312         // form_src is deleted by this GUIFormSpecMenu
313         void setFormSource(IFormSource *form_src)
314         {
315                 delete m_form_src;
316                 m_form_src = form_src;
317         }
318
319         // text_dst is deleted by this GUIFormSpecMenu
320         void setTextDest(TextDest *text_dst)
321         {
322                 delete m_text_dst;
323                 m_text_dst = text_dst;
324         }
325
326         void allowClose(bool value)
327         {
328                 m_allowclose = value;
329         }
330
331         void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
332         {
333                 m_lock = lock;
334                 m_lockscreensize = basescreensize;
335         }
336
337         void removeChildren();
338         void setInitialFocus();
339
340         void setFocus(const std::string &elementname)
341         {
342                 m_focused_element = elementname;
343         }
344
345         /*
346                 Remove and re-add (or reposition) stuff
347         */
348         void regenerateGui(v2u32 screensize);
349
350         ItemSpec getItemAtPos(v2s32 p) const;
351         void drawList(const ListDrawSpec &s, int layer, bool &item_hovered);
352         void drawSelectedItem();
353         void drawMenu();
354         void updateSelectedItem();
355         ItemStack verifySelectedItem();
356
357         void acceptInput(FormspecQuitMode quitmode);
358         bool preprocessEvent(const SEvent& event);
359         bool OnEvent(const SEvent& event);
360         bool doPause;
361         bool pausesGame() { return doPause; }
362
363         GUITable* getTable(const std::string &tablename);
364         std::vector<std::string>* getDropDownValues(const std::string &name);
365
366 #ifdef __ANDROID__
367         bool getAndroidUIInput();
368 #endif
369
370 protected:
371         v2s32 getBasePos() const
372         {
373                         return padding + offset + AbsoluteRect.UpperLeftCorner;
374         }
375         v2s32 getElementBasePos(bool absolute,
376                         const std::vector<std::string> *v_pos);
377
378         v2s32 padding;
379         v2f32 spacing;
380         v2s32 imgsize;
381         v2s32 offset;
382         v2f32 pos_offset;
383         std::stack<v2f32> container_stack;
384
385         InventoryManager *m_invmgr;
386         ISimpleTextureSource *m_tsrc;
387         Client *m_client;
388
389         std::string m_formspec_string;
390         std::string m_formspec_prepend;
391         InventoryLocation m_current_inventory_location;
392
393         std::vector<ListDrawSpec> m_inventorylists;
394         std::vector<ListRingSpec> m_inventory_rings;
395         std::vector<ImageDrawSpec> m_backgrounds;
396         std::vector<ImageDrawSpec> m_images;
397         std::vector<ImageDrawSpec> m_itemimages;
398         std::vector<BoxDrawSpec> m_boxes;
399         std::unordered_map<std::string, bool> field_close_on_enter;
400         std::vector<FieldSpec> m_fields;
401         std::vector<StaticTextSpec> m_static_texts;
402         std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
403         std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
404         std::map<std::string, TooltipSpec> m_tooltips;
405         std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
406         std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
407
408         ItemSpec *m_selected_item = nullptr;
409         u16 m_selected_amount = 0;
410         bool m_selected_dragging = false;
411         ItemStack m_selected_swap;
412
413         v2s32 m_pointer;
414         v2s32 m_old_pointer;  // Mouse position after previous mouse event
415         gui::IGUIStaticText *m_tooltip_element = nullptr;
416
417         u64 m_tooltip_show_delay;
418         bool m_tooltip_append_itemname;
419         u64 m_hovered_time = 0;
420         s32 m_old_tooltip_id = -1;
421
422         bool m_auto_place = false;
423
424         bool m_allowclose = true;
425         bool m_lock = false;
426         v2u32 m_lockscreensize;
427
428         bool m_bgfullscreen;
429         bool m_slotborder;
430         video::SColor m_bgcolor;
431         video::SColor m_fullscreen_bgcolor;
432         video::SColor m_slotbg_n;
433         video::SColor m_slotbg_h;
434         video::SColor m_slotbordercolor;
435         video::SColor m_default_tooltip_bgcolor;
436         video::SColor m_default_tooltip_color;
437
438 private:
439         IFormSource        *m_form_src;
440         TextDest           *m_text_dst;
441         u32                 m_formspec_version = 0;
442         std::string         m_focused_element = "";
443         JoystickController *m_joystick;
444
445         typedef struct {
446                 bool explicit_size;
447                 v2f invsize;
448                 v2s32 size;
449                 v2f32 offset;
450                 v2f32 anchor;
451                 core::rect<s32> rect;
452                 v2s32 basepos;
453                 v2u32 screensize;
454                 std::string focused_fieldname;
455                 GUITable::TableOptions table_options;
456                 GUITable::TableColumns table_columns;
457                 // used to restore table selection/scroll/treeview state
458                 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
459         } parserData;
460
461         typedef struct {
462                 bool key_up;
463                 bool key_down;
464                 bool key_enter;
465                 bool key_escape;
466         } fs_key_pendig;
467
468         fs_key_pendig current_keys_pending;
469         std::string current_field_enter_pending = "";
470
471         void parseElement(parserData* data, const std::string &element);
472
473         void parseSize(parserData* data, const std::string &element);
474         void parseContainer(parserData* data, const std::string &element);
475         void parseContainerEnd(parserData* data);
476         void parseList(parserData* data, const std::string &element);
477         void parseListRing(parserData* data, const std::string &element);
478         void parseCheckbox(parserData* data, const std::string &element);
479         void parseImage(parserData* data, const std::string &element);
480         void parseItemImage(parserData* data, const std::string &element);
481         void parseButton(parserData* data, const std::string &element,
482                         const std::string &typ);
483         void parseBackground(parserData* data, const std::string &element);
484         void parseTableOptions(parserData* data, const std::string &element);
485         void parseTableColumns(parserData* data, const std::string &element);
486         void parseTable(parserData* data, const std::string &element);
487         void parseTextList(parserData* data, const std::string &element);
488         void parseDropDown(parserData* data, const std::string &element);
489         void parseFieldCloseOnEnter(parserData *data, const std::string &element);
490         void parsePwdField(parserData* data, const std::string &element);
491         void parseField(parserData* data, const std::string &element, const std::string &type);
492         void createTextField(parserData *data, FieldSpec &spec,
493                 core::rect<s32> &rect, bool is_multiline);
494         void parseSimpleField(parserData* data,std::vector<std::string> &parts);
495         void parseTextArea(parserData* data,std::vector<std::string>& parts,
496                         const std::string &type);
497         void parseLabel(parserData* data, const std::string &element);
498         void parseVertLabel(parserData* data, const std::string &element);
499         void parseImageButton(parserData* data, const std::string &element,
500                         const std::string &type);
501         void parseItemImageButton(parserData* data, const std::string &element);
502         void parseTabHeader(parserData* data, const std::string &element);
503         void parseBox(parserData* data, const std::string &element);
504         void parseBackgroundColor(parserData* data, const std::string &element);
505         void parseListColors(parserData* data, const std::string &element);
506         void parseTooltip(parserData* data, const std::string &element);
507         bool parseVersionDirect(const std::string &data);
508         bool parseSizeDirect(parserData* data, const std::string &element);
509         void parseScrollBar(parserData* data, const std::string &element);
510         bool parsePositionDirect(parserData *data, const std::string &element);
511         void parsePosition(parserData *data, const std::string &element);
512         bool parseAnchorDirect(parserData *data, const std::string &element);
513         void parseAnchor(parserData *data, const std::string &element);
514
515         void tryClose();
516
517         void showTooltip(const std::wstring &text, const irr::video::SColor &color,
518                 const irr::video::SColor &bgcolor);
519
520         /**
521          * check if event is part of a double click
522          * @param event event to evaluate
523          * @return true/false if a doubleclick was detected
524          */
525         bool DoubleClickDetection(const SEvent event);
526
527         struct clickpos
528         {
529                 v2s32 pos;
530                 s64 time;
531         };
532         clickpos m_doubleclickdetect[2];
533
534         int m_btn_height;
535         gui::IGUIFont *m_font = nullptr;
536
537         std::wstring getLabelByID(s32 id);
538         std::string getNameByID(s32 id);
539 #ifdef __ANDROID__
540         v2s32 m_down_pos;
541         std::string m_JavaDialogFieldName;
542 #endif
543
544         /* If true, remap a double-click (or double-tap) action to ESC. This is so
545          * that, for example, Android users can double-tap to close a formspec.
546         *
547          * This value can (currently) only be set by the class constructor
548          * and the default value for the setting is true.
549          */
550         bool m_remap_dbl_click;
551
552 };
553
554 class FormspecFormSource: public IFormSource
555 {
556 public:
557         FormspecFormSource(const std::string &formspec):
558                 m_formspec(formspec)
559         {
560         }
561
562         ~FormspecFormSource() = default;
563
564         void setForm(const std::string &formspec)
565         {
566                 m_formspec = FORMSPEC_VERSION_STRING + formspec;
567         }
568
569         const std::string &getForm() const
570         {
571                 return m_formspec;
572         }
573
574         std::string m_formspec;
575 };