Cleanup: BKE: Nodes: improve node label functions

Slight cleanup of function naming, return types and related things like early return.

Pull Request: https://projects.blender.org/blender/blender/pulls/135351
This commit is contained in:
илья _
2025-03-05 11:17:50 +01:00
committed by Jacques Lucke
parent c2a5fb28d7
commit 02281dd26a
5 changed files with 31 additions and 38 deletions

View File

@@ -1082,18 +1082,18 @@ void node_preview_merge_tree(bNodeTree *to_ntree, bNodeTree *from_ntree, bool re
/** \name Node Type Access
* \{ */
void nodeLabel(const bNodeTree &ntree, const bNode &node, char *label, int label_maxncpy);
std::string node_label(const bNodeTree &ntree, const bNode &node);
/**
* Get node socket label if it is set.
*/
StringRefNull nodeSocketLabel(const bNodeSocket &sock);
StringRefNull node_socket_label(const bNodeSocket &sock);
/**
* Get node socket short label if it is set.
* It is used when grouping sockets under panels, to avoid redundancy in the label.
*/
std::optional<StringRefNull> nodeSocketShortLabel(const bNodeSocket &sock);
std::optional<StringRefNull> node_socket_short_label(const bNodeSocket &sock);
/**
* Initialize a new node type struct with default values and callbacks.

View File

@@ -4087,25 +4087,22 @@ void node_tree_update_all_users(Main *main, ID *id)
/* ************* node type access ********** */
void nodeLabel(const bNodeTree &ntree, const bNode &node, char *label, const int label_maxncpy)
std::string node_label(const bNodeTree &ntree, const bNode &node)
{
label[0] = '\0';
if (node.label[0] != '\0') {
BLI_strncpy(label, node.label, label_maxncpy);
}
else if (node.typeinfo->labelfunc) {
node.typeinfo->labelfunc(&ntree, &node, label, label_maxncpy);
}
if (label[0] != '\0') {
/* The previous methods (labelfunc) could not provide an adequate label for the node. */
return;
return node.label;
}
BLI_strncpy(label, IFACE_(node.typeinfo->ui_name.c_str()), label_maxncpy);
if (node.typeinfo->labelfunc) {
char label_buffer[MAX_NAME];
node.typeinfo->labelfunc(&ntree, &node, label_buffer, MAX_NAME);
return label_buffer;
}
return node.typeinfo->ui_name;
}
std::optional<StringRefNull> nodeSocketShortLabel(const bNodeSocket &sock)
std::optional<StringRefNull> node_socket_short_label(const bNodeSocket &sock)
{
if (sock.runtime->declaration != nullptr) {
StringRefNull short_label = sock.runtime->declaration->short_label;
@@ -4116,7 +4113,7 @@ std::optional<StringRefNull> nodeSocketShortLabel(const bNodeSocket &sock)
return std::nullopt;
}
StringRefNull nodeSocketLabel(const bNodeSocket &sock)
StringRefNull node_socket_label(const bNodeSocket &sock)
{
return (sock.label[0] != '\0') ? sock.label : sock.name;
}

View File

@@ -69,7 +69,7 @@ static void draw_node_input(bContext *C,
PointerRNA socket_ptr = RNA_pointer_create_discrete(
node_ptr->owner_id, &RNA_NodeSocket, &socket);
const StringRefNull text(IFACE_(bke::nodeSocketLabel(socket).c_str()));
const StringRefNull text(IFACE_(bke::node_socket_label(socket).c_str()));
uiLayout *row = uiLayoutRow(layout, true);
socket.typeinfo->draw(C, row, &socket_ptr, node_ptr, text);
}

View File

@@ -445,14 +445,14 @@ const char *node_socket_get_label(const bNodeSocket *socket, const char *panel_l
{
/* Get the short label if possible. This is used when grouping sockets under panels,
* to avoid redundancy in the label. */
const std::optional<StringRefNull> socket_short_label = bke::nodeSocketShortLabel(*socket);
const std::optional<StringRefNull> socket_short_label = bke::node_socket_short_label(*socket);
const char *socket_translation_context = node_socket_get_translation_context(*socket);
if (socket_short_label.has_value()) {
return CTX_IFACE_(socket_translation_context, socket_short_label->c_str());
}
const StringRefNull socket_label = bke::nodeSocketLabel(*socket);
const StringRefNull socket_label = bke::node_socket_label(*socket);
const char *translated_socket_label = CTX_IFACE_(socket_translation_context,
socket_label.c_str());
@@ -2128,15 +2128,13 @@ static std::string node_socket_get_tooltip(const SpaceNode *snode,
const bool is_extend = StringRef(socket.idname) == "NodeSocketVirtual";
const bNode &node = socket.owner_node();
if (node.is_reroute()) {
char reroute_name[MAX_NAME];
bke::nodeLabel(ntree, node, reroute_name, sizeof(reroute_name));
output << reroute_name;
output << bke::node_label(ntree, node);
}
else if (is_extend) {
output << TIP_("Connect a link to create a new socket");
}
else {
output << bke::nodeSocketLabel(socket);
output << bke::node_socket_label(socket);
}
if (ntree.type == NTREE_GEOMETRY && !is_extend) {
@@ -3590,8 +3588,7 @@ static void node_draw_basis(const bContext &C,
UI_block_emboss_set(&block, UI_EMBOSS);
}
char showname[128];
bke::nodeLabel(ntree, node, showname, sizeof(showname));
const std::string showname = bke::node_label(ntree, node);
uiBut *but = uiDefBut(&block,
UI_BTYPE_LABEL,
@@ -3842,8 +3839,7 @@ static void node_draw_hidden(const bContext &C,
UI_block_emboss_set(&block, UI_EMBOSS);
}
char showname[128];
bke::nodeLabel(ntree, node, showname, sizeof(showname));
const std::string showname = bke::node_label(ntree, node);
uiBut *but = uiDefBut(&block,
UI_BTYPE_LABEL,
@@ -4141,8 +4137,7 @@ static void frame_node_draw_label(TreeDrawContext &tree_draw_ctx,
const NodeFrame *data = (const NodeFrame *)node.storage;
const float font_size = data->label_size / aspect;
char label[MAX_NAME];
bke::nodeLabel(ntree, node, label, sizeof(label));
const std::string label = bke::node_label(ntree, node);
BLF_enable(fontid, BLF_ASPECT);
BLF_aspect(fontid, aspect, aspect, 1.0f);
@@ -4155,7 +4150,7 @@ static void frame_node_draw_label(TreeDrawContext &tree_draw_ctx,
BLF_color3ubv(fontid, color);
const float margin = NODE_FRAME_MARGIN;
const float width = BLF_width(fontid, label, sizeof(label));
const float width = BLF_width(fontid, label.c_str(), label.size());
const int label_height = frame_node_label_height(*data);
const rctf &rct = node.runtime->draw_bounds;
@@ -4166,7 +4161,7 @@ static void frame_node_draw_label(TreeDrawContext &tree_draw_ctx,
const bool has_label = node.label[0] != '\0';
if (has_label) {
BLF_position(fontid, label_x, label_y, 0);
BLF_draw(fontid, label, sizeof(label));
BLF_draw(fontid, label.c_str(), label.size());
}
/* Draw text body. */

View File

@@ -429,16 +429,17 @@ static void ui_node_sock_name(const bNodeTree *ntree,
{
if (sock->link && sock->link->fromnode) {
bNode *node = sock->link->fromnode;
char node_name[UI_MAX_NAME_STR];
bke::nodeLabel(*ntree, *node, node_name, sizeof(node_name));
const std::string node_name = bke::node_label(*ntree, *node);
if (BLI_listbase_is_empty(&node->inputs) && node->outputs.first != node->outputs.last) {
BLI_snprintf(
name, UI_MAX_NAME_STR, "%s | %s", IFACE_(node_name), IFACE_(sock->link->fromsock->name));
BLI_snprintf(name,
UI_MAX_NAME_STR,
"%s | %s",
IFACE_(node_name.c_str()),
IFACE_(sock->link->fromsock->name));
}
else {
BLI_strncpy_utf8(name, IFACE_(node_name), UI_MAX_NAME_STR);
BLI_strncpy_utf8(name, IFACE_(node_name.c_str()), UI_MAX_NAME_STR);
}
}
else if (sock->type == SOCK_SHADER) {