Blender  V2.59
node_header.c
Go to the documentation of this file.
00001 /*
00002  * $Id: node_header.c 38452 2011-07-17 16:14:52Z lukastoenne $
00003  *
00004  * ***** BEGIN GPL LICENSE BLOCK *****
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU General Public License
00008  * as published by the Free Software Foundation; either version 2
00009  * of the License, or (at your option) any later version. 
00010  *
00011  * This program is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  * GNU General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU General Public License
00017  * along with this program; if not, write to the Free Software Foundation,
00018  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00019  *
00020  * The Original Code is Copyright (C) 2008 Blender Foundation.
00021  * All rights reserved.
00022  *
00023  * 
00024  * Contributor(s): Blender Foundation
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #include <string.h>
00035 #include <stdio.h>
00036 
00037 #include "DNA_space_types.h"
00038 #include "DNA_node_types.h"
00039 #include "DNA_scene_types.h"
00040 #include "DNA_screen_types.h"
00041 
00042 #include "MEM_guardedalloc.h"
00043 
00044 #include "BLI_blenlib.h"
00045 #include "BLI_utildefines.h"
00046 
00047 #include "BKE_context.h"
00048 #include "BKE_screen.h"
00049 #include "BKE_node.h"
00050 #include "BKE_main.h"
00051 
00052 #include "WM_api.h"
00053 #include "WM_types.h"
00054 
00055 
00056 #include "UI_interface.h"
00057 #include "UI_resources.h"
00058 #include "UI_interface_icons.h"
00059 #include "UI_view2d.h"
00060 
00061 #include "node_intern.h"
00062 
00063 /* ************************ add menu *********************** */
00064 
00065 static void do_node_add(bContext *C, void *UNUSED(arg), int event)
00066 {
00067         SpaceNode *snode= CTX_wm_space_node(C);
00068         ScrArea *sa= CTX_wm_area(C);
00069         ARegion *ar;
00070         bNode *node;
00071         
00072         /* get location to add node at mouse */
00073         for(ar=sa->regionbase.first; ar; ar=ar->next) {
00074                 if(ar->regiontype == RGN_TYPE_WINDOW) {
00075                         wmWindow *win= CTX_wm_window(C);
00076                         int x= win->eventstate->x - ar->winrct.xmin;
00077                         int y= win->eventstate->y - ar->winrct.ymin;
00078                         
00079                         if(y < 60) y+= 60;
00080                         UI_view2d_region_to_view(&ar->v2d, x, y, &snode->mx, &snode->my);
00081                 }
00082         }
00083         
00084         /* store selection in temp test flag */
00085         for(node= snode->edittree->nodes.first; node; node= node->next) {
00086                 if(node->flag & NODE_SELECT) node->flag |= NODE_TEST;
00087                 else node->flag &= ~NODE_TEST;
00088         }
00089         
00090         node= node_add_node(snode, CTX_data_scene(C), event, snode->mx, snode->my);
00091         
00092         /* select previous selection before autoconnect */
00093         for(node= snode->edittree->nodes.first; node; node= node->next) {
00094                 if(node->flag & NODE_TEST) node->flag |= NODE_SELECT;
00095         }
00096         
00097         /* deselect after autoconnection */
00098         for(node= snode->edittree->nodes.first; node; node= node->next) {
00099                 if(node->flag & NODE_TEST) node->flag &= ~NODE_SELECT;
00100         }
00101                 
00102         snode_notify(C, snode);
00103         snode_dag_update(C, snode);
00104 }
00105 
00106 static void node_auto_add_menu(bContext *C, uiLayout *layout, void *arg_nodeclass)
00107 {
00108         Main *bmain= CTX_data_main(C);
00109         SpaceNode *snode= CTX_wm_space_node(C);
00110         bNodeTree *ntree;
00111         int nodeclass= GET_INT_FROM_POINTER(arg_nodeclass);
00112         int tot= 0, a;
00113         
00114         ntree = snode->nodetree;
00115 
00116         if(!ntree) {
00117                 uiItemS(layout);
00118                 return;
00119         }
00120 
00121         /* mostly taken from toolbox.c, node_add_sublevel() */
00122         if(nodeclass==NODE_CLASS_GROUP) {
00123                 bNodeTree *ngroup= bmain->nodetree.first;
00124                 for(; ngroup; ngroup= ngroup->id.next)
00125                         if(ngroup->type==ntree->type)
00126                                 tot++;
00127         }
00128         else {
00129                 bNodeType *type = ntree->alltypes.first;
00130                 while(type) {
00131                         if(type->nclass == nodeclass)
00132                                 tot++;
00133                         type= type->next;
00134                 }
00135         }       
00136         
00137         if(tot==0) {
00138                 uiItemS(layout);
00139                 return;
00140         }
00141 
00142         uiLayoutSetFunc(layout, do_node_add, NULL);
00143         
00144         if(nodeclass==NODE_CLASS_GROUP) {
00145                 bNodeTree *ngroup= bmain->nodetree.first;
00146 
00147                 for(tot=0, a=0; ngroup; ngroup= ngroup->id.next, tot++) {
00148                         if(ngroup->type==ntree->type) {
00149                                 uiItemV(layout, ngroup->id.name+2, ICON_NONE, NODE_GROUP_MENU+tot);
00150                                 a++;
00151                         }
00152                 }
00153         }
00154         else {
00155                 bNodeType *type;
00156                 int script=0;
00157 
00158                 for(a=0, type= ntree->alltypes.first; type; type=type->next) {
00159                         if(type->nclass == nodeclass && type->name) {
00160                                 if(type->type == NODE_DYNAMIC) {
00161                                         uiItemV(layout, type->name, ICON_NONE, NODE_DYNAMIC_MENU+script);
00162                                         script++;
00163                                 }
00164                                 else
00165                                         uiItemV(layout, type->name, ICON_NONE, type->type);
00166 
00167                                 a++;
00168                         }
00169                 }
00170         }
00171 }
00172 
00173 static void node_menu_add(const bContext *C, Menu *menu)
00174 {
00175         SpaceNode *snode= CTX_wm_space_node(C);
00176         uiLayout *layout= menu->layout;
00177 
00178         if(!snode->nodetree)
00179                 uiLayoutSetActive(layout, 0);
00180 
00181         if(snode->treetype==NTREE_SHADER) {
00182                 uiItemMenuF(layout, "Input", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_INPUT));
00183                 uiItemMenuF(layout, "Output", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OUTPUT));
00184                 uiItemMenuF(layout, "Color", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_COLOR));
00185                 uiItemMenuF(layout, "Vector", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_VECTOR));
00186                 uiItemMenuF(layout, "Convertor", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CONVERTOR));
00187                 uiItemMenuF(layout, "Group", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_GROUP));
00188                 uiItemMenuF(layout, "Dynamic", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_DYNAMIC));
00189         }
00190         else if(snode->treetype==NTREE_COMPOSIT) {
00191                 uiItemMenuF(layout, "Input", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_INPUT));
00192                 uiItemMenuF(layout, "Output", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OUTPUT));
00193                 uiItemMenuF(layout, "Color", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_COLOR));
00194                 uiItemMenuF(layout, "Vector", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_VECTOR));
00195                 uiItemMenuF(layout, "Filter", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_FILTER));
00196                 uiItemMenuF(layout, "Convertor", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CONVERTOR));
00197                 uiItemMenuF(layout, "Matte", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_MATTE));
00198                 uiItemMenuF(layout, "Distort", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_DISTORT));
00199                 uiItemMenuF(layout, "Group", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_GROUP));
00200         }
00201         else if(snode->treetype==NTREE_TEXTURE) {
00202                 uiItemMenuF(layout, "Input", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_INPUT));
00203                 uiItemMenuF(layout, "Output", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OUTPUT));
00204                 uiItemMenuF(layout, "Color", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_OP_COLOR));
00205                 uiItemMenuF(layout, "Patterns", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_PATTERN));
00206                 uiItemMenuF(layout, "Textures", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_TEXTURE));
00207                 uiItemMenuF(layout, "Convertor", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_CONVERTOR));
00208                 uiItemMenuF(layout, "Distort", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_DISTORT));
00209                 uiItemMenuF(layout, "Group", 0, node_auto_add_menu, SET_INT_IN_POINTER(NODE_CLASS_GROUP));
00210         }
00211 }
00212 
00213 void node_menus_register(void)
00214 {
00215         MenuType *mt;
00216 
00217         mt= MEM_callocN(sizeof(MenuType), "spacetype node menu add");
00218         strcpy(mt->idname, "NODE_MT_add");
00219         strcpy(mt->label, "Add");
00220         mt->draw= node_menu_add;
00221         WM_menutype_add(mt);
00222 }
00223