PageRenderTime 57ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/Library/PackageCache/com.unity.timeline@1.2.18/Editor/Utilities/ClipModifier.cs

https://gitlab.com/hieplv.amgame/c0lor-blocks
C# | 386 lines | 296 code | 84 blank | 6 comment | 48 complexity | ed56d2480de896446bd072fe95850bce MD5 | raw file
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. using UnityEngine.Playables;
  6. namespace UnityEditor.Timeline
  7. {
  8. static class ClipModifier
  9. {
  10. public static bool Delete(TimelineAsset timeline, TimelineClip clip)
  11. {
  12. return timeline.DeleteClip(clip);
  13. }
  14. public static bool Tile(TimelineClip[] clips)
  15. {
  16. if (clips.Length < 2)
  17. return false;
  18. var clipsByTracks = clips.GroupBy(x => x.parentTrack)
  19. .Select(track => new {track.Key, Items = track.OrderBy(c => c.start)});
  20. foreach (var track in clipsByTracks)
  21. {
  22. TimelineUndo.PushUndo(track.Key, "Tile");
  23. }
  24. foreach (var track in clipsByTracks)
  25. {
  26. double newStart = track.Items.First().start;
  27. foreach (var c in track.Items)
  28. {
  29. c.start = newStart;
  30. newStart += c.duration;
  31. }
  32. }
  33. return true;
  34. }
  35. public static bool TrimStart(TimelineClip[] clips, double trimTime)
  36. {
  37. var result = false;
  38. foreach (var clip in clips)
  39. result |= TrimStart(clip, trimTime);
  40. return result;
  41. }
  42. public static bool TrimStart(TimelineClip clip, double trimTime)
  43. {
  44. if (clip.asset == null)
  45. return false;
  46. if (clip.start > trimTime)
  47. return false;
  48. if (clip.end < trimTime)
  49. return false;
  50. TimelineUndo.PushUndo(clip.parentTrack, "Trim Clip Start");
  51. // Note: We are NOT using edit modes in this case because we want the same result
  52. // regardless of the selected EditMode: split at cursor and delete left part
  53. SetStart(clip, trimTime);
  54. return true;
  55. }
  56. public static bool TrimEnd(TimelineClip[] clips, double trimTime)
  57. {
  58. var result = false;
  59. foreach (var clip in clips)
  60. result |= TrimEnd(clip, trimTime);
  61. return result;
  62. }
  63. public static bool TrimEnd(TimelineClip clip, double trimTime)
  64. {
  65. if (clip.asset == null)
  66. return false;
  67. if (clip.start > trimTime)
  68. return false;
  69. if (clip.end < trimTime)
  70. return false;
  71. TimelineUndo.PushUndo(clip.parentTrack, "Trim Clip End");
  72. TrimClipWithEditMode(clip, TrimEdge.End, trimTime);
  73. return true;
  74. }
  75. public static bool MatchDuration(TimelineClip[] clips)
  76. {
  77. double referenceDuration = clips[0].duration;
  78. foreach (var clip in clips)
  79. {
  80. TimelineUndo.PushUndo(clip.parentTrack, "Match Clip Duration");
  81. var newEnd = clip.start + referenceDuration;
  82. TrimClipWithEditMode(clip, TrimEdge.End, newEnd);
  83. }
  84. return true;
  85. }
  86. public static bool Split(TimelineClip[] clips, double splitTime, PlayableDirector director)
  87. {
  88. var result = false;
  89. foreach (var clip in clips)
  90. {
  91. if (clip.start >= splitTime)
  92. continue;
  93. if (clip.end <= splitTime)
  94. continue;
  95. TimelineUndo.PushUndo(clip.parentTrack, "Split Clip");
  96. TimelineClip newClip = TimelineHelpers.Clone(clip, director, director, clip.start);
  97. SetStart(clip, splitTime);
  98. SetEnd(newClip, splitTime, false);
  99. // Sort produced by cloning clips on top of each other is unpredictable (it varies between mono runtimes)
  100. clip.parentTrack.SortClips();
  101. result = true;
  102. }
  103. return result;
  104. }
  105. public static void SetStart(TimelineClip clip, double time)
  106. {
  107. var supportsClipIn = clip.SupportsClipIn();
  108. var supportsPadding = TimelineUtility.IsRecordableAnimationClip(clip);
  109. // treat empty recordable clips as not supporting clip in (there are no keys to modify)
  110. if (supportsPadding && (clip.animationClip == null || clip.animationClip.empty))
  111. {
  112. supportsClipIn = false;
  113. }
  114. if (supportsClipIn && !supportsPadding)
  115. {
  116. var minStart = clip.FromLocalTimeUnbound(0.0);
  117. if (time < minStart)
  118. time = minStart;
  119. }
  120. var maxStart = clip.end - TimelineClip.kMinDuration;
  121. if (time > maxStart)
  122. time = maxStart;
  123. var timeOffset = time - clip.start;
  124. var duration = clip.duration - timeOffset;
  125. if (supportsClipIn)
  126. {
  127. if (supportsPadding)
  128. {
  129. double clipInGlobal = clip.clipIn / clip.timeScale;
  130. double keyShift = -timeOffset;
  131. if (timeOffset < 0) // left drag, eliminate clipIn before shifting
  132. {
  133. double clipInDelta = Math.Max(-clipInGlobal, timeOffset);
  134. keyShift = -Math.Min(0, timeOffset - clipInDelta);
  135. clip.clipIn += clipInDelta * clip.timeScale;
  136. }
  137. else if (timeOffset > 0) // right drag, elimate padding in animation clip before adding clip in
  138. {
  139. var clipInfo = AnimationClipCurveCache.Instance.GetCurveInfo(clip.animationClip);
  140. double keyDelta = clip.FromLocalTimeUnbound(clipInfo.keyTimes.Min()) - clip.start;
  141. keyShift = -Math.Max(0, Math.Min(timeOffset, keyDelta));
  142. clip.clipIn += Math.Max(timeOffset + keyShift, 0) * clip.timeScale;
  143. }
  144. if (keyShift != 0)
  145. {
  146. AnimationTrackRecorder.ShiftAnimationClip(clip.animationClip, (float)(keyShift * clip.timeScale));
  147. }
  148. }
  149. else
  150. {
  151. clip.clipIn += timeOffset * clip.timeScale;
  152. }
  153. }
  154. clip.start = time;
  155. clip.duration = duration;
  156. }
  157. public static void SetEnd(TimelineClip clip, double time, bool affectTimeScale)
  158. {
  159. var duration = Math.Max(time - clip.start, TimelineClip.kMinDuration);
  160. if (affectTimeScale && clip.SupportsSpeedMultiplier())
  161. {
  162. var f = clip.duration / duration;
  163. clip.timeScale *= f;
  164. }
  165. clip.duration = duration;
  166. }
  167. public static bool ResetEditing(TimelineClip[] clips)
  168. {
  169. var result = false;
  170. foreach (var clip in clips)
  171. result = result || ResetEditing(clip);
  172. return result;
  173. }
  174. public static bool ResetEditing(TimelineClip clip)
  175. {
  176. if (clip.asset == null)
  177. return false;
  178. TimelineUndo.PushUndo(clip.parentTrack, "Reset Clip Editing");
  179. clip.clipIn = 0.0;
  180. if (clip.clipAssetDuration < double.MaxValue)
  181. {
  182. var duration = clip.clipAssetDuration / clip.timeScale;
  183. TrimClipWithEditMode(clip, TrimEdge.End, clip.start + duration);
  184. }
  185. return true;
  186. }
  187. public static bool MatchContent(TimelineClip[] clips)
  188. {
  189. var result = false;
  190. foreach (var clip in clips)
  191. result = result || MatchContent(clip);
  192. return result;
  193. }
  194. public static bool MatchContent(TimelineClip clip)
  195. {
  196. if (clip.asset == null)
  197. return false;
  198. TimelineUndo.PushUndo(clip.parentTrack, "Match Clip Content");
  199. var newStartCandidate = clip.start - clip.clipIn / clip.timeScale;
  200. var newStart = newStartCandidate < 0.0 ? 0.0 : newStartCandidate;
  201. TrimClipWithEditMode(clip, TrimEdge.Start, newStart);
  202. // In case resetting the start was blocked by edit mode or timeline start, we do the best we can
  203. clip.clipIn = (clip.start - newStartCandidate) * clip.timeScale;
  204. if (clip.clipAssetDuration > 0 && TimelineHelpers.HasUsableAssetDuration(clip))
  205. {
  206. var duration = TimelineHelpers.GetLoopDuration(clip);
  207. var offset = (clip.clipIn / clip.timeScale) % duration;
  208. TrimClipWithEditMode(clip, TrimEdge.End, clip.start - offset + duration);
  209. }
  210. return true;
  211. }
  212. public static void TrimClipWithEditMode(TimelineClip clip, TrimEdge edge, double time)
  213. {
  214. var clipItem = ItemsUtils.ToItem(clip);
  215. EditMode.BeginTrim(clipItem, edge);
  216. if (edge == TrimEdge.Start)
  217. EditMode.TrimStart(clipItem, time);
  218. else
  219. EditMode.TrimEnd(clipItem, time, false);
  220. EditMode.FinishTrim();
  221. }
  222. public static bool CompleteLastLoop(TimelineClip[] clips)
  223. {
  224. foreach (var clip in clips)
  225. {
  226. CompleteLastLoop(clip);
  227. }
  228. return true;
  229. }
  230. public static void CompleteLastLoop(TimelineClip clip)
  231. {
  232. FixLoops(clip, true);
  233. }
  234. public static bool TrimLastLoop(TimelineClip[] clips)
  235. {
  236. foreach (var clip in clips)
  237. {
  238. TrimLastLoop(clip);
  239. }
  240. return true;
  241. }
  242. public static void TrimLastLoop(TimelineClip clip)
  243. {
  244. FixLoops(clip, false);
  245. }
  246. static void FixLoops(TimelineClip clip, bool completeLastLoop)
  247. {
  248. if (!TimelineHelpers.HasUsableAssetDuration(clip))
  249. return;
  250. var loopDuration = TimelineHelpers.GetLoopDuration(clip);
  251. var firstLoopDuration = loopDuration - clip.clipIn * (1.0 / clip.timeScale);
  252. // Making sure we don't trim to zero
  253. if (!completeLastLoop && firstLoopDuration > clip.duration)
  254. return;
  255. var numLoops = (clip.duration - firstLoopDuration) / loopDuration;
  256. var numCompletedLoops = Math.Floor(numLoops);
  257. if (!(numCompletedLoops < numLoops))
  258. return;
  259. if (completeLastLoop)
  260. numCompletedLoops += 1;
  261. var newEnd = clip.start + firstLoopDuration + loopDuration * numCompletedLoops;
  262. TimelineUndo.PushUndo(clip.parentTrack, "Trim Clip Last Loop");
  263. TrimClipWithEditMode(clip, TrimEdge.End, newEnd);
  264. }
  265. public static bool DoubleSpeed(TimelineClip[] clips)
  266. {
  267. foreach (var clip in clips)
  268. {
  269. if (clip.SupportsSpeedMultiplier())
  270. {
  271. TimelineUndo.PushUndo(clip.parentTrack, "Double Clip Speed");
  272. clip.timeScale = clip.timeScale * 2.0f;
  273. }
  274. }
  275. return true;
  276. }
  277. public static bool HalfSpeed(TimelineClip[] clips)
  278. {
  279. foreach (var clip in clips)
  280. {
  281. if (clip.SupportsSpeedMultiplier())
  282. {
  283. TimelineUndo.PushUndo(clip.parentTrack, "Half Clip Speed");
  284. clip.timeScale = clip.timeScale * 0.5f;
  285. }
  286. }
  287. return true;
  288. }
  289. public static bool ResetSpeed(TimelineClip[] clips)
  290. {
  291. foreach (var clip in clips)
  292. {
  293. if (clip.timeScale != 1.0)
  294. {
  295. TimelineUndo.PushUndo(clip.parentTrack, "Reset Clip Speed");
  296. clip.timeScale = 1.0;
  297. }
  298. }
  299. return true;
  300. }
  301. }
  302. }