mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-01 17:38:41 +00:00
Add nametag background setting and object property (#10937)
This commit is contained in:
parent
a8f6befd39
commit
f85e9ab925
17 changed files with 254 additions and 58 deletions
|
@ -79,6 +79,7 @@ Camera::Camera(MapDrawControl &draw_control, Client *client):
|
|||
m_cache_fov = std::fmax(g_settings->getFloat("fov"), 45.0f);
|
||||
m_arm_inertia = g_settings->getBool("arm_inertia");
|
||||
m_nametags.clear();
|
||||
m_show_nametag_backgrounds = g_settings->getBool("show_nametag_backgrounds");
|
||||
}
|
||||
|
||||
Camera::~Camera()
|
||||
|
@ -696,18 +697,14 @@ void Camera::drawNametags()
|
|||
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
|
||||
// shadow can remain.
|
||||
continue;
|
||||
}
|
||||
v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->nametag_pos * BS;
|
||||
// Nametags are hidden in GenericCAO::updateNametag()
|
||||
|
||||
v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->pos * BS;
|
||||
f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
|
||||
trans.multiplyWith1x4Matrix(transformed_pos);
|
||||
if (transformed_pos[3] > 0) {
|
||||
std::wstring nametag_colorless =
|
||||
unescape_translate(utf8_to_wide(nametag->nametag_text));
|
||||
unescape_translate(utf8_to_wide(nametag->text));
|
||||
core::dimension2d<u32> textsize = font->getDimension(
|
||||
nametag_colorless.c_str());
|
||||
f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
|
||||
|
@ -720,26 +717,22 @@ void Camera::drawNametags()
|
|||
core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
|
||||
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);
|
||||
auto bgcolor = nametag->getBgColor(m_show_nametag_backgrounds);
|
||||
if (bgcolor.getAlpha() != 0)
|
||||
driver->draw2DRectangle(bgcolor, bg_size + screen_pos);
|
||||
|
||||
font->draw(
|
||||
translate_string(utf8_to_wide(nametag->nametag_text)).c_str(),
|
||||
size + screen_pos, textColor);
|
||||
translate_string(utf8_to_wide(nametag->text)).c_str(),
|
||||
size + screen_pos, nametag->textcolor);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
|
||||
const std::string &nametag_text, video::SColor nametag_color,
|
||||
const v3f &pos)
|
||||
const std::string &text, video::SColor textcolor,
|
||||
Optional<video::SColor> bgcolor, const v3f &pos)
|
||||
{
|
||||
Nametag *nametag = new Nametag(parent_node, nametag_text, nametag_color, pos);
|
||||
Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos);
|
||||
m_nametags.push_back(nametag);
|
||||
return nametag;
|
||||
}
|
||||
|
|
|
@ -25,27 +25,47 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include <ICameraSceneNode.h>
|
||||
#include <ISceneNode.h>
|
||||
#include <list>
|
||||
#include "util/Optional.h"
|
||||
|
||||
class LocalPlayer;
|
||||
struct MapDrawControl;
|
||||
class Client;
|
||||
class WieldMeshSceneNode;
|
||||
|
||||
struct Nametag {
|
||||
struct Nametag
|
||||
{
|
||||
scene::ISceneNode *parent_node;
|
||||
std::string text;
|
||||
video::SColor textcolor;
|
||||
Optional<video::SColor> bgcolor;
|
||||
v3f pos;
|
||||
|
||||
Nametag(scene::ISceneNode *a_parent_node,
|
||||
const std::string &a_nametag_text,
|
||||
const video::SColor &a_nametag_color,
|
||||
const v3f &a_nametag_pos):
|
||||
const std::string &text,
|
||||
const video::SColor &textcolor,
|
||||
const Optional<video::SColor> &bgcolor,
|
||||
const v3f &pos):
|
||||
parent_node(a_parent_node),
|
||||
nametag_text(a_nametag_text),
|
||||
nametag_color(a_nametag_color),
|
||||
nametag_pos(a_nametag_pos)
|
||||
text(text),
|
||||
textcolor(textcolor),
|
||||
bgcolor(bgcolor),
|
||||
pos(pos)
|
||||
{
|
||||
}
|
||||
scene::ISceneNode *parent_node;
|
||||
std::string nametag_text;
|
||||
video::SColor nametag_color;
|
||||
v3f nametag_pos;
|
||||
|
||||
video::SColor getBgColor(bool use_fallback) const
|
||||
{
|
||||
if (bgcolor)
|
||||
return bgcolor.value();
|
||||
else if (!use_fallback)
|
||||
return video::SColor(0, 0, 0, 0);
|
||||
else if (textcolor.getLuminance() > 186)
|
||||
// Dark background for light text
|
||||
return video::SColor(50, 50, 50, 50);
|
||||
else
|
||||
// Light background for dark text
|
||||
return video::SColor(50, 255, 255, 255);
|
||||
}
|
||||
};
|
||||
|
||||
enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
|
||||
|
@ -164,8 +184,8 @@ public:
|
|||
}
|
||||
|
||||
Nametag *addNametag(scene::ISceneNode *parent_node,
|
||||
const std::string &nametag_text, video::SColor nametag_color,
|
||||
const v3f &pos);
|
||||
const std::string &text, video::SColor textcolor,
|
||||
Optional<video::SColor> bgcolor, const v3f &pos);
|
||||
|
||||
void removeNametag(Nametag *nametag);
|
||||
|
||||
|
@ -245,4 +265,5 @@ private:
|
|||
bool m_arm_inertia;
|
||||
|
||||
std::list<Nametag *> m_nametags;
|
||||
bool m_show_nametag_backgrounds;
|
||||
};
|
||||
|
|
|
@ -934,7 +934,7 @@ void GenericCAO::updateNametag()
|
|||
if (m_is_local_player) // No nametag for local player
|
||||
return;
|
||||
|
||||
if (m_prop.nametag.empty()) {
|
||||
if (m_prop.nametag.empty() || m_prop.nametag_color.getAlpha() == 0) {
|
||||
// Delete nametag
|
||||
if (m_nametag) {
|
||||
m_client->getCamera()->removeNametag(m_nametag);
|
||||
|
@ -952,12 +952,14 @@ void GenericCAO::updateNametag()
|
|||
if (!m_nametag) {
|
||||
// Add nametag
|
||||
m_nametag = m_client->getCamera()->addNametag(node,
|
||||
m_prop.nametag, m_prop.nametag_color, pos);
|
||||
m_prop.nametag, m_prop.nametag_color,
|
||||
m_prop.nametag_bgcolor, pos);
|
||||
} else {
|
||||
// Update nametag
|
||||
m_nametag->nametag_text = m_prop.nametag;
|
||||
m_nametag->nametag_color = m_prop.nametag_color;
|
||||
m_nametag->nametag_pos = pos;
|
||||
m_nametag->text = m_prop.nametag;
|
||||
m_nametag->textcolor = m_prop.nametag_color;
|
||||
m_nametag->bgcolor = m_prop.nametag_bgcolor;
|
||||
m_nametag->pos = pos;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue