Workspace: Move engines to workspace and Properties Editor cleanup
[blender.git] / source / blender / editors / space_node / node_templates.c
index 0cbb7ea0220d8c6f4aedbbf043f54914417c26ff..c791b9f6eae40d2c69b61b09849bc4db8b35c2de 100644 (file)
@@ -32,6 +32,7 @@
 #include "DNA_node_types.h"
 #include "DNA_screen_types.h"
 
+#include "BLI_array.h"
 #include "BLI_listbase.h"
 #include "BLI_string.h"
 
@@ -420,6 +421,27 @@ static int ui_compatible_sockets(int typeA, int typeB)
        return (typeA == typeB);
 }
 
+static int ui_node_item_name_compare(const void *a, const void *b)
+{
+       const bNodeType *type_a = *(const bNodeType **)a;
+       const bNodeType *type_b = *(const bNodeType **)b;
+       return BLI_natstrcmp(type_a->ui_name, type_b->ui_name);
+}
+
+static bool ui_node_item_special_poll(const bNodeTree *UNUSED(ntree),
+                                      const bNodeType *ntype)
+{
+       if (STREQ(ntype->idname, "ShaderNodeUVAlongStroke")) {
+               /* TODO(sergey): Currently we don't have Freestyle nodes edited from
+                * the buttons context, so can ignore it's nodes completely.
+                *
+                * However, we might want to do some extra checks here later.
+                */
+               return false;
+       }
+       return true;
+}
+
 static void ui_node_menu_column(NodeLinkArg *arg, int nclass, const char *cname)
 {
        bNodeTree *ntree = arg->ntree;
@@ -439,7 +461,32 @@ static void ui_node_menu_column(NodeLinkArg *arg, int nclass, const char *cname)
                        compatibility = NODE_OLD_SHADING;
        }
 
+       /* generate array of node types sorted by UI name */
+       bNodeType **sorted_ntypes = NULL;
+       BLI_array_declare(sorted_ntypes);
+
        NODE_TYPES_BEGIN(ntype) {
+               if (compatibility && !(ntype->compatibility & compatibility)) {
+                       continue;
+               }
+
+               if (ntype->nclass != nclass) {
+                       continue;
+               }
+
+               if (!ui_node_item_special_poll(ntree, ntype)) {
+                       continue;
+               }
+
+               BLI_array_append(sorted_ntypes, ntype);
+       }
+       NODE_TYPES_END
+
+       qsort(sorted_ntypes, BLI_array_count(sorted_ntypes), sizeof(bNodeType *), ui_node_item_name_compare);
+
+       /* generate UI */
+       for (int j = 0; j < BLI_array_count(sorted_ntypes); j++) {
+               bNodeType *ntype = sorted_ntypes[j];
                NodeLinkItem *items;
                int totitems;
                char name[UI_MAX_NAME_STR];
@@ -447,12 +494,6 @@ static void ui_node_menu_column(NodeLinkArg *arg, int nclass, const char *cname)
                int i, num = 0;
                int icon = ICON_NONE;
                
-               if (compatibility && !(ntype->compatibility & compatibility))
-                       continue;
-               
-               if (ntype->nclass != nclass)
-                       continue;
-               
                arg->node_type = ntype;
                
                ui_node_link_items(arg, SOCK_OUT, &items, &totitems);
@@ -502,7 +543,8 @@ static void ui_node_menu_column(NodeLinkArg *arg, int nclass, const char *cname)
                if (items)
                        MEM_freeN(items);
        }
-       NODE_TYPES_END
+
+       BLI_array_free(sorted_ntypes);
 }
 
 static void node_menu_column_foreach_cb(void *calldata, int nclass, const char *name)
@@ -517,6 +559,7 @@ static void ui_template_node_link_menu(bContext *C, uiLayout *layout, void *but_
 {
        Main *bmain = CTX_data_main(C);
        Scene *scene = CTX_data_scene(C);
+       ViewRender *view_render = CTX_data_view_render(C);
        uiBlock *block = uiLayoutGetBlock(layout);
        uiBut *but = (uiBut *)but_p;
        uiLayout *split, *column;
@@ -533,7 +576,7 @@ static void ui_template_node_link_menu(bContext *C, uiLayout *layout, void *but_
        arg->layout = split;
 
        if (ntreetype && ntreetype->foreach_nodeclass)
-               ntreetype->foreach_nodeclass(scene, arg, node_menu_column_foreach_cb);
+               ntreetype->foreach_nodeclass(view_render, arg, node_menu_column_foreach_cb);
 
        column = uiLayoutColumn(split, false);
        UI_block_layout_set_current(block, column);
@@ -641,10 +684,11 @@ static void ui_node_draw_input(uiLayout *layout, bContext *C, bNodeTree *ntree,
        RNA_pointer_create(&ntree->id, &RNA_Node, node, &nodeptr);
 
        /* indented label */
-       for (i = 0; i < indent; i++)
+       for (i = 0; i < indent; i++) {
                label[i] = ' ';
+       }
        label[indent] = '\0';
-       BLI_snprintf(label, UI_MAX_NAME_STR, "%s%s:", label, IFACE_(input->name));
+       BLI_snprintf(label + indent, UI_MAX_NAME_STR - indent, "%s:", IFACE_(input->name));
 
        /* split in label and value */
        split = uiLayoutSplit(layout, 0.35f, false);