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