]> git.lizzy.rs Git - minetest.git/blob - src/guiFormSpecMenu.h
Fix background formspec elements from interfering with each other
[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 "client/joystick_controller.h"
33 #include "util/string.h"
34 #include "util/enriched_string.h"
35
36 class IGameDef;
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                         close_on_enter(false),
215                         ftype(f_Unknown),
216                         is_exit(false)
217                 {
218                         //flabel = unescape_enriched(label);
219                         fdefault = unescape_enriched(default_text);
220                 }
221                 std::string fname;
222                 std::wstring flabel;
223                 std::wstring fdefault;
224                 int fid;
225                 bool send;
226                 bool close_on_enter; // used by text fields
227                 FormspecFieldType ftype;
228                 bool is_exit;
229                 core::rect<s32> rect;
230         };
231
232         struct BoxDrawSpec {
233                 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
234                         pos(a_pos),
235                         geom(a_geom),
236                         color(a_color)
237                 {
238                 }
239                 v2s32 pos;
240                 v2s32 geom;
241                 irr::video::SColor color;
242         };
243
244         struct TooltipSpec {
245                 TooltipSpec()
246                 {
247                 }
248                 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
249                                 irr::video::SColor a_color):
250                         bgcolor(a_bgcolor),
251                         color(a_color)
252                 {
253                         //tooltip = unescape_enriched(utf8_to_wide(a_tooltip));
254                         tooltip = utf8_to_wide(a_tooltip);
255                 }
256                 std::wstring tooltip;
257                 irr::video::SColor bgcolor;
258                 irr::video::SColor color;
259         };
260
261         struct StaticTextSpec {
262                 StaticTextSpec():
263                         parent_button(NULL)
264                 {
265                 }
266                 StaticTextSpec(const std::wstring &a_text,
267                                 const core::rect<s32> &a_rect):
268                         rect(a_rect),
269                         parent_button(NULL)
270                 {
271                         //text = unescape_enriched(a_text);
272                         text = a_text;
273                 }
274                 StaticTextSpec(const std::wstring &a_text,
275                                 const core::rect<s32> &a_rect,
276                                 gui::IGUIButton *a_parent_button):
277                         rect(a_rect),
278                         parent_button(a_parent_button)
279                 {
280                         //text = unescape_enriched(a_text);
281                         text = a_text;
282                 }
283                 std::wstring text;
284                 core::rect<s32> rect;
285                 gui::IGUIButton *parent_button;
286         };
287
288 public:
289         GUIFormSpecMenu(irr::IrrlichtDevice* dev,
290                         JoystickController *joystick,
291                         gui::IGUIElement* parent, s32 id,
292                         IMenuManager *menumgr,
293                         InventoryManager *invmgr,
294                         IGameDef *gamedef,
295                         ISimpleTextureSource *tsrc,
296                         IFormSource* fs_src,
297                         TextDest* txt_dst,
298                         Client* client,
299                         bool remap_dbl_click = true);
300
301         ~GUIFormSpecMenu();
302
303         void setFormSpec(const std::string &formspec_string,
304                         InventoryLocation current_inventory_location)
305         {
306                 m_formspec_string = formspec_string;
307                 m_current_inventory_location = current_inventory_location;
308                 regenerateGui(m_screensize_old);
309         }
310
311         // form_src is deleted by this GUIFormSpecMenu
312         void setFormSource(IFormSource *form_src)
313         {
314                 if (m_form_src != NULL) {
315                         delete m_form_src;
316                 }
317                 m_form_src = form_src;
318         }
319
320         // text_dst is deleted by this GUIFormSpecMenu
321         void setTextDest(TextDest *text_dst)
322         {
323                 if (m_text_dst != NULL) {
324                         delete m_text_dst;
325                 }
326                 m_text_dst = text_dst;
327         }
328
329         void allowClose(bool value)
330         {
331                 m_allowclose = value;
332         }
333
334         void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
335         {
336                 m_lock = lock;
337                 m_lockscreensize = basescreensize;
338         }
339
340         void removeChildren();
341         void setInitialFocus();
342
343         void setFocus(std::string &elementname)
344         {
345                 m_focused_element = elementname;
346         }
347
348         /*
349                 Remove and re-add (or reposition) stuff
350         */
351         void regenerateGui(v2u32 screensize);
352
353         ItemSpec getItemAtPos(v2s32 p) const;
354         void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
355         void drawSelectedItem();
356         void drawMenu();
357         void updateSelectedItem();
358         ItemStack verifySelectedItem();
359
360         void acceptInput(FormspecQuitMode quitmode);
361         bool preprocessEvent(const SEvent& event);
362         bool OnEvent(const SEvent& event);
363         bool doPause;
364         bool pausesGame() { return doPause; }
365
366         GUITable* getTable(const std::string &tablename);
367         std::vector<std::string>* getDropDownValues(const std::string &name);
368
369 #ifdef __ANDROID__
370         bool getAndroidUIInput();
371 #endif
372
373 protected:
374         v2s32 getBasePos() const
375         {
376                         return padding + offset + AbsoluteRect.UpperLeftCorner;
377         }
378
379         v2s32 padding;
380         v2s32 spacing;
381         v2s32 imgsize;
382         v2s32 offset;
383
384         irr::IrrlichtDevice* m_device;
385         InventoryManager *m_invmgr;
386         IGameDef *m_gamedef;
387         ISimpleTextureSource *m_tsrc;
388         Client *m_client;
389
390         std::string m_formspec_string;
391         InventoryLocation m_current_inventory_location;
392
393
394         std::vector<ListDrawSpec> m_inventorylists;
395         std::vector<ListRingSpec> m_inventory_rings;
396         std::vector<ImageDrawSpec> m_backgrounds;
397         std::vector<ImageDrawSpec> m_images;
398         std::vector<ImageDrawSpec> m_itemimages;
399         std::vector<BoxDrawSpec> m_boxes;
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;
409         f32 m_timer1;
410         f32 m_timer2;
411         u32 m_selected_amount;
412         bool m_selected_dragging;
413
414         // WARNING: BLACK MAGIC
415         // Used to guess and keep up with some special things the server can do.
416         // If name is "", no guess exists.
417         ItemStack m_selected_content_guess;
418         InventoryLocation m_selected_content_guess_inventory;
419
420         v2s32 m_pointer;
421         v2s32 m_old_pointer;  // Mouse position after previous mouse event
422         gui::IGUIStaticText *m_tooltip_element;
423
424         u32 m_tooltip_show_delay;
425         s32 m_hovered_time;
426         s32 m_old_tooltip_id;
427         std::wstring m_old_tooltip;
428
429         bool m_rmouse_auto_place;
430
431         bool m_allowclose;
432         bool m_lock;
433         v2u32 m_lockscreensize;
434
435         bool m_bgfullscreen;
436         bool m_slotborder;
437         video::SColor m_bgcolor;
438         video::SColor m_slotbg_n;
439         video::SColor m_slotbg_h;
440         video::SColor m_slotbordercolor;
441         video::SColor m_default_tooltip_bgcolor;
442         video::SColor m_default_tooltip_color;
443
444 private:
445         IFormSource        *m_form_src;
446         TextDest           *m_text_dst;
447         unsigned int        m_formspec_version;
448         std::string         m_focused_element;
449         JoystickController *m_joystick;
450
451         typedef struct {
452                 bool explicit_size;
453                 v2f invsize;
454                 v2s32 size;
455                 core::rect<s32> rect;
456                 v2s32 basepos;
457                 v2u32 screensize;
458                 std::string focused_fieldname;
459                 GUITable::TableOptions table_options;
460                 GUITable::TableColumns table_columns;
461                 // used to restore table selection/scroll/treeview state
462                 std::map<std::string, GUITable::DynamicData> table_dyndata;
463         } parserData;
464
465         typedef struct {
466                 bool key_up;
467                 bool key_down;
468                 bool key_enter;
469                 bool key_escape;
470         } fs_key_pendig;
471
472         fs_key_pendig current_keys_pending;
473         std::string current_field_enter_pending;
474
475         void parseElement(parserData* data,std::string element);
476
477         void parseSize(parserData* data,std::string element);
478         void parseList(parserData* data,std::string element);
479         void parseListRing(parserData* data,std::string element);
480         void parseCheckbox(parserData* data,std::string element);
481         void parseImage(parserData* data,std::string element);
482         void parseItemImage(parserData* data,std::string element);
483         void parseButton(parserData* data,std::string element,std::string typ);
484         void parseBackground(parserData* data,std::string element);
485         void parseTableOptions(parserData* data,std::string element);
486         void parseTableColumns(parserData* data,std::string element);
487         void parseTable(parserData* data,std::string element);
488         void parseTextList(parserData* data,std::string element);
489         void parseDropDown(parserData* data,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
508         void tryClose();
509
510         /**
511          * check if event is part of a double click
512          * @param event event to evaluate
513          * @return true/false if a doubleclick was detected
514          */
515         bool DoubleClickDetection(const SEvent event);
516
517         struct clickpos
518         {
519                 v2s32 pos;
520                 s32 time;
521         };
522         clickpos m_doubleclickdetect[2];
523
524         int m_btn_height;
525         gui::IGUIFont *m_font;
526
527         std::wstring getLabelByID(s32 id);
528         std::string getNameByID(s32 id);
529 #ifdef __ANDROID__
530         v2s32 m_down_pos;
531         std::string m_JavaDialogFieldName;
532 #endif
533
534         /* If true, remap a double-click (or double-tap) action to ESC. This is so
535          * that, for example, Android users can double-tap to close a formspec.
536         *
537          * This value can (currently) only be set by the class constructor
538          * and the default value for the setting is true.
539          */
540         bool m_remap_dbl_click;
541
542 };
543
544 class FormspecFormSource: public IFormSource
545 {
546 public:
547         FormspecFormSource(std::string formspec)
548         {
549                 m_formspec = formspec;
550         }
551
552         ~FormspecFormSource()
553         {}
554
555         void setForm(std::string formspec) {
556                 m_formspec = FORMSPEC_VERSION_STRING + formspec;
557         }
558
559         std::string getForm()
560         {
561                 return m_formspec;
562         }
563
564         std::string m_formspec;
565 };
566
567 #endif