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/Optional.h"
36 #include "util/string.h"
37 #include "util/enriched_string.h"
38 #include "StyleSpec.h"
40 class InventoryManager;
41 class ISimpleTextureSource;
43 class GUIScrollContainer;
46 enum FormspecFieldType {
60 enum FormspecQuitMode {
68 virtual ~TextDest() = default;
70 // This is deprecated I guess? -celeron55
71 virtual void gotText(const std::wstring &text) {}
72 virtual void gotText(const StringMap &fields) = 0;
74 std::string m_formname;
80 virtual ~IFormSource() = default;
81 virtual const std::string &getForm() const = 0;
82 // Fill in variables in field text
83 virtual std::string resolveText(const std::string &str) { return str; }
86 class GUIFormSpecMenu : public GUIModalMenu
90 ListRingSpec() = default;
92 ListRingSpec(const InventoryLocation &a_inventoryloc,
93 const std::string &a_listname):
94 inventoryloc(a_inventoryloc),
99 InventoryLocation inventoryloc;
100 std::string listname;
105 FieldSpec() = default;
107 FieldSpec(const std::string &name, const std::wstring &label,
108 const std::wstring &default_text, s32 id, int priority = 0,
109 gui::ECURSOR_ICON cursor_icon = ECI_NORMAL) :
112 fdefault(unescape_enriched(translate_string(default_text))),
118 fcursor_icon(cursor_icon)
124 std::wstring fdefault;
127 FormspecFieldType ftype;
129 // Draw priority for formspec version < 3
131 core::rect<s32> rect;
132 gui::ECURSOR_ICON fcursor_icon;
138 TooltipSpec() = default;
139 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
140 irr::video::SColor a_color):
141 tooltip(translate_string(a_tooltip)),
147 std::wstring tooltip;
148 irr::video::SColor bgcolor;
149 irr::video::SColor color;
153 GUIFormSpecMenu(JoystickController *joystick,
154 gui::IGUIElement* parent, s32 id,
155 IMenuManager *menumgr,
157 gui::IGUIEnvironment *guienv,
158 ISimpleTextureSource *tsrc,
159 ISoundManager *sound_manager,
162 const std::string &formspecPrepend,
163 bool remap_dbl_click = true);
167 static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
168 gui::IGUIEnvironment *guienv, JoystickController *joystick, IFormSource *fs_src,
169 TextDest *txt_dest, const std::string &formspecPrepend,
170 ISoundManager *sound_manager);
172 void setFormSpec(const std::string &formspec_string,
173 const InventoryLocation ¤t_inventory_location)
175 m_formspec_string = formspec_string;
176 m_current_inventory_location = current_inventory_location;
177 m_is_form_regenerated = false;
178 regenerateGui(m_screensize_old);
181 const InventoryLocation &getFormspecLocation()
183 return m_current_inventory_location;
186 void setFormspecPrepend(const std::string &formspecPrepend)
188 m_formspec_prepend = formspecPrepend;
191 // form_src is deleted by this GUIFormSpecMenu
192 void setFormSource(IFormSource *form_src)
195 m_form_src = form_src;
198 // text_dst is deleted by this GUIFormSpecMenu
199 void setTextDest(TextDest *text_dst)
202 m_text_dst = text_dst;
205 void allowClose(bool value)
207 m_allowclose = value;
210 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
213 m_lockscreensize = basescreensize;
216 void removeTooltip();
217 void setInitialFocus();
219 void setFocus(const std::string &elementname)
221 m_focused_element = elementname;
224 Client *getClient() const
229 const GUIInventoryList::ItemSpec *getSelectedItem() const
231 return m_selected_item;
234 u16 getSelectedAmount() const
236 return m_selected_amount;
239 bool doTooltipAppendItemname() const
241 return m_tooltip_append_itemname;
244 void addHoveredItemTooltip(const std::string &name)
246 m_hovered_item_tooltips.emplace_back(name);
250 Remove and re-add (or reposition) stuff
252 void regenerateGui(v2u32 screensize);
254 GUIInventoryList::ItemSpec getItemAtPos(v2s32 p) const;
255 void drawSelectedItem();
257 void updateSelectedItem();
258 ItemStack verifySelectedItem();
260 void acceptInput(FormspecQuitMode quitmode=quit_mode_no);
261 bool preprocessEvent(const SEvent& event);
262 bool OnEvent(const SEvent& event);
264 bool pausesGame() { return doPause; }
266 GUITable* getTable(const std::string &tablename);
267 std::vector<std::string>* getDropDownValues(const std::string &name);
270 bool getAndroidUIInput();
274 v2s32 getBasePos() const
276 return padding + offset + AbsoluteRect.UpperLeftCorner;
278 std::wstring getLabelByID(s32 id);
279 std::string getNameByID(s32 id);
280 const FieldSpec *getSpecByID(s32 id);
281 v2s32 getElementBasePos(const std::vector<std::string> *v_pos);
282 v2s32 getRealCoordinateBasePos(const std::vector<std::string> &v_pos);
283 v2s32 getRealCoordinateGeometry(const std::vector<std::string> &v_geom);
284 bool precheckElement(const std::string &name, const std::string &element,
285 size_t args_min, size_t args_max, std::vector<std::string> &parts);
287 std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_type;
288 std::unordered_map<std::string, std::vector<StyleSpec>> theme_by_name;
289 std::unordered_set<std::string> property_warned;
291 StyleSpec getDefaultStyleForElement(const std::string &type,
292 const std::string &name="", const std::string &parent_type="");
293 std::array<StyleSpec, StyleSpec::NUM_STATES> getStyleForElement(const std::string &type,
294 const std::string &name="", const std::string &parent_type="");
301 std::stack<v2f32> container_stack;
303 InventoryManager *m_invmgr;
304 ISimpleTextureSource *m_tsrc;
305 ISoundManager *m_sound_manager;
308 std::string m_formspec_string;
309 std::string m_formspec_prepend;
310 InventoryLocation m_current_inventory_location;
312 // Default true because we can't control regeneration on resizing, but
313 // we can control cases when the formspec is shown intentionally.
314 bool m_is_form_regenerated = true;
316 std::vector<GUIInventoryList *> m_inventorylists;
317 std::vector<ListRingSpec> m_inventory_rings;
318 std::unordered_map<std::string, bool> field_close_on_enter;
319 std::unordered_map<std::string, bool> m_dropdown_index_event;
320 std::vector<FieldSpec> m_fields;
321 std::vector<std::pair<FieldSpec, GUITable *>> m_tables;
322 std::vector<std::pair<FieldSpec, gui::IGUICheckBox *>> m_checkboxes;
323 std::map<std::string, TooltipSpec> m_tooltips;
324 std::vector<std::pair<gui::IGUIElement *, TooltipSpec>> m_tooltip_rects;
325 std::vector<std::pair<FieldSpec, GUIScrollBar *>> m_scrollbars;
326 std::vector<std::pair<FieldSpec, std::vector<std::string>>> m_dropdowns;
327 std::vector<gui::IGUIElement *> m_clickthrough_elements;
328 std::vector<std::pair<std::string, GUIScrollContainer *>> m_scroll_containers;
330 GUIInventoryList::ItemSpec *m_selected_item = nullptr;
331 u16 m_selected_amount = 0;
332 bool m_selected_dragging = false;
333 ItemStack m_selected_swap;
335 gui::IGUIStaticText *m_tooltip_element = nullptr;
337 u64 m_tooltip_show_delay;
338 bool m_tooltip_append_itemname;
339 u64 m_hovered_time = 0;
340 s32 m_old_tooltip_id = -1;
342 bool m_auto_place = false;
344 bool m_allowclose = true;
346 v2u32 m_lockscreensize;
348 bool m_bgnonfullscreen;
350 video::SColor m_bgcolor;
351 video::SColor m_fullscreen_bgcolor;
352 video::SColor m_default_tooltip_bgcolor;
353 video::SColor m_default_tooltip_color;
356 IFormSource *m_form_src;
357 TextDest *m_text_dst;
358 std::string m_last_formname;
359 u16 m_formspec_version = 1;
360 Optional<std::string> m_focused_element = nullopt;
361 JoystickController *m_joystick;
362 bool m_show_debug = false;
366 bool real_coordinates;
367 u8 simple_field_count;
373 core::rect<s32> rect;
376 GUITable::TableOptions table_options;
377 GUITable::TableColumns table_columns;
378 gui::IGUIElement *current_parent = nullptr;
379 irr_ptr<gui::IGUIElement> background_parent;
381 GUIInventoryList::Options inventorylist_options;
387 s32 large_step = 100;
389 GUIScrollBar::ArrowVisibility arrow_visiblity = GUIScrollBar::DEFAULT;
392 // used to restore table selection/scroll/treeview state
393 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
396 struct fs_key_pending {
403 fs_key_pending current_keys_pending;
404 std::string current_field_enter_pending = "";
405 std::vector<std::string> m_hovered_item_tooltips;
409 void parseElement(parserData* data, const std::string &element);
411 void parseSize(parserData* data, const std::string &element);
412 void parseContainer(parserData* data, const std::string &element);
413 void parseContainerEnd(parserData* data);
414 void parseScrollContainer(parserData *data, const std::string &element);
415 void parseScrollContainerEnd(parserData *data);
416 void parseList(parserData* data, const std::string &element);
417 void parseListRing(parserData* data, const std::string &element);
418 void parseCheckbox(parserData* data, const std::string &element);
419 void parseImage(parserData* data, const std::string &element);
420 void parseAnimatedImage(parserData *data, const std::string &element);
421 void parseItemImage(parserData* data, const std::string &element);
422 void parseButton(parserData* data, const std::string &element,
423 const std::string &typ);
424 void parseBackground(parserData* data, const std::string &element);
425 void parseTableOptions(parserData* data, const std::string &element);
426 void parseTableColumns(parserData* data, const std::string &element);
427 void parseTable(parserData* data, const std::string &element);
428 void parseTextList(parserData* data, const std::string &element);
429 void parseDropDown(parserData* data, const std::string &element);
430 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
431 void parsePwdField(parserData* data, const std::string &element);
432 void parseField(parserData* data, const std::string &element, const std::string &type);
433 void createTextField(parserData *data, FieldSpec &spec,
434 core::rect<s32> &rect, bool is_multiline);
435 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
436 void parseTextArea(parserData* data,std::vector<std::string>& parts,
437 const std::string &type);
438 void parseHyperText(parserData *data, const std::string &element);
439 void parseLabel(parserData* data, const std::string &element);
440 void parseVertLabel(parserData* data, const std::string &element);
441 void parseImageButton(parserData* data, const std::string &element,
442 const std::string &type);
443 void parseItemImageButton(parserData* data, const std::string &element);
444 void parseTabHeader(parserData* data, const std::string &element);
445 void parseBox(parserData* data, const std::string &element);
446 void parseBackgroundColor(parserData* data, const std::string &element);
447 void parseListColors(parserData* data, const std::string &element);
448 void parseTooltip(parserData* data, const std::string &element);
449 bool parseVersionDirect(const std::string &data);
450 bool parseSizeDirect(parserData* data, const std::string &element);
451 void parseScrollBar(parserData* data, const std::string &element);
452 void parseScrollBarOptions(parserData *data, const std::string &element);
453 bool parsePositionDirect(parserData *data, const std::string &element);
454 void parsePosition(parserData *data, const std::string &element);
455 bool parseAnchorDirect(parserData *data, const std::string &element);
456 void parseAnchor(parserData *data, const std::string &element);
457 bool parsePaddingDirect(parserData *data, const std::string &element);
458 void parsePadding(parserData *data, const std::string &element);
459 bool parseStyle(parserData *data, const std::string &element, bool style_type);
460 void parseSetFocus(const std::string &element);
461 void parseModel(parserData *data, const std::string &element);
463 bool parseMiddleRect(const std::string &value, core::rect<s32> *parsed_rect);
467 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
468 const irr::video::SColor &bgcolor);
471 * In formspec version < 2 the elements were not ordered properly. Some element
472 * types were drawn before others.
473 * This function sorts the elements in the old order for backwards compatibility.
475 void legacySortElements(std::list<IGUIElement *>::iterator from);
478 gui::IGUIFont *m_font = nullptr;
481 class FormspecFormSource: public IFormSource
484 FormspecFormSource(const std::string &formspec):
489 ~FormspecFormSource() = default;
491 void setForm(const std::string &formspec)
493 m_formspec = formspec;
496 const std::string &getForm() const
501 std::string m_formspec;