-
Notifications
You must be signed in to change notification settings - Fork 867
Expand file tree
/
Copy pathHlslFunctionView.cs
More file actions
141 lines (129 loc) · 5.42 KB
/
HlslFunctionView.cs
File metadata and controls
141 lines (129 loc) · 5.42 KB
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
using System;
using System.Linq;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEditor.UIElements;
using UnityEditor.Graphing;
namespace UnityEditor.ShaderGraph.Drawing
{
[Serializable]
public enum HlslSourceType { File, String };
internal class HlslFunctionView : VisualElement
{
private EnumField m_Type;
private TextField m_FunctionName;
private ObjectField m_FunctionSource;
private TextField m_FunctionBody;
internal HlslFunctionView(CustomFunctionNode node)
{
styleSheets.Add(Resources.Load<StyleSheet>("Styles/HlslFunctionView"));
Draw(node);
}
private void Draw(CustomFunctionNode node)
{
var currentControls = this.Children().ToArray();
for (int i = 0; i < currentControls.Length; i++)
currentControls[i].RemoveFromHierarchy();
m_Type = new EnumField(node.sourceType);
m_Type.RegisterValueChangedCallback(s =>
{
if ((HlslSourceType)s.newValue != node.sourceType)
{
node.owner.owner.RegisterCompleteObjectUndo("Change Function Type");
node.sourceType = (HlslSourceType)s.newValue;
Draw(node);
node.ValidateNode();
node.Dirty(ModificationScope.Graph);
}
});
m_FunctionName = new TextField { value = node.functionName, multiline = false };
m_FunctionName.RegisterCallback<FocusInEvent>(s =>
{
if (m_FunctionName.value == CustomFunctionNode.defaultFunctionName)
m_FunctionName.value = "";
});
m_FunctionName.RegisterCallback<FocusOutEvent>(s =>
{
if (m_FunctionName.value?.Length == 0)
m_FunctionName.value = CustomFunctionNode.defaultFunctionName;
else
m_FunctionName.value = NodeUtils.ConvertToValidHLSLIdentifier(m_FunctionName.value);
if (m_FunctionName.value != node.functionName)
{
node.owner.owner.RegisterCompleteObjectUndo("Change Function Name");
node.functionName = m_FunctionName.value;
node.ValidateNode();
node.Dirty(ModificationScope.Graph);
}
});
string path = AssetDatabase.GUIDToAssetPath(node.functionSource);
m_FunctionSource = new ObjectField() { value = AssetDatabase.LoadAssetAtPath<ShaderInclude>(path), objectType = typeof(ShaderInclude) };
m_FunctionSource.RegisterValueChangedCallback(s =>
{
long localId;
string guidString = string.Empty;
if (s.newValue != null)
{
AssetDatabase.TryGetGUIDAndLocalFileIdentifier((ShaderInclude)s.newValue, out guidString, out localId);
}
if (guidString != node.functionSource)
{
node.owner.owner.RegisterCompleteObjectUndo("Change Function Source");
node.functionSource = guidString;
node.ValidateNode();
node.Dirty(ModificationScope.Graph);
}
});
m_FunctionBody = new TextField { value = node.functionBody, multiline = true };
m_FunctionBody.RegisterCallback<FocusInEvent>(s =>
{
if (m_FunctionBody.value == CustomFunctionNode.defaultFunctionBody)
m_FunctionBody.value = "";
});
m_FunctionBody.RegisterCallback<FocusOutEvent>(s =>
{
if (m_FunctionBody.value?.Length == 0)
m_FunctionBody.value = CustomFunctionNode.defaultFunctionBody;
if (m_FunctionBody.value != node.functionBody)
{
node.owner.owner.RegisterCompleteObjectUndo("Change Function Body");
node.functionBody = m_FunctionBody.value;
node.ValidateNode();
node.Dirty(ModificationScope.Graph);
}
});
VisualElement typeRow = new VisualElement() { name = "Row" };
{
typeRow.Add(new Label("Type"));
typeRow.Add(m_Type);
}
Add(typeRow);
VisualElement nameRow = new VisualElement() { name = "Row" };
{
nameRow.Add(new Label("Name"));
nameRow.Add(m_FunctionName);
}
Add(nameRow);
switch (node.sourceType)
{
case HlslSourceType.File:
VisualElement sourceRow = new VisualElement() { name = "Row" };
{
sourceRow.Add(new Label("Source"));
sourceRow.Add(m_FunctionSource);
}
Add(sourceRow);
break;
case HlslSourceType.String:
VisualElement bodyRow = new VisualElement() { name = "Row" };
{
bodyRow.Add(new Label("Body"));
bodyRow.style.height = 200;
bodyRow.Add(m_FunctionBody);
}
Add(bodyRow);
break;
}
}
}
}