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) :
149 fdefault(unescape_enriched(translate_string(default_text))),
160 std::wstring fdefault;
163 FormspecFieldType ftype;
165 // Draw priority for formspec version < 3
167 core::rect<s32> rect;
172 TooltipSpec() = default;
173 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
174 irr::video::SColor a_color):
175 tooltip(translate_string(a_tooltip)),
181 std::wstring tooltip;
182 irr::video::SColor bgcolor;
183 irr::video::SColor color;
186 struct StaticTextSpec
193 StaticTextSpec(const std::wstring &a_text,
194 const core::rect<s32> &a_rect):
201 StaticTextSpec(const std::wstring &a_text,
202 const core::rect<s32> &a_rect,
203 gui::IGUIButton *a_parent_button):
206 parent_button(a_parent_button)
211 core::rect<s32> rect;
212 gui::IGUIButton *parent_button;
216 GUIFormSpecMenu(JoystickController *joystick,
217 gui::IGUIElement* parent, s32 id,
218 IMenuManager *menumgr,
220 ISimpleTextureSource *tsrc,
223 const std::string &formspecPrepend,
224 bool remap_dbl_click = true);
228 static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
229 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
230 const std::string &formspecPrepend);
232 void setFormSpec(const std::string &formspec_string,
233 const InventoryLocation ¤t_inventory_location)
235 m_formspec_string = formspec_string;
236 m_current_inventory_location = current_inventory_location;
237 regenerateGui(m_screensize_old);
240 const InventoryLocation &getFormspecLocation()
242 return m_current_inventory_location;
245 void setFormspecPrepend(const std::string &formspecPrepend)
247 m_formspec_prepend = formspecPrepend;
250 // form_src is deleted by this GUIFormSpecMenu
251 void setFormSource(IFormSource *form_src)
254 m_form_src = form_src;
257 // text_dst is deleted by this GUIFormSpecMenu
258 void setTextDest(TextDest *text_dst)
261 m_text_dst = text_dst;
264 void allowClose(bool value)
266 m_allowclose = value;
269 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
272 m_lockscreensize = basescreensize;
275 void removeChildren();
276 void setInitialFocus();
278 void setFocus(const std::string &elementname)
280 m_focused_element = elementname;
284 Remove and re-add (or reposition) stuff
286 void regenerateGui(v2u32 screensize);
288 ItemSpec getItemAtPos(v2s32 p) const;
289 void drawList(const ListDrawSpec &s, int layer, bool &item_hovered);
290 void drawSelectedItem();
292 void updateSelectedItem();
293 ItemStack verifySelectedItem();
295 void acceptInput(FormspecQuitMode quitmode);
296 bool preprocessEvent(const SEvent& event);
297 bool OnEvent(const SEvent& event);
299 bool pausesGame() { return doPause; }
301 GUITable* getTable(const std::string &tablename);
302 std::vector<std::string>* getDropDownValues(const std::string &name);
305 bool getAndroidUIInput();
309 v2s32 getBasePos() const
311 return padding + offset + AbsoluteRect.UpperLeftCorner;
313 std::wstring getLabelByID(s32 id);
314 std::string getNameByID(s32 id);
315 const FieldSpec *getSpecByID(s32 id);
316 v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
317 v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
318 v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
320 std::unordered_map<std::string, StyleSpec> theme_by_type;
321 std::unordered_map<std::string, StyleSpec> theme_by_name;
322 std::unordered_set<std::string> property_warned;
324 StyleSpec getStyleForElement(const std::string &type,
325 const std::string &name="", const std::string &parent_type="");
332 std::stack<v2f32> container_stack;
334 InventoryManager *m_invmgr;
335 ISimpleTextureSource *m_tsrc;
338 std::string m_formspec_string;
339 std::string m_formspec_prepend;
340 InventoryLocation m_current_inventory_location;
342 std::vector<ListDrawSpec> m_inventorylists;
343 std::vector<ListRingSpec> m_inventory_rings;
344 std::vector<gui::IGUIElement *> m_backgrounds;
345 std::unordered_map<std::string, bool> field_close_on_enter;
346 std::vector<FieldSpec> m_fields;
347 std::vector<std::pair<FieldSpec, GUITable *>> m_tables;
348 std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes;
349 std::map<std::string, TooltipSpec> m_tooltips;
350 std::vector<std::pair<gui::IGUIElement *, TooltipSpec>> m_tooltip_rects;
351 std::vector<std::pair<FieldSpec, GUIScrollBar *>> m_scrollbars;
352 std::vector<std::pair<FieldSpec, std::vector<std::string>>> m_dropdowns;
354 ItemSpec *m_selected_item = nullptr;
355 u16 m_selected_amount = 0;
356 bool m_selected_dragging = false;
357 ItemStack m_selected_swap;
359 gui::IGUIStaticText *m_tooltip_element = nullptr;
361 u64 m_tooltip_show_delay;
362 bool m_tooltip_append_itemname;
363 u64 m_hovered_time = 0;
364 s32 m_old_tooltip_id = -1;
366 bool m_auto_place = false;
368 bool m_allowclose = true;
370 v2u32 m_lockscreensize;
374 video::SColor m_bgcolor;
375 video::SColor m_fullscreen_bgcolor;
376 video::SColor m_slotbg_n;
377 video::SColor m_slotbg_h;
378 video::SColor m_slotbordercolor;
379 video::SColor m_default_tooltip_bgcolor;
380 video::SColor m_default_tooltip_color;
384 IFormSource *m_form_src;
385 TextDest *m_text_dst;
386 u16 m_formspec_version = 1;
387 std::string m_focused_element = "";
388 JoystickController *m_joystick;
392 bool real_coordinates;
393 u8 simple_field_count;
398 core::rect<s32> rect;
401 std::string focused_fieldname;
402 GUITable::TableOptions table_options;
403 GUITable::TableColumns table_columns;
404 // used to restore table selection/scroll/treeview state
405 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
415 fs_key_pendig current_keys_pending;
416 std::string current_field_enter_pending = "";
418 void parseElement(parserData* data, const std::string &element);
420 void parseSize(parserData* data, const std::string &element);
421 void parseContainer(parserData* data, const std::string &element);
422 void parseContainerEnd(parserData* data);
423 void parseList(parserData* data, const std::string &element);
424 void parseListRing(parserData* data, const std::string &element);
425 void parseCheckbox(parserData* data, const std::string &element);
426 void parseImage(parserData* data, const std::string &element);
427 void parseItemImage(parserData* data, const std::string &element);
428 void parseButton(parserData* data, const std::string &element,
429 const std::string &typ);
430 void parseBackground(parserData* data, const std::string &element);
431 void parseTableOptions(parserData* data, const std::string &element);
432 void parseTableColumns(parserData* data, const std::string &element);
433 void parseTable(parserData* data, const std::string &element);
434 void parseTextList(parserData* data, const std::string &element);
435 void parseDropDown(parserData* data, const std::string &element);
436 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
437 void parsePwdField(parserData* data, const std::string &element);
438 void parseField(parserData* data, const std::string &element, const std::string &type);
439 void createTextField(parserData *data, FieldSpec &spec,
440 core::rect<s32> &rect, bool is_multiline);
441 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
442 void parseTextArea(parserData* data,std::vector<std::string>& parts,
443 const std::string &type);
444 void parseHyperText(parserData *data, const std::string &element);
445 void parseLabel(parserData* data, const std::string &element);
446 void parseVertLabel(parserData* data, const std::string &element);
447 void parseImageButton(parserData* data, const std::string &element,
448 const std::string &type);
449 void parseItemImageButton(parserData* data, const std::string &element);
450 void parseTabHeader(parserData* data, const std::string &element);
451 void parseBox(parserData* data, const std::string &element);
452 void parseBackgroundColor(parserData* data, const std::string &element);
453 void parseListColors(parserData* data, const std::string &element);
454 void parseTooltip(parserData* data, const std::string &element);
455 bool parseVersionDirect(const std::string &data);
456 bool parseSizeDirect(parserData* data, const std::string &element);
457 void parseScrollBar(parserData* data, const std::string &element);
458 bool parsePositionDirect(parserData *data, const std::string &element);
459 void parsePosition(parserData *data, const std::string &element);
460 bool parseAnchorDirect(parserData *data, const std::string &element);
461 void parseAnchor(parserData *data, const std::string &element);
462 bool parseStyle(parserData *data, const std::string &element, bool style_type);
466 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
467 const irr::video::SColor &bgcolor);
470 * In formspec version < 2 the elements were not ordered properly. Some element
471 * types were drawn before others.
472 * This function sorts the elements in the old order for backwards compatibility.
474 void legacySortElements(core::list<IGUIElement *>::Iterator from);
477 * check if event is part of a double click
478 * @param event event to evaluate
479 * @return true/false if a doubleclick was detected
481 bool DoubleClickDetection(const SEvent event);
488 clickpos m_doubleclickdetect[2];
491 gui::IGUIFont *m_font = nullptr;
493 /* If true, remap a double-click (or double-tap) action to ESC. This is so
494 * that, for example, Android users can double-tap to close a formspec.
496 * This value can (currently) only be set by the class constructor
497 * and the default value for the setting is true.
499 bool m_remap_dbl_click;
502 class FormspecFormSource: public IFormSource
505 FormspecFormSource(const std::string &formspec):
510 ~FormspecFormSource() = default;
512 void setForm(const std::string &formspec)
514 m_formspec = formspec;
517 const std::string &getForm() const
522 std::string m_formspec;