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