2.5
[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         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL); /* Do we need to pass the scene? */
128
129         /* allow tweak event to work too */
130         return OPERATOR_PASS_THROUGH;
131 }
132
133 static int node_select_modal(bContext *C, wmOperator *op, wmEvent *event)
134 {
135         /* execute the events */
136         switch (event->type) {
137                 case MOUSEMOVE:
138                         printf("%d %d\n", event->x, event->y);
139                         break;
140                 case SELECTMOUSE:
141                         //if (event->val==0) {
142                                 /* calculate overall delta mouse-movement for redo */
143                                 printf("done translating\n");
144                                 //WM_cursor_restore(CTX_wm_window(C));
145                                 
146                                 return OPERATOR_FINISHED;
147                         //}
148                         break;
149         }
150
151         return OPERATOR_RUNNING_MODAL;
152 }
153
154 static int node_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
155 {
156         ARegion *ar= CTX_wm_region(C);
157         short mval[2];  
158         
159         mval[0]= event->x - ar->winrct.xmin;
160         mval[1]= event->y - ar->winrct.ymin;
161         
162         RNA_int_set(op->ptr, "mouse_x", mval[0]);
163         RNA_int_set(op->ptr, "mouse_y", mval[1]);
164
165         return node_select_exec(C,op);
166 }
167
168 static int node_extend_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
169 {
170         RNA_boolean_set(op->ptr, "extend", KM_SHIFT);
171
172         return node_select_invoke(C, op, event);
173 }
174
175 /* operators */
176
177 static EnumPropertyItem prop_select_items[] = {
178         {NODE_SELECT_MOUSE, "NORMAL", "Normal Select", "Select using the mouse"},
179         {0, NULL, NULL, NULL}};
180
181 void NODE_OT_extend_select(wmOperatorType *ot)
182 {
183         /* identifiers */
184         ot->name= "Activate/Select (Shift)";
185         ot->idname= "NODE_OT_extend_select";
186         
187         /* api callbacks */
188         ot->invoke= node_extend_select_invoke;
189         ot->poll= ED_operator_node_active;
190         
191         RNA_def_enum(ot->srna, "select_type", prop_select_items, 0, "Select Type", "");
192         
193         RNA_def_int(ot->srna, "mouse_x", 0, INT_MIN, INT_MAX, "Mouse X", "", INT_MIN, INT_MAX);
194         RNA_def_int(ot->srna, "mouse_y", 0, INT_MIN, INT_MAX, "Mouse Y", "", INT_MIN, INT_MAX);
195         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "");
196 }
197
198 void NODE_OT_select(wmOperatorType *ot)
199 {
200         /* identifiers */
201         ot->name= "Activate/Select";
202         ot->idname= "NODE_OT_select";
203         
204         /* api callbacks */
205         ot->invoke= node_select_invoke;
206         ot->poll= ED_operator_node_active;
207         ot->modal= node_select_modal;
208         
209         RNA_def_enum(ot->srna, "select_type", prop_select_items, 0, "Select Type", "");
210         
211         RNA_def_int(ot->srna, "mouse_x", 0, INT_MIN, INT_MAX, "Mouse X", "", INT_MIN, INT_MAX);
212         RNA_def_int(ot->srna, "mouse_y", 0, INT_MIN, INT_MAX, "Mouse Y", "", INT_MIN, INT_MAX);
213         RNA_def_boolean(ot->srna, "extend", 0, "Extend", "");
214 }
215
216 /* ****** Border Select ****** */
217
218 static EnumPropertyItem prop_select_types[] = {
219         {NODE_EXCLUSIVE, "EXCLUSIVE", "Exclusive", ""}, /* right mouse */
220         {NODE_EXTEND, "EXTEND", "Extend", ""}, /* left mouse */
221         {0, NULL, NULL, NULL}
222 };
223
224 static int node_borderselect_exec(bContext *C, wmOperator *op)
225 {
226         SpaceNode *snode= (SpaceNode*)CTX_wm_space_data(C);
227         ARegion *ar= CTX_wm_region(C);
228         bNode *node;
229         rcti rect;
230         rctf rectf;
231         short val;
232         
233         val= RNA_int_get(op->ptr, "event_type");
234         
235         rect.xmin= RNA_int_get(op->ptr, "xmin");
236         rect.ymin= RNA_int_get(op->ptr, "ymin");
237         UI_view2d_region_to_view(&ar->v2d, rect.xmin, rect.ymin, &rectf.xmin, &rectf.ymin);
238         
239         rect.xmax= RNA_int_get(op->ptr, "xmax");
240         rect.ymax= RNA_int_get(op->ptr, "ymax");
241         UI_view2d_region_to_view(&ar->v2d, rect.xmax, rect.ymax, &rectf.xmax, &rectf.ymax);
242         
243         if (snode->edittree == NULL) // XXX should this be in poll()? - campbell
244                 return OPERATOR_FINISHED;
245
246         for(node= snode->edittree->nodes.first; node; node= node->next) {
247                 if(BLI_isect_rctf(&rectf, &node->totr, NULL)) {
248                         if(val==NODE_EXTEND)
249                                 node->flag |= SELECT;
250                         else
251                                 node->flag &= ~SELECT;
252                 }
253         }
254         
255         WM_event_add_notifier(C, NC_SCENE|ND_NODES, NULL); /* Do we need to pass the scene? */
256
257         return OPERATOR_FINISHED;
258 }
259
260 void NODE_OT_border_select(wmOperatorType *ot)
261 {
262         /* identifiers */
263         ot->name= "Border Select";
264         ot->idname= "NODE_OT_border_select";
265         
266         /* api callbacks */
267         ot->invoke= WM_border_select_invoke;
268         ot->exec= node_borderselect_exec;
269         ot->modal= WM_border_select_modal;
270         
271         ot->poll= ED_operator_node_active;
272         
273         /* rna */
274         RNA_def_int(ot->srna, "event_type", 0, INT_MIN, INT_MAX, "Event Type", "", INT_MIN, INT_MAX);
275         RNA_def_int(ot->srna, "xmin", 0, INT_MIN, INT_MAX, "X Min", "", INT_MIN, INT_MAX);
276         RNA_def_int(ot->srna, "xmax", 0, INT_MIN, INT_MAX, "X Max", "", INT_MIN, INT_MAX);
277         RNA_def_int(ot->srna, "ymin", 0, INT_MIN, INT_MAX, "Y Min", "", INT_MIN, INT_MAX);
278         RNA_def_int(ot->srna, "ymax", 0, INT_MIN, INT_MAX, "Y Max", "", INT_MIN, INT_MAX);
279
280         RNA_def_enum(ot->srna, "type", prop_select_types, 0, "Type", "");
281 }