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