3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
25 #include "irrlichttypes_extrabloated.h"
26 #include "inventorymanager.h"
27 #include "modalMenu.h"
29 #include "network/networkprotocol.h"
30 #include "client/joystick_controller.h"
31 #include "util/string.h"
32 #include "util/enriched_string.h"
33 #include "StyleSpec.h"
35 class InventoryManager;
36 class ISimpleTextureSource;
57 virtual ~TextDest() = default;
59 // This is deprecated I guess? -celeron55
60 virtual void gotText(const std::wstring &text) {}
61 virtual void gotText(const StringMap &fields) = 0;
63 std::string m_formname;
69 virtual ~IFormSource() = default;
70 virtual const std::string &getForm() const = 0;
71 // Fill in variables in field text
72 virtual std::string resolveText(const std::string &str) { return str; }
75 class GUIFormSpecMenu : public GUIModalMenu
81 ItemSpec(const InventoryLocation &a_inventoryloc,
82 const std::string &a_listname,
84 inventoryloc(a_inventoryloc),
90 bool isValid() const { return i != -1; }
92 InventoryLocation inventoryloc;
99 ListDrawSpec() = default;
101 ListDrawSpec(const InventoryLocation &a_inventoryloc,
102 const std::string &a_listname,
103 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i,
104 bool a_real_coordinates):
105 inventoryloc(a_inventoryloc),
106 listname(a_listname),
109 start_item_i(a_start_item_i),
110 real_coordinates(a_real_coordinates)
114 InventoryLocation inventoryloc;
115 std::string listname;
119 bool real_coordinates;
124 ListRingSpec() = default;
126 ListRingSpec(const InventoryLocation &a_inventoryloc,
127 const std::string &a_listname):
128 inventoryloc(a_inventoryloc),
133 InventoryLocation inventoryloc;
134 std::string listname;
145 ImageDrawSpec(const std::string &a_name,
146 const std::string &a_item_name,
147 gui::IGUIButton *a_parent_button,
148 const v2s32 &a_pos, const v2s32 &a_geom):
150 item_name(a_item_name),
151 parent_button(a_parent_button),
159 ImageDrawSpec(const std::string &a_name,
160 const std::string &a_item_name,
161 const v2s32 &a_pos, const v2s32 &a_geom):
163 item_name(a_item_name),
172 ImageDrawSpec(const std::string &a_name,
173 const v2s32 &a_pos, const v2s32 &a_geom, bool clip=false):
183 ImageDrawSpec(const std::string &a_name,
184 const v2s32 &a_pos, const v2s32 &a_geom, const core::rect<s32> &middle, bool clip=false):
195 ImageDrawSpec(const std::string &a_name,
206 std::string item_name;
207 gui::IGUIButton *parent_button;
210 core::rect<s32> middle;
217 FieldSpec() = default;
219 FieldSpec(const std::string &name, const std::wstring &label,
220 const std::wstring &default_text, int id) :
223 fdefault(unescape_enriched(translate_string(default_text))),
233 std::wstring fdefault;
236 FormspecFieldType ftype;
238 core::rect<s32> rect;
243 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom, irr::video::SColor a_color):
251 irr::video::SColor color;
256 TooltipSpec() = default;
257 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
258 irr::video::SColor a_color):
259 tooltip(translate_string(a_tooltip)),
265 std::wstring tooltip;
266 irr::video::SColor bgcolor;
267 irr::video::SColor color;
270 struct StaticTextSpec
277 StaticTextSpec(const std::wstring &a_text,
278 const core::rect<s32> &a_rect):
285 StaticTextSpec(const std::wstring &a_text,
286 const core::rect<s32> &a_rect,
287 gui::IGUIButton *a_parent_button):
290 parent_button(a_parent_button)
295 core::rect<s32> rect;
296 gui::IGUIButton *parent_button;
300 GUIFormSpecMenu(JoystickController *joystick,
301 gui::IGUIElement* parent, s32 id,
302 IMenuManager *menumgr,
304 ISimpleTextureSource *tsrc,
307 const std::string &formspecPrepend,
308 bool remap_dbl_click = true);
312 static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
313 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
314 const std::string &formspecPrepend);
316 void setFormSpec(const std::string &formspec_string,
317 const InventoryLocation ¤t_inventory_location)
319 m_formspec_string = formspec_string;
320 m_current_inventory_location = current_inventory_location;
321 regenerateGui(m_screensize_old);
324 const InventoryLocation &getFormspecLocation()
326 return m_current_inventory_location;
329 void setFormspecPrepend(const std::string &formspecPrepend)
331 m_formspec_prepend = formspecPrepend;
334 // form_src is deleted by this GUIFormSpecMenu
335 void setFormSource(IFormSource *form_src)
338 m_form_src = form_src;
341 // text_dst is deleted by this GUIFormSpecMenu
342 void setTextDest(TextDest *text_dst)
345 m_text_dst = text_dst;
348 void allowClose(bool value)
350 m_allowclose = value;
353 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
356 m_lockscreensize = basescreensize;
359 void removeChildren();
360 void setInitialFocus();
362 void setFocus(const std::string &elementname)
364 m_focused_element = elementname;
368 Remove and re-add (or reposition) stuff
370 void regenerateGui(v2u32 screensize);
372 ItemSpec getItemAtPos(v2s32 p) const;
373 void drawList(const ListDrawSpec &s, int layer, bool &item_hovered);
374 void drawSelectedItem();
376 void updateSelectedItem();
377 ItemStack verifySelectedItem();
379 void acceptInput(FormspecQuitMode quitmode);
380 bool preprocessEvent(const SEvent& event);
381 bool OnEvent(const SEvent& event);
383 bool pausesGame() { return doPause; }
385 GUITable* getTable(const std::string &tablename);
386 std::vector<std::string>* getDropDownValues(const std::string &name);
389 bool getAndroidUIInput();
393 v2s32 getBasePos() const
395 return padding + offset + AbsoluteRect.UpperLeftCorner;
397 std::wstring getLabelByID(s32 id);
398 std::string getNameByID(s32 id);
399 v2s32 getElementBasePos(bool absolute,
400 const std::vector<std::string> *v_pos);
401 v2s32 getRealCoordinateBasePos(bool absolute,
402 const std::vector<std::string> &v_pos);
403 v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
405 std::unordered_map<std::string, StyleSpec> theme_by_type;
406 std::unordered_map<std::string, StyleSpec> theme_by_name;
408 StyleSpec getThemeForElement(const std::string &type, const std::string &name);
415 std::stack<v2f32> container_stack;
417 InventoryManager *m_invmgr;
418 ISimpleTextureSource *m_tsrc;
421 std::string m_formspec_string;
422 std::string m_formspec_prepend;
423 InventoryLocation m_current_inventory_location;
425 std::vector<ListDrawSpec> m_inventorylists;
426 std::vector<ListRingSpec> m_inventory_rings;
427 std::vector<ImageDrawSpec> m_backgrounds;
428 std::vector<ImageDrawSpec> m_images;
429 std::vector<ImageDrawSpec> m_itemimages;
430 std::vector<BoxDrawSpec> m_boxes;
431 std::unordered_map<std::string, bool> field_close_on_enter;
432 std::vector<FieldSpec> m_fields;
433 std::vector<StaticTextSpec> m_static_texts;
434 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
435 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
436 std::map<std::string, TooltipSpec> m_tooltips;
437 std::vector<std::pair<irr::core::rect<s32>, TooltipSpec>> m_tooltip_rects;
438 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
439 std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
441 ItemSpec *m_selected_item = nullptr;
442 u16 m_selected_amount = 0;
443 bool m_selected_dragging = false;
444 ItemStack m_selected_swap;
446 gui::IGUIStaticText *m_tooltip_element = nullptr;
448 u64 m_tooltip_show_delay;
449 bool m_tooltip_append_itemname;
450 u64 m_hovered_time = 0;
451 s32 m_old_tooltip_id = -1;
453 bool m_auto_place = false;
455 bool m_allowclose = true;
457 v2u32 m_lockscreensize;
461 video::SColor m_bgcolor;
462 video::SColor m_fullscreen_bgcolor;
463 video::SColor m_slotbg_n;
464 video::SColor m_slotbg_h;
465 video::SColor m_slotbordercolor;
466 video::SColor m_default_tooltip_bgcolor;
467 video::SColor m_default_tooltip_color;
470 IFormSource *m_form_src;
471 TextDest *m_text_dst;
472 u32 m_formspec_version = 0;
473 std::string m_focused_element = "";
474 JoystickController *m_joystick;
478 bool real_coordinates;
483 core::rect<s32> rect;
486 std::string focused_fieldname;
487 GUITable::TableOptions table_options;
488 GUITable::TableColumns table_columns;
489 // used to restore table selection/scroll/treeview state
490 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
500 fs_key_pendig current_keys_pending;
501 std::string current_field_enter_pending = "";
503 void parseElement(parserData* data, const std::string &element);
505 void parseSize(parserData* data, const std::string &element);
506 void parseContainer(parserData* data, const std::string &element);
507 void parseContainerEnd(parserData* data);
508 void parseList(parserData* data, const std::string &element);
509 void parseListRing(parserData* data, const std::string &element);
510 void parseCheckbox(parserData* data, const std::string &element);
511 void parseImage(parserData* data, const std::string &element);
512 void parseItemImage(parserData* data, const std::string &element);
513 void parseButton(parserData* data, const std::string &element,
514 const std::string &typ);
515 void parseBackground(parserData* data, const std::string &element);
516 void parseTableOptions(parserData* data, const std::string &element);
517 void parseTableColumns(parserData* data, const std::string &element);
518 void parseTable(parserData* data, const std::string &element);
519 void parseTextList(parserData* data, const std::string &element);
520 void parseDropDown(parserData* data, const std::string &element);
521 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
522 void parsePwdField(parserData* data, const std::string &element);
523 void parseField(parserData* data, const std::string &element, const std::string &type);
524 void createTextField(parserData *data, FieldSpec &spec,
525 core::rect<s32> &rect, bool is_multiline);
526 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
527 void parseTextArea(parserData* data,std::vector<std::string>& parts,
528 const std::string &type);
529 void parseLabel(parserData* data, const std::string &element);
530 void parseVertLabel(parserData* data, const std::string &element);
531 void parseImageButton(parserData* data, const std::string &element,
532 const std::string &type);
533 void parseItemImageButton(parserData* data, const std::string &element);
534 void parseTabHeader(parserData* data, const std::string &element);
535 void parseBox(parserData* data, const std::string &element);
536 void parseBackgroundColor(parserData* data, const std::string &element);
537 void parseListColors(parserData* data, const std::string &element);
538 void parseTooltip(parserData* data, const std::string &element);
539 bool parseVersionDirect(const std::string &data);
540 bool parseSizeDirect(parserData* data, const std::string &element);
541 void parseScrollBar(parserData* data, const std::string &element);
542 bool parsePositionDirect(parserData *data, const std::string &element);
543 void parsePosition(parserData *data, const std::string &element);
544 bool parseAnchorDirect(parserData *data, const std::string &element);
545 void parseAnchor(parserData *data, const std::string &element);
546 bool parseStyle(parserData *data, const std::string &element, bool style_type);
550 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
551 const irr::video::SColor &bgcolor);
554 * check if event is part of a double click
555 * @param event event to evaluate
556 * @return true/false if a doubleclick was detected
558 bool DoubleClickDetection(const SEvent event);
565 clickpos m_doubleclickdetect[2];
568 gui::IGUIFont *m_font = nullptr;
570 /* If true, remap a double-click (or double-tap) action to ESC. This is so
571 * that, for example, Android users can double-tap to close a formspec.
573 * This value can (currently) only be set by the class constructor
574 * and the default value for the setting is true.
576 bool m_remap_dbl_click;
580 class FormspecFormSource: public IFormSource
583 FormspecFormSource(const std::string &formspec):
588 ~FormspecFormSource() = default;
590 void setForm(const std::string &formspec)
592 m_formspec = FORMSPEC_VERSION_STRING + formspec;
595 const std::string &getForm() const
600 std::string m_formspec;