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