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()
99 delete m_selected_item;
102 void GUIInventoryMenu::removeChildren()
105 gui::IGUIElement *e = getElementFromId(256);
111 void GUIInventoryMenu::regenerateGui(v2u32 screensize)
116 padding = v2s32(24,24);
117 spacing = v2s32(60,56);
118 imgsize = v2s32(48,48);
123 padding.X*2+spacing.X*(8-1)+imgsize.X,
124 padding.Y*2+spacing.Y*(7-1)+imgsize.Y + helptext_h
127 core::rect<s32> rect(
128 screensize.X/2 - size.X/2,
129 screensize.Y/2 - size.Y/2,
130 screensize.X/2 + size.X/2,
131 screensize.Y/2 + size.Y/2
135 recalculateAbsolutePosition(false);
137 v2s32 basepos = getBasePos();
139 m_draw_positions.clear();
140 m_draw_positions.push_back(ListDrawSpec("main",
141 basepos + v2s32(spacing.X*0, spacing.Y*3), v2s32(8, 4)));
142 m_draw_positions.push_back(ListDrawSpec("craft",
143 basepos + v2s32(spacing.X*3, spacing.Y*0), v2s32(3, 3)));
144 m_draw_positions.push_back(ListDrawSpec("craftresult",
145 basepos + v2s32(spacing.X*7, spacing.Y*1), v2s32(1, 1)));
149 core::rect<s32> rect(0, 0, size.X-padding.X*2, helptext_h);
150 rect = rect + v2s32(size.X/2 - rect.getWidth()/2,
151 size.Y-rect.getHeight()-15);
152 const wchar_t *text =
153 L"Left click: Move all items, Right click: Move single item";
154 Environment->addStaticText(text, rect, false, true, this, 256);
158 GUIInventoryMenu::ItemSpec GUIInventoryMenu::getItemAtPos(v2s32 p) const
160 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
162 for(u32 i=0; i<m_draw_positions.size(); i++)
164 const ListDrawSpec &s = m_draw_positions[i];
166 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
168 s32 x = (i%s.geom.X) * spacing.X;
169 s32 y = (i/s.geom.X) * spacing.Y;
171 core::rect<s32> rect = imgrect + s.pos + p0;
172 if(rect.isPointInside(p))
174 return ItemSpec(s.listname, i);
179 return ItemSpec("", -1);
182 //void GUIInventoryMenu::drawList(const std::string &name, v2s32 pos, v2s32 geom)
183 void GUIInventoryMenu::drawList(const ListDrawSpec &s)
185 video::IVideoDriver* driver = Environment->getVideoDriver();
187 InventoryList *ilist = m_inventory->getList(s.listname);
189 core::rect<s32> imgrect(0,0,imgsize.X,imgsize.Y);
191 for(s32 i=0; i<s.geom.X*s.geom.Y; i++)
193 s32 x = (i%s.geom.X) * spacing.X;
194 s32 y = (i/s.geom.X) * spacing.Y;
196 core::rect<s32> rect = imgrect + s.pos + p;
197 InventoryItem *item = NULL;
199 item = ilist->getItem(i);
201 if(m_selected_item != NULL && m_selected_item->listname == s.listname
202 && m_selected_item->i == i)
204 driver->draw2DRectangle(video::SColor(255,255,0,0),
205 core::rect<s32>(rect.UpperLeftCorner - v2s32(2,2),
206 rect.LowerRightCorner + v2s32(2,2)),
207 &AbsoluteClippingRect);
209 drawInventoryItem(Environment, item,
210 rect, &AbsoluteClippingRect);
214 void GUIInventoryMenu::drawMenu()
216 gui::IGUISkin* skin = Environment->getSkin();
219 video::IVideoDriver* driver = Environment->getVideoDriver();
221 video::SColor bgcolor(140,0,0,0);
222 driver->draw2DRectangle(bgcolor, AbsoluteRect, &AbsoluteClippingRect);
228 for(u32 i=0; i<m_draw_positions.size(); i++)
230 ListDrawSpec &s = m_draw_positions[i];
237 gui::IGUIElement::draw();
240 bool GUIInventoryMenu::OnEvent(const SEvent& event)
242 if(event.EventType==EET_KEY_INPUT_EVENT)
244 if(event.KeyInput.Key==KEY_ESCAPE && event.KeyInput.PressedDown)
249 if(event.KeyInput.Key==KEY_KEY_I && event.KeyInput.PressedDown)
255 if(event.EventType==EET_MOUSE_INPUT_EVENT)
257 if(event.MouseInput.Event == EMIE_LMOUSE_PRESSED_DOWN
258 || event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN)
260 bool right = (event.MouseInput.Event == EMIE_RMOUSE_PRESSED_DOWN);
261 v2s32 p(event.MouseInput.X, event.MouseInput.Y);
262 //dstream<<"Mouse down at p=("<<p.X<<","<<p.Y<<")"<<std::endl;
263 ItemSpec s = getItemAtPos(p);
266 //dstream<<"Mouse down on "<<s.listname<<" "<<s.i<<std::endl;
269 InventoryList *list_from =
270 m_inventory->getList(m_selected_item->listname);
271 InventoryList *list_to =
272 m_inventory->getList(s.listname);
273 // Indicates whether source slot completely empties
274 bool source_empties = false;
275 if(list_from && list_to
276 && list_from->getItem(m_selected_item->i) != NULL)
278 dstream<<"Queueing IACTION_MOVE"<<std::endl;
279 IMoveAction *a = new IMoveAction();
280 a->count = right ? 1 : 0;
281 a->from_name = m_selected_item->listname;
282 a->from_i = m_selected_item->i;
283 a->to_name = s.listname;
285 m_actions->push_back(a);
287 if(list_from->getItem(m_selected_item->i)->getCount()==1)
288 source_empties = true;
290 // Remove selection if target was left-clicked or source
292 if(right == false || source_empties)
294 delete m_selected_item;
295 m_selected_item = NULL;
303 InventoryList *list = m_inventory->getList(s.listname);
304 if(list->getItem(s.i) != NULL)
306 m_selected_item = new ItemSpec(s);
314 delete m_selected_item;
315 m_selected_item = NULL;
320 if(event.EventType==EET_GUI_EVENT)
322 if(event.GUIEvent.EventType==gui::EGET_ELEMENT_FOCUS_LOST
325 if(!canTakeFocus(event.GUIEvent.Element))
327 dstream<<"GUIInventoryMenu: Not allowing focus change."
329 // Returning true disables focus change
333 if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
335 /*switch(event.GUIEvent.Caller->getID())
337 case 256: // continue
347 return Parent ? Parent->OnEvent(event) : false;