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"
34 class InventoryManager;
35 class ISimpleTextureSource;
56 virtual ~TextDest() {};
57 // This is deprecated I guess? -celeron55
58 virtual void gotText(std::wstring text){}
59 virtual void gotText(const StringMap &fields) = 0;
60 virtual void setFormName(std::string formname)
61 { m_formname = formname;};
63 std::string m_formname;
69 virtual ~IFormSource(){}
70 virtual std::string getForm() = 0;
71 // Fill in variables in field text
72 virtual std::string resolveText(std::string str){ return str; }
75 class GUIFormSpecMenu : public GUIModalMenu
83 ItemSpec(const InventoryLocation &a_inventoryloc,
84 const std::string &a_listname,
87 inventoryloc = a_inventoryloc;
88 listname = a_listname;
96 InventoryLocation inventoryloc;
106 ListDrawSpec(const InventoryLocation &a_inventoryloc,
107 const std::string &a_listname,
108 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
109 inventoryloc(a_inventoryloc),
110 listname(a_listname),
113 start_item_i(a_start_item_i)
117 InventoryLocation inventoryloc;
118 std::string listname;
129 ListRingSpec(const InventoryLocation &a_inventoryloc,
130 const std::string &a_listname):
131 inventoryloc(a_inventoryloc),
136 InventoryLocation inventoryloc;
137 std::string listname;
146 ImageDrawSpec(const std::string &a_name,
147 const std::string &a_item_name,
148 gui::IGUIButton *a_parent_button,
149 const v2s32 &a_pos, const v2s32 &a_geom):
151 item_name(a_item_name),
152 parent_button(a_parent_button),
158 ImageDrawSpec(const std::string &a_name,
159 const std::string &a_item_name,
160 const v2s32 &a_pos, const v2s32 &a_geom):
162 item_name(a_item_name),
169 ImageDrawSpec(const std::string &a_name,
170 const v2s32 &a_pos, const v2s32 &a_geom):
178 ImageDrawSpec(const std::string &a_name,
187 std::string item_name;
188 gui::IGUIButton *parent_button;
199 FieldSpec(const std::string &name, const std::wstring &label,
200 const std::wstring &fdeflt, int id) :
212 std::wstring fdefault;
215 FormspecFieldType ftype;
217 core::rect<s32> rect;
221 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
229 irr::video::SColor color;
236 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
237 irr::video::SColor a_color):
244 irr::video::SColor bgcolor;
245 irr::video::SColor color;
248 struct StaticTextSpec {
253 StaticTextSpec(const std::wstring &a_text,
254 const core::rect<s32> &a_rect):
260 StaticTextSpec(const std::wstring &a_text,
261 const core::rect<s32> &a_rect,
262 gui::IGUIButton *a_parent_button):
265 parent_button(a_parent_button)
269 core::rect<s32> rect;
270 gui::IGUIButton *parent_button;
274 GUIFormSpecMenu(irr::IrrlichtDevice* dev,
275 gui::IGUIElement* parent, s32 id,
276 IMenuManager *menumgr,
277 InventoryManager *invmgr,
279 ISimpleTextureSource *tsrc,
283 bool remap_dbl_click = true);
287 void setFormSpec(const std::string &formspec_string,
288 InventoryLocation current_inventory_location)
290 m_formspec_string = formspec_string;
291 m_current_inventory_location = current_inventory_location;
292 regenerateGui(m_screensize_old);
295 // form_src is deleted by this GUIFormSpecMenu
296 void setFormSource(IFormSource *form_src)
298 if (m_form_src != NULL) {
301 m_form_src = form_src;
304 // text_dst is deleted by this GUIFormSpecMenu
305 void setTextDest(TextDest *text_dst)
307 if (m_text_dst != NULL) {
310 m_text_dst = text_dst;
313 void allowClose(bool value)
315 m_allowclose = value;
318 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
321 m_lockscreensize = basescreensize;
324 void removeChildren();
325 void setInitialFocus();
327 void setFocus(std::string &elementname)
329 m_focused_element = elementname;
333 Remove and re-add (or reposition) stuff
335 void regenerateGui(v2u32 screensize);
337 ItemSpec getItemAtPos(v2s32 p) const;
338 void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
339 void drawSelectedItem();
341 void updateSelectedItem();
342 ItemStack verifySelectedItem();
344 void acceptInput(FormspecQuitMode quitmode);
345 bool preprocessEvent(const SEvent& event);
346 bool OnEvent(const SEvent& event);
348 bool pausesGame() { return doPause; }
350 GUITable* getTable(const std::string &tablename);
353 bool getAndroidUIInput();
357 v2s32 getBasePos() const
359 return padding + offset + AbsoluteRect.UpperLeftCorner;
367 irr::IrrlichtDevice* m_device;
368 InventoryManager *m_invmgr;
370 ISimpleTextureSource *m_tsrc;
373 std::string m_formspec_string;
374 InventoryLocation m_current_inventory_location;
377 std::vector<ListDrawSpec> m_inventorylists;
378 std::vector<ListRingSpec> m_inventory_rings;
379 std::vector<ImageDrawSpec> m_backgrounds;
380 std::vector<ImageDrawSpec> m_images;
381 std::vector<ImageDrawSpec> m_itemimages;
382 std::vector<BoxDrawSpec> m_boxes;
383 std::vector<FieldSpec> m_fields;
384 std::vector<StaticTextSpec> m_static_texts;
385 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
386 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
387 std::map<std::string, TooltipSpec> m_tooltips;
388 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
390 ItemSpec *m_selected_item;
393 u32 m_selected_amount;
394 bool m_selected_dragging;
396 // WARNING: BLACK MAGIC
397 // Used to guess and keep up with some special things the server can do.
398 // If name is "", no guess exists.
399 ItemStack m_selected_content_guess;
400 InventoryLocation m_selected_content_guess_inventory;
403 v2s32 m_old_pointer; // Mouse position after previous mouse event
404 gui::IGUIStaticText *m_tooltip_element;
406 u32 m_tooltip_show_delay;
408 s32 m_old_tooltip_id;
409 std::string m_old_tooltip;
411 bool m_rmouse_auto_place;
415 v2u32 m_lockscreensize;
419 bool m_clipbackground;
420 video::SColor m_bgcolor;
421 video::SColor m_slotbg_n;
422 video::SColor m_slotbg_h;
423 video::SColor m_slotbordercolor;
424 video::SColor m_default_tooltip_bgcolor;
425 video::SColor m_default_tooltip_color;
428 IFormSource *m_form_src;
429 TextDest *m_text_dst;
430 unsigned int m_formspec_version;
431 std::string m_focused_element;
437 core::rect<s32> rect;
440 std::string focused_fieldname;
441 GUITable::TableOptions table_options;
442 GUITable::TableColumns table_columns;
443 // used to restore table selection/scroll/treeview state
444 std::map<std::string, GUITable::DynamicData> table_dyndata;
454 fs_key_pendig current_keys_pending;
456 void parseElement(parserData* data,std::string element);
458 void parseSize(parserData* data,std::string element);
459 void parseList(parserData* data,std::string element);
460 void parseListRing(parserData* data,std::string element);
461 void parseCheckbox(parserData* data,std::string element);
462 void parseImage(parserData* data,std::string element);
463 void parseItemImage(parserData* data,std::string element);
464 void parseButton(parserData* data,std::string element,std::string typ);
465 void parseBackground(parserData* data,std::string element);
466 void parseTableOptions(parserData* data,std::string element);
467 void parseTableColumns(parserData* data,std::string element);
468 void parseTable(parserData* data,std::string element);
469 void parseTextList(parserData* data,std::string element);
470 void parseDropDown(parserData* data,std::string element);
471 void parsePwdField(parserData* data,std::string element);
472 void parseField(parserData* data,std::string element,std::string type);
473 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
474 void parseTextArea(parserData* data,std::vector<std::string>& parts,
476 void parseLabel(parserData* data,std::string element);
477 void parseVertLabel(parserData* data,std::string element);
478 void parseImageButton(parserData* data,std::string element,std::string type);
479 void parseItemImageButton(parserData* data,std::string element);
480 void parseTabHeader(parserData* data,std::string element);
481 void parseBox(parserData* data,std::string element);
482 void parseBackgroundColor(parserData* data,std::string element);
483 void parseListColors(parserData* data,std::string element);
484 void parseTooltip(parserData* data,std::string element);
485 bool parseVersionDirect(std::string data);
486 bool parseSizeDirect(parserData* data, std::string element);
487 void parseScrollBar(parserData* data, std::string element);
490 * check if event is part of a double click
491 * @param event event to evaluate
492 * @return true/false if a doubleclick was detected
494 bool DoubleClickDetection(const SEvent event);
501 clickpos m_doubleclickdetect[2];
504 gui::IGUIFont *m_font;
506 std::wstring getLabelByID(s32 id);
507 std::string getNameByID(s32 id);
510 std::string m_JavaDialogFieldName;
513 /* If true, remap a double-click (or double-tap) action to ESC. This is so
514 * that, for example, Android users can double-tap to close a formspec.
516 * This value can (currently) only be set by the class constructor
517 * and the default value for the setting is true.
519 bool m_remap_dbl_click;
523 class FormspecFormSource: public IFormSource
526 FormspecFormSource(std::string formspec)
528 m_formspec = formspec;
531 ~FormspecFormSource()
534 void setForm(std::string formspec) {
535 m_formspec = FORMSPEC_VERSION_STRING + formspec;
538 std::string getForm()
543 std::string m_formspec;