]> git.lizzy.rs Git - minetest.git/blob - src/gui/guiFormSpecMenu.h
Update translation sources
[minetest.git] / src / gui / 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 #pragma once
21
22 #include <utility>
23 #include <stack>
24 #include <unordered_set>
25
26 #include "irrlichttypes_extrabloated.h"
27 #include "inventorymanager.h"
28 #include "modalMenu.h"
29 #include "guiTable.h"
30 #include "network/networkprotocol.h"
31 #include "client/joystick_controller.h"
32 #include "util/string.h"
33 #include "util/enriched_string.h"
34 #include "StyleSpec.h"
35
36 class InventoryManager;
37 class ISimpleTextureSource;
38 class Client;
39 class GUIScrollBar;
40
41 typedef enum {
42         f_Button,
43         f_Table,
44         f_TabHeader,
45         f_CheckBox,
46         f_DropDown,
47         f_ScrollBar,
48         f_Box,
49         f_ItemImage,
50         f_Unknown
51 } FormspecFieldType;
52
53 typedef enum {
54         quit_mode_no,
55         quit_mode_accept,
56         quit_mode_cancel
57 } FormspecQuitMode;
58
59 struct TextDest
60 {
61         virtual ~TextDest() = default;
62
63         // This is deprecated I guess? -celeron55
64         virtual void gotText(const std::wstring &text) {}
65         virtual void gotText(const StringMap &fields) = 0;
66
67         std::string m_formname;
68 };
69
70 class IFormSource
71 {
72 public:
73         virtual ~IFormSource() = default;
74         virtual const std::string &getForm() const = 0;
75         // Fill in variables in field text
76         virtual std::string resolveText(const std::string &str) { return str; }
77 };
78
79 class GUIFormSpecMenu : public GUIModalMenu
80 {
81         struct ItemSpec
82         {
83                 ItemSpec() = default;
84
85                 ItemSpec(const InventoryLocation &a_inventoryloc,
86                                 const std::string &a_listname,
87                                 s32 a_i) :
88                         inventoryloc(a_inventoryloc),
89                         listname(a_listname),
90                         i(a_i)
91                 {
92                 }
93
94                 bool isValid() const { return i != -1; }
95
96                 InventoryLocation inventoryloc;
97                 std::string listname;
98                 s32 i = -1;
99         };
100
101         struct ListDrawSpec
102         {
103                 ListDrawSpec() = default;
104
105                 ListDrawSpec(const InventoryLocation &a_inventoryloc,
106                                 const std::string &a_listname,
107                                 IGUIElement *elem, v2s32 a_geom, s32 a_start_item_i,
108                                 bool a_real_coordinates):
109                         inventoryloc(a_inventoryloc),
110                         listname(a_listname),
111                         e(elem),
112                         geom(a_geom),
113                         start_item_i(a_start_item_i),
114                         real_coordinates(a_real_coordinates)
115                 {
116                 }
117
118                 InventoryLocation inventoryloc;
119                 std::string listname;
120                 IGUIElement *e;
121                 v2s32 geom;
122                 s32 start_item_i;
123                 bool real_coordinates;
124         };
125
126         struct ListRingSpec
127         {
128                 ListRingSpec() = default;
129
130                 ListRingSpec(const InventoryLocation &a_inventoryloc,
131                                 const std::string &a_listname):
132                         inventoryloc(a_inventoryloc),
133                         listname(a_listname)
134                 {
135                 }
136
137                 InventoryLocation inventoryloc;
138                 std::string listname;
139         };
140
141         struct FieldSpec
142         {
143                 FieldSpec() = default;
144
145                 FieldSpec(const std::string &name, const std::wstring &label,
146                                 const std::wstring &default_text, s32 id, int priority = 0,
147                                 gui::ECURSOR_ICON cursor_icon = ECI_NORMAL) :
148                         fname(name),
149                         flabel(label),
150                         fdefault(unescape_enriched(translate_string(default_text))),
151                         fid(id),
152                         send(false),
153                         ftype(f_Unknown),
154                         is_exit(false),
155                         priority(priority),
156                         fcursor_icon(cursor_icon)
157                 {
158                 }
159
160                 std::string fname;
161                 std::wstring flabel;
162                 std::wstring fdefault;
163                 s32 fid;
164                 bool send;
165                 FormspecFieldType ftype;
166                 bool is_exit;
167                 // Draw priority for formspec version < 3
168                 int priority;
169                 core::rect<s32> rect;
170                 gui::ECURSOR_ICON fcursor_icon;
171         };
172
173         struct TooltipSpec
174         {
175                 TooltipSpec() = default;
176                 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
177                                 irr::video::SColor a_color):
178                         tooltip(translate_string(a_tooltip)),
179                         bgcolor(a_bgcolor),
180                         color(a_color)
181                 {
182                 }
183
184                 std::wstring tooltip;
185                 irr::video::SColor bgcolor;
186                 irr::video::SColor color;
187         };
188
189         struct StaticTextSpec
190         {
191                 StaticTextSpec():
192                         parent_button(NULL)
193                 {
194                 }
195
196                 StaticTextSpec(const std::wstring &a_text,
197                                 const core::rect<s32> &a_rect):
198                         text(a_text),
199                         rect(a_rect),
200                         parent_button(NULL)
201                 {
202                 }
203
204                 StaticTextSpec(const std::wstring &a_text,
205                                 const core::rect<s32> &a_rect,
206                                 gui::IGUIButton *a_parent_button):
207                         text(a_text),
208                         rect(a_rect),
209                         parent_button(a_parent_button)
210                 {
211                 }
212
213                 std::wstring text;
214                 core::rect<s32> rect;
215                 gui::IGUIButton *parent_button;
216         };
217
218 public:
219         GUIFormSpecMenu(JoystickController *joystick,
220                         gui::IGUIElement* parent, s32 id,
221                         IMenuManager *menumgr,
222                         Client *client,
223                         ISimpleTextureSource *tsrc,
224                         IFormSource* fs_src,
225                         TextDest* txt_dst,
226                         const std::string &formspecPrepend,
227                         bool remap_dbl_click = true);
228
229         ~GUIFormSpecMenu();
230
231         static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
232                 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
233                 const std::string &formspecPrepend);
234
235         void setFormSpec(const std::string &formspec_string,
236                         const InventoryLocation &current_inventory_location)
237         {
238                 m_formspec_string = formspec_string;
239                 m_current_inventory_location = current_inventory_location;
240                 regenerateGui(m_screensize_old);
241         }
242
243         const InventoryLocation &getFormspecLocation()
244         {
245                 return m_current_inventory_location;
246         }
247
248         void setFormspecPrepend(const std::string &formspecPrepend)
249         {
250                 m_formspec_prepend = formspecPrepend;
251         }
252
253         // form_src is deleted by this GUIFormSpecMenu
254         void setFormSource(IFormSource *form_src)
255         {
256                 delete m_form_src;
257                 m_form_src = form_src;
258         }
259
260         // text_dst is deleted by this GUIFormSpecMenu
261         void setTextDest(TextDest *text_dst)
262         {
263                 delete m_text_dst;
264                 m_text_dst = text_dst;
265         }
266
267         void allowClose(bool value)
268         {
269                 m_allowclose = value;
270         }
271
272         void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
273         {
274                 m_lock = lock;
275                 m_lockscreensize = basescreensize;
276         }
277
278         void removeChildren();
279         void setInitialFocus();
280
281         void setFocus(const std::string &elementname)
282         {
283                 m_focused_element = elementname;
284         }
285
286         /*
287                 Remove and re-add (or reposition) stuff
288         */
289         void regenerateGui(v2u32 screensize);
290
291         ItemSpec getItemAtPos(v2s32 p) const;
292         void drawList(const ListDrawSpec &s, int layer, bool &item_hovered);
293         void drawSelectedItem();
294         void drawMenu();
295         void updateSelectedItem();
296         ItemStack verifySelectedItem();
297
298         void acceptInput(FormspecQuitMode quitmode);
299         bool preprocessEvent(const SEvent& event);
300         bool OnEvent(const SEvent& event);
301         bool doPause;
302         bool pausesGame() { return doPause; }
303
304         GUITable* getTable(const std::string &tablename);
305         std::vector<std::string>* getDropDownValues(const std::string &name);
306
307 #ifdef __ANDROID__
308         bool getAndroidUIInput();
309 #endif
310
311 protected:
312         v2s32 getBasePos() const
313         {
314                         return padding + offset + AbsoluteRect.UpperLeftCorner;
315         }
316         std::wstring getLabelByID(s32 id);
317         std::string getNameByID(s32 id);
318         const FieldSpec *getSpecByID(s32 id);
319         v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
320         v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
321         v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
322
323         std::unordered_map<std::string, StyleSpec> theme_by_type;
324         std::unordered_map<std::string, StyleSpec> theme_by_name;
325         std::unordered_set<std::string> property_warned;
326
327         StyleSpec getStyleForElement(const std::string &type,
328                         const std::string &name="", const std::string &parent_type="");
329
330         v2s32 padding;
331         v2f32 spacing;
332         v2s32 imgsize;
333         v2s32 offset;
334         v2f32 pos_offset;
335         std::stack<v2f32> container_stack;
336
337         InventoryManager *m_invmgr;
338         ISimpleTextureSource *m_tsrc;
339         Client *m_client;
340
341         std::string m_formspec_string;
342         std::string m_formspec_prepend;
343         InventoryLocation m_current_inventory_location;
344
345         std::vector<ListDrawSpec> m_inventorylists;
346         std::vector<ListRingSpec> m_inventory_rings;
347         std::vector<gui::IGUIElement *> m_backgrounds;
348         std::unordered_map<std::string, bool> field_close_on_enter;
349         std::vector<FieldSpec> m_fields;
350         std::vector<std::pair<FieldSpec, GUITable *>> m_tables;
351         std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes;
352         std::map<std::string, TooltipSpec> m_tooltips;
353         std::vector<std::pair<gui::IGUIElement *, TooltipSpec>> m_tooltip_rects;
354         std::vector<std::pair<FieldSpec, GUIScrollBar *>> m_scrollbars;
355         std::vector<std::pair<FieldSpec, std::vector<std::string>>> m_dropdowns;
356
357         ItemSpec *m_selected_item = nullptr;
358         u16 m_selected_amount = 0;
359         bool m_selected_dragging = false;
360         ItemStack m_selected_swap;
361
362         gui::IGUIStaticText *m_tooltip_element = nullptr;
363
364         u64 m_tooltip_show_delay;
365         bool m_tooltip_append_itemname;
366         u64 m_hovered_time = 0;
367         s32 m_old_tooltip_id = -1;
368
369         bool m_auto_place = false;
370
371         bool m_allowclose = true;
372         bool m_lock = false;
373         v2u32 m_lockscreensize;
374
375         bool m_bgnonfullscreen;
376         bool m_bgfullscreen;
377         bool m_slotborder;
378         video::SColor m_bgcolor;
379         video::SColor m_fullscreen_bgcolor;
380         video::SColor m_slotbg_n;
381         video::SColor m_slotbg_h;
382         video::SColor m_slotbordercolor;
383         video::SColor m_default_tooltip_bgcolor;
384         video::SColor m_default_tooltip_color;
385
386
387 private:
388         IFormSource        *m_form_src;
389         TextDest           *m_text_dst;
390         u16                 m_formspec_version = 1;
391         std::string         m_focused_element = "";
392         JoystickController *m_joystick;
393
394         typedef struct {
395                 bool explicit_size;
396                 bool real_coordinates;
397                 u8 simple_field_count;
398                 v2f invsize;
399                 v2s32 size;
400                 v2f32 offset;
401                 v2f32 anchor;
402                 core::rect<s32> rect;
403                 v2s32 basepos;
404                 v2u32 screensize;
405                 std::string focused_fieldname;
406                 GUITable::TableOptions table_options;
407                 GUITable::TableColumns table_columns;
408
409                 struct {
410                         s32 max = 1000;
411                         s32 min = 0;
412                         s32 small_step = 10;
413                         s32 large_step = 100;
414                         s32 thumb_size = 1;
415                         GUIScrollBar::ArrowVisibility arrow_visiblity = GUIScrollBar::DEFAULT;
416                 } scrollbar_options;
417
418                 // used to restore table selection/scroll/treeview state
419                 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
420         } parserData;
421
422         typedef struct {
423                 bool key_up;
424                 bool key_down;
425                 bool key_enter;
426                 bool key_escape;
427         } fs_key_pendig;
428
429         fs_key_pendig current_keys_pending;
430         std::string current_field_enter_pending = "";
431
432         void parseElement(parserData* data, const std::string &element);
433
434         void parseSize(parserData* data, const std::string &element);
435         void parseContainer(parserData* data, const std::string &element);
436         void parseContainerEnd(parserData* data);
437         void parseList(parserData* data, const std::string &element);
438         void parseListRing(parserData* data, const std::string &element);
439         void parseCheckbox(parserData* data, const std::string &element);
440         void parseImage(parserData* data, const std::string &element);
441         void parseItemImage(parserData* data, const std::string &element);
442         void parseButton(parserData* data, const std::string &element,
443                         const std::string &typ);
444         void parseBackground(parserData* data, const std::string &element);
445         void parseTableOptions(parserData* data, const std::string &element);
446         void parseTableColumns(parserData* data, const std::string &element);
447         void parseTable(parserData* data, const std::string &element);
448         void parseTextList(parserData* data, const std::string &element);
449         void parseDropDown(parserData* data, const std::string &element);
450         void parseFieldCloseOnEnter(parserData *data, const std::string &element);
451         void parsePwdField(parserData* data, const std::string &element);
452         void parseField(parserData* data, const std::string &element, const std::string &type);
453         void createTextField(parserData *data, FieldSpec &spec,
454                 core::rect<s32> &rect, bool is_multiline);
455         void parseSimpleField(parserData* data,std::vector<std::string> &parts);
456         void parseTextArea(parserData* data,std::vector<std::string>& parts,
457                         const std::string &type);
458         void parseHyperText(parserData *data, const std::string &element);
459         void parseLabel(parserData* data, const std::string &element);
460         void parseVertLabel(parserData* data, const std::string &element);
461         void parseImageButton(parserData* data, const std::string &element,
462                         const std::string &type);
463         void parseItemImageButton(parserData* data, const std::string &element);
464         void parseTabHeader(parserData* data, const std::string &element);
465         void parseBox(parserData* data, const std::string &element);
466         void parseBackgroundColor(parserData* data, const std::string &element);
467         void parseListColors(parserData* data, const std::string &element);
468         void parseTooltip(parserData* data, const std::string &element);
469         bool parseVersionDirect(const std::string &data);
470         bool parseSizeDirect(parserData* data, const std::string &element);
471         void parseScrollBar(parserData* data, const std::string &element);
472         void parseScrollBarOptions(parserData *data, const std::string &element);
473         bool parsePositionDirect(parserData *data, const std::string &element);
474         void parsePosition(parserData *data, const std::string &element);
475         bool parseAnchorDirect(parserData *data, const std::string &element);
476         void parseAnchor(parserData *data, const std::string &element);
477         bool parseStyle(parserData *data, const std::string &element, bool style_type);
478
479         void tryClose();
480
481         void showTooltip(const std::wstring &text, const irr::video::SColor &color,
482                 const irr::video::SColor &bgcolor);
483
484         /**
485          * In formspec version < 2 the elements were not ordered properly. Some element
486          * types were drawn before others.
487          * This function sorts the elements in the old order for backwards compatibility.
488          */
489         void legacySortElements(core::list<IGUIElement *>::Iterator from);
490
491         /**
492          * check if event is part of a double click
493          * @param event event to evaluate
494          * @return true/false if a doubleclick was detected
495          */
496         bool DoubleClickDetection(const SEvent event);
497
498         struct clickpos
499         {
500                 v2s32 pos;
501                 s64 time;
502         };
503         clickpos m_doubleclickdetect[2];
504
505         int m_btn_height;
506         gui::IGUIFont *m_font = nullptr;
507
508         /* If true, remap a double-click (or double-tap) action to ESC. This is so
509          * that, for example, Android users can double-tap to close a formspec.
510         *
511          * This value can (currently) only be set by the class constructor
512          * and the default value for the setting is true.
513          */
514         bool m_remap_dbl_click;
515 };
516
517 class FormspecFormSource: public IFormSource
518 {
519 public:
520         FormspecFormSource(const std::string &formspec):
521                 m_formspec(formspec)
522         {
523         }
524
525         ~FormspecFormSource() = default;
526
527         void setForm(const std::string &formspec)
528         {
529                 m_formspec = formspec;
530         }
531
532         const std::string &getForm() const
533         {
534                 return m_formspec;
535         }
536
537         std::string m_formspec;
538 };