Bugfix #26317
[blender.git] / source / blender / editors / space_node / node_state.c
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version. 
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation, Nathan Letwory
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/space_node/node_state.c
30  *  \ingroup spnode
31  */
32
33
34 #include <stdio.h>
35
36 #include "DNA_node_types.h"
37 #include "DNA_scene_types.h"
38
39 #include "BLI_rect.h"
40 #include "BLI_utildefines.h"
41
42 #include "BKE_context.h"
43 #include "BKE_node.h"
44
45 #include "ED_screen.h"
46
47 #include "RNA_access.h"
48 #include "RNA_define.h"
49
50 #include "WM_api.h"
51 #include "WM_types.h"
52
53 #include "UI_view2d.h"
54  
55 #include "node_intern.h"
56
57 /* **************** Node Header Buttons ************** */
58
59 /* note: call node_tree_verify_groups(snode->nodetree) after this
60  */
61 void node_set_hidden_sockets(SpaceNode *snode, bNode *node, int set)
62 {       
63         bNodeSocket *sock;
64
65         if(set==0) {
66                 for(sock= node->inputs.first; sock; sock= sock->next)
67                         sock->flag &= ~SOCK_HIDDEN;
68                 for(sock= node->outputs.first; sock; sock= sock->next)
69                         sock->flag &= ~SOCK_HIDDEN;
70         }
71         else {
72                 bNode *gnode= node_tree_get_editgroup(snode->nodetree);
73
74                 /* hiding inside group should not break links in other group users */
75                 if(gnode) {
76                         nodeGroupSocketUseFlags((bNodeTree *)gnode->id);
77                         for(sock= node->inputs.first; sock; sock= sock->next)
78                                 if(!(sock->flag & SOCK_IN_USE))
79                                         if(sock->link==NULL)
80                                                 sock->flag |= SOCK_HIDDEN;
81                         for(sock= node->outputs.first; sock; sock= sock->next)
82                                 if(!(sock->flag & SOCK_IN_USE))
83                                         if(nodeCountSocketLinks(snode->edittree, sock)==0)
84                                                 sock->flag |= SOCK_HIDDEN;
85                 }
86                 else {
87                         /* hide unused sockets */
88                         for(sock= node->inputs.first; sock; sock= sock->next) {
89                                 if(sock->link==NULL)
90                                         sock->flag |= SOCK_HIDDEN;
91                         }
92                         for(sock= node->outputs.first; sock; sock= sock->next) {
93                                 if(nodeCountSocketLinks(snode->edittree, sock)==0)
94                                         sock->flag |= SOCK_HIDDEN;
95                         }
96                 }
97         }
98 }
99
100 static void node_hide_unhide_sockets(SpaceNode *snode, bNode *node)
101 {
102         node_set_hidden_sockets(snode, node, !node_has_hidden_sockets(node));
103         node_tree_verify_groups(snode->nodetree);
104 }
105
106 static int do_header_node(SpaceNode *snode, bNode *node, float mx, float my)
107 {
108         rctf totr= node->totr;
109         
110         totr.ymin= totr.ymax-20.0f;
111         
112         totr.xmax= totr.xmin+15.0f;
113         if(BLI_in_rctf(&totr, mx, my)) {
114                 node->flag |= NODE_HIDDEN;
115                 return 1;
116         }       
117         
118         totr.xmax= node->totr.xmax;
119         totr.xmin= totr.xmax-18.0f;
120         if(node->typeinfo->flag & NODE_PREVIEW) {
121                 if(BLI_in_rctf(&totr, mx, my)) {
122                         node->flag ^= NODE_PREVIEW;
123                         return 1;
124                 }
125                 totr.xmin-=15.0f;
126         }
127         if(node->type == NODE_GROUP) {
128                 if(BLI_in_rctf(&totr, mx, my)) {
129                         snode_make_group_editable(snode, node);
130                         return 1;
131                 }
132                 totr.xmin-=15.0f;
133         }
134         if(node->typeinfo->flag & NODE_OPTIONS) {
135                 if(BLI_in_rctf(&totr, mx, my)) {
136                         node->flag ^= NODE_OPTIONS;
137                         return 1;
138                 }
139                 totr.xmin-=15.0f;
140         }
141         /* hide unused sockets */
142         if(BLI_in_rctf(&totr, mx, my)) {
143                 node_hide_unhide_sockets(snode, node);
144         }
145         
146         return 0;
147 }
148
149 static int do_header_hidden_node(bNode *node, float mx, float my)
150 {
151         rctf totr= node->totr;
152         
153         totr.xmax= totr.xmin+15.0f;
154         if(BLI_in_rctf(&totr, mx, my)) {
155                 node->flag &= ~NODE_HIDDEN;
156                 return 1;
157         }       
158         return 0;
159 }
160
161 static int node_toggle_visibility(SpaceNode *snode, ARegion *ar, short *mval)
162 {
163         bNode *node;
164         float mx, my;
165         
166         mx= (float)mval[0];
167         my= (float)mval[1];
168         
169         UI_view2d_region_to_view(&ar->v2d, mval[0], mval[1], &mx, &my);
170         
171         for(next_node(snode->edittree); (node=next_node(NULL));) {
172                 if(node->flag & NODE_HIDDEN) {
173                         if(do_header_hidden_node(node, mx, my)) {
174                                 ED_region_tag_redraw(ar);
175                                 return 1;
176                         }
177                 }
178                 else {
179                         if(do_header_node(snode, node, mx, my)) {
180                                 ED_region_tag_redraw(ar);
181                                 return 1;
182                         }
183                 }
184         }
185         return 0;
186 }
187
188 static int node_toggle_visibility_exec(bContext *C, wmOperator *op)
189 {
190         SpaceNode *snode= CTX_wm_space_node(C);
191         ARegion *ar= CTX_wm_region(C);
192         short mval[2];
193
194         mval[0] = RNA_int_get(op->ptr, "mouse_x");
195         mval[1] = RNA_int_get(op->ptr, "mouse_y");
196         if(node_toggle_visibility(snode, ar, mval))
197                 return OPERATOR_FINISHED;
198         else
199                 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
200 }
201
202 static int node_toggle_visibility_invoke(bContext *C, wmOperator *op, wmEvent *event)
203 {
204         ARegion *ar= CTX_wm_region(C);
205         short mval[2];  
206         
207         mval[0]= event->x - ar->winrct.xmin;
208         mval[1]= event->y - ar->winrct.ymin;
209         
210         RNA_int_set(op->ptr, "mouse_x", mval[0]);
211         RNA_int_set(op->ptr, "mouse_y", mval[1]);
212
213         return node_toggle_visibility_exec(C,op);
214 }
215
216 void NODE_OT_visibility_toggle(wmOperatorType *ot)
217 {
218         /* identifiers */
219         ot->name= "Toggle Visibility";
220         ot->idname= "NODE_OT_visibility_toggle";
221         ot->description= "Handle clicks on node header buttons";
222         
223         /* api callbacks */
224         ot->invoke= node_toggle_visibility_invoke;
225         ot->poll= ED_operator_node_active;
226         
227         /* flags */
228         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
229         
230         RNA_def_int(ot->srna, "mouse_x", 0, INT_MIN, INT_MAX, "Mouse X", "", INT_MIN, INT_MAX);
231         RNA_def_int(ot->srna, "mouse_y", 0, INT_MIN, INT_MAX, "Mouse Y", "", INT_MIN, INT_MAX);
232 }
233
234 /* **************** View All Operator ************** */
235
236 static void snode_home(ScrArea *UNUSED(sa), ARegion *ar, SpaceNode* snode)
237 {
238         bNode *node;
239         rctf *cur, *tot;
240         float oldwidth, oldheight, width, height;
241         int first= 1;
242         
243         cur= &ar->v2d.cur;
244         tot= &ar->v2d.tot;
245         
246         oldwidth= cur->xmax - cur->xmin;
247         oldheight= cur->ymax - cur->ymin;
248         
249         cur->xmin= cur->ymin= 0.0f;
250         cur->xmax=ar->winx;
251         cur->ymax=ar->winy;
252         
253         if(snode->edittree) {
254                 for(node= snode->edittree->nodes.first; node; node= node->next) {
255                         if(first) {
256                                 first= 0;
257                                 ar->v2d.cur= node->totr;
258                         }
259                         else {
260                                 BLI_union_rctf(cur, &node->totr);
261                         }
262                 }
263         }
264         
265         snode->xof= 0;
266         snode->yof= 0;
267         width= cur->xmax - cur->xmin;
268         height= cur->ymax- cur->ymin;
269
270         if(width > height) {
271                 float newheight;
272                 newheight= oldheight * width/oldwidth;
273                 cur->ymin= cur->ymin - newheight/4;
274                 cur->ymax= cur->ymax + newheight/4;
275         }
276         else {
277                 float newwidth;
278                 newwidth= oldwidth * height/oldheight;
279                 cur->xmin= cur->xmin - newwidth/4;
280                 cur->xmax= cur->xmax + newwidth/4;
281         }
282
283         ar->v2d.tot= ar->v2d.cur;
284         UI_view2d_curRect_validate(&ar->v2d);
285 }
286
287 static int node_view_all_exec(bContext *C, wmOperator *UNUSED(op))
288 {
289         ScrArea *sa= CTX_wm_area(C);
290         ARegion *ar= CTX_wm_region(C);
291         SpaceNode *snode= CTX_wm_space_node(C);
292         
293         snode_home(sa, ar, snode);
294         ED_region_tag_redraw(ar);
295         
296         return OPERATOR_FINISHED;
297 }
298
299 void NODE_OT_view_all(wmOperatorType *ot)
300 {
301         /* identifiers */
302         ot->name= "View All";
303         ot->idname= "NODE_OT_view_all";
304         ot->description= "Resize view so you can see all nodes";
305         
306         /* api callbacks */
307         ot->exec= node_view_all_exec;
308         ot->poll= ED_operator_node_active;
309         
310         /* flags */
311         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
312 }