Cleanup: simplify function for adding node to editor
Split it into two functions, one for static node types, and another for ID names.
This commit is contained in:
parent
6b6d3b86d5
commit
2c00889d3e
|
@ -49,29 +49,48 @@ namespace blender::ed::space_node {
|
|||
/** \name Utilities
|
||||
* \{ */
|
||||
|
||||
bNode *node_add_node(const bContext &C, const char *idname, int type, float locx, float locy)
|
||||
static void position_node_based_on_mouse(bNode &node, const float2 &location)
|
||||
{
|
||||
node.locx = location.x - NODE_DY * 1.5f / UI_DPI_FAC;
|
||||
node.locy = location.y + NODE_DY * 0.5f / UI_DPI_FAC;
|
||||
}
|
||||
|
||||
bNode *add_node(const bContext &C, const StringRef idname, const float2 &location)
|
||||
{
|
||||
SpaceNode &snode = *CTX_wm_space_node(&C);
|
||||
Main &bmain = *CTX_data_main(&C);
|
||||
bNode *node = nullptr;
|
||||
|
||||
node_deselect_all(snode);
|
||||
|
||||
if (idname) {
|
||||
node = nodeAddNode(&C, snode.edittree, idname);
|
||||
}
|
||||
else {
|
||||
node = nodeAddStaticNode(&C, snode.edittree, type);
|
||||
}
|
||||
const std::string idname_str = idname;
|
||||
|
||||
bNode *node = nodeAddNode(&C, snode.edittree, idname_str.c_str());
|
||||
BLI_assert(node && node->typeinfo);
|
||||
|
||||
/* Position mouse in node header. */
|
||||
node->locx = locx - NODE_DY * 1.5f / UI_DPI_FAC;
|
||||
node->locy = locy + NODE_DY * 0.5f / UI_DPI_FAC;
|
||||
position_node_based_on_mouse(*node, location);
|
||||
|
||||
nodeSetSelected(node, true);
|
||||
|
||||
ED_node_set_active(&bmain, &snode, snode.edittree, node, nullptr);
|
||||
|
||||
ED_node_tree_propagate_change(&C, &bmain, snode.edittree);
|
||||
return node;
|
||||
}
|
||||
|
||||
bNode *add_static_node(const bContext &C, int type, const float2 &location)
|
||||
{
|
||||
SpaceNode &snode = *CTX_wm_space_node(&C);
|
||||
Main &bmain = *CTX_data_main(&C);
|
||||
|
||||
node_deselect_all(snode);
|
||||
|
||||
bNode *node = nodeAddStaticNode(&C, snode.edittree, type);
|
||||
BLI_assert(node && node->typeinfo);
|
||||
|
||||
position_node_based_on_mouse(*node, location);
|
||||
|
||||
nodeSetSelected(node, true);
|
||||
ED_node_set_active(&bmain, &snode, snode.edittree, node, nullptr);
|
||||
|
||||
ED_node_tree_propagate_change(&C, &bmain, snode.edittree);
|
||||
return node;
|
||||
}
|
||||
|
@ -338,9 +357,9 @@ static int node_add_group_exec(bContext *C, wmOperator *op)
|
|||
Main *bmain = CTX_data_main(C);
|
||||
SpaceNode *snode = CTX_wm_space_node(C);
|
||||
bNodeTree *ntree = snode->edittree;
|
||||
bNodeTree *node_group;
|
||||
|
||||
if (!(node_group = node_add_group_get_and_poll_group_node_tree(bmain, op, ntree))) {
|
||||
bNodeTree *node_group = node_add_group_get_and_poll_group_node_tree(bmain, op, ntree);
|
||||
if (!node_group) {
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
|
||||
|
@ -352,12 +371,7 @@ static int node_add_group_exec(bContext *C, wmOperator *op)
|
|||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
|
||||
bNode *group_node = node_add_node(*C,
|
||||
node_idname,
|
||||
(node_group->type == NTREE_CUSTOM) ? NODE_CUSTOM_GROUP :
|
||||
NODE_GROUP,
|
||||
snode->runtime->cursor[0],
|
||||
snode->runtime->cursor[1]);
|
||||
bNode *group_node = add_node(*C, node_idname, snode->runtime->cursor);
|
||||
if (!group_node) {
|
||||
BKE_report(op->reports, RPT_WARNING, "Could not add node group");
|
||||
return OPERATOR_CANCELLED;
|
||||
|
@ -445,8 +459,7 @@ static int node_add_object_exec(bContext *C, wmOperator *op)
|
|||
|
||||
ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
|
||||
|
||||
bNode *object_node = node_add_node(
|
||||
*C, nullptr, GEO_NODE_OBJECT_INFO, snode->runtime->cursor[0], snode->runtime->cursor[1]);
|
||||
bNode *object_node = add_static_node(*C, GEO_NODE_OBJECT_INFO, snode->runtime->cursor);
|
||||
if (!object_node) {
|
||||
BKE_report(op->reports, RPT_WARNING, "Could not add node object");
|
||||
return OPERATOR_CANCELLED;
|
||||
|
@ -522,7 +535,7 @@ static int node_add_collection_exec(bContext *C, wmOperator *op)
|
|||
{
|
||||
Main *bmain = CTX_data_main(C);
|
||||
SpaceNode &snode = *CTX_wm_space_node(C);
|
||||
bNodeTree *ntree = snode.edittree;
|
||||
bNodeTree &ntree = *snode.edittree;
|
||||
|
||||
Collection *collection = reinterpret_cast<Collection *>(
|
||||
WM_operator_properties_id_lookup_from_name_or_session_uuid(bmain, op->ptr, ID_GR));
|
||||
|
@ -533,8 +546,7 @@ static int node_add_collection_exec(bContext *C, wmOperator *op)
|
|||
|
||||
ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
|
||||
|
||||
bNode *collection_node = node_add_node(
|
||||
*C, nullptr, GEO_NODE_COLLECTION_INFO, snode.runtime->cursor[0], snode.runtime->cursor[1]);
|
||||
bNode *collection_node = add_static_node(*C, GEO_NODE_COLLECTION_INFO, snode.runtime->cursor);
|
||||
if (!collection_node) {
|
||||
BKE_report(op->reports, RPT_WARNING, "Could not add node collection");
|
||||
return OPERATOR_CANCELLED;
|
||||
|
@ -550,8 +562,8 @@ static int node_add_collection_exec(bContext *C, wmOperator *op)
|
|||
socket_data->value = collection;
|
||||
id_us_plus(&collection->id);
|
||||
|
||||
nodeSetActive(ntree, collection_node);
|
||||
ED_node_tree_propagate_change(C, bmain, ntree);
|
||||
nodeSetActive(&ntree, collection_node);
|
||||
ED_node_tree_propagate_change(C, bmain, &ntree);
|
||||
DEG_relations_tag_update(bmain);
|
||||
|
||||
return OPERATOR_FINISHED;
|
||||
|
@ -617,11 +629,9 @@ static int node_add_file_exec(bContext *C, wmOperator *op)
|
|||
{
|
||||
Main *bmain = CTX_data_main(C);
|
||||
SpaceNode &snode = *CTX_wm_space_node(C);
|
||||
bNode *node;
|
||||
Image *ima;
|
||||
int type = 0;
|
||||
|
||||
ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
|
||||
Image *ima = (Image *)WM_operator_drop_load_path(C, op, ID_IM);
|
||||
if (!ima) {
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
|
@ -645,7 +655,7 @@ static int node_add_file_exec(bContext *C, wmOperator *op)
|
|||
|
||||
ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
|
||||
|
||||
node = node_add_node(*C, nullptr, type, snode.runtime->cursor[0], snode.runtime->cursor[1]);
|
||||
bNode *node = add_static_node(*C, type, snode.runtime->cursor);
|
||||
|
||||
if (!node) {
|
||||
BKE_report(op->reports, RPT_WARNING, "Could not add an image node");
|
||||
|
@ -739,7 +749,6 @@ static int node_add_mask_exec(bContext *C, wmOperator *op)
|
|||
{
|
||||
Main *bmain = CTX_data_main(C);
|
||||
SpaceNode &snode = *CTX_wm_space_node(C);
|
||||
bNode *node;
|
||||
|
||||
ID *mask = WM_operator_properties_id_lookup_from_name_or_session_uuid(bmain, op->ptr, ID_MSK);
|
||||
if (!mask) {
|
||||
|
@ -748,8 +757,7 @@ static int node_add_mask_exec(bContext *C, wmOperator *op)
|
|||
|
||||
ED_preview_kill_jobs(CTX_wm_manager(C), CTX_data_main(C));
|
||||
|
||||
node = node_add_node(
|
||||
*C, nullptr, CMP_NODE_MASK, snode.runtime->cursor[0], snode.runtime->cursor[1]);
|
||||
bNode *node = add_static_node(*C, CMP_NODE_MASK, snode.runtime->cursor);
|
||||
|
||||
if (!node) {
|
||||
BKE_report(op->reports, RPT_WARNING, "Could not add a mask node");
|
||||
|
|
|
@ -245,12 +245,9 @@ void draw_nodespace_back_pix(const bContext &C,
|
|||
|
||||
/* node_add.cc */
|
||||
|
||||
/**
|
||||
* XXX Does some additional initialization on top of #nodeAddNode
|
||||
* Can be used with both custom and static nodes,
|
||||
* if `idname == nullptr` the static int type will be used instead.
|
||||
*/
|
||||
bNode *node_add_node(const bContext &C, const char *idname, int type, float locx, float locy);
|
||||
bNode *add_node(const bContext &C, StringRef idname, const float2 &location);
|
||||
bNode *add_static_node(const bContext &C, int type, const float2 &location);
|
||||
|
||||
void NODE_OT_add_reroute(wmOperatorType *ot);
|
||||
void NODE_OT_add_group(wmOperatorType *ot);
|
||||
void NODE_OT_add_object(wmOperatorType *ot);
|
||||
|
|
|
@ -639,8 +639,8 @@ static int link_socket_to_viewer(const bContext &C,
|
|||
if (viewer_bnode == nullptr) {
|
||||
/* Create a new viewer node if none exists. */
|
||||
const int viewer_type = get_default_viewer_type(&C);
|
||||
viewer_bnode = node_add_node(
|
||||
C, nullptr, viewer_type, bsocket_to_view.locx + 100, bsocket_to_view.locy);
|
||||
const float2 location{bsocket_to_view.locx + 100, bsocket_to_view.locy};
|
||||
viewer_bnode = add_static_node(C, viewer_type, location);
|
||||
if (viewer_bnode == nullptr) {
|
||||
return OPERATOR_CANCELLED;
|
||||
}
|
||||
|
@ -1654,7 +1654,7 @@ static int node_join_exec(bContext *C, wmOperator *UNUSED(op))
|
|||
SpaceNode &snode = *CTX_wm_space_node(C);
|
||||
bNodeTree &ntree = *snode.edittree;
|
||||
|
||||
/* XXX save selection: node_add_node call below sets the new frame as single
|
||||
/* XXX save selection: add_static_node call below sets the new frame as single
|
||||
* active+selected node */
|
||||
LISTBASE_FOREACH (bNode *, node, &ntree.nodes) {
|
||||
if (node->flag & NODE_SELECT) {
|
||||
|
@ -1665,7 +1665,7 @@ static int node_join_exec(bContext *C, wmOperator *UNUSED(op))
|
|||
}
|
||||
}
|
||||
|
||||
bNode *frame = node_add_node(*C, nullptr, NODE_FRAME, 0.0f, 0.0f);
|
||||
bNode *frame = add_static_node(*C, NODE_FRAME, float2(0));
|
||||
|
||||
/* reset tags */
|
||||
LISTBASE_FOREACH (bNode *, node, &ntree.nodes) {
|
||||
|
|
Loading…
Reference in New Issue