Skip to content

Commit f85e9ab

Browse files
authored
Add nametag background setting and object property (#10937)
1 parent a8f6bef commit f85e9ab

File tree

17 files changed

+254
-58
lines changed

17 files changed

+254
-58
lines changed

.clang-format

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ AlignAfterOpenBracket: DontAlign
2929
ContinuationIndentWidth: 16
3030
ConstructorInitializerIndentWidth: 16
3131
BreakConstructorInitializers: AfterColon
32+
AlwaysBreakTemplateDeclarations: Yes

builtin/settingtypes.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,10 @@ keymap_decrease_viewing_range_min (View range decrease key) key -
451451

452452
[**Basic]
453453

454+
# Whether nametag backgrounds should be shown by default.
455+
# Mods may still set a background.
456+
show_nametag_backgrounds (Show nametag backgrounds by default) bool true
457+
454458
# Enable vertex buffer objects.
455459
# This should greatly improve graphics performance.
456460
enable_vbo (VBO) bool true

doc/lua_api.txt

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6274,15 +6274,21 @@ object you are working with still exists.
62746274
* `get_nametag_attributes()`
62756275
* returns a table with the attributes of the nametag of an object
62766276
* {
6277-
color = {a=0..255, r=0..255, g=0..255, b=0..255},
62786277
text = "",
6278+
color = {a=0..255, r=0..255, g=0..255, b=0..255},
6279+
bgcolor = {a=0..255, r=0..255, g=0..255, b=0..255},
62796280
}
62806281
* `set_nametag_attributes(attributes)`
62816282
* sets the attributes of the nametag of an object
62826283
* `attributes`:
62836284
{
6284-
color = ColorSpec,
62856285
text = "My Nametag",
6286+
color = ColorSpec,
6287+
-- ^ Text color
6288+
bgcolor = ColorSpec or false,
6289+
-- ^ Sets background color of nametag
6290+
-- `false` will cause the background to be set automatically based on user settings
6291+
-- Default: false
62866292
}
62876293

62886294
#### Lua entity only (no-op for other objects)
@@ -6956,9 +6962,13 @@ Player properties need to be saved manually.
69566962
-- For all other objects, a nil or empty string removes the nametag.
69576963
-- To hide a nametag, set its color alpha to zero. That will disable it entirely.
69586964

6959-
69606965
nametag_color = <ColorSpec>,
6961-
-- Sets color of nametag
6966+
-- Sets text color of nametag
6967+
6968+
nametag_bgcolor = <ColorSpec>,
6969+
-- Sets background color of nametag
6970+
-- `false` will cause the background to be set automatically based on user settings.
6971+
-- Default: false
69626972

69636973
infotext = "",
69646974
-- By default empty, text to be shown when pointed at object

games/devtest/mods/testentities/visuals.lua

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,23 +103,35 @@ minetest.register_entity("testentities:nametag", {
103103

104104
on_activate = function(self, staticdata)
105105
if staticdata ~= "" then
106-
self.color = minetest.deserialize(staticdata).color
106+
local data = minetest.deserialize(staticdata)
107+
self.color = data.color
108+
self.bgcolor = data.bgcolor
107109
else
108110
self.color = {
109111
r = math.random(0, 255),
110112
g = math.random(0, 255),
111113
b = math.random(0, 255),
112114
}
115+
116+
if math.random(0, 10) > 5 then
117+
self.bgcolor = {
118+
r = math.random(0, 255),
119+
g = math.random(0, 255),
120+
b = math.random(0, 255),
121+
a = math.random(0, 255),
122+
}
123+
end
113124
end
114125

115126
assert(self.color)
116127
self.object:set_properties({
117128
nametag = tostring(math.random(1000, 10000)),
118129
nametag_color = self.color,
130+
nametag_bgcolor = self.bgcolor,
119131
})
120132
end,
121133

122134
get_staticdata = function(self)
123-
return minetest.serialize({ color = self.color })
135+
return minetest.serialize({ color = self.color, bgcolor = self.bgcolor })
124136
end,
125137
})

src/activeobjectmgr.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2525
class TestClientActiveObjectMgr;
2626
class TestServerActiveObjectMgr;
2727

28-
template <typename T> class ActiveObjectMgr
28+
template <typename T>
29+
class ActiveObjectMgr
2930
{
3031
friend class ::TestClientActiveObjectMgr;
3132
friend class ::TestServerActiveObjectMgr;

src/client/camera.cpp

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ Camera::Camera(MapDrawControl &draw_control, Client *client):
7979
m_cache_fov = std::fmax(g_settings->getFloat("fov"), 45.0f);
8080
m_arm_inertia = g_settings->getBool("arm_inertia");
8181
m_nametags.clear();
82+
m_show_nametag_backgrounds = g_settings->getBool("show_nametag_backgrounds");
8283
}
8384

8485
Camera::~Camera()
@@ -696,18 +697,14 @@ void Camera::drawNametags()
696697
v2u32 screensize = driver->getScreenSize();
697698

698699
for (const Nametag *nametag : m_nametags) {
699-
if (nametag->nametag_color.getAlpha() == 0) {
700-
// Enforce hiding nametag,
701-
// because if freetype is enabled, a grey
702-
// shadow can remain.
703-
continue;
704-
}
705-
v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->nametag_pos * BS;
700+
// Nametags are hidden in GenericCAO::updateNametag()
701+
702+
v3f pos = nametag->parent_node->getAbsolutePosition() + nametag->pos * BS;
706703
f32 transformed_pos[4] = { pos.X, pos.Y, pos.Z, 1.0f };
707704
trans.multiplyWith1x4Matrix(transformed_pos);
708705
if (transformed_pos[3] > 0) {
709706
std::wstring nametag_colorless =
710-
unescape_translate(utf8_to_wide(nametag->nametag_text));
707+
unescape_translate(utf8_to_wide(nametag->text));
711708
core::dimension2d<u32> textsize = font->getDimension(
712709
nametag_colorless.c_str());
713710
f32 zDiv = transformed_pos[3] == 0.0f ? 1.0f :
@@ -720,26 +717,22 @@ void Camera::drawNametags()
720717
core::rect<s32> size(0, 0, textsize.Width, textsize.Height);
721718
core::rect<s32> bg_size(-2, 0, textsize.Width+2, textsize.Height);
722719

723-
video::SColor textColor = nametag->nametag_color;
724-
725-
bool darkBackground = textColor.getLuminance() > 186;
726-
video::SColor backgroundColor = darkBackground
727-
? video::SColor(50, 50, 50, 50)
728-
: video::SColor(50, 255, 255, 255);
729-
driver->draw2DRectangle(backgroundColor, bg_size + screen_pos);
720+
auto bgcolor = nametag->getBgColor(m_show_nametag_backgrounds);
721+
if (bgcolor.getAlpha() != 0)
722+
driver->draw2DRectangle(bgcolor, bg_size + screen_pos);
730723

731724
font->draw(
732-
translate_string(utf8_to_wide(nametag->nametag_text)).c_str(),
733-
size + screen_pos, textColor);
725+
translate_string(utf8_to_wide(nametag->text)).c_str(),
726+
size + screen_pos, nametag->textcolor);
734727
}
735728
}
736729
}
737730

738731
Nametag *Camera::addNametag(scene::ISceneNode *parent_node,
739-
const std::string &nametag_text, video::SColor nametag_color,
740-
const v3f &pos)
732+
const std::string &text, video::SColor textcolor,
733+
Optional<video::SColor> bgcolor, const v3f &pos)
741734
{
742-
Nametag *nametag = new Nametag(parent_node, nametag_text, nametag_color, pos);
735+
Nametag *nametag = new Nametag(parent_node, text, textcolor, bgcolor, pos);
743736
m_nametags.push_back(nametag);
744737
return nametag;
745738
}

src/client/camera.h

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,27 +25,47 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2525
#include <ICameraSceneNode.h>
2626
#include <ISceneNode.h>
2727
#include <list>
28+
#include "util/Optional.h"
2829

2930
class LocalPlayer;
3031
struct MapDrawControl;
3132
class Client;
3233
class WieldMeshSceneNode;
3334

34-
struct Nametag {
35+
struct Nametag
36+
{
37+
scene::ISceneNode *parent_node;
38+
std::string text;
39+
video::SColor textcolor;
40+
Optional<video::SColor> bgcolor;
41+
v3f pos;
42+
3543
Nametag(scene::ISceneNode *a_parent_node,
36-
const std::string &a_nametag_text,
37-
const video::SColor &a_nametag_color,
38-
const v3f &a_nametag_pos):
44+
const std::string &text,
45+
const video::SColor &textcolor,
46+
const Optional<video::SColor> &bgcolor,
47+
const v3f &pos):
3948
parent_node(a_parent_node),
40-
nametag_text(a_nametag_text),
41-
nametag_color(a_nametag_color),
42-
nametag_pos(a_nametag_pos)
49+
text(text),
50+
textcolor(textcolor),
51+
bgcolor(bgcolor),
52+
pos(pos)
4353
{
4454
}
45-
scene::ISceneNode *parent_node;
46-
std::string nametag_text;
47-
video::SColor nametag_color;
48-
v3f nametag_pos;
55+
56+
video::SColor getBgColor(bool use_fallback) const
57+
{
58+
if (bgcolor)
59+
return bgcolor.value();
60+
else if (!use_fallback)
61+
return video::SColor(0, 0, 0, 0);
62+
else if (textcolor.getLuminance() > 186)
63+
// Dark background for light text
64+
return video::SColor(50, 50, 50, 50);
65+
else
66+
// Light background for dark text
67+
return video::SColor(50, 255, 255, 255);
68+
}
4969
};
5070

5171
enum CameraMode {CAMERA_MODE_FIRST, CAMERA_MODE_THIRD, CAMERA_MODE_THIRD_FRONT};
@@ -164,8 +184,8 @@ class Camera
164184
}
165185

166186
Nametag *addNametag(scene::ISceneNode *parent_node,
167-
const std::string &nametag_text, video::SColor nametag_color,
168-
const v3f &pos);
187+
const std::string &text, video::SColor textcolor,
188+
Optional<video::SColor> bgcolor, const v3f &pos);
169189

170190
void removeNametag(Nametag *nametag);
171191

@@ -245,4 +265,5 @@ class Camera
245265
bool m_arm_inertia;
246266

247267
std::list<Nametag *> m_nametags;
268+
bool m_show_nametag_backgrounds;
248269
};

src/client/content_cao.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -934,7 +934,7 @@ void GenericCAO::updateNametag()
934934
if (m_is_local_player) // No nametag for local player
935935
return;
936936

937-
if (m_prop.nametag.empty()) {
937+
if (m_prop.nametag.empty() || m_prop.nametag_color.getAlpha() == 0) {
938938
// Delete nametag
939939
if (m_nametag) {
940940
m_client->getCamera()->removeNametag(m_nametag);
@@ -952,12 +952,14 @@ void GenericCAO::updateNametag()
952952
if (!m_nametag) {
953953
// Add nametag
954954
m_nametag = m_client->getCamera()->addNametag(node,
955-
m_prop.nametag, m_prop.nametag_color, pos);
955+
m_prop.nametag, m_prop.nametag_color,
956+
m_prop.nametag_bgcolor, pos);
956957
} else {
957958
// Update nametag
958-
m_nametag->nametag_text = m_prop.nametag;
959-
m_nametag->nametag_color = m_prop.nametag_color;
960-
m_nametag->nametag_pos = pos;
959+
m_nametag->text = m_prop.nametag;
960+
m_nametag->textcolor = m_prop.nametag_color;
961+
m_nametag->bgcolor = m_prop.nametag_bgcolor;
962+
m_nametag->pos = pos;
961963
}
962964
}
963965

src/defaultsettings.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,7 @@ void set_default_settings()
240240
#endif
241241
settings->setDefault("enable_particles", "true");
242242
settings->setDefault("arm_inertia", "true");
243+
settings->setDefault("show_nametag_backgrounds", "true");
243244

244245
settings->setDefault("enable_minimap", "true");
245246
settings->setDefault("minimap_shape_round", "true");

src/object_properties.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2424
#include "util/basic_macros.h"
2525
#include <sstream>
2626

27+
static const video::SColor NULL_BGCOLOR{0, 1, 1, 1};
28+
2729
ObjectProperties::ObjectProperties()
2830
{
2931
textures.emplace_back("unknown_object.png");
@@ -62,6 +64,13 @@ std::string ObjectProperties::dump()
6264
os << ", nametag=" << nametag;
6365
os << ", nametag_color=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
6466
<< "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
67+
68+
if (nametag_bgcolor)
69+
os << ", nametag_bgcolor=" << "\"" << nametag_color.getAlpha() << "," << nametag_color.getRed()
70+
<< "," << nametag_color.getGreen() << "," << nametag_color.getBlue() << "\" ";
71+
else
72+
os << ", nametag_bgcolor=null ";
73+
6574
os << ", selectionbox=" << PP(selectionbox.MinEdge) << "," << PP(selectionbox.MaxEdge);
6675
os << ", pointable=" << pointable;
6776
os << ", static_save=" << static_save;
@@ -121,6 +130,13 @@ void ObjectProperties::serialize(std::ostream &os) const
121130
writeU8(os, shaded);
122131
writeU8(os, show_on_minimap);
123132

133+
if (!nametag_bgcolor)
134+
writeARGB8(os, NULL_BGCOLOR);
135+
else if (nametag_bgcolor.value().getAlpha() == 0)
136+
writeARGB8(os, video::SColor(0, 0, 0, 0));
137+
else
138+
writeARGB8(os, nametag_bgcolor.value());
139+
124140
// Add stuff only at the bottom.
125141
// Never remove anything, because we don't want new versions of this
126142
}
@@ -182,5 +198,11 @@ void ObjectProperties::deSerialize(std::istream &is)
182198
if (is.eof())
183199
return;
184200
show_on_minimap = tmp;
201+
202+
auto bgcolor = readARGB8(is);
203+
if (bgcolor != NULL_BGCOLOR)
204+
nametag_bgcolor = bgcolor;
205+
else
206+
nametag_bgcolor = nullopt;
185207
} catch (SerializationError &e) {}
186208
}

0 commit comments

Comments
 (0)