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