]> git.lizzy.rs Git - minetest.git/blob - src/guiFormSpecMenu.h
Shaders: Remove special handling for liquids. (#4670)
[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 "inventory.h"
29 #include "inventorymanager.h"
30 #include "modalMenu.h"
31 #include "guiTable.h"
32 #include "network/networkprotocol.h"
33 #include "client/joystick_controller.h"
34 #include "util/string.h"
35 #include "util/enriched_string.h"
36
37 class IGameDef;
38 class InventoryManager;
39 class ISimpleTextureSource;
40 class Client;
41
42 typedef enum {
43         f_Button,
44         f_Table,
45         f_TabHeader,
46         f_CheckBox,
47         f_DropDown,
48         f_ScrollBar,
49         f_Unknown
50 } FormspecFieldType;
51
52 typedef enum {
53         quit_mode_no,
54         quit_mode_accept,
55         quit_mode_cancel
56 } FormspecQuitMode;
57
58 struct TextDest
59 {
60         virtual ~TextDest() {};
61         // This is deprecated I guess? -celeron55
62         virtual void gotText(std::wstring text){}
63         virtual void gotText(const StringMap &fields) = 0;
64         virtual void setFormName(std::string formname)
65         { m_formname = formname;};
66
67         std::string m_formname;
68 };
69
70 class IFormSource
71 {
72 public:
73         virtual ~IFormSource(){}
74         virtual std::string getForm() = 0;
75         // Fill in variables in field text
76         virtual std::string resolveText(std::string str){ return str; }
77 };
78
79 class GUIFormSpecMenu : public GUIModalMenu
80 {
81         struct ItemSpec
82         {
83                 ItemSpec()
84                 {
85                         i = -1;
86                 }
87                 ItemSpec(const InventoryLocation &a_inventoryloc,
88                                 const std::string &a_listname,
89                                 s32 a_i)
90                 {
91                         inventoryloc = a_inventoryloc;
92                         listname = a_listname;
93                         i = a_i;
94                 }
95                 bool isValid() const
96                 {
97                         return i != -1;
98                 }
99
100                 InventoryLocation inventoryloc;
101                 std::string listname;
102                 s32 i;
103         };
104
105         struct ListDrawSpec
106         {
107                 ListDrawSpec()
108                 {
109                 }
110                 ListDrawSpec(const InventoryLocation &a_inventoryloc,
111                                 const std::string &a_listname,
112                                 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
113                         inventoryloc(a_inventoryloc),
114                         listname(a_listname),
115                         pos(a_pos),
116                         geom(a_geom),
117                         start_item_i(a_start_item_i)
118                 {
119                 }
120
121                 InventoryLocation inventoryloc;
122                 std::string listname;
123                 v2s32 pos;
124                 v2s32 geom;
125                 s32 start_item_i;
126         };
127
128         struct ListRingSpec
129         {
130                 ListRingSpec()
131                 {
132                 }
133                 ListRingSpec(const InventoryLocation &a_inventoryloc,
134                                 const std::string &a_listname):
135                         inventoryloc(a_inventoryloc),
136                         listname(a_listname)
137                 {
138                 }
139
140                 InventoryLocation inventoryloc;
141                 std::string listname;
142         };
143
144         struct ImageDrawSpec
145         {
146                 ImageDrawSpec():
147                         parent_button(NULL),
148                         clip(false)
149                 {}
150
151                 ImageDrawSpec(const std::string &a_name,
152                                 const std::string &a_item_name,
153                                 gui::IGUIButton *a_parent_button,
154                                 const v2s32 &a_pos, const v2s32 &a_geom):
155                         name(a_name),
156                         item_name(a_item_name),
157                         parent_button(a_parent_button),
158                         pos(a_pos),
159                         geom(a_geom),
160                         scale(true),
161                         clip(false)
162                 {}
163
164                 ImageDrawSpec(const std::string &a_name,
165                                 const std::string &a_item_name,
166                                 const v2s32 &a_pos, const v2s32 &a_geom):
167                         name(a_name),
168                         item_name(a_item_name),
169                         parent_button(NULL),
170                         pos(a_pos),
171                         geom(a_geom),
172                         scale(true),
173                         clip(false)
174                 {}
175
176                 ImageDrawSpec(const std::string &a_name,
177                                 const v2s32 &a_pos, const v2s32 &a_geom, bool clip=false):
178                         name(a_name),
179                         parent_button(NULL),
180                         pos(a_pos),
181                         geom(a_geom),
182                         scale(true),
183                         clip(clip)
184                 {}
185
186                 ImageDrawSpec(const std::string &a_name,
187                                 const v2s32 &a_pos):
188                         name(a_name),
189                         parent_button(NULL),
190                         pos(a_pos),
191                         scale(false),
192                         clip(false)
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                         fid(id),
214                         send(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                 FormspecFieldType ftype;
227                 bool is_exit;
228                 core::rect<s32> rect;
229         };
230
231         struct BoxDrawSpec {
232                 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
233                         pos(a_pos),
234                         geom(a_geom),
235                         color(a_color)
236                 {
237                 }
238                 v2s32 pos;
239                 v2s32 geom;
240                 irr::video::SColor color;
241         };
242
243         struct TooltipSpec {
244                 TooltipSpec()
245                 {
246                 }
247                 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
248                                 irr::video::SColor a_color):
249                         bgcolor(a_bgcolor),
250                         color(a_color)
251                 {
252                         //tooltip = unescape_enriched(utf8_to_wide(a_tooltip));
253                         tooltip = utf8_to_wide(a_tooltip);
254                 }
255                 std::wstring tooltip;
256                 irr::video::SColor bgcolor;
257                 irr::video::SColor color;
258         };
259
260         struct StaticTextSpec {
261                 StaticTextSpec():
262                         parent_button(NULL)
263                 {
264                 }
265                 StaticTextSpec(const std::wstring &a_text,
266                                 const core::rect<s32> &a_rect):
267                         rect(a_rect),
268                         parent_button(NULL)
269                 {
270                         //text = unescape_enriched(a_text);
271                         text = a_text;
272                 }
273                 StaticTextSpec(const std::wstring &a_text,
274                                 const core::rect<s32> &a_rect,
275                                 gui::IGUIButton *a_parent_button):
276                         rect(a_rect),
277                         parent_button(a_parent_button)
278                 {
279                         //text = unescape_enriched(a_text);
280                         text = a_text;
281                 }
282                 std::wstring text;
283                 core::rect<s32> rect;
284                 gui::IGUIButton *parent_button;
285         };
286
287 public:
288         GUIFormSpecMenu(irr::IrrlichtDevice* dev,
289                         JoystickController *joystick,
290                         gui::IGUIElement* parent, s32 id,
291                         IMenuManager *menumgr,
292                         InventoryManager *invmgr,
293                         IGameDef *gamedef,
294                         ISimpleTextureSource *tsrc,
295                         IFormSource* fs_src,
296                         TextDest* txt_dst,
297                         Client* client,
298                         bool remap_dbl_click = true);
299
300         ~GUIFormSpecMenu();
301
302         void setFormSpec(const std::string &formspec_string,
303                         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(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         IGameDef *m_gamedef;
388         ISimpleTextureSource *m_tsrc;
389         Client *m_client;
390
391         std::string m_formspec_string;
392         InventoryLocation m_current_inventory_location;
393
394
395         std::vector<ListDrawSpec> m_inventorylists;
396         std::vector<ListRingSpec> m_inventory_rings;
397         std::vector<ImageDrawSpec> m_backgrounds;
398         std::vector<ImageDrawSpec> m_images;
399         std::vector<ImageDrawSpec> m_itemimages;
400         std::vector<BoxDrawSpec> m_boxes;
401         UNORDERED_MAP<std::string, bool> field_close_on_enter;
402         std::vector<FieldSpec> m_fields;
403         std::vector<StaticTextSpec> m_static_texts;
404         std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
405         std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
406         std::map<std::string, TooltipSpec> m_tooltips;
407         std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
408         std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
409
410         ItemSpec *m_selected_item;
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                 UNORDERED_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 parseContainer(parserData* data, std::string element);
479         void parseContainerEnd(parserData* data);
480         void parseList(parserData* data, std::string element);
481         void parseListRing(parserData* data, std::string element);
482         void parseCheckbox(parserData* data, std::string element);
483         void parseImage(parserData* data, std::string element);
484         void parseItemImage(parserData* data,std::string element);
485         void parseButton(parserData* data,std::string element,std::string typ);
486         void parseBackground(parserData* data,std::string element);
487         void parseTableOptions(parserData* data,std::string element);
488         void parseTableColumns(parserData* data,std::string element);
489         void parseTable(parserData* data,std::string element);
490         void parseTextList(parserData* data,std::string element);
491         void parseDropDown(parserData* data,std::string element);
492         void parseFieldCloseOnEnter(parserData *data, const std::string &element);
493         void parsePwdField(parserData* data,std::string element);
494         void parseField(parserData* data,std::string element,std::string type);
495         void parseSimpleField(parserData* data,std::vector<std::string> &parts);
496         void parseTextArea(parserData* data,std::vector<std::string>& parts,
497                         std::string type);
498         void parseLabel(parserData* data,std::string element);
499         void parseVertLabel(parserData* data,std::string element);
500         void parseImageButton(parserData* data,std::string element,std::string type);
501         void parseItemImageButton(parserData* data,std::string element);
502         void parseTabHeader(parserData* data,std::string element);
503         void parseBox(parserData* data,std::string element);
504         void parseBackgroundColor(parserData* data,std::string element);
505         void parseListColors(parserData* data,std::string element);
506         void parseTooltip(parserData* data,std::string element);
507         bool parseVersionDirect(std::string data);
508         bool parseSizeDirect(parserData* data, std::string element);
509         void parseScrollBar(parserData* data, std::string element);
510
511         void tryClose();
512
513         /**
514          * check if event is part of a double click
515          * @param event event to evaluate
516          * @return true/false if a doubleclick was detected
517          */
518         bool DoubleClickDetection(const SEvent event);
519
520         struct clickpos
521         {
522                 v2s32 pos;
523                 s32 time;
524         };
525         clickpos m_doubleclickdetect[2];
526
527         int m_btn_height;
528         gui::IGUIFont *m_font;
529
530         std::wstring getLabelByID(s32 id);
531         std::string getNameByID(s32 id);
532 #ifdef __ANDROID__
533         v2s32 m_down_pos;
534         std::string m_JavaDialogFieldName;
535 #endif
536
537         /* If true, remap a double-click (or double-tap) action to ESC. This is so
538          * that, for example, Android users can double-tap to close a formspec.
539         *
540          * This value can (currently) only be set by the class constructor
541          * and the default value for the setting is true.
542          */
543         bool m_remap_dbl_click;
544
545 };
546
547 class FormspecFormSource: public IFormSource
548 {
549 public:
550         FormspecFormSource(std::string formspec)
551         {
552                 m_formspec = formspec;
553         }
554
555         ~FormspecFormSource()
556         {}
557
558         void setForm(std::string formspec) {
559                 m_formspec = FORMSPEC_VERSION_STRING + formspec;
560         }
561
562         std::string getForm()
563         {
564                 return m_formspec;
565         }
566
567         std::string m_formspec;
568 };
569
570 #endif