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.
21 #ifndef GUIINVENTORYMENU_HEADER
22 #define GUIINVENTORYMENU_HEADER
27 #include "irrlichttypes_extrabloated.h"
28 #include "inventorymanager.h"
29 #include "modalMenu.h"
31 #include "network/networkprotocol.h"
32 #include "client/joystick_controller.h"
33 #include "util/string.h"
34 #include "util/enriched_string.h"
36 class InventoryManager;
37 class ISimpleTextureSource;
58 virtual ~TextDest() {}
59 // This is deprecated I guess? -celeron55
60 virtual void gotText(const std::wstring &text) {}
61 virtual void gotText(const StringMap &fields) = 0;
63 std::string m_formname;
69 virtual ~IFormSource(){}
70 virtual std::string getForm() = 0;
71 // Fill in variables in field text
72 virtual std::string resolveText(const std::string &str) { return str; }
75 class GUIFormSpecMenu : public GUIModalMenu
84 ItemSpec(const InventoryLocation &a_inventoryloc,
85 const std::string &a_listname,
87 inventoryloc(a_inventoryloc),
93 bool isValid() const { return i != -1; }
95 InventoryLocation inventoryloc;
105 ListDrawSpec(const InventoryLocation &a_inventoryloc,
106 const std::string &a_listname,
107 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
108 inventoryloc(a_inventoryloc),
109 listname(a_listname),
112 start_item_i(a_start_item_i)
116 InventoryLocation inventoryloc;
117 std::string listname;
128 ListRingSpec(const InventoryLocation &a_inventoryloc,
129 const std::string &a_listname):
130 inventoryloc(a_inventoryloc),
135 InventoryLocation inventoryloc;
136 std::string listname;
147 ImageDrawSpec(const std::string &a_name,
148 const std::string &a_item_name,
149 gui::IGUIButton *a_parent_button,
150 const v2s32 &a_pos, const v2s32 &a_geom):
152 item_name(a_item_name),
153 parent_button(a_parent_button),
161 ImageDrawSpec(const std::string &a_name,
162 const std::string &a_item_name,
163 const v2s32 &a_pos, const v2s32 &a_geom):
165 item_name(a_item_name),
174 ImageDrawSpec(const std::string &a_name,
175 const v2s32 &a_pos, const v2s32 &a_geom, bool clip=false):
185 ImageDrawSpec(const std::string &a_name,
196 std::string item_name;
197 gui::IGUIButton *parent_button;
209 FieldSpec(const std::string &name, const std::wstring &label,
210 const std::wstring &default_text, int id) :
213 fdefault(unescape_enriched(default_text)),
223 std::wstring fdefault;
226 FormspecFieldType ftype;
228 core::rect<s32> rect;
233 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
241 irr::video::SColor color;
247 TooltipSpec(const std::string &a_tooltip, irr::video::SColor a_bgcolor,
248 irr::video::SColor a_color):
249 tooltip(utf8_to_wide(a_tooltip)),
255 std::wstring tooltip;
256 irr::video::SColor bgcolor;
257 irr::video::SColor color;
260 struct StaticTextSpec
267 StaticTextSpec(const std::wstring &a_text,
268 const core::rect<s32> &a_rect):
275 StaticTextSpec(const std::wstring &a_text,
276 const core::rect<s32> &a_rect,
277 gui::IGUIButton *a_parent_button):
280 parent_button(a_parent_button)
285 core::rect<s32> rect;
286 gui::IGUIButton *parent_button;
290 GUIFormSpecMenu(JoystickController *joystick,
291 gui::IGUIElement* parent, s32 id,
292 IMenuManager *menumgr,
294 ISimpleTextureSource *tsrc,
297 bool remap_dbl_click = true);
301 void setFormSpec(const std::string &formspec_string,
302 const InventoryLocation ¤t_inventory_location)
304 m_formspec_string = formspec_string;
305 m_current_inventory_location = current_inventory_location;
306 regenerateGui(m_screensize_old);
309 // form_src is deleted by this GUIFormSpecMenu
310 void setFormSource(IFormSource *form_src)
313 m_form_src = form_src;
316 // text_dst is deleted by this GUIFormSpecMenu
317 void setTextDest(TextDest *text_dst)
320 m_text_dst = text_dst;
323 void allowClose(bool value)
325 m_allowclose = value;
328 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
331 m_lockscreensize = basescreensize;
334 void removeChildren();
335 void setInitialFocus();
337 void setFocus(const std::string &elementname)
339 m_focused_element = elementname;
343 Remove and re-add (or reposition) stuff
345 void regenerateGui(v2u32 screensize);
347 ItemSpec getItemAtPos(v2s32 p) const;
348 void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
349 void drawSelectedItem();
351 void updateSelectedItem();
352 ItemStack verifySelectedItem();
354 void acceptInput(FormspecQuitMode quitmode);
355 bool preprocessEvent(const SEvent& event);
356 bool OnEvent(const SEvent& event);
358 bool pausesGame() { return doPause; }
360 GUITable* getTable(const std::string &tablename);
361 std::vector<std::string>* getDropDownValues(const std::string &name);
364 bool getAndroidUIInput();
368 v2s32 getBasePos() const
370 return padding + offset + AbsoluteRect.UpperLeftCorner;
378 std::stack<v2s32> container_stack;
380 InventoryManager *m_invmgr;
381 ISimpleTextureSource *m_tsrc;
384 std::string m_formspec_string;
385 InventoryLocation m_current_inventory_location;
387 std::vector<ListDrawSpec> m_inventorylists;
388 std::vector<ListRingSpec> m_inventory_rings;
389 std::vector<ImageDrawSpec> m_backgrounds;
390 std::vector<ImageDrawSpec> m_images;
391 std::vector<ImageDrawSpec> m_itemimages;
392 std::vector<BoxDrawSpec> m_boxes;
393 std::unordered_map<std::string, bool> field_close_on_enter;
394 std::vector<FieldSpec> m_fields;
395 std::vector<StaticTextSpec> m_static_texts;
396 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
397 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
398 std::map<std::string, TooltipSpec> m_tooltips;
399 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
400 std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
402 ItemSpec *m_selected_item = nullptr;
403 u32 m_selected_amount = 0;
404 bool m_selected_dragging = false;
406 // WARNING: BLACK MAGIC
407 // Used to guess and keep up with some special things the server can do.
408 // If name is "", no guess exists.
409 ItemStack m_selected_content_guess;
410 InventoryLocation m_selected_content_guess_inventory;
413 v2s32 m_old_pointer; // Mouse position after previous mouse event
414 gui::IGUIStaticText *m_tooltip_element = nullptr;
416 u64 m_tooltip_show_delay;
417 u64 m_hovered_time = 0;
418 s32 m_old_tooltip_id = -1;
420 bool m_rmouse_auto_place = false;
422 bool m_allowclose = true;
424 v2u32 m_lockscreensize;
428 video::SColor m_bgcolor;
429 video::SColor m_slotbg_n;
430 video::SColor m_slotbg_h;
431 video::SColor m_slotbordercolor;
432 video::SColor m_default_tooltip_bgcolor;
433 video::SColor m_default_tooltip_color;
436 IFormSource *m_form_src;
437 TextDest *m_text_dst;
438 u32 m_formspec_version = 0;
439 std::string m_focused_element = "";
440 JoystickController *m_joystick;
448 core::rect<s32> rect;
451 std::string focused_fieldname;
452 GUITable::TableOptions table_options;
453 GUITable::TableColumns table_columns;
454 // used to restore table selection/scroll/treeview state
455 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
465 fs_key_pendig current_keys_pending;
466 std::string current_field_enter_pending = "";
468 void parseElement(parserData* data, const std::string &element);
470 void parseSize(parserData* data, const std::string &element);
471 void parseContainer(parserData* data, const std::string &element);
472 void parseContainerEnd(parserData* data);
473 void parseList(parserData* data, const std::string &element);
474 void parseListRing(parserData* data, const std::string &element);
475 void parseCheckbox(parserData* data, const std::string &element);
476 void parseImage(parserData* data, const std::string &element);
477 void parseItemImage(parserData* data, const std::string &element);
478 void parseButton(parserData* data, const std::string &element,
479 const std::string &typ);
480 void parseBackground(parserData* data, const std::string &element);
481 void parseTableOptions(parserData* data, const std::string &element);
482 void parseTableColumns(parserData* data, const std::string &element);
483 void parseTable(parserData* data, const std::string &element);
484 void parseTextList(parserData* data, const std::string &element);
485 void parseDropDown(parserData* data, const std::string &element);
486 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
487 void parsePwdField(parserData* data, const std::string &element);
488 void parseField(parserData* data, const std::string &element, const std::string &type);
489 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
490 void parseTextArea(parserData* data,std::vector<std::string>& parts,
491 const std::string &type);
492 void parseLabel(parserData* data, const std::string &element);
493 void parseVertLabel(parserData* data, const std::string &element);
494 void parseImageButton(parserData* data, const std::string &element,
495 const std::string &type);
496 void parseItemImageButton(parserData* data, const std::string &element);
497 void parseTabHeader(parserData* data, const std::string &element);
498 void parseBox(parserData* data, const std::string &element);
499 void parseBackgroundColor(parserData* data, const std::string &element);
500 void parseListColors(parserData* data, const std::string &element);
501 void parseTooltip(parserData* data, const std::string &element);
502 bool parseVersionDirect(const std::string &data);
503 bool parseSizeDirect(parserData* data, const std::string &element);
504 void parseScrollBar(parserData* data, const std::string &element);
505 bool parsePositionDirect(parserData *data, const std::string &element);
506 void parsePosition(parserData *data, const std::string &element);
507 bool parseAnchorDirect(parserData *data, const std::string &element);
508 void parseAnchor(parserData *data, const std::string &element);
512 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
513 const irr::video::SColor &bgcolor);
516 * check if event is part of a double click
517 * @param event event to evaluate
518 * @return true/false if a doubleclick was detected
520 bool DoubleClickDetection(const SEvent event);
527 clickpos m_doubleclickdetect[2];
530 gui::IGUIFont *m_font = nullptr;
532 std::wstring getLabelByID(s32 id);
533 std::string getNameByID(s32 id);
536 std::string m_JavaDialogFieldName;
539 /* If true, remap a double-click (or double-tap) action to ESC. This is so
540 * that, for example, Android users can double-tap to close a formspec.
542 * This value can (currently) only be set by the class constructor
543 * and the default value for the setting is true.
545 bool m_remap_dbl_click;
549 class FormspecFormSource: public IFormSource
552 FormspecFormSource(const std::string &formspec):
557 ~FormspecFormSource()
561 void setForm(const std::string &formspec)
563 m_formspec = FORMSPEC_VERSION_STRING + formspec;
566 std::string getForm() { return m_formspec; }
568 std::string m_formspec;