2.5 / Nodes
[blender-staging.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_scene_types.h"
33 #include "DNA_screen_types.h"
34 #include "DNA_space_types.h"
35
36 #include "BKE_context.h"
37 #include "BKE_node.h"
38 #include "BKE_global.h"
39
40 #include "BLI_rect.h"
41
42 #include "ED_space_api.h"
43 #include "ED_screen.h"
44 #include "ED_types.h"
45
46 #include "RNA_access.h"
47 #include "RNA_define.h"
48
49 #include "WM_api.h"
50 #include "WM_types.h"
51
52 #include "UI_view2d.h"
53  
54 #include "node_intern.h"
55  
56 /* ************************** Node generic ************** */
57
58 /* allows to walk the list in order of visibility */
59 static bNode *next_node(bNodeTree *ntree)
60 {
61         static bNode *current=NULL, *last= NULL;
62         
63         if(ntree) {
64                 /* set current to the first selected node */
65                 for(current= ntree->nodes.last; current; current= current->prev)
66                         if(current->flag & NODE_SELECT)
67                                 break;
68                 
69                 /* set last to the first unselected node */
70                 for(last= ntree->nodes.last; last; last= last->prev)
71                         if((last->flag & NODE_SELECT)==0)
72                                 break;
73                 
74                 if(current==NULL)
75                         current= last;
76                 
77                 return NULL;
78         }
79         /* no nodes, or we are ready */
80         if(current==NULL)
81                 return NULL;
82         
83         /* now we walk the list backwards, but we always return current */
84         if(current->flag & NODE_SELECT) {
85                 bNode *node= current;
86                 
87                 /* find previous selected */
88                 current= current->prev;
89                 while(current && (current->flag & NODE_SELECT)==0)
90                         current= current->prev;
91                 
92                 /* find first unselected */
93                 if(current==NULL)
94                         current= last;
95                 
96                 return node;
97         }
98         else {
99                 bNode *node= current;
100                 
101                 /* find previous unselected */
102                 current= current->prev;
103                 while(current && (current->flag & NODE_SELECT))
104                         current= current->prev;
105                 
106                 return node;
107         }
108         
109         return NULL;
110 }
111
112 static int do_header_node(SpaceNode *snode, bNode *node, float mx, float my)
113 {
114         rctf totr= node->totr;
115         
116         totr.ymin= totr.ymax-20.0f;
117         
118         totr.xmax= totr.xmin+15.0f;
119         if(BLI_in_rctf(&totr, mx, my)) {
120                 node->flag |= NODE_HIDDEN;
121                 // allqueue(REDRAWNODE, 0);
122                 return 1;
123         }       
124         
125         totr.xmax= node->totr.xmax;
126         totr.xmin= totr.xmax-18.0f;
127         if(node->typeinfo->flag & NODE_PREVIEW) {
128                 if(BLI_in_rctf(&totr, mx, my)) {
129                         node->flag ^= NODE_PREVIEW;
130                         // allqueue(REDRAWNODE, 0);
131                         return 1;
132                 }
133                 totr.xmin-=18.0f;
134         }
135         if(node->type == NODE_GROUP) {
136                 if(BLI_in_rctf(&totr, mx, my)) {
137                         snode_make_group_editable(snode, node);
138                         return 1;
139                 }
140                 totr.xmin-=18.0f;
141         }
142         if(node->typeinfo->flag & NODE_OPTIONS) {
143                 if(BLI_in_rctf(&totr, mx, my)) {
144                         node->flag ^= NODE_OPTIONS;
145                         // allqueue(REDRAWNODE, 0);
146                         return 1;
147                 }
148                 totr.xmin-=18.0f;
149         }
150         /* hide unused sockets */
151         if(BLI_in_rctf(&totr, mx, my)) {
152                 // XXX node_hide_unhide_sockets(snode, node);
153         }
154         
155         
156         totr= node->totr;
157         totr.xmin= totr.xmax-10.0f;
158         totr.ymax= totr.ymin+10.0f;
159         if(BLI_in_rctf(&totr, mx, my)) {
160                 // XXX scale_node(snode, node);
161                 return 1;
162         }
163         return 0;
164 }
165
166 static int do_header_hidden_node(SpaceNode *snode, bNode *node, float mx, float my)
167 {
168         rctf totr= node->totr;
169         
170         totr.xmax= totr.xmin+15.0f;
171         if(BLI_in_rctf(&totr, mx, my)) {
172                 node->flag &= ~NODE_HIDDEN;
173                 // allqueue(REDRAWNODE, 0);
174                 return 1;
175         }       
176         
177         totr.xmax= node->totr.xmax;
178         totr.xmin= node->totr.xmax-15.0f;
179         if(BLI_in_rctf(&totr, mx, my)) {
180                 scale_node(snode, node);
181                 return 1;
182         }
183         return 0;
184 }
185
186 static node_mouse_select(SpaceNode *snode, ARegion *ar, short *mval)
187 {
188         bNode *node;
189         float mx, my;
190         
191         mx= (float)mval[0];
192         my= (float)mval[1];
193         
194         UI_view2d_region_to_view(&ar->v2d, mval[0], mval[1], &mx, &my);
195         
196         for(next_node(snode->edittree); (node=next_node(NULL));) {
197                 
198                 /* first check for the headers or scaling widget */
199                 /* XXX if(node->flag & NODE_HIDDEN) {
200                         if(do_header_hidden_node(snode, node, mx, my))
201                                 return 1;
202                 }
203                 else {
204                         if(do_header_node(snode, node, mx, my))
205                                 return 1;
206                 }*/
207                 
208                 /* node body */
209                 if(BLI_in_rctf(&node->totr, mx, my))
210                         break;
211         }
212         if(node) {
213                 // XXX if((G.qual & LR_SHIFTKEY)==0)
214                 //      node_deselectall(snode, 0);
215                 
216                 // XXX
217                 /*
218                 if(G.qual & LR_SHIFTKEY) {
219                         if(node->flag & SELECT)
220                                 node->flag &= ~SELECT;
221                         else
222                                 node->flag |= SELECT;
223                 }
224                 else */
225                         node->flag |= SELECT;
226                 
227                 node_set_active(snode, node);
228                 
229                 /* viewer linking */
230                 //if(G.qual & LR_CTRLKEY)
231                 //      node_link_viewer(snode, node);
232                 
233                 /* not so nice (no event), but function below delays redraw otherwise */
234                 //force_draw(0);
235                 
236                 //std_rmouse_transform(node_transform_ext);     /* does undo push for select */
237                 
238                 return 1;
239         }
240         return 0;
241 }
242
243 static int node_select_exec(bContext *C, wmOperator *op)
244 {
245         SpaceNode *snode= (SpaceNode*)CTX_wm_space_data(C);
246         ScrArea *sa= CTX_wm_area(C);
247         ARegion *ar= CTX_wm_region(C);
248         int select_type;
249         short mval[2];
250
251         select_type = RNA_enum_get(op->ptr, "select_type");
252         
253         switch (select_type) {
254                 case NODE_SELECT_MOUSE:
255                         mval[0] = RNA_int_get(op->ptr, "mx");
256                         mval[1] = RNA_int_get(op->ptr, "my");
257                         node_mouse_select(snode, ar, mval);
258                         break;
259         }
260         return OPERATOR_FINISHED;
261 }
262
263 static int node_select_invoke(bContext *C, wmOperator *op, wmEvent *event)
264 {
265         ScrArea *sa= CTX_wm_area(C);
266         ARegion *ar= CTX_wm_region(C);
267         short mval[2];  
268         
269         mval[0]= event->x - ar->winrct.xmin;
270         mval[1]= event->y - ar->winrct.ymin;
271         
272         RNA_int_set(op->ptr, "mx", mval[0]);
273         RNA_int_set(op->ptr, "my", mval[1]);
274
275         return node_select_exec(C,op);
276 }
277
278 /* operators */
279
280 static EnumPropertyItem prop_select_items[] = {
281         {NODE_SELECT_MOUSE, "NORMAL", "Normal Select", "Select using the mouse"},
282         {0, NULL, NULL, NULL}};
283
284 void NODE_OT_select(wmOperatorType *ot)
285 {
286         PropertyRNA *prop;
287         
288         /* identifiers */
289         ot->name= "Activate/Select";
290         ot->idname= "NODE_OT_select";
291         
292         /* api callbacks */
293         ot->invoke= node_select_invoke;
294         ot->poll= ED_operator_node_active;
295         
296         prop = RNA_def_property(ot->srna, "select_type", PROP_ENUM, PROP_NONE);
297         RNA_def_property_enum_items(prop, prop_select_items);
298         
299         prop = RNA_def_property(ot->srna, "mx", PROP_INT, PROP_NONE);
300         prop = RNA_def_property(ot->srna, "my", PROP_INT, PROP_NONE);
301 }