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 "client/joystick_controller.h"
33 #include "util/string.h"
34 #include "util/enriched_string.h"
37 class InventoryManager;
38 class ISimpleTextureSource;
59 virtual ~TextDest() {};
60 // This is deprecated I guess? -celeron55
61 virtual void gotText(std::wstring text){}
62 virtual void gotText(const StringMap &fields) = 0;
63 virtual void setFormName(std::string formname)
64 { m_formname = formname;};
66 std::string m_formname;
72 virtual ~IFormSource(){}
73 virtual std::string getForm() = 0;
74 // Fill in variables in field text
75 virtual std::string resolveText(std::string str){ return str; }
78 class GUIFormSpecMenu : public GUIModalMenu
86 ItemSpec(const InventoryLocation &a_inventoryloc,
87 const std::string &a_listname,
90 inventoryloc = a_inventoryloc;
91 listname = a_listname;
99 InventoryLocation inventoryloc;
100 std::string listname;
109 ListDrawSpec(const InventoryLocation &a_inventoryloc,
110 const std::string &a_listname,
111 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
112 inventoryloc(a_inventoryloc),
113 listname(a_listname),
116 start_item_i(a_start_item_i)
120 InventoryLocation inventoryloc;
121 std::string listname;
132 ListRingSpec(const InventoryLocation &a_inventoryloc,
133 const std::string &a_listname):
134 inventoryloc(a_inventoryloc),
139 InventoryLocation inventoryloc;
140 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),
161 ImageDrawSpec(const std::string &a_name,
162 const std::string &a_item_name,
163 const v2s32 &a_pos, const v2s32 &a_geom):
165 item_name(a_item_name),
172 ImageDrawSpec(const std::string &a_name,
173 const v2s32 &a_pos, const v2s32 &a_geom):
181 ImageDrawSpec(const std::string &a_name,
190 std::string item_name;
191 gui::IGUIButton *parent_button;
202 FieldSpec(const std::string &name, const std::wstring &label,
203 const std::wstring &default_text, int id) :
208 close_on_enter(false),
212 //flabel = unescape_enriched(label);
213 fdefault = unescape_enriched(default_text);
217 std::wstring fdefault;
220 bool close_on_enter; // used by text fields
221 FormspecFieldType ftype;
223 core::rect<s32> rect;
227 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
235 irr::video::SColor color;
242 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
243 irr::video::SColor a_color):
247 //tooltip = unescape_enriched(utf8_to_wide(a_tooltip));
248 tooltip = utf8_to_wide(a_tooltip);
250 std::wstring tooltip;
251 irr::video::SColor bgcolor;
252 irr::video::SColor color;
255 struct StaticTextSpec {
260 StaticTextSpec(const std::wstring &a_text,
261 const core::rect<s32> &a_rect):
265 //text = unescape_enriched(a_text);
268 StaticTextSpec(const std::wstring &a_text,
269 const core::rect<s32> &a_rect,
270 gui::IGUIButton *a_parent_button):
272 parent_button(a_parent_button)
274 //text = unescape_enriched(a_text);
278 core::rect<s32> rect;
279 gui::IGUIButton *parent_button;
283 GUIFormSpecMenu(irr::IrrlichtDevice* dev,
284 JoystickController *joystick,
285 gui::IGUIElement* parent, s32 id,
286 IMenuManager *menumgr,
287 InventoryManager *invmgr,
289 ISimpleTextureSource *tsrc,
293 bool remap_dbl_click = true);
297 void setFormSpec(const std::string &formspec_string,
298 InventoryLocation current_inventory_location)
300 m_formspec_string = formspec_string;
301 m_current_inventory_location = current_inventory_location;
302 regenerateGui(m_screensize_old);
305 // form_src is deleted by this GUIFormSpecMenu
306 void setFormSource(IFormSource *form_src)
308 if (m_form_src != NULL) {
311 m_form_src = form_src;
314 // text_dst is deleted by this GUIFormSpecMenu
315 void setTextDest(TextDest *text_dst)
317 if (m_text_dst != NULL) {
320 m_text_dst = text_dst;
323 void allowClose(bool value)
325 m_allowclose = value;
328 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
331 m_lockscreensize = basescreensize;
334 void removeChildren();
335 void setInitialFocus();
337 void setFocus(std::string &elementname)
339 m_focused_element = elementname;
343 Remove and re-add (or reposition) stuff
345 void regenerateGui(v2u32 screensize);
347 ItemSpec getItemAtPos(v2s32 p) const;
348 void drawList(const ListDrawSpec &s, int phase, bool &item_hovered);
349 void drawSelectedItem();
351 void updateSelectedItem();
352 ItemStack verifySelectedItem();
354 void acceptInput(FormspecQuitMode quitmode);
355 bool preprocessEvent(const SEvent& event);
356 bool OnEvent(const SEvent& event);
358 bool pausesGame() { return doPause; }
360 GUITable* getTable(const std::string &tablename);
361 std::vector<std::string>* getDropDownValues(const std::string &name);
364 bool getAndroidUIInput();
368 v2s32 getBasePos() const
370 return padding + offset + AbsoluteRect.UpperLeftCorner;
378 irr::IrrlichtDevice* m_device;
379 InventoryManager *m_invmgr;
381 ISimpleTextureSource *m_tsrc;
384 std::string m_formspec_string;
385 InventoryLocation m_current_inventory_location;
388 std::vector<ListDrawSpec> m_inventorylists;
389 std::vector<ListRingSpec> m_inventory_rings;
390 std::vector<ImageDrawSpec> m_backgrounds;
391 std::vector<ImageDrawSpec> m_images;
392 std::vector<ImageDrawSpec> m_itemimages;
393 std::vector<BoxDrawSpec> m_boxes;
394 std::vector<FieldSpec> m_fields;
395 std::vector<StaticTextSpec> m_static_texts;
396 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
397 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
398 std::map<std::string, TooltipSpec> m_tooltips;
399 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
400 std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
402 ItemSpec *m_selected_item;
405 u32 m_selected_amount;
406 bool m_selected_dragging;
408 // WARNING: BLACK MAGIC
409 // Used to guess and keep up with some special things the server can do.
410 // If name is "", no guess exists.
411 ItemStack m_selected_content_guess;
412 InventoryLocation m_selected_content_guess_inventory;
415 v2s32 m_old_pointer; // Mouse position after previous mouse event
416 gui::IGUIStaticText *m_tooltip_element;
418 u32 m_tooltip_show_delay;
420 s32 m_old_tooltip_id;
421 std::wstring m_old_tooltip;
423 bool m_rmouse_auto_place;
427 v2u32 m_lockscreensize;
431 bool m_clipbackground;
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;
450 core::rect<s32> rect;
453 std::string focused_fieldname;
454 GUITable::TableOptions table_options;
455 GUITable::TableColumns table_columns;
456 // used to restore table selection/scroll/treeview state
457 std::map<std::string, GUITable::DynamicData> table_dyndata;
467 fs_key_pendig current_keys_pending;
468 std::string current_field_enter_pending;
470 void parseElement(parserData* data,std::string element);
472 void parseSize(parserData* data,std::string element);
473 void parseList(parserData* data,std::string element);
474 void parseListRing(parserData* data,std::string element);
475 void parseCheckbox(parserData* data,std::string element);
476 void parseImage(parserData* data,std::string element);
477 void parseItemImage(parserData* data,std::string element);
478 void parseButton(parserData* data,std::string element,std::string typ);
479 void parseBackground(parserData* data,std::string element);
480 void parseTableOptions(parserData* data,std::string element);
481 void parseTableColumns(parserData* data,std::string element);
482 void parseTable(parserData* data,std::string element);
483 void parseTextList(parserData* data,std::string element);
484 void parseDropDown(parserData* data,std::string element);
485 void parsePwdField(parserData* data,std::string element);
486 void parseField(parserData* data,std::string element,std::string type);
487 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
488 void parseTextArea(parserData* data,std::vector<std::string>& parts,
490 void parseLabel(parserData* data,std::string element);
491 void parseVertLabel(parserData* data,std::string element);
492 void parseImageButton(parserData* data,std::string element,std::string type);
493 void parseItemImageButton(parserData* data,std::string element);
494 void parseTabHeader(parserData* data,std::string element);
495 void parseBox(parserData* data,std::string element);
496 void parseBackgroundColor(parserData* data,std::string element);
497 void parseListColors(parserData* data,std::string element);
498 void parseTooltip(parserData* data,std::string element);
499 bool parseVersionDirect(std::string data);
500 bool parseSizeDirect(parserData* data, std::string element);
501 void parseScrollBar(parserData* data, std::string element);
506 * check if event is part of a double click
507 * @param event event to evaluate
508 * @return true/false if a doubleclick was detected
510 bool DoubleClickDetection(const SEvent event);
517 clickpos m_doubleclickdetect[2];
520 gui::IGUIFont *m_font;
522 std::wstring getLabelByID(s32 id);
523 std::string getNameByID(s32 id);
526 std::string m_JavaDialogFieldName;
529 /* If true, remap a double-click (or double-tap) action to ESC. This is so
530 * that, for example, Android users can double-tap to close a formspec.
532 * This value can (currently) only be set by the class constructor
533 * and the default value for the setting is true.
535 bool m_remap_dbl_click;
539 class FormspecFormSource: public IFormSource
542 FormspecFormSource(std::string formspec)
544 m_formspec = formspec;
547 ~FormspecFormSource()
550 void setForm(std::string formspec) {
551 m_formspec = FORMSPEC_VERSION_STRING + formspec;
554 std::string getForm()
559 std::string m_formspec;