Files
aibis-dream/Assets/Scripts/FrameAnimation/Runtime/FrameAnimationGraph.cs
T

197 lines
6.4 KiB
C#

using System.Collections.Generic;
using UnityEngine;
namespace AibisDream.FrameAnimation
{
[CreateAssetMenu(fileName = "FrameAnimationGraph", menuName = "AibisDream/Frame Animation/Frame Animation Graph")]
public sealed class FrameAnimationGraph : ScriptableObject
{
[SerializeField] private string id = string.Empty;
[SerializeField] private string displayName = string.Empty;
[SerializeField] private List<FrameClip> clips = new List<FrameClip>();
[SerializeField] private List<AnimationNode> nodes = new List<AnimationNode>();
[SerializeField] private List<AnimationEdge> edges = new List<AnimationEdge>();
[SerializeField] private List<AnimationFlow> flows = new List<AnimationFlow>();
[SerializeField] private List<FrameAnimationImportSource> importSources =
new List<FrameAnimationImportSource>();
[SerializeField] private FrameAnimationGraphSettings settings = new FrameAnimationGraphSettings();
[SerializeField] private FrameAnimationGraphEditorData editorData = new FrameAnimationGraphEditorData();
public string Id => id;
public string DisplayName => displayName;
public IReadOnlyList<FrameClip> Clips => clips;
public IReadOnlyList<AnimationNode> Nodes => nodes;
public IReadOnlyList<AnimationEdge> Edges => edges;
public IReadOnlyList<AnimationFlow> Flows => flows;
public IReadOnlyList<FrameAnimationImportSource> ImportSources => importSources;
public FrameAnimationGraphSettings Settings => settings;
public FrameAnimationGraphEditorData EditorData => editorData;
private void OnEnable()
{
EnsureCollections();
}
#if UNITY_EDITOR
private void OnValidate()
{
EnsureCollections();
EnsureMissingInternalIds();
}
#endif
internal void Configure(
string graphId,
string graphDisplayName,
IEnumerable<FrameClip> graphClips,
IEnumerable<AnimationNode> graphNodes,
IEnumerable<AnimationEdge> graphEdges,
IEnumerable<AnimationFlow> graphFlows,
string defaultPlayableId)
{
id = graphId ?? string.Empty;
displayName = graphDisplayName ?? id;
clips = graphClips != null ? new List<FrameClip>(graphClips) : new List<FrameClip>();
nodes = graphNodes != null ? new List<AnimationNode>(graphNodes) : new List<AnimationNode>();
edges = graphEdges != null ? new List<AnimationEdge>(graphEdges) : new List<AnimationEdge>();
flows = graphFlows != null ? new List<AnimationFlow>(graphFlows) : new List<AnimationFlow>();
importSources ??= new List<FrameAnimationImportSource>();
settings ??= new FrameAnimationGraphSettings();
settings.SetDefaultPlayableId(defaultPlayableId);
editorData ??= new FrameAnimationGraphEditorData();
EnsureMissingInternalIds();
}
internal void EnsureMissingInternalIds()
{
if (nodes != null)
{
foreach (var node in nodes)
{
node?.EnsureInternalId();
}
}
if (edges != null)
{
foreach (var edge in edges)
{
edge?.EnsureInternalId();
}
}
if (importSources != null)
{
foreach (var source in importSources)
{
source?.EnsureInternalId();
}
}
}
internal void AddImportedClip(FrameClip clip)
{
EnsureCollections();
if (clip != null && !clips.Contains(clip))
{
clips.Add(clip);
}
}
internal void AddClip(FrameClip clip)
{
AddImportedClip(clip);
}
internal bool RemoveClip(FrameClip clip)
{
EnsureCollections();
return clip != null && clips.Remove(clip);
}
internal void AddFlow(AnimationFlow flow)
{
EnsureCollections();
if (flow != null && !flows.Contains(flow))
{
flows.Add(flow);
}
}
internal bool RemoveFlow(AnimationFlow flow)
{
EnsureCollections();
return flow != null && flows.Remove(flow);
}
internal void AddNode(AnimationNode node)
{
EnsureCollections();
if (node != null && !nodes.Contains(node))
{
node.EnsureInternalId();
nodes.Add(node);
}
}
internal bool RemoveNode(AnimationNode node)
{
EnsureCollections();
return node != null && nodes.Remove(node);
}
internal void AddEdge(AnimationEdge edge)
{
EnsureCollections();
if (edge != null && !edges.Contains(edge))
{
edge.EnsureInternalId();
edges.Add(edge);
}
}
internal bool RemoveEdge(AnimationEdge edge)
{
EnsureCollections();
return edge != null && edges.Remove(edge);
}
internal void AddImportSource(FrameAnimationImportSource source)
{
EnsureCollections();
if (source != null && !importSources.Contains(source))
{
source.EnsureInternalId();
importSources.Add(source);
}
}
internal bool RemoveImportSource(FrameAnimationImportSource source)
{
EnsureCollections();
return source != null && importSources.Remove(source);
}
internal void SetId(string value)
{
id = value ?? string.Empty;
}
internal void SetDisplayName(string value)
{
displayName = value ?? string.Empty;
}
private void EnsureCollections()
{
clips ??= new List<FrameClip>();
nodes ??= new List<AnimationNode>();
edges ??= new List<AnimationEdge>();
flows ??= new List<AnimationFlow>();
importSources ??= new List<FrameAnimationImportSource>();
settings ??= new FrameAnimationGraphSettings();
editorData ??= new FrameAnimationGraphEditorData();
}
}
}