1
1

Cleanup: Use references and const in node editor

This commit is contained in:
2022-09-02 16:38:08 -05:00
parent aa08545a01
commit cd10fb4826
5 changed files with 102 additions and 98 deletions

View File

@@ -372,7 +372,7 @@ static void node_update_basis(const bContext &C, bNodeTree &ntree, bNode &node,
const char *socket_label = nodeSocketLabel(nsock);
nsock->typeinfo->draw((bContext *)&C, row, &sockptr, &nodeptr, IFACE_(socket_label));
node_socket_add_tooltip(&ntree, &node, nsock, row);
node_socket_add_tooltip(ntree, node, *nsock, *row);
UI_block_align_end(&block);
UI_block_layout_resolve(&block, nullptr, &buty);
@@ -504,7 +504,7 @@ static void node_update_basis(const bContext &C, bNodeTree &ntree, bNode &node,
const char *socket_label = nodeSocketLabel(nsock);
nsock->typeinfo->draw((bContext *)&C, row, &sockptr, &nodeptr, IFACE_(socket_label));
node_socket_add_tooltip(&ntree, &node, nsock, row);
node_socket_add_tooltip(ntree, node, *nsock, *row);
UI_block_align_end(&block);
UI_block_layout_resolve(&block, nullptr, &buty);
@@ -776,9 +776,9 @@ void node_socket_color_get(const bContext &C,
}
struct SocketTooltipData {
bNodeTree *ntree;
bNode *node;
bNodeSocket *socket;
const bNodeTree *ntree;
const bNode *node;
const bNodeSocket *socket;
};
static void create_inspection_string_for_generic_value(const GPointer value, std::stringstream &ss)
@@ -999,11 +999,11 @@ static void create_inspection_string_for_geometry(const geo_log::GeometryValueLo
}
}
static std::optional<std::string> create_socket_inspection_string(bContext *C,
bNode &node,
bNodeSocket &socket)
static std::optional<std::string> create_socket_inspection_string(const bContext &C,
const bNode &node,
const bNodeSocket &socket)
{
SpaceNode *snode = CTX_wm_space_node(C);
const SpaceNode *snode = CTX_wm_space_node(&C);
if (snode == nullptr) {
return {};
};
@@ -1038,41 +1038,41 @@ static std::optional<std::string> create_socket_inspection_string(bContext *C,
return ss.str();
}
static bool node_socket_has_tooltip(bNodeTree *ntree, bNodeSocket *socket)
static bool node_socket_has_tooltip(const bNodeTree &ntree, const bNodeSocket &socket)
{
if (ntree->type == NTREE_GEOMETRY) {
if (ntree.type == NTREE_GEOMETRY) {
return true;
}
if (socket->runtime->declaration != nullptr) {
const blender::nodes::SocketDeclaration &socket_decl = *socket->runtime->declaration;
if (socket.runtime->declaration != nullptr) {
const blender::nodes::SocketDeclaration &socket_decl = *socket.runtime->declaration;
return !socket_decl.description().is_empty();
}
return false;
}
static char *node_socket_get_tooltip(bContext *C,
bNodeTree *ntree,
bNode *node,
bNodeSocket *socket)
static char *node_socket_get_tooltip(const bContext &C,
const bNodeTree &ntree,
const bNode &node,
const bNodeSocket &socket)
{
std::stringstream output;
if (socket->runtime->declaration != nullptr) {
const blender::nodes::SocketDeclaration &socket_decl = *socket->runtime->declaration;
if (socket.runtime->declaration != nullptr) {
const blender::nodes::SocketDeclaration &socket_decl = *socket.runtime->declaration;
blender::StringRef description = socket_decl.description();
if (!description.is_empty()) {
output << TIP_(description.data());
}
}
if (ntree->type == NTREE_GEOMETRY) {
if (ntree.type == NTREE_GEOMETRY) {
if (!output.str().empty()) {
output << ".\n\n";
}
std::optional<std::string> socket_inspection_str = create_socket_inspection_string(
C, *node, *socket);
C, node, socket);
if (socket_inspection_str.has_value()) {
output << *socket_inspection_str;
}
@@ -1082,28 +1082,31 @@ static char *node_socket_get_tooltip(bContext *C,
}
if (output.str().empty()) {
output << nodeSocketLabel(socket);
output << nodeSocketLabel(&socket);
}
return BLI_strdup(output.str().c_str());
}
void node_socket_add_tooltip(bNodeTree *ntree, bNode *node, bNodeSocket *sock, uiLayout *layout)
void node_socket_add_tooltip(const bNodeTree &ntree,
const bNode &node,
const bNodeSocket &sock,
uiLayout &layout)
{
if (!node_socket_has_tooltip(ntree, sock)) {
return;
}
SocketTooltipData *data = MEM_cnew<SocketTooltipData>(__func__);
data->ntree = ntree;
data->node = node;
data->socket = sock;
SocketTooltipData *data = MEM_new<SocketTooltipData>(__func__);
data->ntree = &ntree;
data->node = &node;
data->socket = &sock;
uiLayoutSetTooltipFunc(
layout,
&layout,
[](bContext *C, void *argN, const char *UNUSED(tip)) {
SocketTooltipData *data = static_cast<SocketTooltipData *>(argN);
return node_socket_get_tooltip(C, data->ntree, data->node, data->socket);
const SocketTooltipData *data = static_cast<SocketTooltipData *>(argN);
return node_socket_get_tooltip(*C, *data->ntree, *data->node, *data->socket);
},
data,
MEM_dupallocN,
@@ -1141,7 +1144,7 @@ static void node_socket_draw_nested(const bContext &C,
size_id,
outline_col_id);
if (!node_socket_has_tooltip(&ntree, &sock)) {
if (!node_socket_has_tooltip(ntree, sock)) {
return;
}
@@ -1164,16 +1167,16 @@ static void node_socket_draw_nested(const bContext &C,
0,
nullptr);
SocketTooltipData *data = (SocketTooltipData *)MEM_mallocN(sizeof(SocketTooltipData), __func__);
SocketTooltipData *data = MEM_new<SocketTooltipData>(__func__);
data->ntree = &ntree;
data->node = (bNode *)node_ptr.data;
data->node = static_cast<const bNode *>(node_ptr.data);
data->socket = &sock;
UI_but_func_tooltip_set(
but,
[](bContext *C, void *argN, const char *UNUSED(tip)) {
SocketTooltipData *data = (SocketTooltipData *)argN;
return node_socket_get_tooltip(C, data->ntree, data->node, data->socket);
return node_socket_get_tooltip(*C, *data->ntree, *data->node, *data->socket);
},
data,
MEM_freeN);

View File

@@ -144,7 +144,10 @@ void node_socket_color_get(const bContext &C,
void node_draw_space(const bContext &C, ARegion &region);
void node_socket_add_tooltip(bNodeTree *ntree, bNode *node, bNodeSocket *sock, uiLayout *layout);
void node_socket_add_tooltip(const bNodeTree &ntree,
const bNode &node,
const bNodeSocket &sock,
uiLayout &layout);
/**
* Sort nodes by selection: unselected nodes first, then selected,
@@ -166,7 +169,7 @@ void node_keymap(wmKeyConfig *keyconf);
/* node_select.cc */
rctf node_frame_rect_inside(const bNode &node);
bool node_or_socket_isect_event(bContext *C, const wmEvent *event);
bool node_or_socket_isect_event(const bContext &C, const wmEvent &event);
void node_deselect_all(SpaceNode &snode);
void node_socket_select(bNode *node, bNodeSocket &sock);

View File

@@ -48,7 +48,7 @@
namespace blender::ed::space_node {
static bool is_event_over_node_or_socket(bContext *C, const wmEvent *event);
static bool is_event_over_node_or_socket(const bContext &C, const wmEvent &event);
/**
* Function to detect if there is a visible view3d that uses workbench in texture mode.
@@ -100,17 +100,17 @@ rctf node_frame_rect_inside(const bNode &node)
return frame_inside;
}
bool node_or_socket_isect_event(bContext *C, const wmEvent *event)
bool node_or_socket_isect_event(const bContext &C, const wmEvent &event)
{
return is_event_over_node_or_socket(C, event);
}
static bool node_frame_select_isect_mouse(bNode *node, const float2 &mouse)
static bool node_frame_select_isect_mouse(const bNode &node, const float2 &mouse)
{
/* Frame nodes are selectable by their borders (including their whole rect - as for other nodes -
* would prevent e.g. box selection of nodes inside that frame). */
const rctf frame_inside = node_frame_rect_inside(*node);
if (BLI_rctf_isect_pt(&node->totr, mouse.x, mouse.y) &&
const rctf frame_inside = node_frame_rect_inside(node);
if (BLI_rctf_isect_pt(&node.totr, mouse.x, mouse.y) &&
!BLI_rctf_isect_pt(&frame_inside, mouse.x, mouse.y)) {
return true;
}
@@ -118,19 +118,18 @@ static bool node_frame_select_isect_mouse(bNode *node, const float2 &mouse)
return false;
}
static bNode *node_under_mouse_select(bNodeTree &ntree, int mx, int my)
static bNode *node_under_mouse_select(bNodeTree &ntree, const float2 mouse)
{
LISTBASE_FOREACH_BACKWARD (bNode *, node, &ntree.nodes) {
switch (node->type) {
case NODE_FRAME: {
const float2 mouse{(float)mx, (float)my};
if (node_frame_select_isect_mouse(node, mouse)) {
if (node_frame_select_isect_mouse(*node, mouse)) {
return node;
}
break;
}
default: {
if (BLI_rctf_isect_pt(&node->totr, mx, my)) {
if (BLI_rctf_isect_pt(&node->totr, int(mouse.x), int(mouse.y))) {
return node;
}
break;
@@ -140,35 +139,35 @@ static bNode *node_under_mouse_select(bNodeTree &ntree, int mx, int my)
return nullptr;
}
static bNode *node_under_mouse_tweak(bNodeTree &ntree, const float2 &mouse)
static bool node_under_mouse_tweak(const bNodeTree &ntree, const float2 &mouse)
{
using namespace blender::math;
LISTBASE_FOREACH_BACKWARD (bNode *, node, &ntree.nodes) {
LISTBASE_FOREACH_BACKWARD (const bNode *, node, &ntree.nodes) {
switch (node->type) {
case NODE_REROUTE: {
bNodeSocket *socket = (bNodeSocket *)node->inputs.first;
const float2 location{socket->locx, socket->locy};
if (distance(mouse, location) < 24.0f) {
return node;
return true;
}
break;
}
case NODE_FRAME: {
if (node_frame_select_isect_mouse(node, mouse)) {
return node;
if (node_frame_select_isect_mouse(*node, mouse)) {
return true;
}
break;
}
default: {
if (BLI_rctf_isect_pt(&node->totr, mouse.x, mouse.y)) {
return node;
return true;
}
break;
}
}
}
return nullptr;
return false;
}
static bool is_position_over_node_or_socket(SpaceNode &snode, const float2 &mouse)
@@ -187,17 +186,17 @@ static bool is_position_over_node_or_socket(SpaceNode &snode, const float2 &mous
return false;
}
static bool is_event_over_node_or_socket(bContext *C, const wmEvent *event)
static bool is_event_over_node_or_socket(const bContext &C, const wmEvent &event)
{
SpaceNode *snode = CTX_wm_space_node(C);
ARegion *region = CTX_wm_region(C);
float2 mouse;
SpaceNode &snode = *CTX_wm_space_node(&C);
ARegion &region = *CTX_wm_region(&C);
int2 mval;
WM_event_drag_start_mval(event, region, mval);
WM_event_drag_start_mval(&event, &region, mval);
UI_view2d_region_to_view(&region->v2d, mval[0], mval[1], &mouse.x, &mouse.y);
return is_position_over_node_or_socket(*snode, mouse);
float2 mouse;
UI_view2d_region_to_view(&region.v2d, mval.x, mval.y, &mouse.x, &mouse.y);
return is_position_over_node_or_socket(snode, mouse);
}
void node_socket_select(bNode *node, bNodeSocket &sock)
@@ -526,7 +525,6 @@ static bool node_mouse_select(bContext *C,
bNode *node, *tnode;
bNodeSocket *sock = nullptr;
bNodeSocket *tsock;
float2 cursor;
/* always do socket_select when extending selection. */
const bool socket_select = (params->sel_op == SEL_OP_XOR) ||
@@ -536,6 +534,7 @@ static bool node_mouse_select(bContext *C,
bool node_was_selected = false;
/* get mouse coordinates in view2d space */
float2 cursor;
UI_view2d_region_to_view(&region.v2d, mval.x, mval.y, &cursor.x, &cursor.y);
/* first do socket selection, these generally overlap with nodes. */
@@ -593,7 +592,7 @@ static bool node_mouse_select(bContext *C,
if (!sock) {
/* find the closest visible node */
node = node_under_mouse_select(*snode.edittree, (int)cursor[0], (int)cursor[1]);
node = node_under_mouse_select(*snode.edittree, cursor);
found = (node != nullptr);
node_was_selected = node && (node->flag & SELECT);
@@ -787,7 +786,7 @@ static int node_box_select_invoke(bContext *C, wmOperator *op, const wmEvent *ev
{
const bool tweak = RNA_boolean_get(op->ptr, "tweak");
if (tweak && is_event_over_node_or_socket(C, event)) {
if (tweak && is_event_over_node_or_socket(*C, *event)) {
return OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH;
}
@@ -916,7 +915,7 @@ static int node_lasso_select_invoke(bContext *C, wmOperator *op, const wmEvent *
{
const bool tweak = RNA_boolean_get(op->ptr, "tweak");
if (tweak && is_event_over_node_or_socket(C, event)) {
if (tweak && is_event_over_node_or_socket(*C, *event)) {
return OPERATOR_CANCELLED | OPERATOR_PASS_THROUGH;
}

View File

@@ -758,43 +758,42 @@ namespace blender::ed::space_node {
/**************************** Node Tree Layout *******************************/
static void ui_node_draw_input(
uiLayout *layout, bContext *C, bNodeTree *ntree, bNode *node, bNodeSocket *input, int depth);
uiLayout &layout, bContext &C, bNodeTree &ntree, bNode &node, bNodeSocket &input, int depth);
static void ui_node_draw_node(
uiLayout *layout, bContext *C, bNodeTree *ntree, bNode *node, int depth)
uiLayout &layout, bContext &C, bNodeTree &ntree, bNode &node, int depth)
{
bNodeSocket *input;
PointerRNA nodeptr;
RNA_pointer_create(&ntree->id, &RNA_Node, node, &nodeptr);
RNA_pointer_create(&ntree.id, &RNA_Node, &node, &nodeptr);
if (node->typeinfo->draw_buttons) {
if (node->type != NODE_GROUP) {
uiLayoutSetPropSep(layout, true);
node->typeinfo->draw_buttons(layout, C, &nodeptr);
if (node.typeinfo->draw_buttons) {
if (node.type != NODE_GROUP) {
uiLayoutSetPropSep(&layout, true);
node.typeinfo->draw_buttons(&layout, &C, &nodeptr);
}
}
for (input = (bNodeSocket *)node->inputs.first; input; input = input->next) {
ui_node_draw_input(layout, C, ntree, node, input, depth + 1);
LISTBASE_FOREACH (bNodeSocket *, input, &node.inputs) {
ui_node_draw_input(layout, C, ntree, node, *input, depth + 1);
}
}
static void ui_node_draw_input(
uiLayout *layout, bContext *C, bNodeTree *ntree, bNode *node, bNodeSocket *input, int depth)
uiLayout &layout, bContext &C, bNodeTree &ntree, bNode &node, bNodeSocket &input, int depth)
{
PointerRNA inputptr, nodeptr;
uiBlock *block = uiLayoutGetBlock(layout);
uiBlock *block = uiLayoutGetBlock(&layout);
uiLayout *row = nullptr;
bool dependency_loop;
if (input->flag & SOCK_UNAVAIL) {
if (input.flag & SOCK_UNAVAIL) {
return;
}
/* to avoid eternal loops on cyclic dependencies */
node->flag |= NODE_TEST;
bNode *lnode = (input->link) ? input->link->fromnode : nullptr;
node.flag |= NODE_TEST;
bNode *lnode = (input.link) ? input.link->fromnode : nullptr;
dependency_loop = (lnode && (lnode->flag & NODE_TEST));
if (dependency_loop) {
@@ -802,10 +801,10 @@ static void ui_node_draw_input(
}
/* socket RNA pointer */
RNA_pointer_create(&ntree->id, &RNA_NodeSocket, input, &inputptr);
RNA_pointer_create(&ntree->id, &RNA_Node, node, &nodeptr);
RNA_pointer_create(&ntree.id, &RNA_NodeSocket, &input, &inputptr);
RNA_pointer_create(&ntree.id, &RNA_Node, &node, &nodeptr);
row = uiLayoutRow(layout, true);
row = uiLayoutRow(&layout, true);
/* Decorations are added manually here. */
uiLayoutSetPropDecorate(row, false);
@@ -821,8 +820,8 @@ static void ui_node_draw_input(
if (lnode &&
(lnode->inputs.first || (lnode->typeinfo->draw_buttons && lnode->type != NODE_GROUP))) {
int icon = (input->flag & SOCK_COLLAPSED) ? ICON_DISCLOSURE_TRI_RIGHT :
ICON_DISCLOSURE_TRI_DOWN;
int icon = (input.flag & SOCK_COLLAPSED) ? ICON_DISCLOSURE_TRI_RIGHT :
ICON_DISCLOSURE_TRI_DOWN;
uiItemR(sub, &inputptr, "show_expanded", UI_ITEM_R_ICON_ONLY, "", icon);
}
@@ -831,7 +830,7 @@ static void ui_node_draw_input(
sub = uiLayoutRow(sub, true);
uiLayoutSetAlignment(sub, UI_LAYOUT_ALIGN_RIGHT);
uiItemL(sub, IFACE_(input->name), ICON_NONE);
uiItemL(sub, IFACE_(input.name), ICON_NONE);
}
if (dependency_loop) {
@@ -840,28 +839,28 @@ static void ui_node_draw_input(
}
else if (lnode) {
/* input linked to a node */
uiTemplateNodeLink(row, C, ntree, node, input);
uiTemplateNodeLink(row, &C, &ntree, &node, &input);
add_dummy_decorator = true;
if (depth == 0 || !(input->flag & SOCK_COLLAPSED)) {
if (depth == 0 || !(input.flag & SOCK_COLLAPSED)) {
if (depth == 0) {
uiItemS(layout);
uiItemS(&layout);
}
ui_node_draw_node(layout, C, ntree, lnode, depth);
ui_node_draw_node(layout, C, ntree, *lnode, depth);
}
}
else {
uiLayout *sub = uiLayoutRow(row, true);
uiTemplateNodeLink(sub, C, ntree, node, input);
uiTemplateNodeLink(sub, &C, &ntree, &node, &input);
if (input->flag & SOCK_HIDE_VALUE) {
if (input.flag & SOCK_HIDE_VALUE) {
add_dummy_decorator = true;
}
/* input not linked, show value */
else {
switch (input->type) {
switch (input.type) {
case SOCK_VECTOR:
uiItemS(sub);
sub = uiLayoutColumn(sub, true);
@@ -876,11 +875,11 @@ static void ui_node_draw_input(
break;
case SOCK_STRING: {
const bNodeTree *node_tree = (const bNodeTree *)nodeptr.owner_id;
SpaceNode *snode = CTX_wm_space_node(C);
SpaceNode *snode = CTX_wm_space_node(&C);
if (node_tree->type == NTREE_GEOMETRY && snode != nullptr) {
/* Only add the attribute search in the node editor, in other places there is not
* enough context. */
node_geometry_add_attribute_search_button(*C, *node, inputptr, *sub);
node_geometry_add_attribute_search_button(C, node, inputptr, *sub);
}
else {
uiItemR(sub, &inputptr, "default_value", 0, "", ICON_NONE);
@@ -899,10 +898,10 @@ static void ui_node_draw_input(
uiItemDecoratorR(split_wrapper.decorate_column, nullptr, nullptr, 0);
}
node_socket_add_tooltip(ntree, node, input, row);
node_socket_add_tooltip(ntree, node, input, *row);
/* clear */
node->flag &= ~NODE_TEST;
node.flag &= ~NODE_TEST;
}
} // namespace blender::ed::space_node
@@ -924,9 +923,9 @@ void uiTemplateNodeView(
}
if (input) {
ui_node_draw_input(layout, C, ntree, node, input, 0);
ui_node_draw_input(*layout, *C, *ntree, *node, *input, 0);
}
else {
ui_node_draw_node(layout, C, ntree, node, 0);
ui_node_draw_node(*layout, *C, *ntree, *node, 0);
}
}

View File

@@ -645,7 +645,7 @@ static int sample_invoke(bContext *C, wmOperator *op, const wmEvent *event)
/* Don't handle events intended for nodes (which rely on click/drag distinction).
* which this operator would use since sampling is normally activated on press, see: T98191. */
if (node_or_socket_isect_event(C, event)) {
if (node_or_socket_isect_event(*C, *event)) {
return OPERATOR_PASS_THROUGH;
}