Line data Source code
1 : /*
2 : Minetest
3 : Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 : Copyright (C) 2010-2013 blue42u, Jonathon Anderson <anderjon@umail.iu.edu>
5 : Copyright (C) 2010-2013 kwolekr, Ryan Kwolek <kwolekr@minetest.net>
6 :
7 : This program is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU Lesser General Public License as published by
9 : the Free Software Foundation; either version 2.1 of the License, or
10 : (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU Lesser General Public License for more details.
16 :
17 : You should have received a copy of the GNU Lesser General Public License along
18 : with this program; if not, write to the Free Software Foundation, Inc.,
19 : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 : */
21 :
22 : #include "hud.h"
23 : #include "settings.h"
24 : #include "util/numeric.h"
25 : #include "log.h"
26 : #include "gamedef.h"
27 : #include "itemdef.h"
28 : #include "inventory.h"
29 : #include "client/tile.h"
30 : #include "localplayer.h"
31 : #include "camera.h"
32 : #include "porting.h"
33 : #include "fontengine.h"
34 : #include "guiscalingfilter.h"
35 : #include <IGUIStaticText.h>
36 :
37 : #ifdef HAVE_TOUCHSCREENGUI
38 : #include "touchscreengui.h"
39 : #endif
40 :
41 1 : Hud::Hud(video::IVideoDriver *driver, scene::ISceneManager* smgr,
42 : gui::IGUIEnvironment* guienv, IGameDef *gamedef, LocalPlayer *player,
43 1 : Inventory *inventory) {
44 1 : this->driver = driver;
45 1 : this->smgr = smgr;
46 1 : this->guienv = guienv;
47 1 : this->gamedef = gamedef;
48 1 : this->player = player;
49 1 : this->inventory = inventory;
50 :
51 1 : m_screensize = v2u32(0, 0);
52 1 : m_displaycenter = v2s32(0, 0);
53 1 : m_hotbar_imagesize = floor(HOTBAR_IMAGE_SIZE * porting::getDisplayDensity() + 0.5);
54 1 : m_hotbar_imagesize *= g_settings->getFloat("hud_scaling");
55 1 : m_padding = m_hotbar_imagesize / 12;
56 :
57 1 : const video::SColor hbar_color(255, 255, 255, 255);
58 5 : for (unsigned int i=0; i < 4; i++ ){
59 4 : hbar_colors[i] = hbar_color;
60 : }
61 :
62 1 : tsrc = gamedef->getTextureSource();
63 :
64 1 : v3f crosshair_color = g_settings->getV3F("crosshair_color");
65 1 : u32 cross_r = rangelim(myround(crosshair_color.X), 0, 255);
66 1 : u32 cross_g = rangelim(myround(crosshair_color.Y), 0, 255);
67 1 : u32 cross_b = rangelim(myround(crosshair_color.Z), 0, 255);
68 1 : u32 cross_a = rangelim(g_settings->getS32("crosshair_alpha"), 0, 255);
69 1 : crosshair_argb = video::SColor(cross_a, cross_r, cross_g, cross_b);
70 :
71 1 : v3f selectionbox_color = g_settings->getV3F("selectionbox_color");
72 1 : u32 sbox_r = rangelim(myround(selectionbox_color.X), 0, 255);
73 1 : u32 sbox_g = rangelim(myround(selectionbox_color.Y), 0, 255);
74 1 : u32 sbox_b = rangelim(myround(selectionbox_color.Z), 0, 255);
75 1 : selectionbox_argb = video::SColor(255, sbox_r, sbox_g, sbox_b);
76 :
77 1 : use_crosshair_image = tsrc->isKnownSourceImage("crosshair.png");
78 :
79 1 : hotbar_image = "";
80 1 : use_hotbar_image = false;
81 1 : hotbar_selected_image = "";
82 1 : use_hotbar_selected_image = false;
83 1 : }
84 :
85 9328 : void Hud::drawItem(const ItemStack &item, const core::rect<s32>& rect, bool selected) {
86 :
87 9328 : if (selected) {
88 : /* draw hihlighting around selected item */
89 1166 : if (use_hotbar_selected_image) {
90 1166 : core::rect<s32> imgrect2 = rect;
91 1166 : imgrect2.UpperLeftCorner.X -= (m_padding*2);
92 1166 : imgrect2.UpperLeftCorner.Y -= (m_padding*2);
93 1166 : imgrect2.LowerRightCorner.X += (m_padding*2);
94 1166 : imgrect2.LowerRightCorner.Y += (m_padding*2);
95 1166 : video::ITexture *texture = tsrc->getTexture(hotbar_selected_image);
96 1166 : core::dimension2di imgsize(texture->getOriginalSize());
97 2332 : draw2DImageFilterScaled(driver, texture, imgrect2,
98 : core::rect<s32>(core::position2d<s32>(0,0), imgsize),
99 1166 : NULL, hbar_colors, true);
100 : } else {
101 0 : video::SColor c_outside(255,255,0,0);
102 : //video::SColor c_outside(255,0,0,0);
103 : //video::SColor c_inside(255,192,192,192);
104 0 : s32 x1 = rect.UpperLeftCorner.X;
105 0 : s32 y1 = rect.UpperLeftCorner.Y;
106 0 : s32 x2 = rect.LowerRightCorner.X;
107 0 : s32 y2 = rect.LowerRightCorner.Y;
108 : // Black base borders
109 0 : driver->draw2DRectangle(c_outside,
110 : core::rect<s32>(
111 0 : v2s32(x1 - m_padding, y1 - m_padding),
112 0 : v2s32(x2 + m_padding, y1)
113 0 : ), NULL);
114 0 : driver->draw2DRectangle(c_outside,
115 : core::rect<s32>(
116 0 : v2s32(x1 - m_padding, y2),
117 0 : v2s32(x2 + m_padding, y2 + m_padding)
118 0 : ), NULL);
119 0 : driver->draw2DRectangle(c_outside,
120 : core::rect<s32>(
121 0 : v2s32(x1 - m_padding, y1),
122 : v2s32(x1, y2)
123 0 : ), NULL);
124 0 : driver->draw2DRectangle(c_outside,
125 : core::rect<s32>(
126 : v2s32(x2, y1),
127 0 : v2s32(x2 + m_padding, y2)
128 0 : ), NULL);
129 : /*// Light inside borders
130 : driver->draw2DRectangle(c_inside,
131 : core::rect<s32>(
132 : v2s32(x1 - padding/2, y1 - padding/2),
133 : v2s32(x2 + padding/2, y1)
134 : ), NULL);
135 : driver->draw2DRectangle(c_inside,
136 : core::rect<s32>(
137 : v2s32(x1 - padding/2, y2),
138 : v2s32(x2 + padding/2, y2 + padding/2)
139 : ), NULL);
140 : driver->draw2DRectangle(c_inside,
141 : core::rect<s32>(
142 : v2s32(x1 - padding/2, y1),
143 : v2s32(x1, y2)
144 : ), NULL);
145 : driver->draw2DRectangle(c_inside,
146 : core::rect<s32>(
147 : v2s32(x2, y1),
148 : v2s32(x2 + padding/2, y2)
149 : ), NULL);
150 : */
151 : }
152 : }
153 :
154 9328 : video::SColor bgcolor2(128, 0, 0, 0);
155 9328 : if (!use_hotbar_image)
156 0 : driver->draw2DRectangle(bgcolor2, rect, NULL);
157 9328 : drawItemStack(driver, g_fontengine->getFont(), item, rect, NULL, gamedef);
158 9328 : }
159 :
160 : //NOTE: selectitem = 0 -> no selected; selectitem 1-based
161 1166 : void Hud::drawItems(v2s32 upperleftpos, s32 itemcount, s32 offset,
162 : InventoryList *mainlist, u16 selectitem, u16 direction)
163 : {
164 : #ifdef HAVE_TOUCHSCREENGUI
165 : if ( (g_touchscreengui) && (offset == 0))
166 : g_touchscreengui->resetHud();
167 : #endif
168 :
169 1166 : s32 height = m_hotbar_imagesize + m_padding * 2;
170 1166 : s32 width = (itemcount - offset) * (m_hotbar_imagesize + m_padding * 2);
171 :
172 1166 : if (direction == HUD_DIR_TOP_BOTTOM || direction == HUD_DIR_BOTTOM_TOP) {
173 0 : width = m_hotbar_imagesize + m_padding * 2;
174 0 : height = (itemcount - offset) * (m_hotbar_imagesize + m_padding * 2);
175 : }
176 :
177 : // Position of upper left corner of bar
178 1166 : v2s32 pos = upperleftpos;
179 :
180 1166 : if (hotbar_image != player->hotbar_image) {
181 1 : hotbar_image = player->hotbar_image;
182 1 : if (hotbar_image != "")
183 1 : use_hotbar_image = tsrc->isKnownSourceImage(hotbar_image);
184 : else
185 0 : use_hotbar_image = false;
186 : }
187 :
188 1166 : if (hotbar_selected_image != player->hotbar_selected_image) {
189 1 : hotbar_selected_image = player->hotbar_selected_image;
190 1 : if (hotbar_selected_image != "")
191 1 : use_hotbar_selected_image = tsrc->isKnownSourceImage(hotbar_selected_image);
192 : else
193 0 : use_hotbar_selected_image = false;
194 : }
195 :
196 : /* draw customized item background */
197 1166 : if (use_hotbar_image) {
198 2332 : core::rect<s32> imgrect2(-m_padding/2, -m_padding/2,
199 3498 : width+m_padding/2, height+m_padding/2);
200 1166 : core::rect<s32> rect2 = imgrect2 + pos;
201 1166 : video::ITexture *texture = tsrc->getTexture(hotbar_image);
202 1166 : core::dimension2di imgsize(texture->getOriginalSize());
203 2332 : draw2DImageFilterScaled(driver, texture, rect2,
204 : core::rect<s32>(core::position2d<s32>(0,0), imgsize),
205 1166 : NULL, hbar_colors, true);
206 : }
207 :
208 10494 : for (s32 i = offset; i < itemcount && (size_t)i < mainlist->getSize(); i++)
209 : {
210 9328 : v2s32 steppos;
211 9328 : s32 fullimglen = m_hotbar_imagesize + m_padding * 2;
212 :
213 9328 : core::rect<s32> imgrect(0, 0, m_hotbar_imagesize, m_hotbar_imagesize);
214 :
215 9328 : switch (direction) {
216 : case HUD_DIR_RIGHT_LEFT:
217 0 : steppos = v2s32(-(m_padding + (i - offset) * fullimglen), m_padding);
218 0 : break;
219 : case HUD_DIR_TOP_BOTTOM:
220 0 : steppos = v2s32(m_padding, m_padding + (i - offset) * fullimglen);
221 0 : break;
222 : case HUD_DIR_BOTTOM_TOP:
223 0 : steppos = v2s32(m_padding, -(m_padding + (i - offset) * fullimglen));
224 0 : break;
225 : default:
226 9328 : steppos = v2s32(m_padding + (i - offset) * fullimglen, m_padding);
227 9328 : break;
228 : }
229 :
230 9328 : drawItem(mainlist->getItem(i), (imgrect + pos + steppos), (i +1) == selectitem );
231 :
232 : #ifdef HAVE_TOUCHSCREENGUI
233 : if (g_touchscreengui)
234 : g_touchscreengui->registerHudItem(i, (imgrect + pos + steppos));
235 : #endif
236 : }
237 1166 : }
238 :
239 :
240 1166 : void Hud::drawLuaElements(v3s16 camera_offset) {
241 1166 : u32 text_height = g_fontengine->getTextHeight();
242 1166 : irr::gui::IGUIFont* font = g_fontengine->getFont();
243 3498 : for (size_t i = 0; i != player->maxHudId(); i++) {
244 2332 : HudElement *e = player->getHud(i);
245 2332 : if (!e)
246 0 : continue;
247 :
248 2332 : v2s32 pos(floor(e->pos.X * (float) m_screensize.X + 0.5),
249 4664 : floor(e->pos.Y * (float) m_screensize.Y + 0.5));
250 2332 : switch (e->type) {
251 : case HUD_ELEM_IMAGE: {
252 0 : video::ITexture *texture = tsrc->getTexture(e->text);
253 0 : if (!texture)
254 0 : continue;
255 :
256 0 : const video::SColor color(255, 255, 255, 255);
257 0 : const video::SColor colors[] = {color, color, color, color};
258 0 : core::dimension2di imgsize(texture->getOriginalSize());
259 0 : v2s32 dstsize(imgsize.Width * e->scale.X,
260 0 : imgsize.Height * e->scale.Y);
261 0 : if (e->scale.X < 0)
262 0 : dstsize.X = m_screensize.X * (e->scale.X * -0.01);
263 0 : if (e->scale.Y < 0)
264 0 : dstsize.Y = m_screensize.Y * (e->scale.Y * -0.01);
265 0 : v2s32 offset((e->align.X - 1.0) * dstsize.X / 2,
266 0 : (e->align.Y - 1.0) * dstsize.Y / 2);
267 0 : core::rect<s32> rect(0, 0, dstsize.X, dstsize.Y);
268 0 : rect += pos + offset + v2s32(e->offset.X, e->offset.Y);
269 0 : draw2DImageFilterScaled(driver, texture, rect,
270 : core::rect<s32>(core::position2d<s32>(0,0), imgsize),
271 0 : NULL, colors, true);
272 0 : break; }
273 : case HUD_ELEM_TEXT: {
274 1166 : video::SColor color(255, (e->number >> 16) & 0xFF,
275 1166 : (e->number >> 8) & 0xFF,
276 3498 : (e->number >> 0) & 0xFF);
277 1166 : core::rect<s32> size(0, 0, e->scale.X, text_height * e->scale.Y);
278 2332 : std::wstring text = narrow_to_wide(e->text);
279 1166 : core::dimension2d<u32> textsize = font->getDimension(text.c_str());
280 1166 : v2s32 offset((e->align.X - 1.0) * (textsize.Width / 2),
281 2332 : (e->align.Y - 1.0) * (textsize.Height / 2));
282 1166 : v2s32 offs(e->offset.X, e->offset.Y);
283 1166 : font->draw(text.c_str(), size + pos + offset + offs, color);
284 1166 : break; }
285 : case HUD_ELEM_STATBAR: {
286 1166 : v2s32 offs(e->offset.X, e->offset.Y);
287 1166 : drawStatbar(pos, HUD_CORNER_UPPER, e->dir, e->text, e->number, offs, e->size);
288 1166 : break; }
289 : case HUD_ELEM_INVENTORY: {
290 0 : InventoryList *inv = inventory->getList(e->text);
291 0 : drawItems(pos, e->number, 0, inv, e->item, e->dir);
292 0 : break; }
293 : case HUD_ELEM_WAYPOINT: {
294 0 : v3f p_pos = player->getPosition() / BS;
295 0 : v3f w_pos = e->world_pos * BS;
296 0 : float distance = floor(10 * p_pos.getDistanceFrom(e->world_pos)) / 10;
297 0 : scene::ICameraSceneNode* camera = smgr->getActiveCamera();
298 0 : w_pos -= intToFloat(camera_offset, BS);
299 0 : core::matrix4 trans = camera->getProjectionMatrix();
300 0 : trans *= camera->getViewMatrix();
301 0 : f32 transformed_pos[4] = { w_pos.X, w_pos.Y, w_pos.Z, 1.0f };
302 0 : trans.multiplyWith1x4Matrix(transformed_pos);
303 0 : if (transformed_pos[3] < 0)
304 0 : break;
305 0 : f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
306 0 : core::reciprocal(transformed_pos[3]);
307 0 : pos.X = m_screensize.X * (0.5 * transformed_pos[0] * zDiv + 0.5);
308 0 : pos.Y = m_screensize.Y * (0.5 - transformed_pos[1] * zDiv * 0.5);
309 0 : video::SColor color(255, (e->number >> 16) & 0xFF,
310 0 : (e->number >> 8) & 0xFF,
311 0 : (e->number >> 0) & 0xFF);
312 0 : core::rect<s32> size(0, 0, 200, 2 * text_height);
313 0 : std::wstring text = narrow_to_wide(e->name);
314 0 : font->draw(text.c_str(), size + pos, color);
315 0 : std::ostringstream os;
316 0 : os<<distance<<e->text;
317 0 : text = narrow_to_wide(os.str());
318 0 : pos.Y += text_height;
319 0 : font->draw(text.c_str(), size + pos, color);
320 0 : break; }
321 : default:
322 0 : infostream << "Hud::drawLuaElements: ignoring drawform " << e->type <<
323 0 : " of hud element ID " << i << " due to unrecognized type" << std::endl;
324 : }
325 : }
326 1166 : }
327 :
328 :
329 1166 : void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir, std::string texture,
330 : s32 count, v2s32 offset, v2s32 size)
331 : {
332 1166 : const video::SColor color(255, 255, 255, 255);
333 1166 : const video::SColor colors[] = {color, color, color, color};
334 :
335 1166 : video::ITexture *stat_texture = tsrc->getTexture(texture);
336 1166 : if (!stat_texture)
337 0 : return;
338 :
339 1166 : core::dimension2di srcd(stat_texture->getOriginalSize());
340 1166 : core::dimension2di dstd;
341 1166 : if (size == v2s32()) {
342 0 : dstd = srcd;
343 : } else {
344 3498 : double size_factor = g_settings->getFloat("hud_scaling") *
345 2332 : porting::getDisplayDensity();
346 1166 : dstd.Height = size.Y * size_factor;
347 1166 : dstd.Width = size.X * size_factor;
348 1166 : offset.X *= size_factor;
349 1166 : offset.Y *= size_factor;
350 : }
351 :
352 1166 : v2s32 p = pos;
353 1166 : if (corner & HUD_CORNER_LOWER)
354 0 : p -= dstd.Height;
355 :
356 1166 : p += offset;
357 :
358 1166 : v2s32 steppos;
359 1166 : switch (drawdir) {
360 : case HUD_DIR_RIGHT_LEFT:
361 0 : steppos = v2s32(-1, 0);
362 0 : break;
363 : case HUD_DIR_TOP_BOTTOM:
364 0 : steppos = v2s32(0, 1);
365 0 : break;
366 : case HUD_DIR_BOTTOM_TOP:
367 0 : steppos = v2s32(0, -1);
368 0 : break;
369 : default:
370 1166 : steppos = v2s32(1, 0);
371 : }
372 1166 : steppos.X *= dstd.Width;
373 1166 : steppos.Y *= dstd.Height;
374 :
375 12826 : for (s32 i = 0; i < count / 2; i++)
376 : {
377 11660 : core::rect<s32> srcrect(0, 0, srcd.Width, srcd.Height);
378 11660 : core::rect<s32> dstrect(0,0, dstd.Width, dstd.Height);
379 :
380 11660 : dstrect += p;
381 11660 : draw2DImageFilterScaled(driver, stat_texture, dstrect, srcrect, NULL, colors, true);
382 11660 : p += steppos;
383 : }
384 :
385 1166 : if (count % 2 == 1)
386 : {
387 0 : core::rect<s32> srcrect(0, 0, srcd.Width / 2, srcd.Height);
388 0 : core::rect<s32> dstrect(0,0, dstd.Width / 2, dstd.Height);
389 :
390 0 : dstrect += p;
391 0 : draw2DImageFilterScaled(driver, stat_texture, dstrect, srcrect, NULL, colors, true);
392 : }
393 : }
394 :
395 :
396 1166 : void Hud::drawHotbar(u16 playeritem) {
397 :
398 1166 : v2s32 centerlowerpos(m_displaycenter.X, m_screensize.Y);
399 :
400 1166 : InventoryList *mainlist = inventory->getList("main");
401 1166 : if (mainlist == NULL) {
402 : //silently ignore this we may not be initialized completely
403 0 : return;
404 : }
405 :
406 1166 : s32 hotbar_itemcount = player->hud_hotbar_itemcount;
407 1166 : s32 width = hotbar_itemcount * (m_hotbar_imagesize + m_padding * 2);
408 1166 : v2s32 pos = centerlowerpos - v2s32(width / 2, m_hotbar_imagesize + m_padding * 3);
409 :
410 3498 : if ( (float) width / (float) porting::getWindowSize().X <=
411 2332 : g_settings->getFloat("hud_hotbar_max_width")) {
412 1166 : if (player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE) {
413 1166 : drawItems(pos, hotbar_itemcount, 0, mainlist, playeritem + 1, 0);
414 : }
415 : }
416 : else {
417 0 : pos.X += width/4;
418 :
419 0 : v2s32 secondpos = pos;
420 0 : pos = pos - v2s32(0, m_hotbar_imagesize + m_padding);
421 :
422 0 : if (player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE) {
423 0 : drawItems(pos, hotbar_itemcount/2, 0, mainlist, playeritem + 1, 0);
424 0 : drawItems(secondpos, hotbar_itemcount, hotbar_itemcount/2, mainlist, playeritem + 1, 0);
425 : }
426 : }
427 :
428 : //////////////////////////// compatibility code to be removed //////////////
429 : // this is ugly as hell but there's no other way to keep compatibility to
430 : // old servers
431 1166 : if ((player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE)) {
432 0 : drawStatbar(v2s32(floor(0.5 * (float)m_screensize.X + 0.5),
433 0 : floor(1 * (float) m_screensize.Y + 0.5)),
434 : HUD_CORNER_UPPER, 0, "heart.png",
435 0 : player->hp, v2s32((-10*24)-25,-(48+24+10)), v2s32(24,24));
436 : }
437 :
438 1166 : if ((player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE) &&
439 0 : (player->getBreath() < 11)) {
440 0 : drawStatbar(v2s32(floor(0.5 * (float)m_screensize.X + 0.5),
441 0 : floor(1 * (float) m_screensize.Y + 0.5)),
442 : HUD_CORNER_UPPER, 0, "bubble.png",
443 0 : player->getBreath(), v2s32(25,-(48+24+10)), v2s32(24,24));
444 : }
445 : ////////////////////////////////////////////////////////////////////////////
446 : }
447 :
448 :
449 1166 : void Hud::drawCrosshair() {
450 :
451 1166 : if (use_crosshair_image) {
452 0 : video::ITexture *crosshair = tsrc->getTexture("crosshair.png");
453 0 : v2u32 size = crosshair->getOriginalSize();
454 0 : v2s32 lsize = v2s32(m_displaycenter.X - (size.X / 2),
455 0 : m_displaycenter.Y - (size.Y / 2));
456 0 : driver->draw2DImage(crosshair, lsize,
457 0 : core::rect<s32>(0, 0, size.X, size.Y),
458 0 : 0, crosshair_argb, true);
459 : } else {
460 3498 : driver->draw2DLine(m_displaycenter - v2s32(10, 0),
461 3498 : m_displaycenter + v2s32(10, 0), crosshair_argb);
462 3498 : driver->draw2DLine(m_displaycenter - v2s32(0, 10),
463 3498 : m_displaycenter + v2s32(0, 10), crosshair_argb);
464 : }
465 1166 : }
466 :
467 :
468 1166 : void Hud::drawSelectionBoxes(std::vector<aabb3f> &hilightboxes) {
469 2332 : for (std::vector<aabb3f>::const_iterator
470 1166 : i = hilightboxes.begin();
471 2332 : i != hilightboxes.end(); i++) {
472 0 : driver->draw3DBox(*i, selectionbox_argb);
473 : }
474 1166 : }
475 :
476 :
477 1166 : void Hud::resizeHotbar() {
478 1166 : if (m_screensize != porting::getWindowSize()) {
479 1 : m_hotbar_imagesize = floor(HOTBAR_IMAGE_SIZE * porting::getDisplayDensity() + 0.5);
480 1 : m_hotbar_imagesize *= g_settings->getFloat("hud_scaling");
481 1 : m_padding = m_hotbar_imagesize / 12;
482 1 : m_screensize = porting::getWindowSize();
483 1 : m_displaycenter = v2s32(m_screensize.X/2,m_screensize.Y/2);
484 : }
485 1166 : }
486 :
487 9328 : void drawItemStack(video::IVideoDriver *driver,
488 : gui::IGUIFont *font,
489 : const ItemStack &item,
490 : const core::rect<s32> &rect,
491 : const core::rect<s32> *clip,
492 : IGameDef *gamedef)
493 : {
494 9328 : if(item.empty())
495 1166 : return;
496 :
497 8162 : const ItemDefinition &def = item.getDefinition(gamedef->idef());
498 8162 : video::ITexture *texture = gamedef->idef()->getInventoryTexture(def.name, gamedef);
499 :
500 : // Draw the inventory texture
501 8162 : if(texture != NULL)
502 : {
503 8162 : const video::SColor color(255,255,255,255);
504 8162 : const video::SColor colors[] = {color,color,color,color};
505 16324 : draw2DImageFilterScaled(driver, texture, rect,
506 : core::rect<s32>(core::position2d<s32>(0,0),
507 8162 : core::dimension2di(texture->getOriginalSize())),
508 8162 : clip, colors, true);
509 : }
510 :
511 8162 : if(def.type == ITEM_TOOL && item.wear != 0)
512 : {
513 : // Draw a progressbar
514 5830 : float barheight = rect.getHeight()/16;
515 5830 : float barpad_x = rect.getWidth()/16;
516 5830 : float barpad_y = rect.getHeight()/16;
517 : core::rect<s32> progressrect(
518 5830 : rect.UpperLeftCorner.X + barpad_x,
519 5830 : rect.LowerRightCorner.Y - barpad_y - barheight,
520 5830 : rect.LowerRightCorner.X - barpad_x,
521 23320 : rect.LowerRightCorner.Y - barpad_y);
522 :
523 : // Shrink progressrect by amount of tool damage
524 5830 : float wear = item.wear / 65535.0;
525 : int progressmid =
526 11660 : wear * progressrect.UpperLeftCorner.X +
527 11660 : (1-wear) * progressrect.LowerRightCorner.X;
528 :
529 : // Compute progressbar color
530 : // wear = 0.0: green
531 : // wear = 0.5: yellow
532 : // wear = 1.0: red
533 5830 : video::SColor color(255,255,255,255);
534 5830 : int wear_i = MYMIN(floor(wear * 600), 511);
535 5830 : wear_i = MYMIN(wear_i + 10, 511);
536 5830 : if(wear_i <= 255)
537 4664 : color.set(255, wear_i, 255, 0);
538 : else
539 1166 : color.set(255, 255, 511-wear_i, 0);
540 :
541 5830 : core::rect<s32> progressrect2 = progressrect;
542 5830 : progressrect2.LowerRightCorner.X = progressmid;
543 5830 : driver->draw2DRectangle(color, progressrect2, clip);
544 :
545 5830 : color = video::SColor(255,0,0,0);
546 5830 : progressrect2 = progressrect;
547 5830 : progressrect2.UpperLeftCorner.X = progressmid;
548 5830 : driver->draw2DRectangle(color, progressrect2, clip);
549 : }
550 :
551 8162 : if(font != NULL && item.count >= 2)
552 : {
553 : // Get the item count as a string
554 4664 : std::string text = itos(item.count);
555 2332 : v2u32 dim = font->getDimension(narrow_to_wide(text).c_str());
556 2332 : v2s32 sdim(dim.X,dim.Y);
557 :
558 : core::rect<s32> rect2(
559 : /*rect.UpperLeftCorner,
560 : core::dimension2d<u32>(rect.getWidth(), 15)*/
561 4664 : rect.LowerRightCorner - sdim,
562 : sdim
563 2332 : );
564 :
565 2332 : video::SColor bgcolor(128,0,0,0);
566 2332 : driver->draw2DRectangle(bgcolor, rect2, clip);
567 :
568 2332 : video::SColor color(255,255,255,255);
569 2332 : font->draw(text.c_str(), rect2, color, false, false, clip);
570 : }
571 3 : }
|