COLLADA: merge from trunk -r 24390:24522.
[blender.git] / source / blender / editors / space_node / node_select.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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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 #include "BKE_utildefines.h"
42
43 #include "BLI_rect.h"
44
45 #include "ED_space_api.h"
46 #include "ED_screen.h"
47 #include "ED_types.h"
48
49 #include "RNA_access.h"
50 #include "RNA_define.h"
51
52 #include "WM_api.h"
53 #include "WM_types.h"
54
55 #include "UI_view2d.h"
56  
57 #include "node_intern.h"
58  
59 static bNode *node_mouse_select(SpaceNode *snode, ARegion *ar, short *mval, short extend)
60 {
61         bNode *node;
62         float mx, my;
63         
64         mx= (float)mval[0];
65         my= (float)mval[1];
66         
67         UI_view2d_region_to_view(&ar->v2d, mval[0], mval[1], &mx, &my);
68         
69         for(next_node(snode->edittree); (node=next_node(NULL));) {
70                 
71                 /* first check for the headers or scaling widget */
72                 /* XXX if(node->flag & NODE_HIDDEN) {
73                         if(do_header_hidden_node(snode, node, mx, my))
74                                 return 1;
75                 }
76                 else {
77                         if(do_header_node(snode, node, mx, my))
78                                 return 1;
79                 }*/
80                 
81                 /* node body */
82                 if(BLI_in_rctf(&node->totr, mx, my))
83                         break;
84         }
85         if(node) {
86                 if((extend & KM_SHIFT)==0)
87                         node_deselectall(snode);
88                 
89                 if(extend & KM_SHIFT) {
90                         if(node->flag & SELECT)
91                                 node->flag &= ~SELECT;
92                         else
93                                 node->flag |= SELECT;
94                 }
95                 else
96                         node->flag |= SELECT;
97                 
98                 node_set_active(snode, node);
99                 
100                 /* viewer linking */
101                 if(extend & KM_CTRL)
102                         ;//     node_link_viewer(snode, node);
103                 
104                 //std_rmouse_transform(node_transform_ext);     /* does undo push for select */
105
106         }
107
108         return node;
109 }
110
111 static int node_select_exec(bContext *C, wmOperator *op)
112 {
113         // XXX wmWindow *window=  CTX_wm_window(C);
114         SpaceNode *snode= CTX_wm_space_node(C);
115         ARegion *ar= CTX_wm_region(C);
116         int select_type;
117         short mval[2];
118         short extend;
119         bNode *node= NULL;
120
121         select_type = RNA_enum_get(op->ptr, "select_type");
122         
123         switch (select_type) {
124                 case NODE_SELECT_MOUSE:
125                         mval[0] = RNA_int_get(op->ptr, "mouse_x");
126                         mval[1] = RNA_int_get(op->ptr, "mouse_y");
127                         extend = RNA_boolean_get(op->ptr, "extend");
128                         node= node_mouse_select(snode, ar, mval, extend);
129                         break;
130         }
131
132         /* need refresh/a notifier vs compo notifier */
133         // XXX WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL); /* Do we need to pass the scene? */
134
135         /* WATCH THIS, there are a few other ways to change the active material */
136         if(node) {
137                 if (node->id && GS(node->id->name)== ID_MA) {
138                         WM_event_add_notifier(C, NC_MATERIAL|ND_SHADING_DRAW, node->id);
139                 }
140         }
141
142         ED_region_tag_redraw(ar);
143     
144     /* send notifiers */
145         WM_event_add_notifier(C, NC_NODE|ND_NODE_SELECT, NULL);
146         
147         /* allow tweak event to work too */
148         return OPERATOR_FINISHED|OPERATOR_PASS_THROUGH;
149 }
150
151 static int node_select_modal(bContext *C, wmOperator *op, wmEvent *event)
152 {
153         /* execute the events */
154         switch (event->type) {
155                 case MOUSEMOVE:
156                         printf("%d %d\n", event->x, event->y);
157                         break;
158                 case SELECTMOUSE:
159                         //if (event->val==KM_RELEASE) {
160                                 /* calculate overall delta mouse-movement for redo */
161                                 printf("done translating\n");
162                                 //WM_cursor_restore(CTX_wm_window(C));
163                                 
164                                 return OPERATOR_FINISHED;
165                         //}
166                         break;
167         }
168
169         return OPERATOR_RUNNING_MODAL;
170 }
171
172 static int node_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
173 {
174         ARegion *ar= CTX_wm_region(C);
175         short mval[2];  
176         
177         mval[0]= event->x - ar->winrct.xmin;
178         mval[1]= event->y - ar->winrct.ymin;
179         
180         RNA_int_set(op->ptr, "mouse_x", mval[0]);
181         RNA_int_set(op->ptr, "mouse_y", mval[1]);
182
183         return node_select_exec(C,op);
184 }
185
186 static int node_extend_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
187 {
188         RNA_boolean_set(op->ptr, "extend", KM_SHIFT);
189
190         return node_select_invoke(C, op, event);
191 }
192
193 /* operators */
194
195 static EnumPropertyItem prop_select_items[] = {
196         {NODE_SELECT_MOUSE, "NORMAL", 0, "Normal Select", "Select using the mouse"},
197         {0, NULL, 0, NULL, NULL}};
198
199 void NODE_OT_select_extend(wmOperatorType *ot)
200 {
201         // XXX - Todo - This should just be a toggle option for NODE_OT_select not its own op
202         /* identifiers */
203         ot->name= "Activate/Select (Shift)";
204         ot->idname= "NODE_OT_select_extend";
205         
206         /* api callbacks */
207         ot->invoke= node_extend_select_invoke;
208         ot->poll= ED_operator_node_active;
209         
210         /* flags */
211         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
212         
213         RNA_def_enum(ot->srna, "select_type", prop_select_items, 0, "Select Type", "");
214         
215         RNA_def_int(ot->srna, "mouse_x", 0, INT_MIN, INT_MAX, "Mouse X", "", INT_MIN, INT_MAX);
216         RNA_def_int(ot->srna, "mouse_y", 0, INT_MIN, INT_MAX, "Mouse Y", "", INT_MIN, INT_MAX);
217         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "");
218 }
219
220 void NODE_OT_select(wmOperatorType *ot)
221 {
222         /* identifiers */
223         ot->name= "Activate/Select";
224         ot->idname= "NODE_OT_select";
225         
226         /* api callbacks */
227         ot->invoke= node_select_invoke;
228         ot->poll= ED_operator_node_active;
229         ot->modal= node_select_modal;
230         
231         /* flags */
232         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
233         
234         RNA_def_enum(ot->srna, "select_type", prop_select_items, 0, "Select Type", "");
235         
236         RNA_def_int(ot->srna, "mouse_x", 0, INT_MIN, INT_MAX, "Mouse X", "", INT_MIN, INT_MAX);
237         RNA_def_int(ot->srna, "mouse_y", 0, INT_MIN, INT_MAX, "Mouse Y", "", INT_MIN, INT_MAX);
238         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "");
239 }
240
241 /* ****** Border Select ****** */
242
243 static int node_borderselect_exec(bContext *C, wmOperator *op)
244 {
245         SpaceNode *snode= CTX_wm_space_node(C);
246         ARegion *ar= CTX_wm_region(C);
247         bNode *node;
248         rcti rect;
249         rctf rectf;
250         int gesture_mode= RNA_int_get(op->ptr, "gesture_mode");
251         
252         rect.xmin= RNA_int_get(op->ptr, "xmin");
253         rect.ymin= RNA_int_get(op->ptr, "ymin");
254         UI_view2d_region_to_view(&ar->v2d, rect.xmin, rect.ymin, &rectf.xmin, &rectf.ymin);
255         
256         rect.xmax= RNA_int_get(op->ptr, "xmax");
257         rect.ymax= RNA_int_get(op->ptr, "ymax");
258         UI_view2d_region_to_view(&ar->v2d, rect.xmax, rect.ymax, &rectf.xmax, &rectf.ymax);
259         
260         if (snode->edittree == NULL) // XXX should this be in poll()? - campbell
261                 return OPERATOR_FINISHED;
262
263         for(node= snode->edittree->nodes.first; node; node= node->next) {
264                 if(BLI_isect_rctf(&rectf, &node->totr, NULL)) {
265                         if(gesture_mode==GESTURE_MODAL_SELECT)
266                                 node->flag |= SELECT;
267                         else
268                                 node->flag &= ~SELECT;
269                 }
270         }
271         
272         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL); /* Do we need to pass the scene? */
273
274         return OPERATOR_FINISHED;
275 }
276
277 void NODE_OT_select_border(wmOperatorType *ot)
278 {
279         /* identifiers */
280         ot->name= "Border Select";
281         ot->idname= "NODE_OT_select_border";
282         
283         /* api callbacks */
284         ot->invoke= WM_border_select_invoke;
285         ot->exec= node_borderselect_exec;
286         ot->modal= WM_border_select_modal;
287         
288         ot->poll= ED_operator_node_active;
289         
290         /* flags */
291         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
292         
293         /* rna */
294         WM_operator_properties_gesture_border(ot, FALSE);
295 }
296
297 /* ****** Select/Deselect All ****** */
298
299 static int node_select_all_exec(bContext *C, wmOperator *op)
300 {
301         SpaceNode *snode = CTX_wm_space_node(C);
302         bNode *first = snode->edittree->nodes.first;
303         bNode *node;
304         int count= 0;
305
306         for(node=first; node; node=node->next)
307                 if(node->flag & NODE_SELECT)
308                         count++;
309
310         if(count) {
311                 for(node=first; node; node=node->next)
312                         node->flag &= ~NODE_SELECT;
313         }
314         else {
315                 for(node=first; node; node=node->next)
316                         node->flag |= NODE_SELECT;
317         }
318         
319         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL);
320         return OPERATOR_FINISHED;
321 }
322
323 void NODE_OT_select_all(wmOperatorType *ot)
324 {
325         /* identifiers */
326         ot->name = "Select/Deselect All";
327         ot->description = "(De)select all nodes.";
328         ot->idname = "NODE_OT_select_all";
329         
330         /* api callbacks */
331         ot->exec = node_select_all_exec;
332         ot->poll = ED_operator_node_active;
333         
334         /* flags */
335         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
336 }
337
338 /* ****** Select Linked To ****** */
339
340 static int node_select_linked_to_exec(bContext *C, wmOperator *op)
341 {
342         SpaceNode *snode = CTX_wm_space_node(C);
343         bNodeLink *link;
344         bNode *node;
345         
346         for (node=snode->edittree->nodes.first; node; node=node->next)
347                 node->flag &= ~NODE_TEST;
348
349         for (link=snode->edittree->links.first; link; link=link->next)
350                 if (link->fromnode->flag & NODE_SELECT)
351                         link->tonode->flag |= NODE_TEST;
352         
353         for (node=snode->edittree->nodes.first; node; node=node->next)
354                 if (node->flag & NODE_TEST)
355                         node->flag |= NODE_SELECT;
356         
357         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL);
358         return OPERATOR_FINISHED;
359 }
360
361 void NODE_OT_select_linked_to(wmOperatorType *ot)
362 {
363         /* identifiers */
364         ot->name = "Select Linked To";
365         ot->description = "Select nodes linked to the selected ones.";
366         ot->idname = "NODE_OT_select_linked_to";
367         
368         /* api callbacks */
369         ot->exec = node_select_linked_to_exec;
370         ot->poll = ED_operator_node_active;
371         
372         /* flags */
373         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
374 }
375
376 /* ****** Select Linked From ****** */
377
378 static int node_select_linked_from_exec(bContext *C, wmOperator *op)
379 {
380         SpaceNode *snode = CTX_wm_space_node(C);
381         bNodeLink *link;
382         bNode *node;
383         
384         for(node=snode->edittree->nodes.first; node; node=node->next)
385                 node->flag &= ~NODE_TEST;
386
387         for(link=snode->edittree->links.first; link; link=link->next)
388                 if(link->tonode->flag & NODE_SELECT)
389                         link->fromnode->flag |= NODE_TEST;
390         
391         for(node=snode->edittree->nodes.first; node; node=node->next)
392                 if(node->flag & NODE_TEST)
393                         node->flag |= NODE_SELECT;
394         
395         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL);
396         return OPERATOR_FINISHED;
397 }
398
399 void NODE_OT_select_linked_from(wmOperatorType *ot)
400 {
401         /* identifiers */
402         ot->name = "Select Linked From";
403         ot->description = "Select nodes linked from the selected ones.";
404         ot->idname = "NODE_OT_select_linked_from";
405         
406         /* api callbacks */
407         ot->exec = node_select_linked_from_exec;
408         ot->poll = ED_operator_node_active;
409         
410         /* flags */
411         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
412 }
413