utility.h: Change Buffer's interface to be more compatible with SharedBuffer's interf...
[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 "utility.h"
27 #include "modalMenu.h"
28
29 void drawInventoryItem(video::IVideoDriver *driver,
30                 gui::IGUIFont *font,
31                 InventoryItem *item, core::rect<s32> rect,
32                 const core::rect<s32> *clip);
33
34 class GUIInventoryMenu : public GUIModalMenu
35 {
36         struct ItemSpec
37         {
38                 ItemSpec()
39                 {
40                         i = -1;
41                 }
42                 ItemSpec(const std::string &a_inventoryname,
43                                 const std::string &a_listname,
44                                 s32 a_i)
45                 {
46                         inventoryname = a_inventoryname;
47                         listname = a_listname;
48                         i = a_i;
49                 }
50                 bool isValid() const
51                 {
52                         return i != -1;
53                 }
54
55                 std::string inventoryname;
56                 std::string listname;
57                 s32 i;
58         };
59
60         struct ListDrawSpec
61         {
62                 ListDrawSpec()
63                 {
64                 }
65                 ListDrawSpec(const std::string &a_inventoryname,
66                                 const std::string &a_listname,
67                                 v2s32 a_pos, v2s32 a_geom)
68                 {
69                         inventoryname = a_inventoryname;
70                         listname = a_listname;
71                         pos = a_pos;
72                         geom = a_geom;
73                 }
74
75                 std::string inventoryname;
76                 std::string listname;
77                 v2s32 pos;
78                 v2s32 geom;
79         };
80 public:
81         struct DrawSpec
82         {
83                 DrawSpec()
84                 {
85                 }
86                 DrawSpec(const std::string &a_type,
87                                 const std::string &a_name,
88                                 const std::string &a_subname,
89                                 v2s32 a_pos,
90                                 v2s32 a_geom)
91                 {
92                         type = a_type;
93                         name = a_name;
94                         subname = a_subname;
95                         pos = a_pos;
96                         geom = a_geom;
97                 }
98
99                 std::string type;
100                 std::string name;
101                 std::string subname;
102                 v2s32 pos;
103                 v2s32 geom;
104         };
105         
106         // See .cpp for format
107         static v2s16 makeDrawSpecArrayFromString(
108                         core::array<GUIInventoryMenu::DrawSpec> &draw_spec,
109                         const std::string &data,
110                         const std::string &current_name);
111
112         GUIInventoryMenu(gui::IGUIEnvironment* env,
113                         gui::IGUIElement* parent, s32 id,
114                         IMenuManager *menumgr,
115                         v2s16 menu_size,
116                         InventoryContext *c,
117                         InventoryManager *invmgr
118                         );
119         ~GUIInventoryMenu();
120
121         void setDrawSpec(core::array<DrawSpec> &init_draw_spec)
122         {
123                 m_init_draw_spec = init_draw_spec;
124         }
125
126         void removeChildren();
127         /*
128                 Remove and re-add (or reposition) stuff
129         */
130         void regenerateGui(v2u32 screensize);
131         
132         ItemSpec getItemAtPos(v2s32 p) const;
133         void drawList(const ListDrawSpec &s);
134         void drawMenu();
135
136         bool OnEvent(const SEvent& event);
137         
138 protected:
139         v2s32 getBasePos() const
140         {
141                 return padding + AbsoluteRect.UpperLeftCorner;
142         }
143
144         v2s16 m_menu_size;
145
146         v2s32 padding;
147         v2s32 spacing;
148         v2s32 imgsize;
149         
150         InventoryContext *m_c;
151         InventoryManager *m_invmgr;
152
153         core::array<DrawSpec> m_init_draw_spec;
154         core::array<ListDrawSpec> m_draw_spec;
155
156         ItemSpec *m_selected_item;
157 };
158
159 #endif
160