fix for node editor re-rendering when the background image was moved in the view.
[blender.git] / source / blender / editors / space_node / node_edit.c
index 47005dee63b4a93421bcf5de15cf50e0659386bf..6fc33a8baad5ef7b6ea5500714f4f0c800ccc045 100644 (file)
@@ -1157,6 +1157,9 @@ static int node_group_ungroup(bNodeTree *ntree, bNode *gnode)
                BLI_remlink(&wgroup->nodes, node);
                BLI_addtail(&ntree->nodes, node);
                
+               /* ensure unique node name in the nodee tree */
+               nodeUniqueName(ntree, node);
+               
                node->locx += gnode->locx;
                node->locy += gnode->locy;
                
@@ -1197,7 +1200,8 @@ static int node_group_ungroup(bNodeTree *ntree, bNode *gnode)
                        nodeRemLink(wgroup, link);
        }
        /* restore links from internal nodes */
-       for (link = wgroup->links.first; link; link = link->next) {
+       for (link = wgroup->links.first; link; link = linkn) {
+               linkn = link->next;
                /* indicates link to group input */
                if (!link->fromnode) {
                        /* NB: can't use find_group_node_input here,
@@ -1370,6 +1374,9 @@ static int node_group_separate_selected(bNodeTree *ntree, bNode *gnode, int make
                BLI_remlink(&ngroup->nodes, newnode);
                BLI_addtail(&ntree->nodes, newnode);
                
+               /* ensure unique node name in the node tree */
+               nodeUniqueName(ntree, newnode);
+               
                newnode->locx += gnode->locx;
                newnode->locy += gnode->locy;
        }
@@ -1514,7 +1521,7 @@ static bNode *visible_node(SpaceNode *snode, rctf *rct)
        bNode *node;
        
        for (node = snode->edittree->nodes.last; node; node = node->prev) {
-               if (BLI_isect_rctf(&node->totr, rct, NULL))
+               if (BLI_rctf_isect(&node->totr, rct, NULL))
                        break;
        }
        return node;
@@ -1555,8 +1562,6 @@ static int snode_bg_viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
                        
                        MEM_freeN(nvm);
                        op->customdata = NULL;
-
-                       WM_event_add_notifier(C, NC_SPACE | ND_SPACE_NODE, NULL);
                        
                        return OPERATOR_FINISHED;
        }
@@ -1566,11 +1571,13 @@ static int snode_bg_viewmove_modal(bContext *C, wmOperator *op, wmEvent *event)
 
 static int snode_bg_viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
 {
+       SpaceNode *snode = CTX_wm_space_node(C);
        ARegion *ar = CTX_wm_region(C);
        NodeViewMove *nvm;
        Image *ima;
        ImBuf *ibuf;
-       int pad = 10;
+       const float pad = 32.0f; /* better be bigger then scrollbars */
+
        void *lock;
        
        ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
@@ -1586,10 +1593,10 @@ static int snode_bg_viewmove_invoke(bContext *C, wmOperator *op, wmEvent *event)
        nvm->mvalo[0] = event->mval[0];
        nvm->mvalo[1] = event->mval[1];
 
-       nvm->xmin = -(ar->winx / 2) - ibuf->x / 2 + pad;
-       nvm->xmax = ar->winx / 2 + ibuf->x / 2 - pad;
-       nvm->ymin = -(ar->winy / 2) - ibuf->y / 2 + pad;
-       nvm->ymax = ar->winy / 2 + ibuf->y / 2 - pad;
+       nvm->xmin = -(ar->winx / 2) - (ibuf->x * (0.5f * snode->zoom)) + pad;
+       nvm->xmax =  (ar->winx / 2) + (ibuf->x * (0.5f * snode->zoom)) - pad;
+       nvm->ymin = -(ar->winy / 2) - (ibuf->y * (0.5f * snode->zoom)) + pad;
+       nvm->ymax =  (ar->winy / 2) + (ibuf->y * (0.5f * snode->zoom)) - pad;
 
        BKE_image_release_ibuf(ima, lock);
        
@@ -1643,7 +1650,7 @@ void NODE_OT_backimage_zoom(wmOperatorType *ot)
        /* identifiers */
        ot->name = "Background Image Zoom";
        ot->idname = "NODE_OT_backimage_zoom";
-       ot->description = "Zoom in/out the brackground image";
+       ot->description = "Zoom in/out the background image";
        
        /* api callbacks */
        ot->exec = backimage_zoom;
@@ -1676,10 +1683,12 @@ static void sample_draw(const bContext *C, ARegion *ar, void *arg_info)
        Scene *scene = CTX_data_scene(C);
        ImageSampleInfo *info = arg_info;
 
-       ED_image_draw_info(ar, (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT), info->channels,
-                          info->x, info->y, info->col, info->colf,
-                          NULL, NULL /* zbuf - unused for nodes */
-                          );
+       if (info->draw) {
+               ED_image_draw_info(ar, (scene->r.color_mgt_flag & R_COLOR_MANAGEMENT), info->channels,
+                                  info->x, info->y, info->col, info->colf,
+                                  NULL, NULL /* zbuf - unused for nodes */
+                                  );
+       }
 }
 
 static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
@@ -1694,8 +1703,10 @@ static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
        
        ima = BKE_image_verify_viewer(IMA_TYPE_COMPOSITE, "Viewer Node");
        ibuf = BKE_image_acquire_ibuf(ima, NULL, &lock);
-       if (!ibuf)
+       if (!ibuf) {
+               info->draw = 0;
                return;
+       }
        
        if (!ibuf->rect) {
                if (info->color_manage)
@@ -1745,9 +1756,13 @@ static void sample_apply(bContext *C, wmOperator *op, wmEvent *event)
                        info->colf[2] = fp[2];
                        info->colf[3] = fp[3];
                }
+
+               ED_node_sample_set(info->colf);
        }
-       else
+       else {
                info->draw = 0;
+               ED_node_sample_set(NULL);
+       }
 
        BKE_image_release_ibuf(ima, lock);
        
@@ -1758,6 +1773,7 @@ static void sample_exit(bContext *C, wmOperator *op)
 {
        ImageSampleInfo *info = op->customdata;
 
+       ED_node_sample_set(NULL);
        ED_region_draw_cb_exit(info->art, info->draw_handle);
        ED_area_tag_redraw(CTX_wm_area(C));
        MEM_freeN(info);
@@ -2531,14 +2547,16 @@ bNode *node_add_node(SpaceNode *snode, Main *bmain, Scene *scene, bNodeTemplate
        
        /* generics */
        if (node) {
-               node->locx = locx;
-               node->locy = locy + 60.0f;       // arbitrary.. so its visible, (0,0) is top of node
                node_select(node);
                
                gnode = node_tree_get_editgroup(snode->nodetree);
+               // arbitrary y offset of 60 so its visible
                if (gnode) {
-                       node->locx -= gnode->locx;
-                       node->locy -= gnode->locy;
+                       nodeFromView(gnode, locx, locy + 60.0f, &node->locx, &node->locy);
+               }
+               else {
+                       node->locx = locx;
+                       node->locy = locy + 60.0f;
                }
 
                ntreeUpdateTree(snode->edittree);
@@ -3124,6 +3142,7 @@ static int add_reroute_exec(bContext *C, wmOperator *op)
 {
        SpaceNode *snode = CTX_wm_space_node(C);
        ARegion *ar = CTX_wm_region(C);
+       bNode *gnode = node_tree_get_editgroup(snode->nodetree);
        float mcoords[256][2];
        int i = 0;
 
@@ -3154,8 +3173,13 @@ static int add_reroute_exec(bContext *C, wmOperator *op)
                                
                                ntemp.type = NODE_REROUTE;
                                rerouteNode = nodeAddNode(snode->edittree, &ntemp);
-                               rerouteNode->locx = insertPoint[0];
-                               rerouteNode->locy = insertPoint[1];
+                               if (gnode) {
+                                       nodeFromView(gnode, insertPoint[0], insertPoint[1], &rerouteNode->locx, &rerouteNode->locy);
+                               }
+                               else {
+                                       rerouteNode->locx = insertPoint[0];
+                                       rerouteNode->locy = insertPoint[1];
+                               }
                                
                                nodeAddLink(snode->edittree, link->fromnode, link->fromsock, rerouteNode, rerouteNode->inputs.first);
                                link->fromnode = rerouteNode;
@@ -3701,11 +3725,14 @@ static int node_group_make_insert_selected(bNodeTree *ntree, bNode *gnode)
                        BLI_remlink(&ntree->nodes, node);
                        BLI_addtail(&ngroup->nodes, node);
                        
+                       /* ensure unique node name in the ngroup */
+                       nodeUniqueName(ngroup, node);
+                       
                        node->locx -= 0.5f * (min[0] + max[0]);
                        node->locy -= 0.5f * (min[1] + max[1]);
                }
        }
-
+       
        /* move animation data over */
        if (ntree->adt) {
                LinkData *ld, *ldn = NULL;
@@ -3965,9 +3992,9 @@ static int node_hide_toggle_exec(bContext *C, wmOperator *UNUSED(op))
                return OPERATOR_CANCELLED;
        
        node_flag_toggle_exec(snode, NODE_HIDDEN);
-       
-       snode_notify(C, snode);
-       
+
+       WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
+
        return OPERATOR_FINISHED;
 }
 
@@ -4028,7 +4055,7 @@ static int node_options_toggle_exec(bContext *C, wmOperator *UNUSED(op))
 
        node_flag_toggle_exec(snode, NODE_OPTIONS);
 
-       snode_notify(C, snode);
+       WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
 
        return OPERATOR_FINISHED;
 }
@@ -4079,7 +4106,7 @@ static int node_socket_toggle_exec(bContext *C, wmOperator *UNUSED(op))
 
        ntreeUpdateTree(snode->edittree);
 
-       snode_notify(C, snode);
+       WM_event_add_notifier(C, NC_NODE | ND_DISPLAY, NULL);
 
        return OPERATOR_FINISHED;
 }