e754903efc565f11b19f158bee6e676dc6e03738
[blender-staging.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_material_types.h"
33 #include "DNA_texture_types.h"
34 #include "DNA_scene_types.h"
35 #include "DNA_screen_types.h"
36 #include "DNA_space_types.h"
37
38 #include "BKE_context.h"
39 #include "BKE_node.h"
40 #include "BKE_global.h"
41
42 #include "BLI_rect.h"
43
44 #include "ED_space_api.h"
45 #include "ED_screen.h"
46 #include "ED_types.h"
47
48 #include "RNA_access.h"
49 #include "RNA_define.h"
50
51 #include "WM_api.h"
52 #include "WM_types.h"
53
54 #include "UI_view2d.h"
55  
56 #include "node_intern.h"
57
58 /* **************** Node Header Buttons ************** */
59
60 static void node_hide_unhide_sockets(SpaceNode *snode, bNode *node)
61 {
62         bNodeSocket *sock;
63         
64         /* unhide all */
65         if( node_has_hidden_sockets(node) ) {
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         node_tree_verify_groups(snode->nodetree);
100 }
101
102 static int do_header_node(SpaceNode *snode, bNode *node, float mx, float my)
103 {
104         rctf totr= node->totr;
105         
106         totr.ymin= totr.ymax-20.0f;
107         
108         totr.xmax= totr.xmin+15.0f;
109         if(BLI_in_rctf(&totr, mx, my)) {
110                 node->flag |= NODE_HIDDEN;
111                 return 1;
112         }       
113         
114         totr.xmax= node->totr.xmax;
115         totr.xmin= totr.xmax-18.0f;
116         if(node->typeinfo->flag & NODE_PREVIEW) {
117                 if(BLI_in_rctf(&totr, mx, my)) {
118                         node->flag ^= NODE_PREVIEW;
119                         return 1;
120                 }
121                 totr.xmin-=18.0f;
122         }
123         if(node->type == NODE_GROUP) {
124                 if(BLI_in_rctf(&totr, mx, my)) {
125                         snode_make_group_editable(snode, node);
126                         return 1;
127                 }
128                 totr.xmin-=18.0f;
129         }
130         if(node->typeinfo->flag & NODE_OPTIONS) {
131                 if(BLI_in_rctf(&totr, mx, my)) {
132                         node->flag ^= NODE_OPTIONS;
133                         return 1;
134                 }
135                 totr.xmin-=18.0f;
136         }
137         /* hide unused sockets */
138         if(BLI_in_rctf(&totr, mx, my)) {
139                 node_hide_unhide_sockets(snode, node);
140         }
141         
142         return 0;
143 }
144
145 static int do_header_hidden_node(SpaceNode *snode, bNode *node, float mx, float my)
146 {
147         rctf totr= node->totr;
148         
149         totr.xmax= totr.xmin+15.0f;
150         if(BLI_in_rctf(&totr, mx, my)) {
151                 node->flag &= ~NODE_HIDDEN;
152                 return 1;
153         }       
154         return 0;
155 }
156
157 static int node_toggle_visibility(SpaceNode *snode, ARegion *ar, short *mval)
158 {
159         bNode *node;
160         float mx, my;
161         
162         mx= (float)mval[0];
163         my= (float)mval[1];
164         
165         UI_view2d_region_to_view(&ar->v2d, mval[0], mval[1], &mx, &my);
166         
167         for(next_node(snode->edittree); (node=next_node(NULL));) {
168                 if(node->flag & NODE_HIDDEN) {
169                         if(do_header_hidden_node(snode, node, mx, my)) {
170                                 ED_region_tag_redraw(ar);
171                                 return 1;
172                         }
173                 }
174                 else {
175                         if(do_header_node(snode, node, mx, my)) {
176                                 ED_region_tag_redraw(ar);
177                                 return 1;
178                         }
179                 }
180         }
181         return 0;
182 }
183
184 static int node_toggle_visibility_exec(bContext *C, wmOperator *op)
185 {
186         SpaceNode *snode= CTX_wm_space_node(C);
187         ARegion *ar= CTX_wm_region(C);
188         short mval[2];
189
190         mval[0] = RNA_int_get(op->ptr, "mouse_x");
191         mval[1] = RNA_int_get(op->ptr, "mouse_y");
192         if(node_toggle_visibility(snode, ar, mval))
193                 return OPERATOR_FINISHED;
194         else
195                 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
196 }
197
198 static int node_toggle_visibility_invoke(bContext *C, wmOperator *op, wmEvent *event)
199 {
200         ARegion *ar= CTX_wm_region(C);
201         short mval[2];  
202         
203         mval[0]= event->x - ar->winrct.xmin;
204         mval[1]= event->y - ar->winrct.ymin;
205         
206         RNA_int_set(op->ptr, "mouse_x", mval[0]);
207         RNA_int_set(op->ptr, "mouse_y", mval[1]);
208
209         return node_toggle_visibility_exec(C,op);
210 }
211
212 void NODE_OT_visibility_toggle(wmOperatorType *ot)
213 {
214         /* identifiers */
215         ot->name= "Toggle Visibility";
216         ot->idname= "NODE_OT_visibility_toggle";
217         ot->description= "Handle clicks on node header buttons";
218         
219         /* api callbacks */
220         ot->invoke= node_toggle_visibility_invoke;
221         ot->poll= ED_operator_node_active;
222         
223         /* flags */
224         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
225         
226         RNA_def_int(ot->srna, "mouse_x", 0, INT_MIN, INT_MAX, "Mouse X", "", INT_MIN, INT_MAX);
227         RNA_def_int(ot->srna, "mouse_y", 0, INT_MIN, INT_MAX, "Mouse Y", "", INT_MIN, INT_MAX);
228 }
229
230 /* **************** View All Operator ************** */
231
232 static void snode_home(ScrArea *sa, ARegion *ar, SpaceNode* snode)
233 {
234         bNode *node;
235         rctf *cur, *tot;
236         float oldwidth, oldheight, width, height;
237         int first= 1;
238         
239         cur= &ar->v2d.cur;
240         tot= &ar->v2d.tot;
241         
242         oldwidth= cur->xmax - cur->xmin;
243         oldheight= cur->ymax - cur->ymin;
244         
245         cur->xmin= cur->ymin= 0.0f;
246         cur->xmax=ar->winx;
247         cur->xmax= ar->winy;
248         
249         if(snode->edittree) {
250                 for(node= snode->edittree->nodes.first; node; node= node->next) {
251                         if(first) {
252                                 first= 0;
253                                 ar->v2d.cur= node->totr;
254                         }
255                         else {
256                                 BLI_union_rctf(cur, &node->totr);
257                         }
258                 }
259         }
260         
261         snode->xof= 0;
262         snode->yof= 0;
263         width= cur->xmax - cur->xmin;
264         height= cur->ymax- cur->ymin;
265         if(width > height) {
266                 float newheight;
267                 newheight= oldheight * width/oldwidth;
268                 cur->ymin= cur->ymin - newheight/4;
269                 cur->ymax= cur->ymin + newheight;
270         }
271         else {
272                 float newwidth;
273                 newwidth= oldwidth * height/oldheight;
274                 cur->xmin= cur->xmin - newwidth/4;
275                 cur->xmax= cur->xmin + newwidth;
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 *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         
300         /* api callbacks */
301         ot->exec= node_view_all_exec;
302         ot->poll= ED_operator_node_active;
303         
304         /* flags */
305         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
306 }