/
DoubleSlider.cs
152 lines (134 loc) · 5.09 KB
/
DoubleSlider.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Xml;
using Autodesk.DesignScript.Runtime;
using Dynamo.Graph;
using Dynamo.Graph.Nodes;
using Dynamo.Migration;
using ProtoCore.AST.AssociativeAST;
namespace CoreNodeModels.Input
{
[NodeName("Number Slider")]
[NodeCategory(BuiltinNodeCategories.CORE_INPUT)]
[NodeDescription("DoubleSliderNodeDescription", typeof(Properties.Resources))]
[NodeSearchTags("DoubleSliderSearchTags", typeof(Properties.Resources))]
[SupressImportIntoVM]
[IsDesignScriptCompatible]
[AlsoKnownAs("DSCoreNodesUI.Input.DoubleSlider")]
public class DoubleSlider : SliderBase<double>
{
public DoubleSlider()
{
Min = 0;
Max = 100;
Step = 0.1;
Value = 1;
ShouldDisplayPreviewCore = false;
}
public override IEnumerable<AssociativeNode> BuildOutputAst(List<AssociativeNode> inputAstNodes)
{
var rhs = AstFactory.BuildDoubleNode(Value);
var assignment = AstFactory.BuildAssignment(GetAstIdentifierForOutputIndex(0), rhs);
return new[] {assignment};
}
protected override double DeserializeValue(string val)
{
try
{
return Convert.ToDouble(val, CultureInfo.InvariantCulture);
}
catch
{
return 0;
}
}
protected override string SerializeValue()
{
return Value.ToString(CultureInfo.InvariantCulture);
}
protected override bool UpdateValueCore(UpdateValueParams updateValueParams)
{
string name = updateValueParams.PropertyName;
string value = updateValueParams.PropertyValue;
switch (name)
{
case "Min":
case "MinText":
Min = ConvertStringToDouble(value);
return true; // UpdateValueCore handled.
case "Max":
case "MaxText":
Max = ConvertStringToDouble(value);
return true; // UpdateValueCore handled.
case "Value":
case "ValueText":
Value = ConvertStringToDouble(value);
return true; // UpdateValueCore handled.
case "Step":
case "StepText":
Step = ConvertStringToDouble(value);
return true;
}
return base.UpdateValueCore(updateValueParams);
}
#region Serialization/Deserialization Methods
protected override void SerializeCore(XmlElement element, SaveContext context)
{
base.SerializeCore(element, context); // Base implementation must be called.
var xmlDocument = element.OwnerDocument;
var subNode = xmlDocument.CreateElement("Range");
subNode.SetAttribute("min", Min.ToString(CultureInfo.InvariantCulture));
subNode.SetAttribute("max", Max.ToString(CultureInfo.InvariantCulture));
subNode.SetAttribute("step", Step.ToString(CultureInfo.InvariantCulture));
element.AppendChild(subNode);
}
protected override void DeserializeCore(XmlElement element, SaveContext context)
{
base.DeserializeCore(element, context); //Base implementation must be called.
foreach (XmlNode subNode in element.ChildNodes)
{
if (!subNode.Name.Equals("Range"))
continue;
if (subNode.Attributes == null || (subNode.Attributes.Count <= 0))
continue;
foreach (XmlAttribute attr in subNode.Attributes)
{
switch (attr.Name)
{
case "min":
Min = ConvertStringToDouble(attr.Value);
break;
case "max":
Max = ConvertStringToDouble(attr.Value);
break;
case "step":
Step = ConvertStringToDouble(attr.Value);
break;
default:
Log(string.Format("{0} attribute could not be deserialized for {1}", attr.Name, GetType()));
break;
}
}
break;
}
}
#endregion
}
}
namespace Dynamo.Nodes
{
public class DoubleSlider
{
[NodeMigration(@from: "0.7.5.0")]
public static NodeMigrationData Migrate_0750(NodeMigrationData data)
{
var migrationData = new NodeMigrationData(data.Document);
XmlElement oldNode = data.MigratedNodes.ElementAt(0);
XmlElement newNode = MigrationManager.CloneAndChangeName(oldNode, "CoreNodeModels.Input.DoubleSlider", "Number Slider", true);
migrationData.AppendNode(newNode);
return migrationData;
}
}
}