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;
62 virtual ~TextDest() = default;
64 // This is deprecated I guess? -celeron55
65 virtual void gotText(const std::wstring &text) {}
66 virtual void gotText(const StringMap &fields) = 0;
68 std::string m_formname;
74 virtual ~IFormSource() = default;
75 virtual const std::string &getForm() const = 0;
76 // Fill in variables in field text
77 virtual std::string resolveText(const std::string &str) { return str; }
80 class GUIFormSpecMenu : public GUIModalMenu
84 ListRingSpec() = default;
86 ListRingSpec(const InventoryLocation &a_inventoryloc,
87 const std::string &a_listname):
88 inventoryloc(a_inventoryloc),
93 InventoryLocation inventoryloc;
99 FieldSpec() = default;
101 FieldSpec(const std::string &name, const std::wstring &label,
102 const std::wstring &default_text, s32 id, int priority = 0,
103 gui::ECURSOR_ICON cursor_icon = ECI_NORMAL) :
106 fdefault(unescape_enriched(translate_string(default_text))),
112 fcursor_icon(cursor_icon)
118 std::wstring fdefault;
121 FormspecFieldType ftype;
123 // Draw priority for formspec version < 3
125 core::rect<s32> rect;
126 gui::ECURSOR_ICON fcursor_icon;
131 TooltipSpec() = default;
132 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
133 irr::video::SColor a_color):
134 tooltip(translate_string(a_tooltip)),
140 std::wstring tooltip;
141 irr::video::SColor bgcolor;
142 irr::video::SColor color;
146 GUIFormSpecMenu(JoystickController *joystick,
147 gui::IGUIElement* parent, s32 id,
148 IMenuManager *menumgr,
150 ISimpleTextureSource *tsrc,
153 const std::string &formspecPrepend,
154 bool remap_dbl_click = true);
158 static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
159 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
160 const std::string &formspecPrepend);
162 void setFormSpec(const std::string &formspec_string,
163 const InventoryLocation ¤t_inventory_location)
165 m_formspec_string = formspec_string;
166 m_current_inventory_location = current_inventory_location;
167 regenerateGui(m_screensize_old);
170 const InventoryLocation &getFormspecLocation()
172 return m_current_inventory_location;
175 void setFormspecPrepend(const std::string &formspecPrepend)
177 m_formspec_prepend = formspecPrepend;
180 // form_src is deleted by this GUIFormSpecMenu
181 void setFormSource(IFormSource *form_src)
184 m_form_src = form_src;
187 // text_dst is deleted by this GUIFormSpecMenu
188 void setTextDest(TextDest *text_dst)
191 m_text_dst = text_dst;
194 void allowClose(bool value)
196 m_allowclose = value;
199 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
202 m_lockscreensize = basescreensize;
205 void removeChildren();
206 void setInitialFocus();
208 void setFocus(const std::string &elementname)
210 m_focused_element = elementname;
213 Client *getClient() const
218 const GUIInventoryList::ItemSpec *getSelectedItem() const
220 return m_selected_item;
223 const u16 getSelectedAmount() const
225 return m_selected_amount;
228 bool doTooltipAppendItemname() const
230 return m_tooltip_append_itemname;
233 void addHoveredItemTooltip(const std::string &name)
235 m_hovered_item_tooltips.emplace_back(name);
239 Remove and re-add (or reposition) stuff
241 void regenerateGui(v2u32 screensize);
243 GUIInventoryList::ItemSpec getItemAtPos(v2s32 p) const;
244 void drawSelectedItem();
246 void updateSelectedItem();
247 ItemStack verifySelectedItem();
249 void acceptInput(FormspecQuitMode quitmode);
250 bool preprocessEvent(const SEvent& event);
251 bool OnEvent(const SEvent& event);
253 bool pausesGame() { return doPause; }
255 GUITable* getTable(const std::string &tablename);
256 std::vector<std::string>* getDropDownValues(const std::string &name);
259 bool getAndroidUIInput();
263 v2s32 getBasePos() const
265 return padding + offset + AbsoluteRect.UpperLeftCorner;
267 std::wstring getLabelByID(s32 id);
268 std::string getNameByID(s32 id);
269 const FieldSpec *getSpecByID(s32 id);
270 v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
271 v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
272 v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
274 std::unordered_map<std::string, StyleSpec> theme_by_type;
275 std::unordered_map<std::string, StyleSpec> theme_by_name;
276 std::unordered_set<std::string> property_warned;
278 StyleSpec getStyleForElement(const std::string &type,
279 const std::string &name="", const std::string &parent_type="");
286 std::stack<v2f32> container_stack;
288 InventoryManager *m_invmgr;
289 ISimpleTextureSource *m_tsrc;
292 std::string m_formspec_string;
293 std::string m_formspec_prepend;
294 InventoryLocation m_current_inventory_location;
296 std::vector<GUIInventoryList *> m_inventorylists;
297 std::vector<ListRingSpec> m_inventory_rings;
298 std::vector<gui::IGUIElement *> m_backgrounds;
299 std::unordered_map<std::string, bool> field_close_on_enter;
300 std::vector<FieldSpec> m_fields;
301 std::vector<std::pair<FieldSpec, GUITable *>> m_tables;
302 std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes;
303 std::map<std::string, TooltipSpec> m_tooltips;
304 std::vector<std::pair<gui::IGUIElement *, TooltipSpec>> m_tooltip_rects;
305 std::vector<std::pair<FieldSpec, GUIScrollBar *>> m_scrollbars;
306 std::vector<std::pair<FieldSpec, std::vector<std::string>>> m_dropdowns;
308 GUIInventoryList::ItemSpec *m_selected_item = nullptr;
309 u16 m_selected_amount = 0;
310 bool m_selected_dragging = false;
311 ItemStack m_selected_swap;
313 gui::IGUIStaticText *m_tooltip_element = nullptr;
315 u64 m_tooltip_show_delay;
316 bool m_tooltip_append_itemname;
317 u64 m_hovered_time = 0;
318 s32 m_old_tooltip_id = -1;
320 bool m_auto_place = false;
322 bool m_allowclose = true;
324 v2u32 m_lockscreensize;
326 bool m_bgnonfullscreen;
328 video::SColor m_bgcolor;
329 video::SColor m_fullscreen_bgcolor;
330 video::SColor m_default_tooltip_bgcolor;
331 video::SColor m_default_tooltip_color;
335 IFormSource *m_form_src;
336 TextDest *m_text_dst;
337 u16 m_formspec_version = 1;
338 std::string m_focused_element = "";
339 JoystickController *m_joystick;
343 bool real_coordinates;
344 u8 simple_field_count;
349 core::rect<s32> rect;
352 std::string focused_fieldname;
353 GUITable::TableOptions table_options;
354 GUITable::TableColumns table_columns;
356 GUIInventoryList::Options inventorylist_options;
362 s32 large_step = 100;
364 GUIScrollBar::ArrowVisibility arrow_visiblity = GUIScrollBar::DEFAULT;
367 // used to restore table selection/scroll/treeview state
368 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
378 fs_key_pendig current_keys_pending;
379 std::string current_field_enter_pending = "";
380 std::vector<std::string> m_hovered_item_tooltips;
382 void parseElement(parserData* data, const std::string &element);
384 void parseSize(parserData* data, const std::string &element);
385 void parseContainer(parserData* data, const std::string &element);
386 void parseContainerEnd(parserData* data);
387 void parseList(parserData* data, const std::string &element);
388 void parseListRing(parserData* data, const std::string &element);
389 void parseCheckbox(parserData* data, const std::string &element);
390 void parseImage(parserData* data, const std::string &element);
391 void parseItemImage(parserData* data, const std::string &element);
392 void parseButton(parserData* data, const std::string &element,
393 const std::string &typ);
394 void parseBackground(parserData* data, const std::string &element);
395 void parseTableOptions(parserData* data, const std::string &element);
396 void parseTableColumns(parserData* data, const std::string &element);
397 void parseTable(parserData* data, const std::string &element);
398 void parseTextList(parserData* data, const std::string &element);
399 void parseDropDown(parserData* data, const std::string &element);
400 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
401 void parsePwdField(parserData* data, const std::string &element);
402 void parseField(parserData* data, const std::string &element, const std::string &type);
403 void createTextField(parserData *data, FieldSpec &spec,
404 core::rect<s32> &rect, bool is_multiline);
405 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
406 void parseTextArea(parserData* data,std::vector<std::string>& parts,
407 const std::string &type);
408 void parseHyperText(parserData *data, const std::string &element);
409 void parseLabel(parserData* data, const std::string &element);
410 void parseVertLabel(parserData* data, const std::string &element);
411 void parseImageButton(parserData* data, const std::string &element,
412 const std::string &type);
413 void parseItemImageButton(parserData* data, const std::string &element);
414 void parseTabHeader(parserData* data, const std::string &element);
415 void parseBox(parserData* data, const std::string &element);
416 void parseBackgroundColor(parserData* data, const std::string &element);
417 void parseListColors(parserData* data, const std::string &element);
418 void parseTooltip(parserData* data, const std::string &element);
419 bool parseVersionDirect(const std::string &data);
420 bool parseSizeDirect(parserData* data, const std::string &element);
421 void parseScrollBar(parserData* data, const std::string &element);
422 void parseScrollBarOptions(parserData *data, const std::string &element);
423 bool parsePositionDirect(parserData *data, const std::string &element);
424 void parsePosition(parserData *data, const std::string &element);
425 bool parseAnchorDirect(parserData *data, const std::string &element);
426 void parseAnchor(parserData *data, const std::string &element);
427 bool parseStyle(parserData *data, const std::string &element, bool style_type);
431 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
432 const irr::video::SColor &bgcolor);
435 * In formspec version < 2 the elements were not ordered properly. Some element
436 * types were drawn before others.
437 * This function sorts the elements in the old order for backwards compatibility.
439 void legacySortElements(core::list<IGUIElement *>::Iterator from);
442 * check if event is part of a double click
443 * @param event event to evaluate
444 * @return true/false if a doubleclick was detected
446 bool DoubleClickDetection(const SEvent event);
453 clickpos m_doubleclickdetect[2];
456 gui::IGUIFont *m_font = nullptr;
458 /* If true, remap a double-click (or double-tap) action to ESC. This is so
459 * that, for example, Android users can double-tap to close a formspec.
461 * This value can (currently) only be set by the class constructor
462 * and the default value for the setting is true.
464 bool m_remap_dbl_click;
467 class FormspecFormSource: public IFormSource
470 FormspecFormSource(const std::string &formspec):
475 ~FormspecFormSource() = default;
477 void setForm(const std::string &formspec)
479 m_formspec = formspec;
482 const std::string &getForm() const
487 std::string m_formspec;