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"
29 #include "guiInventoryList.h"
31 #include "network/networkprotocol.h"
32 #include "client/joystick_controller.h"
33 #include "util/string.h"
34 #include "util/enriched_string.h"
35 #include "StyleSpec.h"
37 class InventoryManager;
38 class ISimpleTextureSource;
63 virtual ~TextDest() = default;
65 // This is deprecated I guess? -celeron55
66 virtual void gotText(const std::wstring &text) {}
67 virtual void gotText(const StringMap &fields) = 0;
69 std::string m_formname;
75 virtual ~IFormSource() = default;
76 virtual const std::string &getForm() const = 0;
77 // Fill in variables in field text
78 virtual std::string resolveText(const std::string &str) { return str; }
81 class GUIFormSpecMenu : public GUIModalMenu
85 ListRingSpec() = default;
87 ListRingSpec(const InventoryLocation &a_inventoryloc,
88 const std::string &a_listname):
89 inventoryloc(a_inventoryloc),
94 InventoryLocation inventoryloc;
100 FieldSpec() = default;
102 FieldSpec(const std::string &name, const std::wstring &label,
103 const std::wstring &default_text, s32 id, int priority = 0,
104 gui::ECURSOR_ICON cursor_icon = ECI_NORMAL) :
107 fdefault(unescape_enriched(translate_string(default_text))),
113 fcursor_icon(cursor_icon)
119 std::wstring fdefault;
122 FormspecFieldType ftype;
124 // Draw priority for formspec version < 3
126 core::rect<s32> rect;
127 gui::ECURSOR_ICON fcursor_icon;
132 TooltipSpec() = default;
133 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
134 irr::video::SColor a_color):
135 tooltip(translate_string(a_tooltip)),
141 std::wstring tooltip;
142 irr::video::SColor bgcolor;
143 irr::video::SColor color;
147 GUIFormSpecMenu(JoystickController *joystick,
148 gui::IGUIElement* parent, s32 id,
149 IMenuManager *menumgr,
151 ISimpleTextureSource *tsrc,
154 const std::string &formspecPrepend,
155 bool remap_dbl_click = true);
159 static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
160 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
161 const std::string &formspecPrepend);
163 void setFormSpec(const std::string &formspec_string,
164 const InventoryLocation ¤t_inventory_location)
166 m_formspec_string = formspec_string;
167 m_current_inventory_location = current_inventory_location;
168 regenerateGui(m_screensize_old);
171 const InventoryLocation &getFormspecLocation()
173 return m_current_inventory_location;
176 void setFormspecPrepend(const std::string &formspecPrepend)
178 m_formspec_prepend = formspecPrepend;
181 // form_src is deleted by this GUIFormSpecMenu
182 void setFormSource(IFormSource *form_src)
185 m_form_src = form_src;
188 // text_dst is deleted by this GUIFormSpecMenu
189 void setTextDest(TextDest *text_dst)
192 m_text_dst = text_dst;
195 void allowClose(bool value)
197 m_allowclose = value;
200 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
203 m_lockscreensize = basescreensize;
206 void removeChildren();
207 void setInitialFocus();
209 void setFocus(const std::string &elementname)
211 m_focused_element = elementname;
214 Client *getClient() const
219 const GUIInventoryList::ItemSpec *getSelectedItem() const
221 return m_selected_item;
224 const u16 getSelectedAmount() const
226 return m_selected_amount;
229 bool doTooltipAppendItemname() const
231 return m_tooltip_append_itemname;
234 void addHoveredItemTooltip(const std::string &name)
236 m_hovered_item_tooltips.emplace_back(name);
240 Remove and re-add (or reposition) stuff
242 void regenerateGui(v2u32 screensize);
244 GUIInventoryList::ItemSpec getItemAtPos(v2s32 p) const;
245 void drawSelectedItem();
247 void updateSelectedItem();
248 ItemStack verifySelectedItem();
250 void acceptInput(FormspecQuitMode quitmode);
251 bool preprocessEvent(const SEvent& event);
252 bool OnEvent(const SEvent& event);
254 bool pausesGame() { return doPause; }
256 GUITable* getTable(const std::string &tablename);
257 std::vector<std::string>* getDropDownValues(const std::string &name);
260 bool getAndroidUIInput();
264 v2s32 getBasePos() const
266 return padding + offset + AbsoluteRect.UpperLeftCorner;
268 std::wstring getLabelByID(s32 id);
269 std::string getNameByID(s32 id);
270 const FieldSpec *getSpecByID(s32 id);
271 v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
272 v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
273 v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
275 std::unordered_map<std::string, StyleSpec> theme_by_type;
276 std::unordered_map<std::string, StyleSpec> theme_by_name;
277 std::unordered_set<std::string> property_warned;
279 StyleSpec getStyleForElement(const std::string &type,
280 const std::string &name="", const std::string &parent_type="");
287 std::stack<v2f32> container_stack;
289 InventoryManager *m_invmgr;
290 ISimpleTextureSource *m_tsrc;
293 std::string m_formspec_string;
294 std::string m_formspec_prepend;
295 InventoryLocation m_current_inventory_location;
297 std::vector<GUIInventoryList *> m_inventorylists;
298 std::vector<ListRingSpec> m_inventory_rings;
299 std::vector<gui::IGUIElement *> m_backgrounds;
300 std::unordered_map<std::string, bool> field_close_on_enter;
301 std::vector<FieldSpec> m_fields;
302 std::vector<std::pair<FieldSpec, GUITable *>> m_tables;
303 std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes;
304 std::map<std::string, TooltipSpec> m_tooltips;
305 std::vector<std::pair<gui::IGUIElement *, TooltipSpec>> m_tooltip_rects;
306 std::vector<std::pair<FieldSpec, GUIScrollBar *>> m_scrollbars;
307 std::vector<std::pair<FieldSpec, std::vector<std::string>>> m_dropdowns;
309 GUIInventoryList::ItemSpec *m_selected_item = nullptr;
310 u16 m_selected_amount = 0;
311 bool m_selected_dragging = false;
312 ItemStack m_selected_swap;
314 gui::IGUIStaticText *m_tooltip_element = nullptr;
316 u64 m_tooltip_show_delay;
317 bool m_tooltip_append_itemname;
318 u64 m_hovered_time = 0;
319 s32 m_old_tooltip_id = -1;
321 bool m_auto_place = false;
323 bool m_allowclose = true;
325 v2u32 m_lockscreensize;
327 bool m_bgnonfullscreen;
329 video::SColor m_bgcolor;
330 video::SColor m_fullscreen_bgcolor;
331 video::SColor m_default_tooltip_bgcolor;
332 video::SColor m_default_tooltip_color;
336 IFormSource *m_form_src;
337 TextDest *m_text_dst;
338 u16 m_formspec_version = 1;
339 std::string m_focused_element = "";
340 JoystickController *m_joystick;
344 bool real_coordinates;
345 u8 simple_field_count;
350 core::rect<s32> rect;
353 std::string focused_fieldname;
354 GUITable::TableOptions table_options;
355 GUITable::TableColumns table_columns;
357 GUIInventoryList::Options inventorylist_options;
363 s32 large_step = 100;
365 GUIScrollBar::ArrowVisibility arrow_visiblity = GUIScrollBar::DEFAULT;
368 // used to restore table selection/scroll/treeview state
369 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
379 fs_key_pendig current_keys_pending;
380 std::string current_field_enter_pending = "";
381 std::vector<std::string> m_hovered_item_tooltips;
383 void parseElement(parserData* data, const std::string &element);
385 void parseSize(parserData* data, const std::string &element);
386 void parseContainer(parserData* data, const std::string &element);
387 void parseContainerEnd(parserData* data);
388 void parseList(parserData* data, const std::string &element);
389 void parseListRing(parserData* data, const std::string &element);
390 void parseCheckbox(parserData* data, const std::string &element);
391 void parseImage(parserData* data, const std::string &element);
392 void parseAnimatedImage(parserData *data, const std::string &element);
393 void parseItemImage(parserData* data, const std::string &element);
394 void parseButton(parserData* data, const std::string &element,
395 const std::string &typ);
396 void parseBackground(parserData* data, const std::string &element);
397 void parseTableOptions(parserData* data, const std::string &element);
398 void parseTableColumns(parserData* data, const std::string &element);
399 void parseTable(parserData* data, const std::string &element);
400 void parseTextList(parserData* data, const std::string &element);
401 void parseDropDown(parserData* data, const std::string &element);
402 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
403 void parsePwdField(parserData* data, const std::string &element);
404 void parseField(parserData* data, const std::string &element, const std::string &type);
405 void createTextField(parserData *data, FieldSpec &spec,
406 core::rect<s32> &rect, bool is_multiline);
407 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
408 void parseTextArea(parserData* data,std::vector<std::string>& parts,
409 const std::string &type);
410 void parseHyperText(parserData *data, const std::string &element);
411 void parseLabel(parserData* data, const std::string &element);
412 void parseVertLabel(parserData* data, const std::string &element);
413 void parseImageButton(parserData* data, const std::string &element,
414 const std::string &type);
415 void parseItemImageButton(parserData* data, const std::string &element);
416 void parseTabHeader(parserData* data, const std::string &element);
417 void parseBox(parserData* data, const std::string &element);
418 void parseBackgroundColor(parserData* data, const std::string &element);
419 void parseListColors(parserData* data, const std::string &element);
420 void parseTooltip(parserData* data, const std::string &element);
421 bool parseVersionDirect(const std::string &data);
422 bool parseSizeDirect(parserData* data, const std::string &element);
423 void parseScrollBar(parserData* data, const std::string &element);
424 void parseScrollBarOptions(parserData *data, const std::string &element);
425 bool parsePositionDirect(parserData *data, const std::string &element);
426 void parsePosition(parserData *data, const std::string &element);
427 bool parseAnchorDirect(parserData *data, const std::string &element);
428 void parseAnchor(parserData *data, const std::string &element);
429 bool parseStyle(parserData *data, const std::string &element, bool style_type);
433 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
434 const irr::video::SColor &bgcolor);
437 * In formspec version < 2 the elements were not ordered properly. Some element
438 * types were drawn before others.
439 * This function sorts the elements in the old order for backwards compatibility.
441 void legacySortElements(core::list<IGUIElement *>::Iterator from);
444 * check if event is part of a double click
445 * @param event event to evaluate
446 * @return true/false if a doubleclick was detected
448 bool DoubleClickDetection(const SEvent event);
455 clickpos m_doubleclickdetect[2];
458 gui::IGUIFont *m_font = nullptr;
460 /* If true, remap a double-click (or double-tap) action to ESC. This is so
461 * that, for example, Android users can double-tap to close a formspec.
463 * This value can (currently) only be set by the class constructor
464 * and the default value for the setting is true.
466 bool m_remap_dbl_click;
469 class FormspecFormSource: public IFormSource
472 FormspecFormSource(const std::string &formspec):
477 ~FormspecFormSource() = default;
479 void setForm(const std::string &formspec)
481 m_formspec = formspec;
484 const std::string &getForm() const
489 std::string m_formspec;