Açıklama Yok
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TimelineClipGUI.cs 28KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. using UnityEngine.Playables;
  6. using UnityEngine.Timeline;
  7. namespace UnityEditor.Timeline
  8. {
  9. class TimelineClipGUI : TimelineItemGUI, IClipCurveEditorOwner, ISnappable, IAttractable
  10. {
  11. EditorClip m_EditorItem;
  12. Rect m_ClipCenterSection;
  13. readonly List<Rect> m_LoopRects = new List<Rect>();
  14. ClipDrawData m_ClipDrawData;
  15. Rect m_MixOutRect;
  16. Rect m_MixInRect;
  17. int m_MinLoopIndex = 1;
  18. // clip dirty detection
  19. int m_LastDirtyIndex = Int32.MinValue;
  20. bool m_ClipViewDirty = true;
  21. bool supportResize { get; }
  22. public ClipCurveEditor clipCurveEditor { get; set; }
  23. public TimelineClipGUI previousClip { get; set; }
  24. public TimelineClipGUI nextClip { get; set; }
  25. static readonly float k_MinMixWidth = 2;
  26. static readonly float k_MaxHandleWidth = 10f;
  27. static readonly float k_MinHandleWidth = 1f;
  28. bool? m_ShowDrillIcon;
  29. ClipEditor m_ClipEditor;
  30. static List<PlayableDirector> s_TempSubDirectors = new List<PlayableDirector>();
  31. static readonly IconData k_DiggableClipIcon = new IconData(DirectorStyles.LoadIcon("TimelineDigIn"));
  32. string name
  33. {
  34. get
  35. {
  36. if (string.IsNullOrEmpty(clip.displayName))
  37. return "(Empty)";
  38. return clip.displayName;
  39. }
  40. }
  41. public bool inlineCurvesSelected => SelectionManager.IsCurveEditorFocused(this);
  42. public Rect mixOutRect
  43. {
  44. get
  45. {
  46. var percent = clip.mixOutPercentage;
  47. var x = Mathf.Round(treeViewRect.width * (1 - percent));
  48. var width = Mathf.Round(treeViewRect.width * percent);
  49. m_MixOutRect.Set(x, 0.0f, width, treeViewRect.height);
  50. return m_MixOutRect;
  51. }
  52. }
  53. public Rect mixInRect
  54. {
  55. get
  56. {
  57. var width = Mathf.Round(treeViewRect.width * clip.mixInPercentage);
  58. m_MixInRect.Set(0.0f, 0.0f, width, treeViewRect.height);
  59. return m_MixInRect;
  60. }
  61. }
  62. public ClipBlends GetClipBlends()
  63. {
  64. var _mixInRect = mixInRect;
  65. var _mixOutRect = mixOutRect;
  66. var blendInKind = BlendKind.None;
  67. if (_mixInRect.width > k_MinMixWidth && clip.hasBlendIn)
  68. blendInKind = BlendKind.Mix;
  69. else if (_mixInRect.width > k_MinMixWidth)
  70. blendInKind = BlendKind.Ease;
  71. var blendOutKind = BlendKind.None;
  72. if (_mixOutRect.width > k_MinMixWidth && clip.hasBlendOut)
  73. blendOutKind = BlendKind.Mix;
  74. else if (_mixOutRect.width > k_MinMixWidth)
  75. blendOutKind = BlendKind.Ease;
  76. return new ClipBlends(blendInKind, _mixInRect, blendOutKind, _mixOutRect);
  77. }
  78. public override double start
  79. {
  80. get { return clip.start; }
  81. }
  82. public override double end
  83. {
  84. get { return clip.end; }
  85. }
  86. public bool supportsLooping
  87. {
  88. get { return clip.SupportsLooping(); }
  89. }
  90. // for the inline curve editor, only show loops if we recorded the asset
  91. bool IClipCurveEditorOwner.showLoops
  92. {
  93. get { return clip.SupportsLooping() && (clip.asset is AnimationPlayableAsset); }
  94. }
  95. TrackAsset IClipCurveEditorOwner.owner
  96. {
  97. get { return clip.GetParentTrack(); }
  98. }
  99. public bool supportsSubTimelines
  100. {
  101. get { return m_ClipEditor.supportsSubTimelines; }
  102. }
  103. public int minLoopIndex
  104. {
  105. get { return m_MinLoopIndex; }
  106. }
  107. public Rect clippedRect { get; private set; }
  108. public override void Select()
  109. {
  110. MoveToTop();
  111. SelectionManager.Add(clip);
  112. if (clipCurveEditor != null && SelectionManager.Count() == 1)
  113. SelectionManager.SelectInlineCurveEditor(this);
  114. }
  115. public override bool IsSelected()
  116. {
  117. return SelectionManager.Contains(clip);
  118. }
  119. public override void Deselect()
  120. {
  121. SelectionManager.Remove(clip);
  122. if (inlineCurvesSelected)
  123. SelectionManager.SelectInlineCurveEditor(null);
  124. }
  125. public override bool CanSelect(Event evt)
  126. {
  127. ClipBlends clipBlends = GetClipBlends();
  128. Vector2 mousePos = evt.mousePosition - rect.position;
  129. return m_ClipCenterSection.Contains(mousePos) || IsPointLocatedInClipBlend(mousePos, clipBlends);
  130. }
  131. static bool IsPointLocatedInClipBlend(Vector2 pt, ClipBlends blends)
  132. {
  133. if (blends.inRect.Contains(pt))
  134. {
  135. if (blends.inKind == BlendKind.Mix)
  136. return Sign(pt, blends.inRect.min, blends.inRect.max) < 0;
  137. return true;
  138. }
  139. if (blends.outRect.Contains(pt))
  140. {
  141. if (blends.outKind == BlendKind.Mix)
  142. return Sign(pt, blends.outRect.min, blends.outRect.max) >= 0;
  143. return true;
  144. }
  145. return false;
  146. }
  147. static float Sign(Vector2 point, Vector2 linePoint1, Vector2 linePoint2)
  148. {
  149. return (point.x - linePoint2.x) * (linePoint1.y - linePoint2.y) - (linePoint1.x - linePoint2.x) * (point.y - linePoint2.y);
  150. }
  151. public override ITimelineItem item
  152. {
  153. get { return ItemsUtils.ToItem(clip); }
  154. }
  155. IZOrderProvider zOrderProvider { get; }
  156. public TimelineClipHandle leftHandle { get; }
  157. public TimelineClipHandle rightHandle { get; }
  158. public TimelineClipGUI(TimelineClip clip, IRowGUI parent, IZOrderProvider provider) : base(parent)
  159. {
  160. zOrderProvider = provider;
  161. zOrder = provider.Next();
  162. m_EditorItem = EditorClipFactory.GetEditorClip(clip);
  163. m_ClipEditor = CustomTimelineEditorCache.GetClipEditor(clip);
  164. supportResize = true;
  165. leftHandle = new TimelineClipHandle(this, TrimEdge.Start);
  166. rightHandle = new TimelineClipHandle(this, TrimEdge.End);
  167. ItemToItemGui.Add(clip, this);
  168. }
  169. void CreateInlineCurveEditor(WindowState state)
  170. {
  171. if (clipCurveEditor != null)
  172. return;
  173. var animationClip = clip.animationClip;
  174. if (animationClip != null && animationClip.empty)
  175. animationClip = null;
  176. // prune out clips coming from FBX
  177. if (animationClip != null && !clip.recordable)
  178. return; // don't show, even if there are curves
  179. if (animationClip == null && !clip.HasAnyAnimatableParameters())
  180. return; // nothing to show
  181. state.AddEndFrameDelegate((istate, currentEvent) =>
  182. {
  183. clipCurveEditor = new ClipCurveEditor(CurveDataSource.Create(this), TimelineWindow.instance, clip.GetParentTrack());
  184. return true;
  185. });
  186. }
  187. public TimelineClip clip
  188. {
  189. get { return m_EditorItem.clip; }
  190. }
  191. // Draw the actual clip. Defers to the track drawer for customization
  192. void UpdateDrawData(WindowState state, Rect drawRect, string title, bool selected, bool previousClipSelected, float rectXOffset)
  193. {
  194. m_ClipDrawData.clip = clip;
  195. m_ClipDrawData.targetRect = drawRect;
  196. m_ClipDrawData.clipCenterSection = m_ClipCenterSection;
  197. m_ClipDrawData.unclippedRect = treeViewRect;
  198. m_ClipDrawData.title = title;
  199. m_ClipDrawData.selected = selected;
  200. m_ClipDrawData.inlineCurvesSelected = inlineCurvesSelected;
  201. m_ClipDrawData.previousClip = previousClip != null ? previousClip.clip : null;
  202. m_ClipDrawData.previousClipSelected = previousClipSelected;
  203. Vector3 shownAreaTime = state.timeAreaShownRange;
  204. m_ClipDrawData.localVisibleStartTime = clip.ToLocalTimeUnbound(Math.Max(clip.start, shownAreaTime.x));
  205. m_ClipDrawData.localVisibleEndTime = clip.ToLocalTimeUnbound(Math.Min(clip.end, shownAreaTime.y));
  206. m_ClipDrawData.clippedRect = new Rect(clippedRect.x - rectXOffset, 0.0f, clippedRect.width, clippedRect.height);
  207. m_ClipDrawData.minLoopIndex = minLoopIndex;
  208. m_ClipDrawData.loopRects = m_LoopRects;
  209. m_ClipDrawData.supportsLooping = supportsLooping;
  210. m_ClipDrawData.clipBlends = GetClipBlends();
  211. m_ClipDrawData.clipEditor = m_ClipEditor;
  212. m_ClipDrawData.ClipDrawOptions = UpdateClipDrawOptions(m_ClipEditor, clip);
  213. UpdateClipIcons(state);
  214. }
  215. void UpdateClipIcons(WindowState state)
  216. {
  217. // Pass 1 - gather size
  218. int required = 0;
  219. bool requiresDigIn = ShowDrillIcon(state.editSequence.director);
  220. if (requiresDigIn)
  221. required++;
  222. var icons = m_ClipDrawData.ClipDrawOptions.icons;
  223. foreach (var icon in icons)
  224. {
  225. if (icon != null)
  226. required++;
  227. }
  228. // Pass 2 - copy icon data
  229. if (required == 0)
  230. {
  231. m_ClipDrawData.rightIcons = null;
  232. return;
  233. }
  234. if (m_ClipDrawData.rightIcons == null || m_ClipDrawData.rightIcons.Length != required)
  235. m_ClipDrawData.rightIcons = new IconData[required];
  236. int index = 0;
  237. if (requiresDigIn)
  238. m_ClipDrawData.rightIcons[index++] = k_DiggableClipIcon;
  239. foreach (var icon in icons)
  240. {
  241. if (icon != null)
  242. m_ClipDrawData.rightIcons[index++] = new IconData(icon);
  243. }
  244. }
  245. static ClipDrawOptions UpdateClipDrawOptions(ClipEditor clipEditor, TimelineClip clip)
  246. {
  247. try
  248. {
  249. return clipEditor.GetClipOptions(clip);
  250. }
  251. catch (Exception e)
  252. {
  253. Debug.LogException(e);
  254. }
  255. return CustomTimelineEditorCache.GetDefaultClipEditor().GetClipOptions(clip);
  256. }
  257. static void DrawClip(ClipDrawData drawData)
  258. {
  259. ClipDrawer.DrawDefaultClip(drawData);
  260. if (drawData.clip.asset is AnimationPlayableAsset)
  261. {
  262. var state = TimelineWindow.instance.state;
  263. if (state.recording && state.IsArmedForRecord(drawData.clip.GetParentTrack()))
  264. {
  265. ClipDrawer.DrawAnimationRecordBorder(drawData);
  266. ClipDrawer.DrawRecordProhibited(drawData);
  267. }
  268. }
  269. }
  270. public void DrawGhostClip(Rect targetRect)
  271. {
  272. DrawSimpleClip(targetRect, ClipBorder.Selection(), new Color(1.0f, 1.0f, 1.0f, 0.5f));
  273. }
  274. public void DrawInvalidClip(Rect targetRect)
  275. {
  276. DrawSimpleClip(targetRect, ClipBorder.Selection(), DirectorStyles.Instance.customSkin.colorInvalidClipOverlay);
  277. }
  278. void DrawSimpleClip(Rect targetRect, ClipBorder border, Color overlay)
  279. {
  280. var drawOptions = UpdateClipDrawOptions(CustomTimelineEditorCache.GetClipEditor(clip), clip);
  281. ClipDrawer.DrawSimpleClip(clip, targetRect, border, overlay, drawOptions);
  282. }
  283. void DrawInto(Rect drawRect, WindowState state)
  284. {
  285. if (Event.current.type != EventType.Repaint)
  286. return;
  287. // create the inline curve editor if not already created
  288. CreateInlineCurveEditor(state);
  289. // @todo optimization, most of the calculations (rect, offsets, colors, etc.) could be cached
  290. // and rebuilt when the hash of the clip changes.
  291. if (isInvalid)
  292. {
  293. DrawInvalidClip(treeViewRect);
  294. return;
  295. }
  296. GUI.BeginClip(drawRect);
  297. var originRect = new Rect(0.0f, 0.0f, drawRect.width, drawRect.height);
  298. string clipLabel = name;
  299. var selected = SelectionManager.Contains(clip);
  300. var previousClipSelected = previousClip != null && SelectionManager.Contains(previousClip.clip);
  301. if (selected && 1.0 != clip.timeScale)
  302. clipLabel += " " + clip.timeScale.ToString("F2") + "x";
  303. UpdateDrawData(state, originRect, clipLabel, selected, previousClipSelected, drawRect.x);
  304. DrawClip(m_ClipDrawData);
  305. GUI.EndClip();
  306. if (clip.GetParentTrack() != null && !clip.GetParentTrack().lockedInHierarchy)
  307. {
  308. if (selected && supportResize)
  309. {
  310. var cursorRect = rect;
  311. cursorRect.xMin += leftHandle.boundingRect.width;
  312. cursorRect.xMax -= rightHandle.boundingRect.width;
  313. EditorGUIUtility.AddCursorRect(cursorRect, MouseCursor.MoveArrow);
  314. }
  315. if (supportResize)
  316. {
  317. var handleWidth = Mathf.Clamp(drawRect.width * 0.3f, k_MinHandleWidth, k_MaxHandleWidth);
  318. leftHandle.Draw(drawRect, handleWidth, state);
  319. rightHandle.Draw(drawRect, handleWidth, state);
  320. }
  321. }
  322. }
  323. void CalculateClipRectangle(Rect trackRect, WindowState state)
  324. {
  325. if (m_ClipViewDirty)
  326. {
  327. var clipRect = RectToTimeline(trackRect, state);
  328. treeViewRect = clipRect;
  329. // calculate clipped rect
  330. clipRect.xMin = Mathf.Max(clipRect.xMin, trackRect.xMin);
  331. clipRect.xMax = Mathf.Min(clipRect.xMax, trackRect.xMax);
  332. if (clipRect.width > 0 && clipRect.width < 2)
  333. {
  334. clipRect.width = 5.0f;
  335. }
  336. clippedRect = clipRect;
  337. }
  338. }
  339. void AddToSpacePartitioner(WindowState state)
  340. {
  341. if (Event.current.type == EventType.Repaint && !parent.locked)
  342. state.spacePartitioner.AddBounds(this, rect);
  343. }
  344. void CalculateBlendRect()
  345. {
  346. m_ClipCenterSection = treeViewRect;
  347. m_ClipCenterSection.x = 0;
  348. m_ClipCenterSection.y = 0;
  349. m_ClipCenterSection.xMin = mixInRect.xMax;
  350. m_ClipCenterSection.width = Mathf.Round(treeViewRect.width - mixInRect.width - mixOutRect.width);
  351. m_ClipCenterSection.xMax = m_ClipCenterSection.xMin + m_ClipCenterSection.width;
  352. }
  353. // Entry point to the Clip Drawing...
  354. public override void Draw(Rect trackRect, bool trackRectChanged, WindowState state)
  355. {
  356. // if the clip has changed, fire the appropriate callback
  357. DetectClipChanged(trackRectChanged);
  358. // update the clip projected rectangle on the timeline
  359. CalculateClipRectangle(trackRect, state);
  360. AddToSpacePartitioner(state);
  361. // update the blend rects (when clip overlaps with others)
  362. CalculateBlendRect();
  363. // update the loop rects (when clip loops)
  364. CalculateLoopRects(trackRect, state);
  365. DrawExtrapolation(trackRect, treeViewRect);
  366. DrawInto(treeViewRect, state);
  367. ResetClipChanged();
  368. }
  369. void DetectClipChanged(bool trackRectChanged)
  370. {
  371. if (Event.current.type == EventType.Layout)
  372. {
  373. if (clip.DirtyIndex != m_LastDirtyIndex)
  374. {
  375. m_ClipViewDirty = true;
  376. try
  377. {
  378. m_ClipEditor.OnClipChanged(clip);
  379. }
  380. catch (Exception e)
  381. {
  382. Debug.LogException(e);
  383. }
  384. m_LastDirtyIndex = clip.DirtyIndex;
  385. }
  386. m_ClipViewDirty |= trackRectChanged;
  387. }
  388. }
  389. void ResetClipChanged()
  390. {
  391. if (Event.current.type == EventType.Repaint)
  392. m_ClipViewDirty = false;
  393. }
  394. internal void MoveToTop()
  395. {
  396. zOrder = zOrderProvider.Next();
  397. }
  398. GUIStyle GetExtrapolationIcon(TimelineClip.ClipExtrapolation mode)
  399. {
  400. GUIStyle extrapolationIcon = null;
  401. switch (mode)
  402. {
  403. case TimelineClip.ClipExtrapolation.None: return null;
  404. case TimelineClip.ClipExtrapolation.Hold: extrapolationIcon = m_Styles.extrapolationHold; break;
  405. case TimelineClip.ClipExtrapolation.Loop: extrapolationIcon = m_Styles.extrapolationLoop; break;
  406. case TimelineClip.ClipExtrapolation.PingPong: extrapolationIcon = m_Styles.extrapolationPingPong; break;
  407. case TimelineClip.ClipExtrapolation.Continue: extrapolationIcon = m_Styles.extrapolationContinue; break;
  408. }
  409. return extrapolationIcon;
  410. }
  411. Rect GetPreExtrapolationBounds(Rect trackRect, Rect clipRect, GUIStyle icon)
  412. {
  413. float x = clipRect.xMin - (icon.fixedWidth + 10.0f);
  414. float y = trackRect.yMin + (trackRect.height - icon.fixedHeight) / 2.0f;
  415. if (previousClip != null)
  416. {
  417. float distance = Mathf.Abs(treeViewRect.xMin - previousClip.treeViewRect.xMax);
  418. if (distance < icon.fixedWidth)
  419. return new Rect(0.0f, 0.0f, 0.0f, 0.0f);
  420. if (distance < icon.fixedWidth + 20.0f)
  421. {
  422. float delta = (distance - icon.fixedWidth) / 2.0f;
  423. x = clipRect.xMin - (icon.fixedWidth + delta);
  424. }
  425. }
  426. return new Rect(x, y, icon.fixedWidth, icon.fixedHeight);
  427. }
  428. Rect GetPostExtrapolationBounds(Rect trackRect, Rect clipRect, GUIStyle icon)
  429. {
  430. float x = clipRect.xMax + 10.0f;
  431. float y = trackRect.yMin + (trackRect.height - icon.fixedHeight) / 2.0f;
  432. if (nextClip != null)
  433. {
  434. float distance = Mathf.Abs(nextClip.treeViewRect.xMin - treeViewRect.xMax);
  435. if (distance < icon.fixedWidth)
  436. return new Rect(0.0f, 0.0f, 0.0f, 0.0f);
  437. if (distance < icon.fixedWidth + 20.0f)
  438. {
  439. float delta = (distance - icon.fixedWidth) / 2.0f;
  440. x = clipRect.xMax + delta;
  441. }
  442. }
  443. return new Rect(x, y, icon.fixedWidth, icon.fixedHeight);
  444. }
  445. static void DrawExtrapolationIcon(Rect rect, GUIStyle icon)
  446. {
  447. GUI.Label(rect, GUIContent.none, icon);
  448. }
  449. void DrawExtrapolation(Rect trackRect, Rect clipRect)
  450. {
  451. if (clip.hasPreExtrapolation)
  452. {
  453. GUIStyle icon = GetExtrapolationIcon(clip.preExtrapolationMode);
  454. if (icon != null)
  455. {
  456. Rect iconBounds = GetPreExtrapolationBounds(trackRect, clipRect, icon);
  457. if (iconBounds.width > 1 && iconBounds.height > 1)
  458. DrawExtrapolationIcon(iconBounds, icon);
  459. }
  460. }
  461. if (clip.hasPostExtrapolation)
  462. {
  463. GUIStyle icon = GetExtrapolationIcon(clip.postExtrapolationMode);
  464. if (icon != null)
  465. {
  466. Rect iconBounds = GetPostExtrapolationBounds(trackRect, clipRect, icon);
  467. if (iconBounds.width > 1 && iconBounds.height > 1)
  468. DrawExtrapolationIcon(iconBounds, icon);
  469. }
  470. }
  471. }
  472. static Rect ProjectRectOnTimeline(Rect rect, Rect trackRect, WindowState state)
  473. {
  474. Rect newRect = rect;
  475. // transform clipRect into pixel-space
  476. newRect.x *= state.timeAreaScale.x;
  477. newRect.width *= state.timeAreaScale.x;
  478. newRect.x += state.timeAreaTranslation.x + trackRect.xMin;
  479. // adjust clipRect height and vertical centering
  480. const int clipPadding = 2;
  481. newRect.y = trackRect.y + clipPadding;
  482. newRect.height = trackRect.height - (2 * clipPadding);
  483. return newRect;
  484. }
  485. void CalculateLoopRects(Rect trackRect, WindowState state)
  486. {
  487. if (!m_ClipViewDirty)
  488. return;
  489. m_LoopRects.Clear();
  490. if (clip.duration < WindowState.kTimeEpsilon)
  491. return;
  492. var times = TimelineHelpers.GetLoopTimes(clip);
  493. var loopDuration = TimelineHelpers.GetLoopDuration(clip);
  494. m_MinLoopIndex = -1;
  495. // we have a hold, no need to compute all loops
  496. if (!supportsLooping)
  497. {
  498. if (times.Length > 1)
  499. {
  500. var t = times[1];
  501. float loopTime = (float)(clip.duration - t);
  502. m_LoopRects.Add(ProjectRectOnTimeline(new Rect((float)(t + clip.start), 0, loopTime, 0), trackRect, state));
  503. }
  504. return;
  505. }
  506. var range = state.timeAreaShownRange;
  507. var visibleStartTime = range.x - clip.start;
  508. var visibleEndTime = range.y - clip.start;
  509. for (int i = 1; i < times.Length; i++)
  510. {
  511. var t = times[i];
  512. // don't draw off screen loops
  513. if (t > visibleEndTime)
  514. break;
  515. float loopTime = Mathf.Min((float)(clip.duration - t), (float)loopDuration);
  516. var loopEnd = t + loopTime;
  517. if (loopEnd < visibleStartTime)
  518. continue;
  519. m_LoopRects.Add(ProjectRectOnTimeline(new Rect((float)(t + clip.start), 0, loopTime, 0), trackRect, state));
  520. if (m_MinLoopIndex == -1)
  521. m_MinLoopIndex = i;
  522. }
  523. }
  524. public override Rect RectToTimeline(Rect trackRect, WindowState state)
  525. {
  526. var offsetFromTimeSpaceToPixelSpace = state.timeAreaTranslation.x + trackRect.xMin;
  527. var start = (float)(DiscreteTime)clip.start;
  528. var end = (float)(DiscreteTime)clip.end;
  529. return Rect.MinMaxRect(
  530. Mathf.Round(start * state.timeAreaScale.x + offsetFromTimeSpaceToPixelSpace), Mathf.Round(trackRect.yMin),
  531. Mathf.Round(end * state.timeAreaScale.x + offsetFromTimeSpaceToPixelSpace), Mathf.Round(trackRect.yMax)
  532. );
  533. }
  534. public IEnumerable<Edge> SnappableEdgesFor(IAttractable attractable, ManipulateEdges manipulateEdges)
  535. {
  536. var edges = new List<Edge>();
  537. bool canAddEdges = !parent.muted;
  538. if (canAddEdges)
  539. {
  540. // Hack: Trim Start in Ripple mode should not have any snap point added
  541. if (EditMode.editType == EditMode.EditType.Ripple && manipulateEdges == ManipulateEdges.Left)
  542. return edges;
  543. if (attractable != this)
  544. {
  545. if (EditMode.editType == EditMode.EditType.Ripple)
  546. {
  547. bool skip = false;
  548. // Hack: Since Trim End and Move in Ripple mode causes other snap point to move on the same track (which is not supported), disable snapping for this special cases...
  549. // TODO Find a proper way to have different snap edges for each edit mode.
  550. if (manipulateEdges == ManipulateEdges.Right)
  551. {
  552. var otherClipGUI = attractable as TimelineClipGUI;
  553. skip = otherClipGUI != null && otherClipGUI.parent == parent;
  554. }
  555. else if (manipulateEdges == ManipulateEdges.Both)
  556. {
  557. var moveHandler = attractable as MoveItemHandler;
  558. skip = moveHandler != null && moveHandler.movingItems.Any(clips => clips.targetTrack == clip.GetParentTrack() && clip.start >= clips.start);
  559. }
  560. if (skip)
  561. return edges;
  562. }
  563. AddEdge(edges, clip.start);
  564. AddEdge(edges, clip.end);
  565. }
  566. else
  567. {
  568. if (manipulateEdges == ManipulateEdges.Right)
  569. {
  570. var d = TimelineHelpers.GetClipAssetEndTime(clip);
  571. if (d < double.MaxValue)
  572. {
  573. if (clip.SupportsLooping())
  574. {
  575. var l = TimelineHelpers.GetLoopDuration(clip);
  576. var shownTime = TimelineWindow.instance.state.timeAreaShownRange;
  577. do
  578. {
  579. AddEdge(edges, d, false);
  580. d += l;
  581. }
  582. while (d < shownTime.y);
  583. }
  584. else
  585. {
  586. AddEdge(edges, d, false);
  587. }
  588. }
  589. }
  590. if (manipulateEdges == ManipulateEdges.Left)
  591. {
  592. var clipInfo = AnimationClipCurveCache.Instance.GetCurveInfo(clip.animationClip);
  593. if (clipInfo != null && clipInfo.keyTimes.Any())
  594. AddEdge(edges, clip.FromLocalTimeUnbound(clipInfo.keyTimes.Min()), false);
  595. }
  596. }
  597. }
  598. return edges;
  599. }
  600. public bool ShouldSnapTo(ISnappable snappable)
  601. {
  602. return true;
  603. }
  604. bool ShowDrillIcon(PlayableDirector resolver)
  605. {
  606. if (!m_ShowDrillIcon.HasValue || TimelineWindow.instance.hierarchyChangedThisFrame)
  607. {
  608. var nestable = m_ClipEditor.supportsSubTimelines;
  609. m_ShowDrillIcon = nestable && resolver != null;
  610. if (m_ShowDrillIcon.Value)
  611. {
  612. s_TempSubDirectors.Clear();
  613. try
  614. {
  615. m_ClipEditor.GetSubTimelines(clip, resolver, s_TempSubDirectors);
  616. }
  617. catch (Exception e)
  618. {
  619. Debug.LogException(e);
  620. }
  621. m_ShowDrillIcon &= s_TempSubDirectors.Count > 0;
  622. }
  623. }
  624. return m_ShowDrillIcon.Value;
  625. }
  626. static void AddEdge(List<Edge> edges, double time, bool showEdgeHint = true)
  627. {
  628. var shownTime = TimelineWindow.instance.state.timeAreaShownRange;
  629. if (time >= shownTime.x && time <= shownTime.y)
  630. edges.Add(new Edge(time, showEdgeHint));
  631. }
  632. public void SelectCurves()
  633. {
  634. SelectionManager.SelectOnly(clip);
  635. SelectionManager.SelectInlineCurveEditor(this);
  636. }
  637. public void ValidateCurvesSelection()
  638. {
  639. if (!IsSelected()) //if clip is not selected, deselect the inline curve
  640. SelectionManager.SelectInlineCurveEditor(null);
  641. }
  642. }
  643. }