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 "clientserver.h"
34 class InventoryManager;
35 class ISimpleTextureSource;
54 virtual ~TextDest() {};
55 // This is deprecated I guess? -celeron55
56 virtual void gotText(std::wstring text){}
57 virtual void gotText(std::map<std::string, std::string> fields) = 0;
58 virtual void setFormName(std::string formname)
59 { m_formname = formname;};
61 std::string m_formname;
67 virtual ~IFormSource(){}
68 virtual std::string getForm() = 0;
69 // Fill in variables in field text
70 virtual std::string resolveText(std::string str){ return str; }
73 class GUIFormSpecMenu : public GUIModalMenu
81 ItemSpec(const InventoryLocation &a_inventoryloc,
82 const std::string &a_listname,
85 inventoryloc = a_inventoryloc;
86 listname = a_listname;
94 InventoryLocation inventoryloc;
104 ListDrawSpec(const InventoryLocation &a_inventoryloc,
105 const std::string &a_listname,
106 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
107 inventoryloc(a_inventoryloc),
108 listname(a_listname),
111 start_item_i(a_start_item_i)
115 InventoryLocation inventoryloc;
116 std::string listname;
127 ImageDrawSpec(const std::string &a_name,
128 v2s32 a_pos, v2s32 a_geom):
135 ImageDrawSpec(const std::string &a_name,
153 FieldSpec(const std::wstring &name, const std::wstring &label,
154 const std::wstring &fdeflt, int id) :
166 std::wstring fdefault;
169 FormspecFieldType ftype;
171 core::rect<s32> rect;
175 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
183 irr::video::SColor color;
190 TooltipSpec(std::string a_tooltip, irr::video::SColor a_bgcolor,
191 irr::video::SColor a_color):
198 irr::video::SColor bgcolor;
199 irr::video::SColor color;
203 GUIFormSpecMenu(irr::IrrlichtDevice* dev,
204 gui::IGUIElement* parent, s32 id,
205 IMenuManager *menumgr,
206 InventoryManager *invmgr,
208 ISimpleTextureSource *tsrc,
211 GUIFormSpecMenu** ext_ptr
216 void setFormSpec(const std::string &formspec_string,
217 InventoryLocation current_inventory_location)
219 m_formspec_string = formspec_string;
220 m_current_inventory_location = current_inventory_location;
221 regenerateGui(m_screensize_old);
224 // form_src is deleted by this GUIFormSpecMenu
225 void setFormSource(IFormSource *form_src)
227 if (m_form_src != NULL) {
230 m_form_src = form_src;
233 // text_dst is deleted by this GUIFormSpecMenu
234 void setTextDest(TextDest *text_dst)
236 if (m_text_dst != NULL) {
239 m_text_dst = text_dst;
242 void allowClose(bool value)
244 m_allowclose = value;
247 void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0)) {
249 m_lockscreensize = basescreensize;
252 void removeChildren();
253 void setInitialFocus();
255 Remove and re-add (or reposition) stuff
257 void regenerateGui(v2u32 screensize);
259 ItemSpec getItemAtPos(v2s32 p) const;
260 void drawList(const ListDrawSpec &s, int phase);
261 void drawSelectedItem();
263 void updateSelectedItem();
264 ItemStack verifySelectedItem();
266 void acceptInput(FormspecQuitMode quitmode);
267 bool preprocessEvent(const SEvent& event);
268 bool OnEvent(const SEvent& event);
270 bool pausesGame() { return doPause; }
272 GUITable* getTable(std::wstring tablename);
274 static bool parseColor(const std::string &value,
275 video::SColor &color, bool quiet);
278 v2s32 getBasePos() const
280 return padding + offset + AbsoluteRect.UpperLeftCorner;
288 irr::IrrlichtDevice* m_device;
289 InventoryManager *m_invmgr;
291 ISimpleTextureSource *m_tsrc;
293 std::string m_formspec_string;
294 InventoryLocation m_current_inventory_location;
296 std::vector<ListDrawSpec> m_inventorylists;
297 std::vector<ImageDrawSpec> m_backgrounds;
298 std::vector<ImageDrawSpec> m_images;
299 std::vector<ImageDrawSpec> m_itemimages;
300 std::vector<BoxDrawSpec> m_boxes;
301 std::vector<FieldSpec> m_fields;
302 std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
303 std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
304 std::map<std::wstring, TooltipSpec> m_tooltips;
306 ItemSpec *m_selected_item;
307 u32 m_selected_amount;
308 bool m_selected_dragging;
310 // WARNING: BLACK MAGIC
311 // Used to guess and keep up with some special things the server can do.
312 // If name is "", no guess exists.
313 ItemStack m_selected_content_guess;
314 InventoryLocation m_selected_content_guess_inventory;
317 gui::IGUIStaticText *m_tooltip_element;
319 u32 m_tooltip_show_delay;
321 s32 m_old_tooltip_id;
322 std::string m_old_tooltip;
326 v2u32 m_lockscreensize;
330 bool m_clipbackground;
331 video::SColor m_bgcolor;
332 video::SColor m_slotbg_n;
333 video::SColor m_slotbg_h;
334 video::SColor m_slotbordercolor;
335 video::SColor m_default_tooltip_bgcolor;
336 video::SColor m_default_tooltip_color;
339 IFormSource *m_form_src;
340 TextDest *m_text_dst;
341 GUIFormSpecMenu **m_ext_ptr;
342 gui::IGUIFont *m_font;
343 unsigned int m_formspec_version;
347 core::rect<s32> rect;
351 std::wstring focused_fieldname;
352 GUITable::TableOptions table_options;
353 GUITable::TableColumns table_columns;
354 // used to restore table selection/scroll/treeview state
355 std::map<std::wstring,GUITable::DynamicData> table_dyndata;
365 fs_key_pendig current_keys_pending;
367 void parseElement(parserData* data,std::string element);
369 void parseSize(parserData* data,std::string element);
370 void parseList(parserData* data,std::string element);
371 void parseCheckbox(parserData* data,std::string element);
372 void parseImage(parserData* data,std::string element);
373 void parseItemImage(parserData* data,std::string element);
374 void parseButton(parserData* data,std::string element,std::string typ);
375 void parseBackground(parserData* data,std::string element);
376 void parseTableOptions(parserData* data,std::string element);
377 void parseTableColumns(parserData* data,std::string element);
378 void parseTable(parserData* data,std::string element);
379 void parseTextList(parserData* data,std::string element);
380 void parseDropDown(parserData* data,std::string element);
381 void parsePwdField(parserData* data,std::string element);
382 void parseField(parserData* data,std::string element,std::string type);
383 void parseSimpleField(parserData* data,std::vector<std::string> &parts);
384 void parseTextArea(parserData* data,std::vector<std::string>& parts,
386 void parseLabel(parserData* data,std::string element);
387 void parseVertLabel(parserData* data,std::string element);
388 void parseImageButton(parserData* data,std::string element,std::string type);
389 void parseItemImageButton(parserData* data,std::string element);
390 void parseTabHeader(parserData* data,std::string element);
391 void parseBox(parserData* data,std::string element);
392 void parseBackgroundColor(parserData* data,std::string element);
393 void parseListColors(parserData* data,std::string element);
394 void parseTooltip(parserData* data,std::string element);
395 bool parseVersionDirect(std::string data);
398 * check if event is part of a double click
399 * @param event event to evaluate
400 * @return true/false if a doubleclick was detected
402 bool DoubleClickDetection(const SEvent event);
409 clickpos m_doubleclickdetect[2];
414 class FormspecFormSource: public IFormSource
417 FormspecFormSource(std::string formspec)
419 m_formspec = formspec;
422 ~FormspecFormSource()
425 void setForm(std::string formspec) {
426 m_formspec = FORMSPEC_VERSION_STRING + formspec;
429 std::string getForm()
434 std::string m_formspec;