Code cleanup: use bool for static methods
[blender.git] / source / blender / editors / space_node / node_buttons.c
index 1b5f241738402b40972b1bb0798cbe1c29a798c3..035287c0411cf17333ef363aee68b5185d385cd4 100644 (file)
  *  \ingroup spnode
  */
 
-
-#include <string.h>
-#include <stdio.h>
-#include <math.h>
-#include <float.h>
-
 #include "MEM_guardedalloc.h"
 
 #include "DNA_node_types.h"
-#include "DNA_scene_types.h"
 
 #include "BLI_math.h"
 #include "BLI_blenlib.h"
-#include "BLI_rand.h"
-#include "BLI_utildefines.h"
+
+#include "BLF_translation.h"
 
 #include "BKE_context.h"
 #include "BKE_global.h"
 #include "ED_gpencil.h"
 #include "ED_screen.h"
 
-#include "UI_interface.h"
 #include "UI_resources.h"
 
-#include "node_intern.h"       // own include
+#include "node_intern.h"  /* own include */
 
 
 /* ******************* node space & buttons ************** */
-#define B_NOP          1
-#define B_REDR         2
 
-static void do_node_region_buttons(bContext *C, void *UNUSED(arg), int event)
+/* poll for active nodetree */
+static int active_nodetree_poll(const bContext *C, PanelType *UNUSED(pt))
 {
-       //SpaceNode *snode= CTX_wm_space_node(C);
+       SpaceNode *snode = CTX_wm_space_node(C);
        
-       switch(event) {
-       case B_REDR:
-               ED_area_tag_redraw(CTX_wm_area(C));
-               return; /* no notifier! */
-       }
-}
-
-/* poll callback for active node */
-static int active_node_poll(const bContext *C, PanelType *UNUSED(pt))
-{
-       SpaceNode *snode= CTX_wm_space_node(C);
-       
-       // TODO: include check for whether there is an active node...
        return (snode && snode->nodetree);
 }
 
-/* active node */
-static void active_node_panel(const bContext *C, Panel *pa)
-{
-       SpaceNode *snode= CTX_wm_space_node(C);
-       bNodeTree *ntree= (snode) ? snode->edittree : NULL;
-       bNode *node = (ntree) ? nodeGetActive(ntree) : NULL; // xxx... for editing group nodes
-       uiLayout *layout= pa->layout;
-       uiBlock *block;
-       PointerRNA ptr;
-       
-       /* verify pointers, and create RNA pointer for the node */
-       if ELEM(NULL, ntree, node)
-               return;
-       //if (node->id) /* for group nodes */
-       //      RNA_pointer_create(node->id, &RNA_Node, node, &ptr);
-       //else
-               RNA_pointer_create(&ntree->id, &RNA_Node, node, &ptr); 
-       
-       /* set update callback */
-       // xxx is this really needed
-       block= uiLayoutGetBlock(layout);
-       uiBlockSetHandleFunc(block, do_node_region_buttons, NULL);
-       
-       /* draw this node's name, etc. */
-       uiItemR(layout, &ptr, "label", 0, NULL, ICON_NODE);
-       uiItemS(layout);
-       uiItemR(layout, &ptr, "name", 0, NULL, ICON_NODE);
-       uiItemS(layout);
-       
-       uiItemO(layout, NULL, 0, "NODE_OT_hide_socket_toggle");
-       uiItemS(layout);
-
-       /* draw this node's settings */
-       if (node->typeinfo && node->typeinfo->uifuncbut)
-               node->typeinfo->uifuncbut(layout, (bContext *)C, &ptr);
-       else if (node->typeinfo && node->typeinfo->uifunc)
-               node->typeinfo->uifunc(layout, (bContext *)C, &ptr);
-}
-
 static int node_sockets_poll(const bContext *C, PanelType *UNUSED(pt))
 {
-       SpaceNode *snode= CTX_wm_space_node(C);
+       SpaceNode *snode = CTX_wm_space_node(C);
        
-       return (snode && snode->nodetree && G.rt == 777);
+       return (snode && snode->nodetree && G.debug_value == 777);
 }
 
 static void node_sockets_panel(const bContext *C, Panel *pa)
 {
-       SpaceNode *snode= CTX_wm_space_node(C);
-       bNodeTree *ntree= (snode) ? snode->edittree : NULL;
+       SpaceNode *snode = CTX_wm_space_node(C);
+       bNodeTree *ntree = (snode) ? snode->edittree : NULL;
        bNode *node = (ntree) ? nodeGetActive(ntree) : NULL;
        bNodeSocket *sock;
-       uiLayout *layout= pa->layout, *split;
+       uiLayout *layout = pa->layout, *split;
        char name[UI_MAX_NAME_STR];
        
-       if(ELEM(NULL, ntree, node))
+       if (ELEM(NULL, ntree, node))
                return;
        
-       for(sock=node->inputs.first; sock; sock=sock->next) {
+       for (sock = node->inputs.first; sock; sock = sock->next) {
                BLI_snprintf(name, sizeof(name), "%s:", sock->name);
 
-               split = uiLayoutSplit(layout, 0.35f, 0);
+               split = uiLayoutSplit(layout, 0.35f, FALSE);
                uiItemL(split, name, ICON_NONE);
                uiTemplateNodeLink(split, ntree, node, sock);
        }
 }
 
+static int node_tree_interface_poll(const bContext *C, PanelType *UNUSED(pt))
+{
+       SpaceNode *snode = CTX_wm_space_node(C);
+       
+       return (snode && snode->edittree && (snode->edittree->inputs.first || snode->edittree->outputs.first));
+}
+
+static bool node_tree_find_active_socket(bNodeTree *ntree, bNodeSocket **r_sock, int *r_in_out)
+{
+       bNodeSocket *sock;
+       for (sock = ntree->inputs.first; sock; sock = sock->next) {
+               if (sock->flag & SELECT) {
+                       *r_sock = sock;
+                       *r_in_out = SOCK_IN;
+                       return TRUE;
+               }
+       }
+       for (sock = ntree->outputs.first; sock; sock = sock->next) {
+               if (sock->flag & SELECT) {
+                       *r_sock = sock;
+                       *r_in_out = SOCK_OUT;
+                       return TRUE;
+               }
+       }
+       
+       *r_sock = NULL;
+       *r_in_out = 0;
+       return FALSE;
+}
+
+static void node_tree_interface_panel(const bContext *C, Panel *pa)
+{
+       SpaceNode *snode = CTX_wm_space_node(C);
+       bNodeTree *ntree = (snode) ? snode->edittree : NULL;
+       bNodeSocket *sock;
+       int in_out;
+       uiLayout *layout = pa->layout, *row, *split, *col;
+       PointerRNA ptr, sockptr, opptr;
+
+       if (!ntree)
+               return;
+       
+       RNA_id_pointer_create((ID *)ntree, &ptr);
+       
+       node_tree_find_active_socket(ntree, &sock, &in_out);
+       RNA_pointer_create((ID *)ntree, &RNA_NodeSocketInterface, sock, &sockptr);
+       
+       row = uiLayoutRow(layout, FALSE);
+       
+       split = uiLayoutRow(row, TRUE);
+       col = uiLayoutColumn(split, TRUE);
+       uiItemL(col, IFACE_("Inputs:"), ICON_NONE);
+       uiTemplateList(col, (bContext *)C, "NODE_UL_interface_sockets", "inputs", &ptr, "inputs", &ptr, "active_input",
+                      0, 0, 0, 0);
+       opptr = uiItemFullO(col, "NODE_OT_tree_socket_add", "", ICON_PLUS, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
+       RNA_enum_set(&opptr, "in_out", SOCK_IN);
+       
+       col = uiLayoutColumn(split, TRUE);
+       uiItemL(col, IFACE_("Outputs:"), ICON_NONE);
+       uiTemplateList(col, (bContext *)C, "NODE_UL_interface_sockets", "outputs", &ptr, "outputs", &ptr, "active_output",
+                      0, 0, 0, 0);
+       opptr = uiItemFullO(col, "NODE_OT_tree_socket_add", "", ICON_PLUS, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
+       RNA_enum_set(&opptr, "in_out", SOCK_OUT);
+       
+       col = uiLayoutColumn(row, TRUE);
+       opptr = uiItemFullO(col, "NODE_OT_tree_socket_move", "", ICON_TRIA_UP, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
+       RNA_enum_set(&opptr, "direction", 1);
+       opptr = uiItemFullO(col, "NODE_OT_tree_socket_move", "", ICON_TRIA_DOWN, NULL, WM_OP_EXEC_DEFAULT, UI_ITEM_O_RETURN_PROPS);
+       RNA_enum_set(&opptr, "direction", 2);
+       
+       if (sock) {
+               row = uiLayoutRow(layout, TRUE);
+               uiItemR(row, &sockptr, "name", 0, NULL, ICON_NONE);
+               uiItemO(row, "", ICON_X, "NODE_OT_tree_socket_remove");
+               
+               if (sock->typeinfo->interface_draw) {
+                       uiItemS(layout);
+                       sock->typeinfo->interface_draw((bContext *)C, layout, &sockptr);
+               }
+       }
+}
+
 /* ******************* node buttons registration ************** */
 
 void node_buttons_register(ARegionType *art)
 {
        PanelType *pt;
        
-       pt= MEM_callocN(sizeof(PanelType), "spacetype node panel active node");
-       strcpy(pt->idname, "NODE_PT_item");
-       strcpy(pt->label, "Active Node");
-       pt->draw= active_node_panel;
-       pt->poll= active_node_poll;
-       BLI_addtail(&art->paneltypes, pt);
-
-       pt= MEM_callocN(sizeof(PanelType), "spacetype node panel node sockets");
+       pt = MEM_callocN(sizeof(PanelType), "spacetype node panel node sockets");
        strcpy(pt->idname, "NODE_PT_sockets");
-       strcpy(pt->label, "Sockets");
-       pt->draw= node_sockets_panel;
-       pt->poll= node_sockets_poll;
+       strcpy(pt->label, N_("Sockets"));
+       strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
+       pt->draw = node_sockets_panel;
+       pt->poll = node_sockets_poll;
        pt->flag |= PNL_DEFAULT_CLOSED;
        BLI_addtail(&art->paneltypes, pt);
-       
-       pt= MEM_callocN(sizeof(PanelType), "spacetype node panel gpencil");
+
+       pt = MEM_callocN(sizeof(PanelType), "spacetype node panel tree interface");
+       strcpy(pt->idname, "NODE_PT_node_tree_interface");
+       strcpy(pt->label, N_("Interface"));
+       strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
+       pt->draw = node_tree_interface_panel;
+       pt->poll = node_tree_interface_poll;
+       BLI_addtail(&art->paneltypes, pt);
+
+       pt = MEM_callocN(sizeof(PanelType), "spacetype node panel gpencil");
        strcpy(pt->idname, "NODE_PT_gpencil");
-       strcpy(pt->label, "Grease Pencil");
-       pt->draw= gpencil_panel_standard;
+       strcpy(pt->label, N_("Grease Pencil"));
+       strcpy(pt->translation_context, BLF_I18NCONTEXT_DEFAULT_BPYRNA);
+       pt->draw_header = gpencil_panel_standard_header;
+       pt->draw = gpencil_panel_standard;
+       pt->poll = active_nodetree_poll;
        BLI_addtail(&art->paneltypes, pt);
 }
 
-static int node_properties(bContext *C, wmOperator *UNUSED(op))
+static int node_properties_toggle_exec(bContext *C, wmOperator *UNUSED(op))
 {
-       ScrArea *sa= CTX_wm_area(C);
-       ARegion *ar= node_has_buttons_region(sa);
+       ScrArea *sa = CTX_wm_area(C);
+       ARegion *ar = node_has_buttons_region(sa);
        
-       if(ar)
+       if (ar)
                ED_region_toggle_hidden(C, ar);
 
        return OPERATOR_FINISHED;
@@ -196,19 +222,19 @@ static int node_properties(bContext *C, wmOperator *UNUSED(op))
 /* non-standard poll operator which doesn't care if there are any nodes */
 static int node_properties_poll(bContext *C)
 {
-       ScrArea *sa= CTX_wm_area(C);
+       ScrArea *sa = CTX_wm_area(C);
        return (sa && (sa->spacetype == SPACE_NODE));
 }
 
 void NODE_OT_properties(wmOperatorType *ot)
 {
-       ot->name= "Properties";
-       ot->description= "Toggles the properties panel display";
-       ot->idname= "NODE_OT_properties";
+       ot->name = "Properties";
+       ot->description = "Toggles the properties panel display";
+       ot->idname = "NODE_OT_properties";
        
-       ot->exec= node_properties;
-       ot->poll= node_properties_poll;
+       ot->exec = node_properties_toggle_exec;
+       ot->poll = node_properties_poll;
        
        /* flags */
-       ot->flag= 0;
+       ot->flag = 0;
 }