hud.cpp 41 KB

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