svn merge -r 20822:21041 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[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, 0);
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= (SpaceNode*)CTX_wm_space_data(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==0) {
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;
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 EnumPropertyItem prop_select_types[] = {
228         {NODE_EXCLUSIVE, "EXCLUSIVE", 0, "Exclusive", ""}, /* right mouse */
229         {NODE_EXTEND, "EXTEND", 0, "Extend", ""}, /* left mouse */
230         {0, NULL, 0, NULL, NULL}
231 };
232
233 static int node_borderselect_exec(bContext *C, wmOperator *op)
234 {
235         SpaceNode *snode= (SpaceNode*)CTX_wm_space_data(C);
236         ARegion *ar= CTX_wm_region(C);
237         bNode *node;
238         rcti rect;
239         rctf rectf;
240         short val;
241         
242         val= RNA_int_get(op->ptr, "event_type");
243         
244         rect.xmin= RNA_int_get(op->ptr, "xmin");
245         rect.ymin= RNA_int_get(op->ptr, "ymin");
246         UI_view2d_region_to_view(&ar->v2d, rect.xmin, rect.ymin, &rectf.xmin, &rectf.ymin);
247         
248         rect.xmax= RNA_int_get(op->ptr, "xmax");
249         rect.ymax= RNA_int_get(op->ptr, "ymax");
250         UI_view2d_region_to_view(&ar->v2d, rect.xmax, rect.ymax, &rectf.xmax, &rectf.ymax);
251         
252         if (snode->edittree == NULL) // XXX should this be in poll()? - campbell
253                 return OPERATOR_FINISHED;
254
255         for(node= snode->edittree->nodes.first; node; node= node->next) {
256                 if(BLI_isect_rctf(&rectf, &node->totr, NULL)) {
257                         if(val==NODE_EXTEND)
258                                 node->flag |= SELECT;
259                         else
260                                 node->flag &= ~SELECT;
261                 }
262         }
263         
264         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL); /* Do we need to pass the scene? */
265
266         return OPERATOR_FINISHED;
267 }
268
269 void NODE_OT_select_border(wmOperatorType *ot)
270 {
271         /* identifiers */
272         ot->name= "Border Select";
273         ot->idname= "NODE_OT_select_border";
274         
275         /* api callbacks */
276         ot->invoke= WM_border_select_invoke;
277         ot->exec= node_borderselect_exec;
278         ot->modal= WM_border_select_modal;
279         
280         ot->poll= ED_operator_node_active;
281         
282         /* flags */
283         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
284         
285         /* rna */
286         RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
287         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
288         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
289         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
290         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
291
292         RNA_def_enum(ot->srna, "type", prop_select_types, 0, "Type", "");
293 }