Merge support for anaglyph stereo
[oweals/minetest.git] / src / guiFormSpecMenu.h
1 /*
2 Minetest-c55
3 Copyright (C) 2010 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                 }
137                 std::wstring fname;
138                 std::wstring flabel;
139                 std::wstring fdefault;
140                 int fid;
141                 bool send;
142                 bool is_button;
143                 bool is_exit;
144         };
145
146 public:
147         GUIFormSpecMenu(irr::IrrlichtDevice* dev,
148                         gui::IGUIElement* parent, s32 id,
149                         IMenuManager *menumgr,
150                         InventoryManager *invmgr,
151                         IGameDef *gamedef
152                         );
153         ~GUIFormSpecMenu();
154
155         void setFormSpec(const std::string &formspec_string,
156                         InventoryLocation current_inventory_location)
157         {
158                 m_formspec_string = formspec_string;
159                 m_current_inventory_location = current_inventory_location;
160                 regenerateGui(m_screensize_old);
161         }
162         
163         // form_src is deleted by this GUIFormSpecMenu
164         void setFormSource(IFormSource *form_src)
165         {
166                 m_form_src = form_src;
167         }
168
169         // text_dst is deleted by this GUIFormSpecMenu
170         void setTextDest(TextDest *text_dst)
171         {
172                 m_text_dst = text_dst;
173         }
174
175         void removeChildren();
176         /*
177                 Remove and re-add (or reposition) stuff
178         */
179         void regenerateGui(v2u32 screensize);
180         
181         ItemSpec getItemAtPos(v2s32 p) const;
182         void drawList(const ListDrawSpec &s, int phase);
183         void drawSelectedItem();
184         void drawMenu();
185         void updateSelectedItem();
186
187         void acceptInput();
188         bool OnEvent(const SEvent& event);
189         
190 protected:
191         v2s32 getBasePos() const
192         {
193                 return padding + AbsoluteRect.UpperLeftCorner;
194         }
195
196         v2s32 padding;
197         v2s32 spacing;
198         v2s32 imgsize;
199         
200         irr::IrrlichtDevice* m_device;
201         InventoryManager *m_invmgr;
202         IGameDef *m_gamedef;
203
204         std::string m_formspec_string;
205         InventoryLocation m_current_inventory_location;
206         IFormSource *m_form_src;
207         TextDest *m_text_dst;
208
209         core::array<ListDrawSpec> m_inventorylists;
210         core::array<ImageDrawSpec> m_backgrounds;       
211         core::array<ImageDrawSpec> m_images;
212         core::array<FieldSpec> m_fields;
213
214         ItemSpec *m_selected_item;
215         u32 m_selected_amount;
216         bool m_selected_dragging;
217         
218         // WARNING: BLACK MAGIC
219         // Used to guess and keep up with some special things the server can do.
220         // If name is "", no guess exists.
221         ItemStack m_selected_content_guess;
222         InventoryLocation m_selected_content_guess_inventory;
223
224         v2s32 m_pointer;
225         gui::IGUIStaticText *m_tooltip_element;
226 };
227
228 #endif
229