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;
149 ImageDrawSpec(const std::string &a_name,
150 const std::string &a_item_name,
151 gui::IGUIButton *a_parent_button,
152 const v2s32 &a_pos, const v2s32 &a_geom):
154 item_name(a_item_name),
155 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),
172 ImageDrawSpec(const std::string &a_name,
173 const v2s32 &a_pos, const v2s32 &a_geom):
181 ImageDrawSpec(const std::string &a_name,
190 std::string item_name;
191 gui::IGUIButton *parent_button;
202 FieldSpec(const std::string &name, const std::wstring &label,
203 const std::wstring &default_text, int id) :
207 //flabel = unescape_enriched(label);
209 fdefault = unescape_enriched(default_text);
216 std::wstring fdefault;
219 FormspecFieldType ftype;
221 core::rect<s32> rect;
225 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
233 irr::video::SColor color;
240 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
241 irr::video::SColor a_color):
245 //tooltip = unescape_enriched(utf8_to_wide(a_tooltip));
246 tooltip = utf8_to_wide(a_tooltip);
248 std::wstring tooltip;
249 irr::video::SColor bgcolor;
250 irr::video::SColor color;
253 struct StaticTextSpec {
258 StaticTextSpec(const std::wstring &a_text,
259 const core::rect<s32> &a_rect):
263 //text = unescape_enriched(a_text);
266 StaticTextSpec(const std::wstring &a_text,
267 const core::rect<s32> &a_rect,
268 gui::IGUIButton *a_parent_button):
270 parent_button(a_parent_button)
272 //text = unescape_enriched(a_text);
276 core::rect<s32> rect;
277 gui::IGUIButton *parent_button;
281 GUIFormSpecMenu(irr::IrrlichtDevice* dev,
282 JoystickController *joystick,
283 gui::IGUIElement* parent, s32 id,
284 IMenuManager *menumgr,
285 InventoryManager *invmgr,
287 ISimpleTextureSource *tsrc,
291 bool remap_dbl_click = true);
295 void setFormSpec(const std::string &formspec_string,
296 InventoryLocation current_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)
306 if (m_form_src != NULL) {
309 m_form_src = form_src;
312 // text_dst is deleted by this GUIFormSpecMenu
313 void setTextDest(TextDest *text_dst)
315 if (m_text_dst != NULL) {
318 m_text_dst = text_dst;
321 void allowClose(bool value)
323 m_allowclose = value;
326 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
329 m_lockscreensize = basescreensize;
332 void removeChildren();
333 void setInitialFocus();
335 void setFocus(std::string &elementname)
337 m_focused_element = elementname;
341 Remove and re-add (or reposition) stuff
343 void regenerateGui(v2u32 screensize);
345 ItemSpec getItemAtPos(v2s32 p) const;
346 void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
347 void drawSelectedItem();
349 void updateSelectedItem();
350 ItemStack verifySelectedItem();
352 void acceptInput(FormspecQuitMode quitmode);
353 bool preprocessEvent(const SEvent& event);
354 bool OnEvent(const SEvent& event);
356 bool pausesGame() { return doPause; }
358 GUITable* getTable(const std::string &tablename);
359 std::vector<std::string>* getDropDownValues(const std::string &name);
362 bool getAndroidUIInput();
366 v2s32 getBasePos() const
368 return padding + offset + AbsoluteRect.UpperLeftCorner;
376 irr::IrrlichtDevice* m_device;
377 InventoryManager *m_invmgr;
379 ISimpleTextureSource *m_tsrc;
382 std::string m_formspec_string;
383 InventoryLocation m_current_inventory_location;
386 std::vector<ListDrawSpec> m_inventorylists;
387 std::vector<ListRingSpec> m_inventory_rings;
388 std::vector<ImageDrawSpec> m_backgrounds;
389 std::vector<ImageDrawSpec> m_images;
390 std::vector<ImageDrawSpec> m_itemimages;
391 std::vector<BoxDrawSpec> m_boxes;
392 std::vector<FieldSpec> m_fields;
393 std::vector<StaticTextSpec> m_static_texts;
394 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
395 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
396 std::map<std::string, TooltipSpec> m_tooltips;
397 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
398 std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
400 ItemSpec *m_selected_item;
403 u32 m_selected_amount;
404 bool m_selected_dragging;
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;
416 u32 m_tooltip_show_delay;
418 s32 m_old_tooltip_id;
419 std::wstring m_old_tooltip;
421 bool m_rmouse_auto_place;
425 v2u32 m_lockscreensize;
429 bool m_clipbackground;
430 video::SColor m_bgcolor;
431 video::SColor m_slotbg_n;
432 video::SColor m_slotbg_h;
433 video::SColor m_slotbordercolor;
434 video::SColor m_default_tooltip_bgcolor;
435 video::SColor m_default_tooltip_color;
438 IFormSource *m_form_src;
439 TextDest *m_text_dst;
440 unsigned int m_formspec_version;
441 std::string m_focused_element;
442 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::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,std::string element);
470 void parseSize(parserData* data,std::string element);
471 void parseList(parserData* data,std::string element);
472 void parseListRing(parserData* data,std::string element);
473 void parseCheckbox(parserData* data,std::string element);
474 void parseImage(parserData* data,std::string element);
475 void parseItemImage(parserData* data,std::string element);
476 void parseButton(parserData* data,std::string element,std::string typ);
477 void parseBackground(parserData* data,std::string element);
478 void parseTableOptions(parserData* data,std::string element);
479 void parseTableColumns(parserData* data,std::string element);
480 void parseTable(parserData* data,std::string element);
481 void parseTextList(parserData* data,std::string element);
482 void parseDropDown(parserData* data,std::string element);
483 void parsePwdField(parserData* data,std::string element);
484 void parseField(parserData* data,std::string element,std::string type);
485 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
486 void parseTextArea(parserData* data,std::vector<std::string>& parts,
488 void parseLabel(parserData* data,std::string element);
489 void parseVertLabel(parserData* data,std::string element);
490 void parseImageButton(parserData* data,std::string element,std::string type);
491 void parseItemImageButton(parserData* data,std::string element);
492 void parseTabHeader(parserData* data,std::string element);
493 void parseBox(parserData* data,std::string element);
494 void parseBackgroundColor(parserData* data,std::string element);
495 void parseListColors(parserData* data,std::string element);
496 void parseTooltip(parserData* data,std::string element);
497 bool parseVersionDirect(std::string data);
498 bool parseSizeDirect(parserData* data, std::string element);
499 void parseScrollBar(parserData* data, std::string element);
504 * check if event is part of a double click
505 * @param event event to evaluate
506 * @return true/false if a doubleclick was detected
508 bool DoubleClickDetection(const SEvent event);
515 clickpos m_doubleclickdetect[2];
518 gui::IGUIFont *m_font;
520 std::wstring getLabelByID(s32 id);
521 std::string getNameByID(s32 id);
524 std::string m_JavaDialogFieldName;
527 /* If true, remap a double-click (or double-tap) action to ESC. This is so
528 * that, for example, Android users can double-tap to close a formspec.
530 * This value can (currently) only be set by the class constructor
531 * and the default value for the setting is true.
533 bool m_remap_dbl_click;
537 class FormspecFormSource: public IFormSource
540 FormspecFormSource(std::string formspec)
542 m_formspec = formspec;
545 ~FormspecFormSource()
548 void setForm(std::string formspec) {
549 m_formspec = FORMSPEC_VERSION_STRING + formspec;
552 std::string getForm()
557 std::string m_formspec;