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"
28 #include "inventorymanager.h"
29 #include "modalMenu.h"
30 #include "guiInventoryList.h"
31 #include "guiScrollBar.h"
33 #include "network/networkprotocol.h"
34 #include "client/joystick_controller.h"
35 #include "util/string.h"
36 #include "util/enriched_string.h"
37 #include "StyleSpec.h"
39 class InventoryManager;
40 class ISimpleTextureSource;
42 class GUIScrollContainer;
45 enum FormspecFieldType {
59 enum FormspecQuitMode {
67 virtual ~TextDest() = default;
69 // This is deprecated I guess? -celeron55
70 virtual void gotText(const std::wstring &text) {}
71 virtual void gotText(const StringMap &fields) = 0;
73 std::string m_formname;
79 virtual ~IFormSource() = default;
80 virtual const std::string &getForm() const = 0;
81 // Fill in variables in field text
82 virtual std::string resolveText(const std::string &str) { return str; }
85 class GUIFormSpecMenu : public GUIModalMenu
89 ListRingSpec() = default;
91 ListRingSpec(const InventoryLocation &a_inventoryloc,
92 const std::string &a_listname):
93 inventoryloc(a_inventoryloc),
98 InventoryLocation inventoryloc;
104 FieldSpec() = default;
106 FieldSpec(const std::string &name, const std::wstring &label,
107 const std::wstring &default_text, s32 id, int priority = 0,
108 gui::ECURSOR_ICON cursor_icon = ECI_NORMAL) :
111 fdefault(unescape_enriched(translate_string(default_text))),
117 fcursor_icon(cursor_icon)
123 std::wstring fdefault;
126 FormspecFieldType ftype;
128 // Draw priority for formspec version < 3
130 core::rect<s32> rect;
131 gui::ECURSOR_ICON fcursor_icon;
137 TooltipSpec() = default;
138 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
139 irr::video::SColor a_color):
140 tooltip(translate_string(a_tooltip)),
146 std::wstring tooltip;
147 irr::video::SColor bgcolor;
148 irr::video::SColor color;
152 GUIFormSpecMenu(JoystickController *joystick,
153 gui::IGUIElement* parent, s32 id,
154 IMenuManager *menumgr,
156 gui::IGUIEnvironment *guienv,
157 ISimpleTextureSource *tsrc,
158 ISoundManager *sound_manager,
161 const std::string &formspecPrepend,
162 bool remap_dbl_click = true);
166 static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
167 gui::IGUIEnvironment *guienv, JoystickController *joystick, IFormSource *fs_src,
168 TextDest *txt_dest, const std::string &formspecPrepend,
169 ISoundManager *sound_manager);
171 void setFormSpec(const std::string &formspec_string,
172 const InventoryLocation ¤t_inventory_location)
174 m_formspec_string = formspec_string;
175 m_current_inventory_location = current_inventory_location;
176 m_is_form_regenerated = false;
177 regenerateGui(m_screensize_old);
180 const InventoryLocation &getFormspecLocation()
182 return m_current_inventory_location;
185 void setFormspecPrepend(const std::string &formspecPrepend)
187 m_formspec_prepend = formspecPrepend;
190 // form_src is deleted by this GUIFormSpecMenu
191 void setFormSource(IFormSource *form_src)
194 m_form_src = form_src;
197 // text_dst is deleted by this GUIFormSpecMenu
198 void setTextDest(TextDest *text_dst)
201 m_text_dst = text_dst;
204 void allowClose(bool value)
206 m_allowclose = value;
209 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
212 m_lockscreensize = basescreensize;
215 void removeTooltip();
216 void setInitialFocus();
218 void setFocus(const std::string &elementname)
220 m_focused_element = elementname;
223 Client *getClient() const
228 const GUIInventoryList::ItemSpec *getSelectedItem() const
230 return m_selected_item;
233 u16 getSelectedAmount() const
235 return m_selected_amount;
238 bool doTooltipAppendItemname() const
240 return m_tooltip_append_itemname;
243 void addHoveredItemTooltip(const std::string &name)
245 m_hovered_item_tooltips.emplace_back(name);
249 Remove and re-add (or reposition) stuff
251 void regenerateGui(v2u32 screensize);
253 GUIInventoryList::ItemSpec getItemAtPos(v2s32 p) const;
254 void drawSelectedItem();
256 void updateSelectedItem();
257 ItemStack verifySelectedItem();
259 void acceptInput(FormspecQuitMode quitmode=quit_mode_no);
260 bool preprocessEvent(const SEvent& event);
261 bool OnEvent(const SEvent& event);
263 bool pausesGame() { return doPause; }
265 GUITable* getTable(const std::string &tablename);
266 std::vector<std::string>* getDropDownValues(const std::string &name);
269 bool getAndroidUIInput();
273 v2s32 getBasePos() const
275 return padding + offset + AbsoluteRect.UpperLeftCorner;
277 std::wstring getLabelByID(s32 id);
278 std::string getNameByID(s32 id);
279 const FieldSpec *getSpecByID(s32 id);
280 v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
281 v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
282 v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
283 bool precheckElement(const std::string &name, const std::string &element,
284 size_t args_min, size_t args_max, std::vector<std::string> &parts);
286 std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_type;
287 std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_name;
288 std::unordered_set<std::string> property_warned;
290 StyleSpec getDefaultStyleForElement(const std::string &type,
291 const std::string &name="", const std::string &parent_type="");
292 std::array<StyleSpec, StyleSpec::NUM_STATES> getStyleForElement(const std::string &type,
293 const std::string &name="", const std::string &parent_type="");
300 std::stack<v2f32> container_stack;
302 InventoryManager *m_invmgr;
303 ISimpleTextureSource *m_tsrc;
304 ISoundManager *m_sound_manager;
307 std::string m_formspec_string;
308 std::string m_formspec_prepend;
309 InventoryLocation m_current_inventory_location;
311 // Default true because we can't control regeneration on resizing, but
312 // we can control cases when the formspec is shown intentionally.
313 bool m_is_form_regenerated = true;
315 std::vector<GUIInventoryList *> m_inventorylists;
316 std::vector<ListRingSpec> m_inventory_rings;
317 std::unordered_map<std::string, bool> field_close_on_enter;
318 std::unordered_map<std::string, bool> m_dropdown_index_event;
319 std::vector<FieldSpec> m_fields;
320 std::vector<std::pair<FieldSpec, GUITable *>> m_tables;
321 std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes;
322 std::map<std::string, TooltipSpec> m_tooltips;
323 std::vector<std::pair<gui::IGUIElement *, TooltipSpec>> m_tooltip_rects;
324 std::vector<std::pair<FieldSpec, GUIScrollBar *>> m_scrollbars;
325 std::vector<std::pair<FieldSpec, std::vector<std::string>>> m_dropdowns;
326 std::vector<gui::IGUIElement *> m_clickthrough_elements;
327 std::vector<std::pair<std::string, GUIScrollContainer *>> m_scroll_containers;
329 GUIInventoryList::ItemSpec *m_selected_item = nullptr;
330 u16 m_selected_amount = 0;
331 bool m_selected_dragging = false;
332 ItemStack m_selected_swap;
334 gui::IGUIStaticText *m_tooltip_element = nullptr;
336 u64 m_tooltip_show_delay;
337 bool m_tooltip_append_itemname;
338 u64 m_hovered_time = 0;
339 s32 m_old_tooltip_id = -1;
341 bool m_auto_place = false;
343 bool m_allowclose = true;
345 v2u32 m_lockscreensize;
347 bool m_bgnonfullscreen;
349 video::SColor m_bgcolor;
350 video::SColor m_fullscreen_bgcolor;
351 video::SColor m_default_tooltip_bgcolor;
352 video::SColor m_default_tooltip_color;
355 IFormSource *m_form_src;
356 TextDest *m_text_dst;
357 std::string m_last_formname;
358 u16 m_formspec_version = 1;
359 std::string m_focused_element = "";
360 JoystickController *m_joystick;
361 bool m_show_debug = false;
365 bool real_coordinates;
366 u8 simple_field_count;
372 core::rect<s32> rect;
375 GUITable::TableOptions table_options;
376 GUITable::TableColumns table_columns;
377 gui::IGUIElement *current_parent = nullptr;
378 irr_ptr<gui::IGUIElement> background_parent;
380 GUIInventoryList::Options inventorylist_options;
386 s32 large_step = 100;
388 GUIScrollBar::ArrowVisibility arrow_visiblity = GUIScrollBar::DEFAULT;
391 // used to restore table selection/scroll/treeview state
392 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
395 struct fs_key_pending {
402 fs_key_pending current_keys_pending;
403 std::string current_field_enter_pending = "";
404 std::vector<std::string> m_hovered_item_tooltips;
406 void parseElement(parserData* data, const std::string &element);
408 void parseSize(parserData* data, const std::string &element);
409 void parseContainer(parserData* data, const std::string &element);
410 void parseContainerEnd(parserData* data);
411 void parseScrollContainer(parserData *data, const std::string &element);
412 void parseScrollContainerEnd(parserData *data);
413 void parseList(parserData* data, const std::string &element);
414 void parseListRing(parserData* data, const std::string &element);
415 void parseCheckbox(parserData* data, const std::string &element);
416 void parseImage(parserData* data, const std::string &element);
417 void parseAnimatedImage(parserData *data, const std::string &element);
418 void parseItemImage(parserData* data, const std::string &element);
419 void parseButton(parserData* data, const std::string &element,
420 const std::string &typ);
421 void parseBackground(parserData* data, const std::string &element);
422 void parseTableOptions(parserData* data, const std::string &element);
423 void parseTableColumns(parserData* data, const std::string &element);
424 void parseTable(parserData* data, const std::string &element);
425 void parseTextList(parserData* data, const std::string &element);
426 void parseDropDown(parserData* data, const std::string &element);
427 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
428 void parsePwdField(parserData* data, const std::string &element);
429 void parseField(parserData* data, const std::string &element, const std::string &type);
430 void createTextField(parserData *data, FieldSpec &spec,
431 core::rect<s32> &rect, bool is_multiline);
432 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
433 void parseTextArea(parserData* data,std::vector<std::string>& parts,
434 const std::string &type);
435 void parseHyperText(parserData *data, const std::string &element);
436 void parseLabel(parserData* data, const std::string &element);
437 void parseVertLabel(parserData* data, const std::string &element);
438 void parseImageButton(parserData* data, const std::string &element,
439 const std::string &type);
440 void parseItemImageButton(parserData* data, const std::string &element);
441 void parseTabHeader(parserData* data, const std::string &element);
442 void parseBox(parserData* data, const std::string &element);
443 void parseBackgroundColor(parserData* data, const std::string &element);
444 void parseListColors(parserData* data, const std::string &element);
445 void parseTooltip(parserData* data, const std::string &element);
446 bool parseVersionDirect(const std::string &data);
447 bool parseSizeDirect(parserData* data, const std::string &element);
448 void parseScrollBar(parserData* data, const std::string &element);
449 void parseScrollBarOptions(parserData *data, const std::string &element);
450 bool parsePositionDirect(parserData *data, const std::string &element);
451 void parsePosition(parserData *data, const std::string &element);
452 bool parseAnchorDirect(parserData *data, const std::string &element);
453 void parseAnchor(parserData *data, const std::string &element);
454 bool parsePaddingDirect(parserData *data, const std::string &element);
455 void parsePadding(parserData *data, const std::string &element);
456 bool parseStyle(parserData *data, const std::string &element, bool style_type);
457 void parseSetFocus(const std::string &element);
458 void parseModel(parserData *data, const std::string &element);
462 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
463 const irr::video::SColor &bgcolor);
466 * In formspec version < 2 the elements were not ordered properly. Some element
467 * types were drawn before others.
468 * This function sorts the elements in the old order for backwards compatibility.
470 void legacySortElements(std::list<IGUIElement *>::iterator from);
473 gui::IGUIFont *m_font = nullptr;
476 class FormspecFormSource: public IFormSource
479 FormspecFormSource(const std::string &formspec):
484 ~FormspecFormSource() = default;
486 void setForm(const std::string &formspec)
488 m_formspec = formspec;
491 const std::string &getForm() const
496 std::string m_formspec;