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 v2s32 a_pos, v2s32 a_geom):
153 ImageDrawSpec(const std::string &a_name,
171 FieldSpec(const std::string &name, const std::wstring &label,
172 const std::wstring &fdeflt, int id) :
184 std::wstring fdefault;
187 FormspecFieldType ftype;
189 core::rect<s32> rect;
193 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
201 irr::video::SColor color;
208 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
209 irr::video::SColor a_color):
216 irr::video::SColor bgcolor;
217 irr::video::SColor color;
221 GUIFormSpecMenu(irr::IrrlichtDevice* dev,
222 gui::IGUIElement* parent, s32 id,
223 IMenuManager *menumgr,
224 InventoryManager *invmgr,
226 ISimpleTextureSource *tsrc,
230 bool remap_dbl_click = true);
234 void setFormSpec(const std::string &formspec_string,
235 InventoryLocation current_inventory_location)
237 m_formspec_string = formspec_string;
238 m_current_inventory_location = current_inventory_location;
239 regenerateGui(m_screensize_old);
242 // form_src is deleted by this GUIFormSpecMenu
243 void setFormSource(IFormSource *form_src)
245 if (m_form_src != NULL) {
248 m_form_src = form_src;
251 // text_dst is deleted by this GUIFormSpecMenu
252 void setTextDest(TextDest *text_dst)
254 if (m_text_dst != NULL) {
257 m_text_dst = text_dst;
260 void allowClose(bool value)
262 m_allowclose = value;
265 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
268 m_lockscreensize = basescreensize;
271 void removeChildren();
272 void setInitialFocus();
274 void setFocus(std::string &elementname)
276 m_focused_element = elementname;
280 Remove and re-add (or reposition) stuff
282 void regenerateGui(v2u32 screensize);
284 ItemSpec getItemAtPos(v2s32 p) const;
285 void drawList(const ListDrawSpec &s, int phase);
286 void drawSelectedItem();
288 void updateSelectedItem();
289 ItemStack verifySelectedItem();
291 void acceptInput(FormspecQuitMode quitmode);
292 bool preprocessEvent(const SEvent& event);
293 bool OnEvent(const SEvent& event);
295 bool pausesGame() { return doPause; }
297 GUITable* getTable(const std::string &tablename);
300 bool getAndroidUIInput();
304 v2s32 getBasePos() const
306 return padding + offset + AbsoluteRect.UpperLeftCorner;
314 irr::IrrlichtDevice* m_device;
315 InventoryManager *m_invmgr;
317 ISimpleTextureSource *m_tsrc;
320 std::string m_formspec_string;
321 InventoryLocation m_current_inventory_location;
324 std::vector<ListDrawSpec> m_inventorylists;
325 std::vector<ListRingSpec> m_inventory_rings;
326 std::vector<ImageDrawSpec> m_backgrounds;
327 std::vector<ImageDrawSpec> m_images;
328 std::vector<ImageDrawSpec> m_itemimages;
329 std::vector<BoxDrawSpec> m_boxes;
330 std::vector<FieldSpec> m_fields;
331 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
332 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
333 std::map<std::string, TooltipSpec> m_tooltips;
334 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
336 ItemSpec *m_selected_item;
337 u32 m_selected_amount;
338 bool m_selected_dragging;
340 // WARNING: BLACK MAGIC
341 // Used to guess and keep up with some special things the server can do.
342 // If name is "", no guess exists.
343 ItemStack m_selected_content_guess;
344 InventoryLocation m_selected_content_guess_inventory;
347 v2s32 m_old_pointer; // Mouse position after previous mouse event
348 gui::IGUIStaticText *m_tooltip_element;
350 u32 m_tooltip_show_delay;
352 s32 m_old_tooltip_id;
353 std::string m_old_tooltip;
355 bool m_rmouse_auto_place;
359 v2u32 m_lockscreensize;
363 bool m_clipbackground;
364 video::SColor m_bgcolor;
365 video::SColor m_slotbg_n;
366 video::SColor m_slotbg_h;
367 video::SColor m_slotbordercolor;
368 video::SColor m_default_tooltip_bgcolor;
369 video::SColor m_default_tooltip_color;
372 IFormSource *m_form_src;
373 TextDest *m_text_dst;
374 unsigned int m_formspec_version;
375 std::string m_focused_element;
381 core::rect<s32> rect;
384 std::string focused_fieldname;
385 GUITable::TableOptions table_options;
386 GUITable::TableColumns table_columns;
387 // used to restore table selection/scroll/treeview state
388 std::map<std::string, GUITable::DynamicData> table_dyndata;
398 fs_key_pendig current_keys_pending;
400 void parseElement(parserData* data,std::string element);
402 void parseSize(parserData* data,std::string element);
403 void parseList(parserData* data,std::string element);
404 void parseListRing(parserData* data,std::string element);
405 void parseCheckbox(parserData* data,std::string element);
406 void parseImage(parserData* data,std::string element);
407 void parseItemImage(parserData* data,std::string element);
408 void parseButton(parserData* data,std::string element,std::string typ);
409 void parseBackground(parserData* data,std::string element);
410 void parseTableOptions(parserData* data,std::string element);
411 void parseTableColumns(parserData* data,std::string element);
412 void parseTable(parserData* data,std::string element);
413 void parseTextList(parserData* data,std::string element);
414 void parseDropDown(parserData* data,std::string element);
415 void parsePwdField(parserData* data,std::string element);
416 void parseField(parserData* data,std::string element,std::string type);
417 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
418 void parseTextArea(parserData* data,std::vector<std::string>& parts,
420 void parseLabel(parserData* data,std::string element);
421 void parseVertLabel(parserData* data,std::string element);
422 void parseImageButton(parserData* data,std::string element,std::string type);
423 void parseItemImageButton(parserData* data,std::string element);
424 void parseTabHeader(parserData* data,std::string element);
425 void parseBox(parserData* data,std::string element);
426 void parseBackgroundColor(parserData* data,std::string element);
427 void parseListColors(parserData* data,std::string element);
428 void parseTooltip(parserData* data,std::string element);
429 bool parseVersionDirect(std::string data);
430 bool parseSizeDirect(parserData* data, std::string element);
431 void parseScrollBar(parserData* data, std::string element);
434 * check if event is part of a double click
435 * @param event event to evaluate
436 * @return true/false if a doubleclick was detected
438 bool DoubleClickDetection(const SEvent event);
445 clickpos m_doubleclickdetect[2];
448 gui::IGUIFont *m_font;
450 std::wstring getLabelByID(s32 id);
451 std::string getNameByID(s32 id);
454 std::string m_JavaDialogFieldName;
457 /* If true, remap a double-click (or double-tap) action to ESC. This is so
458 * that, for example, Android users can double-tap to close a formspec.
460 * This value can (currently) only be set by the class constructor
461 * and the default value for the setting is true.
463 bool m_remap_dbl_click;
467 class FormspecFormSource: public IFormSource
470 FormspecFormSource(std::string formspec)
472 m_formspec = formspec;
475 ~FormspecFormSource()
478 void setForm(std::string formspec) {
479 m_formspec = FORMSPEC_VERSION_STRING + formspec;
482 std::string getForm()
487 std::string m_formspec;