TimelineWindow_TimeCursor.cs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.Timeline;
  4. using UnityEngine.Playables;
  5. namespace UnityEditor.Timeline
  6. {
  7. partial class TimelineWindow
  8. {
  9. TimeAreaItem m_PlayHead;
  10. void TimeCursorGUI(TimelineItemArea area)
  11. {
  12. DrawTimeOnSlider();
  13. if (!CanDrawTimeCursor(area))
  14. return;
  15. if (m_PlayHead == null || m_PlayHead.style != styles.timeCursor)
  16. {
  17. m_PlayHead = new TimeAreaItem(styles.timeCursor, OnTrackHeadDrag);
  18. m_PlayHead.AddManipulator(new PlayheadContextMenu(m_PlayHead));
  19. }
  20. var headerMode = area == TimelineItemArea.Header;
  21. DrawTimeCursor(headerMode, !headerMode);
  22. }
  23. bool CanDrawTimeCursor(TimelineItemArea area)
  24. {
  25. if (!currentMode.ShouldShowTimeCursor(state))
  26. return false;
  27. if (treeView == null || state.editSequence.asset == null || (state.editSequence.asset != null && state.IsEditingAnEmptyTimeline()))
  28. return false;
  29. if (area == TimelineItemArea.Lines && !state.TimeIsInRange((float)state.editSequence.time))
  30. return false;
  31. return true;
  32. }
  33. void DrawTimeOnSlider()
  34. {
  35. if (currentMode.ShouldShowTimeCursor(state))
  36. {
  37. var colorDimFactor = EditorGUIUtility.isProSkin ? 0.7f : 0.9f;
  38. var c = styles.timeCursor.normal.textColor * colorDimFactor;
  39. float time = Mathf.Max((float)state.editSequence.time, 0);
  40. float duration = (float)state.editSequence.duration;
  41. m_TimeArea.DrawTimeOnSlider(time, c, duration, DirectorStyles.kDurationGuiThickness);
  42. }
  43. }
  44. void DrawTimeCursor(bool drawHead, bool drawline)
  45. {
  46. m_PlayHead.HandleManipulatorsEvents(state);
  47. if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
  48. {
  49. if (state.timeAreaRect.Contains(Event.current.mousePosition))
  50. {
  51. state.SetPlaying(false);
  52. m_PlayHead.HandleManipulatorsEvents(state);
  53. state.editSequence.time = Math.Max(0.0, state.GetSnappedTimeAtMousePosition(Event.current.mousePosition));
  54. }
  55. }
  56. state.isClipSnapping = false;
  57. m_PlayHead.drawLine = drawline;
  58. m_PlayHead.drawHead = drawHead;
  59. m_PlayHead.Draw(sequenceContentRect, state, state.editSequence.time);
  60. }
  61. void OnTrackHeadDrag(double newTime)
  62. {
  63. state.editSequence.time = Math.Max(0.0, newTime);
  64. m_PlayHead.showTooltip = true;
  65. }
  66. }
  67. }