Formspec button_exit[] and image_button_exit[]
[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):
90                         inventoryloc(a_inventoryloc),
91                         listname(a_listname),
92                         pos(a_pos),
93                         geom(a_geom)
94                 {
95                 }
96
97                 InventoryLocation inventoryloc;
98                 std::string listname;
99                 v2s32 pos;
100                 v2s32 geom;
101         };
102
103         struct ImageDrawSpec
104         {
105                 ImageDrawSpec()
106                 {
107                 }
108                 ImageDrawSpec(const std::string &a_name,
109                                 v2s32 a_pos, v2s32 a_geom):
110                         name(a_name),
111                         pos(a_pos),
112                         geom(a_geom)
113                 {
114                 }
115                 std::string name;
116                 v2s32 pos;
117                 v2s32 geom;
118         };
119         
120         struct FieldSpec
121         {
122                 FieldSpec()
123                 {
124                 }
125                 FieldSpec(const std::wstring name, const std::wstring label, const std::wstring fdeflt, int id):
126                         fname(name),
127                         flabel(label),
128                         fdefault(fdeflt),
129                         fid(id)
130                 {
131                         send = false;
132                         is_button = false;
133                         is_exit = false;
134                 }
135                 std::wstring fname;
136                 std::wstring flabel;
137                 std::wstring fdefault;
138                 int fid;
139                 bool send;
140                 bool is_button;
141                 bool is_exit;
142         };
143
144 public:
145         GUIFormSpecMenu(gui::IGUIEnvironment* env,
146                         gui::IGUIElement* parent, s32 id,
147                         IMenuManager *menumgr,
148                         InventoryManager *invmgr,
149                         IGameDef *gamedef
150                         );
151         ~GUIFormSpecMenu();
152
153         void setFormSpec(const std::string &formspec_string,
154                         InventoryLocation current_inventory_location)
155         {
156                 m_formspec_string = formspec_string;
157                 m_current_inventory_location = current_inventory_location;
158                 regenerateGui(m_screensize_old);
159         }
160         
161         // form_src is deleted by this GUIFormSpecMenu
162         void setFormSource(IFormSource *form_src)
163         {
164                 m_form_src = form_src;
165         }
166
167         // text_dst is deleted by this GUIFormSpecMenu
168         void setTextDest(TextDest *text_dst)
169         {
170                 m_text_dst = text_dst;
171         }
172
173         void removeChildren();
174         /*
175                 Remove and re-add (or reposition) stuff
176         */
177         void regenerateGui(v2u32 screensize);
178         
179         ItemSpec getItemAtPos(v2s32 p) const;
180         void drawList(const ListDrawSpec &s, int phase);
181         void drawSelectedItem();
182         void drawMenu();
183         void updateSelectedItem();
184
185         void acceptInput();
186         bool OnEvent(const SEvent& event);
187         
188 protected:
189         v2s32 getBasePos() const
190         {
191                 return padding + AbsoluteRect.UpperLeftCorner;
192         }
193
194         v2s32 padding;
195         v2s32 spacing;
196         v2s32 imgsize;
197         
198         InventoryManager *m_invmgr;
199         IGameDef *m_gamedef;
200
201         std::string m_formspec_string;
202         InventoryLocation m_current_inventory_location;
203         IFormSource *m_form_src;
204         TextDest *m_text_dst;
205
206         core::array<ListDrawSpec> m_inventorylists;
207         core::array<ImageDrawSpec> m_images;
208         core::array<FieldSpec> m_fields;
209
210         ItemSpec *m_selected_item;
211         u32 m_selected_amount;
212         bool m_selected_dragging;
213
214         v2s32 m_pointer;
215         gui::IGUIStaticText *m_tooltip_element;
216 };
217
218 #endif
219