- modal keymap for border select
[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         /* allow tweak event to work too */
145         return OPERATOR_FINISHED|OPERATOR_PASS_THROUGH;
146 }
147
148 static int node_select_modal(bContext *C, wmOperator *op, wmEvent *event)
149 {
150         /* execute the events */
151         switch (event->type) {
152                 case MOUSEMOVE:
153                         printf("%d %d\n", event->x, event->y);
154                         break;
155                 case SELECTMOUSE:
156                         //if (event->val==KM_RELEASE) {
157                                 /* calculate overall delta mouse-movement for redo */
158                                 printf("done translating\n");
159                                 //WM_cursor_restore(CTX_wm_window(C));
160                                 
161                                 return OPERATOR_FINISHED;
162                         //}
163                         break;
164         }
165
166         return OPERATOR_RUNNING_MODAL;
167 }
168
169 static int node_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
170 {
171         ARegion *ar= CTX_wm_region(C);
172         short mval[2];  
173         
174         mval[0]= event->x - ar->winrct.xmin;
175         mval[1]= event->y - ar->winrct.ymin;
176         
177         RNA_int_set(op->ptr, "mouse_x", mval[0]);
178         RNA_int_set(op->ptr, "mouse_y", mval[1]);
179
180         return node_select_exec(C,op);
181 }
182
183 static int node_extend_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
184 {
185         RNA_boolean_set(op->ptr, "extend", KM_SHIFT);
186
187         return node_select_invoke(C, op, event);
188 }
189
190 /* operators */
191
192 static EnumPropertyItem prop_select_items[] = {
193         {NODE_SELECT_MOUSE, "NORMAL", 0, "Normal Select", "Select using the mouse"},
194         {0, NULL, 0, NULL, NULL}};
195
196 void NODE_OT_select_extend(wmOperatorType *ot)
197 {
198         // XXX - Todo - This should just be a toggle option for NODE_OT_select not its own op
199         /* identifiers */
200         ot->name= "Activate/Select (Shift)";
201         ot->idname= "NODE_OT_select_extend";
202         
203         /* api callbacks */
204         ot->invoke= node_extend_select_invoke;
205         ot->poll= ED_operator_node_active;
206         
207         /* flags */
208         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
209         
210         RNA_def_enum(ot->srna, "select_type", prop_select_items, 0, "Select Type", "");
211         
212         RNA_def_int(ot->srna, "mouse_x", 0, INT_MIN, INT_MAX, "Mouse X", "", INT_MIN, INT_MAX);
213         RNA_def_int(ot->srna, "mouse_y", 0, INT_MIN, INT_MAX, "Mouse Y", "", INT_MIN, INT_MAX);
214         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "");
215 }
216
217 void NODE_OT_select(wmOperatorType *ot)
218 {
219         /* identifiers */
220         ot->name= "Activate/Select";
221         ot->idname= "NODE_OT_select";
222         
223         /* api callbacks */
224         ot->invoke= node_select_invoke;
225         ot->poll= ED_operator_node_active;
226         ot->modal= node_select_modal;
227         
228         /* flags */
229         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO|OPTYPE_BLOCKING;
230         
231         RNA_def_enum(ot->srna, "select_type", prop_select_items, 0, "Select Type", "");
232         
233         RNA_def_int(ot->srna, "mouse_x", 0, INT_MIN, INT_MAX, "Mouse X", "", INT_MIN, INT_MAX);
234         RNA_def_int(ot->srna, "mouse_y", 0, INT_MIN, INT_MAX, "Mouse Y", "", INT_MIN, INT_MAX);
235         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "");
236 }
237
238 /* ****** Border Select ****** */
239
240 static int node_borderselect_exec(bContext *C, wmOperator *op)
241 {
242         SpaceNode *snode= CTX_wm_space_node(C);
243         ARegion *ar= CTX_wm_region(C);
244         bNode *node;
245         rcti rect;
246         rctf rectf;
247         int gesture_mode= RNA_int_get(op->ptr, "gesture_mode");
248         
249         rect.xmin= RNA_int_get(op->ptr, "xmin");
250         rect.ymin= RNA_int_get(op->ptr, "ymin");
251         UI_view2d_region_to_view(&ar->v2d, rect.xmin, rect.ymin, &rectf.xmin, &rectf.ymin);
252         
253         rect.xmax= RNA_int_get(op->ptr, "xmax");
254         rect.ymax= RNA_int_get(op->ptr, "ymax");
255         UI_view2d_region_to_view(&ar->v2d, rect.xmax, rect.ymax, &rectf.xmax, &rectf.ymax);
256         
257         if (snode->edittree == NULL) // XXX should this be in poll()? - campbell
258                 return OPERATOR_FINISHED;
259
260         for(node= snode->edittree->nodes.first; node; node= node->next) {
261                 if(BLI_isect_rctf(&rectf, &node->totr, NULL)) {
262                         if(gesture_mode==GESTURE_MODAL_SELECT)
263                                 node->flag |= SELECT;
264                         else
265                                 node->flag &= ~SELECT;
266                 }
267         }
268         
269         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL); /* Do we need to pass the scene? */
270
271         return OPERATOR_FINISHED;
272 }
273
274 void NODE_OT_select_border(wmOperatorType *ot)
275 {
276         /* identifiers */
277         ot->name= "Border Select";
278         ot->idname= "NODE_OT_select_border";
279         
280         /* api callbacks */
281         ot->invoke= WM_border_select_invoke;
282         ot->exec= node_borderselect_exec;
283         ot->modal= WM_border_select_modal;
284         
285         ot->poll= ED_operator_node_active;
286         
287         /* flags */
288         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
289         
290         /* rna */
291         WM_operator_properties_gesture_border(ot, FALSE);
292 }
293
294 /* ****** Select/Deselect All ****** */
295
296 static int node_select_all_exec(bContext *C, wmOperator *op)
297 {
298         SpaceNode *snode = CTX_wm_space_node(C);
299         bNode *first = snode->edittree->nodes.first;
300         bNode *node;
301         int count= 0;
302
303         for(node=first; node; node=node->next)
304                 if(node->flag & NODE_SELECT)
305                         count++;
306
307         if(count) {
308                 for(node=first; node; node=node->next)
309                         node->flag &= ~NODE_SELECT;
310         }
311         else {
312                 for(node=first; node; node=node->next)
313                         node->flag |= NODE_SELECT;
314         }
315         
316         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL);
317         return OPERATOR_FINISHED;
318 }
319
320 void NODE_OT_select_all(wmOperatorType *ot)
321 {
322         /* identifiers */
323         ot->name = "Select/Deselect All";
324         ot->description = "(De)select all nodes.";
325         ot->idname = "NODE_OT_select_all";
326         
327         /* api callbacks */
328         ot->exec = node_select_all_exec;
329         ot->poll = ED_operator_node_active;
330         
331         /* flags */
332         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
333 }
334
335 /* ****** Select Linked To ****** */
336
337 static int node_select_linked_to_exec(bContext *C, wmOperator *op)
338 {
339         SpaceNode *snode = CTX_wm_space_node(C);
340         bNodeLink *link;
341         bNode *node;
342         
343         for (node=snode->edittree->nodes.first; node; node=node->next)
344                 node->flag &= ~NODE_TEST;
345
346         for (link=snode->edittree->links.first; link; link=link->next)
347                 if (link->fromnode->flag & NODE_SELECT)
348                         link->tonode->flag |= NODE_TEST;
349         
350         for (node=snode->edittree->nodes.first; node; node=node->next)
351                 if (node->flag & NODE_TEST)
352                         node->flag |= NODE_SELECT;
353         
354         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL);
355         return OPERATOR_FINISHED;
356 }
357
358 void NODE_OT_select_linked_to(wmOperatorType *ot)
359 {
360         /* identifiers */
361         ot->name = "Select Linked To";
362         ot->description = "Select nodes linked to the selected ones.";
363         ot->idname = "NODE_OT_select_linked_to";
364         
365         /* api callbacks */
366         ot->exec = node_select_linked_to_exec;
367         ot->poll = ED_operator_node_active;
368         
369         /* flags */
370         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
371 }
372
373 /* ****** Select Linked From ****** */
374
375 static int node_select_linked_from_exec(bContext *C, wmOperator *op)
376 {
377         SpaceNode *snode = CTX_wm_space_node(C);
378         bNodeLink *link;
379         bNode *node;
380         
381         for(node=snode->edittree->nodes.first; node; node=node->next)
382                 node->flag &= ~NODE_TEST;
383
384         for(link=snode->edittree->links.first; link; link=link->next)
385                 if(link->tonode->flag & NODE_SELECT)
386                         link->fromnode->flag |= NODE_TEST;
387         
388         for(node=snode->edittree->nodes.first; node; node=node->next)
389                 if(node->flag & NODE_TEST)
390                         node->flag |= NODE_SELECT;
391         
392         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL);
393         return OPERATOR_FINISHED;
394 }
395
396 void NODE_OT_select_linked_from(wmOperatorType *ot)
397 {
398         /* identifiers */
399         ot->name = "Select Linked From";
400         ot->description = "Select nodes linked from the selected ones.";
401         ot->idname = "NODE_OT_select_linked_from";
402         
403         /* api callbacks */
404         ot->exec = node_select_linked_from_exec;
405         ot->poll = ED_operator_node_active;
406         
407         /* flags */
408         ot->flag = OPTYPE_REGISTER|OPTYPE_UNDO;
409 }
410