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;
42 class GUIScrollContainer;
66 virtual ~TextDest() = default;
68 // This is deprecated I guess? -celeron55
69 virtual void gotText(const std::wstring &text) {}
70 virtual void gotText(const StringMap &fields) = 0;
72 std::string m_formname;
78 virtual ~IFormSource() = default;
79 virtual const std::string &getForm() const = 0;
80 // Fill in variables in field text
81 virtual std::string resolveText(const std::string &str) { return str; }
84 class GUIFormSpecMenu : public GUIModalMenu
88 ListRingSpec() = default;
90 ListRingSpec(const InventoryLocation &a_inventoryloc,
91 const std::string &a_listname):
92 inventoryloc(a_inventoryloc),
97 InventoryLocation inventoryloc;
103 FieldSpec() = default;
105 FieldSpec(const std::string &name, const std::wstring &label,
106 const std::wstring &default_text, s32 id, int priority = 0,
107 gui::ECURSOR_ICON cursor_icon = ECI_NORMAL) :
110 fdefault(unescape_enriched(translate_string(default_text))),
116 fcursor_icon(cursor_icon)
122 std::wstring fdefault;
125 FormspecFieldType ftype;
127 // Draw priority for formspec version < 3
129 core::rect<s32> rect;
130 gui::ECURSOR_ICON fcursor_icon;
135 TooltipSpec() = default;
136 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
137 irr::video::SColor a_color):
138 tooltip(translate_string(a_tooltip)),
144 std::wstring tooltip;
145 irr::video::SColor bgcolor;
146 irr::video::SColor color;
150 GUIFormSpecMenu(JoystickController *joystick,
151 gui::IGUIElement* parent, s32 id,
152 IMenuManager *menumgr,
154 ISimpleTextureSource *tsrc,
157 const std::string &formspecPrepend,
158 bool remap_dbl_click = true);
162 static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
163 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
164 const std::string &formspecPrepend);
166 void setFormSpec(const std::string &formspec_string,
167 const InventoryLocation ¤t_inventory_location)
169 m_formspec_string = formspec_string;
170 m_current_inventory_location = current_inventory_location;
171 regenerateGui(m_screensize_old);
174 const InventoryLocation &getFormspecLocation()
176 return m_current_inventory_location;
179 void setFormspecPrepend(const std::string &formspecPrepend)
181 m_formspec_prepend = formspecPrepend;
184 // form_src is deleted by this GUIFormSpecMenu
185 void setFormSource(IFormSource *form_src)
188 m_form_src = form_src;
191 // text_dst is deleted by this GUIFormSpecMenu
192 void setTextDest(TextDest *text_dst)
195 m_text_dst = text_dst;
198 void allowClose(bool value)
200 m_allowclose = value;
203 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
206 m_lockscreensize = basescreensize;
209 void removeChildren();
210 void setInitialFocus();
212 void setFocus(const std::string &elementname)
214 m_focused_element = elementname;
217 Client *getClient() const
222 const GUIInventoryList::ItemSpec *getSelectedItem() const
224 return m_selected_item;
227 const u16 getSelectedAmount() const
229 return m_selected_amount;
232 bool doTooltipAppendItemname() const
234 return m_tooltip_append_itemname;
237 void addHoveredItemTooltip(const std::string &name)
239 m_hovered_item_tooltips.emplace_back(name);
243 Remove and re-add (or reposition) stuff
245 void regenerateGui(v2u32 screensize);
247 GUIInventoryList::ItemSpec getItemAtPos(v2s32 p) const;
248 void drawSelectedItem();
250 void updateSelectedItem();
251 ItemStack verifySelectedItem();
253 void acceptInput(FormspecQuitMode quitmode);
254 bool preprocessEvent(const SEvent& event);
255 bool OnEvent(const SEvent& event);
257 bool pausesGame() { return doPause; }
259 GUITable* getTable(const std::string &tablename);
260 std::vector<std::string>* getDropDownValues(const std::string &name);
263 bool getAndroidUIInput();
267 v2s32 getBasePos() const
269 return padding + offset + AbsoluteRect.UpperLeftCorner;
271 std::wstring getLabelByID(s32 id);
272 std::string getNameByID(s32 id);
273 const FieldSpec *getSpecByID(s32 id);
274 v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
275 v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
276 v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
278 std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_type;
279 std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_name;
280 std::unordered_set<std::string> property_warned;
282 StyleSpec getDefaultStyleForElement(const std::string &type,
283 const std::string &name="", const std::string &parent_type="");
284 std::array<StyleSpec, StyleSpec::NUM_STATES> getStyleForElement(const std::string &type,
285 const std::string &name="", const std::string &parent_type="");
292 std::stack<v2f32> container_stack;
294 InventoryManager *m_invmgr;
295 ISimpleTextureSource *m_tsrc;
298 std::string m_formspec_string;
299 std::string m_formspec_prepend;
300 InventoryLocation m_current_inventory_location;
302 std::vector<GUIInventoryList *> m_inventorylists;
303 std::vector<ListRingSpec> m_inventory_rings;
304 std::vector<gui::IGUIElement *> m_backgrounds;
305 std::unordered_map<std::string, bool> field_close_on_enter;
306 std::vector<FieldSpec> m_fields;
307 std::vector<std::pair<FieldSpec, GUITable *>> m_tables;
308 std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes;
309 std::map<std::string, TooltipSpec> m_tooltips;
310 std::vector<std::pair<gui::IGUIElement *, TooltipSpec>> m_tooltip_rects;
311 std::vector<std::pair<FieldSpec, GUIScrollBar *>> m_scrollbars;
312 std::vector<std::pair<FieldSpec, std::vector<std::string>>> m_dropdowns;
313 std::vector<gui::IGUIElement *> m_clickthrough_elements;
314 std::vector<std::pair<std::string, GUIScrollContainer *>> m_scroll_containers;
316 GUIInventoryList::ItemSpec *m_selected_item = nullptr;
317 u16 m_selected_amount = 0;
318 bool m_selected_dragging = false;
319 ItemStack m_selected_swap;
321 gui::IGUIStaticText *m_tooltip_element = nullptr;
323 u64 m_tooltip_show_delay;
324 bool m_tooltip_append_itemname;
325 u64 m_hovered_time = 0;
326 s32 m_old_tooltip_id = -1;
328 bool m_auto_place = false;
330 bool m_allowclose = true;
332 v2u32 m_lockscreensize;
334 bool m_bgnonfullscreen;
336 video::SColor m_bgcolor;
337 video::SColor m_fullscreen_bgcolor;
338 video::SColor m_default_tooltip_bgcolor;
339 video::SColor m_default_tooltip_color;
343 IFormSource *m_form_src;
344 TextDest *m_text_dst;
345 u16 m_formspec_version = 1;
346 std::string m_focused_element = "";
347 JoystickController *m_joystick;
348 bool m_show_debug = false;
352 bool real_coordinates;
353 u8 simple_field_count;
358 core::rect<s32> rect;
361 std::string focused_fieldname;
362 GUITable::TableOptions table_options;
363 GUITable::TableColumns table_columns;
364 gui::IGUIElement *current_parent = nullptr;
366 GUIInventoryList::Options inventorylist_options;
372 s32 large_step = 100;
374 GUIScrollBar::ArrowVisibility arrow_visiblity = GUIScrollBar::DEFAULT;
377 // used to restore table selection/scroll/treeview state
378 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
388 fs_key_pendig current_keys_pending;
389 std::string current_field_enter_pending = "";
390 std::vector<std::string> m_hovered_item_tooltips;
392 void parseElement(parserData* data, const std::string &element);
394 void parseSize(parserData* data, const std::string &element);
395 void parseContainer(parserData* data, const std::string &element);
396 void parseContainerEnd(parserData* data);
397 void parseScrollContainer(parserData *data, const std::string &element);
398 void parseScrollContainerEnd(parserData *data);
399 void parseList(parserData* data, const std::string &element);
400 void parseListRing(parserData* data, const std::string &element);
401 void parseCheckbox(parserData* data, const std::string &element);
402 void parseImage(parserData* data, const std::string &element);
403 void parseAnimatedImage(parserData *data, const std::string &element);
404 void parseItemImage(parserData* data, const std::string &element);
405 void parseButton(parserData* data, const std::string &element,
406 const std::string &typ);
407 void parseBackground(parserData* data, const std::string &element);
408 void parseTableOptions(parserData* data, const std::string &element);
409 void parseTableColumns(parserData* data, const std::string &element);
410 void parseTable(parserData* data, const std::string &element);
411 void parseTextList(parserData* data, const std::string &element);
412 void parseDropDown(parserData* data, const std::string &element);
413 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
414 void parsePwdField(parserData* data, const std::string &element);
415 void parseField(parserData* data, const std::string &element, const std::string &type);
416 void createTextField(parserData *data, FieldSpec &spec,
417 core::rect<s32> &rect, bool is_multiline);
418 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
419 void parseTextArea(parserData* data,std::vector<std::string>& parts,
420 const std::string &type);
421 void parseHyperText(parserData *data, const std::string &element);
422 void parseLabel(parserData* data, const std::string &element);
423 void parseVertLabel(parserData* data, const std::string &element);
424 void parseImageButton(parserData* data, const std::string &element,
425 const std::string &type);
426 void parseItemImageButton(parserData* data, const std::string &element);
427 void parseTabHeader(parserData* data, const std::string &element);
428 void parseBox(parserData* data, const std::string &element);
429 void parseBackgroundColor(parserData* data, const std::string &element);
430 void parseListColors(parserData* data, const std::string &element);
431 void parseTooltip(parserData* data, const std::string &element);
432 bool parseVersionDirect(const std::string &data);
433 bool parseSizeDirect(parserData* data, const std::string &element);
434 void parseScrollBar(parserData* data, const std::string &element);
435 void parseScrollBarOptions(parserData *data, const std::string &element);
436 bool parsePositionDirect(parserData *data, const std::string &element);
437 void parsePosition(parserData *data, const std::string &element);
438 bool parseAnchorDirect(parserData *data, const std::string &element);
439 void parseAnchor(parserData *data, const std::string &element);
440 bool parseStyle(parserData *data, const std::string &element, bool style_type);
444 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
445 const irr::video::SColor &bgcolor);
448 * In formspec version < 2 the elements were not ordered properly. Some element
449 * types were drawn before others.
450 * This function sorts the elements in the old order for backwards compatibility.
452 void legacySortElements(core::list<IGUIElement *>::Iterator from);
455 * check if event is part of a double click
456 * @param event event to evaluate
457 * @return true/false if a doubleclick was detected
459 bool DoubleClickDetection(const SEvent event);
466 clickpos m_doubleclickdetect[2];
469 gui::IGUIFont *m_font = nullptr;
471 /* If true, remap a double-click (or double-tap) action to ESC. This is so
472 * that, for example, Android users can double-tap to close a formspec.
474 * This value can (currently) only be set by the class constructor
475 * and the default value for the setting is true.
477 bool m_remap_dbl_click;
480 class FormspecFormSource: public IFormSource
483 FormspecFormSource(const std::string &formspec):
488 ~FormspecFormSource() = default;
490 void setForm(const std::string &formspec)
492 m_formspec = formspec;
495 const std::string &getForm() const
500 std::string m_formspec;