Skip to content

Commit

Permalink
Semi-transparent background for nametags (#10152)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zughy committed Dec 14, 2020
1 parent f2c8c6b commit 4d41ed0
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
29 changes: 29 additions & 0 deletions games/devtest/mods/testentities/visuals.lua
Expand Up @@ -94,3 +94,32 @@ minetest.register_entity("testentities:upright_animated", {
self.object:set_sprite({x=0, y=0}, 4, 1.0, false)
end,
})

minetest.register_entity("testentities:nametag", {
initial_properties = {
visual = "sprite",
textures = { "testentities_sprite.png" },
},

on_activate = function(self, staticdata)
if staticdata ~= "" then
self.color = minetest.deserialize(staticdata).color
else
self.color = {
r = math.random(0, 255),
g = math.random(0, 255),
b = math.random(0, 255),
}
end

assert(self.color)
self.object:set_properties({
nametag = tostring(math.random(1000, 10000)),
nametag_color = self.color,
})
end,

get_staticdata = function(self)
return minetest.serialize({ color = self.color })
end,
})
27 changes: 18 additions & 9 deletions src/client/camera.cpp
Expand Up @@ -691,10 +691,11 @@ void Camera::drawNametags()
core::matrix4 trans = m_cameranode->getProjectionMatrix();
trans *= m_cameranode->getViewMatrix();

for (std::list<Nametag *>::const_iterator
i = m_nametags.begin();
i != m_nametags.end(); ++i) {
Nametag *nametag = *i;
gui::IGUIFont *font = g_fontengine->getFont();
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
v2u32 screensize = driver->getScreenSize();

for (const Nametag *nametag : m_nametags) {
if (nametag->nametag_color.getAlpha() == 0) {
// Enforce hiding nametag,
// because if freetype is enabled, a grey
Expand All @@ -707,21 +708,29 @@ void Camera::drawNametags()
if (transformed_pos[3] > 0) {
std::wstring nametag_colorless =
unescape_translate(utf8_to_wide(nametag->nametag_text));
core::dimension2d<u32> textsize =
g_fontengine->getFont()->getDimension(
core::dimension2d<u32> textsize = font->getDimension(
nametag_colorless.c_str());
f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
core::reciprocal(transformed_pos[3]);
v2u32 screensize = RenderingEngine::get_video_driver()->getScreenSize();
v2s32 screen_pos;
screen_pos.X = screensize.X *
(0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2;
screen_pos.Y = screensize.Y *
(0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2;
core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
g_fontengine->getFont()->draw(
core::rect<s32> bg_size(-2, 0, textsize.Width+2, textsize.Height);

video::SColor textColor = nametag->nametag_color;

bool darkBackground = textColor.getLuminance() > 186;
video::SColor backgroundColor = darkBackground
? video::SColor(50, 50, 50, 50)
: video::SColor(50, 255, 255, 255);
driver->draw2DRectangle(backgroundColor, bg_size + screen_pos);

font->draw(
translate_string(utf8_to_wide(nametag->nametag_text)).c_str(),
size + screen_pos, nametag->nametag_color);
size + screen_pos, textColor);
}
}
}
Expand Down

0 comments on commit 4d41ed0

Please sign in to comment.