forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphElementModel.cs
More file actions
174 lines (147 loc) · 5.13 KB
/
Copy pathGraphElementModel.cs
File metadata and controls
174 lines (147 loc) · 5.13 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
// 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 System.Collections.Generic;
using System.Linq;
using UnityEngine;
using UnityEngine.Scripting.APIUpdating;
namespace Unity.GraphToolsFoundation.Editor
{
/// <summary>
/// Base class for a model that represents an element in a graph.
/// </summary>
[Serializable]
[MovedFrom(false, "Unity.GraphToolsFoundation.Editor", "Unity.GraphTools.Foundation.Model")]
abstract class GraphElementModel : Model
{
[SerializeField, HideInInspector]
Color m_Color;
[SerializeField, HideInInspector]
bool m_HasUserColor;
[SerializeField, HideInInspector]
SerializationVersion m_Version;
protected List<Capabilities> m_Capabilities = new();
GraphModel m_GraphModel;
/// <summary>
/// Serialized version, used for backward compatibility
/// </summary>
public SerializationVersion Version => m_Version;
/// <summary>
/// The graph model to which the element belongs.
/// </summary>
public virtual GraphModel GraphModel
{
get => m_GraphModel;
set
{
m_GraphModel = value;
foreach (var subModel in DependentModels.Where(m => m != null))
{
subModel.GraphModel = value;
}
}
}
/// <summary>
/// The dependent models for this model (for example, ports on a node, blocks in context node).
/// </summary>
public virtual IEnumerable<GraphElementModel> DependentModels => Enumerable.Empty<GraphElementModel>();
/// <summary>
/// The list of capabilities of the element.
/// </summary>
public IReadOnlyList<Capabilities> Capabilities => m_Capabilities;
/// <summary>
/// Used for backward compatibility
/// </summary>
protected internal Color SerializedColor_Internal => m_Color;
/// <summary>
/// Default Color to use when no user color is provided
/// </summary>
public virtual Color DefaultColor => Color.clear;
/// <summary>
/// Color for the element.
/// </summary>
/// <remarks>
/// Setting a color should set HasUserColor to true.
/// </remarks>
public virtual Color Color
{
get => HasUserColor ? m_Color : DefaultColor;
set
{
if (!this.IsColorable())
return;
m_HasUserColor = true;
if (m_Color == value)
return;
m_Color = value;
GraphModel?.CurrentGraphChangeDescription?.AddChangedModel(this, ChangeHint.Style);
}
}
/// <summary>
/// True if the color was changed.
/// </summary>
public virtual bool HasUserColor => m_HasUserColor;
/// <summary>
/// If true, the color picker used to set the color should show the alpha editing controls.
/// </summary>
public virtual bool UseColorAlpha => true;
/// <summary>
/// The container for this graph element.
/// </summary>
public virtual IGraphElementContainer Container => GraphModel;
/// <summary>
/// Version number for serialization.
/// </summary>
/// <remarks>
/// Useful for models backward compatibility
/// </remarks>
public enum SerializationVersion
{
// Use package release number as the name of the version.
// ReSharper disable once InconsistentNaming
GTF_V_0_8_2 = 0,
// ReSharper disable once InconsistentNaming
GTF_V_0_13_0 = 1,
/// <summary>
/// Keep Latest as the highest value in this enum
/// </summary>
Latest
}
/// <summary>
/// Reset the color to its original state.
/// </summary>
/// <remarks>
/// Resetting a color should set HasUserColor to false.
/// </remarks>
public virtual void ResetColor()
{
if (!m_HasUserColor)
return;
m_HasUserColor = false;
GraphModel?.CurrentGraphChangeDescription?.AddChangedModel(this, ChangeHint.Style);
}
/// <inheritdoc />
public override void OnBeforeSerialize()
{
base.OnBeforeSerialize();
m_Version = SerializationVersion.Latest;
}
public override void OnAfterDeserialize()
{
base.OnAfterDeserialize();
GraphModel = null;
}
/// <summary>
/// Recursively assign a new guid to this model and its <see cref="DependentModels"/>.
/// </summary>
public void AssignNewGuidRecursively()
{
AssignNewGuid();
foreach (var model in DependentModels)
{
model.AssignNewGuidRecursively();
}
}
}
}