forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVariableNodeModel.cs
More file actions
205 lines (174 loc) · 7.44 KB
/
Copy pathVariableNodeModel.cs
File metadata and controls
205 lines (174 loc) · 7.44 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using UnityEngine;
using UnityEngine.Scripting.APIUpdating;
namespace Unity.GraphToolsFoundation.Editor
{
[Serializable]
[MovedFrom(false, "Unity.GraphToolsFoundation.Editor", "Unity.GraphTools.Foundation.Model")]
class VariableNodeModel : NodeModel, ISingleInputPortNodeModel, ISingleOutputPortNodeModel, IHasDeclarationModel, IRenamable, ICloneable
{
const string k_MainPortName = "MainPortName";
[SerializeReference]
VariableDeclarationModel m_DeclarationModel;
[SerializeField, Obsolete]
#pragma warning disable CS0618
SerializableGUID m_DeclarationModelGuid;
#pragma warning restore CS0618
[SerializeField]
Hash128 m_DeclarationModelHashGuid;
protected PortModel m_MainPortModel;
/// <summary>
/// The human readable name of the data type of the variable declaration model.
/// </summary>
public virtual string DataTypeString => VariableDeclarationModel?.DataType.GetMetadata(GraphModel.Stencil).FriendlyName ?? string.Empty;
/// <summary>
/// The string used to describe this variable.
/// </summary>
public virtual string VariableString => DeclarationModel == null ? string.Empty : VariableDeclarationModel.IsExposed ? "Exposed variable" : "Variable";
/// <inheritdoc />
public override string Title => DeclarationModel == null ? "" : DeclarationModel.Title;
/// <inheritdoc />
public DeclarationModel DeclarationModel
{
get
{
if (m_DeclarationModel == null && GraphModel.TryGetModelFromGuid(m_DeclarationModelHashGuid, out var model) && model is VariableDeclarationPlaceholder missingDeclarationModel)
{
this.SetCapability(Editor.Capabilities.Movable, false);
this.SetCapability(Editor.Capabilities.Copiable, false);
this.SetCapability(Editor.Capabilities.Droppable, false);
return missingDeclarationModel;
}
this.SetCapability(Editor.Capabilities.Movable, true);
this.SetCapability(Editor.Capabilities.Copiable, true);
this.SetCapability(Editor.Capabilities.Droppable, true);
return m_DeclarationModel;
}
set
{
if (ReferenceEquals(m_DeclarationModel, value))
return;
m_DeclarationModel = (VariableDeclarationModel)value;
m_DeclarationModelHashGuid = m_DeclarationModel.Guid;
GraphModel?.CurrentGraphChangeDescription?.AddChangedModel(this, ChangeHint.Data);
DefineNode();
}
}
/// <summary>
/// The <see cref="VariableDeclarationModel"/> associated with this node.
/// </summary>
public VariableDeclarationModel VariableDeclarationModel
{
get => DeclarationModel as VariableDeclarationModel;
set => DeclarationModel = value;
}
/// <inheritdoc />
public PortModel InputPort => m_MainPortModel?.Direction == PortDirection.Input ? m_MainPortModel : null;
/// <inheritdoc />
public PortModel OutputPort => m_MainPortModel?.Direction == PortDirection.Output ? m_MainPortModel : null;
/// <summary>
/// Initializes a new instance of the <see cref="VariableNodeModel"/> class.
/// </summary>
public VariableNodeModel()
{
m_Capabilities.Add(Editor.Capabilities.Renamable);
this.SetCapability(Editor.Capabilities.Colorable, false);
}
/// <summary>
/// Updates the port type from the variable declaration type.
/// </summary>
public virtual void UpdateTypeFromDeclaration()
{
if (m_MainPortModel?.DataTypeHandle != VariableDeclarationModel.DataType)
{
DefineNode();
GraphModel?.CurrentGraphChangeDescription?.AddChangedModel(this, ChangeHint.Data);
// update connected nodes' ports colors/types
if (m_MainPortModel != null)
foreach (var connectedPortModel in m_MainPortModel.GetConnectedPorts())
connectedPortModel.NodeModel.OnConnection(connectedPortModel, m_MainPortModel);
}
}
/// <inheritdoc />
protected override void OnDefineNode()
{
if (m_DeclarationModel != null && m_DeclarationModel.Modifiers.HasFlag(ModifierFlags.Write))
{
if (GetDataType() == TypeHandle.ExecutionFlow)
m_MainPortModel = this.AddExecutionInputPort(null);
else
m_MainPortModel = this.AddDataInputPort(null, GetDataType(), k_MainPortName);
}
else
{
if (GetDataType() == TypeHandle.ExecutionFlow)
m_MainPortModel = this.AddExecutionOutputPort(null);
else
m_MainPortModel = this.AddDataOutputPort(null, m_DeclarationModel == null ? TypeHandle.MissingPort : GetDataType(), k_MainPortName);
}
}
/// <inheritdoc />
public virtual void Rename(string newName)
{
if (!this.IsRenamable())
return;
DeclarationModel?.Rename(newName);
}
/// <inheritdoc />
public GraphElementModel Clone()
{
var decl = m_DeclarationModel;
try
{
m_DeclarationModel = null;
var clone = CloneHelpers.CloneUsingScriptableObjectInstantiate(this);
clone.m_DeclarationModel = decl;
return clone;
}
finally
{
m_DeclarationModel = decl;
}
}
/// <inheritdoc />
public override string Tooltip
{
get
{
var tooltip = $"{VariableString}";
if (!string.IsNullOrEmpty(DataTypeString))
tooltip += $" of type {DataTypeString}";
if (!string.IsNullOrEmpty(VariableDeclarationModel?.Tooltip))
tooltip += "\n" + VariableDeclarationModel.Tooltip;
if (string.IsNullOrEmpty(tooltip))
return base.Tooltip;
return tooltip;
}
set => base.Tooltip = value;
}
/// <summary>
/// Gets the data type of the variable node.
/// </summary>
/// <returns>The type of the variable declaration associated with this node, or <see cref="TypeHandle.Unknown"/> if there is none.</returns>
public virtual TypeHandle GetDataType() => VariableDeclarationModel?.DataType ?? TypeHandle.Unknown;
/// <inheritdoc />
public override void OnBeforeSerialize()
{
base.OnBeforeSerialize();
#pragma warning disable CS0612
m_DeclarationModelGuid = m_DeclarationModelHashGuid;
#pragma warning restore CS0612
}
/// <inheritdoc />
public override void OnAfterDeserialize()
{
base.OnAfterDeserialize();
#pragma warning disable CS0612
m_DeclarationModelHashGuid = m_DeclarationModelGuid;
#pragma warning restore CS0612
}
}
}