WindowState.cs 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEditor.Experimental.SceneManagement;
  5. using UnityEngine.Experimental.Animations;
  6. using UnityEditorInternal;
  7. using UnityEngine;
  8. using UnityEngine.Playables;
  9. using UnityEngine.Timeline;
  10. using UnityEngine.Animations;
  11. namespace UnityEditor.Timeline
  12. {
  13. delegate bool PendingUpdateDelegate(WindowState state, Event currentEvent);
  14. class WindowState
  15. {
  16. const int k_TimeCodeTextFieldId = 3790;
  17. readonly TimelineWindow m_Window;
  18. bool m_Recording;
  19. readonly SpacePartitioner m_SpacePartitioner = new SpacePartitioner();
  20. readonly List<Manipulator> m_CaptureSession = new List<Manipulator>();
  21. int m_DirtyStamp;
  22. float m_SequencerHeaderWidth = WindowConstants.defaultHeaderWidth;
  23. float m_BindingAreaWidth = WindowConstants.defaultBindingAreaWidth;
  24. bool m_MustRebuildGraph;
  25. float m_LastTime;
  26. readonly PropertyCollector m_PropertyCollector = new PropertyCollector();
  27. static AnimationModeDriver s_PreviewDriver;
  28. List<Animator> m_PreviewedAnimators;
  29. List<IAnimationWindowPreview> m_PreviewedComponents;
  30. public static double kTimeEpsilon { get { return TimeUtility.kTimeEpsilon; } }
  31. public static readonly float kMaxShownTime = (float)TimeUtility.k_MaxTimelineDurationInSeconds;
  32. static readonly ISequenceState k_NullSequenceState = new NullSequenceState();
  33. // which tracks are armed for record - only one allowed per 'actor'
  34. Dictionary<TrackAsset, TrackAsset> m_ArmedTracks = new Dictionary<TrackAsset, TrackAsset>();
  35. TimelineWindow.TimelineWindowPreferences m_Preferences;
  36. List<PendingUpdateDelegate> m_OnStartFrameUpdates;
  37. List<PendingUpdateDelegate> m_OnEndFrameUpdates;
  38. readonly SequenceHierarchy m_SequenceHierarchy;
  39. public event Action<WindowState, Event> windowOnGuiStarted;
  40. public event Action<WindowState, Event> windowOnGuiFinished;
  41. public event Action<bool> OnPlayStateChange;
  42. public event Action OnDirtyStampChange;
  43. public event Action OnRebuildGraphChange;
  44. public event Action OnTimeChange;
  45. public event Action OnRecordingChange;
  46. public event Action OnBeforeSequenceChange;
  47. public event Action OnAfterSequenceChange;
  48. public WindowState(TimelineWindow w, SequenceHierarchy hierarchy)
  49. {
  50. m_Window = w;
  51. m_Preferences = w.preferences;
  52. hierarchy.Init(this);
  53. m_SequenceHierarchy = hierarchy;
  54. TimelinePlayable.muteAudioScrubbing = muteAudioScrubbing;
  55. }
  56. public static AnimationModeDriver previewDriver
  57. {
  58. get
  59. {
  60. if (s_PreviewDriver == null)
  61. s_PreviewDriver = ScriptableObject.CreateInstance<AnimationModeDriver>();
  62. return s_PreviewDriver;
  63. }
  64. }
  65. public EditorWindow editorWindow
  66. {
  67. get { return m_Window; }
  68. }
  69. public ISequenceState editSequence
  70. {
  71. get
  72. {
  73. // Using "null" ISequenceState to avoid checking against null all the time.
  74. // This *should* be removed in a phase 2 of refactoring, where we make sure
  75. // to pass around the correct state object instead of letting clients dig
  76. // into the WindowState for whatever they want.
  77. return m_SequenceHierarchy.editSequence ?? k_NullSequenceState;
  78. }
  79. }
  80. public ISequenceState masterSequence
  81. {
  82. get { return m_SequenceHierarchy.masterSequence ?? k_NullSequenceState; }
  83. }
  84. public ISequenceState referenceSequence
  85. {
  86. get { return timeReferenceMode == TimeReferenceMode.Local ? editSequence : masterSequence; }
  87. }
  88. public bool rebuildGraph
  89. {
  90. get { return m_MustRebuildGraph; }
  91. set { SyncNotifyValue(ref m_MustRebuildGraph, value, OnRebuildGraphChange); }
  92. }
  93. public float mouseDragLag { get; set; }
  94. public SpacePartitioner spacePartitioner
  95. {
  96. get { return m_SpacePartitioner; }
  97. }
  98. public List<Manipulator> captured
  99. {
  100. get { return m_CaptureSession; }
  101. }
  102. public void AddCaptured(Manipulator manipulator)
  103. {
  104. if (!m_CaptureSession.Contains(manipulator))
  105. m_CaptureSession.Add(manipulator);
  106. }
  107. public void RemoveCaptured(Manipulator manipulator)
  108. {
  109. m_CaptureSession.Remove(manipulator);
  110. }
  111. public bool isJogging { get; set; }
  112. public int viewStateHash { get; private set; }
  113. public float bindingAreaWidth
  114. {
  115. get { return m_BindingAreaWidth; }
  116. set { m_BindingAreaWidth = value; }
  117. }
  118. public float sequencerHeaderWidth
  119. {
  120. get { return m_SequencerHeaderWidth; }
  121. set
  122. {
  123. m_SequencerHeaderWidth = Mathf.Clamp(value, WindowConstants.minHeaderWidth, WindowConstants.maxHeaderWidth);
  124. }
  125. }
  126. public float mainAreaWidth { get; set; }
  127. public float trackScale
  128. {
  129. get { return editSequence.viewModel.trackScale; }
  130. set
  131. {
  132. editSequence.viewModel.trackScale = value;
  133. m_Window.treeView.CalculateRowRects();
  134. }
  135. }
  136. public int dirtyStamp
  137. {
  138. get { return m_DirtyStamp; }
  139. private set { SyncNotifyValue(ref m_DirtyStamp, value, OnDirtyStampChange); }
  140. }
  141. public bool showQuadTree { get; set; }
  142. public bool canRecord
  143. {
  144. get { return AnimationMode.InAnimationMode(previewDriver) || !AnimationMode.InAnimationMode(); }
  145. }
  146. public bool recording
  147. {
  148. get
  149. {
  150. if (!previewMode)
  151. m_Recording = false;
  152. return m_Recording;
  153. }
  154. // set can only be used to disable recording
  155. set
  156. {
  157. // force preview mode on
  158. if (value)
  159. previewMode = true;
  160. bool newValue = value;
  161. if (!previewMode)
  162. newValue = false;
  163. if (newValue && m_ArmedTracks.Count == 0)
  164. {
  165. Debug.LogError("Cannot enable recording without an armed track");
  166. newValue = false;
  167. }
  168. if (!newValue)
  169. m_ArmedTracks.Clear();
  170. if (newValue != m_Recording)
  171. {
  172. if (newValue)
  173. AnimationMode.StartAnimationRecording();
  174. else
  175. AnimationMode.StopAnimationRecording();
  176. InspectorWindow.RepaintAllInspectors();
  177. }
  178. SyncNotifyValue(ref m_Recording, newValue, OnRecordingChange);
  179. }
  180. }
  181. public bool previewMode
  182. {
  183. get { return Application.isPlaying || AnimationMode.InAnimationMode(previewDriver); }
  184. set
  185. {
  186. if (Application.isPlaying)
  187. return;
  188. bool inAnimationMode = AnimationMode.InAnimationMode(previewDriver);
  189. if (!value)
  190. {
  191. if (inAnimationMode)
  192. {
  193. Stop();
  194. OnStopPreview();
  195. AnimationMode.StopAnimationMode(previewDriver);
  196. AnimationPropertyContextualMenu.Instance.SetResponder(null);
  197. previewedDirectors = null;
  198. }
  199. }
  200. else if (!inAnimationMode)
  201. {
  202. editSequence.time = editSequence.viewModel.windowTime;
  203. EvaluateImmediate(); // does appropriate caching prior to enabling
  204. }
  205. }
  206. }
  207. public bool playing
  208. {
  209. get
  210. {
  211. return masterSequence.director != null && masterSequence.director.state == PlayState.Playing;
  212. }
  213. }
  214. public float playbackSpeed { get; set; }
  215. public bool frameSnap
  216. {
  217. get { return m_Preferences.frameSnap; }
  218. set { m_Preferences.frameSnap = value; }
  219. }
  220. public bool edgeSnaps
  221. {
  222. get { return m_Preferences.edgeSnaps; }
  223. set { m_Preferences.edgeSnaps = value; }
  224. }
  225. public bool muteAudioScrubbing
  226. {
  227. get { return m_Preferences.muteAudioScrub; }
  228. set
  229. {
  230. m_Preferences.muteAudioScrub = value;
  231. TimelinePlayable.muteAudioScrubbing = value;
  232. RebuildPlayableGraph();
  233. }
  234. }
  235. public bool playRangeLoopMode
  236. {
  237. get { return m_Preferences.playRangeLoopMode; }
  238. set { m_Preferences.playRangeLoopMode = value; }
  239. }
  240. public TimeReferenceMode timeReferenceMode
  241. {
  242. get { return m_Preferences.timeReferenceMode; }
  243. set { m_Preferences.timeReferenceMode = value; }
  244. }
  245. public bool timeInFrames
  246. {
  247. get { return editSequence.viewModel.timeInFrames; }
  248. set { editSequence.viewModel.timeInFrames = value; }
  249. }
  250. public bool showAudioWaveform
  251. {
  252. get { return editSequence.viewModel.showAudioWaveform; }
  253. set { editSequence.viewModel.showAudioWaveform = value; }
  254. }
  255. public Vector2 playRange
  256. {
  257. get { return masterSequence.viewModel.timeAreaPlayRange; }
  258. set { masterSequence.viewModel.timeAreaPlayRange = ValidatePlayRange(value); }
  259. }
  260. public bool showMarkerHeader
  261. {
  262. get { return editSequence.viewModel.showMarkerHeader; }
  263. set { editSequence.viewModel.showMarkerHeader = value; }
  264. }
  265. void UnSelectMarkerOnHeaderTrack()
  266. {
  267. //Where(m => editSequence.asset.markerTrack == m.parent)
  268. foreach (IMarker marker in SelectionManager.SelectedMarkers())
  269. {
  270. if (marker.parent == editSequence.asset.markerTrack)
  271. SelectionManager.Remove(marker);
  272. }
  273. }
  274. public EditMode.EditType editType
  275. {
  276. get { return m_Preferences.editType; }
  277. set { m_Preferences.editType = value; }
  278. }
  279. public PlaybackScrollMode autoScrollMode
  280. {
  281. get { return m_Preferences.autoScrollMode; }
  282. set { m_Preferences.autoScrollMode = value; }
  283. }
  284. public bool isClipSnapping { get; set; }
  285. public List<PlayableDirector> previewedDirectors { get; private set; }
  286. public void OnDestroy()
  287. {
  288. if (!Application.isPlaying)
  289. Stop();
  290. if (m_OnStartFrameUpdates != null)
  291. m_OnStartFrameUpdates.Clear();
  292. if (m_OnEndFrameUpdates != null)
  293. m_OnEndFrameUpdates.Clear();
  294. m_SequenceHierarchy.Clear();
  295. windowOnGuiStarted = null;
  296. windowOnGuiFinished = null;
  297. }
  298. public void OnSceneSaved()
  299. {
  300. // the director will reset it's time when the scene is saved.
  301. EnsureWindowTimeConsistency();
  302. }
  303. public void SetCurrentSequence(TimelineAsset timelineAsset, PlayableDirector director, TimelineClip hostClip)
  304. {
  305. if (OnBeforeSequenceChange != null)
  306. OnBeforeSequenceChange.Invoke();
  307. OnCurrentDirectorWillChange();
  308. if (hostClip == null || timelineAsset == null)
  309. {
  310. m_PropertyCollector.Clear();
  311. m_SequenceHierarchy.Clear();
  312. }
  313. if (timelineAsset != null)
  314. m_SequenceHierarchy.Add(timelineAsset, director, hostClip);
  315. if (OnAfterSequenceChange != null)
  316. OnAfterSequenceChange.Invoke();
  317. }
  318. public void PopSequencesUntilCount(int count)
  319. {
  320. if (count >= m_SequenceHierarchy.count) return;
  321. if (count < 1) return;
  322. if (OnBeforeSequenceChange != null)
  323. OnBeforeSequenceChange.Invoke();
  324. var nextDirector = m_SequenceHierarchy.GetStateAtIndex(count - 1).director;
  325. OnCurrentDirectorWillChange();
  326. m_SequenceHierarchy.RemoveUntilCount(count);
  327. EnsureWindowTimeConsistency();
  328. if (OnAfterSequenceChange != null)
  329. OnAfterSequenceChange.Invoke();
  330. }
  331. public SequencePath GetCurrentSequencePath()
  332. {
  333. return m_SequenceHierarchy.ToSequencePath();
  334. }
  335. public void SetCurrentSequencePath(SequencePath path, bool forceRebuild)
  336. {
  337. if (!m_SequenceHierarchy.NeedsUpdate(path, forceRebuild))
  338. return;
  339. if (OnBeforeSequenceChange != null)
  340. OnBeforeSequenceChange.Invoke();
  341. m_SequenceHierarchy.FromSequencePath(path, forceRebuild);
  342. if (OnAfterSequenceChange != null)
  343. OnAfterSequenceChange.Invoke();
  344. }
  345. public IEnumerable<ISequenceState> GetAllSequences()
  346. {
  347. return m_SequenceHierarchy.allSequences;
  348. }
  349. public double SnapToFrameIfRequired(double currentTime)
  350. {
  351. return frameSnap ? TimeReferenceUtility.SnapToFrame(currentTime) : currentTime;
  352. }
  353. public void Reset()
  354. {
  355. recording = false;
  356. previewMode = false;
  357. }
  358. public double GetSnappedTimeAtMousePosition(Vector2 mousePos)
  359. {
  360. return SnapToFrameIfRequired(ScreenSpacePixelToTimeAreaTime(mousePos.x));
  361. }
  362. static void SyncNotifyValue<T>(ref T oldValue, T newValue, Action changeStateCallback)
  363. {
  364. var stateChanged = false;
  365. if (oldValue == null)
  366. {
  367. oldValue = newValue;
  368. stateChanged = true;
  369. }
  370. else
  371. {
  372. if (!oldValue.Equals(newValue))
  373. {
  374. oldValue = newValue;
  375. stateChanged = true;
  376. }
  377. }
  378. if (stateChanged && changeStateCallback != null)
  379. {
  380. changeStateCallback.Invoke();
  381. }
  382. }
  383. public void SetTimeAreaTransform(Vector2 newTranslation, Vector2 newScale)
  384. {
  385. m_Window.timeArea.SetTransform(newTranslation, newScale);
  386. TimeAreaChanged();
  387. }
  388. public void SetTimeAreaShownRange(float min, float max)
  389. {
  390. m_Window.timeArea.SetShownHRange(min, max);
  391. TimeAreaChanged();
  392. }
  393. internal void TimeAreaChanged()
  394. {
  395. if (editSequence.asset != null)
  396. {
  397. Vector2 newShownRange = new Vector2(m_Window.timeArea.shownArea.x, m_Window.timeArea.shownArea.xMax);
  398. if (editSequence.viewModel.timeAreaShownRange != newShownRange)
  399. {
  400. editSequence.viewModel.timeAreaShownRange = newShownRange;
  401. if (!FileUtil.IsReadOnly(editSequence.asset))
  402. EditorUtility.SetDirty(editSequence.asset);
  403. }
  404. }
  405. }
  406. public void ResetPreviewMode()
  407. {
  408. var mode = previewMode;
  409. previewMode = false;
  410. previewMode = mode;
  411. }
  412. public bool TimeIsInRange(float value)
  413. {
  414. Rect shownArea = m_Window.timeArea.shownArea;
  415. return value >= shownArea.x && value <= shownArea.xMax;
  416. }
  417. public bool RangeIsVisible(Range range)
  418. {
  419. var shownArea = m_Window.timeArea.shownArea;
  420. return range.start < shownArea.xMax && range.end > shownArea.xMin;
  421. }
  422. public void EnsurePlayHeadIsVisible()
  423. {
  424. double minDisplayedTime = PixelToTime(timeAreaRect.xMin);
  425. double maxDisplayedTime = PixelToTime(timeAreaRect.xMax);
  426. double currentTime = editSequence.time;
  427. if (currentTime >= minDisplayedTime && currentTime <= maxDisplayedTime)
  428. return;
  429. float displayedTimeRange = (float)(maxDisplayedTime - minDisplayedTime);
  430. float minimumTimeToDisplay = (float)currentTime - displayedTimeRange / 2.0f;
  431. float maximumTimeToDisplay = (float)currentTime + displayedTimeRange / 2.0f;
  432. SetTimeAreaShownRange(minimumTimeToDisplay, maximumTimeToDisplay);
  433. }
  434. public void SetPlayHeadToMiddle()
  435. {
  436. double minDisplayedTime = PixelToTime(timeAreaRect.xMin);
  437. double maxDisplayedTime = PixelToTime(timeAreaRect.xMax);
  438. double currentTime = editSequence.time;
  439. float displayedTimeRange = (float)(maxDisplayedTime - minDisplayedTime);
  440. if (currentTime >= minDisplayedTime && currentTime <= maxDisplayedTime)
  441. {
  442. if (currentTime < minDisplayedTime + displayedTimeRange / 2)
  443. return;
  444. }
  445. const float kCatchUpSpeed = 3f;
  446. float realDelta = Mathf.Clamp(Time.realtimeSinceStartup - m_LastTime, 0f, 1f) * kCatchUpSpeed;
  447. float scrollCatchupAmount = kCatchUpSpeed * realDelta * displayedTimeRange / 2;
  448. if (currentTime < minDisplayedTime)
  449. {
  450. SetTimeAreaShownRange((float)currentTime, (float)currentTime + displayedTimeRange);
  451. }
  452. else if (currentTime > maxDisplayedTime)
  453. {
  454. SetTimeAreaShownRange((float)currentTime - displayedTimeRange + scrollCatchupAmount, (float)currentTime + scrollCatchupAmount);
  455. }
  456. else if (currentTime > minDisplayedTime + displayedTimeRange / 2)
  457. {
  458. float targetMinDisplayedTime = Mathf.Min((float)minDisplayedTime + scrollCatchupAmount,
  459. (float)(currentTime - displayedTimeRange / 2));
  460. SetTimeAreaShownRange(targetMinDisplayedTime, targetMinDisplayedTime + displayedTimeRange);
  461. }
  462. }
  463. internal void UpdateLastFrameTime()
  464. {
  465. m_LastTime = Time.realtimeSinceStartup;
  466. }
  467. public Vector2 timeAreaShownRange
  468. {
  469. get
  470. {
  471. if (m_Window.state.editSequence.asset != null)
  472. return editSequence.viewModel.timeAreaShownRange;
  473. return TimelineAssetViewModel.TimeAreaDefaultRange;
  474. }
  475. }
  476. public Vector2 timeAreaTranslation
  477. {
  478. get { return m_Window.timeArea.translation; }
  479. }
  480. public Vector2 timeAreaScale
  481. {
  482. get { return m_Window.timeArea.scale; }
  483. }
  484. public Rect timeAreaRect
  485. {
  486. get
  487. {
  488. var sequenceContentRect = m_Window.sequenceContentRect;
  489. return new Rect(
  490. sequenceContentRect.x,
  491. WindowConstants.timeAreaYPosition,
  492. Mathf.Max(sequenceContentRect.width, WindowConstants.timeAreaMinWidth),
  493. WindowConstants.timeAreaHeight
  494. );
  495. }
  496. }
  497. public float windowHeight
  498. {
  499. get { return m_Window.position.height; }
  500. }
  501. public bool playRangeEnabled
  502. {
  503. get { return !EditorApplication.isPlaying && masterSequence.viewModel.playRangeEnabled && !IsEditingASubTimeline(); }
  504. set
  505. {
  506. if (EditorApplication.isPlaying)
  507. return;
  508. masterSequence.viewModel.playRangeEnabled = value;
  509. }
  510. }
  511. public TimelineWindow GetWindow()
  512. {
  513. return m_Window;
  514. }
  515. public void Play()
  516. {
  517. if (masterSequence.director == null)
  518. return;
  519. if (!previewMode)
  520. previewMode = true;
  521. if (previewMode)
  522. {
  523. if (masterSequence.time > masterSequence.duration)
  524. masterSequence.time = 0;
  525. masterSequence.director.Play();
  526. masterSequence.director.ProcessPendingGraphChanges();
  527. PlayableDirector.ResetFrameTiming();
  528. InvokePlayStateChangeCallback(true);
  529. }
  530. }
  531. public void Pause()
  532. {
  533. if (masterSequence.director != null)
  534. {
  535. masterSequence.director.Pause();
  536. masterSequence.director.ProcessPendingGraphChanges();
  537. SynchronizeSequencesAfterPlayback();
  538. InvokePlayStateChangeCallback(false);
  539. }
  540. }
  541. public void SetPlaying(bool start)
  542. {
  543. if (start && !playing)
  544. {
  545. Play();
  546. }
  547. if (!start && playing)
  548. {
  549. Pause();
  550. }
  551. }
  552. public void Stop()
  553. {
  554. if (masterSequence.director != null)
  555. {
  556. masterSequence.director.Stop();
  557. masterSequence.director.ProcessPendingGraphChanges();
  558. InvokePlayStateChangeCallback(false);
  559. }
  560. }
  561. void InvokePlayStateChangeCallback(bool isPlaying)
  562. {
  563. if (OnPlayStateChange != null)
  564. OnPlayStateChange.Invoke(isPlaying);
  565. }
  566. public void RebuildPlayableGraph()
  567. {
  568. if (masterSequence.director != null)
  569. {
  570. masterSequence.director.RebuildGraph();
  571. // rebuild both the parent and the edit sequences. control tracks don't necessary
  572. // rebuild the subdirector on recreation
  573. if (editSequence.director != null && editSequence.director != masterSequence.director)
  574. {
  575. editSequence.director.RebuildGraph();
  576. }
  577. }
  578. }
  579. public void Evaluate()
  580. {
  581. if (masterSequence.director != null)
  582. {
  583. if (!EditorApplication.isPlaying && !previewMode)
  584. GatherProperties(masterSequence.director);
  585. ForceTimeOnDirector(masterSequence.director);
  586. masterSequence.director.DeferredEvaluate();
  587. if (EditorApplication.isPlaying == false)
  588. {
  589. PlayModeView.RepaintAll();
  590. SceneView.RepaintAll();
  591. AudioMixerWindow.RepaintAudioMixerWindow();
  592. }
  593. }
  594. }
  595. public void EvaluateImmediate()
  596. {
  597. if (masterSequence.director != null)
  598. {
  599. if (!EditorApplication.isPlaying && !previewMode)
  600. GatherProperties(masterSequence.director);
  601. if (previewMode)
  602. {
  603. ForceTimeOnDirector(masterSequence.director);
  604. masterSequence.director.ProcessPendingGraphChanges();
  605. masterSequence.director.Evaluate();
  606. }
  607. }
  608. }
  609. public void Refresh()
  610. {
  611. CheckRecordingState();
  612. dirtyStamp = dirtyStamp + 1;
  613. rebuildGraph = true;
  614. }
  615. public void UpdateViewStateHash()
  616. {
  617. viewStateHash = timeAreaTranslation.GetHashCode()
  618. .CombineHash(timeAreaScale.GetHashCode())
  619. .CombineHash(trackScale.GetHashCode());
  620. }
  621. public bool IsEditingASubItem()
  622. {
  623. return IsCurrentEditingASequencerTextField() || !SelectionManager.IsCurveEditorFocused(null);
  624. }
  625. public bool IsEditingASubTimeline()
  626. {
  627. return editSequence != masterSequence;
  628. }
  629. public bool IsEditingAnEmptyTimeline()
  630. {
  631. return editSequence.asset == null;
  632. }
  633. public bool IsEditingAPrefabAsset()
  634. {
  635. var stage = PrefabStageUtility.GetCurrentPrefabStage();
  636. return stage != null && editSequence.director != null && stage.IsPartOfPrefabContents(editSequence.director.gameObject);
  637. }
  638. public bool IsCurrentEditingASequencerTextField()
  639. {
  640. if (editSequence.asset == null)
  641. return false;
  642. if (k_TimeCodeTextFieldId == GUIUtility.keyboardControl)
  643. return true;
  644. return editSequence.asset.flattenedTracks.Count(t => t.GetInstanceID() == GUIUtility.keyboardControl) != 0;
  645. }
  646. public float TimeToTimeAreaPixel(double t) // TimeToTimeAreaPixel
  647. {
  648. float pixelX = (float)t;
  649. pixelX *= timeAreaScale.x;
  650. pixelX += timeAreaTranslation.x + sequencerHeaderWidth;
  651. return pixelX;
  652. }
  653. public float TimeToScreenSpacePixel(double time)
  654. {
  655. float pixelX = (float)time;
  656. pixelX *= timeAreaScale.x;
  657. pixelX += timeAreaTranslation.x;
  658. return pixelX;
  659. }
  660. public float TimeToPixel(double time)
  661. {
  662. return m_Window.timeArea.TimeToPixel((float)time, timeAreaRect);
  663. }
  664. public float PixelToTime(float pixel)
  665. {
  666. return m_Window.timeArea.PixelToTime(pixel, timeAreaRect);
  667. }
  668. public float PixelDeltaToDeltaTime(float p)
  669. {
  670. return PixelToTime(p) - PixelToTime(0);
  671. }
  672. public float TimeAreaPixelToTime(float pixel)
  673. {
  674. return PixelToTime(pixel);
  675. }
  676. public float ScreenSpacePixelToTimeAreaTime(float p)
  677. {
  678. // transform into track space by offsetting the pixel by the screen-space offset of the time area
  679. p -= timeAreaRect.x;
  680. return TrackSpacePixelToTimeAreaTime(p);
  681. }
  682. public float TrackSpacePixelToTimeAreaTime(float p)
  683. {
  684. p -= timeAreaTranslation.x;
  685. if (timeAreaScale.x > 0.0f)
  686. return p / timeAreaScale.x;
  687. return p;
  688. }
  689. public void OffsetTimeArea(int pixels)
  690. {
  691. Vector3 tx = timeAreaTranslation;
  692. tx.x += pixels;
  693. SetTimeAreaTransform(tx, timeAreaScale);
  694. }
  695. public GameObject GetSceneReference(TrackAsset asset)
  696. {
  697. if (editSequence.director == null)
  698. return null; // no player bound
  699. return TimelineUtility.GetSceneGameObject(editSequence.director, asset);
  700. }
  701. public void CalculateRowRects()
  702. {
  703. // arming a track might add inline curve tracks, recalc track heights
  704. if (m_Window != null && m_Window.treeView != null)
  705. m_Window.treeView.CalculateRowRects();
  706. }
  707. // Only one track within a 'track' hierarchy can be armed
  708. public void ArmForRecord(TrackAsset track)
  709. {
  710. m_ArmedTracks[TimelineUtility.GetSceneReferenceTrack(track)] = track;
  711. if (track != null && !recording)
  712. recording = true;
  713. if (!recording)
  714. return;
  715. track.OnRecordingArmed(editSequence.director);
  716. CalculateRowRects();
  717. }
  718. public void UnarmForRecord(TrackAsset track)
  719. {
  720. m_ArmedTracks.Remove(TimelineUtility.GetSceneReferenceTrack(track));
  721. if (m_ArmedTracks.Count == 0)
  722. recording = false;
  723. track.OnRecordingUnarmed(editSequence.director);
  724. }
  725. public void UpdateRecordingState()
  726. {
  727. if (recording)
  728. {
  729. foreach (var track in m_ArmedTracks.Values)
  730. {
  731. if (track != null)
  732. track.OnRecordingTimeChanged(editSequence.director);
  733. }
  734. }
  735. }
  736. public bool IsTrackRecordable(TrackAsset track)
  737. {
  738. // A track with animated parameters can always be recorded to
  739. return IsArmedForRecord(track) || track.HasAnyAnimatableParameters();
  740. }
  741. public bool IsArmedForRecord(TrackAsset track)
  742. {
  743. return track == GetArmedTrack(track);
  744. }
  745. public TrackAsset GetArmedTrack(TrackAsset track)
  746. {
  747. TrackAsset outTrack;
  748. m_ArmedTracks.TryGetValue(TimelineUtility.GetSceneReferenceTrack(track), out outTrack);
  749. return outTrack;
  750. }
  751. void CheckRecordingState()
  752. {
  753. // checks for deleted tracks, and makes sure the recording state matches
  754. if (m_ArmedTracks.Any(t => t.Value == null))
  755. {
  756. m_ArmedTracks = m_ArmedTracks.Where(t => t.Value != null).ToDictionary(t => t.Key, t => t.Value);
  757. if (m_ArmedTracks.Count == 0)
  758. recording = false;
  759. }
  760. }
  761. void OnCurrentDirectorWillChange()
  762. {
  763. SynchronizeViewModelTime(editSequence);
  764. if (!Application.isPlaying)
  765. Stop();
  766. rebuildGraph = true; // needed for asset previews
  767. }
  768. public void GatherProperties(PlayableDirector director)
  769. {
  770. if (director == null || Application.isPlaying)
  771. return;
  772. var asset = director.playableAsset as TimelineAsset;
  773. if (!previewMode)
  774. {
  775. AnimationMode.StartAnimationMode(previewDriver);
  776. OnStartPreview(director);
  777. AnimationPropertyContextualMenu.Instance.SetResponder(new TimelineRecordingContextualResponder(this));
  778. if (!previewMode)
  779. return;
  780. EnsureWindowTimeConsistency();
  781. }
  782. if (asset != null)
  783. {
  784. m_PropertyCollector.Reset();
  785. m_PropertyCollector.PushActiveGameObject(null); // avoid overflow on unbound tracks
  786. asset.GatherProperties(director, m_PropertyCollector);
  787. }
  788. }
  789. void OnStartPreview(PlayableDirector director)
  790. {
  791. previewedDirectors = TimelineUtility.GetAllDirectorsInHierarchy(director).ToList();
  792. if (previewedDirectors == null)
  793. return;
  794. m_PreviewedAnimators = TimelineUtility.GetBindingsFromDirectors<Animator>(previewedDirectors).ToList();
  795. m_PreviewedComponents = new List<IAnimationWindowPreview>();
  796. foreach (var animator in m_PreviewedAnimators)
  797. {
  798. m_PreviewedComponents.AddRange(animator.GetComponents<IAnimationWindowPreview>());
  799. }
  800. foreach (var previewedComponent in m_PreviewedComponents)
  801. {
  802. previewedComponent.StartPreview();
  803. }
  804. }
  805. void OnStopPreview()
  806. {
  807. if (m_PreviewedComponents != null)
  808. {
  809. foreach (var previewComponent in m_PreviewedComponents)
  810. {
  811. if (previewComponent != null)
  812. {
  813. previewComponent.StopPreview();
  814. }
  815. }
  816. m_PreviewedComponents = null;
  817. }
  818. if (m_PreviewedAnimators != null)
  819. {
  820. foreach (var previewAnimator in m_PreviewedAnimators)
  821. {
  822. if (previewAnimator != null)
  823. {
  824. previewAnimator.UnbindAllHandles();
  825. }
  826. }
  827. m_PreviewedAnimators = null;
  828. }
  829. }
  830. internal void ProcessStartFramePendingUpdates()
  831. {
  832. if (m_OnStartFrameUpdates != null)
  833. m_OnStartFrameUpdates.RemoveAll(callback => callback.Invoke(this, Event.current));
  834. }
  835. internal void ProcessEndFramePendingUpdates()
  836. {
  837. if (m_OnEndFrameUpdates != null)
  838. m_OnEndFrameUpdates.RemoveAll(callback => callback.Invoke(this, Event.current));
  839. }
  840. public void AddStartFrameDelegate(PendingUpdateDelegate updateDelegate)
  841. {
  842. if (m_OnStartFrameUpdates == null)
  843. m_OnStartFrameUpdates = new List<PendingUpdateDelegate>();
  844. if (m_OnStartFrameUpdates.Contains(updateDelegate))
  845. return;
  846. m_OnStartFrameUpdates.Add(updateDelegate);
  847. }
  848. public void AddEndFrameDelegate(PendingUpdateDelegate updateDelegate)
  849. {
  850. if (m_OnEndFrameUpdates == null)
  851. m_OnEndFrameUpdates = new List<PendingUpdateDelegate>();
  852. if (m_OnEndFrameUpdates.Contains(updateDelegate))
  853. return;
  854. m_OnEndFrameUpdates.Add(updateDelegate);
  855. }
  856. internal void InvokeWindowOnGuiStarted(Event evt)
  857. {
  858. if (windowOnGuiStarted != null)
  859. windowOnGuiStarted.Invoke(this, evt);
  860. }
  861. internal void InvokeWindowOnGuiFinished(Event evt)
  862. {
  863. if (windowOnGuiFinished != null)
  864. windowOnGuiFinished.Invoke(this, evt);
  865. }
  866. public void UpdateRootPlayableDuration(double duration)
  867. {
  868. if (editSequence.director != null)
  869. {
  870. if (editSequence.director.playableGraph.IsValid())
  871. {
  872. if (editSequence.director.playableGraph.GetRootPlayableCount() > 0)
  873. {
  874. var rootPlayable = editSequence.director.playableGraph.GetRootPlayable(0);
  875. if (rootPlayable.IsValid())
  876. rootPlayable.SetDuration(duration);
  877. }
  878. }
  879. }
  880. }
  881. public void InvokeTimeChangeCallback()
  882. {
  883. if (OnTimeChange != null)
  884. OnTimeChange.Invoke();
  885. }
  886. Vector2 ValidatePlayRange(Vector2 range)
  887. {
  888. if (range == TimelineAssetViewModel.NoPlayRangeSet)
  889. return range;
  890. float minimumPlayRangeTime = 0.01f / Mathf.Max(1.0f, referenceSequence.frameRate);
  891. // Validate min
  892. if (range.y - range.x < minimumPlayRangeTime)
  893. range.x = range.y - minimumPlayRangeTime;
  894. if (range.x < 0.0f)
  895. range.x = 0.0f;
  896. // Validate max
  897. if (range.y > editSequence.duration)
  898. range.y = (float)editSequence.duration;
  899. if (range.y - range.x < minimumPlayRangeTime)
  900. range.y = Mathf.Min(range.x + minimumPlayRangeTime, (float)editSequence.duration);
  901. return range;
  902. }
  903. void EnsureWindowTimeConsistency()
  904. {
  905. if (Application.isPlaying || masterSequence.director == null || masterSequence.viewModel == null)
  906. return;
  907. masterSequence.time = masterSequence.viewModel.windowTime;
  908. }
  909. void SynchronizeSequencesAfterPlayback()
  910. {
  911. // Synchronizing editSequence will synchronize all view models up to the master
  912. SynchronizeViewModelTime(editSequence);
  913. }
  914. static void SynchronizeViewModelTime(ISequenceState state)
  915. {
  916. if (state.director == null || state.viewModel == null)
  917. return;
  918. var t = state.time;
  919. state.time = t;
  920. }
  921. // because we may be evaluating outside the duration of the root playable
  922. // we explicitly set the time - this causes the graph to not 'advance' the time
  923. // because advancing it can force it to change due to wrapping to the duration
  924. // This can happen if the graph is force evaluated outside it's duration
  925. // case 910114, 936844 and 943377
  926. static void ForceTimeOnDirector(PlayableDirector director)
  927. {
  928. var directorTime = director.time;
  929. director.time = directorTime;
  930. }
  931. }
  932. }