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 "util/string.h"
35 class InventoryManager;
36 class ISimpleTextureSource;
57 virtual ~TextDest() {};
58 // This is deprecated I guess? -celeron55
59 virtual void gotText(std::wstring text){}
60 virtual void gotText(const StringMap &fields) = 0;
61 virtual void setFormName(std::string formname)
62 { m_formname = formname;};
64 std::string m_formname;
70 virtual ~IFormSource(){}
71 virtual std::string getForm() = 0;
72 // Fill in variables in field text
73 virtual std::string resolveText(std::string str){ return str; }
76 class GUIFormSpecMenu : public GUIModalMenu
84 ItemSpec(const InventoryLocation &a_inventoryloc,
85 const std::string &a_listname,
88 inventoryloc = a_inventoryloc;
89 listname = a_listname;
97 InventoryLocation inventoryloc;
107 ListDrawSpec(const InventoryLocation &a_inventoryloc,
108 const std::string &a_listname,
109 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
110 inventoryloc(a_inventoryloc),
111 listname(a_listname),
114 start_item_i(a_start_item_i)
118 InventoryLocation inventoryloc;
119 std::string listname;
130 ListRingSpec(const InventoryLocation &a_inventoryloc,
131 const std::string &a_listname):
132 inventoryloc(a_inventoryloc),
137 InventoryLocation inventoryloc;
138 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),
159 ImageDrawSpec(const std::string &a_name,
160 const std::string &a_item_name,
161 const v2s32 &a_pos, const v2s32 &a_geom):
163 item_name(a_item_name),
170 ImageDrawSpec(const std::string &a_name,
171 const v2s32 &a_pos, const v2s32 &a_geom):
179 ImageDrawSpec(const std::string &a_name,
188 std::string item_name;
189 gui::IGUIButton *parent_button;
195 /* The responsibility of unescaping the strings has been shifted
196 * from the formspec parsing methods to the draw methods.
197 * There still are a few exceptions:
198 * - Vertical label, because it modifies the string by inserting
199 * '\n' between each character,
200 * - Tab header, because it gives the string immediately to
201 * Irrlicht and we can't unescape it later.
208 FieldSpec(const std::string &name, const std::wstring &label,
209 const std::wstring &default_text, int id) :
213 flabel = unescape_string(unescape_enriched(label));
214 fdefault = unescape_string(unescape_enriched(default_text));
221 std::wstring fdefault;
224 FormspecFieldType ftype;
226 core::rect<s32> rect;
230 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
238 irr::video::SColor color;
245 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
246 irr::video::SColor a_color):
250 tooltip = unescape_string(unescape_enriched(utf8_to_wide(a_tooltip)));
252 std::wstring tooltip;
253 irr::video::SColor bgcolor;
254 irr::video::SColor color;
257 struct StaticTextSpec {
262 StaticTextSpec(const std::wstring &a_text,
263 const core::rect<s32> &a_rect):
267 text = unescape_string(unescape_enriched(a_text));
269 StaticTextSpec(const std::wstring &a_text,
270 const core::rect<s32> &a_rect,
271 gui::IGUIButton *a_parent_button):
273 parent_button(a_parent_button)
275 text = unescape_string(unescape_enriched(a_text));
278 core::rect<s32> rect;
279 gui::IGUIButton *parent_button;
283 GUIFormSpecMenu(irr::IrrlichtDevice* dev,
284 gui::IGUIElement* parent, s32 id,
285 IMenuManager *menumgr,
286 InventoryManager *invmgr,
288 ISimpleTextureSource *tsrc,
292 bool remap_dbl_click = true);
296 void setFormSpec(const std::string &formspec_string,
297 InventoryLocation current_inventory_location)
299 m_formspec_string = formspec_string;
300 m_current_inventory_location = current_inventory_location;
301 regenerateGui(m_screensize_old);
304 // form_src is deleted by this GUIFormSpecMenu
305 void setFormSource(IFormSource *form_src)
307 if (m_form_src != NULL) {
310 m_form_src = form_src;
313 // text_dst is deleted by this GUIFormSpecMenu
314 void setTextDest(TextDest *text_dst)
316 if (m_text_dst != NULL) {
319 m_text_dst = text_dst;
322 void allowClose(bool value)
324 m_allowclose = value;
327 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
330 m_lockscreensize = basescreensize;
333 void removeChildren();
334 void setInitialFocus();
336 void setFocus(std::string &elementname)
338 m_focused_element = elementname;
342 Remove and re-add (or reposition) stuff
344 void regenerateGui(v2u32 screensize);
346 ItemSpec getItemAtPos(v2s32 p) const;
347 void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
348 void drawSelectedItem();
350 void updateSelectedItem();
351 ItemStack verifySelectedItem();
353 void acceptInput(FormspecQuitMode quitmode);
354 bool preprocessEvent(const SEvent& event);
355 bool OnEvent(const SEvent& event);
357 bool pausesGame() { return doPause; }
359 GUITable* getTable(const std::string &tablename);
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;
399 ItemSpec *m_selected_item;
402 u32 m_selected_amount;
403 bool m_selected_dragging;
405 // WARNING: BLACK MAGIC
406 // Used to guess and keep up with some special things the server can do.
407 // If name is "", no guess exists.
408 ItemStack m_selected_content_guess;
409 InventoryLocation m_selected_content_guess_inventory;
412 v2s32 m_old_pointer; // Mouse position after previous mouse event
413 gui::IGUIStaticText *m_tooltip_element;
415 u32 m_tooltip_show_delay;
417 s32 m_old_tooltip_id;
418 std::wstring m_old_tooltip;
420 bool m_rmouse_auto_place;
424 v2u32 m_lockscreensize;
428 bool m_clipbackground;
429 video::SColor m_bgcolor;
430 video::SColor m_slotbg_n;
431 video::SColor m_slotbg_h;
432 video::SColor m_slotbordercolor;
433 video::SColor m_default_tooltip_bgcolor;
434 video::SColor m_default_tooltip_color;
437 IFormSource *m_form_src;
438 TextDest *m_text_dst;
439 unsigned int m_formspec_version;
440 std::string m_focused_element;
446 core::rect<s32> rect;
449 std::string focused_fieldname;
450 GUITable::TableOptions table_options;
451 GUITable::TableColumns table_columns;
452 // used to restore table selection/scroll/treeview state
453 std::map<std::string, GUITable::DynamicData> table_dyndata;
463 fs_key_pendig current_keys_pending;
465 void parseElement(parserData* data,std::string element);
467 void parseSize(parserData* data,std::string element);
468 void parseList(parserData* data,std::string element);
469 void parseListRing(parserData* data,std::string element);
470 void parseCheckbox(parserData* data,std::string element);
471 void parseImage(parserData* data,std::string element);
472 void parseItemImage(parserData* data,std::string element);
473 void parseButton(parserData* data,std::string element,std::string typ);
474 void parseBackground(parserData* data,std::string element);
475 void parseTableOptions(parserData* data,std::string element);
476 void parseTableColumns(parserData* data,std::string element);
477 void parseTable(parserData* data,std::string element);
478 void parseTextList(parserData* data,std::string element);
479 void parseDropDown(parserData* data,std::string element);
480 void parsePwdField(parserData* data,std::string element);
481 void parseField(parserData* data,std::string element,std::string type);
482 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
483 void parseTextArea(parserData* data,std::vector<std::string>& parts,
485 void parseLabel(parserData* data,std::string element);
486 void parseVertLabel(parserData* data,std::string element);
487 void parseImageButton(parserData* data,std::string element,std::string type);
488 void parseItemImageButton(parserData* data,std::string element);
489 void parseTabHeader(parserData* data,std::string element);
490 void parseBox(parserData* data,std::string element);
491 void parseBackgroundColor(parserData* data,std::string element);
492 void parseListColors(parserData* data,std::string element);
493 void parseTooltip(parserData* data,std::string element);
494 bool parseVersionDirect(std::string data);
495 bool parseSizeDirect(parserData* data, std::string element);
496 void parseScrollBar(parserData* data, std::string element);
499 * check if event is part of a double click
500 * @param event event to evaluate
501 * @return true/false if a doubleclick was detected
503 bool DoubleClickDetection(const SEvent event);
510 clickpos m_doubleclickdetect[2];
513 gui::IGUIFont *m_font;
515 std::wstring getLabelByID(s32 id);
516 std::string getNameByID(s32 id);
519 std::string m_JavaDialogFieldName;
522 /* If true, remap a double-click (or double-tap) action to ESC. This is so
523 * that, for example, Android users can double-tap to close a formspec.
525 * This value can (currently) only be set by the class constructor
526 * and the default value for the setting is true.
528 bool m_remap_dbl_click;
532 class FormspecFormSource: public IFormSource
535 FormspecFormSource(std::string formspec)
537 m_formspec = formspec;
540 ~FormspecFormSource()
543 void setForm(std::string formspec) {
544 m_formspec = FORMSPEC_VERSION_STRING + formspec;
547 std::string getForm()
552 std::string m_formspec;