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.
25 #include "irrlichttypes_extrabloated.h"
26 #include "inventorymanager.h"
27 #include "modalMenu.h"
29 #include "network/networkprotocol.h"
30 #include "client/joystick_controller.h"
31 #include "util/string.h"
32 #include "util/enriched_string.h"
34 class InventoryManager;
35 class ISimpleTextureSource;
56 virtual ~TextDest() = default;
58 // This is deprecated I guess? -celeron55
59 virtual void gotText(const std::wstring &text) {}
60 virtual void gotText(const StringMap &fields) = 0;
62 std::string m_formname;
68 virtual ~IFormSource() = default;
69 virtual const std::string &getForm() const = 0;
70 // Fill in variables in field text
71 virtual std::string resolveText(const std::string &str) { return str; }
74 class GUIFormSpecMenu : public GUIModalMenu
80 ItemSpec(const InventoryLocation &a_inventoryloc,
81 const std::string &a_listname,
83 inventoryloc(a_inventoryloc),
89 bool isValid() const { return i != -1; }
91 InventoryLocation inventoryloc;
98 ListDrawSpec() = default;
100 ListDrawSpec(const InventoryLocation &a_inventoryloc,
101 const std::string &a_listname,
102 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
103 inventoryloc(a_inventoryloc),
104 listname(a_listname),
107 start_item_i(a_start_item_i)
111 InventoryLocation inventoryloc;
112 std::string listname;
120 ListRingSpec() = default;
122 ListRingSpec(const InventoryLocation &a_inventoryloc,
123 const std::string &a_listname):
124 inventoryloc(a_inventoryloc),
129 InventoryLocation inventoryloc;
130 std::string listname;
141 ImageDrawSpec(const std::string &a_name,
142 const std::string &a_item_name,
143 gui::IGUIButton *a_parent_button,
144 const v2s32 &a_pos, const v2s32 &a_geom):
146 item_name(a_item_name),
147 parent_button(a_parent_button),
155 ImageDrawSpec(const std::string &a_name,
156 const std::string &a_item_name,
157 const v2s32 &a_pos, const v2s32 &a_geom):
159 item_name(a_item_name),
168 ImageDrawSpec(const std::string &a_name,
169 const v2s32 &a_pos, const v2s32 &a_geom, bool clip=false):
179 ImageDrawSpec(const std::string &a_name,
190 std::string item_name;
191 gui::IGUIButton *parent_button;
200 FieldSpec() = default;
202 FieldSpec(const std::string &name, const std::wstring &label,
203 const std::wstring &default_text, int id) :
206 fdefault(unescape_enriched(translate_string(default_text))),
216 std::wstring fdefault;
219 FormspecFieldType ftype;
221 core::rect<s32> rect;
226 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom, irr::video::SColor a_color):
234 irr::video::SColor color;
239 TooltipSpec() = default;
240 TooltipSpec(const std::wstring &a_tooltip, irr::video::SColor a_bgcolor,
241 irr::video::SColor a_color):
242 tooltip(translate_string(a_tooltip)),
248 std::wstring tooltip;
249 irr::video::SColor bgcolor;
250 irr::video::SColor color;
253 struct StaticTextSpec
260 StaticTextSpec(const std::wstring &a_text,
261 const core::rect<s32> &a_rect):
268 StaticTextSpec(const std::wstring &a_text,
269 const core::rect<s32> &a_rect,
270 gui::IGUIButton *a_parent_button):
273 parent_button(a_parent_button)
278 core::rect<s32> rect;
279 gui::IGUIButton *parent_button;
283 GUIFormSpecMenu(JoystickController *joystick,
284 gui::IGUIElement* parent, s32 id,
285 IMenuManager *menumgr,
287 ISimpleTextureSource *tsrc,
290 const std::string &formspecPrepend,
291 bool remap_dbl_click = true);
295 static void create(GUIFormSpecMenu *&cur_formspec, Client *client,
296 JoystickController *joystick, IFormSource *fs_src, TextDest *txt_dest,
297 const std::string &formspecPrepend);
299 void setFormSpec(const std::string &formspec_string,
300 const InventoryLocation ¤t_inventory_location)
302 m_formspec_string = formspec_string;
303 m_current_inventory_location = current_inventory_location;
304 regenerateGui(m_screensize_old);
307 const InventoryLocation &getFormspecLocation()
309 return m_current_inventory_location;
312 void setFormspecPrepend(const std::string &formspecPrepend)
314 m_formspec_prepend = formspecPrepend;
317 // form_src is deleted by this GUIFormSpecMenu
318 void setFormSource(IFormSource *form_src)
321 m_form_src = form_src;
324 // text_dst is deleted by this GUIFormSpecMenu
325 void setTextDest(TextDest *text_dst)
328 m_text_dst = text_dst;
331 void allowClose(bool value)
333 m_allowclose = value;
336 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0))
339 m_lockscreensize = basescreensize;
342 void removeChildren();
343 void setInitialFocus();
345 void setFocus(const std::string &elementname)
347 m_focused_element = elementname;
351 Remove and re-add (or reposition) stuff
353 void regenerateGui(v2u32 screensize);
355 ItemSpec getItemAtPos(v2s32 p) const;
356 void drawList(const ListDrawSpec &s, int layer, bool &item_hovered);
357 void drawSelectedItem();
359 void updateSelectedItem();
360 ItemStack verifySelectedItem();
362 void acceptInput(FormspecQuitMode quitmode);
363 bool preprocessEvent(const SEvent& event);
364 bool OnEvent(const SEvent& event);
366 bool pausesGame() { return doPause; }
368 GUITable* getTable(const std::string &tablename);
369 std::vector<std::string>* getDropDownValues(const std::string &name);
372 bool getAndroidUIInput();
376 v2s32 getBasePos() const
378 return padding + offset + AbsoluteRect.UpperLeftCorner;
380 std::wstring getLabelByID(s32 id);
381 std::string getNameByID(s32 id);
382 v2s32 getElementBasePos(bool absolute,
383 const std::vector<std::string> *v_pos);
390 std::stack<v2f32> container_stack;
392 InventoryManager *m_invmgr;
393 ISimpleTextureSource *m_tsrc;
396 std::string m_formspec_string;
397 std::string m_formspec_prepend;
398 InventoryLocation m_current_inventory_location;
400 std::vector<ListDrawSpec> m_inventorylists;
401 std::vector<ListRingSpec> m_inventory_rings;
402 std::vector<ImageDrawSpec> m_backgrounds;
403 std::vector<ImageDrawSpec> m_images;
404 std::vector<ImageDrawSpec> m_itemimages;
405 std::vector<BoxDrawSpec> m_boxes;
406 std::unordered_map<std::string, bool> field_close_on_enter;
407 std::vector<FieldSpec> m_fields;
408 std::vector<StaticTextSpec> m_static_texts;
409 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
410 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
411 std::map<std::string, TooltipSpec> m_tooltips;
412 std::vector<std::pair<irr::core::rect<s32>, TooltipSpec>> m_tooltip_rects;
413 std::vector<std::pair<FieldSpec,gui::IGUIScrollBar*> > m_scrollbars;
414 std::vector<std::pair<FieldSpec, std::vector<std::string> > > m_dropdowns;
416 ItemSpec *m_selected_item = nullptr;
417 u16 m_selected_amount = 0;
418 bool m_selected_dragging = false;
419 ItemStack m_selected_swap;
421 gui::IGUIStaticText *m_tooltip_element = nullptr;
423 u64 m_tooltip_show_delay;
424 bool m_tooltip_append_itemname;
425 u64 m_hovered_time = 0;
426 s32 m_old_tooltip_id = -1;
428 bool m_auto_place = false;
430 bool m_allowclose = true;
432 v2u32 m_lockscreensize;
436 video::SColor m_bgcolor;
437 video::SColor m_fullscreen_bgcolor;
438 video::SColor m_slotbg_n;
439 video::SColor m_slotbg_h;
440 video::SColor m_slotbordercolor;
441 video::SColor m_default_tooltip_bgcolor;
442 video::SColor m_default_tooltip_color;
445 IFormSource *m_form_src;
446 TextDest *m_text_dst;
447 u32 m_formspec_version = 0;
448 std::string m_focused_element = "";
449 JoystickController *m_joystick;
457 core::rect<s32> rect;
460 std::string focused_fieldname;
461 GUITable::TableOptions table_options;
462 GUITable::TableColumns table_columns;
463 // used to restore table selection/scroll/treeview state
464 std::unordered_map<std::string, GUITable::DynamicData> table_dyndata;
474 fs_key_pendig current_keys_pending;
475 std::string current_field_enter_pending = "";
477 void parseElement(parserData* data, const std::string &element);
479 void parseSize(parserData* data, const std::string &element);
480 void parseContainer(parserData* data, const std::string &element);
481 void parseContainerEnd(parserData* data);
482 void parseList(parserData* data, const std::string &element);
483 void parseListRing(parserData* data, const std::string &element);
484 void parseCheckbox(parserData* data, const std::string &element);
485 void parseImage(parserData* data, const std::string &element);
486 void parseItemImage(parserData* data, const std::string &element);
487 void parseButton(parserData* data, const std::string &element,
488 const std::string &typ);
489 void parseBackground(parserData* data, const std::string &element);
490 void parseTableOptions(parserData* data, const std::string &element);
491 void parseTableColumns(parserData* data, const std::string &element);
492 void parseTable(parserData* data, const std::string &element);
493 void parseTextList(parserData* data, const std::string &element);
494 void parseDropDown(parserData* data, const std::string &element);
495 void parseFieldCloseOnEnter(parserData *data, const std::string &element);
496 void parsePwdField(parserData* data, const std::string &element);
497 void parseField(parserData* data, const std::string &element, const std::string &type);
498 void createTextField(parserData *data, FieldSpec &spec,
499 core::rect<s32> &rect, bool is_multiline);
500 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
501 void parseTextArea(parserData* data,std::vector<std::string>& parts,
502 const std::string &type);
503 void parseLabel(parserData* data, const std::string &element);
504 void parseVertLabel(parserData* data, const std::string &element);
505 void parseImageButton(parserData* data, const std::string &element,
506 const std::string &type);
507 void parseItemImageButton(parserData* data, const std::string &element);
508 void parseTabHeader(parserData* data, const std::string &element);
509 void parseBox(parserData* data, const std::string &element);
510 void parseBackgroundColor(parserData* data, const std::string &element);
511 void parseListColors(parserData* data, const std::string &element);
512 void parseTooltip(parserData* data, const std::string &element);
513 bool parseVersionDirect(const std::string &data);
514 bool parseSizeDirect(parserData* data, const std::string &element);
515 void parseScrollBar(parserData* data, const std::string &element);
516 bool parsePositionDirect(parserData *data, const std::string &element);
517 void parsePosition(parserData *data, const std::string &element);
518 bool parseAnchorDirect(parserData *data, const std::string &element);
519 void parseAnchor(parserData *data, const std::string &element);
523 void showTooltip(const std::wstring &text, const irr::video::SColor &color,
524 const irr::video::SColor &bgcolor);
527 * check if event is part of a double click
528 * @param event event to evaluate
529 * @return true/false if a doubleclick was detected
531 bool DoubleClickDetection(const SEvent event);
538 clickpos m_doubleclickdetect[2];
541 gui::IGUIFont *m_font = nullptr;
543 /* If true, remap a double-click (or double-tap) action to ESC. This is so
544 * that, for example, Android users can double-tap to close a formspec.
546 * This value can (currently) only be set by the class constructor
547 * and the default value for the setting is true.
549 bool m_remap_dbl_click;
553 class FormspecFormSource: public IFormSource
556 FormspecFormSource(const std::string &formspec):
561 ~FormspecFormSource() = default;
563 void setForm(const std::string &formspec)
565 m_formspec = FORMSPEC_VERSION_STRING + formspec;
568 const std::string &getForm() const
573 std::string m_formspec;