protected void AddNode(string type, Point p) { Node n = null; UINode unode = null; n = Graph.CreateNode(type); if (n != null) { if (n is GraphInstanceNode) { GraphInstanceNode gn = (GraphInstanceNode)n; gn.Load(type); } Modified = true; n.ViewOriginX = p.X; n.ViewOriginY = p.Y; Graph.Add(n); unode = new UINode(n, this, p.X, p.Y, XShift, YShift, Scale); unode.HorizontalAlignment = HorizontalAlignment.Left; unode.VerticalAlignment = VerticalAlignment.Top; lookup[n.Id] = unode; ViewPort.Children.Add(unode); GraphNodes.Add(unode); UndoRedoManager.AddUndo(new UndoCreateNode(Id, unode.Id, this)); } }
/// <summary> /// Helper for Undeo Redo System. Does not trigger new undo added to stack. /// </summary> /// <param name="json"></param> /// <param name="p"></param> /// <returns></returns> public UINode AddNodeFromJson(string json, Point p) { try { Node.NodeData nd = JsonConvert.DeserializeObject <Node.NodeData>(json); if (nd == null) { return(null); } Node n = null; UINode unode = null; n = Graph.CreateNode(nd.type); if (n != null) { if (n is GraphInstanceNode) { GraphInstanceNode gn = (GraphInstanceNode)n; gn.Load(nd.type); } n.Id = nd.id; n.Width = nd.width; n.Height = nd.height; Graph.Add(n); n.FromJson(Graph.NodeLookup, json); unode = new UINode(n, this, p.X, p.Y, XShift, YShift, Scale); unode.HorizontalAlignment = HorizontalAlignment.Left; unode.VerticalAlignment = VerticalAlignment.Top; lookup[n.Id] = unode; ViewPort.Children.Add(unode); GraphNodes.Add(unode); Modified = true; Task.Delay(250).ContinueWith(t => { unode.LoadConnections(lookup); }); } return(unode); } catch (Exception e) { Console.WriteLine(e.StackTrace); } return(null); }
public virtual Node CreateNode(string type) { if (ReadOnly) { return(null); } if (type.Contains(System.IO.Path.PathSeparator)) { var n = new GraphInstanceNode(width, height); n.ParentGraph = this; return(n); } try { Type t = Type.GetType(type); if (t != null) { if (t.Equals(typeof(OutputNode))) { var n = new OutputNode(defaultTextureType); n.ParentGraph = this; return(n); } else if (t.Equals(typeof(InputNode))) { var n = new InputNode(defaultTextureType); n.ParentGraph = this; return(n); } else { Node n = (Node)Activator.CreateInstance(t, width, height, defaultTextureType); n.ParentGraph = this; return(n); } } else { var n = new GraphInstanceNode(width, height); n.ParentGraph = this; return(n); } } catch (Exception e) { Console.WriteLine(e.StackTrace); return(null); } }
/// <summary> /// real lookup is required as the nodes added from json /// from a paste command will have new ids /// but will need to lookup the old ids /// to reconnect any that may have been connected when pasted /// </summary> /// <param name="json"></param> /// <param name="realLookup"></param> /// <returns></returns> protected UINode AddNodeFromJson(string json, Dictionary <string, Node> realLookup) { try { Node.NodeData nd = JsonConvert.DeserializeObject <Node.NodeData>(json); if (nd == null) { return(null); } Node n = null; UINode unode = null; n = Graph.CreateNode(nd.type); if (n != null) { if (n is GraphInstanceNode) { GraphInstanceNode gn = (GraphInstanceNode)n; gn.Load(nd.type); } realLookup[nd.id] = n; Graph.Add(n); unode = new UINode(n, this, 0, 0, XShift, YShift, Scale); unode.HorizontalAlignment = HorizontalAlignment.Left; unode.VerticalAlignment = VerticalAlignment.Top; lookup[n.Id] = unode; ViewPort.Children.Add(unode); GraphNodes.Add(unode); UndoRedoManager.AddUndo(new UndoCreateNode(Id, unode.Id, this)); Modified = true; } return(unode); } catch (Exception e) { Console.WriteLine(e.StackTrace); return(null); } }
public void Set(Graph g, Dictionary <string, GraphParameterValue> values, bool showLabel = true, bool inlinePropertyLabels = false) { Clear(); Dictionary <string, List <Tuple <PropertyLabel, UIElement> > > sorter = new Dictionary <string, List <Tuple <PropertyLabel, UIElement> > >(); foreach (var k in values.Keys) { var v = values[k]; if (v.IsFunction()) { continue; } string[] split = k.Split('.'); var n = g.FindSubNodeById(split[0]); PropertyInfo nodeInfo = null; string customHeader = ""; if (n != null) { nodeInfo = n.GetType().GetProperty(split[1]); if (nodeInfo == null && n is GraphInstanceNode) { //then it might be an underling custom parameter on the node GraphInstanceNode inst = n as GraphInstanceNode; var realParam = inst.GetCustomParameter(split[1]); if (realParam != null) { //initiate custom header //for proper underlying processing //on the label customHeader = "$Custom."; //just set the parameter inputtype the same //also ensure min and max are the same v.InputType = realParam.InputType; v.Max = realParam.Max; v.Min = realParam.Min; v.Section = realParam.Section; } } } PropertyLabel lbl = new PropertyLabel(v.Name, n, customHeader + split[1]); EditableAttribute ed = null; if (nodeInfo != null) { ed = nodeInfo.GetCustomAttribute <EditableAttribute>(); } if (ed == null) { ed = new EditableAttribute(GetParamInputType(v), v.Name, v.Section, v.Min, v.Max); } UIElement ele = BuildParamater(v, ed, v.GetType().GetProperty("Value"), nodeInfo); if (ele != null) { string inherit = ""; if (g != null && g.ParentNode is GraphInstanceNode) { inherit = "Inherited "; } string sect = inherit + v.Section; if (string.IsNullOrEmpty(v.Section)) { sect = inherit + "Default"; } List <Tuple <PropertyLabel, UIElement> > items = null; if (sorter.TryGetValue(sect, out items)) { items.Add(new Tuple <PropertyLabel, UIElement>(lbl, ele)); } else { items = new List <Tuple <PropertyLabel, UIElement> >(); items.Add(new Tuple <PropertyLabel, UIElement>(lbl, ele)); sorter[sect] = items; } } } List <string> keys = sorter.Keys.ToList(); keys.Sort(); foreach (string k in keys) { List <Tuple <PropertyLabel, UIElement> > items = sorter[k]; if (showLabel) { PropertySection sect = new PropertySection(); sect.Title = k; if (!Stack.Children.Contains(sect) && sect.Parent == null) { Stack.Children.Add(sect); } foreach (var t in items) { if (!inlinePropertyLabels) { sect.Add(t.Item1); sect.Add(t.Item2); } else { StackPanelAuto inlinePanel = new StackPanelAuto(); inlinePanel.Direction = Orientation.Horizontal; inlinePanel.HalfAndHalf = true; inlinePanel.Children.Add(t.Item1); inlinePanel.Children.Add(t.Item2); sect.Add(inlinePanel); } } } else { foreach (var t in items) { if (!inlinePropertyLabels) { Stack.Children.Add(t.Item1); Stack.Children.Add(t.Item2); } else { StackPanelAuto inlinePanel = new StackPanelAuto(); inlinePanel.Direction = Orientation.Horizontal; inlinePanel.HalfAndHalf = true; inlinePanel.Children.Add(t.Item1); inlinePanel.Children.Add(t.Item2); Stack.Children.Add(inlinePanel); } } } } }
private void CreateFunctionParameter(FunctionGraph g) { g.AssignParentNode(Node); try { PropertyInfo info = null; if (Parameter.StartsWith("$Custom.")) { if (Node is GraphInstanceNode) { GraphInstanceNode gn = Node as GraphInstanceNode; string cparam = Parameter.Replace("$Custom.", ""); var param = gn.GetCustomParameter(cparam); if (param != null) { var cparent = Node.ParentGraph; g.ExpectedOutput = param.Type; if (cparent != null) { cparent.SetParameterValue(Node.Id, cparam, g, true, param.Type); FIcon.Opacity = 1; DefaultVar.IsEnabled = true; ConstantVar.IsEnabled = false; FunctionVar.IsEnabled = false; } else { Log.Warn("Failed to promote to function"); } } else { //log error Log.Error("Could not find custom parameter: " + cparam); } } else { Log.Warn("Failed to promoto to function"); } } else { info = Node.GetType().GetProperty(Parameter); if (info == null) { Log.Warn("Failed to promote to function"); return; } var pro = info.GetCustomAttribute <PromoteAttribute>(); if (pro != null) { g.ExpectedOutput = pro.ExpectedType; } var p = Node.ParentGraph; p = p.ParentNode != null ? p.ParentNode.ParentGraph : p; if (p != null) { p.SetParameterValue(Node.Id, Parameter, g, true, g.ExpectedOutput); FIcon.Opacity = 1; DefaultVar.IsEnabled = true; ConstantVar.IsEnabled = false; FunctionVar.IsEnabled = false; } else { Log.Warn("Failed to promote to function"); } } } catch (Exception ex) { Log.Error(ex); } }
private void ConstantVar_Click(object sender, RoutedEventArgs e) { if (Node == null || string.IsNullOrEmpty(Parameter)) { return; } try { PropertyInfo info = null; if (Parameter.StartsWith("$Custom.")) { if (Node is GraphInstanceNode) { GraphInstanceNode gn = Node as GraphInstanceNode; string cparam = Parameter.Replace("$Custom.", ""); var param = gn.GetCustomParameter(cparam); if (param != null) { var cparent = Node.ParentGraph; if (cparent != null) { cparent.SetParameterValue(Node.Id, cparam, param.Value, true, param.Type); var nparam = cparent.GetParameterRaw(Node.Id, cparam); //copy settings over if (nparam != null) { nparam.Description = param.Description; nparam.InputType = param.InputType; nparam.Min = param.Min; nparam.Max = param.Max; nparam.Name = param.Name; } FIcon.Opacity = 0.25; DefaultVar.IsEnabled = true; ConstantVar.IsEnabled = false; FunctionVar.IsEnabled = false; } else { Log.Warn("Failed to promote to constant"); } } else { //log error Log.Error("Could not find custom parameter: " + cparam); } } else { Log.Warn("Failed to promote to constant"); } } else { info = Node.GetType().GetProperty(Parameter); if (info != null) { var pro = info.GetCustomAttribute <PromoteAttribute>(); NodeType t = NodeType.Float; if (pro != null) { t = pro.ExpectedType; } var v = info.GetValue(Node); var p = Node.ParentGraph; p = p.ParentNode != null ? p.ParentNode.ParentGraph : p; if (p != null) { p.SetParameterValue(Node.Id, Parameter, v, pro != null, t); FIcon.Opacity = 0.25; DefaultVar.IsEnabled = true; ConstantVar.IsEnabled = false; FunctionVar.IsEnabled = false; } else { Log.Warn("Failed to promote to constant"); } } else { Log.Warn("Failed to promote to constant"); } } } catch (Exception ex) { Log.Error(ex); } }
private void FunctionVar_Click(object sender, RoutedEventArgs e) { if (Node == null || string.IsNullOrEmpty(Parameter)) { return; } FunctionGraph g = new FunctionGraph(Node.Name + " - " + Parameter.Replace("$Custom.", "") + " Function", Node.Width, Node.Height); g.AssignParentNode(Node); try { PropertyInfo info = null; if (Parameter.StartsWith("$Custom.")) { if (Node is GraphInstanceNode) { GraphInstanceNode gn = Node as GraphInstanceNode; string cparam = Parameter.Replace("$Custom.", ""); var param = gn.GetCustomParameter(cparam); if (param != null) { var cparent = Node.ParentGraph; if (cparent != null) { cparent = cparent.TopGraph(); } g.ExpectedOutput = param.Type; if (cparent != null) { cparent.SetParameterValue(Node.Id, cparam, g, true, param.Type); FIcon.Opacity = 1; DefaultVar.IsEnabled = true; ConstantVar.IsEnabled = false; FunctionVar.IsEnabled = false; } else { Log.Warn("Failed to promote to function"); } } else { //log error Log.Error("Could not find custom parameter: " + cparam); } } else { Log.Warn("Failed to promoto to function"); } } else { info = Node.GetType().GetProperty(Parameter); if (info == null) { Log.Warn("Failed to promote to function"); return; } var pro = info.GetCustomAttribute <PromoteAttribute>(); if (pro != null) { g.ExpectedOutput = pro.ExpectedType; } var p = Node.ParentGraph; if (p != null) { p = p.TopGraph(); } if (p != null) { var pg = p; pg.SetParameterValue(Node.Id, Parameter, g, true, g.ExpectedOutput); FIcon.Opacity = 1; DefaultVar.IsEnabled = true; ConstantVar.IsEnabled = false; FunctionVar.IsEnabled = false; } else { Log.Warn("Failed to promote to function"); } } } catch (Exception ex) { Log.Error(ex); } }
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; } }