Merging r46725 through r46963 from trunk into soc-2011-tomato
[blender.git] / source / blender / editors / space_node / space_node.c
index 1047e90..e29f964 100644 (file)
@@ -1,6 +1,4 @@
 /*
- * $Id$
- *
  * ***** BEGIN GPL LICENSE BLOCK *****
  *
  * This program is free software; you can redistribute it and/or
 #include <string.h>
 #include <stdio.h>
 
+#include "DNA_lamp_types.h"
+#include "DNA_material_types.h"
 #include "DNA_node_types.h"
 #include "DNA_object_types.h"
-#include "DNA_material_types.h"
 #include "DNA_scene_types.h"
+#include "DNA_world_types.h"
 
 #include "MEM_guardedalloc.h"
 
@@ -72,13 +72,13 @@ ARegion *node_has_buttons_region(ScrArea *sa)
        ARegion *ar, *arnew;
 
        ar= BKE_area_find_region_type(sa, RGN_TYPE_UI);
-       if(ar) return ar;
+       if (ar) return ar;
        
        /* add subdiv level; after header */
        ar= BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
 
        /* is error! */
-       if(ar==NULL) return NULL;
+       if (ar==NULL) return NULL;
        
        arnew= MEM_callocN(sizeof(ARegion), "buttons for node");
        
@@ -125,15 +125,15 @@ static SpaceLink *node_new(const bContext *UNUSED(C))
        BLI_addtail(&snode->regionbase, ar);
        ar->regiontype= RGN_TYPE_WINDOW;
        
-       ar->v2d.tot.xmin=  -256.0f;
-       ar->v2d.tot.ymin=  -256.0f;
-       ar->v2d.tot.xmax= 768.0f;
-       ar->v2d.tot.ymax= 768.0f;
+       ar->v2d.tot.xmin =  -256.0f;
+       ar->v2d.tot.ymin =  -256.0f;
+       ar->v2d.tot.xmax = 768.0f;
+       ar->v2d.tot.ymax = 768.0f;
        
-       ar->v2d.cur.xmin=  -256.0f;
-       ar->v2d.cur.ymin=  -256.0f;
-       ar->v2d.cur.xmax= 768.0f;
-       ar->v2d.cur.ymax= 768.0f;
+       ar->v2d.cur.xmin =  -256.0f;
+       ar->v2d.cur.ymin =  -256.0f;
+       ar->v2d.cur.xmax = 768.0f;
+       ar->v2d.cur.ymax = 768.0f;
        
        ar->v2d.min[0]= 1.0f;
        ar->v2d.min[1]= 1.0f;
@@ -169,9 +169,10 @@ static void node_area_listener(ScrArea *sa, wmNotifier *wmn)
        /* note, ED_area_tag_refresh will re-execute compositor */
        SpaceNode *snode= sa->spacedata.first;
        int type= snode->treetype;
+       short shader_type = snode->shaderfrom;
        
        /* preview renders */
-       switch(wmn->category) {
+       switch (wmn->category) {
                case NC_SCENE:
                        switch (wmn->data) {
                                case ND_NODES:
@@ -182,8 +183,8 @@ static void node_area_listener(ScrArea *sa, wmNotifier *wmn)
                                        ED_area_tag_redraw(sa);
                                        break;
                                case ND_TRANSFORM_DONE:
-                                       if(type==NTREE_COMPOSIT) {
-                                               if(snode->flag & SNODE_AUTO_RENDER) {
+                                       if (type==NTREE_COMPOSIT) {
+                                               if (snode->flag & SNODE_AUTO_RENDER) {
                                                        snode->recalc= 1;
                                                        ED_area_tag_refresh(sa);
                                                }
@@ -192,37 +193,48 @@ static void node_area_listener(ScrArea *sa, wmNotifier *wmn)
                        }
                        break;
                case NC_WM:
-                       if(wmn->data==ND_FILEREAD)
+                       if (wmn->data==ND_FILEREAD)
                                ED_area_tag_refresh(sa);
                        break;
                
                /* future: add ID checks? */
                case NC_MATERIAL:
-                       if(type==NTREE_SHADER) {
-                               if(wmn->data==ND_SHADING)
+                       if (type==NTREE_SHADER) {
+                               if (wmn->data==ND_SHADING)
                                        ED_area_tag_refresh(sa);
-                               else if(wmn->data==ND_SHADING_DRAW)
+                               else if (wmn->data==ND_SHADING_DRAW)
                                        ED_area_tag_refresh(sa);
-                               else if(wmn->action==NA_ADDED && snode->edittree)
+                               else if (wmn->action==NA_ADDED && snode->edittree)
                                        nodeSetActiveID(snode->edittree, ID_MA, wmn->reference);
                                        
                        }
                        break;
                case NC_TEXTURE:
-                       if(type==NTREE_SHADER || type==NTREE_TEXTURE) {
-                               if(wmn->data==ND_NODES)
+                       if (type==NTREE_SHADER || type==NTREE_TEXTURE) {
+                               if (wmn->data==ND_NODES)
+                                       ED_area_tag_refresh(sa);
+                       }
+                       break;
+               case NC_WORLD:
+                       if (type==NTREE_SHADER && shader_type==SNODE_SHADER_WORLD) {
+                               ED_area_tag_refresh(sa);        
+                       }
+                       break;
+               case NC_OBJECT:
+                       if (type==NTREE_SHADER) {
+                               if (wmn->data==ND_OB_SHADING)
                                        ED_area_tag_refresh(sa);
                        }
                        break;
                case NC_TEXT:
                        /* pynodes */
-                       if(wmn->data==ND_SHADING)
+                       if (wmn->data==ND_SHADING)
                                ED_area_tag_refresh(sa);
                        break;
                case NC_SPACE:
-                       if(wmn->data==ND_SPACE_NODE)
+                       if (wmn->data==ND_SPACE_NODE)
                                ED_area_tag_refresh(sa);
-                       else if(wmn->data==ND_SPACE_NODE_VIEW)
+                       else if (wmn->data==ND_SPACE_NODE_VIEW)
                                ED_area_tag_redraw(sa);
                        break;
                case NC_NODE:
@@ -232,22 +244,27 @@ static void node_area_listener(ScrArea *sa, wmNotifier *wmn)
                                ED_area_tag_redraw(sa);
                        break;
                case NC_SCREEN:
-                       switch(wmn->data) {
+                       switch (wmn->data) {
                                case ND_ANIMPLAY:
                                        ED_area_tag_refresh(sa);
                                        break;
                        }
                        break;
+               case NC_MASK:
+                       if (wmn->action == NA_EDITED) {
+                               if (type==NTREE_COMPOSIT) {
+                                       ED_area_tag_refresh(sa);
+                               }
+                       }
+                       break;
 
                case NC_IMAGE:
                        if (wmn->action == NA_EDITED) {
-                               if(type==NTREE_COMPOSIT) {
-                                       Scene *scene= wmn->window->screen->scene;
-                                       
+                               if (type==NTREE_COMPOSIT) {
                                        /* note that nodeUpdateID is already called by BKE_image_signal() on all
                                         * scenes so really this is just to know if the images is used in the compo else
                                         * painting on images could become very slow when the compositor is open. */
-                                       if(nodeUpdateID(scene->nodetree, wmn->reference))
+                                       if (nodeUpdateID(snode->nodetree, wmn->reference))
                                                ED_area_tag_refresh(sa);
                                }
                        }
@@ -262,17 +279,29 @@ static void node_area_refresh(const struct bContext *C, struct ScrArea *sa)
 
        snode_set_context(snode, CTX_data_scene(C));
        
-       if(snode->nodetree) {
-               if(snode->treetype==NTREE_SHADER) {
-                       Material *ma= (Material *)snode->id;
-                       if(ma->use_nodes)
-                               ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
+       if (snode->nodetree) {
+               if (snode->treetype==NTREE_SHADER) {
+                       if (GS(snode->id->name) == ID_MA) {
+                               Material *ma= (Material *)snode->id;
+                               if (ma->use_nodes)
+                                       ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
+                       }
+                       else if (GS(snode->id->name) == ID_LA) {
+                               Lamp *la= (Lamp *)snode->id;
+                               if (la->use_nodes)
+                                       ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
+                       }
+                       else if (GS(snode->id->name) == ID_WO) {
+                               World *wo= (World *)snode->id;
+                               if (wo->use_nodes)
+                                       ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
+                       }
                }
-               else if(snode->treetype==NTREE_COMPOSIT) {
+               else if (snode->treetype==NTREE_COMPOSIT) {
                        Scene *scene= (Scene *)snode->id;
-                       if(scene->use_nodes) {
+                       if (scene->use_nodes) {
                                /* recalc is set on 3d view changes for auto compo */
-                               if(snode->recalc) {
+                               if (snode->recalc) {
                                        snode->recalc= 0;
                                        node_render_changed_exec((struct bContext*)C, NULL);
                                }
@@ -280,9 +309,9 @@ static void node_area_refresh(const struct bContext *C, struct ScrArea *sa)
                                        snode_composite_job(C, sa);
                        }
                }
-               else if(snode->treetype==NTREE_TEXTURE) {
+               else if (snode->treetype==NTREE_TEXTURE) {
                        Tex *tex= (Tex *)snode->id;
-                       if(tex->use_nodes) {
+                       if (tex->use_nodes) {
                                ED_preview_shader_job(C, sa, snode->id, NULL, NULL, 100, 100, PR_NODE_RENDER);
                        }
                }
@@ -308,7 +337,7 @@ static void node_buttons_area_init(wmWindowManager *wm, ARegion *ar)
 
        ED_region_panels_init(wm, ar);
        
-       keymap= WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
+       keymap = WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
        WM_event_add_keymap_handler(&ar->handlers, keymap);
 }
 
@@ -317,7 +346,18 @@ static void node_buttons_area_draw(const bContext *C, ARegion *ar)
        ED_region_panels(C, ar, 1, NULL, -1);
 }
 
-/* Initialise main area, setting handlers. */
+static void node_cursor(wmWindow *win, ScrArea *sa, ARegion *ar)
+{
+       SpaceNode *snode= sa->spacedata.first;
+       
+       /* convert mouse coordinates to v2d space */
+       UI_view2d_region_to_view(&ar->v2d, win->eventstate->x - ar->winrct.xmin, win->eventstate->y - ar->winrct.ymin,
+                                &snode->mx, &snode->my);
+       
+       node_set_cursor(win, snode);
+}
+
+/* Initialize main area, setting handlers. */
 static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
 {
        wmKeyMap *keymap;
@@ -326,10 +366,10 @@ static void node_main_area_init(wmWindowManager *wm, ARegion *ar)
        UI_view2d_region_reinit(&ar->v2d, V2D_COMMONVIEW_CUSTOM, ar->winx, ar->winy);
        
        /* own keymaps */
-       keymap= WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
+       keymap = WM_keymap_find(wm->defaultconf, "Node Generic", SPACE_NODE, 0);
        WM_event_add_keymap_handler(&ar->handlers, keymap);
        
-       keymap= WM_keymap_find(wm->defaultconf, "Node Editor", SPACE_NODE, 0);
+       keymap = WM_keymap_find(wm->defaultconf, "Node Editor", SPACE_NODE, 0);
        WM_event_add_keymap_handler_bb(&ar->handlers, keymap, &ar->v2d.mask, &ar->winrct);
        
        /* add drop boxes */
@@ -350,13 +390,13 @@ static void node_main_area_draw(const bContext *C, ARegion *ar)
 
 static int node_drop_poll(bContext *UNUSED(C), wmDrag *drag, wmEvent *UNUSED(event))
 {
-       if(drag->type==WM_DRAG_ID) {
+       if (drag->type==WM_DRAG_ID) {
                ID *id= (ID *)drag->poin;
-               if( GS(id->name)==ID_IM )
+               if ( GS(id->name)==ID_IM )
                        return 1;
        }
-       else if(drag->type==WM_DRAG_PATH){
-               if(ELEM(drag->icon, 0, ICON_FILE_IMAGE))        /* rule might not work? */
+       else if (drag->type==WM_DRAG_PATH) {
+               if (ELEM(drag->icon, 0, ICON_FILE_IMAGE))       /* rule might not work? */
                        return 1;
        }
        return 0;
@@ -366,7 +406,7 @@ static void node_id_path_drop_copy(wmDrag *drag, wmDropBox *drop)
 {
        ID *id= (ID *)drag->poin;
        
-       if(id) {
+       if (id) {
                RNA_string_set(drop->ptr, "name", id->name+2);
        }
        if (drag->path[0]) {
@@ -407,13 +447,13 @@ static void node_header_area_draw(const bContext *C, ARegion *ar)
 static void node_region_listener(ARegion *ar, wmNotifier *wmn)
 {
        /* context changes */
-       switch(wmn->category) {
+       switch (wmn->category) {
                case NC_SPACE:
-                       if(wmn->data==ND_SPACE_NODE)
+                       if (wmn->data==ND_SPACE_NODE)
                                ED_region_tag_redraw(ar);
                        break;
                case NC_SCREEN:
-                       if(wmn->data == ND_GPENCIL)     
+                       if (wmn->data == ND_GPENCIL)    
                                ED_region_tag_redraw(ar);
                        break;
                case NC_SCENE:
@@ -422,8 +462,12 @@ static void node_region_listener(ARegion *ar, wmNotifier *wmn)
                case NC_NODE:
                        ED_region_tag_redraw(ar);
                        break;
+               case NC_OBJECT:
+                       if (wmn->data==ND_OB_SHADING)
+                               ED_region_tag_redraw(ar);
+                       break;
                case NC_ID:
-                       if(wmn->action == NA_RENAME)
+                       if (wmn->action == NA_RENAME)
                                ED_region_tag_redraw(ar);
                        break;
        }
@@ -435,16 +479,16 @@ static int node_context(const bContext *C, const char *member, bContextDataResul
 {
        SpaceNode *snode= CTX_wm_space_node(C);
        
-       if(CTX_data_dir(member)) {
+       if (CTX_data_dir(member)) {
                CTX_data_dir_set(result, node_context_dir);
                return 1;
        }
-       else if(CTX_data_equals(member, "selected_nodes")) {
+       else if (CTX_data_equals(member, "selected_nodes")) {
                bNode *node;
                
-               if(snode->edittree) {
-                       for(node=snode->edittree->nodes.last; node; node=node->prev) {
-                               if(node->flag & NODE_SELECT) {
+               if (snode->edittree) {
+                       for (node=snode->edittree->nodes.last; node; node=node->prev) {
+                               if (node->flag & NODE_SELECT) {
                                        CTX_data_list_add(result, &snode->edittree->id, &RNA_Node, node);
                                }
                        }
@@ -452,12 +496,12 @@ static int node_context(const bContext *C, const char *member, bContextDataResul
                CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
                return 1;
        }
-       else if(CTX_data_equals(member, "active_node")) {
+       else if (CTX_data_equals(member, "active_node")) {
                bNode *node;
                
-               if(snode->edittree) {
-                       for(node=snode->edittree->nodes.last; node; node=node->prev) {
-                               if(node->flag & NODE_ACTIVE) {
+               if (snode->edittree) {
+                       for (node=snode->edittree->nodes.last; node; node=node->prev) {
+                               if (node->flag & NODE_ACTIVE) {
                                        CTX_data_pointer_set(result, &snode->edittree->id, &RNA_Node, node);
                                        break;
                                }
@@ -496,6 +540,8 @@ void ED_spacetype_node(void)
        art->init= node_main_area_init;
        art->draw= node_main_area_draw;
        art->listener= node_region_listener;
+       art->cursor = node_cursor;
+       art->event_cursor = TRUE;
        art->keymapflag= ED_KEYMAP_UI|ED_KEYMAP_VIEW2D|ED_KEYMAP_FRAMES|ED_KEYMAP_GPENCIL;
 
        BLI_addhead(&st->regiontypes, art);