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) :
215 close_on_enter(false),
219 //flabel = unescape_enriched(label);
220 fdefault = unescape_enriched(default_text);
224 std::wstring fdefault;
227 bool close_on_enter; // used by text fields
228 FormspecFieldType ftype;
230 core::rect<s32> rect;
234 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
242 irr::video::SColor color;
249 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
250 irr::video::SColor a_color):
254 //tooltip = unescape_enriched(utf8_to_wide(a_tooltip));
255 tooltip = utf8_to_wide(a_tooltip);
257 std::wstring tooltip;
258 irr::video::SColor bgcolor;
259 irr::video::SColor color;
262 struct StaticTextSpec {
267 StaticTextSpec(const std::wstring &a_text,
268 const core::rect<s32> &a_rect):
272 //text = unescape_enriched(a_text);
275 StaticTextSpec(const std::wstring &a_text,
276 const core::rect<s32> &a_rect,
277 gui::IGUIButton *a_parent_button):
279 parent_button(a_parent_button)
281 //text = unescape_enriched(a_text);
285 core::rect<s32> rect;
286 gui::IGUIButton *parent_button;
290 GUIFormSpecMenu(irr::IrrlichtDevice* dev,
291 JoystickController *joystick,
292 gui::IGUIElement* parent, s32 id,
293 IMenuManager *menumgr,
294 InventoryManager *invmgr,
296 ISimpleTextureSource *tsrc,
300 bool remap_dbl_click = true);
304 void setFormSpec(const std::string &formspec_string,
305 InventoryLocation current_inventory_location)
307 m_formspec_string = formspec_string;
308 m_current_inventory_location = current_inventory_location;
309 regenerateGui(m_screensize_old);
312 // form_src is deleted by this GUIFormSpecMenu
313 void setFormSource(IFormSource *form_src)
315 if (m_form_src != NULL) {
318 m_form_src = form_src;
321 // text_dst is deleted by this GUIFormSpecMenu
322 void setTextDest(TextDest *text_dst)
324 if (m_text_dst != NULL) {
327 m_text_dst = text_dst;
330 void allowClose(bool value)
332 m_allowclose = value;
335 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
338 m_lockscreensize = basescreensize;
341 void removeChildren();
342 void setInitialFocus();
344 void setFocus(std::string &elementname)
346 m_focused_element = elementname;
350 Remove and re-add (or reposition) stuff
352 void regenerateGui(v2u32 screensize);
354 ItemSpec getItemAtPos(v2s32 p) const;
355 void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
356 void drawSelectedItem();
358 void updateSelectedItem();
359 ItemStack verifySelectedItem();
361 void acceptInput(FormspecQuitMode quitmode);
362 bool preprocessEvent(const SEvent& event);
363 bool OnEvent(const SEvent& event);
365 bool pausesGame() { return doPause; }
367 GUITable* getTable(const std::string &tablename);
368 std::vector<std::string>* getDropDownValues(const std::string &name);
371 bool getAndroidUIInput();
375 v2s32 getBasePos() const
377 return padding + offset + AbsoluteRect.UpperLeftCorner;
385 std::stack<v2s32> container_stack;
387 irr::IrrlichtDevice* m_device;
388 InventoryManager *m_invmgr;
390 ISimpleTextureSource *m_tsrc;
393 std::string m_formspec_string;
394 InventoryLocation m_current_inventory_location;
397 std::vector<ListDrawSpec> m_inventorylists;
398 std::vector<ListRingSpec> m_inventory_rings;
399 std::vector<ImageDrawSpec> m_backgrounds;
400 std::vector<ImageDrawSpec> m_images;
401 std::vector<ImageDrawSpec> m_itemimages;
402 std::vector<BoxDrawSpec> m_boxes;
403 std::vector<FieldSpec> m_fields;
404 std::vector<StaticTextSpec> m_static_texts;
405 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
406 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
407 std::map<std::string, TooltipSpec> m_tooltips;
408 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
409 std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
411 ItemSpec *m_selected_item;
412 u32 m_selected_amount;
413 bool m_selected_dragging;
415 // WARNING: BLACK MAGIC
416 // Used to guess and keep up with some special things the server can do.
417 // If name is "", no guess exists.
418 ItemStack m_selected_content_guess;
419 InventoryLocation m_selected_content_guess_inventory;
422 v2s32 m_old_pointer; // Mouse position after previous mouse event
423 gui::IGUIStaticText *m_tooltip_element;
425 u32 m_tooltip_show_delay;
427 s32 m_old_tooltip_id;
428 std::wstring m_old_tooltip;
430 bool m_rmouse_auto_place;
434 v2u32 m_lockscreensize;
438 video::SColor m_bgcolor;
439 video::SColor m_slotbg_n;
440 video::SColor m_slotbg_h;
441 video::SColor m_slotbordercolor;
442 video::SColor m_default_tooltip_bgcolor;
443 video::SColor m_default_tooltip_color;
446 IFormSource *m_form_src;
447 TextDest *m_text_dst;
448 unsigned int m_formspec_version;
449 std::string m_focused_element;
450 JoystickController *m_joystick;
456 core::rect<s32> rect;
459 std::string focused_fieldname;
460 GUITable::TableOptions table_options;
461 GUITable::TableColumns table_columns;
462 // used to restore table selection/scroll/treeview state
463 UNORDERED_MAP<std::string, GUITable::DynamicData> table_dyndata;
473 fs_key_pendig current_keys_pending;
474 std::string current_field_enter_pending;
476 void parseElement(parserData* data, std::string element);
478 void parseSize(parserData* data, std::string element);
479 void parseContainer(parserData* data, std::string element);
480 void parseContainerEnd(parserData* data);
481 void parseList(parserData* data, std::string element);
482 void parseListRing(parserData* data, std::string element);
483 void parseCheckbox(parserData* data, std::string element);
484 void parseImage(parserData* data, std::string element);
485 void parseItemImage(parserData* data,std::string element);
486 void parseButton(parserData* data,std::string element,std::string typ);
487 void parseBackground(parserData* data,std::string element);
488 void parseTableOptions(parserData* data,std::string element);
489 void parseTableColumns(parserData* data,std::string element);
490 void parseTable(parserData* data,std::string element);
491 void parseTextList(parserData* data,std::string element);
492 void parseDropDown(parserData* data,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;