Remove unused 'm_selection_active' field
[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
33 class IGameDef;
34 class InventoryManager;
35 class ISimpleTextureSource;
36 class Client;
37
38 typedef enum {
39         f_Button,
40         f_Table,
41         f_TabHeader,
42         f_CheckBox,
43         f_DropDown,
44         f_ScrollBar,
45         f_Unknown
46 } FormspecFieldType;
47
48 typedef enum {
49         quit_mode_no,
50         quit_mode_accept,
51         quit_mode_cancel
52 } FormspecQuitMode;
53
54 struct TextDest
55 {
56         virtual ~TextDest() {};
57         // This is deprecated I guess? -celeron55
58         virtual void gotText(std::wstring text){}
59         virtual void gotText(const StringMap &fields) = 0;
60         virtual void setFormName(std::string formname)
61         { m_formname = formname;};
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(std::string str){ return str; }
73 };
74
75 class GUIFormSpecMenu : public GUIModalMenu
76 {
77         struct ItemSpec
78         {
79                 ItemSpec()
80                 {
81                         i = -1;
82                 }
83                 ItemSpec(const InventoryLocation &a_inventoryloc,
84                                 const std::string &a_listname,
85                                 s32 a_i)
86                 {
87                         inventoryloc = a_inventoryloc;
88                         listname = a_listname;
89                         i = a_i;
90                 }
91                 bool isValid() const
92                 {
93                         return i != -1;
94                 }
95
96                 InventoryLocation inventoryloc;
97                 std::string listname;
98                 s32 i;
99         };
100
101         struct ListDrawSpec
102         {
103                 ListDrawSpec()
104                 {
105                 }
106                 ListDrawSpec(const InventoryLocation &a_inventoryloc,
107                                 const std::string &a_listname,
108                                 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
109                         inventoryloc(a_inventoryloc),
110                         listname(a_listname),
111                         pos(a_pos),
112                         geom(a_geom),
113                         start_item_i(a_start_item_i)
114                 {
115                 }
116
117                 InventoryLocation inventoryloc;
118                 std::string listname;
119                 v2s32 pos;
120                 v2s32 geom;
121                 s32 start_item_i;
122         };
123
124         struct ListRingSpec
125         {
126                 ListRingSpec()
127                 {
128                 }
129                 ListRingSpec(const InventoryLocation &a_inventoryloc,
130                                 const std::string &a_listname):
131                         inventoryloc(a_inventoryloc),
132                         listname(a_listname)
133                 {
134                 }
135
136                 InventoryLocation inventoryloc;
137                 std::string listname;
138         };
139
140         struct ImageDrawSpec
141         {
142                 ImageDrawSpec()
143                 {
144                 }
145                 ImageDrawSpec(const std::string &a_name,
146                                 const std::string &a_item_name,
147                                 const v2s32 &a_pos, const v2s32 &a_geom):
148                         name(a_name),
149                         item_name (a_item_name),
150                         pos(a_pos),
151                         geom(a_geom)
152                 {
153                         scale = true;
154                 }
155                 ImageDrawSpec(const std::string &a_name,
156                                 const v2s32 &a_pos, const v2s32 &a_geom):
157                         name(a_name),
158                         pos(a_pos),
159                         geom(a_geom)
160                 {
161                         scale = true;
162                 }
163                 ImageDrawSpec(const std::string &a_name,
164                                 const v2s32 &a_pos):
165                         name(a_name),
166                         pos(a_pos)
167                 {
168                         scale = false;
169                 }
170                 std::string name;
171                 std::string item_name;
172                 v2s32 pos;
173                 v2s32 geom;
174                 bool scale;
175         };
176
177         struct FieldSpec
178         {
179                 FieldSpec()
180                 {
181                 }
182                 FieldSpec(const std::string &name, const std::wstring &label,
183                                 const std::wstring &fdeflt, int id) :
184                         fname(name),
185                         flabel(label),
186                         fdefault(fdeflt),
187                         fid(id)
188                 {
189                         send = false;
190                         ftype = f_Unknown;
191                         is_exit = false;
192                 }
193                 std::string fname;
194                 std::wstring flabel;
195                 std::wstring fdefault;
196                 int fid;
197                 bool send;
198                 FormspecFieldType ftype;
199                 bool is_exit;
200                 core::rect<s32> rect;
201         };
202
203         struct BoxDrawSpec {
204                 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
205                         pos(a_pos),
206                         geom(a_geom),
207                         color(a_color)
208                 {
209                 }
210                 v2s32 pos;
211                 v2s32 geom;
212                 irr::video::SColor color;
213         };
214
215         struct TooltipSpec {
216                 TooltipSpec()
217                 {
218                 }
219                 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
220                                 irr::video::SColor a_color):
221                         tooltip(a_tooltip),
222                         bgcolor(a_bgcolor),
223                         color(a_color)
224                 {
225                 }
226                 std::string tooltip;
227                 irr::video::SColor bgcolor;
228                 irr::video::SColor color;
229         };
230
231         struct StaticTextSpec {
232                 StaticTextSpec()
233                 {
234                 }
235                 StaticTextSpec(const std::wstring &a_text,
236                                 const core::rect<s32> &a_rect):
237                         text(a_text),
238                         rect(a_rect)
239                 {
240                 }
241                 std::wstring text;
242                 core::rect<s32> rect;
243         };
244
245 public:
246         GUIFormSpecMenu(irr::IrrlichtDevice* dev,
247                         gui::IGUIElement* parent, s32 id,
248                         IMenuManager *menumgr,
249                         InventoryManager *invmgr,
250                         IGameDef *gamedef,
251                         ISimpleTextureSource *tsrc,
252                         IFormSource* fs_src,
253                         TextDest* txt_dst,
254                         Client* client,
255                         bool remap_dbl_click = true);
256
257         ~GUIFormSpecMenu();
258
259         void setFormSpec(const std::string &formspec_string,
260                         InventoryLocation current_inventory_location)
261         {
262                 m_formspec_string = formspec_string;
263                 m_current_inventory_location = current_inventory_location;
264                 regenerateGui(m_screensize_old);
265         }
266
267         // form_src is deleted by this GUIFormSpecMenu
268         void setFormSource(IFormSource *form_src)
269         {
270                 if (m_form_src != NULL) {
271                         delete m_form_src;
272                 }
273                 m_form_src = form_src;
274         }
275
276         // text_dst is deleted by this GUIFormSpecMenu
277         void setTextDest(TextDest *text_dst)
278         {
279                 if (m_text_dst != NULL) {
280                         delete m_text_dst;
281                 }
282                 m_text_dst = text_dst;
283         }
284
285         void allowClose(bool value)
286         {
287                 m_allowclose = value;
288         }
289
290         void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
291         {
292                 m_lock = lock;
293                 m_lockscreensize = basescreensize;
294         }
295
296         void removeChildren();
297         void setInitialFocus();
298
299         void setFocus(std::string &elementname)
300         {
301                 m_focused_element = elementname;
302         }
303
304         /*
305                 Remove and re-add (or reposition) stuff
306         */
307         void regenerateGui(v2u32 screensize);
308
309         ItemSpec getItemAtPos(v2s32 p) const;
310         void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
311         void drawSelectedItem();
312         void drawMenu();
313         void updateSelectedItem();
314         ItemStack verifySelectedItem();
315
316         void acceptInput(FormspecQuitMode quitmode);
317         bool preprocessEvent(const SEvent& event);
318         bool OnEvent(const SEvent& event);
319         bool doPause;
320         bool pausesGame() { return doPause; }
321
322         GUITable* getTable(const std::string &tablename);
323
324 #ifdef __ANDROID__
325         bool getAndroidUIInput();
326 #endif
327
328 protected:
329         v2s32 getBasePos() const
330         {
331                         return padding + offset + AbsoluteRect.UpperLeftCorner;
332         }
333
334         v2s32 padding;
335         v2s32 spacing;
336         v2s32 imgsize;
337         v2s32 offset;
338
339         irr::IrrlichtDevice* m_device;
340         InventoryManager *m_invmgr;
341         IGameDef *m_gamedef;
342         ISimpleTextureSource *m_tsrc;
343         Client *m_client;
344
345         std::string m_formspec_string;
346         InventoryLocation m_current_inventory_location;
347
348
349         std::vector<ListDrawSpec> m_inventorylists;
350         std::vector<ListRingSpec> m_inventory_rings;
351         std::vector<ImageDrawSpec> m_backgrounds;
352         std::vector<ImageDrawSpec> m_images;
353         std::vector<ImageDrawSpec> m_itemimages;
354         std::vector<BoxDrawSpec> m_boxes;
355         std::vector<FieldSpec> m_fields;
356         std::vector<StaticTextSpec> m_static_texts;
357         std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
358         std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
359         std::map<std::string, TooltipSpec> m_tooltips;
360         std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
361
362         ItemSpec *m_selected_item;
363         f32 m_timer1;
364         f32 m_timer2;
365         u32 m_selected_amount;
366         bool m_selected_dragging;
367
368         // WARNING: BLACK MAGIC
369         // Used to guess and keep up with some special things the server can do.
370         // If name is "", no guess exists.
371         ItemStack m_selected_content_guess;
372         InventoryLocation m_selected_content_guess_inventory;
373
374         v2s32 m_pointer;
375         v2s32 m_old_pointer;  // Mouse position after previous mouse event
376         gui::IGUIStaticText *m_tooltip_element;
377
378         u32 m_tooltip_show_delay;
379         s32 m_hovered_time;
380         s32 m_old_tooltip_id;
381         std::string m_old_tooltip;
382
383         bool m_rmouse_auto_place;
384
385         bool m_allowclose;
386         bool m_lock;
387         v2u32 m_lockscreensize;
388
389         bool m_bgfullscreen;
390         bool m_slotborder;
391         bool m_clipbackground;
392         video::SColor m_bgcolor;
393         video::SColor m_slotbg_n;
394         video::SColor m_slotbg_h;
395         video::SColor m_slotbordercolor;
396         video::SColor m_default_tooltip_bgcolor;
397         video::SColor m_default_tooltip_color;
398
399 private:
400         IFormSource      *m_form_src;
401         TextDest         *m_text_dst;
402         unsigned int      m_formspec_version;
403         std::string       m_focused_element;
404
405         typedef struct {
406                 bool explicit_size;
407                 v2f invsize;
408                 v2s32 size;
409                 core::rect<s32> rect;
410                 v2s32 basepos;
411                 v2u32 screensize;
412                 std::string focused_fieldname;
413                 GUITable::TableOptions table_options;
414                 GUITable::TableColumns table_columns;
415                 // used to restore table selection/scroll/treeview state
416                 std::map<std::string, GUITable::DynamicData> table_dyndata;
417         } parserData;
418
419         typedef struct {
420                 bool key_up;
421                 bool key_down;
422                 bool key_enter;
423                 bool key_escape;
424         } fs_key_pendig;
425
426         fs_key_pendig current_keys_pending;
427
428         void parseElement(parserData* data,std::string element);
429
430         void parseSize(parserData* data,std::string element);
431         void parseList(parserData* data,std::string element);
432         void parseListRing(parserData* data,std::string element);
433         void parseCheckbox(parserData* data,std::string element);
434         void parseImage(parserData* data,std::string element);
435         void parseItemImage(parserData* data,std::string element);
436         void parseButton(parserData* data,std::string element,std::string typ);
437         void parseBackground(parserData* data,std::string element);
438         void parseTableOptions(parserData* data,std::string element);
439         void parseTableColumns(parserData* data,std::string element);
440         void parseTable(parserData* data,std::string element);
441         void parseTextList(parserData* data,std::string element);
442         void parseDropDown(parserData* data,std::string element);
443         void parsePwdField(parserData* data,std::string element);
444         void parseField(parserData* data,std::string element,std::string type);
445         void parseSimpleField(parserData* data,std::vector<std::string> &parts);
446         void parseTextArea(parserData* data,std::vector<std::string>& parts,
447                         std::string type);
448         void parseLabel(parserData* data,std::string element);
449         void parseVertLabel(parserData* data,std::string element);
450         void parseImageButton(parserData* data,std::string element,std::string type);
451         void parseItemImageButton(parserData* data,std::string element);
452         void parseTabHeader(parserData* data,std::string element);
453         void parseBox(parserData* data,std::string element);
454         void parseBackgroundColor(parserData* data,std::string element);
455         void parseListColors(parserData* data,std::string element);
456         void parseTooltip(parserData* data,std::string element);
457         bool parseVersionDirect(std::string data);
458         bool parseSizeDirect(parserData* data, std::string element);
459         void parseScrollBar(parserData* data, std::string element);
460
461         /**
462          * check if event is part of a double click
463          * @param event event to evaluate
464          * @return true/false if a doubleclick was detected
465          */
466         bool DoubleClickDetection(const SEvent event);
467
468         struct clickpos
469         {
470                 v2s32 pos;
471                 s32 time;
472         };
473         clickpos m_doubleclickdetect[2];
474
475         int m_btn_height;
476         gui::IGUIFont *m_font;
477
478         std::wstring getLabelByID(s32 id);
479         std::string getNameByID(s32 id);
480 #ifdef __ANDROID__
481         v2s32 m_down_pos;
482         std::string m_JavaDialogFieldName;
483 #endif
484
485         /* If true, remap a double-click (or double-tap) action to ESC. This is so
486          * that, for example, Android users can double-tap to close a formspec.
487         *
488          * This value can (currently) only be set by the class constructor
489          * and the default value for the setting is true.
490          */
491         bool m_remap_dbl_click;
492
493 };
494
495 class FormspecFormSource: public IFormSource
496 {
497 public:
498         FormspecFormSource(std::string formspec)
499         {
500                 m_formspec = formspec;
501         }
502
503         ~FormspecFormSource()
504         {}
505
506         void setForm(std::string formspec) {
507                 m_formspec = FORMSPEC_VERSION_STRING + formspec;
508         }
509
510         std::string getForm()
511         {
512                 return m_formspec;
513         }
514
515         std::string m_formspec;
516 };
517
518 #endif
519