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