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