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;
65 virtual ~TextDest() = default;
67 // This is deprecated I guess? -celeron55
68 virtual void gotText(const std::wstring &text) {}
69 virtual void gotText(const StringMap &fields) = 0;
71 std::string m_formname;
77 virtual ~IFormSource() = default;
78 virtual const std::string &getForm() const = 0;
79 // Fill in variables in field text
80 virtual std::string resolveText(const std::string &str) { return str; }
83 class GUIFormSpecMenu : public GUIModalMenu
87 ListRingSpec() = default;
89 ListRingSpec(const InventoryLocation &a_inventoryloc,
90 const std::string &a_listname):
91 inventoryloc(a_inventoryloc),
96 InventoryLocation inventoryloc;
102 FieldSpec() = default;
104 FieldSpec(const std::string &name, const std::wstring &label,
105 const std::wstring &default_text, s32 id, int priority = 0,
106 gui::ECURSOR_ICON cursor_icon = ECI_NORMAL) :
109 fdefault(unescape_enriched(translate_string(default_text))),
115 fcursor_icon(cursor_icon)
121 std::wstring fdefault;
124 FormspecFieldType ftype;
126 // Draw priority for formspec version < 3
128 core::rect<s32> rect;
129 gui::ECURSOR_ICON fcursor_icon;
134 TooltipSpec() = default;
135 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
136 irr::video::SColor a_color):
137 tooltip(translate_string(a_tooltip)),
143 std::wstring tooltip;
144 irr::video::SColor bgcolor;
145 irr::video::SColor color;
149 GUIFormSpecMenu(JoystickController *joystick,
150 gui::IGUIElement* parent, s32 id,
151 IMenuManager *menumgr,
153 ISimpleTextureSource *tsrc,
156 const std::string &formspecPrepend,
157 bool remap_dbl_click = true);
161 static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
162 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
163 const std::string &formspecPrepend);
165 void setFormSpec(const std::string &formspec_string,
166 const InventoryLocation ¤t_inventory_location)
168 m_formspec_string = formspec_string;
169 m_current_inventory_location = current_inventory_location;
170 regenerateGui(m_screensize_old);
173 const InventoryLocation &getFormspecLocation()
175 return m_current_inventory_location;
178 void setFormspecPrepend(const std::string &formspecPrepend)
180 m_formspec_prepend = formspecPrepend;
183 // form_src is deleted by this GUIFormSpecMenu
184 void setFormSource(IFormSource *form_src)
187 m_form_src = form_src;
190 // text_dst is deleted by this GUIFormSpecMenu
191 void setTextDest(TextDest *text_dst)
194 m_text_dst = text_dst;
197 void allowClose(bool value)
199 m_allowclose = value;
202 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
205 m_lockscreensize = basescreensize;
208 void removeChildren();
209 void setInitialFocus();
211 void setFocus(const std::string &elementname)
213 m_focused_element = elementname;
216 Client *getClient() const
221 const GUIInventoryList::ItemSpec *getSelectedItem() const
223 return m_selected_item;
226 const u16 getSelectedAmount() const
228 return m_selected_amount;
231 bool doTooltipAppendItemname() const
233 return m_tooltip_append_itemname;
236 void addHoveredItemTooltip(const std::string &name)
238 m_hovered_item_tooltips.emplace_back(name);
242 Remove and re-add (or reposition) stuff
244 void regenerateGui(v2u32 screensize);
246 GUIInventoryList::ItemSpec getItemAtPos(v2s32 p) const;
247 void drawSelectedItem();
249 void updateSelectedItem();
250 ItemStack verifySelectedItem();
252 void acceptInput(FormspecQuitMode quitmode);
253 bool preprocessEvent(const SEvent& event);
254 bool OnEvent(const SEvent& event);
256 bool pausesGame() { return doPause; }
258 GUITable* getTable(const std::string &tablename);
259 std::vector<std::string>* getDropDownValues(const std::string &name);
262 bool getAndroidUIInput();
266 v2s32 getBasePos() const
268 return padding + offset + AbsoluteRect.UpperLeftCorner;
270 std::wstring getLabelByID(s32 id);
271 std::string getNameByID(s32 id);
272 const FieldSpec *getSpecByID(s32 id);
273 v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
274 v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
275 v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
277 std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_type;
278 std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_name;
279 std::unordered_set<std::string> property_warned;
281 StyleSpec getDefaultStyleForElement(const std::string &type,
282 const std::string &name="", const std::string &parent_type="");
283 std::array<StyleSpec, StyleSpec::NUM_STATES> getStyleForElement(const std::string &type,
284 const std::string &name="", const std::string &parent_type="");
291 std::stack<v2f32> container_stack;
293 InventoryManager *m_invmgr;
294 ISimpleTextureSource *m_tsrc;
297 std::string m_formspec_string;
298 std::string m_formspec_prepend;
299 InventoryLocation m_current_inventory_location;
301 std::vector<GUIInventoryList *> m_inventorylists;
302 std::vector<ListRingSpec> m_inventory_rings;
303 std::vector<gui::IGUIElement *> m_backgrounds;
304 std::unordered_map<std::string, bool> field_close_on_enter;
305 std::vector<FieldSpec> m_fields;
306 std::vector<std::pair<FieldSpec, GUITable *>> m_tables;
307 std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes;
308 std::map<std::string, TooltipSpec> m_tooltips;
309 std::vector<std::pair<gui::IGUIElement *, TooltipSpec>> m_tooltip_rects;
310 std::vector<std::pair<FieldSpec, GUIScrollBar *>> m_scrollbars;
311 std::vector<std::pair<FieldSpec, std::vector<std::string>>> m_dropdowns;
312 std::vector<gui::IGUIElement *> m_clickthrough_elements;
314 GUIInventoryList::ItemSpec *m_selected_item = nullptr;
315 u16 m_selected_amount = 0;
316 bool m_selected_dragging = false;
317 ItemStack m_selected_swap;
319 gui::IGUIStaticText *m_tooltip_element = nullptr;
321 u64 m_tooltip_show_delay;
322 bool m_tooltip_append_itemname;
323 u64 m_hovered_time = 0;
324 s32 m_old_tooltip_id = -1;
326 bool m_auto_place = false;
328 bool m_allowclose = true;
330 v2u32 m_lockscreensize;
332 bool m_bgnonfullscreen;
334 video::SColor m_bgcolor;
335 video::SColor m_fullscreen_bgcolor;
336 video::SColor m_default_tooltip_bgcolor;
337 video::SColor m_default_tooltip_color;
341 IFormSource *m_form_src;
342 TextDest *m_text_dst;
343 u16 m_formspec_version = 1;
344 std::string m_focused_element = "";
345 JoystickController *m_joystick;
349 bool real_coordinates;
350 u8 simple_field_count;
355 core::rect<s32> rect;
358 std::string focused_fieldname;
359 GUITable::TableOptions table_options;
360 GUITable::TableColumns table_columns;
362 GUIInventoryList::Options inventorylist_options;
368 s32 large_step = 100;
370 GUIScrollBar::ArrowVisibility arrow_visiblity = GUIScrollBar::DEFAULT;
373 // used to restore table selection/scroll/treeview state
374 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
384 fs_key_pendig current_keys_pending;
385 std::string current_field_enter_pending = "";
386 std::vector<std::string> m_hovered_item_tooltips;
388 void parseElement(parserData* data, const std::string &element);
390 void parseSize(parserData* data, const std::string &element);
391 void parseContainer(parserData* data, const std::string &element);
392 void parseContainerEnd(parserData* data);
393 void parseList(parserData* data, const std::string &element);
394 void parseListRing(parserData* data, const std::string &element);
395 void parseCheckbox(parserData* data, const std::string &element);
396 void parseImage(parserData* data, const std::string &element);
397 void parseAnimatedImage(parserData *data, const std::string &element);
398 void parseItemImage(parserData* data, const std::string &element);
399 void parseButton(parserData* data, const std::string &element,
400 const std::string &typ);
401 void parseBackground(parserData* data, const std::string &element);
402 void parseTableOptions(parserData* data, const std::string &element);
403 void parseTableColumns(parserData* data, const std::string &element);
404 void parseTable(parserData* data, const std::string &element);
405 void parseTextList(parserData* data, const std::string &element);
406 void parseDropDown(parserData* data, const std::string &element);
407 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
408 void parsePwdField(parserData* data, const std::string &element);
409 void parseField(parserData* data, const std::string &element, const std::string &type);
410 void createTextField(parserData *data, FieldSpec &spec,
411 core::rect<s32> &rect, bool is_multiline);
412 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
413 void parseTextArea(parserData* data,std::vector<std::string>& parts,
414 const std::string &type);
415 void parseHyperText(parserData *data, const std::string &element);
416 void parseLabel(parserData* data, const std::string &element);
417 void parseVertLabel(parserData* data, const std::string &element);
418 void parseImageButton(parserData* data, const std::string &element,
419 const std::string &type);
420 void parseItemImageButton(parserData* data, const std::string &element);
421 void parseTabHeader(parserData* data, const std::string &element);
422 void parseBox(parserData* data, const std::string &element);
423 void parseBackgroundColor(parserData* data, const std::string &element);
424 void parseListColors(parserData* data, const std::string &element);
425 void parseTooltip(parserData* data, const std::string &element);
426 bool parseVersionDirect(const std::string &data);
427 bool parseSizeDirect(parserData* data, const std::string &element);
428 void parseScrollBar(parserData* data, const std::string &element);
429 void parseScrollBarOptions(parserData *data, const std::string &element);
430 bool parsePositionDirect(parserData *data, const std::string &element);
431 void parsePosition(parserData *data, const std::string &element);
432 bool parseAnchorDirect(parserData *data, const std::string &element);
433 void parseAnchor(parserData *data, const std::string &element);
434 bool parseStyle(parserData *data, const std::string &element, bool style_type);
438 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
439 const irr::video::SColor &bgcolor);
442 * In formspec version < 2 the elements were not ordered properly. Some element
443 * types were drawn before others.
444 * This function sorts the elements in the old order for backwards compatibility.
446 void legacySortElements(core::list<IGUIElement *>::Iterator from);
449 * check if event is part of a double click
450 * @param event event to evaluate
451 * @return true/false if a doubleclick was detected
453 bool DoubleClickDetection(const SEvent event);
460 clickpos m_doubleclickdetect[2];
463 gui::IGUIFont *m_font = nullptr;
465 /* If true, remap a double-click (or double-tap) action to ESC. This is so
466 * that, for example, Android users can double-tap to close a formspec.
468 * This value can (currently) only be set by the class constructor
469 * and the default value for the setting is true.
471 bool m_remap_dbl_click;
474 class FormspecFormSource: public IFormSource
477 FormspecFormSource(const std::string &formspec):
482 ~FormspecFormSource() = default;
484 void setForm(const std::string &formspec)
486 m_formspec = formspec;
489 const std::string &getForm() const
494 std::string m_formspec;