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