mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
1
This commit is contained in:
parent
81d62d01d1
commit
147e9bf413
2 changed files with 47 additions and 42 deletions
|
@ -631,40 +631,54 @@ void Camera::drawNametags()
|
||||||
core::matrix4 trans = m_cameranode->getProjectionMatrix();
|
core::matrix4 trans = m_cameranode->getProjectionMatrix();
|
||||||
trans *= m_cameranode->getViewMatrix();
|
trans *= m_cameranode->getViewMatrix();
|
||||||
|
|
||||||
gui::IGUIFont *font = g_fontengine->getFont();
|
const u32 default_font_size = g_fontengine->getFontSize(FM_Unspecified);
|
||||||
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
|
video::IVideoDriver *driver = RenderingEngine::get_video_driver();
|
||||||
v2u32 screensize = driver->getScreenSize();
|
v2u32 screensize = driver->getScreenSize();
|
||||||
|
|
||||||
|
// Note: hidden nametags (e.g. GenericCAO) are removed from the array
|
||||||
for (const Nametag *nametag : m_nametags) {
|
for (const Nametag *nametag : m_nametags) {
|
||||||
// Nametags are hidden in GenericCAO::updateNametag()
|
|
||||||
|
|
||||||
v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->pos * BS;
|
v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->pos * BS;
|
||||||
f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
|
f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
|
||||||
trans.multiplyWith1x4Matrix(transformed_pos);
|
trans.multiplyWith1x4Matrix(transformed_pos);
|
||||||
if (transformed_pos[3] > 0) {
|
if (transformed_pos[3] <= 0) // negative Z means behind camera
|
||||||
std::wstring nametag_colorless =
|
continue;
|
||||||
unescape_translate(utf8_to_wide(nametag->text));
|
f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f : (1.0f / transformed_pos[3]);
|
||||||
core::dimension2d<u32> textsize = font->getDimension(
|
|
||||||
nametag_colorless.c_str());
|
|
||||||
f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
|
|
||||||
core::reciprocal(transformed_pos[3]);
|
|
||||||
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);
|
|
||||||
|
|
||||||
auto bgcolor = nametag->getBgColor(m_show_nametag_backgrounds);
|
u32 font_size = 0;
|
||||||
if (bgcolor.getAlpha() != 0) {
|
if (nametag->scale_z) {
|
||||||
core::rect<s32> bg_size(-2, 0, textsize.Width + 2, textsize.Height);
|
// Higher default since nametag should be reasonably visible
|
||||||
driver->draw2DRectangle(bgcolor, bg_size + screen_pos);
|
// even at distance.
|
||||||
}
|
u32 base_size = nametag->textsize.value_or(default_font_size * 4);
|
||||||
|
font_size = rangelim(base_size * BS * zDiv, 0, base_size);
|
||||||
font->draw(
|
} else {
|
||||||
translate_string(utf8_to_wide(nametag->text)).c_str(),
|
font_size = nametag->textsize.value_or(default_font_size);
|
||||||
size + screen_pos, nametag->textcolor);
|
|
||||||
}
|
}
|
||||||
|
if (font_size <= 1)
|
||||||
|
continue;
|
||||||
|
// TODO: This is quite primitive. It would be better to draw the font once
|
||||||
|
// at its maximum size to an RTT and let normal scaling happen.
|
||||||
|
auto *font = g_fontengine->getFont(font_size);
|
||||||
|
assert(font);
|
||||||
|
|
||||||
|
std::wstring text =
|
||||||
|
unescape_translate(utf8_to_wide(nametag->text));
|
||||||
|
core::dimension2d<u32> textsize = font->getDimension(text.c_str());
|
||||||
|
v2s32 screen_pos;
|
||||||
|
screen_pos.X = screensize.X *
|
||||||
|
(0.5f + transformed_pos[0] * zDiv * 0.5f) - textsize.Width / 2;
|
||||||
|
screen_pos.Y = screensize.Y *
|
||||||
|
(0.5f - transformed_pos[1] * zDiv * 0.5f) - textsize.Height / 2;
|
||||||
|
core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
|
||||||
|
|
||||||
|
auto bgcolor = nametag->getBgColor(m_show_nametag_backgrounds);
|
||||||
|
if (bgcolor.getAlpha() != 0) {
|
||||||
|
core::rect<s32> bg_size(-2, 0, textsize.Width + 2, textsize.Height);
|
||||||
|
driver->draw2DRectangle(bgcolor, bg_size + screen_pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
font->draw(
|
||||||
|
translate_string(utf8_to_wide(nametag->text)).c_str(),
|
||||||
|
size + screen_pos, nametag->textcolor);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -672,7 +686,9 @@ Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
|
||||||
const std::string &text, video::SColor textcolor,
|
const std::string &text, video::SColor textcolor,
|
||||||
std::optional<video::SColor> bgcolor, const v3f &pos)
|
std::optional<video::SColor> bgcolor, const v3f &pos)
|
||||||
{
|
{
|
||||||
Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos);
|
assert(parent_node);
|
||||||
|
Nametag *nametag = new Nametag{
|
||||||
|
parent_node, text, textcolor, bgcolor, std::nullopt, pos, false};
|
||||||
m_nametags.push_back(nametag);
|
m_nametags.push_back(nametag);
|
||||||
return nametag;
|
return nametag;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,28 +23,17 @@ class WieldMeshSceneNode;
|
||||||
|
|
||||||
struct Nametag
|
struct Nametag
|
||||||
{
|
{
|
||||||
scene::ISceneNode *parent_node;
|
scene::ISceneNode *parent_node = nullptr;
|
||||||
std::string text;
|
std::string text;
|
||||||
video::SColor textcolor;
|
video::SColor textcolor;
|
||||||
std::optional<video::SColor> bgcolor;
|
std::optional<video::SColor> bgcolor;
|
||||||
v3f pos;
|
std::optional<u32> textsize;
|
||||||
|
v3f pos; // offset from parent node
|
||||||
Nametag(scene::ISceneNode *a_parent_node,
|
bool scale_z;
|
||||||
const std::string &text,
|
|
||||||
const video::SColor &textcolor,
|
|
||||||
const std::optional<video::SColor> &bgcolor,
|
|
||||||
const v3f &pos):
|
|
||||||
parent_node(a_parent_node),
|
|
||||||
text(text),
|
|
||||||
textcolor(textcolor),
|
|
||||||
bgcolor(bgcolor),
|
|
||||||
pos(pos)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
video::SColor getBgColor(bool use_fallback) const
|
video::SColor getBgColor(bool use_fallback) const
|
||||||
{
|
{
|
||||||
if (bgcolor)
|
if (bgcolor.has_value())
|
||||||
return bgcolor.value();
|
return bgcolor.value();
|
||||||
else if (!use_fallback)
|
else if (!use_fallback)
|
||||||
return video::SColor(0, 0, 0, 0);
|
return video::SColor(0, 0, 0, 0);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue