Fix android build using hardcoded path for data instead of using the one fetched...
[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 "clientserver.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(std::map<std::string, std::string> 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 ImageDrawSpec
125         {
126                 ImageDrawSpec()
127                 {
128                 }
129                 ImageDrawSpec(const std::string &a_name,
130                                 v2s32 a_pos, v2s32 a_geom):
131                         name(a_name),
132                         pos(a_pos),
133                         geom(a_geom)
134                 {
135                         scale = true;
136                 }
137                 ImageDrawSpec(const std::string &a_name,
138                                 v2s32 a_pos):
139                         name(a_name),
140                         pos(a_pos)
141                 {
142                         scale = false;
143                 }
144                 std::string name;
145                 v2s32 pos;
146                 v2s32 geom;
147                 bool scale;
148         };
149
150         struct FieldSpec
151         {
152                 FieldSpec()
153                 {
154                 }
155                 FieldSpec(const std::wstring &name, const std::wstring &label,
156                                 const std::wstring &fdeflt, int id) :
157                         fname(name),
158                         flabel(label),
159                         fdefault(fdeflt),
160                         fid(id)
161                 {
162                         send = false;
163                         ftype = f_Unknown;
164                         is_exit = false;
165                 }
166                 std::wstring fname;
167                 std::wstring flabel;
168                 std::wstring fdefault;
169                 int fid;
170                 bool send;
171                 FormspecFieldType ftype;
172                 bool is_exit;
173                 core::rect<s32> rect;
174         };
175
176         struct BoxDrawSpec {
177                 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
178                         pos(a_pos),
179                         geom(a_geom),
180                         color(a_color)
181                 {
182                 }
183                 v2s32 pos;
184                 v2s32 geom;
185                 irr::video::SColor color;
186         };
187
188         struct TooltipSpec {
189                 TooltipSpec()
190                 {
191                 }
192                 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
193                                 irr::video::SColor a_color):
194                         tooltip(a_tooltip),
195                         bgcolor(a_bgcolor),
196                         color(a_color)
197                 {
198                 }
199                 std::string tooltip;
200                 irr::video::SColor bgcolor;
201                 irr::video::SColor color;
202         };
203
204 public:
205         GUIFormSpecMenu(irr::IrrlichtDevice* dev,
206                         gui::IGUIElement* parent, s32 id,
207                         IMenuManager *menumgr,
208                         InventoryManager *invmgr,
209                         IGameDef *gamedef,
210                         ISimpleTextureSource *tsrc,
211                         IFormSource* fs_src,
212                         TextDest* txt_dst,
213                         GUIFormSpecMenu** ext_ptr,
214                         Client* client
215                         );
216
217         ~GUIFormSpecMenu();
218
219         void setFormSpec(const std::string &formspec_string,
220                         InventoryLocation current_inventory_location)
221         {
222                 m_formspec_string = formspec_string;
223                 m_current_inventory_location = current_inventory_location;
224                 regenerateGui(m_screensize_old);
225         }
226
227         // form_src is deleted by this GUIFormSpecMenu
228         void setFormSource(IFormSource *form_src)
229         {
230                 if (m_form_src != NULL) {
231                         delete m_form_src;
232                 }
233                 m_form_src = form_src;
234         }
235
236         // text_dst is deleted by this GUIFormSpecMenu
237         void setTextDest(TextDest *text_dst)
238         {
239                 if (m_text_dst != NULL) {
240                         delete m_text_dst;
241                 }
242                 m_text_dst = text_dst;
243         }
244
245         void allowClose(bool value)
246         {
247                 m_allowclose = value;
248         }
249
250         void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0)) {
251                 m_lock = lock;
252                 m_lockscreensize = basescreensize;
253         }
254
255         void removeChildren();
256         void setInitialFocus();
257         /*
258                 Remove and re-add (or reposition) stuff
259         */
260         void regenerateGui(v2u32 screensize);
261
262         ItemSpec getItemAtPos(v2s32 p) const;
263         void drawList(const ListDrawSpec &s, int phase);
264         void drawSelectedItem();
265         void drawMenu();
266         void updateSelectedItem();
267         ItemStack verifySelectedItem();
268
269         void acceptInput(FormspecQuitMode quitmode);
270         bool preprocessEvent(const SEvent& event);
271         bool OnEvent(const SEvent& event);
272         bool doPause;
273         bool pausesGame() { return doPause; }
274
275         GUITable* getTable(std::wstring tablename);
276
277         static bool parseColor(const std::string &value,
278                         video::SColor &color, bool quiet);
279
280 #ifdef __ANDROID__
281         bool getAndroidUIInput();
282 #endif
283
284 protected:
285         v2s32 getBasePos() const
286         {
287                         return padding + offset + AbsoluteRect.UpperLeftCorner;
288         }
289
290         v2s32 padding;
291         v2s32 spacing;
292         v2s32 imgsize;
293         v2s32 offset;
294
295         irr::IrrlichtDevice* m_device;
296         InventoryManager *m_invmgr;
297         IGameDef *m_gamedef;
298         ISimpleTextureSource *m_tsrc;
299         Client *m_client;
300
301         std::string m_formspec_string;
302         InventoryLocation m_current_inventory_location;
303
304
305         std::vector<ListDrawSpec> m_inventorylists;
306         std::vector<ImageDrawSpec> m_backgrounds;
307         std::vector<ImageDrawSpec> m_images;
308         std::vector<ImageDrawSpec> m_itemimages;
309         std::vector<BoxDrawSpec> m_boxes;
310         std::vector<FieldSpec> m_fields;
311         std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
312         std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
313         std::map<std::wstring, TooltipSpec> m_tooltips;
314         std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
315
316         ItemSpec *m_selected_item;
317         u32 m_selected_amount;
318         bool m_selected_dragging;
319
320         // WARNING: BLACK MAGIC
321         // Used to guess and keep up with some special things the server can do.
322         // If name is "", no guess exists.
323         ItemStack m_selected_content_guess;
324         InventoryLocation m_selected_content_guess_inventory;
325
326         v2s32 m_pointer;
327         v2s32 m_old_pointer;  // Mouse position after previous mouse event
328         gui::IGUIStaticText *m_tooltip_element;
329
330         u32 m_tooltip_show_delay;
331         s32 m_hoovered_time;
332         s32 m_old_tooltip_id;
333         std::string m_old_tooltip;
334
335         bool m_allowclose;
336         bool m_lock;
337         v2u32 m_lockscreensize;
338
339         bool m_bgfullscreen;
340         bool m_slotborder;
341         bool m_clipbackground;
342         video::SColor m_bgcolor;
343         video::SColor m_slotbg_n;
344         video::SColor m_slotbg_h;
345         video::SColor m_slotbordercolor;
346         video::SColor m_default_tooltip_bgcolor;
347         video::SColor m_default_tooltip_color;
348
349 private:
350         IFormSource      *m_form_src;
351         TextDest         *m_text_dst;
352         GUIFormSpecMenu **m_ext_ptr;
353         gui::IGUIFont    *m_font;
354         unsigned int      m_formspec_version;
355
356         typedef struct {
357                 v2s32 size;
358                 core::rect<s32> rect;
359                 v2s32 basepos;
360                 int bp_set;
361                 v2u32 screensize;
362                 std::wstring focused_fieldname;
363                 GUITable::TableOptions table_options;
364                 GUITable::TableColumns table_columns;
365                 // used to restore table selection/scroll/treeview state
366                 std::map<std::wstring,GUITable::DynamicData> table_dyndata;
367         } parserData;
368
369         typedef struct {
370                 bool key_up;
371                 bool key_down;
372                 bool key_enter;
373                 bool key_escape;
374         } fs_key_pendig;
375
376         fs_key_pendig current_keys_pending;
377
378         void parseElement(parserData* data,std::string element);
379
380         void parseSize(parserData* data,std::string element);
381         void parseList(parserData* data,std::string element);
382         void parseCheckbox(parserData* data,std::string element);
383         void parseImage(parserData* data,std::string element);
384         void parseItemImage(parserData* data,std::string element);
385         void parseButton(parserData* data,std::string element,std::string typ);
386         void parseBackground(parserData* data,std::string element);
387         void parseTableOptions(parserData* data,std::string element);
388         void parseTableColumns(parserData* data,std::string element);
389         void parseTable(parserData* data,std::string element);
390         void parseTextList(parserData* data,std::string element);
391         void parseDropDown(parserData* data,std::string element);
392         void parsePwdField(parserData* data,std::string element);
393         void parseField(parserData* data,std::string element,std::string type);
394         void parseSimpleField(parserData* data,std::vector<std::string> &parts);
395         void parseTextArea(parserData* data,std::vector<std::string>& parts,
396                         std::string type);
397         void parseLabel(parserData* data,std::string element);
398         void parseVertLabel(parserData* data,std::string element);
399         void parseImageButton(parserData* data,std::string element,std::string type);
400         void parseItemImageButton(parserData* data,std::string element);
401         void parseTabHeader(parserData* data,std::string element);
402         void parseBox(parserData* data,std::string element);
403         void parseBackgroundColor(parserData* data,std::string element);
404         void parseListColors(parserData* data,std::string element);
405         void parseTooltip(parserData* data,std::string element);
406         bool parseVersionDirect(std::string data);
407         void parseScrollBar(parserData* data, std::string element);
408
409         /**
410          * check if event is part of a double click
411          * @param event event to evaluate
412          * @return true/false if a doubleclick was detected
413          */
414         bool DoubleClickDetection(const SEvent event);
415
416         struct clickpos
417         {
418                 v2s32 pos;
419                 s32 time;
420         };
421         clickpos m_doubleclickdetect[2];
422
423         int m_btn_height;
424
425         std::wstring getLabelByID(s32 id);
426         std::wstring getNameByID(s32 id);
427 #ifdef __ANDROID__
428         v2s32 m_down_pos;
429         std::wstring m_JavaDialogFieldName;
430 #endif
431
432 };
433
434 class FormspecFormSource: public IFormSource
435 {
436 public:
437         FormspecFormSource(std::string formspec)
438         {
439                 m_formspec = formspec;
440         }
441
442         ~FormspecFormSource()
443         {}
444
445         void setForm(std::string formspec) {
446                 m_formspec = FORMSPEC_VERSION_STRING + formspec;
447         }
448
449         std::string getForm()
450         {
451                 return m_formspec;
452         }
453
454         std::string m_formspec;
455 };
456
457 #endif
458