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