Blender  V2.59
node_state.c
Go to the documentation of this file.
00001 /*
00002  * $Id: node_state.c 36790 2011-05-20 07:40:05Z campbellbarton $
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, Nathan Letwory
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #include <stdio.h>
00035 
00036 #include "DNA_node_types.h"
00037 #include "DNA_scene_types.h"
00038 
00039 #include "BLI_rect.h"
00040 #include "BLI_utildefines.h"
00041 
00042 #include "BKE_context.h"
00043 #include "BKE_node.h"
00044 
00045 #include "ED_screen.h"
00046 
00047 #include "RNA_access.h"
00048 #include "RNA_define.h"
00049 
00050 #include "WM_api.h"
00051 #include "WM_types.h"
00052 
00053 #include "UI_view2d.h"
00054  
00055 #include "node_intern.h"
00056 
00057 /* **************** Node Header Buttons ************** */
00058 
00059 /* note: call node_tree_verify_groups(snode->nodetree) after this
00060  */
00061 void node_set_hidden_sockets(SpaceNode *snode, bNode *node, int set)
00062 {       
00063         bNodeSocket *sock;
00064 
00065         if(set==0) {
00066                 for(sock= node->inputs.first; sock; sock= sock->next)
00067                         sock->flag &= ~SOCK_HIDDEN;
00068                 for(sock= node->outputs.first; sock; sock= sock->next)
00069                         sock->flag &= ~SOCK_HIDDEN;
00070         }
00071         else {
00072                 bNode *gnode= node_tree_get_editgroup(snode->nodetree);
00073 
00074                 /* hiding inside group should not break links in other group users */
00075                 if(gnode) {
00076                         nodeGroupSocketUseFlags((bNodeTree *)gnode->id);
00077                         for(sock= node->inputs.first; sock; sock= sock->next)
00078                                 if(!(sock->flag & SOCK_IN_USE))
00079                                         if(sock->link==NULL)
00080                                                 sock->flag |= SOCK_HIDDEN;
00081                         for(sock= node->outputs.first; sock; sock= sock->next)
00082                                 if(!(sock->flag & SOCK_IN_USE))
00083                                         if(nodeCountSocketLinks(snode->edittree, sock)==0)
00084                                                 sock->flag |= SOCK_HIDDEN;
00085                 }
00086                 else {
00087                         /* hide unused sockets */
00088                         for(sock= node->inputs.first; sock; sock= sock->next) {
00089                                 if(sock->link==NULL)
00090                                         sock->flag |= SOCK_HIDDEN;
00091                         }
00092                         for(sock= node->outputs.first; sock; sock= sock->next) {
00093                                 if(nodeCountSocketLinks(snode->edittree, sock)==0)
00094                                         sock->flag |= SOCK_HIDDEN;
00095                         }
00096                 }
00097         }
00098 }
00099 
00100 static void node_hide_unhide_sockets(SpaceNode *snode, bNode *node)
00101 {
00102         node_set_hidden_sockets(snode, node, !node_has_hidden_sockets(node));
00103         node_tree_verify_groups(snode->nodetree);
00104 }
00105 
00106 static int do_header_node(SpaceNode *snode, bNode *node, float mx, float my)
00107 {
00108         rctf totr= node->totr;
00109         
00110         totr.ymin= totr.ymax-20.0f;
00111         
00112         totr.xmax= totr.xmin+15.0f;
00113         if(BLI_in_rctf(&totr, mx, my)) {
00114                 node->flag |= NODE_HIDDEN;
00115                 return 1;
00116         }       
00117         
00118         totr.xmax= node->totr.xmax;
00119         totr.xmin= totr.xmax-18.0f;
00120         if(node->typeinfo->flag & NODE_PREVIEW) {
00121                 if(BLI_in_rctf(&totr, mx, my)) {
00122                         node->flag ^= NODE_PREVIEW;
00123                         return 1;
00124                 }
00125                 totr.xmin-=15.0f;
00126         }
00127         if(node->type == NODE_GROUP) {
00128                 if(BLI_in_rctf(&totr, mx, my)) {
00129                         snode_make_group_editable(snode, node);
00130                         return 1;
00131                 }
00132                 totr.xmin-=15.0f;
00133         }
00134         if(node->typeinfo->flag & NODE_OPTIONS) {
00135                 if(BLI_in_rctf(&totr, mx, my)) {
00136                         node->flag ^= NODE_OPTIONS;
00137                         return 1;
00138                 }
00139                 totr.xmin-=15.0f;
00140         }
00141         /* hide unused sockets */
00142         if(BLI_in_rctf(&totr, mx, my)) {
00143                 node_hide_unhide_sockets(snode, node);
00144         }
00145         
00146         return 0;
00147 }
00148 
00149 static int do_header_hidden_node(bNode *node, float mx, float my)
00150 {
00151         rctf totr= node->totr;
00152         
00153         totr.xmax= totr.xmin+15.0f;
00154         if(BLI_in_rctf(&totr, mx, my)) {
00155                 node->flag &= ~NODE_HIDDEN;
00156                 return 1;
00157         }       
00158         return 0;
00159 }
00160 
00161 static int node_toggle_visibility(SpaceNode *snode, ARegion *ar, const int mval[2])
00162 {
00163         bNode *node;
00164         float mx, my;
00165         
00166         mx= (float)mval[0];
00167         my= (float)mval[1];
00168         
00169         UI_view2d_region_to_view(&ar->v2d, mval[0], mval[1], &mx, &my);
00170         
00171         for(next_node(snode->edittree); (node=next_node(NULL));) {
00172                 if(node->flag & NODE_HIDDEN) {
00173                         if(do_header_hidden_node(node, mx, my)) {
00174                                 ED_region_tag_redraw(ar);
00175                                 return 1;
00176                         }
00177                 }
00178                 else {
00179                         if(do_header_node(snode, node, mx, my)) {
00180                                 ED_region_tag_redraw(ar);
00181                                 return 1;
00182                         }
00183                 }
00184         }
00185         return 0;
00186 }
00187 
00188 static int node_toggle_visibility_exec(bContext *C, wmOperator *op)
00189 {
00190         SpaceNode *snode= CTX_wm_space_node(C);
00191         ARegion *ar= CTX_wm_region(C);
00192         int mval[2];
00193 
00194         mval[0] = RNA_int_get(op->ptr, "mouse_x");
00195         mval[1] = RNA_int_get(op->ptr, "mouse_y");
00196         if(node_toggle_visibility(snode, ar, mval))
00197                 return OPERATOR_FINISHED;
00198         else
00199                 return OPERATOR_CANCELLED|OPERATOR_PASS_THROUGH;
00200 }
00201 
00202 static int node_toggle_visibility_invoke(bContext *C, wmOperator *op, wmEvent *event)
00203 {
00204         RNA_int_set(op->ptr, "mouse_x", event->mval[0]);
00205         RNA_int_set(op->ptr, "mouse_y", event->mval[1]);
00206 
00207         return node_toggle_visibility_exec(C,op);
00208 }
00209 
00210 void NODE_OT_visibility_toggle(wmOperatorType *ot)
00211 {
00212         /* identifiers */
00213         ot->name= "Toggle Visibility";
00214         ot->idname= "NODE_OT_visibility_toggle";
00215         ot->description= "Handle clicks on node header buttons";
00216         
00217         /* api callbacks */
00218         ot->invoke= node_toggle_visibility_invoke;
00219         ot->poll= ED_operator_node_active;
00220         
00221         /* flags */
00222         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00223         
00224         RNA_def_int(ot->srna, "mouse_x", 0, INT_MIN, INT_MAX, "Mouse X", "", INT_MIN, INT_MAX);
00225         RNA_def_int(ot->srna, "mouse_y", 0, INT_MIN, INT_MAX, "Mouse Y", "", INT_MIN, INT_MAX);
00226 }
00227 
00228 /* **************** View All Operator ************** */
00229 
00230 static void snode_home(ScrArea *UNUSED(sa), ARegion *ar, SpaceNode* snode)
00231 {
00232         bNode *node;
00233         rctf *cur, *tot;
00234         float oldwidth, oldheight, width, height;
00235         int first= 1;
00236         
00237         cur= &ar->v2d.cur;
00238         tot= &ar->v2d.tot;
00239         
00240         oldwidth= cur->xmax - cur->xmin;
00241         oldheight= cur->ymax - cur->ymin;
00242         
00243         cur->xmin= cur->ymin= 0.0f;
00244         cur->xmax=ar->winx;
00245         cur->ymax=ar->winy;
00246         
00247         if(snode->edittree) {
00248                 for(node= snode->edittree->nodes.first; node; node= node->next) {
00249                         if(first) {
00250                                 first= 0;
00251                                 ar->v2d.cur= node->totr;
00252                         }
00253                         else {
00254                                 BLI_union_rctf(cur, &node->totr);
00255                         }
00256                 }
00257         }
00258         
00259         snode->xof= 0;
00260         snode->yof= 0;
00261         width= cur->xmax - cur->xmin;
00262         height= cur->ymax- cur->ymin;
00263 
00264         if(width > height) {
00265                 float newheight;
00266                 newheight= oldheight * width/oldwidth;
00267                 cur->ymin= cur->ymin - newheight/4;
00268                 cur->ymax= cur->ymax + newheight/4;
00269         }
00270         else {
00271                 float newwidth;
00272                 newwidth= oldwidth * height/oldheight;
00273                 cur->xmin= cur->xmin - newwidth/4;
00274                 cur->xmax= cur->xmax + newwidth/4;
00275         }
00276 
00277         ar->v2d.tot= ar->v2d.cur;
00278         UI_view2d_curRect_validate(&ar->v2d);
00279 }
00280 
00281 static int node_view_all_exec(bContext *C, wmOperator *UNUSED(op))
00282 {
00283         ScrArea *sa= CTX_wm_area(C);
00284         ARegion *ar= CTX_wm_region(C);
00285         SpaceNode *snode= CTX_wm_space_node(C);
00286         
00287         snode_home(sa, ar, snode);
00288         ED_region_tag_redraw(ar);
00289         
00290         return OPERATOR_FINISHED;
00291 }
00292 
00293 void NODE_OT_view_all(wmOperatorType *ot)
00294 {
00295         /* identifiers */
00296         ot->name= "View All";
00297         ot->idname= "NODE_OT_view_all";
00298         ot->description= "Resize view so you can see all nodes";
00299         
00300         /* api callbacks */
00301         ot->exec= node_view_all_exec;
00302         ot->poll= ED_operator_node_active;
00303         
00304         /* flags */
00305         ot->flag= OPTYPE_REGISTER|OPTYPE_UNDO;
00306 }