]> git.lizzy.rs Git - dragonfireclient.git/blob - src/guiFormSpecMenu.h
Add propper client initialization
[dragonfireclient.git] / src / guiFormSpecMenu.h
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20
21 #ifndef GUIINVENTORYMENU_HEADER
22 #define GUIINVENTORYMENU_HEADER
23
24 #include <utility>
25
26 #include "irrlichttypes_extrabloated.h"
27 #include "inventory.h"
28 #include "inventorymanager.h"
29 #include "modalMenu.h"
30 #include "guiTable.h"
31
32 class IGameDef;
33 class InventoryManager;
34 class ISimpleTextureSource;
35
36 typedef enum {
37         f_Button,
38         f_Table,
39         f_TabHeader,
40         f_CheckBox,
41         f_DropDown,
42         f_Unknown
43 } FormspecFieldType;
44
45 struct TextDest
46 {
47         virtual ~TextDest() {};
48         // This is deprecated I guess? -celeron55
49         virtual void gotText(std::wstring text){}
50         virtual void gotText(std::map<std::string, std::string> fields) = 0;
51 };
52
53 class IFormSource
54 {
55 public:
56         virtual ~IFormSource(){}
57         virtual std::string getForm() = 0;
58         // Fill in variables in field text
59         virtual std::string resolveText(std::string str){ return str; }
60 };
61
62 class GUIFormSpecMenu : public GUIModalMenu
63 {
64         struct ItemSpec
65         {
66                 ItemSpec()
67                 {
68                         i = -1;
69                 }
70                 ItemSpec(const InventoryLocation &a_inventoryloc,
71                                 const std::string &a_listname,
72                                 s32 a_i)
73                 {
74                         inventoryloc = a_inventoryloc;
75                         listname = a_listname;
76                         i = a_i;
77                 }
78                 bool isValid() const
79                 {
80                         return i != -1;
81                 }
82
83                 InventoryLocation inventoryloc;
84                 std::string listname;
85                 s32 i;
86         };
87
88         struct ListDrawSpec
89         {
90                 ListDrawSpec()
91                 {
92                 }
93                 ListDrawSpec(const InventoryLocation &a_inventoryloc,
94                                 const std::string &a_listname,
95                                 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
96                         inventoryloc(a_inventoryloc),
97                         listname(a_listname),
98                         pos(a_pos),
99                         geom(a_geom),
100                         start_item_i(a_start_item_i)
101                 {
102                 }
103
104                 InventoryLocation inventoryloc;
105                 std::string listname;
106                 v2s32 pos;
107                 v2s32 geom;
108                 s32 start_item_i;
109         };
110
111         struct ImageDrawSpec
112         {
113                 ImageDrawSpec()
114                 {
115                 }
116                 ImageDrawSpec(const std::string &a_name,
117                                 v2s32 a_pos, v2s32 a_geom):
118                         name(a_name),
119                         pos(a_pos),
120                         geom(a_geom)
121                 {
122                         scale = true;
123                 }
124                 ImageDrawSpec(const std::string &a_name,
125                                 v2s32 a_pos):
126                         name(a_name),
127                         pos(a_pos)
128                 {
129                         scale = false;
130                 }
131                 std::string name;
132                 v2s32 pos;
133                 v2s32 geom;
134                 bool scale;
135         };
136         
137         struct FieldSpec
138         {
139                 FieldSpec()
140                 {
141                 }
142                 FieldSpec(const std::wstring name, const std::wstring label, const std::wstring fdeflt, int id):
143                         fname(name),
144                         flabel(label),
145                         fdefault(fdeflt),
146                         fid(id)
147                 {
148                         send = false;
149                         ftype = f_Unknown;
150                         is_exit = false;
151                         tooltip="";
152                 }
153                 std::wstring fname;
154                 std::wstring flabel;
155                 std::wstring fdefault;
156                 int fid;
157                 bool send;
158                 FormspecFieldType ftype;
159                 bool is_exit;
160                 core::rect<s32> rect;
161                 std::string tooltip;
162         };
163
164         struct BoxDrawSpec {
165                 BoxDrawSpec(v2s32 a_pos, v2s32 a_geom,irr::video::SColor a_color):
166                         pos(a_pos),
167                         geom(a_geom),
168                         color(a_color)
169                 {
170                 }
171                 v2s32 pos;
172                 v2s32 geom;
173                 irr::video::SColor color;
174         };
175
176 public:
177         GUIFormSpecMenu(irr::IrrlichtDevice* dev,
178                         gui::IGUIElement* parent, s32 id,
179                         IMenuManager *menumgr,
180                         InventoryManager *invmgr,
181                         IGameDef *gamedef,
182                         ISimpleTextureSource *tsrc
183                         );
184
185         ~GUIFormSpecMenu();
186
187         void setFormSpec(const std::string &formspec_string,
188                         InventoryLocation current_inventory_location)
189         {
190                 m_formspec_string = formspec_string;
191                 m_current_inventory_location = current_inventory_location;
192                 regenerateGui(m_screensize_old);
193         }
194         
195         // form_src is deleted by this GUIFormSpecMenu
196         void setFormSource(IFormSource *form_src)
197         {
198                 m_form_src = form_src;
199         }
200
201         // text_dst is deleted by this GUIFormSpecMenu
202         void setTextDest(TextDest *text_dst)
203         {
204                 m_text_dst = text_dst;
205         }
206
207         void allowClose(bool value)
208         {
209                 m_allowclose = value;
210         }
211
212         void lockSize(bool lock,v2u32 basescreensize=v2u32(0,0)) {
213                 m_lock = lock;
214                 m_lockscreensize = basescreensize;
215         }
216
217         void removeChildren();
218         void setInitialFocus();
219         /*
220                 Remove and re-add (or reposition) stuff
221         */
222         void regenerateGui(v2u32 screensize);
223         
224         ItemSpec getItemAtPos(v2s32 p) const;
225         void drawList(const ListDrawSpec &s, int phase);
226         void drawSelectedItem();
227         void drawMenu();
228         void updateSelectedItem();
229         ItemStack verifySelectedItem();
230
231         void acceptInput(bool quit);
232         bool preprocessEvent(const SEvent& event);
233         bool OnEvent(const SEvent& event);
234
235         GUITable* getTable(std::wstring tablename);
236
237         static bool parseColor(const std::string &value,
238                         video::SColor &color, bool quiet);
239
240 protected:
241         v2s32 getBasePos() const
242         {
243                         return padding + offset + AbsoluteRect.UpperLeftCorner;
244         }
245
246         v2s32 padding;
247         v2s32 spacing;
248         v2s32 imgsize;
249         v2s32 offset;
250         
251         irr::IrrlichtDevice* m_device;
252         InventoryManager *m_invmgr;
253         IGameDef *m_gamedef;
254         ISimpleTextureSource *m_tsrc;
255
256         std::string m_formspec_string;
257         InventoryLocation m_current_inventory_location;
258         IFormSource *m_form_src;
259         TextDest *m_text_dst;
260
261         std::vector<ListDrawSpec> m_inventorylists;
262         std::vector<ImageDrawSpec> m_backgrounds;
263         std::vector<ImageDrawSpec> m_images;
264         std::vector<ImageDrawSpec> m_itemimages;
265         std::vector<BoxDrawSpec> m_boxes;
266         std::vector<FieldSpec> m_fields;
267         std::vector<std::pair<FieldSpec,GUITable*> > m_tables;
268         std::vector<std::pair<FieldSpec,gui::IGUICheckBox*> > m_checkboxes;
269
270         ItemSpec *m_selected_item;
271         u32 m_selected_amount;
272         bool m_selected_dragging;
273         
274         // WARNING: BLACK MAGIC
275         // Used to guess and keep up with some special things the server can do.
276         // If name is "", no guess exists.
277         ItemStack m_selected_content_guess;
278         InventoryLocation m_selected_content_guess_inventory;
279
280         v2s32 m_pointer;
281         gui::IGUIStaticText *m_tooltip_element;
282
283         bool m_allowclose;
284         bool m_lock;
285         v2u32 m_lockscreensize;
286
287         bool m_bgfullscreen;
288         bool m_slotborder;
289         bool m_clipbackground;
290         video::SColor m_bgcolor;
291         video::SColor m_slotbg_n;
292         video::SColor m_slotbg_h;
293         video::SColor m_slotbordercolor;
294 private:
295         typedef struct {
296                 v2s32 size;
297                 s32 helptext_h;
298                 core::rect<s32> rect;
299                 v2s32 basepos;
300                 int bp_set;
301                 v2u32 screensize;
302                 std::wstring focused_fieldname;
303                 GUITable::TableOptions table_options;
304                 GUITable::TableColumns table_columns;
305                 // used to restore table selection/scroll/treeview state
306                 std::map<std::wstring,GUITable::DynamicData> table_dyndata;
307         } parserData;
308
309         typedef struct {
310                 bool key_up;
311                 bool key_down;
312                 bool key_enter;
313                 bool key_escape;
314         } fs_key_pendig;
315
316         fs_key_pendig current_keys_pending;
317
318         void parseElement(parserData* data,std::string element);
319
320         void parseSize(parserData* data,std::string element);
321         void parseList(parserData* data,std::string element);
322         void parseCheckbox(parserData* data,std::string element);
323         void parseImage(parserData* data,std::string element);
324         void parseItemImage(parserData* data,std::string element);
325         void parseButton(parserData* data,std::string element,std::string typ);
326         void parseBackground(parserData* data,std::string element);
327         void parseTableOptions(parserData* data,std::string element);
328         void parseTableColumns(parserData* data,std::string element);
329         void parseTable(parserData* data,std::string element);
330         void parseTextList(parserData* data,std::string element);
331         void parseDropDown(parserData* data,std::string element);
332         void parsePwdField(parserData* data,std::string element);
333         void parseField(parserData* data,std::string element,std::string type);
334         void parseSimpleField(parserData* data,std::vector<std::string> &parts);
335         void parseTextArea(parserData* data,std::vector<std::string>& parts,std::string type);
336         void parseLabel(parserData* data,std::string element);
337         void parseVertLabel(parserData* data,std::string element);
338         void parseImageButton(parserData* data,std::string element,std::string type);
339         void parseItemImageButton(parserData* data,std::string element);
340         void parseTabHeader(parserData* data,std::string element);
341         void parseBox(parserData* data,std::string element);
342         void parseBackgroundColor(parserData* data,std::string element);
343         void parseListColors(parserData* data,std::string element);
344 };
345
346 class FormspecFormSource: public IFormSource
347 {
348 public:
349         FormspecFormSource(std::string formspec,FormspecFormSource** game_formspec)
350         {
351                 m_formspec = formspec;
352                 m_game_formspec = game_formspec;
353         }
354
355         ~FormspecFormSource()
356         {
357                 *m_game_formspec = 0;
358         }
359
360         void setForm(std::string formspec) {
361                 m_formspec = formspec;
362         }
363
364         std::string getForm()
365         {
366                 return m_formspec;
367         }
368
369         std::string m_formspec;
370         FormspecFormSource** m_game_formspec;
371 };
372
373 #endif
374