-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNodeParameter.cs
117 lines (108 loc) · 3.57 KB
/
NodeParameter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace NodeBlock.Engine
{
public class NodeParameter : ICloneable
{
public string Id { get; set; }
[JsonIgnore]
public Node Node { get; }
public string Name { get; set; }
public object Value { get; set; }
public Type ValueType { get; set; }
public bool IsIn { get; set; }
public bool IsDynamic { get; set; }
public NodeParameter Assignments { get; set; }
public bool IsReference { get; set; }
public NodeParameter(Node node, string name, Type valueType, bool isIn, object value = null, string id = "", bool isDynamic = false)
{
this.Id = id == "" ? Guid.NewGuid().ToString() : id;
this.Node = node;
this.Name = name;
this.ValueType = valueType;
this.IsIn = isIn;
this.IsDynamic = isDynamic;
this.IsReference = valueType == typeof(Node);
if(value != null)
{
if (value.GetType() != valueType) throw new Exception("Invalid type for the value");
this.Value = value;
}
}
public NodeParameter InstanciateWithValue(object value)
{
return new NodeParameter(this.Node, this.Name, this.ValueType, false) { Value = value };
}
public bool SetValue(object value)
{
//if (!this.ValueType.IsAssignableFrom(value.GetType())) return false;
this.Value = value;
return true;
}
public object GetValue()
{
try
{
if (this.IsIn)
{
if (this.Assignments != null)
{
var v = this.Node.ComputeParameterValue(this, this.Assignments.GetValue());
if (this.ValueType == typeof(string) && v == null) v = "";
return v;
}
}
return this.Node.ComputeParameterValue(this, this.Value);
}
catch(Exception)
{
return null;
}
}
public Node GetNode()
{
try
{
if (this.IsIn)
{
if (this.Assignments != null)
{
return this.Assignments.Node;
}
}
return null;
}
catch (Exception)
{
return null;
}
}
public double GetValueAsDouble()
{
if(this.GetValue().GetType() != typeof(double) &&
this.GetValue().GetType() != typeof(int)
&& this.GetValue().GetType() != typeof(long)
&& this.GetValue().GetType() != typeof(float)) {
return double.Parse(this.GetValue().ToString(), CultureInfo.InvariantCulture);
}
else
{
if(this.GetValue().GetType() != typeof(double))
{
return Convert.ToDouble(this.GetValue());
}
else
{
return (double)this.GetValue();
}
}
}
public object Clone()
{
return this.MemberwiseClone();
}
}
}