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 "inventory.h"
29 #include "inventorymanager.h"
30 #include "modalMenu.h"
32 #include "network/networkprotocol.h"
33 #include "client/joystick_controller.h"
34 #include "util/string.h"
35 #include "util/enriched_string.h"
38 class InventoryManager;
39 class ISimpleTextureSource;
60 virtual ~TextDest() {};
61 // This is deprecated I guess? -celeron55
62 virtual void gotText(std::wstring text){}
63 virtual void gotText(const StringMap &fields) = 0;
64 virtual void setFormName(std::string formname)
65 { m_formname = formname;};
67 std::string m_formname;
73 virtual ~IFormSource(){}
74 virtual std::string getForm() = 0;
75 // Fill in variables in field text
76 virtual std::string resolveText(std::string str){ return str; }
79 class GUIFormSpecMenu : public GUIModalMenu
87 ItemSpec(const InventoryLocation &a_inventoryloc,
88 const std::string &a_listname,
91 inventoryloc = a_inventoryloc;
92 listname = a_listname;
100 InventoryLocation inventoryloc;
101 std::string listname;
110 ListDrawSpec(const InventoryLocation &a_inventoryloc,
111 const std::string &a_listname,
112 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
113 inventoryloc(a_inventoryloc),
114 listname(a_listname),
117 start_item_i(a_start_item_i)
121 InventoryLocation inventoryloc;
122 std::string listname;
133 ListRingSpec(const InventoryLocation &a_inventoryloc,
134 const std::string &a_listname):
135 inventoryloc(a_inventoryloc),
140 InventoryLocation inventoryloc;
141 std::string listname;
151 ImageDrawSpec(const std::string &a_name,
152 const std::string &a_item_name,
153 gui::IGUIButton *a_parent_button,
154 const v2s32 &a_pos, const v2s32 &a_geom):
156 item_name(a_item_name),
157 parent_button(a_parent_button),
164 ImageDrawSpec(const std::string &a_name,
165 const std::string &a_item_name,
166 const v2s32 &a_pos, const v2s32 &a_geom):
168 item_name(a_item_name),
176 ImageDrawSpec(const std::string &a_name,
177 const v2s32 &a_pos, const v2s32 &a_geom, bool clip=false):
186 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) :
218 //flabel = unescape_enriched(label);
219 fdefault = unescape_enriched(default_text);
223 std::wstring fdefault;
226 FormspecFieldType ftype;
228 core::rect<s32> rect;
232 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
240 irr::video::SColor color;
247 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
248 irr::video::SColor a_color):
252 //tooltip = unescape_enriched(utf8_to_wide(a_tooltip));
253 tooltip = utf8_to_wide(a_tooltip);
255 std::wstring tooltip;
256 irr::video::SColor bgcolor;
257 irr::video::SColor color;
260 struct StaticTextSpec {
265 StaticTextSpec(const std::wstring &a_text,
266 const core::rect<s32> &a_rect):
270 //text = unescape_enriched(a_text);
273 StaticTextSpec(const std::wstring &a_text,
274 const core::rect<s32> &a_rect,
275 gui::IGUIButton *a_parent_button):
277 parent_button(a_parent_button)
279 //text = unescape_enriched(a_text);
283 core::rect<s32> rect;
284 gui::IGUIButton *parent_button;
288 GUIFormSpecMenu(irr::IrrlichtDevice* dev,
289 JoystickController *joystick,
290 gui::IGUIElement* parent, s32 id,
291 IMenuManager *menumgr,
292 InventoryManager *invmgr,
294 ISimpleTextureSource *tsrc,
298 bool remap_dbl_click = true);
302 void setFormSpec(const std::string &formspec_string,
303 InventoryLocation current_inventory_location)
305 m_formspec_string = formspec_string;
306 m_current_inventory_location = current_inventory_location;
307 regenerateGui(m_screensize_old);
310 // form_src is deleted by this GUIFormSpecMenu
311 void setFormSource(IFormSource *form_src)
313 if (m_form_src != NULL) {
316 m_form_src = form_src;
319 // text_dst is deleted by this GUIFormSpecMenu
320 void setTextDest(TextDest *text_dst)
322 if (m_text_dst != NULL) {
325 m_text_dst = text_dst;
328 void allowClose(bool value)
330 m_allowclose = value;
333 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
336 m_lockscreensize = basescreensize;
339 void removeChildren();
340 void setInitialFocus();
342 void setFocus(std::string &elementname)
344 m_focused_element = elementname;
348 Remove and re-add (or reposition) stuff
350 void regenerateGui(v2u32 screensize);
352 ItemSpec getItemAtPos(v2s32 p) const;
353 void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
354 void drawSelectedItem();
356 void updateSelectedItem();
357 ItemStack verifySelectedItem();
359 void acceptInput(FormspecQuitMode quitmode);
360 bool preprocessEvent(const SEvent& event);
361 bool OnEvent(const SEvent& event);
363 bool pausesGame() { return doPause; }
365 GUITable* getTable(const std::string &tablename);
366 std::vector<std::string>* getDropDownValues(const std::string &name);
369 bool getAndroidUIInput();
373 v2s32 getBasePos() const
375 return padding + offset + AbsoluteRect.UpperLeftCorner;
383 std::stack<v2s32> container_stack;
385 irr::IrrlichtDevice* m_device;
386 InventoryManager *m_invmgr;
388 ISimpleTextureSource *m_tsrc;
391 std::string m_formspec_string;
392 InventoryLocation m_current_inventory_location;
395 std::vector<ListDrawSpec> m_inventorylists;
396 std::vector<ListRingSpec> m_inventory_rings;
397 std::vector<ImageDrawSpec> m_backgrounds;
398 std::vector<ImageDrawSpec> m_images;
399 std::vector<ImageDrawSpec> m_itemimages;
400 std::vector<BoxDrawSpec> m_boxes;
401 UNORDERED_MAP<std::string, bool> field_close_on_enter;
402 std::vector<FieldSpec> m_fields;
403 std::vector<StaticTextSpec> m_static_texts;
404 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
405 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
406 std::map<std::string, TooltipSpec> m_tooltips;
407 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
408 std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
410 ItemSpec *m_selected_item;
411 u32 m_selected_amount;
412 bool m_selected_dragging;
414 // WARNING: BLACK MAGIC
415 // Used to guess and keep up with some special things the server can do.
416 // If name is "", no guess exists.
417 ItemStack m_selected_content_guess;
418 InventoryLocation m_selected_content_guess_inventory;
421 v2s32 m_old_pointer; // Mouse position after previous mouse event
422 gui::IGUIStaticText *m_tooltip_element;
424 u32 m_tooltip_show_delay;
426 s32 m_old_tooltip_id;
427 std::wstring m_old_tooltip;
429 bool m_rmouse_auto_place;
433 v2u32 m_lockscreensize;
437 video::SColor m_bgcolor;
438 video::SColor m_slotbg_n;
439 video::SColor m_slotbg_h;
440 video::SColor m_slotbordercolor;
441 video::SColor m_default_tooltip_bgcolor;
442 video::SColor m_default_tooltip_color;
445 IFormSource *m_form_src;
446 TextDest *m_text_dst;
447 unsigned int m_formspec_version;
448 std::string m_focused_element;
449 JoystickController *m_joystick;
455 core::rect<s32> rect;
458 std::string focused_fieldname;
459 GUITable::TableOptions table_options;
460 GUITable::TableColumns table_columns;
461 // used to restore table selection/scroll/treeview state
462 UNORDERED_MAP<std::string, GUITable::DynamicData> table_dyndata;
472 fs_key_pendig current_keys_pending;
473 std::string current_field_enter_pending;
475 void parseElement(parserData* data, std::string element);
477 void parseSize(parserData* data, std::string element);
478 void parseContainer(parserData* data, std::string element);
479 void parseContainerEnd(parserData* data);
480 void parseList(parserData* data, std::string element);
481 void parseListRing(parserData* data, std::string element);
482 void parseCheckbox(parserData* data, std::string element);
483 void parseImage(parserData* data, std::string element);
484 void parseItemImage(parserData* data,std::string element);
485 void parseButton(parserData* data,std::string element,std::string typ);
486 void parseBackground(parserData* data,std::string element);
487 void parseTableOptions(parserData* data,std::string element);
488 void parseTableColumns(parserData* data,std::string element);
489 void parseTable(parserData* data,std::string element);
490 void parseTextList(parserData* data,std::string element);
491 void parseDropDown(parserData* data,std::string element);
492 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
493 void parsePwdField(parserData* data,std::string element);
494 void parseField(parserData* data,std::string element,std::string type);
495 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
496 void parseTextArea(parserData* data,std::vector<std::string>& parts,
498 void parseLabel(parserData* data,std::string element);
499 void parseVertLabel(parserData* data,std::string element);
500 void parseImageButton(parserData* data,std::string element,std::string type);
501 void parseItemImageButton(parserData* data,std::string element);
502 void parseTabHeader(parserData* data,std::string element);
503 void parseBox(parserData* data,std::string element);
504 void parseBackgroundColor(parserData* data,std::string element);
505 void parseListColors(parserData* data,std::string element);
506 void parseTooltip(parserData* data,std::string element);
507 bool parseVersionDirect(std::string data);
508 bool parseSizeDirect(parserData* data, std::string element);
509 void parseScrollBar(parserData* data, std::string element);
514 * check if event is part of a double click
515 * @param event event to evaluate
516 * @return true/false if a doubleclick was detected
518 bool DoubleClickDetection(const SEvent event);
525 clickpos m_doubleclickdetect[2];
528 gui::IGUIFont *m_font;
530 std::wstring getLabelByID(s32 id);
531 std::string getNameByID(s32 id);
534 std::string m_JavaDialogFieldName;
537 /* If true, remap a double-click (or double-tap) action to ESC. This is so
538 * that, for example, Android users can double-tap to close a formspec.
540 * This value can (currently) only be set by the class constructor
541 * and the default value for the setting is true.
543 bool m_remap_dbl_click;
547 class FormspecFormSource: public IFormSource
550 FormspecFormSource(std::string formspec)
552 m_formspec = formspec;
555 ~FormspecFormSource()
558 void setForm(std::string formspec) {
559 m_formspec = FORMSPEC_VERSION_STRING + formspec;
562 std::string getForm()
567 std::string m_formspec;