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();
60 core::rect<s32> rect2(rect.UpperLeftCorner,
61 (core::dimension2d<u32>(rect.getWidth(), 15)));
63 video::SColor bgcolor(128,0,0,0);
64 driver->draw2DRectangle(bgcolor, rect2, clip);
66 font->draw(item->getText().c_str(), rect2,
67 video::SColor(255,255,255,255), false, false,
77 GUIInventorySlot::GUIInventorySlot(gui::IGUIEnvironment* env,
78 gui::IGUIElement* parent, s32 id, core::rect<s32> rect):
79 IGUIElement(gui::EGUIET_ELEMENT, env, parent, id, rect)
84 void GUIInventorySlot::draw()
89 drawInventoryItem(Environment, m_item, AbsoluteRect,
90 &AbsoluteClippingRect);
92 gui::IGUIElement::draw();
95 bool GUIInventorySlot::OnEvent(const SEvent& event)
98 return IGUIElement::OnEvent(event);*/
100 switch(event.EventType)
102 case EET_MOUSE_INPUT_EVENT:
103 if (event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
105 dstream<<"Slot pressed"<<std::endl;
109 if (event.MouseInput.Event == EMIE_LMOUSE_LEFT_UP)
111 dstream<<"Slot released"<<std::endl;
119 return Parent ? Parent->OnEvent(event) : false;
126 GUIInventoryMenu::GUIInventoryMenu(gui::IGUIEnvironment* env,
127 gui::IGUIElement* parent, s32 id,
128 Inventory *inventory):
129 IGUIElement(gui::EGUIET_ELEMENT, env, parent, id,
130 core::rect<s32>(0,0,100,100))
132 m_inventory = inventory;
133 m_screensize_old = v2u32(0,0);
140 GUIInventoryMenu::~GUIInventoryMenu()
144 void GUIInventoryMenu::resizeGui()
146 video::IVideoDriver* driver = Environment->getVideoDriver();
147 v2u32 screensize = driver->getScreenSize();
148 if(screensize == m_screensize_old)
150 m_screensize_old = screensize;
152 core::rect<s32> rect(
153 screensize.X/2 - 560/2,
154 screensize.Y/2 - 480/2,
155 screensize.X/2 + 560/2,
156 screensize.Y/2 + 480/2
160 recalculateAbsolutePosition(false);
163 void GUIInventoryMenu::update()
167 void GUIInventoryMenu::draw()
172 gui::IGUISkin* skin = Environment->getSkin();
175 video::IVideoDriver* driver = Environment->getVideoDriver();
177 video::SColor bgcolor(140,0,0,0);
178 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
185 InventoryList *ilist = m_inventory->getList("main");
188 core::rect<s32> imgsize(0,0,48,48);
189 v2s32 basepos(30, 210);
190 basepos += AbsoluteRect.UpperLeftCorner;
191 for(s32 i=0; i<PLAYER_INVENTORY_SIZE; i++)
196 core::rect<s32> rect = imgsize + basepos + p;
197 drawInventoryItem(Environment, ilist->getItem(i),
198 rect, &AbsoluteClippingRect);
204 InventoryList *ilist = m_inventory->getList("craft");
207 core::rect<s32> imgsize(0,0,48,48);
208 v2s32 basepos(30, 30);
209 basepos += AbsoluteRect.UpperLeftCorner;
210 for(s32 i=0; i<9; i++)
215 core::rect<s32> rect = imgsize + basepos + p;
216 drawInventoryItem(Environment, ilist->getItem(i),
217 rect, &AbsoluteClippingRect);
225 gui::IGUIElement::draw();
228 bool GUIInventoryMenu::OnEvent(const SEvent& event)
230 if(event.EventType==EET_KEY_INPUT_EVENT)
232 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
237 if(event.KeyInput.Key==KEY_KEY_I && event.KeyInput.PressedDown)
243 if(event.EventType==EET_MOUSE_INPUT_EVENT)
245 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN)
249 if(event.EventType==EET_GUI_EVENT)
251 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
254 if(!canTakeFocus(event.GUIEvent.Element))
256 dstream<<"GUIInventoryMenu: Not allowing focus change."
258 // Returning true disables focus change
262 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
264 /*switch(event.GUIEvent.Caller->getID())
266 case 256: // continue
276 return Parent ? Parent->OnEvent(event) : false;