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.
25 #include "irrlichttypes_extrabloated.h"
26 #include "inventorymanager.h"
27 #include "modalMenu.h"
29 #include "network/networkprotocol.h"
30 #include "client/joystick_controller.h"
31 #include "util/string.h"
32 #include "util/enriched_string.h"
34 class InventoryManager;
35 class ISimpleTextureSource;
56 virtual ~TextDest() = default;
58 // This is deprecated I guess? -celeron55
59 virtual void gotText(const std::wstring &text) {}
60 virtual void gotText(const StringMap &fields) = 0;
62 std::string m_formname;
68 virtual ~IFormSource() = default;
69 virtual std::string getForm() = 0;
70 // Fill in variables in field text
71 virtual std::string resolveText(const std::string &str) { return str; }
74 class GUIFormSpecMenu : public GUIModalMenu
80 ItemSpec(const InventoryLocation &a_inventoryloc,
81 const std::string &a_listname,
83 inventoryloc(a_inventoryloc),
89 bool isValid() const { return i != -1; }
91 InventoryLocation inventoryloc;
98 ListDrawSpec() = default;
100 ListDrawSpec(const InventoryLocation &a_inventoryloc,
101 const std::string &a_listname,
102 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
103 inventoryloc(a_inventoryloc),
104 listname(a_listname),
107 start_item_i(a_start_item_i)
111 InventoryLocation inventoryloc;
112 std::string listname;
120 ListRingSpec() = default;
122 ListRingSpec(const InventoryLocation &a_inventoryloc,
123 const std::string &a_listname):
124 inventoryloc(a_inventoryloc),
129 InventoryLocation inventoryloc;
130 std::string listname;
141 ImageDrawSpec(const std::string &a_name,
142 const std::string &a_item_name,
143 gui::IGUIButton *a_parent_button,
144 const v2s32 &a_pos, const v2s32 &a_geom):
146 item_name(a_item_name),
147 parent_button(a_parent_button),
155 ImageDrawSpec(const std::string &a_name,
156 const std::string &a_item_name,
157 const v2s32 &a_pos, const v2s32 &a_geom):
159 item_name(a_item_name),
168 ImageDrawSpec(const std::string &a_name,
169 const v2s32 &a_pos, const v2s32 &a_geom, bool clip=false):
179 ImageDrawSpec(const std::string &a_name,
190 std::string item_name;
191 gui::IGUIButton *parent_button;
200 FieldSpec() = default;
202 FieldSpec(const std::string &name, const std::wstring &label,
203 const std::wstring &default_text, int id) :
206 fdefault(unescape_enriched(default_text)),
216 std::wstring fdefault;
219 FormspecFieldType ftype;
221 core::rect<s32> rect;
226 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
234 irr::video::SColor color;
239 TooltipSpec() = default;
241 TooltipSpec(const std::string &a_tooltip, irr::video::SColor a_bgcolor,
242 irr::video::SColor a_color):
243 tooltip(utf8_to_wide(a_tooltip)),
249 std::wstring tooltip;
250 irr::video::SColor bgcolor;
251 irr::video::SColor color;
254 struct StaticTextSpec
261 StaticTextSpec(const std::wstring &a_text,
262 const core::rect<s32> &a_rect):
269 StaticTextSpec(const std::wstring &a_text,
270 const core::rect<s32> &a_rect,
271 gui::IGUIButton *a_parent_button):
274 parent_button(a_parent_button)
279 core::rect<s32> rect;
280 gui::IGUIButton *parent_button;
284 GUIFormSpecMenu(JoystickController *joystick,
285 gui::IGUIElement* parent, s32 id,
286 IMenuManager *menumgr,
288 ISimpleTextureSource *tsrc,
291 bool remap_dbl_click = true);
295 void setFormSpec(const std::string &formspec_string,
296 const InventoryLocation ¤t_inventory_location)
298 m_formspec_string = formspec_string;
299 m_current_inventory_location = current_inventory_location;
300 regenerateGui(m_screensize_old);
303 // form_src is deleted by this GUIFormSpecMenu
304 void setFormSource(IFormSource *form_src)
307 m_form_src = form_src;
310 // text_dst is deleted by this GUIFormSpecMenu
311 void setTextDest(TextDest *text_dst)
314 m_text_dst = text_dst;
317 void allowClose(bool value)
319 m_allowclose = value;
322 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
325 m_lockscreensize = basescreensize;
328 void removeChildren();
329 void setInitialFocus();
331 void setFocus(const std::string &elementname)
333 m_focused_element = elementname;
337 Remove and re-add (or reposition) stuff
339 void regenerateGui(v2u32 screensize);
341 ItemSpec getItemAtPos(v2s32 p) const;
342 void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
343 void drawSelectedItem();
345 void updateSelectedItem();
346 ItemStack verifySelectedItem();
348 void acceptInput(FormspecQuitMode quitmode);
349 bool preprocessEvent(const SEvent& event);
350 bool OnEvent(const SEvent& event);
352 bool pausesGame() { return doPause; }
354 GUITable* getTable(const std::string &tablename);
355 std::vector<std::string>* getDropDownValues(const std::string &name);
358 bool getAndroidUIInput();
362 v2s32 getBasePos() const
364 return padding + offset + AbsoluteRect.UpperLeftCorner;
372 std::stack<v2s32> container_stack;
374 InventoryManager *m_invmgr;
375 ISimpleTextureSource *m_tsrc;
378 std::string m_formspec_string;
379 InventoryLocation m_current_inventory_location;
381 std::vector<ListDrawSpec> m_inventorylists;
382 std::vector<ListRingSpec> m_inventory_rings;
383 std::vector<ImageDrawSpec> m_backgrounds;
384 std::vector<ImageDrawSpec> m_images;
385 std::vector<ImageDrawSpec> m_itemimages;
386 std::vector<BoxDrawSpec> m_boxes;
387 std::unordered_map<std::string, bool> field_close_on_enter;
388 std::vector<FieldSpec> m_fields;
389 std::vector<StaticTextSpec> m_static_texts;
390 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
391 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
392 std::map<std::string, TooltipSpec> m_tooltips;
393 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
394 std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
396 ItemSpec *m_selected_item = nullptr;
397 u32 m_selected_amount = 0;
398 bool m_selected_dragging = false;
400 // WARNING: BLACK MAGIC
401 // Used to guess and keep up with some special things the server can do.
402 // If name is "", no guess exists.
403 ItemStack m_selected_content_guess;
404 InventoryLocation m_selected_content_guess_inventory;
407 v2s32 m_old_pointer; // Mouse position after previous mouse event
408 gui::IGUIStaticText *m_tooltip_element = nullptr;
410 u64 m_tooltip_show_delay;
411 u64 m_hovered_time = 0;
412 s32 m_old_tooltip_id = -1;
414 bool m_rmouse_auto_place = false;
416 bool m_allowclose = true;
418 v2u32 m_lockscreensize;
422 video::SColor m_bgcolor;
423 video::SColor m_slotbg_n;
424 video::SColor m_slotbg_h;
425 video::SColor m_slotbordercolor;
426 video::SColor m_default_tooltip_bgcolor;
427 video::SColor m_default_tooltip_color;
430 IFormSource *m_form_src;
431 TextDest *m_text_dst;
432 u32 m_formspec_version = 0;
433 std::string m_focused_element = "";
434 JoystickController *m_joystick;
442 core::rect<s32> rect;
445 std::string focused_fieldname;
446 GUITable::TableOptions table_options;
447 GUITable::TableColumns table_columns;
448 // used to restore table selection/scroll/treeview state
449 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
459 fs_key_pendig current_keys_pending;
460 std::string current_field_enter_pending = "";
462 void parseElement(parserData* data, const std::string &element);
464 void parseSize(parserData* data, const std::string &element);
465 void parseContainer(parserData* data, const std::string &element);
466 void parseContainerEnd(parserData* data);
467 void parseList(parserData* data, const std::string &element);
468 void parseListRing(parserData* data, const std::string &element);
469 void parseCheckbox(parserData* data, const std::string &element);
470 void parseImage(parserData* data, const std::string &element);
471 void parseItemImage(parserData* data, const std::string &element);
472 void parseButton(parserData* data, const std::string &element,
473 const std::string &typ);
474 void parseBackground(parserData* data, const std::string &element);
475 void parseTableOptions(parserData* data, const std::string &element);
476 void parseTableColumns(parserData* data, const std::string &element);
477 void parseTable(parserData* data, const std::string &element);
478 void parseTextList(parserData* data, const std::string &element);
479 void parseDropDown(parserData* data, const std::string &element);
480 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
481 void parsePwdField(parserData* data, const std::string &element);
482 void parseField(parserData* data, const std::string &element, const std::string &type);
483 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
484 void parseTextArea(parserData* data,std::vector<std::string>& parts,
485 const std::string &type);
486 void parseLabel(parserData* data, const std::string &element);
487 void parseVertLabel(parserData* data, const std::string &element);
488 void parseImageButton(parserData* data, const std::string &element,
489 const std::string &type);
490 void parseItemImageButton(parserData* data, const std::string &element);
491 void parseTabHeader(parserData* data, const std::string &element);
492 void parseBox(parserData* data, const std::string &element);
493 void parseBackgroundColor(parserData* data, const std::string &element);
494 void parseListColors(parserData* data, const std::string &element);
495 void parseTooltip(parserData* data, const std::string &element);
496 bool parseVersionDirect(const std::string &data);
497 bool parseSizeDirect(parserData* data, const std::string &element);
498 void parseScrollBar(parserData* data, const std::string &element);
499 bool parsePositionDirect(parserData *data, const std::string &element);
500 void parsePosition(parserData *data, const std::string &element);
501 bool parseAnchorDirect(parserData *data, const std::string &element);
502 void parseAnchor(parserData *data, const std::string &element);
506 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
507 const irr::video::SColor &bgcolor);
510 * check if event is part of a double click
511 * @param event event to evaluate
512 * @return true/false if a doubleclick was detected
514 bool DoubleClickDetection(const SEvent event);
521 clickpos m_doubleclickdetect[2];
524 gui::IGUIFont *m_font = nullptr;
526 std::wstring getLabelByID(s32 id);
527 std::string getNameByID(s32 id);
530 std::string m_JavaDialogFieldName;
533 /* If true, remap a double-click (or double-tap) action to ESC. This is so
534 * that, for example, Android users can double-tap to close a formspec.
536 * This value can (currently) only be set by the class constructor
537 * and the default value for the setting is true.
539 bool m_remap_dbl_click;
543 class FormspecFormSource: public IFormSource
546 FormspecFormSource(const std::string &formspec):
551 ~FormspecFormSource() = default;
553 void setForm(const std::string &formspec)
555 m_formspec = FORMSPEC_VERSION_STRING + formspec;
558 std::string getForm() { return m_formspec; }
560 std::string m_formspec;