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