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"
30 #include "guiScrollBar.h"
32 #include "network/networkprotocol.h"
33 #include "client/joystick_controller.h"
34 #include "util/string.h"
35 #include "util/enriched_string.h"
36 #include "StyleSpec.h"
38 class InventoryManager;
39 class ISimpleTextureSource;
41 class GUIScrollContainer;
44 enum FormspecFieldType {
58 enum FormspecQuitMode {
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;
136 TooltipSpec() = default;
137 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
138 irr::video::SColor a_color):
139 tooltip(translate_string(a_tooltip)),
145 std::wstring tooltip;
146 irr::video::SColor bgcolor;
147 irr::video::SColor color;
151 GUIFormSpecMenu(JoystickController *joystick,
152 gui::IGUIElement* parent, s32 id,
153 IMenuManager *menumgr,
155 gui::IGUIEnvironment *guienv,
156 ISimpleTextureSource *tsrc,
157 ISoundManager *sound_manager,
160 const std::string &formspecPrepend,
161 bool remap_dbl_click = true);
165 static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
166 gui::IGUIEnvironment *guienv, JoystickController *joystick, IFormSource *fs_src,
167 TextDest *txt_dest, const std::string &formspecPrepend,
168 ISoundManager *sound_manager);
170 void setFormSpec(const std::string &formspec_string,
171 const InventoryLocation ¤t_inventory_location)
173 m_formspec_string = formspec_string;
174 m_current_inventory_location = current_inventory_location;
175 m_is_form_regenerated = false;
176 regenerateGui(m_screensize_old);
179 const InventoryLocation &getFormspecLocation()
181 return m_current_inventory_location;
184 void setFormspecPrepend(const std::string &formspecPrepend)
186 m_formspec_prepend = formspecPrepend;
189 // form_src is deleted by this GUIFormSpecMenu
190 void setFormSource(IFormSource *form_src)
193 m_form_src = form_src;
196 // text_dst is deleted by this GUIFormSpecMenu
197 void setTextDest(TextDest *text_dst)
200 m_text_dst = text_dst;
203 void allowClose(bool value)
205 m_allowclose = value;
208 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
211 m_lockscreensize = basescreensize;
214 void removeChildren();
215 void setInitialFocus();
217 void setFocus(const std::string &elementname)
219 m_focused_element = elementname;
222 Client *getClient() const
227 const GUIInventoryList::ItemSpec *getSelectedItem() const
229 return m_selected_item;
232 const u16 getSelectedAmount() const
234 return m_selected_amount;
237 bool doTooltipAppendItemname() const
239 return m_tooltip_append_itemname;
242 void addHoveredItemTooltip(const std::string &name)
244 m_hovered_item_tooltips.emplace_back(name);
248 Remove and re-add (or reposition) stuff
250 void regenerateGui(v2u32 screensize);
252 GUIInventoryList::ItemSpec getItemAtPos(v2s32 p) const;
253 void drawSelectedItem();
255 void updateSelectedItem();
256 ItemStack verifySelectedItem();
258 void acceptInput(FormspecQuitMode quitmode=quit_mode_no);
259 bool preprocessEvent(const SEvent& event);
260 bool OnEvent(const SEvent& event);
262 bool pausesGame() { return doPause; }
264 GUITable* getTable(const std::string &tablename);
265 std::vector<std::string>* getDropDownValues(const std::string &name);
268 bool getAndroidUIInput();
272 v2s32 getBasePos() const
274 return padding + offset + AbsoluteRect.UpperLeftCorner;
276 std::wstring getLabelByID(s32 id);
277 std::string getNameByID(s32 id);
278 const FieldSpec *getSpecByID(s32 id);
279 v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
280 v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
281 v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
283 std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_type;
284 std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_name;
285 std::unordered_set<std::string> property_warned;
287 StyleSpec getDefaultStyleForElement(const std::string &type,
288 const std::string &name="", const std::string &parent_type="");
289 std::array<StyleSpec, StyleSpec::NUM_STATES> getStyleForElement(const std::string &type,
290 const std::string &name="", const std::string &parent_type="");
297 std::stack<v2f32> container_stack;
299 InventoryManager *m_invmgr;
300 ISimpleTextureSource *m_tsrc;
301 ISoundManager *m_sound_manager;
304 std::string m_formspec_string;
305 std::string m_formspec_prepend;
306 InventoryLocation m_current_inventory_location;
308 // Default true because we can't control regeneration on resizing, but
309 // we can control cases when the formspec is shown intentionally.
310 bool m_is_form_regenerated = true;
312 std::vector<GUIInventoryList *> m_inventorylists;
313 std::vector<ListRingSpec> m_inventory_rings;
314 std::vector<gui::IGUIElement *> m_backgrounds;
315 std::unordered_map<std::string, bool> field_close_on_enter;
316 std::unordered_map<std::string, bool> m_dropdown_index_event;
317 std::vector<FieldSpec> m_fields;
318 std::vector<std::pair<FieldSpec, GUITable *>> m_tables;
319 std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes;
320 std::map<std::string, TooltipSpec> m_tooltips;
321 std::vector<std::pair<gui::IGUIElement *, TooltipSpec>> m_tooltip_rects;
322 std::vector<std::pair<FieldSpec, GUIScrollBar *>> m_scrollbars;
323 std::vector<std::pair<FieldSpec, std::vector<std::string>>> m_dropdowns;
324 std::vector<gui::IGUIElement *> m_clickthrough_elements;
325 std::vector<std::pair<std::string, GUIScrollContainer *>> m_scroll_containers;
327 GUIInventoryList::ItemSpec *m_selected_item = nullptr;
328 u16 m_selected_amount = 0;
329 bool m_selected_dragging = false;
330 ItemStack m_selected_swap;
332 gui::IGUIStaticText *m_tooltip_element = nullptr;
334 u64 m_tooltip_show_delay;
335 bool m_tooltip_append_itemname;
336 u64 m_hovered_time = 0;
337 s32 m_old_tooltip_id = -1;
339 bool m_auto_place = false;
341 bool m_allowclose = true;
343 v2u32 m_lockscreensize;
345 bool m_bgnonfullscreen;
347 video::SColor m_bgcolor;
348 video::SColor m_fullscreen_bgcolor;
349 video::SColor m_default_tooltip_bgcolor;
350 video::SColor m_default_tooltip_color;
353 IFormSource *m_form_src;
354 TextDest *m_text_dst;
355 std::string m_last_formname;
356 u16 m_formspec_version = 1;
357 std::string m_focused_element = "";
358 JoystickController *m_joystick;
359 bool m_show_debug = false;
363 bool real_coordinates;
364 u8 simple_field_count;
369 core::rect<s32> rect;
372 GUITable::TableOptions table_options;
373 GUITable::TableColumns table_columns;
374 gui::IGUIElement *current_parent = nullptr;
376 GUIInventoryList::Options inventorylist_options;
382 s32 large_step = 100;
384 GUIScrollBar::ArrowVisibility arrow_visiblity = GUIScrollBar::DEFAULT;
387 // used to restore table selection/scroll/treeview state
388 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
391 struct fs_key_pending {
398 fs_key_pending current_keys_pending;
399 std::string current_field_enter_pending = "";
400 std::vector<std::string> m_hovered_item_tooltips;
402 void parseElement(parserData* data, const std::string &element);
404 void parseSize(parserData* data, const std::string &element);
405 void parseContainer(parserData* data, const std::string &element);
406 void parseContainerEnd(parserData* data);
407 void parseScrollContainer(parserData *data, const std::string &element);
408 void parseScrollContainerEnd(parserData *data);
409 void parseList(parserData* data, const std::string &element);
410 void parseListRing(parserData* data, const std::string &element);
411 void parseCheckbox(parserData* data, const std::string &element);
412 void parseImage(parserData* data, const std::string &element);
413 void parseAnimatedImage(parserData *data, const std::string &element);
414 void parseItemImage(parserData* data, const std::string &element);
415 void parseButton(parserData* data, const std::string &element,
416 const std::string &typ);
417 void parseBackground(parserData* data, const std::string &element);
418 void parseTableOptions(parserData* data, const std::string &element);
419 void parseTableColumns(parserData* data, const std::string &element);
420 void parseTable(parserData* data, const std::string &element);
421 void parseTextList(parserData* data, const std::string &element);
422 void parseDropDown(parserData* data, const std::string &element);
423 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
424 void parsePwdField(parserData* data, const std::string &element);
425 void parseField(parserData* data, const std::string &element, const std::string &type);
426 void createTextField(parserData *data, FieldSpec &spec,
427 core::rect<s32> &rect, bool is_multiline);
428 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
429 void parseTextArea(parserData* data,std::vector<std::string>& parts,
430 const std::string &type);
431 void parseHyperText(parserData *data, const std::string &element);
432 void parseLabel(parserData* data, const std::string &element);
433 void parseVertLabel(parserData* data, const std::string &element);
434 void parseImageButton(parserData* data, const std::string &element,
435 const std::string &type);
436 void parseItemImageButton(parserData* data, const std::string &element);
437 void parseTabHeader(parserData* data, const std::string &element);
438 void parseBox(parserData* data, const std::string &element);
439 void parseBackgroundColor(parserData* data, const std::string &element);
440 void parseListColors(parserData* data, const std::string &element);
441 void parseTooltip(parserData* data, const std::string &element);
442 bool parseVersionDirect(const std::string &data);
443 bool parseSizeDirect(parserData* data, const std::string &element);
444 void parseScrollBar(parserData* data, const std::string &element);
445 void parseScrollBarOptions(parserData *data, const std::string &element);
446 bool parsePositionDirect(parserData *data, const std::string &element);
447 void parsePosition(parserData *data, const std::string &element);
448 bool parseAnchorDirect(parserData *data, const std::string &element);
449 void parseAnchor(parserData *data, const std::string &element);
450 bool parseStyle(parserData *data, const std::string &element, bool style_type);
451 void parseSetFocus(const std::string &element);
452 void parseModel(parserData *data, const std::string &element);
456 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
457 const irr::video::SColor &bgcolor);
460 * In formspec version < 2 the elements were not ordered properly. Some element
461 * types were drawn before others.
462 * This function sorts the elements in the old order for backwards compatibility.
464 void legacySortElements(core::list<IGUIElement *>::Iterator from);
467 gui::IGUIFont *m_font = nullptr;
470 class FormspecFormSource: public IFormSource
473 FormspecFormSource(const std::string &formspec):
478 ~FormspecFormSource() = default;
480 void setForm(const std::string &formspec)
482 m_formspec = formspec;
485 const std::string &getForm() const
490 std::string m_formspec;