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