Bugfix #26101
[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 #include <stdio.h>
30
31 #include "DNA_node_types.h"
32 #include "DNA_scene_types.h"
33
34 #include "BLI_rect.h"
35 #include "BLI_utildefines.h"
36
37 #include "BKE_context.h"
38 #include "BKE_node.h"
39
40 #include "ED_screen.h"
41
42 #include "RNA_access.h"
43 #include "RNA_define.h"
44
45 #include "WM_api.h"
46 #include "WM_types.h"
47
48 #include "UI_view2d.h"
49  
50 #include "node_intern.h"
51
52 /* **************** Node Header Buttons ************** */
53
54 /* note: call node_tree_verify_groups(snode->nodetree) after this
55  */
56 void node_set_hidden_sockets(SpaceNode *snode, bNode *node, int set)
57 {       
58         bNodeSocket *sock;
59
60         if(set==0) {
61                 for(sock= node->inputs.first; sock; sock= sock->next)
62                         sock->flag &= ~SOCK_HIDDEN;
63                 for(sock= node->outputs.first; sock; sock= sock->next)
64                         sock->flag &= ~SOCK_HIDDEN;
65         }
66         else {
67                 bNode *gnode= node_tree_get_editgroup(snode->nodetree);
68
69                 /* hiding inside group should not break links in other group users */
70                 if(gnode) {
71                         nodeGroupSocketUseFlags((bNodeTree *)gnode->id);
72                         for(sock= node->inputs.first; sock; sock= sock->next)
73                                 if(!(sock->flag & SOCK_IN_USE))
74                                         if(sock->link==NULL)
75                                                 sock->flag |= SOCK_HIDDEN;
76                         for(sock= node->outputs.first; sock; sock= sock->next)
77                                 if(!(sock->flag & SOCK_IN_USE))
78                                         if(nodeCountSocketLinks(snode->edittree, sock)==0)
79                                                 sock->flag |= SOCK_HIDDEN;
80                 }
81                 else {
82                         /* hide unused sockets */
83                         for(sock= node->inputs.first; sock; sock= sock->next) {
84                                 if(sock->link==NULL)
85                                         sock->flag |= SOCK_HIDDEN;
86                         }
87                         for(sock= node->outputs.first; sock; sock= sock->next) {
88                                 if(nodeCountSocketLinks(snode->edittree, sock)==0)
89                                         sock->flag |= SOCK_HIDDEN;
90                         }
91                 }
92         }
93 }
94
95 static void node_hide_unhide_sockets(SpaceNode *snode, bNode *node)
96 {
97         node_set_hidden_sockets(snode, node, !node_has_hidden_sockets(node));
98         node_tree_verify_groups(snode->nodetree);
99 }
100
101 static int do_header_node(SpaceNode *snode, bNode *node, float mx, float my)
102 {
103         rctf totr= node->totr;
104         
105         totr.ymin= totr.ymax-20.0f;
106         
107         totr.xmax= totr.xmin+15.0f;
108         if(BLI_in_rctf(&totr, mx, my)) {
109                 node->flag |= NODE_HIDDEN;
110                 return 1;
111         }       
112         
113         totr.xmax= node->totr.xmax;
114         totr.xmin= totr.xmax-18.0f;
115         if(node->typeinfo->flag & NODE_PREVIEW) {
116                 if(BLI_in_rctf(&totr, mx, my)) {
117                         node->flag ^= NODE_PREVIEW;
118                         return 1;
119                 }
120                 totr.xmin-=15.0f;
121         }
122         if(node->type == NODE_GROUP) {
123                 if(BLI_in_rctf(&totr, mx, my)) {
124                         snode_make_group_editable(snode, node);
125                         return 1;
126                 }
127                 totr.xmin-=15.0f;
128         }
129         if(node->typeinfo->flag & NODE_OPTIONS) {
130                 if(BLI_in_rctf(&totr, mx, my)) {
131                         node->flag ^= NODE_OPTIONS;
132                         return 1;
133                 }
134                 totr.xmin-=15.0f;
135         }
136         /* hide unused sockets */
137         if(BLI_in_rctf(&totr, mx, my)) {
138                 node_hide_unhide_sockets(snode, node);
139         }
140         
141         return 0;
142 }
143
144 static int do_header_hidden_node(bNode *node, float mx, float my)
145 {
146         rctf totr= node->totr;
147         
148         totr.xmax= totr.xmin+15.0f;
149         if(BLI_in_rctf(&totr, mx, my)) {
150                 node->flag &= ~NODE_HIDDEN;
151                 return 1;
152         }       
153         return 0;
154 }
155
156 static int node_toggle_visibility(SpaceNode *snode, ARegion *ar, short *mval)
157 {
158         bNode *node;
159         float mx, my;
160         
161         mx= (float)mval[0];
162         my= (float)mval[1];
163         
164         UI_view2d_region_to_view(&ar->v2d, mval[0], mval[1], &mx, &my);
165         
166         for(next_node(snode->edittree); (node=next_node(NULL));) {
167                 if(node->flag & NODE_HIDDEN) {
168                         if(do_header_hidden_node(node, mx, my)) {
169                                 ED_region_tag_redraw(ar);
170                                 return 1;
171                         }
172                 }
173                 else {
174                         if(do_header_node(snode, node, mx, my)) {
175                                 ED_region_tag_redraw(ar);
176                                 return 1;
177                         }
178                 }
179         }
180         return 0;
181 }
182
183 static int node_toggle_visibility_exec(bContext *C, wmOperator *op)
184 {
185         SpaceNode *snode= CTX_wm_space_node(C);
186         ARegion *ar= CTX_wm_region(C);
187         short mval[2];
188
189         mval[0] = RNA_int_get(op->ptr, "mouse_x");
190         mval[1] = RNA_int_get(op->ptr, "mouse_y");
191         if(node_toggle_visibility(snode, ar, mval))
192                 return OPERATOR_FINISHED;
193         else
194                 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
195 }
196
197 static int node_toggle_visibility_invoke(bContext *C, wmOperator *op, wmEvent *event)
198 {
199         ARegion *ar= CTX_wm_region(C);
200         short mval[2];  
201         
202         mval[0]= event->x - ar->winrct.xmin;
203         mval[1]= event->y - ar->winrct.ymin;
204         
205         RNA_int_set(op->ptr, "mouse_x", mval[0]);
206         RNA_int_set(op->ptr, "mouse_y", mval[1]);
207
208         return node_toggle_visibility_exec(C,op);
209 }
210
211 void NODE_OT_visibility_toggle(wmOperatorType *ot)
212 {
213         /* identifiers */
214         ot->name= "Toggle Visibility";
215         ot->idname= "NODE_OT_visibility_toggle";
216         ot->description= "Handle clicks on node header buttons";
217         
218         /* api callbacks */
219         ot->invoke= node_toggle_visibility_invoke;
220         ot->poll= ED_operator_node_active;
221         
222         /* flags */
223         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
224         
225         RNA_def_int(ot->srna, "mouse_x", 0, INT_MIN, INT_MAX, "Mouse X", "", INT_MIN, INT_MAX);
226         RNA_def_int(ot->srna, "mouse_y", 0, INT_MIN, INT_MAX, "Mouse Y", "", INT_MIN, INT_MAX);
227 }
228
229 /* **************** View All Operator ************** */
230
231 static void snode_home(ScrArea *UNUSED(sa), ARegion *ar, SpaceNode* snode)
232 {
233         bNode *node;
234         rctf *cur, *tot;
235         float oldwidth, oldheight, width, height;
236         int first= 1;
237         
238         cur= &ar->v2d.cur;
239         tot= &ar->v2d.tot;
240         
241         oldwidth= cur->xmax - cur->xmin;
242         oldheight= cur->ymax - cur->ymin;
243         
244         cur->xmin= cur->ymin= 0.0f;
245         cur->xmax=ar->winx;
246         cur->ymax=ar->winy;
247         
248         if(snode->edittree) {
249                 for(node= snode->edittree->nodes.first; node; node= node->next) {
250                         if(first) {
251                                 first= 0;
252                                 ar->v2d.cur= node->totr;
253                         }
254                         else {
255                                 BLI_union_rctf(cur, &node->totr);
256                         }
257                 }
258         }
259         
260         snode->xof= 0;
261         snode->yof= 0;
262         width= cur->xmax - cur->xmin;
263         height= cur->ymax- cur->ymin;
264
265         if(width > height) {
266                 float newheight;
267                 newheight= oldheight * width/oldwidth;
268                 cur->ymin= cur->ymin - newheight/4;
269                 cur->ymax= cur->ymax + newheight/4;
270         }
271         else {
272                 float newwidth;
273                 newwidth= oldwidth * height/oldheight;
274                 cur->xmin= cur->xmin - newwidth/4;
275                 cur->xmax= cur->xmax + newwidth/4;
276         }
277
278         ar->v2d.tot= ar->v2d.cur;
279         UI_view2d_curRect_validate(&ar->v2d);
280 }
281
282 static int node_view_all_exec(bContext *C, wmOperator *UNUSED(op))
283 {
284         ScrArea *sa= CTX_wm_area(C);
285         ARegion *ar= CTX_wm_region(C);
286         SpaceNode *snode= CTX_wm_space_node(C);
287         
288         snode_home(sa, ar, snode);
289         ED_region_tag_redraw(ar);
290         
291         return OPERATOR_FINISHED;
292 }
293
294 void NODE_OT_view_all(wmOperatorType *ot)
295 {
296         /* identifiers */
297         ot->name= "View All";
298         ot->idname= "NODE_OT_view_all";
299         ot->description= "Resize view so you can see all nodes";
300         
301         /* api callbacks */
302         ot->exec= node_view_all_exec;
303         ot->poll= ED_operator_node_active;
304         
305         /* flags */
306         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
307 }