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