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;
145 ImageDrawSpec(const std::string &a_name,
146 const std::string &a_item_name,
147 const v2s32 &a_pos, const v2s32 &a_geom):
149 item_name (a_item_name),
155 ImageDrawSpec(const std::string &a_name,
156 const v2s32 &a_pos, const v2s32 &a_geom):
163 ImageDrawSpec(const std::string &a_name,
171 std::string item_name;
182 FieldSpec(const std::string &name, const std::wstring &label,
183 const std::wstring &fdeflt, int id) :
195 std::wstring fdefault;
198 FormspecFieldType ftype;
200 core::rect<s32> rect;
204 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
212 irr::video::SColor color;
219 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
220 irr::video::SColor a_color):
227 irr::video::SColor bgcolor;
228 irr::video::SColor color;
231 struct StaticTextSpec {
235 StaticTextSpec(const std::wstring &a_text,
236 const core::rect<s32> &a_rect):
242 core::rect<s32> rect;
246 GUIFormSpecMenu(irr::IrrlichtDevice* dev,
247 gui::IGUIElement* parent, s32 id,
248 IMenuManager *menumgr,
249 InventoryManager *invmgr,
251 ISimpleTextureSource *tsrc,
255 bool remap_dbl_click = true);
259 void setFormSpec(const std::string &formspec_string,
260 InventoryLocation current_inventory_location)
262 m_formspec_string = formspec_string;
263 m_current_inventory_location = current_inventory_location;
264 regenerateGui(m_screensize_old);
267 // form_src is deleted by this GUIFormSpecMenu
268 void setFormSource(IFormSource *form_src)
270 if (m_form_src != NULL) {
273 m_form_src = form_src;
276 // text_dst is deleted by this GUIFormSpecMenu
277 void setTextDest(TextDest *text_dst)
279 if (m_text_dst != NULL) {
282 m_text_dst = text_dst;
285 void allowClose(bool value)
287 m_allowclose = value;
290 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
293 m_lockscreensize = basescreensize;
296 void removeChildren();
297 void setInitialFocus();
299 void setFocus(std::string &elementname)
301 m_focused_element = elementname;
305 Remove and re-add (or reposition) stuff
307 void regenerateGui(v2u32 screensize);
309 ItemSpec getItemAtPos(v2s32 p) const;
310 void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
311 void drawSelectedItem();
313 void updateSelectedItem();
314 ItemStack verifySelectedItem();
316 void acceptInput(FormspecQuitMode quitmode);
317 bool preprocessEvent(const SEvent& event);
318 bool OnEvent(const SEvent& event);
320 bool pausesGame() { return doPause; }
322 GUITable* getTable(const std::string &tablename);
325 bool getAndroidUIInput();
329 v2s32 getBasePos() const
331 return padding + offset + AbsoluteRect.UpperLeftCorner;
339 irr::IrrlichtDevice* m_device;
340 InventoryManager *m_invmgr;
342 ISimpleTextureSource *m_tsrc;
345 std::string m_formspec_string;
346 InventoryLocation m_current_inventory_location;
349 std::vector<ListDrawSpec> m_inventorylists;
350 std::vector<ListRingSpec> m_inventory_rings;
351 std::vector<ImageDrawSpec> m_backgrounds;
352 std::vector<ImageDrawSpec> m_images;
353 std::vector<ImageDrawSpec> m_itemimages;
354 std::vector<BoxDrawSpec> m_boxes;
355 std::vector<FieldSpec> m_fields;
356 std::vector<StaticTextSpec> m_static_texts;
357 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
358 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
359 std::map<std::string, TooltipSpec> m_tooltips;
360 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
362 ItemSpec *m_selected_item;
365 u32 m_selected_amount;
366 bool m_selected_dragging;
368 // WARNING: BLACK MAGIC
369 // Used to guess and keep up with some special things the server can do.
370 // If name is "", no guess exists.
371 ItemStack m_selected_content_guess;
372 InventoryLocation m_selected_content_guess_inventory;
375 v2s32 m_old_pointer; // Mouse position after previous mouse event
376 gui::IGUIStaticText *m_tooltip_element;
378 u32 m_tooltip_show_delay;
380 s32 m_old_tooltip_id;
381 std::string m_old_tooltip;
383 bool m_rmouse_auto_place;
387 v2u32 m_lockscreensize;
391 bool m_clipbackground;
392 video::SColor m_bgcolor;
393 video::SColor m_slotbg_n;
394 video::SColor m_slotbg_h;
395 video::SColor m_slotbordercolor;
396 video::SColor m_default_tooltip_bgcolor;
397 video::SColor m_default_tooltip_color;
400 IFormSource *m_form_src;
401 TextDest *m_text_dst;
402 unsigned int m_formspec_version;
403 std::string m_focused_element;
404 bool m_selection_active;
410 core::rect<s32> rect;
413 std::string focused_fieldname;
414 GUITable::TableOptions table_options;
415 GUITable::TableColumns table_columns;
416 // used to restore table selection/scroll/treeview state
417 std::map<std::string, GUITable::DynamicData> table_dyndata;
427 fs_key_pendig current_keys_pending;
429 void parseElement(parserData* data,std::string element);
431 void parseSize(parserData* data,std::string element);
432 void parseList(parserData* data,std::string element);
433 void parseListRing(parserData* data,std::string element);
434 void parseCheckbox(parserData* data,std::string element);
435 void parseImage(parserData* data,std::string element);
436 void parseItemImage(parserData* data,std::string element);
437 void parseButton(parserData* data,std::string element,std::string typ);
438 void parseBackground(parserData* data,std::string element);
439 void parseTableOptions(parserData* data,std::string element);
440 void parseTableColumns(parserData* data,std::string element);
441 void parseTable(parserData* data,std::string element);
442 void parseTextList(parserData* data,std::string element);
443 void parseDropDown(parserData* data,std::string element);
444 void parsePwdField(parserData* data,std::string element);
445 void parseField(parserData* data,std::string element,std::string type);
446 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
447 void parseTextArea(parserData* data,std::vector<std::string>& parts,
449 void parseLabel(parserData* data,std::string element);
450 void parseVertLabel(parserData* data,std::string element);
451 void parseImageButton(parserData* data,std::string element,std::string type);
452 void parseItemImageButton(parserData* data,std::string element);
453 void parseTabHeader(parserData* data,std::string element);
454 void parseBox(parserData* data,std::string element);
455 void parseBackgroundColor(parserData* data,std::string element);
456 void parseListColors(parserData* data,std::string element);
457 void parseTooltip(parserData* data,std::string element);
458 bool parseVersionDirect(std::string data);
459 bool parseSizeDirect(parserData* data, std::string element);
460 void parseScrollBar(parserData* data, std::string element);
463 * check if event is part of a double click
464 * @param event event to evaluate
465 * @return true/false if a doubleclick was detected
467 bool DoubleClickDetection(const SEvent event);
474 clickpos m_doubleclickdetect[2];
477 gui::IGUIFont *m_font;
479 std::wstring getLabelByID(s32 id);
480 std::string getNameByID(s32 id);
483 std::string m_JavaDialogFieldName;
486 /* If true, remap a double-click (or double-tap) action to ESC. This is so
487 * that, for example, Android users can double-tap to close a formspec.
489 * This value can (currently) only be set by the class constructor
490 * and the default value for the setting is true.
492 bool m_remap_dbl_click;
496 class FormspecFormSource: public IFormSource
499 FormspecFormSource(std::string formspec)
501 m_formspec = formspec;
504 ~FormspecFormSource()
507 void setForm(std::string formspec) {
508 m_formspec = FORMSPEC_VERSION_STRING + formspec;
511 std::string getForm()
516 std::string m_formspec;