diff --git a/games/devtest/mods/testentities/visuals.lua b/games/devtest/mods/testentities/visuals.lua index 8848ba49f..e3b758329 100644 --- a/games/devtest/mods/testentities/visuals.lua +++ b/games/devtest/mods/testentities/visuals.lua @@ -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, +}) diff --git a/src/client/camera.cpp b/src/client/camera.cpp index 11f8a1c90..9a08254b4 100644 --- a/src/client/camera.cpp +++ b/src/client/camera.cpp @@ -691,10 +691,11 @@ void Camera::drawNametags() core::matrix4 trans = m_cameranode->getProjectionMatrix(); trans *= m_cameranode->getViewMatrix(); - for (std::list::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 @@ -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 textsize = - g_fontengine->getFont()->getDimension( + core::dimension2d 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 size(0, 0, textsize.Width, textsize.Height); - g_fontengine->getFont()->draw( + core::rect 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); } } }