Some serialization version stuff
[oweals/minetest.git] / src / guiInventoryMenu.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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 "common_irrlicht.h"
25 #include "inventory.h"
26 #include "inventorymanager.h"
27 #include "utility.h"
28 #include "modalMenu.h"
29
30 class IGameDef;
31 class InventoryManager;
32
33 void drawItemStack(video::IVideoDriver *driver,
34                 gui::IGUIFont *font,
35                 const ItemStack &item,
36                 const core::rect<s32> &rect,
37                 const core::rect<s32> *clip,
38                 IGameDef *gamedef);
39
40 class GUIInventoryMenu : public GUIModalMenu
41 {
42         struct ItemSpec
43         {
44                 ItemSpec()
45                 {
46                         i = -1;
47                 }
48                 ItemSpec(const InventoryLocation &a_inventoryloc,
49                                 const std::string &a_listname,
50                                 s32 a_i)
51                 {
52                         inventoryloc = a_inventoryloc;
53                         listname = a_listname;
54                         i = a_i;
55                 }
56                 bool isValid() const
57                 {
58                         return i != -1;
59                 }
60
61                 InventoryLocation inventoryloc;
62                 std::string listname;
63                 s32 i;
64         };
65
66         struct ListDrawSpec
67         {
68                 ListDrawSpec()
69                 {
70                 }
71                 ListDrawSpec(const InventoryLocation &a_inventoryloc,
72                                 const std::string &a_listname,
73                                 v2s32 a_pos, v2s32 a_geom)
74                 {
75                         inventoryloc = a_inventoryloc;
76                         listname = a_listname;
77                         pos = a_pos;
78                         geom = a_geom;
79                 }
80
81                 InventoryLocation inventoryloc;
82                 std::string listname;
83                 v2s32 pos;
84                 v2s32 geom;
85         };
86 public:
87         struct DrawSpec
88         {
89                 DrawSpec()
90                 {
91                 }
92                 DrawSpec(const std::string &a_type,
93                                 const InventoryLocation &a_name,
94                                 const std::string &a_subname,
95                                 v2s32 a_pos,
96                                 v2s32 a_geom)
97                 {
98                         type = a_type;
99                         name = a_name;
100                         subname = a_subname;
101                         pos = a_pos;
102                         geom = a_geom;
103                 }
104
105                 std::string type;
106                 InventoryLocation name;
107                 std::string subname;
108                 v2s32 pos;
109                 v2s32 geom;
110         };
111         
112         // See .cpp for format
113         static v2s16 makeDrawSpecArrayFromString(
114                         core::array<GUIInventoryMenu::DrawSpec> &draw_spec,
115                         const std::string &data,
116                         const InventoryLocation &current_location);
117
118         GUIInventoryMenu(gui::IGUIEnvironment* env,
119                         gui::IGUIElement* parent, s32 id,
120                         IMenuManager *menumgr,
121                         v2s16 menu_size,
122                         InventoryManager *invmgr,
123                         IGameDef *gamedef
124                         );
125         ~GUIInventoryMenu();
126
127         void setDrawSpec(core::array<DrawSpec> &init_draw_spec)
128         {
129                 m_init_draw_spec = init_draw_spec;
130         }
131
132         void removeChildren();
133         /*
134                 Remove and re-add (or reposition) stuff
135         */
136         void regenerateGui(v2u32 screensize);
137         
138         ItemSpec getItemAtPos(v2s32 p) const;
139         void drawList(const ListDrawSpec &s, int phase);
140         void drawSelectedItem();
141         void drawMenu();
142         void updateSelectedItem();
143
144         bool OnEvent(const SEvent& event);
145         
146 protected:
147         v2s32 getBasePos() const
148         {
149                 return padding + AbsoluteRect.UpperLeftCorner;
150         }
151
152         v2s16 m_menu_size;
153
154         v2s32 padding;
155         v2s32 spacing;
156         v2s32 imgsize;
157         
158         InventoryManager *m_invmgr;
159         IGameDef *m_gamedef;
160
161         core::array<DrawSpec> m_init_draw_spec;
162         core::array<ListDrawSpec> m_draw_spec;
163
164         ItemSpec *m_selected_item;
165         u32 m_selected_amount;
166         bool m_selected_dragging;
167
168         v2s32 m_pointer;
169         gui::IGUIStaticText *m_tooltip_element;
170 };
171
172 #endif
173