Cycles: svn merge -r39457:39669 https://svn.blender.org/svnroot/bf-blender/trunk...
[blender.git] / source / blender / editors / space_node / node_header.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2008 Blender Foundation.
21  * All rights reserved.
22  *
23  * 
24  * Contributor(s): Blender Foundation
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 /** \file blender/editors/space_node/node_header.c
30  *  \ingroup spnode
31  */
32
33
34 #include <string.h>
35 #include <stdio.h>
36
37 #include "DNA_space_types.h"
38 #include "DNA_node_types.h"
39 #include "DNA_scene_types.h"
40 #include "DNA_screen_types.h"
41
42 #include "MEM_guardedalloc.h"
43
44 #include "BLI_blenlib.h"
45 #include "BLI_utildefines.h"
46
47 #include "BKE_context.h"
48 #include "BKE_scene.h"
49 #include "BKE_screen.h"
50 #include "BKE_node.h"
51 #include "BKE_main.h"
52
53 #include "WM_api.h"
54 #include "WM_types.h"
55
56
57 #include "UI_interface.h"
58 #include "UI_resources.h"
59 #include "UI_interface_icons.h"
60 #include "UI_view2d.h"
61
62 #include "node_intern.h"
63
64 /* ************************ add menu *********************** */
65
66 static void do_node_add(bContext *C, void *UNUSED(arg), int event)
67 {
68         Main *bmain= CTX_data_main(C);
69         Scene *scene= CTX_data_scene(C);
70         SpaceNode *snode= CTX_wm_space_node(C);
71         ScrArea *sa= CTX_wm_area(C);
72         ARegion *ar;
73         bNode *node;
74         
75         /* get location to add node at mouse */
76         for(ar=sa->regionbase.first; ar; ar=ar->next) {
77                 if(ar->regiontype == RGN_TYPE_WINDOW) {
78                         wmWindow *win= CTX_wm_window(C);
79                         int x= win->eventstate->x - ar->winrct.xmin;
80                         int y= win->eventstate->y - ar->winrct.ymin;
81                         
82                         if(y < 60) y+= 60;
83                         UI_view2d_region_to_view(&ar->v2d, x, y, &snode->mx, &snode->my);
84                 }
85         }
86         
87         /* store selection in temp test flag */
88         for(node= snode->edittree->nodes.first; node; node= node->next) {
89                 if(node->flag & NODE_SELECT) node->flag |= NODE_TEST;
90                 else node->flag &= ~NODE_TEST;
91         }
92         
93         node= node_add_node(snode, bmain, scene, event, snode->mx, snode->my);
94         
95         /* select previous selection before autoconnect */
96         for(node= snode->edittree->nodes.first; node; node= node->next) {
97                 if(node->flag & NODE_TEST) node->flag |= NODE_SELECT;
98         }
99         
100         /* deselect after autoconnection */
101         for(node= snode->edittree->nodes.first; node; node= node->next) {
102                 if(node->flag & NODE_TEST) node->flag &= ~NODE_SELECT;
103         }
104                 
105         snode_notify(C, snode);
106         snode_dag_update(C, snode);
107 }
108
109 static void node_auto_add_menu(bContext *C, uiLayout *layout, void *arg_nodeclass)
110 {
111         Main *bmain= CTX_data_main(C);
112         SpaceNode *snode= CTX_wm_space_node(C);
113         bNodeTree *ntree;
114         int nodeclass= GET_INT_FROM_POINTER(arg_nodeclass);
115         int tot= 0, a;
116         
117         ntree = snode->nodetree;
118
119         if(!ntree) {
120                 uiItemS(layout);
121                 return;
122         }
123
124         /* mostly taken from toolbox.c, node_add_sublevel() */
125         if(nodeclass==NODE_CLASS_GROUP) {
126                 bNodeTree *ngroup= bmain->nodetree.first;
127                 for(; ngroup; ngroup= ngroup->id.next)
128                         if(ngroup->type==ntree->type)
129                                 tot++;
130         }
131         else {
132                 bNodeType *type = ntree->alltypes.first;
133                 while(type) {
134                         if(type->nclass == nodeclass)
135                                 tot++;
136                         type= type->next;
137                 }
138         }       
139         
140         if(tot==0) {
141                 uiItemS(layout);
142                 return;
143         }
144
145         uiLayoutSetFunc(layout, do_node_add, NULL);
146         
147         if(nodeclass==NODE_CLASS_GROUP) {
148                 bNodeTree *ngroup= bmain->nodetree.first;
149
150                 for(tot=0, a=0; ngroup; ngroup= ngroup->id.next, tot++) {
151                         if(ngroup->type==ntree->type) {
152                                 uiItemV(layout, ngroup->id.name+2, ICON_NONE, NODE_GROUP_MENU+tot);
153                                 a++;
154                         }
155                 }
156         }
157         else {
158                 bNodeType *type;
159                 int script=0;
160
161                 for(a=0, type= ntree->alltypes.first; type; type=type->next) {
162                         if(type->nclass == nodeclass && type->name) {
163                                 if(type->type == NODE_DYNAMIC) {
164                                         uiItemV(layout, type->name, ICON_NONE, NODE_DYNAMIC_MENU+script);
165                                         script++;
166                                 }
167                                 else
168                                         uiItemV(layout, type->name, ICON_NONE, type->type);
169
170                                 a++;
171                         }
172                 }
173         }
174 }
175
176 static void node_menu_add(const bContext *C, Menu *menu)
177 {
178         Scene *scene= CTX_data_scene(C);
179         SpaceNode *snode= CTX_wm_space_node(C);
180         uiLayout *layout= menu->layout;
181
182         if(!snode->nodetree)
183                 uiLayoutSetActive(layout, 0);
184
185         if(snode->treetype==NTREE_SHADER) {
186                 uiItemMenuF(layout, "Input", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_INPUT));
187                 uiItemMenuF(layout, "Output", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OUTPUT));
188                 if(scene_use_new_shading_system(scene))
189                         uiItemMenuF(layout, "Closure", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CLOSURE));
190                 uiItemMenuF(layout, "Color", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_COLOR));
191                 uiItemMenuF(layout, "Vector", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_VECTOR));
192                 uiItemMenuF(layout, "Convertor", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CONVERTOR));
193                 uiItemMenuF(layout, "Group", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_GROUP));
194                 if(scene_use_new_shading_system(scene))
195                         uiItemMenuF(layout, "Texture", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_TEXTURE));
196                 //uiItemMenuF(layout, "Dynamic", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_DYNAMIC));
197         }
198         else if(snode->treetype==NTREE_COMPOSIT) {
199                 uiItemMenuF(layout, "Input", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_INPUT));
200                 uiItemMenuF(layout, "Output", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OUTPUT));
201                 uiItemMenuF(layout, "Color", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_COLOR));
202                 uiItemMenuF(layout, "Vector", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_VECTOR));
203                 uiItemMenuF(layout, "Filter", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_FILTER));
204                 uiItemMenuF(layout, "Convertor", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CONVERTOR));
205                 uiItemMenuF(layout, "Matte", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_MATTE));
206                 uiItemMenuF(layout, "Distort", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_DISTORT));
207                 uiItemMenuF(layout, "Group", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_GROUP));
208         }
209         else if(snode->treetype==NTREE_TEXTURE) {
210                 uiItemMenuF(layout, "Input", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_INPUT));
211                 uiItemMenuF(layout, "Output", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OUTPUT));
212                 uiItemMenuF(layout, "Color", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_COLOR));
213                 uiItemMenuF(layout, "Patterns", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_PATTERN));
214                 uiItemMenuF(layout, "Textures", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_TEXTURE));
215                 uiItemMenuF(layout, "Convertor", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CONVERTOR));
216                 uiItemMenuF(layout, "Distort", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_DISTORT));
217                 uiItemMenuF(layout, "Group", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_GROUP));
218         }
219 }
220
221 void node_menus_register(void)
222 {
223         MenuType *mt;
224
225         mt= MEM_callocN(sizeof(MenuType), "spacetype node menu add");
226         strcpy(mt->idname, "NODE_MT_add");
227         strcpy(mt->label, "Add");
228         mt->draw= node_menu_add;
229         WM_menutype_add(mt);
230 }
231