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
27 #include "irrlichttypes_extrabloated.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"
36 class InventoryManager;
37 class ISimpleTextureSource;
58 virtual ~TextDest() {};
59 // This is deprecated I guess? -celeron55
60 virtual void gotText(std::wstring text){}
61 virtual void gotText(const StringMap &fields) = 0;
62 virtual void setFormName(std::string formname)
63 { m_formname = formname;};
65 std::string m_formname;
71 virtual ~IFormSource(){}
72 virtual std::string getForm() = 0;
73 // Fill in variables in field text
74 virtual std::string resolveText(std::string str){ return str; }
77 class GUIFormSpecMenu : public GUIModalMenu
85 ItemSpec(const InventoryLocation &a_inventoryloc,
86 const std::string &a_listname,
89 inventoryloc = a_inventoryloc;
90 listname = a_listname;
98 InventoryLocation inventoryloc;
108 ListDrawSpec(const InventoryLocation &a_inventoryloc,
109 const std::string &a_listname,
110 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
111 inventoryloc(a_inventoryloc),
112 listname(a_listname),
115 start_item_i(a_start_item_i)
119 InventoryLocation inventoryloc;
120 std::string listname;
131 ListRingSpec(const InventoryLocation &a_inventoryloc,
132 const std::string &a_listname):
133 inventoryloc(a_inventoryloc),
138 InventoryLocation inventoryloc;
139 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),
162 ImageDrawSpec(const std::string &a_name,
163 const std::string &a_item_name,
164 const v2s32 &a_pos, const v2s32 &a_geom):
166 item_name(a_item_name),
174 ImageDrawSpec(const std::string &a_name,
175 const v2s32 &a_pos, const v2s32 &a_geom, bool clip=false):
184 ImageDrawSpec(const std::string &a_name,
194 std::string item_name;
195 gui::IGUIButton *parent_button;
207 FieldSpec(const std::string &name, const std::wstring &label,
208 const std::wstring &default_text, int id) :
216 //flabel = unescape_enriched(label);
217 fdefault = 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_enriched(utf8_to_wide(a_tooltip));
251 tooltip = utf8_to_wide(a_tooltip);
253 std::wstring tooltip;
254 irr::video::SColor bgcolor;
255 irr::video::SColor color;
258 struct StaticTextSpec {
263 StaticTextSpec(const std::wstring &a_text,
264 const core::rect<s32> &a_rect):
268 //text = unescape_enriched(a_text);
271 StaticTextSpec(const std::wstring &a_text,
272 const core::rect<s32> &a_rect,
273 gui::IGUIButton *a_parent_button):
275 parent_button(a_parent_button)
277 //text = unescape_enriched(a_text);
281 core::rect<s32> rect;
282 gui::IGUIButton *parent_button;
286 GUIFormSpecMenu(irr::IrrlichtDevice* dev,
287 JoystickController *joystick,
288 gui::IGUIElement* parent, s32 id,
289 IMenuManager *menumgr,
291 ISimpleTextureSource *tsrc,
294 bool remap_dbl_click = true);
298 void setFormSpec(const std::string &formspec_string,
299 InventoryLocation current_inventory_location)
301 m_formspec_string = formspec_string;
302 m_current_inventory_location = current_inventory_location;
303 regenerateGui(m_screensize_old);
306 // form_src is deleted by this GUIFormSpecMenu
307 void setFormSource(IFormSource *form_src)
309 if (m_form_src != NULL) {
312 m_form_src = form_src;
315 // text_dst is deleted by this GUIFormSpecMenu
316 void setTextDest(TextDest *text_dst)
318 if (m_text_dst != NULL) {
321 m_text_dst = text_dst;
324 void allowClose(bool value)
326 m_allowclose = value;
329 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
332 m_lockscreensize = basescreensize;
335 void removeChildren();
336 void setInitialFocus();
338 void setFocus(std::string &elementname)
340 m_focused_element = elementname;
344 Remove and re-add (or reposition) stuff
346 void regenerateGui(v2u32 screensize);
348 ItemSpec getItemAtPos(v2s32 p) const;
349 void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
350 void drawSelectedItem();
352 void updateSelectedItem();
353 ItemStack verifySelectedItem();
355 void acceptInput(FormspecQuitMode quitmode);
356 bool preprocessEvent(const SEvent& event);
357 bool OnEvent(const SEvent& event);
359 bool pausesGame() { return doPause; }
361 GUITable* getTable(const std::string &tablename);
362 std::vector<std::string>* getDropDownValues(const std::string &name);
365 bool getAndroidUIInput();
369 v2s32 getBasePos() const
371 return padding + offset + AbsoluteRect.UpperLeftCorner;
379 std::stack<v2s32> container_stack;
381 irr::IrrlichtDevice* m_device;
382 InventoryManager *m_invmgr;
383 ISimpleTextureSource *m_tsrc;
386 std::string m_formspec_string;
387 InventoryLocation m_current_inventory_location;
390 std::vector<ListDrawSpec> m_inventorylists;
391 std::vector<ListRingSpec> m_inventory_rings;
392 std::vector<ImageDrawSpec> m_backgrounds;
393 std::vector<ImageDrawSpec> m_images;
394 std::vector<ImageDrawSpec> m_itemimages;
395 std::vector<BoxDrawSpec> m_boxes;
396 UNORDERED_MAP<std::string, bool> field_close_on_enter;
397 std::vector<FieldSpec> m_fields;
398 std::vector<StaticTextSpec> m_static_texts;
399 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
400 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
401 std::map<std::string, TooltipSpec> m_tooltips;
402 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
403 std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
405 ItemSpec *m_selected_item;
406 u32 m_selected_amount;
407 bool m_selected_dragging;
409 // WARNING: BLACK MAGIC
410 // Used to guess and keep up with some special things the server can do.
411 // If name is "", no guess exists.
412 ItemStack m_selected_content_guess;
413 InventoryLocation m_selected_content_guess_inventory;
416 v2s32 m_old_pointer; // Mouse position after previous mouse event
417 gui::IGUIStaticText *m_tooltip_element;
419 u32 m_tooltip_show_delay;
421 s32 m_old_tooltip_id;
422 std::wstring m_old_tooltip;
424 bool m_rmouse_auto_place;
428 v2u32 m_lockscreensize;
432 video::SColor m_bgcolor;
433 video::SColor m_slotbg_n;
434 video::SColor m_slotbg_h;
435 video::SColor m_slotbordercolor;
436 video::SColor m_default_tooltip_bgcolor;
437 video::SColor m_default_tooltip_color;
440 IFormSource *m_form_src;
441 TextDest *m_text_dst;
442 unsigned int m_formspec_version;
443 std::string m_focused_element;
444 JoystickController *m_joystick;
452 core::rect<s32> rect;
455 std::string focused_fieldname;
456 GUITable::TableOptions table_options;
457 GUITable::TableColumns table_columns;
458 // used to restore table selection/scroll/treeview state
459 UNORDERED_MAP<std::string, GUITable::DynamicData> table_dyndata;
469 fs_key_pendig current_keys_pending;
470 std::string current_field_enter_pending;
472 void parseElement(parserData* data, std::string element);
474 void parseSize(parserData* data, std::string element);
475 void parseContainer(parserData* data, std::string element);
476 void parseContainerEnd(parserData* data);
477 void parseList(parserData* data, std::string element);
478 void parseListRing(parserData* data, std::string element);
479 void parseCheckbox(parserData* data, std::string element);
480 void parseImage(parserData* data, std::string element);
481 void parseItemImage(parserData* data,std::string element);
482 void parseButton(parserData* data,std::string element,std::string typ);
483 void parseBackground(parserData* data,std::string element);
484 void parseTableOptions(parserData* data,std::string element);
485 void parseTableColumns(parserData* data,std::string element);
486 void parseTable(parserData* data,std::string element);
487 void parseTextList(parserData* data,std::string element);
488 void parseDropDown(parserData* data,std::string element);
489 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
490 void parsePwdField(parserData* data,std::string element);
491 void parseField(parserData* data,std::string element,std::string type);
492 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
493 void parseTextArea(parserData* data,std::vector<std::string>& parts,
495 void parseLabel(parserData* data,std::string element);
496 void parseVertLabel(parserData* data,std::string element);
497 void parseImageButton(parserData* data,std::string element,std::string type);
498 void parseItemImageButton(parserData* data,std::string element);
499 void parseTabHeader(parserData* data,std::string element);
500 void parseBox(parserData* data,std::string element);
501 void parseBackgroundColor(parserData* data,std::string element);
502 void parseListColors(parserData* data,std::string element);
503 void parseTooltip(parserData* data,std::string element);
504 bool parseVersionDirect(std::string data);
505 bool parseSizeDirect(parserData* data, std::string element);
506 void parseScrollBar(parserData* data, std::string element);
507 bool parsePositionDirect(parserData *data, const std::string &element);
508 void parsePosition(parserData *data, const std::string &element);
509 bool parseAnchorDirect(parserData *data, const std::string &element);
510 void parseAnchor(parserData *data, const std::string &element);
515 * check if event is part of a double click
516 * @param event event to evaluate
517 * @return true/false if a doubleclick was detected
519 bool DoubleClickDetection(const SEvent event);
526 clickpos m_doubleclickdetect[2];
529 gui::IGUIFont *m_font;
531 std::wstring getLabelByID(s32 id);
532 std::string getNameByID(s32 id);
535 std::string m_JavaDialogFieldName;
538 /* If true, remap a double-click (or double-tap) action to ESC. This is so
539 * that, for example, Android users can double-tap to close a formspec.
541 * This value can (currently) only be set by the class constructor
542 * and the default value for the setting is true.
544 bool m_remap_dbl_click;
548 class FormspecFormSource: public IFormSource
551 FormspecFormSource(const std::string &formspec)
553 m_formspec = formspec;
556 ~FormspecFormSource()
559 void setForm(const std::string &formspec) {
560 m_formspec = FORMSPEC_VERSION_STRING + formspec;
563 std::string getForm()
568 std::string m_formspec;