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