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.
24 #include <unordered_set>
26 #include "irrlichttypes_extrabloated.h"
27 #include "inventorymanager.h"
28 #include "modalMenu.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"
36 class InventoryManager;
37 class ISimpleTextureSource;
58 virtual ~TextDest() = default;
60 // This is deprecated I guess? -celeron55
61 virtual void gotText(const std::wstring &text) {}
62 virtual void gotText(const StringMap &fields) = 0;
64 std::string m_formname;
70 virtual ~IFormSource() = default;
71 virtual const std::string &getForm() const = 0;
72 // Fill in variables in field text
73 virtual std::string resolveText(const std::string &str) { return str; }
76 class GUIFormSpecMenu : public GUIModalMenu
82 ItemSpec(const InventoryLocation &a_inventoryloc,
83 const std::string &a_listname,
85 inventoryloc(a_inventoryloc),
91 bool isValid() const { return i != -1; }
93 InventoryLocation inventoryloc;
100 ListDrawSpec() = default;
102 ListDrawSpec(const InventoryLocation &a_inventoryloc,
103 const std::string &a_listname,
104 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i,
105 bool a_real_coordinates):
106 inventoryloc(a_inventoryloc),
107 listname(a_listname),
110 start_item_i(a_start_item_i),
111 real_coordinates(a_real_coordinates)
115 InventoryLocation inventoryloc;
116 std::string listname;
120 bool real_coordinates;
125 ListRingSpec() = default;
127 ListRingSpec(const InventoryLocation &a_inventoryloc,
128 const std::string &a_listname):
129 inventoryloc(a_inventoryloc),
134 InventoryLocation inventoryloc;
135 std::string listname;
146 ImageDrawSpec(const std::string &a_name,
147 const std::string &a_item_name,
148 gui::IGUIButton *a_parent_button,
149 const v2s32 &a_pos, const v2s32 &a_geom):
151 item_name(a_item_name),
152 parent_button(a_parent_button),
160 ImageDrawSpec(const std::string &a_name,
161 const std::string &a_item_name,
162 const v2s32 &a_pos, const v2s32 &a_geom):
164 item_name(a_item_name),
173 ImageDrawSpec(const std::string &a_name,
174 const v2s32 &a_pos, const v2s32 &a_geom, bool clip=false):
184 ImageDrawSpec(const std::string &a_name,
185 const v2s32 &a_pos, const v2s32 &a_geom, const core::rect<s32> &middle, bool clip=false):
196 ImageDrawSpec(const std::string &a_name,
207 std::string item_name;
208 gui::IGUIButton *parent_button;
211 core::rect<s32> middle;
218 FieldSpec() = default;
220 FieldSpec(const std::string &name, const std::wstring &label,
221 const std::wstring &default_text, int id) :
224 fdefault(unescape_enriched(translate_string(default_text))),
234 std::wstring fdefault;
237 FormspecFieldType ftype;
239 core::rect<s32> rect;
244 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom, irr::video::SColor a_color):
252 irr::video::SColor color;
257 TooltipSpec() = default;
258 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
259 irr::video::SColor a_color):
260 tooltip(translate_string(a_tooltip)),
266 std::wstring tooltip;
267 irr::video::SColor bgcolor;
268 irr::video::SColor color;
271 struct StaticTextSpec
278 StaticTextSpec(const std::wstring &a_text,
279 const core::rect<s32> &a_rect):
286 StaticTextSpec(const std::wstring &a_text,
287 const core::rect<s32> &a_rect,
288 gui::IGUIButton *a_parent_button):
291 parent_button(a_parent_button)
296 core::rect<s32> rect;
297 gui::IGUIButton *parent_button;
301 GUIFormSpecMenu(JoystickController *joystick,
302 gui::IGUIElement* parent, s32 id,
303 IMenuManager *menumgr,
305 ISimpleTextureSource *tsrc,
308 const std::string &formspecPrepend,
309 bool remap_dbl_click = true);
313 static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
314 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
315 const std::string &formspecPrepend);
317 void setFormSpec(const std::string &formspec_string,
318 const InventoryLocation ¤t_inventory_location)
320 m_formspec_string = formspec_string;
321 m_current_inventory_location = current_inventory_location;
322 regenerateGui(m_screensize_old);
325 const InventoryLocation &getFormspecLocation()
327 return m_current_inventory_location;
330 void setFormspecPrepend(const std::string &formspecPrepend)
332 m_formspec_prepend = formspecPrepend;
335 // form_src is deleted by this GUIFormSpecMenu
336 void setFormSource(IFormSource *form_src)
339 m_form_src = form_src;
342 // text_dst is deleted by this GUIFormSpecMenu
343 void setTextDest(TextDest *text_dst)
346 m_text_dst = text_dst;
349 void allowClose(bool value)
351 m_allowclose = value;
354 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
357 m_lockscreensize = basescreensize;
360 void removeChildren();
361 void setInitialFocus();
363 void setFocus(const std::string &elementname)
365 m_focused_element = elementname;
369 Remove and re-add (or reposition) stuff
371 void regenerateGui(v2u32 screensize);
373 ItemSpec getItemAtPos(v2s32 p) const;
374 void drawList(const ListDrawSpec &s, int layer, bool &item_hovered);
375 void drawSelectedItem();
377 void updateSelectedItem();
378 ItemStack verifySelectedItem();
380 void acceptInput(FormspecQuitMode quitmode);
381 bool preprocessEvent(const SEvent& event);
382 bool OnEvent(const SEvent& event);
384 bool pausesGame() { return doPause; }
386 GUITable* getTable(const std::string &tablename);
387 std::vector<std::string>* getDropDownValues(const std::string &name);
390 bool getAndroidUIInput();
394 v2s32 getBasePos() const
396 return padding + offset + AbsoluteRect.UpperLeftCorner;
398 std::wstring getLabelByID(s32 id);
399 std::string getNameByID(s32 id);
400 v2s32 getElementBasePos(bool absolute,
401 const std::vector<std::string> *v_pos);
402 v2s32 getRealCoordinateBasePos(bool absolute,
403 const std::vector<std::string> &v_pos);
404 v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
406 std::unordered_map<std::string, StyleSpec> theme_by_type;
407 std::unordered_map<std::string, StyleSpec> theme_by_name;
408 std::unordered_set<std::string> property_warned;
410 StyleSpec getStyleForElement(const std::string &type,
411 const std::string &name="", const std::string &parent_type="");
418 std::stack<v2f32> container_stack;
420 InventoryManager *m_invmgr;
421 ISimpleTextureSource *m_tsrc;
424 std::string m_formspec_string;
425 std::string m_formspec_prepend;
426 InventoryLocation m_current_inventory_location;
428 std::vector<ListDrawSpec> m_inventorylists;
429 std::vector<ListRingSpec> m_inventory_rings;
430 std::vector<ImageDrawSpec> m_backgrounds;
431 std::vector<ImageDrawSpec> m_images;
432 std::vector<ImageDrawSpec> m_itemimages;
433 std::vector<BoxDrawSpec> m_boxes;
434 std::unordered_map<std::string, bool> field_close_on_enter;
435 std::vector<FieldSpec> m_fields;
436 std::vector<StaticTextSpec> m_static_texts;
437 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
438 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
439 std::map<std::string, TooltipSpec> m_tooltips;
440 std::vector<std::pair<irr::core::rect<s32>, TooltipSpec>> m_tooltip_rects;
441 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
442 std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
444 ItemSpec *m_selected_item = nullptr;
445 u16 m_selected_amount = 0;
446 bool m_selected_dragging = false;
447 ItemStack m_selected_swap;
449 gui::IGUIStaticText *m_tooltip_element = nullptr;
451 u64 m_tooltip_show_delay;
452 bool m_tooltip_append_itemname;
453 u64 m_hovered_time = 0;
454 s32 m_old_tooltip_id = -1;
456 bool m_auto_place = false;
458 bool m_allowclose = true;
460 v2u32 m_lockscreensize;
464 video::SColor m_bgcolor;
465 video::SColor m_fullscreen_bgcolor;
466 video::SColor m_slotbg_n;
467 video::SColor m_slotbg_h;
468 video::SColor m_slotbordercolor;
469 video::SColor m_default_tooltip_bgcolor;
470 video::SColor m_default_tooltip_color;
473 IFormSource *m_form_src;
474 TextDest *m_text_dst;
475 u32 m_formspec_version = 0;
476 std::string m_focused_element = "";
477 JoystickController *m_joystick;
481 bool real_coordinates;
486 core::rect<s32> rect;
489 std::string focused_fieldname;
490 GUITable::TableOptions table_options;
491 GUITable::TableColumns table_columns;
492 // used to restore table selection/scroll/treeview state
493 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
503 fs_key_pendig current_keys_pending;
504 std::string current_field_enter_pending = "";
506 void parseElement(parserData* data, const std::string &element);
508 void parseSize(parserData* data, const std::string &element);
509 void parseContainer(parserData* data, const std::string &element);
510 void parseContainerEnd(parserData* data);
511 void parseList(parserData* data, const std::string &element);
512 void parseListRing(parserData* data, const std::string &element);
513 void parseCheckbox(parserData* data, const std::string &element);
514 void parseImage(parserData* data, const std::string &element);
515 void parseItemImage(parserData* data, const std::string &element);
516 void parseButton(parserData* data, const std::string &element,
517 const std::string &typ);
518 void parseBackground(parserData* data, const std::string &element);
519 void parseTableOptions(parserData* data, const std::string &element);
520 void parseTableColumns(parserData* data, const std::string &element);
521 void parseTable(parserData* data, const std::string &element);
522 void parseTextList(parserData* data, const std::string &element);
523 void parseDropDown(parserData* data, const std::string &element);
524 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
525 void parsePwdField(parserData* data, const std::string &element);
526 void parseField(parserData* data, const std::string &element, const std::string &type);
527 void createTextField(parserData *data, FieldSpec &spec,
528 core::rect<s32> &rect, bool is_multiline);
529 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
530 void parseTextArea(parserData* data,std::vector<std::string>& parts,
531 const std::string &type);
532 void parseLabel(parserData* data, const std::string &element);
533 void parseVertLabel(parserData* data, const std::string &element);
534 void parseImageButton(parserData* data, const std::string &element,
535 const std::string &type);
536 void parseItemImageButton(parserData* data, const std::string &element);
537 void parseTabHeader(parserData* data, const std::string &element);
538 void parseBox(parserData* data, const std::string &element);
539 void parseBackgroundColor(parserData* data, const std::string &element);
540 void parseListColors(parserData* data, const std::string &element);
541 void parseTooltip(parserData* data, const std::string &element);
542 bool parseVersionDirect(const std::string &data);
543 bool parseSizeDirect(parserData* data, const std::string &element);
544 void parseScrollBar(parserData* data, const std::string &element);
545 bool parsePositionDirect(parserData *data, const std::string &element);
546 void parsePosition(parserData *data, const std::string &element);
547 bool parseAnchorDirect(parserData *data, const std::string &element);
548 void parseAnchor(parserData *data, const std::string &element);
549 bool parseStyle(parserData *data, const std::string &element, bool style_type);
553 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
554 const irr::video::SColor &bgcolor);
557 * check if event is part of a double click
558 * @param event event to evaluate
559 * @return true/false if a doubleclick was detected
561 bool DoubleClickDetection(const SEvent event);
568 clickpos m_doubleclickdetect[2];
571 gui::IGUIFont *m_font = nullptr;
573 /* If true, remap a double-click (or double-tap) action to ESC. This is so
574 * that, for example, Android users can double-tap to close a formspec.
576 * This value can (currently) only be set by the class constructor
577 * and the default value for the setting is true.
579 bool m_remap_dbl_click;
582 class FormspecFormSource: public IFormSource
585 FormspecFormSource(const std::string &formspec):
590 ~FormspecFormSource() = default;
592 void setForm(const std::string &formspec)
594 m_formspec = FORMSPEC_VERSION_STRING + formspec;
597 const std::string &getForm() const
602 std::string m_formspec;