]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/client/hud.cpp
Merge branch 'master' of https://github.com/minetest/minetest
[dragonfireclient.git] / src / client / hud.cpp
index 3f687b698dcb602ebd0f07fd8dd3767725be3719..d5debecfbb71ade34c6485f78609a4ed3ef925ea 100644 (file)
@@ -20,6 +20,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 */
 
 #include "client/hud.h"
+#include <string>
+#include <iostream>
 #include <cmath>
 #include "settings.h"
 #include "util/numeric.h"
@@ -222,6 +224,7 @@ void Hud::drawItem(const ItemStack &item, const core::rect<s32>& rect,
 }
 
 //NOTE: selectitem = 0 -> no selected; selectitem 1-based
+// mainlist can be NULL, but draw the frame anyway.
 void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
                s32 inv_offset, InventoryList *mainlist, u16 selectitem, u16 direction)
 {
@@ -269,7 +272,8 @@ void Hud::drawItems(v2s32 upperleftpos, v2s32 screen_offset, s32 itemcount,
 
        // Draw items
        core::rect<s32> imgrect(0, 0, m_hotbar_imagesize, m_hotbar_imagesize);
-       for (s32 i = inv_offset; i < itemcount && (size_t)i < mainlist->getSize(); i++) {
+       const s32 list_size = mainlist ? mainlist->getSize() : 0;
+       for (s32 i = inv_offset; i < itemcount && i < list_size; i++) {
                s32 fullimglen = m_hotbar_imagesize + m_padding * 2;
 
                v2s32 steppos;
@@ -377,15 +381,19 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
                                std::wstring text = unescape_translate(utf8_to_wide(e->text));
                                core::dimension2d<u32> textsize = textfont->getDimension(text.c_str());
 
-                               v2s32 offset((e->align.X - 1.0) * (textsize.Width / 2),
-                                            (e->align.Y - 1.0) * (textsize.Height / 2));
+                               v2s32 offset(0, (e->align.Y - 1.0) * (textsize.Height / 2));
                                core::rect<s32> size(0, 0, e->scale.X * m_scale_factor,
-                                                    text_height * e->scale.Y * m_scale_factor);
+                                               text_height * e->scale.Y * m_scale_factor);
                                v2s32 offs(e->offset.X * m_scale_factor,
-                                          e->offset.Y * m_scale_factor);
-
+                                               e->offset.Y * m_scale_factor);
+                               std::wstringstream wss(text);
+                               std::wstring line;
+                               while (std::getline(wss, line, L'\n'))
                                {
-                                       textfont->draw(text.c_str(), size + pos + offset + offs, color);
+                                       core::dimension2d<u32> linesize = textfont->getDimension(line.c_str());
+                                       v2s32 line_offset((e->align.X - 1.0) * (linesize.Width / 2), 0);
+                                       textfont->draw(line.c_str(), size + pos + offset + offs + line_offset, color);
+                                       offset.Y += linesize.Height;
                                }
                                break; }
                        case HUD_ELEM_STATBAR: {
@@ -395,6 +403,8 @@ void Hud::drawLuaElements(const v3s16 &camera_offset)
                                break; }
                        case HUD_ELEM_INVENTORY: {
                                InventoryList *inv = inventory->getList(e->text);
+                               if (!inv)
+                                       warningstream << "HUD: Unknown inventory list. name=" << e->text << std::endl;
                                drawItems(pos, v2s32(e->offset.X, e->offset.Y), e->number, 0,
                                        inv, e->item, e->dir);
                                break; }
@@ -666,7 +676,7 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
        // Rectangles for 1/2 the "off state" texture
        core::rect<s32> srchalfrect2, dsthalfrect2;
 
-       if (count % 2 == 1) {
+       if (count % 2 == 1 || maxcount % 2 == 1) {
                // Need to draw halves: Calculate rectangles
                srchalfrect  = calculate_clipping_rect(srcd, steppos);
                dsthalfrect  = calculate_clipping_rect(dstd, steppos);
@@ -701,7 +711,7 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
                }
        }
 
-       if (stat_texture_bg && maxcount > count / 2) {
+       if (stat_texture_bg && maxcount > count) {
                // Draw "off state" textures
                s32 start_offset;
                if (count % 2 == 1)
@@ -721,8 +731,7 @@ void Hud::drawStatbar(v2s32 pos, u16 corner, u16 drawdir,
 
                if (maxcount % 2 == 1) {
                        draw2DImageFilterScaled(driver, stat_texture_bg,
-                                       dsthalfrect + p, srchalfrect,
-                                       NULL, colors, true);
+                               dsthalfrect + p, srchalfrect, NULL, colors, true);
                }
        }
 }
@@ -1001,11 +1010,19 @@ void drawItemStack(
 
        bool draw_overlay = false;
 
+       bool has_mesh = false;
+       ItemMesh *imesh;
+
+       core::rect<s32> viewrect = rect;
+       if (clip != nullptr)
+               viewrect.clipAgainst(*clip);
+
        // Render as mesh if animated or no inventory image
        if ((enable_animations && rotation_kind < IT_ROT_NONE) || def.inventory_image.empty()) {
-               ItemMesh *imesh = client->idef()->getWieldMesh(def.name, client);
-               if (!imesh || !imesh->mesh)
-                       return;
+               imesh = client->idef()->getWieldMesh(def.name, client);
+               has_mesh = imesh && imesh->mesh;
+       }
+       if (has_mesh) {
                scene::IMesh *mesh = imesh->mesh;
                driver->clearBuffers(video::ECBF_DEPTH);
                s32 delta = 0;
@@ -1021,9 +1038,6 @@ void drawItemStack(
                core::rect<s32> oldViewPort = driver->getViewPort();
                core::matrix4 oldProjMat = driver->getTransform(video::ETS_PROJECTION);
                core::matrix4 oldViewMat = driver->getTransform(video::ETS_VIEW);
-               core::rect<s32> viewrect = rect;
-               if (clip)
-                       viewrect.clipAgainst(*clip);
 
                core::matrix4 ProjMatrix;
                ProjMatrix.buildProjectionMatrixOrthoLH(2.0f, 2.0f, -1.0f, 100.0f);
@@ -1097,10 +1111,17 @@ void drawItemStack(
                draw_overlay = def.type == ITEM_NODE && def.inventory_image.empty();
        } else { // Otherwise just draw as 2D
                video::ITexture *texture = client->idef()->getInventoryTexture(def.name, client);
-               if (!texture)
-                       return;
-               video::SColor color =
-                       client->idef()->getItemstackColor(item, client);
+               video::SColor color;
+               if (texture) {
+                       color = client->idef()->getItemstackColor(item, client);
+               } else {
+                       color = video::SColor(255, 255, 255, 255);
+                       ITextureSource *tsrc = client->getTextureSource();
+                       texture = tsrc->getTexture("no_texture.png");
+                       if (!texture)
+                               return;
+               }
+
                const video::SColor colors[] = { color, color, color, color };
 
                draw2DImageFilterScaled(driver, texture, rect,
@@ -1160,24 +1181,68 @@ void drawItemStack(
                driver->draw2DRectangle(color, progressrect2, clip);
        }
 
-       if (font != NULL && item.count >= 2) {
+       const std::string &count_text = item.metadata.getString("count_meta");
+       if (font != nullptr && (item.count >= 2 || !count_text.empty())) {
                // Get the item count as a string
-               std::string text = itos(item.count);
-               v2u32 dim = font->getDimension(utf8_to_wide(text).c_str());
+               std::string text = count_text.empty() ? itos(item.count) : count_text;
+               v2u32 dim = font->getDimension(utf8_to_wide(unescape_enriched(text)).c_str());
                v2s32 sdim(dim.X, dim.Y);
 
                core::rect<s32> rect2(
-                       /*rect.UpperLeftCorner,
-                       core::dimension2d<u32>(rect.getWidth(), 15)*/
                        rect.LowerRightCorner - sdim,
-                       sdim
+                       rect.LowerRightCorner
                );
 
-               video::SColor bgcolor(128, 0, 0, 0);
-               driver->draw2DRectangle(bgcolor, rect2, clip);
+               // get the count alignment
+               s32 count_alignment = stoi(item.metadata.getString("count_alignment"));
+               if (count_alignment != 0) {
+                       s32 a_x = count_alignment & 3;
+                       s32 a_y = (count_alignment >> 2) & 3;
+
+                       s32 x1, x2, y1, y2;
+                       switch (a_x) {
+                       case 1: // left
+                               x1 = rect.UpperLeftCorner.X;
+                               x2 = x1 + sdim.X;
+                               break;
+                       case 2: // middle
+                               x1 = (rect.UpperLeftCorner.X + rect.LowerRightCorner.X - sdim.X) / 2;
+                               x2 = x1 + sdim.X;
+                               break;
+                       case 3: // right
+                               x2 = rect.LowerRightCorner.X;
+                               x1 = x2 - sdim.X;
+                               break;
+                       default: // 0 = default
+                               x1 = rect2.UpperLeftCorner.X;
+                               x2 = rect2.LowerRightCorner.X;
+                               break;
+                       }
+
+                       switch (a_y) {
+                       case 1: // up
+                               y1 = rect.UpperLeftCorner.Y;
+                               y2 = y1 + sdim.Y;
+                               break;
+                       case 2: // middle
+                               y1 = (rect.UpperLeftCorner.Y + rect.LowerRightCorner.Y - sdim.Y) / 2;
+                               y2 = y1 + sdim.Y;
+                               break;
+                       case 3: // down
+                               y2 = rect.LowerRightCorner.Y;
+                               y1 = y2 - sdim.Y;
+                               break;
+                       default: // 0 = default
+                               y1 = rect2.UpperLeftCorner.Y;
+                               y2 = rect2.LowerRightCorner.Y;
+                               break;
+                       }
+
+                       rect2 = core::rect<s32>(x1, y1, x2, y2);
+               }
 
                video::SColor color(255, 255, 255, 255);
-               font->draw(text.c_str(), rect2, color, false, false, clip);
+               font->draw(utf8_to_wide(text).c_str(), rect2, color, false, false, &viewrect);
        }
 }