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