TimelineAssetViewModel.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEditor.IMGUI.Controls;
  4. using UnityEngine;
  5. using UnityEngine.Timeline;
  6. using UnityObject = UnityEngine.Object;
  7. namespace UnityEditor.Timeline
  8. {
  9. [Serializable]
  10. class TrackViewModelData : ISerializationCallbackReceiver
  11. {
  12. public static readonly float DefaultinlineAnimationCurveHeight = 100.0f;
  13. public bool collapsed = true;
  14. public bool showMarkers = true;
  15. public bool showInlineCurves = false;
  16. public float inlineAnimationCurveHeight = DefaultinlineAnimationCurveHeight;
  17. public int lastInlineCurveDataID = -1;
  18. public TreeViewState inlineCurvesState = null;
  19. public Rect inlineCurvesShownAreaInsideMargins = new Rect(1, 1, 1, 1);
  20. public int trackHeightExtension;
  21. public Dictionary<int, long> markerTimeStamps = new Dictionary<int, long>();
  22. [SerializeField] List<int> m_MarkerTimeStampsKeys;
  23. [SerializeField] List<long> m_MarkerTimeStampsValues;
  24. public void OnBeforeSerialize()
  25. {
  26. if (markerTimeStamps == null)
  27. return;
  28. m_MarkerTimeStampsKeys = new List<int>(markerTimeStamps.Count);
  29. m_MarkerTimeStampsValues = new List<long>(markerTimeStamps.Count);
  30. foreach (var kvp in markerTimeStamps)
  31. {
  32. m_MarkerTimeStampsKeys.Add(kvp.Key);
  33. m_MarkerTimeStampsValues.Add(kvp.Value);
  34. }
  35. }
  36. public void OnAfterDeserialize()
  37. {
  38. markerTimeStamps = new Dictionary<int, long>();
  39. if (m_MarkerTimeStampsKeys == null || m_MarkerTimeStampsValues == null ||
  40. m_MarkerTimeStampsKeys.Count != m_MarkerTimeStampsValues.Count)
  41. return;
  42. for (int i = 0; i < m_MarkerTimeStampsKeys.Count; ++i)
  43. markerTimeStamps.Add(m_MarkerTimeStampsKeys[i], m_MarkerTimeStampsValues[i]);
  44. }
  45. }
  46. [Serializable]
  47. class TimelineAssetViewModel : ScriptableObject, ISerializationCallbackReceiver
  48. {
  49. public const float DefaultTrackScale = 1.0f;
  50. public const float DefaultVerticalScroll = 0;
  51. public static readonly Vector2 TimeAreaDefaultRange = new Vector2(-WindowConstants.timeAreaShownRangePadding, 5.0f); // in seconds. Hack: using negative value to force the UI to have a left margin at 0.
  52. public static readonly Vector2 NoPlayRangeSet = new Vector2(float.MaxValue, float.MaxValue);
  53. public Vector2 timeAreaShownRange = TimeAreaDefaultRange;
  54. public float trackScale = DefaultTrackScale;
  55. public bool playRangeEnabled;
  56. public Vector2 timeAreaPlayRange = NoPlayRangeSet;
  57. public double windowTime;
  58. public float verticalScroll = DefaultVerticalScroll;
  59. public bool showMarkerHeader;
  60. public float sequencerHeaderWidth = WindowConstants.defaultHeaderWidth;
  61. public Dictionary<TrackAsset, TrackViewModelData> tracksViewModelData = new Dictionary<TrackAsset, TrackViewModelData>();
  62. // Used only for serialization of the dictionary
  63. [SerializeField] List<TrackAsset> m_Keys = new List<TrackAsset>();
  64. [SerializeField] List<TrackViewModelData> m_Vals = new List<TrackViewModelData>();
  65. public void OnBeforeSerialize()
  66. {
  67. m_Keys.Clear();
  68. m_Vals.Clear();
  69. foreach (var data in tracksViewModelData)
  70. {
  71. // Assets that don't save, will create nulls when deserializeds
  72. if (data.Key != null && data.Value != null && (data.Key.hideFlags & HideFlags.DontSave) == 0)
  73. {
  74. m_Keys.Add(data.Key);
  75. m_Vals.Add(data.Value);
  76. }
  77. }
  78. }
  79. public void OnAfterDeserialize()
  80. {
  81. }
  82. public void OnEnable()
  83. {
  84. if (m_Keys.Count == m_Vals.Count)
  85. {
  86. tracksViewModelData.Clear();
  87. for (int i = 0; i < m_Keys.Count; i++)
  88. {
  89. if (m_Keys[i] != null) // if the asset is overwritten the tracks can be null
  90. tracksViewModelData[m_Keys[i]] = m_Vals[i];
  91. }
  92. }
  93. m_Keys.Clear();
  94. m_Vals.Clear();
  95. }
  96. }
  97. }