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
26 #include "irrlichttypes_extrabloated.h"
27 #include "inventory.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"
37 class InventoryManager;
38 class ISimpleTextureSource;
59 virtual ~TextDest() {};
60 // This is deprecated I guess? -celeron55
61 virtual void gotText(std::wstring text){}
62 virtual void gotText(const StringMap &fields) = 0;
63 virtual void setFormName(std::string formname)
64 { m_formname = formname;};
66 std::string m_formname;
72 virtual ~IFormSource(){}
73 virtual std::string getForm() = 0;
74 // Fill in variables in field text
75 virtual std::string resolveText(std::string str){ return str; }
78 class GUIFormSpecMenu : public GUIModalMenu
86 ItemSpec(const InventoryLocation &a_inventoryloc,
87 const std::string &a_listname,
90 inventoryloc = a_inventoryloc;
91 listname = a_listname;
99 InventoryLocation inventoryloc;
100 std::string listname;
109 ListDrawSpec(const InventoryLocation &a_inventoryloc,
110 const std::string &a_listname,
111 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
112 inventoryloc(a_inventoryloc),
113 listname(a_listname),
116 start_item_i(a_start_item_i)
120 InventoryLocation inventoryloc;
121 std::string listname;
132 ListRingSpec(const InventoryLocation &a_inventoryloc,
133 const std::string &a_listname):
134 inventoryloc(a_inventoryloc),
139 InventoryLocation inventoryloc;
140 std::string listname;
150 ImageDrawSpec(const std::string &a_name,
151 const std::string &a_item_name,
152 gui::IGUIButton *a_parent_button,
153 const v2s32 &a_pos, const v2s32 &a_geom):
155 item_name(a_item_name),
156 parent_button(a_parent_button),
163 ImageDrawSpec(const std::string &a_name,
164 const std::string &a_item_name,
165 const v2s32 &a_pos, const v2s32 &a_geom):
167 item_name(a_item_name),
175 ImageDrawSpec(const std::string &a_name,
176 const v2s32 &a_pos, const v2s32 &a_geom, bool clip=false):
185 ImageDrawSpec(const std::string &a_name,
195 std::string item_name;
196 gui::IGUIButton *parent_button;
208 FieldSpec(const std::string &name, const std::wstring &label,
209 const std::wstring &default_text, int id) :
214 close_on_enter(false),
218 //flabel = unescape_enriched(label);
219 fdefault = unescape_enriched(default_text);
223 std::wstring fdefault;
226 bool close_on_enter; // used by text fields
227 FormspecFieldType ftype;
229 core::rect<s32> rect;
233 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
241 irr::video::SColor color;
248 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
249 irr::video::SColor a_color):
253 //tooltip = unescape_enriched(utf8_to_wide(a_tooltip));
254 tooltip = utf8_to_wide(a_tooltip);
256 std::wstring tooltip;
257 irr::video::SColor bgcolor;
258 irr::video::SColor color;
261 struct StaticTextSpec {
266 StaticTextSpec(const std::wstring &a_text,
267 const core::rect<s32> &a_rect):
271 //text = unescape_enriched(a_text);
274 StaticTextSpec(const std::wstring &a_text,
275 const core::rect<s32> &a_rect,
276 gui::IGUIButton *a_parent_button):
278 parent_button(a_parent_button)
280 //text = unescape_enriched(a_text);
284 core::rect<s32> rect;
285 gui::IGUIButton *parent_button;
289 GUIFormSpecMenu(irr::IrrlichtDevice* dev,
290 JoystickController *joystick,
291 gui::IGUIElement* parent, s32 id,
292 IMenuManager *menumgr,
293 InventoryManager *invmgr,
295 ISimpleTextureSource *tsrc,
299 bool remap_dbl_click = true);
303 void setFormSpec(const std::string &formspec_string,
304 InventoryLocation current_inventory_location)
306 m_formspec_string = formspec_string;
307 m_current_inventory_location = current_inventory_location;
308 regenerateGui(m_screensize_old);
311 // form_src is deleted by this GUIFormSpecMenu
312 void setFormSource(IFormSource *form_src)
314 if (m_form_src != NULL) {
317 m_form_src = form_src;
320 // text_dst is deleted by this GUIFormSpecMenu
321 void setTextDest(TextDest *text_dst)
323 if (m_text_dst != NULL) {
326 m_text_dst = text_dst;
329 void allowClose(bool value)
331 m_allowclose = value;
334 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
337 m_lockscreensize = basescreensize;
340 void removeChildren();
341 void setInitialFocus();
343 void setFocus(std::string &elementname)
345 m_focused_element = elementname;
349 Remove and re-add (or reposition) stuff
351 void regenerateGui(v2u32 screensize);
353 ItemSpec getItemAtPos(v2s32 p) const;
354 void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
355 void drawSelectedItem();
357 void updateSelectedItem();
358 ItemStack verifySelectedItem();
360 void acceptInput(FormspecQuitMode quitmode);
361 bool preprocessEvent(const SEvent& event);
362 bool OnEvent(const SEvent& event);
364 bool pausesGame() { return doPause; }
366 GUITable* getTable(const std::string &tablename);
367 std::vector<std::string>* getDropDownValues(const std::string &name);
370 bool getAndroidUIInput();
374 v2s32 getBasePos() const
376 return padding + offset + AbsoluteRect.UpperLeftCorner;
384 irr::IrrlichtDevice* m_device;
385 InventoryManager *m_invmgr;
387 ISimpleTextureSource *m_tsrc;
390 std::string m_formspec_string;
391 InventoryLocation m_current_inventory_location;
394 std::vector<ListDrawSpec> m_inventorylists;
395 std::vector<ListRingSpec> m_inventory_rings;
396 std::vector<ImageDrawSpec> m_backgrounds;
397 std::vector<ImageDrawSpec> m_images;
398 std::vector<ImageDrawSpec> m_itemimages;
399 std::vector<BoxDrawSpec> m_boxes;
400 std::vector<FieldSpec> m_fields;
401 std::vector<StaticTextSpec> m_static_texts;
402 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
403 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
404 std::map<std::string, TooltipSpec> m_tooltips;
405 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
406 std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
408 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 std::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 parseList(parserData* data,std::string element);
479 void parseListRing(parserData* data,std::string element);
480 void parseCheckbox(parserData* data,std::string element);
481 void parseImage(parserData* data,std::string element);
482 void parseItemImage(parserData* data,std::string element);
483 void parseButton(parserData* data,std::string element,std::string typ);
484 void parseBackground(parserData* data,std::string element);
485 void parseTableOptions(parserData* data,std::string element);
486 void parseTableColumns(parserData* data,std::string element);
487 void parseTable(parserData* data,std::string element);
488 void parseTextList(parserData* data,std::string element);
489 void parseDropDown(parserData* data,std::string element);
490 void parsePwdField(parserData* data,std::string element);
491 void parseField(parserData* data,std::string element,std::string type);
492 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
493 void parseTextArea(parserData* data,std::vector<std::string>& parts,
495 void parseLabel(parserData* data,std::string element);
496 void parseVertLabel(parserData* data,std::string element);
497 void parseImageButton(parserData* data,std::string element,std::string type);
498 void parseItemImageButton(parserData* data,std::string element);
499 void parseTabHeader(parserData* data,std::string element);
500 void parseBox(parserData* data,std::string element);
501 void parseBackgroundColor(parserData* data,std::string element);
502 void parseListColors(parserData* data,std::string element);
503 void parseTooltip(parserData* data,std::string element);
504 bool parseVersionDirect(std::string data);
505 bool parseSizeDirect(parserData* data, std::string element);
506 void parseScrollBar(parserData* data, std::string element);
511 * check if event is part of a double click
512 * @param event event to evaluate
513 * @return true/false if a doubleclick was detected
515 bool DoubleClickDetection(const SEvent event);
522 clickpos m_doubleclickdetect[2];
525 gui::IGUIFont *m_font;
527 std::wstring getLabelByID(s32 id);
528 std::string getNameByID(s32 id);
531 std::string m_JavaDialogFieldName;
534 /* If true, remap a double-click (or double-tap) action to ESC. This is so
535 * that, for example, Android users can double-tap to close a formspec.
537 * This value can (currently) only be set by the class constructor
538 * and the default value for the setting is true.
540 bool m_remap_dbl_click;
544 class FormspecFormSource: public IFormSource
547 FormspecFormSource(std::string formspec)
549 m_formspec = formspec;
552 ~FormspecFormSource()
555 void setForm(std::string formspec) {
556 m_formspec = FORMSPEC_VERSION_STRING + formspec;
559 std::string getForm()
564 std::string m_formspec;