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