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