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;
61 virtual ~TextDest() = default;
63 // This is deprecated I guess? -celeron55
64 virtual void gotText(const std::wstring &text) {}
65 virtual void gotText(const StringMap &fields) = 0;
67 std::string m_formname;
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; }
79 class GUIFormSpecMenu : public GUIModalMenu
85 ItemSpec(const InventoryLocation &a_inventoryloc,
86 const std::string &a_listname,
88 inventoryloc(a_inventoryloc),
94 bool isValid() const { return i != -1; }
96 InventoryLocation inventoryloc;
103 ListDrawSpec() = default;
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),
113 start_item_i(a_start_item_i),
114 real_coordinates(a_real_coordinates)
118 InventoryLocation inventoryloc;
119 std::string listname;
123 bool real_coordinates;
128 ListRingSpec() = default;
130 ListRingSpec(const InventoryLocation &a_inventoryloc,
131 const std::string &a_listname):
132 inventoryloc(a_inventoryloc),
137 InventoryLocation inventoryloc;
138 std::string listname;
143 FieldSpec() = default;
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) :
150 fdefault(unescape_enriched(translate_string(default_text))),
156 fcursor_icon(cursor_icon)
162 std::wstring fdefault;
165 FormspecFieldType ftype;
167 // Draw priority for formspec version < 3
169 core::rect<s32> rect;
170 gui::ECURSOR_ICON fcursor_icon;
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)),
184 std::wstring tooltip;
185 irr::video::SColor bgcolor;
186 irr::video::SColor color;
189 struct StaticTextSpec
196 StaticTextSpec(const std::wstring &a_text,
197 const core::rect<s32> &a_rect):
204 StaticTextSpec(const std::wstring &a_text,
205 const core::rect<s32> &a_rect,
206 gui::IGUIButton *a_parent_button):
209 parent_button(a_parent_button)
214 core::rect<s32> rect;
215 gui::IGUIButton *parent_button;
219 GUIFormSpecMenu(JoystickController *joystick,
220 gui::IGUIElement* parent, s32 id,
221 IMenuManager *menumgr,
223 ISimpleTextureSource *tsrc,
226 const std::string &formspecPrepend,
227 bool remap_dbl_click = true);
231 static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
232 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
233 const std::string &formspecPrepend);
235 void setFormSpec(const std::string &formspec_string,
236 const InventoryLocation ¤t_inventory_location)
238 m_formspec_string = formspec_string;
239 m_current_inventory_location = current_inventory_location;
240 regenerateGui(m_screensize_old);
243 const InventoryLocation &getFormspecLocation()
245 return m_current_inventory_location;
248 void setFormspecPrepend(const std::string &formspecPrepend)
250 m_formspec_prepend = formspecPrepend;
253 // form_src is deleted by this GUIFormSpecMenu
254 void setFormSource(IFormSource *form_src)
257 m_form_src = form_src;
260 // text_dst is deleted by this GUIFormSpecMenu
261 void setTextDest(TextDest *text_dst)
264 m_text_dst = text_dst;
267 void allowClose(bool value)
269 m_allowclose = value;
272 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
275 m_lockscreensize = basescreensize;
278 void removeChildren();
279 void setInitialFocus();
281 void setFocus(const std::string &elementname)
283 m_focused_element = elementname;
287 Remove and re-add (or reposition) stuff
289 void regenerateGui(v2u32 screensize);
291 ItemSpec getItemAtPos(v2s32 p) const;
292 void drawList(const ListDrawSpec &s, int layer, bool &item_hovered);
293 void drawSelectedItem();
295 void updateSelectedItem();
296 ItemStack verifySelectedItem();
298 void acceptInput(FormspecQuitMode quitmode);
299 bool preprocessEvent(const SEvent& event);
300 bool OnEvent(const SEvent& event);
302 bool pausesGame() { return doPause; }
304 GUITable* getTable(const std::string &tablename);
305 std::vector<std::string>* getDropDownValues(const std::string &name);
308 bool getAndroidUIInput();
312 v2s32 getBasePos() const
314 return padding + offset + AbsoluteRect.UpperLeftCorner;
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);
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;
327 StyleSpec getStyleForElement(const std::string &type,
328 const std::string &name="", const std::string &parent_type="");
335 std::stack<v2f32> container_stack;
337 InventoryManager *m_invmgr;
338 ISimpleTextureSource *m_tsrc;
341 std::string m_formspec_string;
342 std::string m_formspec_prepend;
343 InventoryLocation m_current_inventory_location;
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;
357 ItemSpec *m_selected_item = nullptr;
358 u16 m_selected_amount = 0;
359 bool m_selected_dragging = false;
360 ItemStack m_selected_swap;
362 gui::IGUIStaticText *m_tooltip_element = nullptr;
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;
369 bool m_auto_place = false;
371 bool m_allowclose = true;
373 v2u32 m_lockscreensize;
375 bool m_bgnonfullscreen;
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;
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;
396 bool real_coordinates;
397 u8 simple_field_count;
402 core::rect<s32> rect;
405 std::string focused_fieldname;
406 GUITable::TableOptions table_options;
407 GUITable::TableColumns table_columns;
413 s32 large_step = 100;
415 GUIScrollBar::ArrowVisibility arrow_visiblity = GUIScrollBar::DEFAULT;
418 // used to restore table selection/scroll/treeview state
419 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
429 fs_key_pendig current_keys_pending;
430 std::string current_field_enter_pending = "";
432 void parseElement(parserData* data, const std::string &element);
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);
481 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
482 const irr::video::SColor &bgcolor);
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.
489 void legacySortElements(core::list<IGUIElement *>::Iterator from);
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
496 bool DoubleClickDetection(const SEvent event);
503 clickpos m_doubleclickdetect[2];
506 gui::IGUIFont *m_font = nullptr;
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.
511 * This value can (currently) only be set by the class constructor
512 * and the default value for the setting is true.
514 bool m_remap_dbl_click;
517 class FormspecFormSource: public IFormSource
520 FormspecFormSource(const std::string &formspec):
525 ~FormspecFormSource() = default;
527 void setForm(const std::string &formspec)
529 m_formspec = formspec;
532 const std::string &getForm() const
537 std::string m_formspec;