]> git.lizzy.rs Git - minetest.git/blob - src/guiFormSpecMenu.h
d06aa6cf5c319d468a12f0322d5458d7fd501ff3
[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                 {
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                 {
160                 }
161                 ImageDrawSpec(const std::string &a_name,
162                                 const std::string &a_item_name,
163                                 const v2s32 &a_pos, const v2s32 &a_geom):
164                         name(a_name),
165                         item_name(a_item_name),
166                         parent_button(NULL),
167                         pos(a_pos),
168                         geom(a_geom),
169                         scale(true)
170                 {
171                 }
172                 ImageDrawSpec(const std::string &a_name,
173                                 const v2s32 &a_pos, const v2s32 &a_geom):
174                         name(a_name),
175                         parent_button(NULL),
176                         pos(a_pos),
177                         geom(a_geom),
178                         scale(true)
179                 {
180                 }
181                 ImageDrawSpec(const std::string &a_name,
182                                 const v2s32 &a_pos):
183                         name(a_name),
184                         parent_button(NULL),
185                         pos(a_pos),
186                         scale(false)
187                 {
188                 }
189                 std::string name;
190                 std::string item_name;
191                 gui::IGUIButton *parent_button;
192                 v2s32 pos;
193                 v2s32 geom;
194                 bool scale;
195         };
196
197         struct FieldSpec
198         {
199                 FieldSpec()
200                 {
201                 }
202                 FieldSpec(const std::string &name, const std::wstring &label,
203                                 const std::wstring &default_text, int id) :
204                         fname(name),
205                         flabel(label),
206                         fid(id),
207                         send(false),
208                         close_on_enter(false),
209                         ftype(f_Unknown),
210                         is_exit(false)
211                 {
212                         //flabel = unescape_enriched(label);
213                         fdefault = unescape_enriched(default_text);
214                 }
215                 std::string fname;
216                 std::wstring flabel;
217                 std::wstring fdefault;
218                 int fid;
219                 bool send;
220                 bool close_on_enter; // used by text fields
221                 FormspecFieldType ftype;
222                 bool is_exit;
223                 core::rect<s32> rect;
224         };
225
226         struct BoxDrawSpec {
227                 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
228                         pos(a_pos),
229                         geom(a_geom),
230                         color(a_color)
231                 {
232                 }
233                 v2s32 pos;
234                 v2s32 geom;
235                 irr::video::SColor color;
236         };
237
238         struct TooltipSpec {
239                 TooltipSpec()
240                 {
241                 }
242                 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
243                                 irr::video::SColor a_color):
244                         bgcolor(a_bgcolor),
245                         color(a_color)
246                 {
247                         //tooltip = unescape_enriched(utf8_to_wide(a_tooltip));
248                         tooltip = utf8_to_wide(a_tooltip);
249                 }
250                 std::wstring tooltip;
251                 irr::video::SColor bgcolor;
252                 irr::video::SColor color;
253         };
254
255         struct StaticTextSpec {
256                 StaticTextSpec():
257                         parent_button(NULL)
258                 {
259                 }
260                 StaticTextSpec(const std::wstring &a_text,
261                                 const core::rect<s32> &a_rect):
262                         rect(a_rect),
263                         parent_button(NULL)
264                 {
265                         //text = unescape_enriched(a_text);
266                         text = a_text;
267                 }
268                 StaticTextSpec(const std::wstring &a_text,
269                                 const core::rect<s32> &a_rect,
270                                 gui::IGUIButton *a_parent_button):
271                         rect(a_rect),
272                         parent_button(a_parent_button)
273                 {
274                         //text = unescape_enriched(a_text);
275                         text = a_text;
276                 }
277                 std::wstring text;
278                 core::rect<s32> rect;
279                 gui::IGUIButton *parent_button;
280         };
281
282 public:
283         GUIFormSpecMenu(irr::IrrlichtDevice* dev,
284                         JoystickController *joystick,
285                         gui::IGUIElement* parent, s32 id,
286                         IMenuManager *menumgr,
287                         InventoryManager *invmgr,
288                         IGameDef *gamedef,
289                         ISimpleTextureSource *tsrc,
290                         IFormSource* fs_src,
291                         TextDest* txt_dst,
292                         Client* client,
293                         bool remap_dbl_click = true);
294
295         ~GUIFormSpecMenu();
296
297         void setFormSpec(const std::string &formspec_string,
298                         InventoryLocation current_inventory_location)
299         {
300                 m_formspec_string = formspec_string;
301                 m_current_inventory_location = current_inventory_location;
302                 regenerateGui(m_screensize_old);
303         }
304
305         // form_src is deleted by this GUIFormSpecMenu
306         void setFormSource(IFormSource *form_src)
307         {
308                 if (m_form_src != NULL) {
309                         delete m_form_src;
310                 }
311                 m_form_src = form_src;
312         }
313
314         // text_dst is deleted by this GUIFormSpecMenu
315         void setTextDest(TextDest *text_dst)
316         {
317                 if (m_text_dst != NULL) {
318                         delete m_text_dst;
319                 }
320                 m_text_dst = text_dst;
321         }
322
323         void allowClose(bool value)
324         {
325                 m_allowclose = value;
326         }
327
328         void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
329         {
330                 m_lock = lock;
331                 m_lockscreensize = basescreensize;
332         }
333
334         void removeChildren();
335         void setInitialFocus();
336
337         void setFocus(std::string &elementname)
338         {
339                 m_focused_element = elementname;
340         }
341
342         /*
343                 Remove and re-add (or reposition) stuff
344         */
345         void regenerateGui(v2u32 screensize);
346
347         ItemSpec getItemAtPos(v2s32 p) const;
348         void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
349         void drawSelectedItem();
350         void drawMenu();
351         void updateSelectedItem();
352         ItemStack verifySelectedItem();
353
354         void acceptInput(FormspecQuitMode quitmode);
355         bool preprocessEvent(const SEvent& event);
356         bool OnEvent(const SEvent& event);
357         bool doPause;
358         bool pausesGame() { return doPause; }
359
360         GUITable* getTable(const std::string &tablename);
361         std::vector<std::string>* getDropDownValues(const std::string &name);
362
363 #ifdef __ANDROID__
364         bool getAndroidUIInput();
365 #endif
366
367 protected:
368         v2s32 getBasePos() const
369         {
370                         return padding + offset + AbsoluteRect.UpperLeftCorner;
371         }
372
373         v2s32 padding;
374         v2s32 spacing;
375         v2s32 imgsize;
376         v2s32 offset;
377
378         irr::IrrlichtDevice* m_device;
379         InventoryManager *m_invmgr;
380         IGameDef *m_gamedef;
381         ISimpleTextureSource *m_tsrc;
382         Client *m_client;
383
384         std::string m_formspec_string;
385         InventoryLocation m_current_inventory_location;
386
387
388         std::vector<ListDrawSpec> m_inventorylists;
389         std::vector<ListRingSpec> m_inventory_rings;
390         std::vector<ImageDrawSpec> m_backgrounds;
391         std::vector<ImageDrawSpec> m_images;
392         std::vector<ImageDrawSpec> m_itemimages;
393         std::vector<BoxDrawSpec> m_boxes;
394         std::vector<FieldSpec> m_fields;
395         std::vector<StaticTextSpec> m_static_texts;
396         std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
397         std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
398         std::map<std::string, TooltipSpec> m_tooltips;
399         std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
400         std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
401
402         ItemSpec *m_selected_item;
403         f32 m_timer1;
404         f32 m_timer2;
405         u32 m_selected_amount;
406         bool m_selected_dragging;
407
408         // WARNING: BLACK MAGIC
409         // Used to guess and keep up with some special things the server can do.
410         // If name is "", no guess exists.
411         ItemStack m_selected_content_guess;
412         InventoryLocation m_selected_content_guess_inventory;
413
414         v2s32 m_pointer;
415         v2s32 m_old_pointer;  // Mouse position after previous mouse event
416         gui::IGUIStaticText *m_tooltip_element;
417
418         u32 m_tooltip_show_delay;
419         s32 m_hovered_time;
420         s32 m_old_tooltip_id;
421         std::wstring m_old_tooltip;
422
423         bool m_rmouse_auto_place;
424
425         bool m_allowclose;
426         bool m_lock;
427         v2u32 m_lockscreensize;
428
429         bool m_bgfullscreen;
430         bool m_slotborder;
431         bool m_clipbackground;
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                 core::rect<s32> rect;
451                 v2s32 basepos;
452                 v2u32 screensize;
453                 std::string focused_fieldname;
454                 GUITable::TableOptions table_options;
455                 GUITable::TableColumns table_columns;
456                 // used to restore table selection/scroll/treeview state
457                 std::map<std::string, GUITable::DynamicData> table_dyndata;
458         } parserData;
459
460         typedef struct {
461                 bool key_up;
462                 bool key_down;
463                 bool key_enter;
464                 bool key_escape;
465         } fs_key_pendig;
466
467         fs_key_pendig current_keys_pending;
468         std::string current_field_enter_pending;
469
470         void parseElement(parserData* data,std::string element);
471
472         void parseSize(parserData* data,std::string element);
473         void parseList(parserData* data,std::string element);
474         void parseListRing(parserData* data,std::string element);
475         void parseCheckbox(parserData* data,std::string element);
476         void parseImage(parserData* data,std::string element);
477         void parseItemImage(parserData* data,std::string element);
478         void parseButton(parserData* data,std::string element,std::string typ);
479         void parseBackground(parserData* data,std::string element);
480         void parseTableOptions(parserData* data,std::string element);
481         void parseTableColumns(parserData* data,std::string element);
482         void parseTable(parserData* data,std::string element);
483         void parseTextList(parserData* data,std::string element);
484         void parseDropDown(parserData* data,std::string element);
485         void parsePwdField(parserData* data,std::string element);
486         void parseField(parserData* data,std::string element,std::string type);
487         void parseSimpleField(parserData* data,std::vector<std::string> &parts);
488         void parseTextArea(parserData* data,std::vector<std::string>& parts,
489                         std::string type);
490         void parseLabel(parserData* data,std::string element);
491         void parseVertLabel(parserData* data,std::string element);
492         void parseImageButton(parserData* data,std::string element,std::string type);
493         void parseItemImageButton(parserData* data,std::string element);
494         void parseTabHeader(parserData* data,std::string element);
495         void parseBox(parserData* data,std::string element);
496         void parseBackgroundColor(parserData* data,std::string element);
497         void parseListColors(parserData* data,std::string element);
498         void parseTooltip(parserData* data,std::string element);
499         bool parseVersionDirect(std::string data);
500         bool parseSizeDirect(parserData* data, std::string element);
501         void parseScrollBar(parserData* data, std::string element);
502
503         void tryClose();
504
505         /**
506          * check if event is part of a double click
507          * @param event event to evaluate
508          * @return true/false if a doubleclick was detected
509          */
510         bool DoubleClickDetection(const SEvent event);
511
512         struct clickpos
513         {
514                 v2s32 pos;
515                 s32 time;
516         };
517         clickpos m_doubleclickdetect[2];
518
519         int m_btn_height;
520         gui::IGUIFont *m_font;
521
522         std::wstring getLabelByID(s32 id);
523         std::string getNameByID(s32 id);
524 #ifdef __ANDROID__
525         v2s32 m_down_pos;
526         std::string m_JavaDialogFieldName;
527 #endif
528
529         /* If true, remap a double-click (or double-tap) action to ESC. This is so
530          * that, for example, Android users can double-tap to close a formspec.
531         *
532          * This value can (currently) only be set by the class constructor
533          * and the default value for the setting is true.
534          */
535         bool m_remap_dbl_click;
536
537 };
538
539 class FormspecFormSource: public IFormSource
540 {
541 public:
542         FormspecFormSource(std::string formspec)
543         {
544                 m_formspec = formspec;
545         }
546
547         ~FormspecFormSource()
548         {}
549
550         void setForm(std::string formspec) {
551                 m_formspec = FORMSPEC_VERSION_STRING + formspec;
552         }
553
554         std::string getForm()
555         {
556                 return m_formspec;
557         }
558
559         std::string m_formspec;
560 };
561
562 #endif