TimelineInspectorUtility.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  1. using System;
  2. using System.Linq;
  3. using UnityEngine;
  4. using UnityEngine.Timeline;
  5. namespace UnityEditor.Timeline
  6. {
  7. [Flags]
  8. enum InputEvent
  9. {
  10. None = 0,
  11. DragEnter = 1,
  12. DragExit = 2,
  13. Drag = 4,
  14. KeyboardInput = 8
  15. }
  16. static class InputEventMethods
  17. {
  18. public static bool InputHasBegun(this InputEvent evt)
  19. {
  20. return evt == InputEvent.DragEnter || evt == InputEvent.KeyboardInput;
  21. }
  22. }
  23. static class TimelineInspectorUtility
  24. {
  25. internal static class Styles
  26. {
  27. public static readonly GUIContent SecondsPrefix = EditorGUIUtility.TrTextContent("s", "Seconds");
  28. public static readonly GUIContent FramesPrefix = EditorGUIUtility.TrTextContent("f", "Frames");
  29. }
  30. public static void TimeField(SerializedProperty property, GUIContent label, bool readOnly, double frameRate, double minValue, double maxValue, ref InputEvent inputEvent)
  31. {
  32. var rect = EditorGUILayout.GetControlRect();
  33. TimeField(rect, property, label, readOnly, frameRate, minValue, maxValue, ref inputEvent);
  34. }
  35. // Display Time related properties in frames and seconds
  36. public static void TimeField(Rect rect, SerializedProperty property, GUIContent label, bool readOnly, double frameRate, double minValue, double maxValue, ref InputEvent inputEvent)
  37. {
  38. GUIContent title = EditorGUI.BeginProperty(rect, label, property);
  39. rect = EditorGUI.PrefixLabel(rect, title);
  40. int indentLevel = EditorGUI.indentLevel;
  41. float labelWidth = EditorGUIUtility.labelWidth;
  42. EditorGUI.indentLevel = 0;
  43. EditorGUIUtility.labelWidth = (int)EditorGUI.kMiniLabelW;
  44. using (new GUIMixedValueScope(property.hasMultipleDifferentValues))
  45. {
  46. var secondsRect = new Rect(rect.xMin, rect.yMin, rect.width / 2 - EditorGUI.kSpacingSubLabel, rect.height);
  47. var framesRect = new Rect(rect.xMin + rect.width / 2, rect.yMin, rect.width / 2, rect.height);
  48. if (readOnly)
  49. {
  50. EditorGUI.FloatField(secondsRect, Styles.SecondsPrefix, (float)property.doubleValue, EditorStyles.label);
  51. }
  52. else
  53. {
  54. EditorGUI.BeginChangeCheck();
  55. DelayedAndDraggableDoubleField(secondsRect, Styles.SecondsPrefix, property, ref inputEvent);
  56. if (EditorGUI.EndChangeCheck())
  57. {
  58. property.doubleValue = Clamp(property.doubleValue, minValue, maxValue);
  59. }
  60. }
  61. if (frameRate > TimeUtility.kTimeEpsilon)
  62. {
  63. EditorGUI.BeginChangeCheck();
  64. double time = property.doubleValue;
  65. int frames = TimeUtility.ToFrames(time, frameRate);
  66. double exactFrames = TimeUtility.ToExactFrames(time, frameRate);
  67. bool useIntField = TimeUtility.OnFrameBoundary(time, frameRate);
  68. if (readOnly)
  69. {
  70. if (useIntField)
  71. EditorGUI.IntField(framesRect, Styles.FramesPrefix, frames, EditorStyles.label);
  72. else
  73. EditorGUI.DoubleField(framesRect, Styles.FramesPrefix, exactFrames, EditorStyles.label);
  74. }
  75. else
  76. {
  77. if (useIntField)
  78. {
  79. int newFrames = DelayedAndDraggableIntField(framesRect, Styles.FramesPrefix, frames, ref inputEvent);
  80. time = Math.Max(0, TimeUtility.FromFrames(newFrames, frameRate));
  81. }
  82. else
  83. {
  84. double newExactFrames = DelayedAndDraggableDoubleField(framesRect, Styles.FramesPrefix, exactFrames, ref inputEvent);
  85. time = Math.Max(0, TimeUtility.FromFrames((int)Math.Floor(newExactFrames), frameRate));
  86. }
  87. }
  88. if (EditorGUI.EndChangeCheck())
  89. {
  90. property.doubleValue = Clamp(time, minValue, maxValue);
  91. }
  92. }
  93. EditorGUI.indentLevel = indentLevel;
  94. EditorGUIUtility.labelWidth = labelWidth;
  95. EditorGUI.EndProperty();
  96. }
  97. }
  98. public static double TimeFieldUsingTimeReference(
  99. GUIContent label, double time, bool readOnly, bool showMixed, double frameRate, double minValue,
  100. double maxValue, ref InputEvent inputEvent)
  101. {
  102. var state = TimelineWindow.instance.state;
  103. var needsTimeConversion = state != null && state.timeReferenceMode == TimeReferenceMode.Global;
  104. if (needsTimeConversion)
  105. time = state.editSequence.ToGlobalTime(time);
  106. var t = TimeField(label, time, readOnly, showMixed, frameRate, minValue, maxValue, ref inputEvent);
  107. if (needsTimeConversion)
  108. t = state.editSequence.ToLocalTime(t);
  109. return t;
  110. }
  111. public static double DurationFieldUsingTimeReference(
  112. GUIContent label, double start, double end, bool readOnly, bool showMixed, double frameRate,
  113. double minValue, double maxValue, ref InputEvent inputEvent)
  114. {
  115. var state = TimelineWindow.instance.state;
  116. var needsTimeConversion = state != null && state.timeReferenceMode == TimeReferenceMode.Global;
  117. if (needsTimeConversion)
  118. {
  119. start = state.editSequence.ToGlobalTime(start);
  120. end = state.editSequence.ToGlobalTime(end);
  121. }
  122. var duration = end - start;
  123. var t = TimeField(label, duration, readOnly, showMixed, frameRate, minValue, maxValue, ref inputEvent);
  124. end = start + t;
  125. if (needsTimeConversion)
  126. {
  127. start = state.editSequence.ToLocalTime(start);
  128. end = state.editSequence.ToLocalTime(end);
  129. }
  130. return end - start;
  131. }
  132. public static double TimeField(Rect rect, GUIContent label, double time, bool readOnly, bool showMixed, double frameRate, double minValue, double maxValue, ref InputEvent inputEvent)
  133. {
  134. EditorGUILayout.BeginHorizontal(label, GUIStyle.none);
  135. rect = EditorGUI.PrefixLabel(rect, label);
  136. int indentLevel = EditorGUI.indentLevel;
  137. float labelWidth = EditorGUIUtility.labelWidth;
  138. EditorGUI.indentLevel = 0;
  139. EditorGUIUtility.labelWidth = (int)EditorGUI.kMiniLabelW;
  140. using (new GUIMixedValueScope(showMixed))
  141. {
  142. var secondsRect = new Rect(rect.xMin, rect.yMin, rect.width / 2 - EditorGUI.kSpacingSubLabel, rect.height);
  143. var framesRect = new Rect(rect.xMin + rect.width / 2, rect.yMin, rect.width / 2, rect.height);
  144. if (readOnly)
  145. {
  146. EditorGUI.FloatField(secondsRect, Styles.SecondsPrefix, (float)time, EditorStyles.label);
  147. }
  148. else
  149. {
  150. time = DelayedAndDraggableDoubleField(secondsRect, Styles.SecondsPrefix, time, ref inputEvent);
  151. }
  152. if (frameRate > TimeUtility.kTimeEpsilon)
  153. {
  154. int frames = TimeUtility.ToFrames(time, frameRate);
  155. double exactFrames = TimeUtility.ToExactFrames(time, frameRate);
  156. bool useIntField = TimeUtility.OnFrameBoundary(time, frameRate);
  157. if (readOnly)
  158. {
  159. if (useIntField)
  160. EditorGUI.IntField(framesRect, Styles.FramesPrefix, frames, EditorStyles.label);
  161. else
  162. EditorGUI.FloatField(framesRect, Styles.FramesPrefix, (float)exactFrames, EditorStyles.label);
  163. }
  164. else
  165. {
  166. double newTime;
  167. EditorGUI.BeginChangeCheck();
  168. if (useIntField)
  169. {
  170. int newFrames = DelayedAndDraggableIntField(framesRect, Styles.FramesPrefix, frames, ref inputEvent);
  171. newTime = Math.Max(0, TimeUtility.FromFrames(newFrames, frameRate));
  172. }
  173. else
  174. {
  175. double newExactFrames = DelayedAndDraggableDoubleField(framesRect, Styles.FramesPrefix, exactFrames, ref inputEvent);
  176. newTime = Math.Max(0, TimeUtility.FromFrames((int)Math.Floor(newExactFrames), frameRate));
  177. }
  178. if (EditorGUI.EndChangeCheck())
  179. {
  180. time = newTime;
  181. }
  182. }
  183. }
  184. EditorGUILayout.EndHorizontal();
  185. EditorGUI.indentLevel = indentLevel;
  186. EditorGUIUtility.labelWidth = labelWidth;
  187. }
  188. return Clamp(time, minValue, maxValue);
  189. }
  190. public static double TimeField(GUIContent label, double time, bool readOnly, bool showMixed, double frameRate, double minValue, double maxValue, ref InputEvent inputEvent)
  191. {
  192. var rect = EditorGUILayout.GetControlRect();
  193. return TimeField(rect, label, time, readOnly, showMixed, frameRate, minValue, maxValue, ref inputEvent);
  194. }
  195. static InputEvent InputEventType(Rect rect, int id)
  196. {
  197. var evt = Event.current;
  198. switch (evt.GetTypeForControl(id))
  199. {
  200. case EventType.MouseDown:
  201. if (rect.Contains(evt.mousePosition) && evt.button == 0)
  202. {
  203. return InputEvent.DragEnter;
  204. }
  205. break;
  206. case EventType.MouseUp:
  207. if (GUIUtility.hotControl == id)
  208. {
  209. return InputEvent.DragExit;
  210. }
  211. break;
  212. case EventType.MouseDrag:
  213. if (GUIUtility.hotControl == id)
  214. {
  215. return InputEvent.Drag;
  216. }
  217. break;
  218. case EventType.KeyDown:
  219. if (GUIUtility.hotControl == id && evt.keyCode == KeyCode.Escape)
  220. {
  221. return InputEvent.DragExit;
  222. }
  223. break;
  224. }
  225. return InputEvent.None;
  226. }
  227. static double DelayedAndDraggableDoubleField(Rect rect, GUIContent label, double value, ref InputEvent inputEvent, double dragSensitivity)
  228. {
  229. var id = GUIUtility.GetControlID(FocusType.Keyboard);
  230. var fieldRect = EditorGUI.PrefixLabel(rect, id, label);
  231. rect.xMax = fieldRect.x;
  232. double refValue = value;
  233. long dummy = 0;
  234. inputEvent |= InputEventType(rect, id);
  235. EditorGUI.DragNumberValue(rect, id, true, ref refValue, ref dummy, dragSensitivity);
  236. EditorGUI.BeginChangeCheck();
  237. var result = EditorGUI.DelayedDoubleFieldInternal(fieldRect, GUIContent.none, refValue, EditorStyles.numberField);
  238. if (EditorGUI.EndChangeCheck())
  239. inputEvent |= InputEvent.KeyboardInput;
  240. return result;
  241. }
  242. static int DelayedAndDraggableIntField(Rect rect, GUIContent label, int value, ref InputEvent inputEvent, long dragSensitivity)
  243. {
  244. var id = GUIUtility.GetControlID(FocusType.Keyboard);
  245. var fieldRect = EditorGUI.PrefixLabel(rect, id, label);
  246. rect.xMax = fieldRect.x;
  247. double dummy = 0.0;
  248. long refValue = value;
  249. inputEvent |= InputEventType(rect, id);
  250. EditorGUI.DragNumberValue(rect, id, false, ref dummy, ref refValue, dragSensitivity);
  251. EditorGUI.BeginChangeCheck();
  252. var result = EditorGUI.DelayedIntFieldInternal(fieldRect, GUIContent.none, (int)refValue, EditorStyles.numberField);
  253. if (EditorGUI.EndChangeCheck())
  254. inputEvent |= InputEvent.KeyboardInput;
  255. return result;
  256. }
  257. internal static double DelayedAndDraggableDoubleField(GUIContent label, double value, ref InputEvent action, double dragSensitivity)
  258. {
  259. var r = EditorGUILayout.s_LastRect = EditorGUILayout.GetControlRect(false, EditorGUI.kSingleLineHeight);
  260. return DelayedAndDraggableDoubleField(r, label, value, ref action, dragSensitivity);
  261. }
  262. static void DelayedAndDraggableDoubleField(Rect rect, GUIContent label, SerializedProperty property, ref InputEvent inputEvent)
  263. {
  264. EditorGUI.BeginChangeCheck();
  265. var newValue = DelayedAndDraggableDoubleField(rect, label, property.doubleValue, ref inputEvent);
  266. if (EditorGUI.EndChangeCheck())
  267. property.doubleValue = newValue;
  268. }
  269. static double DelayedAndDraggableDoubleField(Rect rect, GUIContent label, double value, ref InputEvent inputEvent)
  270. {
  271. var dragSensitivity = NumericFieldDraggerUtility.CalculateFloatDragSensitivity(value);
  272. return DelayedAndDraggableDoubleField(rect, label, value, ref inputEvent, dragSensitivity);
  273. }
  274. static int DelayedAndDraggableIntField(Rect rect, GUIContent label, int value, ref InputEvent inputEvent)
  275. {
  276. var dragSensitivity = NumericFieldDraggerUtility.CalculateIntDragSensitivity(value);
  277. return DelayedAndDraggableIntField(rect, label, value, ref inputEvent, dragSensitivity);
  278. }
  279. internal static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
  280. {
  281. if (val.CompareTo(min) < 0) return min;
  282. if (val.CompareTo(max) > 0) return max;
  283. return val;
  284. }
  285. public static Editor GetInspectorForObjects(UnityEngine.Object[] objects)
  286. {
  287. // create cached editor throws on assembly reload...
  288. try
  289. {
  290. if (objects.Any(x => x != null))
  291. {
  292. var director = TimelineWindow.instance.state.editSequence.director;
  293. return Editor.CreateEditorWithContext(objects, director, null);
  294. }
  295. }
  296. catch (Exception)
  297. {}
  298. return null;
  299. }
  300. }
  301. }