3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
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.
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.
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.
21 #include "guiInventoryMenu.h"
22 #include "constants.h"
24 void drawInventoryItem(gui::IGUIEnvironment* env,
25 InventoryItem *item, core::rect<s32> rect,
26 const core::rect<s32> *clip)
28 gui::IGUISkin* skin = env->getSkin();
31 video::IVideoDriver* driver = env->getVideoDriver();
33 video::ITexture *texture = NULL;
37 texture = item->getImage();
42 const video::SColor color(255,255,255,255);
43 const video::SColor colors[] = {color,color,color,color};
44 driver->draw2DImage(texture, rect,
45 core::rect<s32>(core::position2d<s32>(0,0),
46 core::dimension2di(texture->getOriginalSize())),
51 video::SColor bgcolor(128,128,128,128);
52 driver->draw2DRectangle(bgcolor, rect, clip);
57 gui::IGUIFont *font = skin->getFont();
58 std::string text = item->getText();
59 if(font && text != "")
61 core::rect<s32> rect2(rect.UpperLeftCorner,
62 (core::dimension2d<u32>(rect.getWidth(), 15)));
64 video::SColor bgcolor(128,0,0,0);
65 driver->draw2DRectangle(bgcolor, rect2, clip);
67 font->draw(text.c_str(), rect2,
68 video::SColor(255,255,255,255), false, false,
78 GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
79 gui::IGUIElement* parent, s32 id,
81 Queue<InventoryAction*> *actions,
82 int *active_menu_count):
83 GUIModalMenu(env, parent, id, active_menu_count)
85 m_inventory = inventory;
86 m_selected_item = NULL;
89 /*m_selected_item = new ItemSpec;
90 m_selected_item->listname = "main";
91 m_selected_item->i = 3;*/
94 GUIInventoryMenu::~GUIInventoryMenu()
97 delete m_selected_item;
100 void GUIInventoryMenu::regenerateGui(v2u32 screensize)
102 padding = v2s32(24,24);
103 spacing = v2s32(60,56);
104 imgsize = v2s32(48,48);
107 padding.X*2+spacing.X*(8-1)+imgsize.X,
108 padding.Y*2+spacing.Y*(7-1)+imgsize.Y
111 core::rect<s32> rect(
112 screensize.X/2 - size.X/2,
113 screensize.Y/2 - size.Y/2,
114 screensize.X/2 + size.X/2,
115 screensize.Y/2 + size.Y/2
119 recalculateAbsolutePosition(false);
121 v2s32 basepos = getBasePos();
123 m_draw_positions.clear();
124 m_draw_positions.push_back(ListDrawSpec("main",
125 basepos + v2s32(spacing.X*0, spacing.Y*3), v2s32(8, 4)));
126 m_draw_positions.push_back(ListDrawSpec("craft",
127 basepos + v2s32(spacing.X*3, spacing.Y*0), v2s32(3, 3)));
128 m_draw_positions.push_back(ListDrawSpec("craftresult",
129 basepos + v2s32(spacing.X*7, spacing.Y*1), v2s32(1, 1)));
132 GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
134 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
136 for(u32 i=0; i<m_draw_positions.size(); i++)
138 const ListDrawSpec &s = m_draw_positions[i];
140 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
142 s32 x = (i%s.geom.X) * spacing.X;
143 s32 y = (i/s.geom.X) * spacing.Y;
145 core::rect<s32> rect = imgrect + s.pos + p0;
146 if(rect.isPointInside(p))
148 return ItemSpec(s.listname, i);
153 return ItemSpec("", -1);
156 //void GUIInventoryMenu::drawList(const std::string &name, v2s32 pos, v2s32 geom)
157 void GUIInventoryMenu::drawList(const ListDrawSpec &s)
159 video::IVideoDriver* driver = Environment->getVideoDriver();
161 InventoryList *ilist = m_inventory->getList(s.listname);
163 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
165 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
167 s32 x = (i%s.geom.X) * spacing.X;
168 s32 y = (i/s.geom.X) * spacing.Y;
170 core::rect<s32> rect = imgrect + s.pos + p;
171 InventoryItem *item = NULL;
173 item = ilist->getItem(i);
175 if(m_selected_item != NULL && m_selected_item->listname == s.listname
176 && m_selected_item->i == i)
178 driver->draw2DRectangle(video::SColor(255,255,0,0),
179 core::rect<s32>(rect.UpperLeftCorner - v2s32(2,2),
180 rect.LowerRightCorner + v2s32(2,2)),
181 &AbsoluteClippingRect);
183 drawInventoryItem(Environment, item,
184 rect, &AbsoluteClippingRect);
188 void GUIInventoryMenu::drawMenu()
190 gui::IGUISkin* skin = Environment->getSkin();
193 video::IVideoDriver* driver = Environment->getVideoDriver();
195 video::SColor bgcolor(140,0,0,0);
196 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
202 for(u32 i=0; i<m_draw_positions.size(); i++)
204 ListDrawSpec &s = m_draw_positions[i];
211 gui::IGUIElement::draw();
214 bool GUIInventoryMenu::OnEvent(const SEvent& event)
216 if(event.EventType==EET_KEY_INPUT_EVENT)
218 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
223 if(event.KeyInput.Key==KEY_KEY_I && event.KeyInput.PressedDown)
229 if(event.EventType==EET_MOUSE_INPUT_EVENT)
231 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
233 v2s32 p(event.MouseInput.X, event.MouseInput.Y);
234 //dstream<<"Mouse down at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
235 ItemSpec s = getItemAtPos(p);
238 //dstream<<"Mouse down on "<<s.listname<<" "<<s.i<<std::endl;
241 InventoryList *list_from =
242 m_inventory->getList(m_selected_item->listname);
243 InventoryList *list_to =
244 m_inventory->getList(s.listname);
245 if(list_from && list_to
246 && list_from->getItem(m_selected_item->i) != NULL)
248 dstream<<"Queueing IACTION_MOVE"<<std::endl;
252 a->from_name = m_selected_item->listname;
253 a->from_i = m_selected_item->i;
254 a->to_name = s.listname;
256 m_actions->push_back(a);
258 delete m_selected_item;
259 m_selected_item = NULL;
266 InventoryList *list = m_inventory->getList(s.listname);
267 if(list->getItem(s.i) != NULL)
269 m_selected_item = new ItemSpec(s);
277 delete m_selected_item;
278 m_selected_item = NULL;
283 if(event.EventType==EET_GUI_EVENT)
285 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
288 if(!canTakeFocus(event.GUIEvent.Element))
290 dstream<<"GUIInventoryMenu: Not allowing focus change."
292 // Returning true disables focus change
296 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
298 /*switch(event.GUIEvent.Caller->getID())
300 case 256: // continue
310 return Parent ? Parent->OnEvent(event) : false;