Semi-transparent background for nametags (#10152)

This commit is contained in:
Zughy 2020-12-14 23:49:30 +01:00 committed by GitHub
parent f2c8c6bf51
commit 4d41ed0975
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 47 additions and 9 deletions

@ -94,3 +94,32 @@ minetest.register_entity("testentities:upright_animated", {
self.object:set_sprite({x=0, y=0}, 4, 1.0, false) self.object:set_sprite({x=0, y=0}, 4, 1.0, false)
end, 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,
})

@ -691,10 +691,11 @@ void Camera::drawNametags()
core::matrix4 trans = m_cameranode->getProjectionMatrix(); core::matrix4 trans = m_cameranode->getProjectionMatrix();
trans *= m_cameranode->getViewMatrix(); trans *= m_cameranode->getViewMatrix();
for (std::list<Nametag *>::const_iterator gui::IGUIFont *font = g_fontengine->getFont();
i = m_nametags.begin(); video::IVideoDriver *driver = RenderingEngine::get_video_driver();
i != m_nametags.end(); ++i) { v2u32 screensize = driver->getScreenSize();
Nametag *nametag = *i;
for (const Nametag *nametag : m_nametags) {
if (nametag->nametag_color.getAlpha() == 0) { if (nametag->nametag_color.getAlpha() == 0) {
// Enforce hiding nametag, // Enforce hiding nametag,
// because if freetype is enabled, a grey // because if freetype is enabled, a grey
@ -707,21 +708,29 @@ void Camera::drawNametags()
if (transformed_pos[3] > 0) { if (transformed_pos[3] > 0) {
std::wstring nametag_colorless = std::wstring nametag_colorless =
unescape_translate(utf8_to_wide(nametag->nametag_text)); unescape_translate(utf8_to_wide(nametag->nametag_text));
core::dimension2d<u32> textsize = core::dimension2d<u32> textsize = font->getDimension(
g_fontengine->getFont()->getDimension(
nametag_colorless.c_str()); nametag_colorless.c_str());
f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f : f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
core::reciprocal(transformed_pos[3]); core::reciprocal(transformed_pos[3]);
v2u32 screensize = RenderingEngine::get_video_driver()->getScreenSize();
v2s32 screen_pos; v2s32 screen_pos;
screen_pos.X = screensize.X * screen_pos.X = screensize.X *
(0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2; (0.5 * transformed_pos[0] * zDiv + 0.5) - textsize.Width / 2;
screen_pos.Y = screensize.Y * screen_pos.Y = screensize.Y *
(0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2; (0.5 - transformed_pos[1] * zDiv * 0.5) - textsize.Height / 2;
core::rect<s32> size(0, 0, textsize.Width, textsize.Height); 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(), translate_string(utf8_to_wide(nametag->nametag_text)).c_str(),
size + screen_pos, nametag->nametag_color); size + screen_pos, textColor);
} }
} }
} }