Exemple #1
0
        protected void SetParentNodeVars(Graph g)
        {
            try
            {
                if (g == null || parentNode == null)
                {
                    return;
                }

                var props = parentNode.GetType().GetProperties();

                var p = g;

                if (p != null)
                {
                    int count = props.Length;
                    for (int i = 0; i < count; i++)
                    {
                        var prop = props[i];
                        PromoteAttribute  promote  = prop.GetCustomAttribute <PromoteAttribute>();
                        EditableAttribute editable = prop.GetCustomAttribute <EditableAttribute>();

                        if (editable == null)
                        {
                            continue;
                        }

                        object v = null;

                        NodeType pType = NodeType.Float;

                        if (p.HasParameterValue(parentNode.Id, prop.Name))
                        {
                            var gp = p.GetParameterRaw(parentNode.Id, prop.Name);
                            if (!gp.IsFunction())
                            {
                                v     = gp.Value;
                                pType = gp.Type;
                            }
                            else
                            {
                                continue;
                            }
                        }
                        else
                        {
                            v = prop.GetValue(parentNode);

                            if (promote != null)
                            {
                                pType = promote.ExpectedType;
                            }
                            else
                            {
                                if (Helpers.Utils.IsNumber(v))
                                {
                                    pType = NodeType.Float;
                                }
                                else if (v != null && v is MVector)
                                {
                                    pType = NodeType.Float2 | NodeType.Float3 | NodeType.Float4 | NodeType.Gray | NodeType.Color;
                                }
                                else if (v != null && v is Vector4)
                                {
                                    pType = NodeType.Float4;
                                }
                                else if (v != null && v is bool)
                                {
                                    pType = NodeType.Bool;
                                }
                                else
                                {
                                    //do not add it
                                    continue;
                                }
                            }
                        }


                        if (v != null)
                        {
                            if (v is Vector4)
                            {
                                Vector4 vec = (Vector4)v;
                                v = new MVector(vec.X, vec.Y, vec.Z, vec.W);
                            }

                            SetVar(prop.Name, v, pType);
                        }
                    }
                }
                else
                {
                    int count = props.Length;
                    for (int i = 0; i < count; i++)
                    {
                        NodeType          pType    = NodeType.Float;
                        var               prop     = props[i];
                        PromoteAttribute  promote  = prop.GetCustomAttribute <PromoteAttribute>();
                        EditableAttribute editable = prop.GetCustomAttribute <EditableAttribute>();

                        if (editable == null)
                        {
                            continue;
                        }

                        object v = prop.GetValue(parentNode);


                        if (promote != null)
                        {
                            pType = promote.ExpectedType;
                        }
                        else
                        {
                            if (Helpers.Utils.IsNumber(v))
                            {
                                pType = NodeType.Float;
                            }
                            else if (v != null && v is MVector)
                            {
                                pType = NodeType.Float2 | NodeType.Float3 | NodeType.Float4 | NodeType.Gray | NodeType.Color;
                            }
                            else if (v != null && v is Vector4)
                            {
                                pType = NodeType.Float4;
                            }
                            else if (v != null && v is bool)
                            {
                                pType = NodeType.Bool;
                            }
                            else
                            {
                                //do not add
                                continue;
                            }
                        }


                        if (v != null)
                        {
                            if (v is Vector4)
                            {
                                Vector4 vec = (Vector4)v;
                                v = new MVector(vec.X, vec.Y, vec.Z, vec.W);
                            }

                            SetVar(prop.Name, v, pType);
                        }
                    }
                }
            }
            catch (StackOverflowException e)
            {
                Log.Error(e);
                //stackoverflow possible if you do a loop of function parameter values
                Log.Error("There is an infinite function reference loop between node parameters");
            }
        }
        void CreateUIElement(Type t, PropertyInfo p, string name)
        {
            DropdownAttribute             dp   = p.GetCustomAttribute <DropdownAttribute>();
            LevelEditorAttribute          le   = p.GetCustomAttribute <LevelEditorAttribute>();
            CurveEditorAttribute          ce   = p.GetCustomAttribute <CurveEditorAttribute>();
            SliderAttribute               sl   = p.GetCustomAttribute <SliderAttribute>();
            FileSelectorAttribute         fsl  = p.GetCustomAttribute <FileSelectorAttribute>();
            HidePropertyAttribute         hp   = p.GetCustomAttribute <HidePropertyAttribute>();
            ColorPickerAttribute          cp   = p.GetCustomAttribute <ColorPickerAttribute>();
            TitleAttribute                ti   = p.GetCustomAttribute <TitleAttribute>();
            TextInputAttribute            tinp = p.GetCustomAttribute <TextInputAttribute>();
            GraphParameterEditorAttribute gpe  = p.GetCustomAttribute <GraphParameterEditorAttribute>();
            ParameterMapEditorAttribute   pme  = p.GetCustomAttribute <ParameterMapEditorAttribute>();
            PromoteAttribute              pro  = p.GetCustomAttribute <PromoteAttribute>();

            //handle very special stuff
            //exposed constant parameter variable names
            if (gpe != null)
            {
                if (node is Graph)
                {
                    Graph g = node as Graph;

                    GraphParameterEditor inp = new GraphParameterEditor(g, g.Parameters);
                    Stack.Children.Add(inp);
                    elementLookup[name] = inp;
                }
            }
            //for graph instance exposed parameters from underlying graph
            else if (pme != null)
            {
                if (node is GraphInstanceNode)
                {
                    GraphInstanceNode gin = node as GraphInstanceNode;
                    ParameterMap      pm  = new ParameterMap(gin.GraphInst, gin.Parameters);
                    Stack.Children.Add(pm);
                    elementLookup[name] = pm;
                }
            }

            string title = name;

            if (ti != null)
            {
                title = ti.Title;
            }

            PropertyInfo op = null;

            //we don't create an element for this one
            //as it is hidden
            if (hp != null)
            {
                return;
            }

            try
            {
                if (ce != null)
                {
                    op = node.GetType().GetProperty(ce.OutputProperty);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.StackTrace);
            }

            if (t.Equals(typeof(Vector4)))
            {
                if (cp != null)
                {
                    PropertyLabel l = null;
                    if (pro != null && node is Node)
                    {
                        l = new PropertyLabel(title, node as Node, name);
                    }
                    else
                    {
                        l       = new PropertyLabel();
                        l.Title = title;
                    }

                    labels.Add(l);
                    Stack.Children.Add(l);

                    ColorSelect cs = new ColorSelect(p, node);
                    Stack.Children.Add(cs);
                    elementLookup[name] = cs;
                }
            }
            else if (t.Equals(typeof(string[])))
            {
                if (dp != null)
                {
                    PropertyLabel l = new PropertyLabel();
                    l.Title = title;
                    labels.Add(l);
                    Stack.Children.Add(l);

                    DropDown inp = new DropDown((string[])p.GetValue(node), node, p, dp.OutputProperty);
                    Stack.Children.Add(inp);
                    elementLookup[name] = inp;
                }
            }
            else if (t.Equals(typeof(bool)))
            {
                PropertyLabel l = null;
                if (pro != null && node is Node)
                {
                    l = new PropertyLabel(title, node as Node, name);
                }
                else
                {
                    l       = new PropertyLabel();
                    l.Title = title;
                }

                labels.Add(l);
                Stack.Children.Add(l);

                ToggleControl tg = new ToggleControl(name, p, node);
                Stack.Children.Add(tg);
                elementLookup[name] = tg;
            }
            else if (t.Equals(typeof(string)))
            {
                if (tinp != null)
                {
                    PropertyLabel l = new PropertyLabel();
                    l.Title = title;
                    labels.Add(l);
                    Stack.Children.Add(l);

                    PropertyInput ip = new PropertyInput(p, node);
                    Stack.Children.Add(ip);
                    elementLookup[name] = ip;
                }
                else if (fsl != null)
                {
                    PropertyLabel l = new PropertyLabel();
                    l.Title = title;
                    labels.Add(l);
                    Stack.Children.Add(l);

                    FileSelector inp = new FileSelector(p, node, fsl.Filter);
                    Stack.Children.Add(inp);
                    elementLookup[name] = inp;
                }
                else if (dp != null)
                {
                    PropertyLabel l = new PropertyLabel();
                    l.Title = title;
                    labels.Add(l);
                    Stack.Children.Add(l);

                    object[] names = dp.Values;
                    DropDown inp   = new DropDown(names, node, p, dp.OutputProperty);
                    Stack.Children.Add(inp);
                    elementLookup[name] = inp;
                }
            }
            if (t.Equals(typeof(float)))
            {
                if (sl != null)
                {
                    PropertyLabel l = null;
                    if (pro != null && node is Node)
                    {
                        l = new PropertyLabel(title, node as Node, name);
                    }
                    else
                    {
                        l       = new PropertyLabel();
                        l.Title = title;
                    }

                    labels.Add(l);
                    Stack.Children.Add(l);

                    NumberSlider inp = new NumberSlider(sl, p, node);
                    Stack.Children.Add(inp);
                    elementLookup[name] = inp;
                }
                else
                {
                    PropertyLabel l = null;
                    if (pro != null && node is Node)
                    {
                        l = new PropertyLabel(title, node as Node, name);
                    }
                    else
                    {
                        l       = new PropertyLabel();
                        l.Title = title;
                    }

                    labels.Add(l);
                    Stack.Children.Add(l);

                    NumberInput inp = new NumberInput(NumberInputType.Float, node, p);
                    Stack.Children.Add(inp);
                    elementLookup[name] = inp;
                }
            }
            else if (t.Equals(typeof(int)))
            {
                if (dp != null)
                {
                    PropertyLabel l = new PropertyLabel();
                    l.Title = title;
                    labels.Add(l);
                    Stack.Children.Add(l);

                    //do a dropdown
                    object[] names = dp.Values;
                    DropDown inp   = new DropDown(names, node, p, dp.OutputProperty);
                    Stack.Children.Add(inp);
                    elementLookup[name] = inp;
                }
                else if (sl != null)
                {
                    PropertyLabel l = null;
                    if (pro != null && node is Node)
                    {
                        l = new PropertyLabel(title, node as Node, name);
                    }
                    else
                    {
                        l       = new PropertyLabel();
                        l.Title = title;
                    }

                    labels.Add(l);
                    Stack.Children.Add(l);

                    NumberSlider inp = new NumberSlider(sl, p, node);
                    Stack.Children.Add(inp);
                    elementLookup[name] = inp;
                }
                else
                {
                    PropertyLabel l = null;
                    if (pro != null && node is Node)
                    {
                        l = new PropertyLabel(title, node as Node, name);
                    }
                    else
                    {
                        l       = new PropertyLabel();
                        l.Title = title;
                    }

                    labels.Add(l);
                    Stack.Children.Add(l);

                    NumberInput inp = new NumberInput(NumberInputType.Int, node, p);
                    Stack.Children.Add(inp);
                    elementLookup[name] = inp;
                }
            }
            else if (t.Equals(typeof(MultiRange)))
            {
                if (le != null)
                {
                    UILevels lv = null;
                    if (node is Node)
                    {
                        Node nd = (Node)node;
                        if (nd.Inputs.Count > 0 && nd.Inputs[0].Input != null)
                        {
                            var    n      = nd.Inputs[0].Input.Node;
                            byte[] result = n.GetPreview(n.Width, n.Height);

                            RawBitmap bit = null;

                            if (result != null)
                            {
                                bit = new RawBitmap(n.Width, n.Height, result);
                            }

                            lv = new UILevels(bit, node, p);
                        }
                        else
                        {
                            lv = new UILevels(null, node, p);
                        }
                        Stack.Children.Add(lv);
                        elementLookup[name] = lv;
                    }
                }
            }
            else if (op != null && ce != null)
            {
                UICurves cv = new UICurves(p, op, node);
                Stack.Children.Add(cv);
                elementLookup[name] = cv;
            }
            else if (t.IsEnum)
            {
                PropertyLabel l = new PropertyLabel();
                l.Title = title;
                labels.Add(l);
                Stack.Children.Add(l);

                string[] names = Enum.GetNames(t);
                DropDown inp   = new DropDown(names, node, p);
                Stack.Children.Add(inp);
                elementLookup[name] = inp;
            }
        }