hud.cpp 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291
  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. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU Lesser General Public License as published by
  8. the Free Software Foundation; either version 2.1 of the License, or
  9. (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 Lesser General Public License for more details.
  14. You should have received a copy of the GNU Lesser General Public License along
  15. with this program; if not, write to the Free Software Foundation, Inc.,
  16. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include "client/hud.h"
  19. #include <string>
  20. #include <iostream>
  21. #include <cmath>
  22. #include "settings.h"
  23. #include "util/numeric.h"
  24. #include "log.h"
  25. #include "client.h"
  26. #include "inventory.h"
  27. #include "shader.h"
  28. #include "client/tile.h"
  29. #include "localplayer.h"
  30. #include "camera.h"
  31. #include "porting.h"
  32. #include "fontengine.h"
  33. #include "guiscalingfilter.h"
  34. #include "mesh.h"
  35. #include "wieldmesh.h"
  36. #include "client/renderingengine.h"
  37. #include "client/minimap.h"
  38. #include "gui/touchscreengui.h"
  39. #define OBJECT_CROSSHAIR_LINE_SIZE 8
  40. #define CROSSHAIR_LINE_SIZE 10
  41. Hud::Hud(Client *client, LocalPlayer *player,
  42. Inventory *inventory)
  43. {
  44. driver = RenderingEngine::get_video_driver();
  45. this->client = client;
  46. this->player = player;
  47. this->inventory = inventory;
  48. m_hud_scaling = g_settings->getFloat("hud_scaling", 0.5f, 20.0f);
  49. m_scale_factor = m_hud_scaling * RenderingEngine::getDisplayDensity();
  50. m_hotbar_imagesize = std::floor(HOTBAR_IMAGE_SIZE *
  51. RenderingEngine::getDisplayDensity() + 0.5f);
  52. m_hotbar_imagesize *= m_hud_scaling;
  53. m_padding = m_hotbar_imagesize / 12;
  54. for (auto &hbar_color : hbar_colors)
  55. hbar_color = video::SColor(255, 255, 255, 255);
  56. tsrc = client->getTextureSource();
  57. v3f crosshair_color = g_settings->getV3F("crosshair_color");
  58. u32 cross_r = rangelim(myround(crosshair_color.X), 0, 255);
  59. u32 cross_g = rangelim(myround(crosshair_color.Y), 0, 255);
  60. u32 cross_b = rangelim(myround(crosshair_color.Z), 0, 255);
  61. u32 cross_a = rangelim(g_settings->getS32("crosshair_alpha"), 0, 255);
  62. crosshair_argb = video::SColor(cross_a, cross_r, cross_g, cross_b);
  63. v3f selectionbox_color = g_settings->getV3F("selectionbox_color");
  64. u32 sbox_r = rangelim(myround(selectionbox_color.X), 0, 255);
  65. u32 sbox_g = rangelim(myround(selectionbox_color.Y), 0, 255);
  66. u32 sbox_b = rangelim(myround(selectionbox_color.Z), 0, 255);
  67. selectionbox_argb = video::SColor(255, sbox_r, sbox_g, sbox_b);
  68. use_crosshair_image = tsrc->isKnownSourceImage("crosshair.png");
  69. use_object_crosshair_image = tsrc->isKnownSourceImage("object_crosshair.png");
  70. m_selection_boxes.clear();
  71. m_halo_boxes.clear();
  72. std::string mode_setting = g_settings->get("node_highlighting");
  73. if (mode_setting == "halo") {
  74. m_mode = HIGHLIGHT_HALO;
  75. } else if (mode_setting == "none") {
  76. m_mode = HIGHLIGHT_NONE;
  77. } else {
  78. m_mode = HIGHLIGHT_BOX;
  79. }
  80. m_selection_material.Lighting = false;
  81. if (g_settings->getBool("enable_shaders")) {
  82. IShaderSource *shdrsrc = client->getShaderSource();
  83. u16 shader_id = shdrsrc->getShader(
  84. m_mode == HIGHLIGHT_HALO ? "selection_shader" : "default_shader", TILE_MATERIAL_ALPHA);
  85. m_selection_material.MaterialType = shdrsrc->getShaderInfo(shader_id).material;
  86. } else {
  87. m_selection_material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  88. }
  89. if (m_mode == HIGHLIGHT_BOX) {
  90. m_selection_material.Thickness =
  91. rangelim(g_settings->getS16("selectionbox_width"), 1, 5);
  92. } else if (m_mode == HIGHLIGHT_HALO) {
  93. m_selection_material.setTexture(0, tsrc->getTextureForMesh("halo.png"));
  94. m_selection_material.BackfaceCulling = true;
  95. } else {
  96. m_selection_material.MaterialType = video::EMT_SOLID;
  97. }
  98. // Prepare mesh for compass drawing
  99. m_rotation_mesh_buffer.Vertices.set_used(4);
  100. m_rotation_mesh_buffer.Indices.set_used(6);
  101. video::SColor white(255, 255, 255, 255);
  102. v3f normal(0.f, 0.f, 1.f);
  103. m_rotation_mesh_buffer.Vertices[0] = video::S3DVertex(v3f(-1.f, -1.f, 0.f), normal, white, v2f(0.f, 1.f));
  104. m_rotation_mesh_buffer.Vertices[1] = video::S3DVertex(v3f(-1.f, 1.f, 0.f), normal, white, v2f(0.f, 0.f));
  105. m_rotation_mesh_buffer.Vertices[2] = video::S3DVertex(v3f( 1.f, 1.f, 0.f), normal, white, v2f(1.f, 0.f));
  106. m_rotation_mesh_buffer.Vertices[3] = video::S3DVertex(v3f( 1.f, -1.f, 0.f), normal, white, v2f(1.f, 1.f));
  107. m_rotation_mesh_buffer.Indices[0] = 0;
  108. m_rotation_mesh_buffer.Indices[1] = 1;
  109. m_rotation_mesh_buffer.Indices[2] = 2;
  110. m_rotation_mesh_buffer.Indices[3] = 2;
  111. m_rotation_mesh_buffer.Indices[4] = 3;
  112. m_rotation_mesh_buffer.Indices[5] = 0;
  113. m_rotation_mesh_buffer.getMaterial().Lighting = false;
  114. m_rotation_mesh_buffer.getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
  115. m_rotation_mesh_buffer.setHardwareMappingHint(scene::EHM_STATIC);
  116. }
  117. Hud::~Hud()
  118. {
  119. if (m_selection_mesh)
  120. m_selection_mesh->drop();
  121. }
  122. void Hud::drawItem(const ItemStack &item, const core::rect<s32>& rect,
  123. bool selected)
  124. {
  125. if (selected) {
  126. /* draw highlighting around selected item */
  127. if (use_hotbar_selected_image) {
  128. core::rect<s32> imgrect2 = rect;
  129. imgrect2.UpperLeftCorner.X -= (m_padding*2);
  130. imgrect2.UpperLeftCorner.Y -= (m_padding*2);
  131. imgrect2.LowerRightCorner.X += (m_padding*2);
  132. imgrect2.LowerRightCorner.Y += (m_padding*2);
  133. video::ITexture *texture = tsrc->getTexture(hotbar_selected_image);
  134. core::dimension2di imgsize(texture->getOriginalSize());
  135. draw2DImageFilterScaled(driver, texture, imgrect2,
  136. core::rect<s32>(core::position2d<s32>(0,0), imgsize),
  137. NULL, hbar_colors, true);
  138. } else {
  139. video::SColor c_outside(255,255,0,0);
  140. //video::SColor c_outside(255,0,0,0);
  141. //video::SColor c_inside(255,192,192,192);
  142. s32 x1 = rect.UpperLeftCorner.X;
  143. s32 y1 = rect.UpperLeftCorner.Y;
  144. s32 x2 = rect.LowerRightCorner.X;
  145. s32 y2 = rect.LowerRightCorner.Y;
  146. // Black base borders
  147. driver->draw2DRectangle(c_outside,
  148. core::rect<s32>(
  149. v2s32(x1 - m_padding, y1 - m_padding),
  150. v2s32(x2 + m_padding, y1)
  151. ), NULL);
  152. driver->draw2DRectangle(c_outside,
  153. core::rect<s32>(
  154. v2s32(x1 - m_padding, y2),
  155. v2s32(x2 + m_padding, y2 + m_padding)
  156. ), NULL);
  157. driver->draw2DRectangle(c_outside,
  158. core::rect<s32>(
  159. v2s32(x1 - m_padding, y1),
  160. v2s32(x1, y2)
  161. ), NULL);
  162. driver->draw2DRectangle(c_outside,
  163. core::rect<s32>(
  164. v2s32(x2, y1),
  165. v2s32(x2 + m_padding, y2)
  166. ), NULL);
  167. /*// Light inside borders
  168. driver->draw2DRectangle(c_inside,
  169. core::rect<s32>(
  170. v2s32(x1 - padding/2, y1 - padding/2),
  171. v2s32(x2 + padding/2, y1)
  172. ), NULL);
  173. driver->draw2DRectangle(c_inside,
  174. core::rect<s32>(
  175. v2s32(x1 - padding/2, y2),
  176. v2s32(x2 + padding/2, y2 + padding/2)
  177. ), NULL);
  178. driver->draw2DRectangle(c_inside,
  179. core::rect<s32>(
  180. v2s32(x1 - padding/2, y1),
  181. v2s32(x1, y2)
  182. ), NULL);
  183. driver->draw2DRectangle(c_inside,
  184. core::rect<s32>(
  185. v2s32(x2, y1),
  186. v2s32(x2 + padding/2, y2)
  187. ), NULL);
  188. */
  189. }
  190. }
  191. video::SColor bgcolor2(128, 0, 0, 0);
  192. if (!use_hotbar_image)
  193. driver->draw2DRectangle(bgcolor2, rect, NULL);
  194. drawItemStack(driver, g_fontengine->getFont(), item, rect, NULL,
  195. client, selected ? IT_ROT_SELECTED : IT_ROT_NONE);
  196. }
  197. // NOTE: selectitem = 0 -> no selected; selectitem is 1-based
  198. // mainlist can be NULL, but draw the frame anyway.
  199. void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
  200. s32 inv_offset, InventoryList *mainlist, u16 selectitem, u16 direction,
  201. bool is_hotbar)
  202. {
  203. s32 height = m_hotbar_imagesize + m_padding * 2;
  204. s32 width = (itemcount - inv_offset) * (m_hotbar_imagesize + m_padding * 2);
  205. if (direction == HUD_DIR_TOP_BOTTOM || direction == HUD_DIR_BOTTOM_TOP) {
  206. s32 tmp = height;
  207. height = width;
  208. width = tmp;
  209. }
  210. // Position of upper left corner of bar
  211. v2s32 pos = screen_offset * m_scale_factor;
  212. pos += upperleftpos;
  213. // Store hotbar_image in member variable, used by drawItem()
  214. if (hotbar_image != player->hotbar_image) {
  215. hotbar_image = player->hotbar_image;
  216. use_hotbar_image = !hotbar_image.empty();
  217. }
  218. // Store hotbar_selected_image in member variable, used by drawItem()
  219. if (hotbar_selected_image != player->hotbar_selected_image) {
  220. hotbar_selected_image = player->hotbar_selected_image;
  221. use_hotbar_selected_image = !hotbar_selected_image.empty();
  222. }
  223. // draw customized item background
  224. if (use_hotbar_image) {
  225. core::rect<s32> imgrect2(-m_padding/2, -m_padding/2,
  226. width+m_padding/2, height+m_padding/2);
  227. core::rect<s32> rect2 = imgrect2 + pos;
  228. video::ITexture *texture = tsrc->getTexture(hotbar_image);
  229. core::dimension2di imgsize(texture->getOriginalSize());
  230. draw2DImageFilterScaled(driver, texture, rect2,
  231. core::rect<s32>(core::position2d<s32>(0,0), imgsize),
  232. NULL, hbar_colors, true);
  233. }
  234. // Draw items
  235. core::rect<s32> imgrect(0, 0, m_hotbar_imagesize, m_hotbar_imagesize);
  236. const s32 list_size = mainlist ? mainlist->getSize() : 0;
  237. for (s32 i = inv_offset; i < itemcount && i < list_size; i++) {
  238. s32 fullimglen = m_hotbar_imagesize + m_padding * 2;
  239. v2s32 steppos;
  240. switch (direction) {
  241. case HUD_DIR_RIGHT_LEFT:
  242. steppos = v2s32(-(m_padding + (i - inv_offset) * fullimglen), m_padding);
  243. break;
  244. case HUD_DIR_TOP_BOTTOM:
  245. steppos = v2s32(m_padding, m_padding + (i - inv_offset) * fullimglen);
  246. break;
  247. case HUD_DIR_BOTTOM_TOP:
  248. steppos = v2s32(m_padding, -(m_padding + (i - inv_offset) * fullimglen));
  249. break;
  250. default:
  251. steppos = v2s32(m_padding + (i - inv_offset) * fullimglen, m_padding);
  252. break;
  253. }
  254. core::rect<s32> item_rect = imgrect + pos + steppos;
  255. drawItem(mainlist->getItem(i), item_rect, (i + 1) == selectitem);
  256. if (is_hotbar && g_touchscreengui)
  257. g_touchscreengui->registerHotbarRect(i, item_rect);
  258. }
  259. }
  260. bool Hud::hasElementOfType(HudElementType type)
  261. {
  262. for (size_t i = 0; i != player->maxHudId(); i++) {
  263. HudElement *e = player->getHud(i);
  264. if (!e)
  265. continue;
  266. if (e->type == type)
  267. return true;
  268. }
  269. return false;
  270. }
  271. // Calculates screen position of waypoint. Returns true if waypoint is visible (in front of the player), else false.
  272. bool Hud::calculateScreenPos(const v3s16 &camera_offset, HudElement *e, v2s32 *pos)
  273. {
  274. v3f w_pos = e->world_pos * BS;
  275. scene::ICameraSceneNode* camera =
  276. client->getSceneManager()->getActiveCamera();
  277. w_pos -= intToFloat(camera_offset, BS);
  278. core::matrix4 trans = camera->getProjectionMatrix();
  279. trans *= camera->getViewMatrix();
  280. f32 transformed_pos[4] = { w_pos.X, w_pos.Y, w_pos.Z, 1.0f };
  281. trans.multiplyWith1x4Matrix(transformed_pos);
  282. if (transformed_pos[3] < 0)
  283. return false;
  284. f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
  285. core::reciprocal(transformed_pos[3]);
  286. pos->X = m_screensize.X * (0.5 * transformed_pos[0] * zDiv + 0.5);
  287. pos->Y = m_screensize.Y * (0.5 - transformed_pos[1] * zDiv * 0.5);
  288. return true;
  289. }
  290. void Hud::drawLuaElements(const v3s16 &camera_offset)
  291. {
  292. const u32 text_height = g_fontengine->getTextHeight();
  293. gui::IGUIFont *const font = g_fontengine->getFont();
  294. // Reorder elements by z_index
  295. std::vector<HudElement*> elems;
  296. elems.reserve(player->maxHudId());
  297. // Add builtin minimap if the server doesn't send it.
  298. HudElement minimap;
  299. if (client->getProtoVersion() < 44 && (player->hud_flags & HUD_FLAG_MINIMAP_VISIBLE)) {
  300. minimap = {HUD_ELEM_MINIMAP, v2f(1, 0), "", v2f(), "", 0 , 0, 0, v2f(-1, 1),
  301. v2f(-10, 10), v3f(), v2s32(256, 256), 0, "", 0};
  302. elems.push_back(&minimap);
  303. }
  304. for (size_t i = 0; i != player->maxHudId(); i++) {
  305. HudElement *e = player->getHud(i);
  306. if (!e)
  307. continue;
  308. auto it = elems.begin();
  309. while (it != elems.end() && (*it)->z_index <= e->z_index)
  310. ++it;
  311. elems.insert(it, e);
  312. }
  313. for (HudElement *e : elems) {
  314. v2s32 pos(floor(e->pos.X * (float) m_screensize.X + 0.5),
  315. floor(e->pos.Y * (float) m_screensize.Y + 0.5));
  316. switch (e->type) {
  317. case HUD_ELEM_TEXT: {
  318. unsigned int font_size = g_fontengine->getDefaultFontSize();
  319. if (e->size.X > 0)
  320. font_size *= e->size.X;
  321. #ifdef __ANDROID__
  322. // The text size on Android is not proportional with the actual scaling
  323. // FIXME: why do we have such a weird unportable hack??
  324. if (font_size > 3 && e->offset.X < -20)
  325. font_size -= 3;
  326. #endif
  327. auto textfont = g_fontengine->getFont(FontSpec(font_size,
  328. (e->style & HUD_STYLE_MONO) ? FM_Mono : FM_Unspecified,
  329. e->style & HUD_STYLE_BOLD, e->style & HUD_STYLE_ITALIC));
  330. video::SColor color(255, (e->number >> 16) & 0xFF,
  331. (e->number >> 8) & 0xFF,
  332. (e->number >> 0) & 0xFF);
  333. std::wstring text = unescape_translate(utf8_to_wide(e->text));
  334. core::dimension2d<u32> textsize = textfont->getDimension(text.c_str());
  335. v2s32 offset(0, (e->align.Y - 1.0) * (textsize.Height / 2));
  336. core::rect<s32> size(0, 0, e->scale.X * m_scale_factor,
  337. text_height * e->scale.Y * m_scale_factor);
  338. v2s32 offs(e->offset.X * m_scale_factor,
  339. e->offset.Y * m_scale_factor);
  340. std::wstringstream wss(text);
  341. std::wstring line;
  342. while (std::getline(wss, line, L'\n'))
  343. {
  344. core::dimension2d<u32> linesize = textfont->getDimension(line.c_str());
  345. v2s32 line_offset((e->align.X - 1.0) * (linesize.Width / 2), 0);
  346. textfont->draw(line.c_str(), size + pos + offset + offs + line_offset, color);
  347. offset.Y += linesize.Height;
  348. }
  349. break; }
  350. case HUD_ELEM_STATBAR: {
  351. v2s32 offs(e->offset.X, e->offset.Y);
  352. drawStatbar(pos, HUD_CORNER_UPPER, e->dir, e->text, e->text2,
  353. e->number, e->item, offs, e->size);
  354. break; }
  355. case HUD_ELEM_INVENTORY: {
  356. InventoryList *inv = inventory->getList(e->text);
  357. if (!inv)
  358. warningstream << "HUD: Unknown inventory list. name=" << e->text << std::endl;
  359. drawItems(pos, v2s32(e->offset.X, e->offset.Y), e->number, 0,
  360. inv, e->item, e->dir, false);
  361. break; }
  362. case HUD_ELEM_WAYPOINT: {
  363. if (!calculateScreenPos(camera_offset, e, &pos))
  364. break;
  365. pos += v2s32(e->offset.X, e->offset.Y);
  366. video::SColor color(255, (e->number >> 16) & 0xFF,
  367. (e->number >> 8) & 0xFF,
  368. (e->number >> 0) & 0xFF);
  369. std::wstring text = unescape_translate(utf8_to_wide(e->name));
  370. const std::string &unit = e->text;
  371. // Waypoints reuse the item field to store precision,
  372. // item = precision + 1 and item = 0 <=> precision = 10 for backwards compatibility.
  373. // Also see `push_hud_element`.
  374. u32 item = e->item;
  375. float precision = (item == 0) ? 10.0f : (item - 1.f);
  376. bool draw_precision = precision > 0;
  377. core::rect<s32> bounds(0, 0, font->getDimension(text.c_str()).Width, (draw_precision ? 2:1) * text_height);
  378. pos.Y += (e->align.Y - 1.0) * bounds.getHeight() / 2;
  379. bounds += pos;
  380. font->draw(text.c_str(), bounds + v2s32((e->align.X - 1.0) * bounds.getWidth() / 2, 0), color);
  381. if (draw_precision) {
  382. std::ostringstream os;
  383. v3f p_pos = player->getPosition() / BS;
  384. float distance = std::floor(precision * p_pos.getDistanceFrom(e->world_pos)) / precision;
  385. os << distance << unit;
  386. text = unescape_translate(utf8_to_wide(os.str()));
  387. bounds.LowerRightCorner.X = bounds.UpperLeftCorner.X + font->getDimension(text.c_str()).Width;
  388. font->draw(text.c_str(), bounds + v2s32((e->align.X - 1.0f) * bounds.getWidth() / 2, text_height), color);
  389. }
  390. break; }
  391. case HUD_ELEM_IMAGE_WAYPOINT: {
  392. if (!calculateScreenPos(camera_offset, e, &pos))
  393. break;
  394. [[fallthrough]];
  395. }
  396. case HUD_ELEM_IMAGE: {
  397. video::ITexture *texture = tsrc->getTexture(e->text);
  398. if (!texture)
  399. continue;
  400. const video::SColor color(255, 255, 255, 255);
  401. const video::SColor colors[] = {color, color, color, color};
  402. core::dimension2di imgsize(texture->getOriginalSize());
  403. v2s32 dstsize(imgsize.Width * e->scale.X * m_scale_factor,
  404. imgsize.Height * e->scale.Y * m_scale_factor);
  405. if (e->scale.X < 0)
  406. dstsize.X = m_screensize.X * (e->scale.X * -0.01);
  407. if (e->scale.Y < 0)
  408. dstsize.Y = m_screensize.Y * (e->scale.Y * -0.01);
  409. v2s32 offset((e->align.X - 1.0) * dstsize.X / 2,
  410. (e->align.Y - 1.0) * dstsize.Y / 2);
  411. core::rect<s32> rect(0, 0, dstsize.X, dstsize.Y);
  412. rect += pos + offset + v2s32(e->offset.X * m_scale_factor,
  413. e->offset.Y * m_scale_factor);
  414. draw2DImageFilterScaled(driver, texture, rect,
  415. core::rect<s32>(core::position2d<s32>(0,0), imgsize),
  416. NULL, colors, true);
  417. break; }
  418. case HUD_ELEM_COMPASS: {
  419. video::ITexture *texture = tsrc->getTexture(e->text);
  420. if (!texture)
  421. continue;
  422. // Positionning :
  423. v2s32 dstsize(e->size.X, e->size.Y);
  424. if (e->size.X < 0)
  425. dstsize.X = m_screensize.X * (e->size.X * -0.01);
  426. if (e->size.Y < 0)
  427. dstsize.Y = m_screensize.Y * (e->size.Y * -0.01);
  428. if (dstsize.X <= 0 || dstsize.Y <= 0)
  429. return; // Avoid zero divides
  430. // Angle according to camera view
  431. v3f fore(0.f, 0.f, 1.f);
  432. scene::ICameraSceneNode *cam = client->getSceneManager()->getActiveCamera();
  433. cam->getAbsoluteTransformation().rotateVect(fore);
  434. int angle = - fore.getHorizontalAngle().Y;
  435. // Limit angle and ajust with given offset
  436. angle = (angle + (int)e->number) % 360;
  437. core::rect<s32> dstrect(0, 0, dstsize.X, dstsize.Y);
  438. dstrect += pos + v2s32(
  439. (e->align.X - 1.0) * dstsize.X / 2,
  440. (e->align.Y - 1.0) * dstsize.Y / 2) +
  441. v2s32(e->offset.X * m_hud_scaling, e->offset.Y * m_hud_scaling);
  442. switch (e->dir) {
  443. case HUD_COMPASS_ROTATE:
  444. drawCompassRotate(e, texture, dstrect, angle);
  445. break;
  446. case HUD_COMPASS_ROTATE_REVERSE:
  447. drawCompassRotate(e, texture, dstrect, -angle);
  448. break;
  449. case HUD_COMPASS_TRANSLATE:
  450. drawCompassTranslate(e, texture, dstrect, angle);
  451. break;
  452. case HUD_COMPASS_TRANSLATE_REVERSE:
  453. drawCompassTranslate(e, texture, dstrect, -angle);
  454. break;
  455. default:
  456. break;
  457. }
  458. break; }
  459. case HUD_ELEM_MINIMAP: {
  460. if (e->size.X <= 0 || e->size.Y <= 0)
  461. break;
  462. if (!client->getMinimap())
  463. break;
  464. // Draw a minimap of size "size"
  465. v2s32 dstsize(e->size.X * m_scale_factor,
  466. e->size.Y * m_scale_factor);
  467. // (no percent size as minimap would likely be anamorphosed)
  468. v2s32 offset((e->align.X - 1.0) * dstsize.X / 2,
  469. (e->align.Y - 1.0) * dstsize.Y / 2);
  470. core::rect<s32> rect(0, 0, dstsize.X, dstsize.Y);
  471. rect += pos + offset + v2s32(e->offset.X * m_scale_factor,
  472. e->offset.Y * m_scale_factor);
  473. client->getMinimap()->drawMinimap(rect);
  474. break; }
  475. default:
  476. infostream << "Hud::drawLuaElements: ignoring drawform " << e->type
  477. << " due to unrecognized type" << std::endl;
  478. }
  479. }
  480. }
  481. void Hud::drawCompassTranslate(HudElement *e, video::ITexture *texture,
  482. const core::rect<s32> &rect, int angle)
  483. {
  484. const video::SColor color(255, 255, 255, 255);
  485. const video::SColor colors[] = {color, color, color, color};
  486. // Compute source image scaling
  487. core::dimension2di imgsize(texture->getOriginalSize());
  488. core::rect<s32> srcrect(0, 0, imgsize.Width, imgsize.Height);
  489. v2s32 dstsize(rect.getHeight() * e->scale.X * imgsize.Width / imgsize.Height,
  490. rect.getHeight() * e->scale.Y);
  491. // Avoid infinite loop
  492. if (dstsize.X <= 0 || dstsize.Y <= 0)
  493. return;
  494. core::rect<s32> tgtrect(0, 0, dstsize.X, dstsize.Y);
  495. tgtrect += v2s32(
  496. (rect.getWidth() - dstsize.X) / 2,
  497. (rect.getHeight() - dstsize.Y) / 2) +
  498. rect.UpperLeftCorner;
  499. int offset = angle * dstsize.X / 360;
  500. tgtrect += v2s32(offset, 0);
  501. // Repeat image as much as needed
  502. while (tgtrect.UpperLeftCorner.X > rect.UpperLeftCorner.X)
  503. tgtrect -= v2s32(dstsize.X, 0);
  504. draw2DImageFilterScaled(driver, texture, tgtrect, srcrect, &rect, colors, true);
  505. tgtrect += v2s32(dstsize.X, 0);
  506. while (tgtrect.UpperLeftCorner.X < rect.LowerRightCorner.X) {
  507. draw2DImageFilterScaled(driver, texture, tgtrect, srcrect, &rect, colors, true);
  508. tgtrect += v2s32(dstsize.X, 0);
  509. }
  510. }
  511. void Hud::drawCompassRotate(HudElement *e, video::ITexture *texture,
  512. const core::rect<s32> &rect, int angle)
  513. {
  514. core::rect<s32> oldViewPort = driver->getViewPort();
  515. core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
  516. core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
  517. core::matrix4 Matrix;
  518. Matrix.makeIdentity();
  519. Matrix.setRotationDegrees(v3f(0.f, 0.f, angle));
  520. driver->setViewPort(rect);
  521. driver->setTransform(video::ETS_PROJECTION, core::matrix4());
  522. driver->setTransform(video::ETS_VIEW, core::matrix4());
  523. driver->setTransform(video::ETS_WORLD, Matrix);
  524. video::SMaterial &material = m_rotation_mesh_buffer.getMaterial();
  525. material.TextureLayers[0].Texture = texture;
  526. driver->setMaterial(material);
  527. driver->drawMeshBuffer(&m_rotation_mesh_buffer);
  528. driver->setTransform(video::ETS_WORLD, core::matrix4());
  529. driver->setTransform(video::ETS_VIEW, oldViewMat);
  530. driver->setTransform(video::ETS_PROJECTION, oldProjMat);
  531. // restore the view area
  532. driver->setViewPort(oldViewPort);
  533. }
  534. void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
  535. const std::string &texture, const std::string &bgtexture,
  536. s32 count, s32 maxcount, v2s32 offset, v2s32 size)
  537. {
  538. const video::SColor color(255, 255, 255, 255);
  539. const video::SColor colors[] = {color, color, color, color};
  540. video::ITexture *stat_texture = tsrc->getTexture(texture);
  541. if (!stat_texture)
  542. return;
  543. video::ITexture *stat_texture_bg = nullptr;
  544. if (!bgtexture.empty()) {
  545. stat_texture_bg = tsrc->getTexture(bgtexture);
  546. }
  547. core::dimension2di srcd(stat_texture->getOriginalSize());
  548. core::dimension2di dstd;
  549. if (size == v2s32()) {
  550. dstd = srcd;
  551. dstd.Height *= m_scale_factor;
  552. dstd.Width *= m_scale_factor;
  553. offset.X *= m_scale_factor;
  554. offset.Y *= m_scale_factor;
  555. } else {
  556. dstd.Height = size.Y * m_scale_factor;
  557. dstd.Width = size.X * m_scale_factor;
  558. offset.X *= m_scale_factor;
  559. offset.Y *= m_scale_factor;
  560. }
  561. v2s32 p = pos;
  562. if (corner & HUD_CORNER_LOWER)
  563. p -= dstd.Height;
  564. p += offset;
  565. v2s32 steppos;
  566. switch (drawdir) {
  567. case HUD_DIR_RIGHT_LEFT:
  568. steppos = v2s32(-1, 0);
  569. break;
  570. case HUD_DIR_TOP_BOTTOM:
  571. steppos = v2s32(0, 1);
  572. break;
  573. case HUD_DIR_BOTTOM_TOP:
  574. steppos = v2s32(0, -1);
  575. break;
  576. default:
  577. // From left to right
  578. steppos = v2s32(1, 0);
  579. break;
  580. }
  581. auto calculate_clipping_rect = [] (core::dimension2di src,
  582. v2s32 steppos) -> core::rect<s32> {
  583. // Create basic rectangle
  584. core::rect<s32> rect(0, 0,
  585. src.Width - std::abs(steppos.X) * src.Width / 2,
  586. src.Height - std::abs(steppos.Y) * src.Height / 2
  587. );
  588. // Move rectangle left or down
  589. if (steppos.X == -1)
  590. rect += v2s32(src.Width / 2, 0);
  591. if (steppos.Y == -1)
  592. rect += v2s32(0, src.Height / 2);
  593. return rect;
  594. };
  595. // Rectangles for 1/2 the actual value to display
  596. core::rect<s32> srchalfrect, dsthalfrect;
  597. // Rectangles for 1/2 the "off state" texture
  598. core::rect<s32> srchalfrect2, dsthalfrect2;
  599. if (count % 2 == 1 || maxcount % 2 == 1) {
  600. // Need to draw halves: Calculate rectangles
  601. srchalfrect = calculate_clipping_rect(srcd, steppos);
  602. dsthalfrect = calculate_clipping_rect(dstd, steppos);
  603. srchalfrect2 = calculate_clipping_rect(srcd, steppos * -1);
  604. dsthalfrect2 = calculate_clipping_rect(dstd, steppos * -1);
  605. }
  606. steppos.X *= dstd.Width;
  607. steppos.Y *= dstd.Height;
  608. // Draw full textures
  609. for (s32 i = 0; i < count / 2; i++) {
  610. core::rect<s32> srcrect(0, 0, srcd.Width, srcd.Height);
  611. core::rect<s32> dstrect(0, 0, dstd.Width, dstd.Height);
  612. dstrect += p;
  613. draw2DImageFilterScaled(driver, stat_texture,
  614. dstrect, srcrect, NULL, colors, true);
  615. p += steppos;
  616. }
  617. if (count % 2 == 1) {
  618. // Draw half a texture
  619. draw2DImageFilterScaled(driver, stat_texture,
  620. dsthalfrect + p, srchalfrect, NULL, colors, true);
  621. if (stat_texture_bg && maxcount > count) {
  622. draw2DImageFilterScaled(driver, stat_texture_bg,
  623. dsthalfrect2 + p, srchalfrect2,
  624. NULL, colors, true);
  625. p += steppos;
  626. }
  627. }
  628. if (stat_texture_bg && maxcount > count) {
  629. // Draw "off state" textures
  630. s32 start_offset;
  631. if (count % 2 == 1)
  632. start_offset = count / 2 + 1;
  633. else
  634. start_offset = count / 2;
  635. for (s32 i = start_offset; i < maxcount / 2; i++) {
  636. core::rect<s32> srcrect(0, 0, srcd.Width, srcd.Height);
  637. core::rect<s32> dstrect(0, 0, dstd.Width, dstd.Height);
  638. dstrect += p;
  639. draw2DImageFilterScaled(driver, stat_texture_bg,
  640. dstrect, srcrect,
  641. NULL, colors, true);
  642. p += steppos;
  643. }
  644. if (maxcount % 2 == 1) {
  645. draw2DImageFilterScaled(driver, stat_texture_bg,
  646. dsthalfrect + p, srchalfrect, NULL, colors, true);
  647. }
  648. }
  649. }
  650. void Hud::drawHotbar(u16 playeritem)
  651. {
  652. if (g_touchscreengui)
  653. g_touchscreengui->resetHotbarRects();
  654. InventoryList *mainlist = inventory->getList("main");
  655. if (mainlist == NULL) {
  656. // Silently ignore this. We may not be initialized completely.
  657. return;
  658. }
  659. v2s32 centerlowerpos(m_displaycenter.X, m_screensize.Y);
  660. s32 hotbar_itemcount = player->hud_hotbar_itemcount;
  661. s32 width = hotbar_itemcount * (m_hotbar_imagesize + m_padding * 2);
  662. v2s32 pos = centerlowerpos - v2s32(width / 2, m_hotbar_imagesize + m_padding * 3);
  663. const v2u32 &window_size = RenderingEngine::getWindowSize();
  664. if ((float) width / (float) window_size.X <=
  665. g_settings->getFloat("hud_hotbar_max_width")) {
  666. if (player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE) {
  667. drawItems(pos, v2s32(0, 0), hotbar_itemcount, 0, mainlist, playeritem + 1, 0, true);
  668. }
  669. } else {
  670. pos.X += width/4;
  671. v2s32 secondpos = pos;
  672. pos = pos - v2s32(0, m_hotbar_imagesize + m_padding);
  673. if (player->hud_flags & HUD_FLAG_HOTBAR_VISIBLE) {
  674. drawItems(pos, v2s32(0, 0), hotbar_itemcount / 2, 0,
  675. mainlist, playeritem + 1, 0, true);
  676. drawItems(secondpos, v2s32(0, 0), hotbar_itemcount,
  677. hotbar_itemcount / 2, mainlist, playeritem + 1, 0, true);
  678. }
  679. }
  680. }
  681. void Hud::drawCrosshair()
  682. {
  683. auto draw_image_crosshair = [this] (video::ITexture *tex) {
  684. core::dimension2di orig_size(tex->getOriginalSize());
  685. core::dimension2di scaled_size(
  686. core::round32(orig_size.Width * m_scale_factor),
  687. core::round32(orig_size.Height * m_scale_factor));
  688. core::rect<s32> src_rect(orig_size);
  689. core::position2d pos(m_displaycenter.X - scaled_size.Width / 2,
  690. m_displaycenter.Y - scaled_size.Height / 2);
  691. core::rect<s32> dest_rect(pos, scaled_size);
  692. video::SColor colors[] = { crosshair_argb, crosshair_argb,
  693. crosshair_argb, crosshair_argb };
  694. draw2DImageFilterScaled(driver, tex, dest_rect, src_rect,
  695. nullptr, colors, true);
  696. };
  697. if (pointing_at_object) {
  698. if (use_object_crosshair_image) {
  699. draw_image_crosshair(tsrc->getTexture("object_crosshair.png"));
  700. } else {
  701. s32 line_size = core::round32(OBJECT_CROSSHAIR_LINE_SIZE * m_scale_factor);
  702. driver->draw2DLine(
  703. m_displaycenter - v2s32(line_size, line_size),
  704. m_displaycenter + v2s32(line_size, line_size),
  705. crosshair_argb);
  706. driver->draw2DLine(
  707. m_displaycenter + v2s32(line_size, -line_size),
  708. m_displaycenter + v2s32(-line_size, line_size),
  709. crosshair_argb);
  710. }
  711. return;
  712. }
  713. if (use_crosshair_image) {
  714. draw_image_crosshair(tsrc->getTexture("crosshair.png"));
  715. } else {
  716. s32 line_size = core::round32(CROSSHAIR_LINE_SIZE * m_scale_factor);
  717. driver->draw2DLine(m_displaycenter - v2s32(line_size, 0),
  718. m_displaycenter + v2s32(line_size, 0), crosshair_argb);
  719. driver->draw2DLine(m_displaycenter - v2s32(0, line_size),
  720. m_displaycenter + v2s32(0, line_size), crosshair_argb);
  721. }
  722. }
  723. void Hud::setSelectionPos(const v3f &pos, const v3s16 &camera_offset)
  724. {
  725. m_camera_offset = camera_offset;
  726. m_selection_pos = pos;
  727. m_selection_pos_with_offset = pos - intToFloat(camera_offset, BS);
  728. }
  729. void Hud::drawSelectionMesh()
  730. {
  731. if (m_mode == HIGHLIGHT_NONE || (m_mode == HIGHLIGHT_HALO && !m_selection_mesh))
  732. return;
  733. const video::SMaterial oldmaterial = driver->getMaterial2D();
  734. driver->setMaterial(m_selection_material);
  735. const core::matrix4 oldtransform = driver->getTransform(video::ETS_WORLD);
  736. core::matrix4 translate;
  737. translate.setTranslation(m_selection_pos_with_offset);
  738. core::matrix4 rotation;
  739. rotation.setRotationDegrees(m_selection_rotation);
  740. driver->setTransform(video::ETS_WORLD, translate * rotation);
  741. if (m_mode == HIGHLIGHT_BOX) {
  742. // Draw 3D selection boxes
  743. for (auto & selection_box : m_selection_boxes) {
  744. u32 r = (selectionbox_argb.getRed() *
  745. m_selection_mesh_color.getRed() / 255);
  746. u32 g = (selectionbox_argb.getGreen() *
  747. m_selection_mesh_color.getGreen() / 255);
  748. u32 b = (selectionbox_argb.getBlue() *
  749. m_selection_mesh_color.getBlue() / 255);
  750. driver->draw3DBox(selection_box, video::SColor(255, r, g, b));
  751. }
  752. } else if (m_mode == HIGHLIGHT_HALO && m_selection_mesh) {
  753. // Draw selection mesh
  754. setMeshColor(m_selection_mesh, m_selection_mesh_color);
  755. video::SColor face_color(0,
  756. MYMIN(255, m_selection_mesh_color.getRed() * 1.5),
  757. MYMIN(255, m_selection_mesh_color.getGreen() * 1.5),
  758. MYMIN(255, m_selection_mesh_color.getBlue() * 1.5));
  759. setMeshColorByNormal(m_selection_mesh, m_selected_face_normal,
  760. face_color);
  761. u32 mc = m_selection_mesh->getMeshBufferCount();
  762. for (u32 i = 0; i < mc; i++) {
  763. scene::IMeshBuffer *buf = m_selection_mesh->getMeshBuffer(i);
  764. driver->drawMeshBuffer(buf);
  765. }
  766. }
  767. driver->setMaterial(oldmaterial);
  768. driver->setTransform(video::ETS_WORLD, oldtransform);
  769. }
  770. enum Hud::BlockBoundsMode Hud::toggleBlockBounds()
  771. {
  772. m_block_bounds_mode = static_cast<BlockBoundsMode>(m_block_bounds_mode + 1);
  773. if (m_block_bounds_mode >= BLOCK_BOUNDS_MAX) {
  774. m_block_bounds_mode = BLOCK_BOUNDS_OFF;
  775. }
  776. return m_block_bounds_mode;
  777. }
  778. void Hud::disableBlockBounds()
  779. {
  780. m_block_bounds_mode = BLOCK_BOUNDS_OFF;
  781. }
  782. void Hud::drawBlockBounds()
  783. {
  784. if (m_block_bounds_mode == BLOCK_BOUNDS_OFF) {
  785. return;
  786. }
  787. video::SMaterial old_material = driver->getMaterial2D();
  788. driver->setMaterial(m_selection_material);
  789. v3s16 pos = player->getStandingNodePos();
  790. v3s16 blockPos(
  791. floorf((float) pos.X / MAP_BLOCKSIZE),
  792. floorf((float) pos.Y / MAP_BLOCKSIZE),
  793. floorf((float) pos.Z / MAP_BLOCKSIZE)
  794. );
  795. v3f offset = intToFloat(client->getCamera()->getOffset(), BS);
  796. s8 radius = m_block_bounds_mode == BLOCK_BOUNDS_NEAR ? 2 : 0;
  797. v3f halfNode = v3f(BS, BS, BS) / 2.0f;
  798. for (s8 x = -radius; x <= radius; x++)
  799. for (s8 y = -radius; y <= radius; y++)
  800. for (s8 z = -radius; z <= radius; z++) {
  801. v3s16 blockOffset(x, y, z);
  802. aabb3f box(
  803. intToFloat((blockPos + blockOffset) * MAP_BLOCKSIZE, BS) - offset - halfNode,
  804. intToFloat(((blockPos + blockOffset) * MAP_BLOCKSIZE) + (MAP_BLOCKSIZE - 1), BS) - offset + halfNode
  805. );
  806. driver->draw3DBox(box, video::SColor(255, 255, 0, 0));
  807. }
  808. driver->setMaterial(old_material);
  809. }
  810. void Hud::updateSelectionMesh(const v3s16 &camera_offset)
  811. {
  812. m_camera_offset = camera_offset;
  813. if (m_mode != HIGHLIGHT_HALO)
  814. return;
  815. if (m_selection_mesh) {
  816. m_selection_mesh->drop();
  817. m_selection_mesh = NULL;
  818. }
  819. if (m_selection_boxes.empty()) {
  820. // No pointed object
  821. return;
  822. }
  823. // New pointed object, create new mesh.
  824. // Texture UV coordinates for selection boxes
  825. static f32 texture_uv[24] = {
  826. 0,0,1,1,
  827. 0,0,1,1,
  828. 0,0,1,1,
  829. 0,0,1,1,
  830. 0,0,1,1,
  831. 0,0,1,1
  832. };
  833. // Use single halo box instead of multiple overlapping boxes.
  834. // Temporary solution - problem can be solved with multiple
  835. // rendering targets, or some method to remove inner surfaces.
  836. // Thats because of halo transparency.
  837. aabb3f halo_box(100.0, 100.0, 100.0, -100.0, -100.0, -100.0);
  838. m_halo_boxes.clear();
  839. for (const auto &selection_box : m_selection_boxes) {
  840. halo_box.addInternalBox(selection_box);
  841. }
  842. m_halo_boxes.push_back(halo_box);
  843. m_selection_mesh = convertNodeboxesToMesh(
  844. m_halo_boxes, texture_uv, 0.5);
  845. }
  846. void Hud::resizeHotbar() {
  847. const v2u32 &window_size = RenderingEngine::getWindowSize();
  848. if (m_screensize != window_size) {
  849. m_hotbar_imagesize = floor(HOTBAR_IMAGE_SIZE *
  850. RenderingEngine::getDisplayDensity() + 0.5);
  851. m_hotbar_imagesize *= m_hud_scaling;
  852. m_padding = m_hotbar_imagesize / 12;
  853. m_screensize = window_size;
  854. m_displaycenter = v2s32(m_screensize.X/2,m_screensize.Y/2);
  855. }
  856. }
  857. struct MeshTimeInfo {
  858. u64 time;
  859. scene::IMesh *mesh = nullptr;
  860. };
  861. void drawItemStack(
  862. video::IVideoDriver *driver,
  863. gui::IGUIFont *font,
  864. const ItemStack &item,
  865. const core::rect<s32> &rect,
  866. const core::rect<s32> *clip,
  867. Client *client,
  868. ItemRotationKind rotation_kind,
  869. const v3s16 &angle,
  870. const v3s16 &rotation_speed)
  871. {
  872. static MeshTimeInfo rotation_time_infos[IT_ROT_NONE];
  873. if (item.empty()) {
  874. if (rotation_kind < IT_ROT_NONE && rotation_kind != IT_ROT_OTHER) {
  875. rotation_time_infos[rotation_kind].mesh = NULL;
  876. }
  877. return;
  878. }
  879. const static thread_local bool enable_animations =
  880. g_settings->getBool("inventory_items_animations");
  881. auto *idef = client->idef();
  882. const ItemDefinition &def = item.getDefinition(idef);
  883. bool draw_overlay = false;
  884. const std::string inventory_image = item.getInventoryImage(idef);
  885. const std::string inventory_overlay = item.getInventoryOverlay(idef);
  886. bool has_mesh = false;
  887. ItemMesh *imesh;
  888. core::rect<s32> viewrect = rect;
  889. if (clip != nullptr)
  890. viewrect.clipAgainst(*clip);
  891. // Render as mesh if animated or no inventory image
  892. if ((enable_animations && rotation_kind < IT_ROT_NONE) || inventory_image.empty()) {
  893. imesh = idef->getWieldMesh(item, client);
  894. has_mesh = imesh && imesh->mesh;
  895. }
  896. if (has_mesh) {
  897. scene::IMesh *mesh = imesh->mesh;
  898. driver->clearBuffers(video::ECBF_DEPTH);
  899. s32 delta = 0;
  900. if (rotation_kind < IT_ROT_NONE) {
  901. MeshTimeInfo &ti = rotation_time_infos[rotation_kind];
  902. if (mesh != ti.mesh && rotation_kind != IT_ROT_OTHER) {
  903. ti.mesh = mesh;
  904. ti.time = porting::getTimeMs();
  905. } else {
  906. delta = porting::getDeltaMs(ti.time, porting::getTimeMs()) % 100000;
  907. }
  908. }
  909. core::rect<s32> oldViewPort = driver->getViewPort();
  910. core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
  911. core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
  912. core::matrix4 ProjMatrix;
  913. ProjMatrix.buildProjectionMatrixOrthoLH(2.0f, 2.0f, -1.0f, 100.0f);
  914. core::matrix4 ViewMatrix;
  915. ViewMatrix.buildProjectionMatrixOrthoLH(
  916. 2.0f * viewrect.getWidth() / rect.getWidth(),
  917. 2.0f * viewrect.getHeight() / rect.getHeight(),
  918. -1.0f,
  919. 100.0f);
  920. ViewMatrix.setTranslation(core::vector3df(
  921. 1.0f * (rect.LowerRightCorner.X + rect.UpperLeftCorner.X -
  922. viewrect.LowerRightCorner.X - viewrect.UpperLeftCorner.X) /
  923. viewrect.getWidth(),
  924. 1.0f * (viewrect.LowerRightCorner.Y + viewrect.UpperLeftCorner.Y -
  925. rect.LowerRightCorner.Y - rect.UpperLeftCorner.Y) /
  926. viewrect.getHeight(),
  927. 0.0f));
  928. driver->setTransform(video::ETS_PROJECTION, ProjMatrix);
  929. driver->setTransform(video::ETS_VIEW, ViewMatrix);
  930. core::matrix4 matrix;
  931. matrix.makeIdentity();
  932. if (enable_animations) {
  933. float timer_f = (float) delta / 5000.f;
  934. matrix.setRotationDegrees(v3f(
  935. angle.X + rotation_speed.X * 3.60f * timer_f,
  936. angle.Y + rotation_speed.Y * 3.60f * timer_f,
  937. angle.Z + rotation_speed.Z * 3.60f * timer_f)
  938. );
  939. }
  940. driver->setTransform(video::ETS_WORLD, matrix);
  941. driver->setViewPort(viewrect);
  942. video::SColor basecolor =
  943. client->idef()->getItemstackColor(item, client);
  944. u32 mc = mesh->getMeshBufferCount();
  945. for (u32 j = 0; j < mc; ++j) {
  946. scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
  947. // we can modify vertices relatively fast,
  948. // because these meshes are not buffered.
  949. assert(buf->getHardwareMappingHint_Vertex() == scene::EHM_NEVER);
  950. video::SColor c = basecolor;
  951. if (imesh->buffer_colors.size() > j) {
  952. ItemPartColor *p = &imesh->buffer_colors[j];
  953. if (p->override_base)
  954. c = p->color;
  955. }
  956. if (imesh->needs_shading)
  957. colorizeMeshBuffer(buf, &c);
  958. else
  959. setMeshBufferColor(buf, c);
  960. video::SMaterial &material = buf->getMaterial();
  961. material.MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
  962. material.Lighting = false;
  963. driver->setMaterial(material);
  964. driver->drawMeshBuffer(buf);
  965. }
  966. driver->setTransform(video::ETS_VIEW, oldViewMat);
  967. driver->setTransform(video::ETS_PROJECTION, oldProjMat);
  968. driver->setViewPort(oldViewPort);
  969. draw_overlay = def.type == ITEM_NODE && inventory_image.empty();
  970. } else { // Otherwise just draw as 2D
  971. video::ITexture *texture = client->idef()->getInventoryTexture(item, client);
  972. video::SColor color;
  973. if (texture) {
  974. color = client->idef()->getItemstackColor(item, client);
  975. } else {
  976. color = video::SColor(255, 255, 255, 255);
  977. ITextureSource *tsrc = client->getTextureSource();
  978. texture = tsrc->getTexture("no_texture.png");
  979. if (!texture)
  980. return;
  981. }
  982. const video::SColor colors[] = { color, color, color, color };
  983. draw2DImageFilterScaled(driver, texture, rect,
  984. core::rect<s32>({0, 0}, core::dimension2di(texture->getOriginalSize())),
  985. clip, colors, true);
  986. draw_overlay = true;
  987. }
  988. // draw the inventory_overlay
  989. if (!inventory_overlay.empty() && draw_overlay) {
  990. ITextureSource *tsrc = client->getTextureSource();
  991. video::ITexture *overlay_texture = tsrc->getTexture(inventory_overlay);
  992. core::dimension2d<u32> dimens = overlay_texture->getOriginalSize();
  993. core::rect<s32> srcrect(0, 0, dimens.Width, dimens.Height);
  994. draw2DImageFilterScaled(driver, overlay_texture, rect, srcrect, clip, 0, true);
  995. }
  996. if (def.type == ITEM_TOOL && item.wear != 0) {
  997. // Draw a progressbar
  998. float barheight = static_cast<float>(rect.getHeight()) / 16;
  999. float barpad_x = static_cast<float>(rect.getWidth()) / 16;
  1000. float barpad_y = static_cast<float>(rect.getHeight()) / 16;
  1001. core::rect<s32> progressrect(
  1002. rect.UpperLeftCorner.X + barpad_x,
  1003. rect.LowerRightCorner.Y - barpad_y - barheight,
  1004. rect.LowerRightCorner.X - barpad_x,
  1005. rect.LowerRightCorner.Y - barpad_y);
  1006. // Shrink progressrect by amount of tool damage
  1007. float wear = item.wear / 65535.0f;
  1008. int progressmid =
  1009. wear * progressrect.UpperLeftCorner.X +
  1010. (1 - wear) * progressrect.LowerRightCorner.X;
  1011. // Compute progressbar color
  1012. // default scheme:
  1013. // wear = 0.0: green
  1014. // wear = 0.5: yellow
  1015. // wear = 1.0: red
  1016. video::SColor color;
  1017. auto barParams = item.getWearBarParams(client->idef());
  1018. if (barParams.has_value()) {
  1019. f32 durabilityPercent = 1.0 - wear;
  1020. color = barParams->getWearBarColor(durabilityPercent);
  1021. } else {
  1022. color = video::SColor(255, 255, 255, 255);
  1023. int wear_i = MYMIN(std::floor(wear * 600), 511);
  1024. wear_i = MYMIN(wear_i + 10, 511);
  1025. if (wear_i <= 255)
  1026. color.set(255, wear_i, 255, 0);
  1027. else
  1028. color.set(255, 255, 511 - wear_i, 0);
  1029. }
  1030. core::rect<s32> progressrect2 = progressrect;
  1031. progressrect2.LowerRightCorner.X = progressmid;
  1032. driver->draw2DRectangle(color, progressrect2, clip);
  1033. color = video::SColor(255, 0, 0, 0);
  1034. progressrect2 = progressrect;
  1035. progressrect2.UpperLeftCorner.X = progressmid;
  1036. driver->draw2DRectangle(color, progressrect2, clip);
  1037. }
  1038. const std::string &count_text = item.metadata.getString("count_meta");
  1039. if (font != nullptr && (item.count >= 2 || !count_text.empty())) {
  1040. // Get the item count as a string
  1041. std::string text = count_text.empty() ? itos(item.count) : count_text;
  1042. v2u32 dim = font->getDimension(utf8_to_wide(unescape_enriched(text)).c_str());
  1043. v2s32 sdim(dim.X, dim.Y);
  1044. core::rect<s32> rect2(
  1045. rect.LowerRightCorner - sdim,
  1046. rect.LowerRightCorner
  1047. );
  1048. // get the count alignment
  1049. s32 count_alignment = stoi(item.metadata.getString("count_alignment"));
  1050. if (count_alignment != 0) {
  1051. s32 a_x = count_alignment & 3;
  1052. s32 a_y = (count_alignment >> 2) & 3;
  1053. s32 x1, x2, y1, y2;
  1054. switch (a_x) {
  1055. case 1: // left
  1056. x1 = rect.UpperLeftCorner.X;
  1057. x2 = x1 + sdim.X;
  1058. break;
  1059. case 2: // middle
  1060. x1 = (rect.UpperLeftCorner.X + rect.LowerRightCorner.X - sdim.X) / 2;
  1061. x2 = x1 + sdim.X;
  1062. break;
  1063. case 3: // right
  1064. x2 = rect.LowerRightCorner.X;
  1065. x1 = x2 - sdim.X;
  1066. break;
  1067. default: // 0 = default
  1068. x1 = rect2.UpperLeftCorner.X;
  1069. x2 = rect2.LowerRightCorner.X;
  1070. break;
  1071. }
  1072. switch (a_y) {
  1073. case 1: // up
  1074. y1 = rect.UpperLeftCorner.Y;
  1075. y2 = y1 + sdim.Y;
  1076. break;
  1077. case 2: // middle
  1078. y1 = (rect.UpperLeftCorner.Y + rect.LowerRightCorner.Y - sdim.Y) / 2;
  1079. y2 = y1 + sdim.Y;
  1080. break;
  1081. case 3: // down
  1082. y2 = rect.LowerRightCorner.Y;
  1083. y1 = y2 - sdim.Y;
  1084. break;
  1085. default: // 0 = default
  1086. y1 = rect2.UpperLeftCorner.Y;
  1087. y2 = rect2.LowerRightCorner.Y;
  1088. break;
  1089. }
  1090. rect2 = core::rect<s32>(x1, y1, x2, y2);
  1091. }
  1092. video::SColor color(255, 255, 255, 255);
  1093. font->draw(utf8_to_wide(text).c_str(), rect2, color, false, false, &viewrect);
  1094. }
  1095. }
  1096. void drawItemStack(
  1097. video::IVideoDriver *driver,
  1098. gui::IGUIFont *font,
  1099. const ItemStack &item,
  1100. const core::rect<s32> &rect,
  1101. const core::rect<s32> *clip,
  1102. Client *client,
  1103. ItemRotationKind rotation_kind)
  1104. {
  1105. drawItemStack(driver, font, item, rect, clip, client, rotation_kind,
  1106. v3s16(0, 0, 0), v3s16(0, 100, 0));
  1107. }