]> git.lizzy.rs Git - dragonfireclient.git/blob - src/guiFormSpecMenu.h
Merge remote-tracking branch 'origin/master'
[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 "irrlichttypes_extrabloated.h"
25 #include "inventory.h"
26 #include "inventorymanager.h"
27 #include "modalMenu.h"
28
29 class IGameDef;
30 class InventoryManager;
31
32 struct TextDest
33 {
34         virtual ~TextDest() {};
35         // This is deprecated I guess? -celeron55
36         virtual void gotText(std::wstring text){}
37         virtual void gotText(std::map<std::string, std::string> fields) = 0;
38 };
39
40 class IFormSource
41 {
42 public:
43         virtual ~IFormSource(){}
44         virtual std::string getForm() = 0;
45         // Fill in variables in field text
46         virtual std::string resolveText(std::string str){ return str; }
47 };
48
49 void drawItemStack(video::IVideoDriver *driver,
50                 gui::IGUIFont *font,
51                 const ItemStack &item,
52                 const core::rect<s32> &rect,
53                 const core::rect<s32> *clip,
54                 IGameDef *gamedef);
55
56 class GUIFormSpecMenu : public GUIModalMenu
57 {
58         struct ItemSpec
59         {
60                 ItemSpec()
61                 {
62                         i = -1;
63                 }
64                 ItemSpec(const InventoryLocation &a_inventoryloc,
65                                 const std::string &a_listname,
66                                 s32 a_i)
67                 {
68                         inventoryloc = a_inventoryloc;
69                         listname = a_listname;
70                         i = a_i;
71                 }
72                 bool isValid() const
73                 {
74                         return i != -1;
75                 }
76
77                 InventoryLocation inventoryloc;
78                 std::string listname;
79                 s32 i;
80         };
81
82         struct ListDrawSpec
83         {
84                 ListDrawSpec()
85                 {
86                 }
87                 ListDrawSpec(const InventoryLocation &a_inventoryloc,
88                                 const std::string &a_listname,
89                                 v2s32 a_pos, v2s32 a_geom, s32 a_start_item_i):
90                         inventoryloc(a_inventoryloc),
91                         listname(a_listname),
92                         pos(a_pos),
93                         geom(a_geom),
94                         start_item_i(a_start_item_i)
95                 {
96                 }
97
98                 InventoryLocation inventoryloc;
99                 std::string listname;
100                 v2s32 pos;
101                 v2s32 geom;
102                 s32 start_item_i;
103         };
104
105         struct ImageDrawSpec
106         {
107                 ImageDrawSpec()
108                 {
109                 }
110                 ImageDrawSpec(const std::string &a_name,
111                                 v2s32 a_pos, v2s32 a_geom):
112                         name(a_name),
113                         pos(a_pos),
114                         geom(a_geom)
115                 {
116                 }
117                 std::string name;
118                 v2s32 pos;
119                 v2s32 geom;
120         };
121         
122         struct FieldSpec
123         {
124                 FieldSpec()
125                 {
126                 }
127                 FieldSpec(const std::wstring name, const std::wstring label, const std::wstring fdeflt, int id):
128                         fname(name),
129                         flabel(label),
130                         fdefault(fdeflt),
131                         fid(id)
132                 {
133                         send = false;
134                         is_button = false;
135                         is_exit = false;
136                         tooltip="";
137                 }
138                 std::wstring fname;
139                 std::wstring flabel;
140                 std::wstring fdefault;
141                 int fid;
142                 bool send;
143                 bool is_button;
144                 bool is_exit;
145                 core::rect<s32> rect;
146                 std::string tooltip;
147         };
148
149 public:
150         GUIFormSpecMenu(irr::IrrlichtDevice* dev,
151                         gui::IGUIElement* parent, s32 id,
152                         IMenuManager *menumgr,
153                         InventoryManager *invmgr,
154                         IGameDef *gamedef
155                         );
156         ~GUIFormSpecMenu();
157
158         void setFormSpec(const std::string &formspec_string,
159                         InventoryLocation current_inventory_location)
160         {
161                 m_formspec_string = formspec_string;
162                 m_current_inventory_location = current_inventory_location;
163                 regenerateGui(m_screensize_old);
164         }
165         
166         // form_src is deleted by this GUIFormSpecMenu
167         void setFormSource(IFormSource *form_src)
168         {
169                 m_form_src = form_src;
170         }
171
172         // text_dst is deleted by this GUIFormSpecMenu
173         void setTextDest(TextDest *text_dst)
174         {
175                 m_text_dst = text_dst;
176         }
177
178         void removeChildren();
179         /*
180                 Remove and re-add (or reposition) stuff
181         */
182         void regenerateGui(v2u32 screensize);
183         
184         ItemSpec getItemAtPos(v2s32 p) const;
185         void drawList(const ListDrawSpec &s, int phase);
186         void drawSelectedItem();
187         void drawMenu();
188         void updateSelectedItem();
189         ItemStack verifySelectedItem();
190
191         void acceptInput();
192         bool OnEvent(const SEvent& event);
193         
194 protected:
195         v2s32 getBasePos() const
196         {
197                 return padding + AbsoluteRect.UpperLeftCorner;
198         }
199
200         v2s32 padding;
201         v2s32 spacing;
202         v2s32 imgsize;
203         
204         irr::IrrlichtDevice* m_device;
205         InventoryManager *m_invmgr;
206         IGameDef *m_gamedef;
207
208         std::string m_formspec_string;
209         InventoryLocation m_current_inventory_location;
210         IFormSource *m_form_src;
211         TextDest *m_text_dst;
212
213         std::vector<ListDrawSpec> m_inventorylists;
214         std::vector<ImageDrawSpec> m_backgrounds;
215         std::vector<ImageDrawSpec> m_images;
216         std::vector<ImageDrawSpec> m_itemimages;
217         std::vector<FieldSpec> m_fields;
218
219         ItemSpec *m_selected_item;
220         u32 m_selected_amount;
221         bool m_selected_dragging;
222         
223         // WARNING: BLACK MAGIC
224         // Used to guess and keep up with some special things the server can do.
225         // If name is "", no guess exists.
226         ItemStack m_selected_content_guess;
227         InventoryLocation m_selected_content_guess_inventory;
228
229         v2s32 m_pointer;
230         gui::IGUIStaticText *m_tooltip_element;
231 };
232
233 #endif
234