Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

TimelineInspectorUtility.cs 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 = L10n.TextContent("s", "Seconds");
  28. public static readonly GUIContent FramesPrefix = L10n.TextContent("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. using (var propertyScope = new PropertyScope(rect, label, property))
  39. {
  40. GUIContent title = propertyScope.content;
  41. rect = EditorGUI.PrefixLabel(rect, title);
  42. using (new IndentLevelScope(0))
  43. using (new LabelWidthScope((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. }
  94. }
  95. }
  96. public static double TimeFieldUsingTimeReference(
  97. GUIContent label, double time, bool readOnly, bool showMixed, double frameRate, double minValue,
  98. double maxValue, ref InputEvent inputEvent)
  99. {
  100. var state = TimelineWindow.instance.state;
  101. var needsTimeConversion = state != null && state.timeReferenceMode == TimeReferenceMode.Global;
  102. if (needsTimeConversion)
  103. time = state.editSequence.ToGlobalTime(time);
  104. var t = TimeField(label, time, readOnly, showMixed, frameRate, minValue, maxValue, ref inputEvent);
  105. if (needsTimeConversion)
  106. t = state.editSequence.ToLocalTime(t);
  107. return t;
  108. }
  109. public static double DurationFieldUsingTimeReference(
  110. GUIContent label, double start, double end, bool readOnly, bool showMixed, double frameRate,
  111. double minValue, double maxValue, ref InputEvent inputEvent)
  112. {
  113. var state = TimelineWindow.instance.state;
  114. var needsTimeConversion = state != null && state.timeReferenceMode == TimeReferenceMode.Global;
  115. if (needsTimeConversion)
  116. {
  117. start = state.editSequence.ToGlobalTime(start);
  118. end = state.editSequence.ToGlobalTime(end);
  119. }
  120. var duration = end - start;
  121. var t = TimeField(label, duration, readOnly, showMixed, frameRate, minValue, maxValue, ref inputEvent);
  122. end = start + t;
  123. if (needsTimeConversion)
  124. {
  125. start = state.editSequence.ToLocalTime(start);
  126. end = state.editSequence.ToLocalTime(end);
  127. }
  128. return end - start;
  129. }
  130. public static double TimeField(Rect rect, GUIContent label, double time, bool readOnly, bool showMixed, double frameRate, double minValue, double maxValue, ref InputEvent inputEvent)
  131. {
  132. using (new HorizontalScope(label, GUIStyle.none))
  133. {
  134. rect = EditorGUI.PrefixLabel(rect, label);
  135. using (new IndentLevelScope(0))
  136. using (new LabelWidthScope((int)EditorGUI.kMiniLabelW))
  137. using (new GUIMixedValueScope(showMixed))
  138. {
  139. var secondsRect = new Rect(rect.xMin, rect.yMin, rect.width / 2 - EditorGUI.kSpacingSubLabel, rect.height);
  140. var framesRect = new Rect(rect.xMin + rect.width / 2, rect.yMin, rect.width / 2, rect.height);
  141. if (readOnly)
  142. {
  143. EditorGUI.FloatField(secondsRect, Styles.SecondsPrefix, (float)time, EditorStyles.label);
  144. }
  145. else
  146. {
  147. time = DelayedAndDraggableDoubleField(secondsRect, Styles.SecondsPrefix, time, ref inputEvent);
  148. }
  149. if (frameRate > TimeUtility.kTimeEpsilon)
  150. {
  151. int frames = TimeUtility.ToFrames(time, frameRate);
  152. double exactFrames = TimeUtility.ToExactFrames(time, frameRate);
  153. bool useIntField = TimeUtility.OnFrameBoundary(time, frameRate);
  154. if (readOnly)
  155. {
  156. if (useIntField)
  157. EditorGUI.IntField(framesRect, Styles.FramesPrefix, frames, EditorStyles.label);
  158. else
  159. EditorGUI.FloatField(framesRect, Styles.FramesPrefix, (float)exactFrames, EditorStyles.label);
  160. }
  161. else
  162. {
  163. double newTime;
  164. EditorGUI.BeginChangeCheck();
  165. if (useIntField)
  166. {
  167. int newFrames = DelayedAndDraggableIntField(framesRect, Styles.FramesPrefix, frames, ref inputEvent);
  168. newTime = Math.Max(0, TimeUtility.FromFrames(newFrames, frameRate));
  169. }
  170. else
  171. {
  172. double newExactFrames = DelayedAndDraggableDoubleField(framesRect, Styles.FramesPrefix, exactFrames, ref inputEvent);
  173. newTime = Math.Max(0, TimeUtility.FromFrames((int)Math.Floor(newExactFrames), frameRate));
  174. }
  175. if (EditorGUI.EndChangeCheck())
  176. {
  177. time = newTime;
  178. }
  179. }
  180. }
  181. }
  182. }
  183. return Clamp(time, minValue, maxValue);
  184. }
  185. public static double TimeField(GUIContent label, double time, bool readOnly, bool showMixed, double frameRate, double minValue, double maxValue, ref InputEvent inputEvent)
  186. {
  187. var rect = EditorGUILayout.GetControlRect();
  188. return TimeField(rect, label, time, readOnly, showMixed, frameRate, minValue, maxValue, ref inputEvent);
  189. }
  190. static InputEvent InputEventType(Rect rect, int id)
  191. {
  192. var evt = Event.current;
  193. switch (evt.GetTypeForControl(id))
  194. {
  195. case EventType.MouseDown:
  196. if (rect.Contains(evt.mousePosition) && evt.button == 0)
  197. {
  198. return InputEvent.DragEnter;
  199. }
  200. break;
  201. case EventType.MouseUp:
  202. if (GUIUtility.hotControl == id)
  203. {
  204. return InputEvent.DragExit;
  205. }
  206. break;
  207. case EventType.MouseDrag:
  208. if (GUIUtility.hotControl == id)
  209. {
  210. return InputEvent.Drag;
  211. }
  212. break;
  213. case EventType.KeyDown:
  214. if (GUIUtility.hotControl == id && evt.keyCode == KeyCode.Escape)
  215. {
  216. return InputEvent.DragExit;
  217. }
  218. break;
  219. }
  220. return InputEvent.None;
  221. }
  222. static double DelayedAndDraggableDoubleField(Rect rect, GUIContent label, double value, ref InputEvent inputEvent, double dragSensitivity)
  223. {
  224. var id = GUIUtility.GetControlID(FocusType.Keyboard);
  225. var fieldRect = EditorGUI.PrefixLabel(rect, id, label);
  226. rect.xMax = fieldRect.x;
  227. double refValue = value;
  228. long dummy = 0;
  229. inputEvent |= InputEventType(rect, id);
  230. EditorGUI.DragNumberValue(rect, id, true, ref refValue, ref dummy, dragSensitivity);
  231. EditorGUI.BeginChangeCheck();
  232. var result = EditorGUI.DelayedDoubleFieldInternal(fieldRect, GUIContent.none, refValue, EditorStyles.numberField);
  233. if (EditorGUI.EndChangeCheck())
  234. inputEvent |= InputEvent.KeyboardInput;
  235. return result;
  236. }
  237. static int DelayedAndDraggableIntField(Rect rect, GUIContent label, int value, ref InputEvent inputEvent, long dragSensitivity)
  238. {
  239. var id = GUIUtility.GetControlID(FocusType.Keyboard);
  240. var fieldRect = EditorGUI.PrefixLabel(rect, id, label);
  241. rect.xMax = fieldRect.x;
  242. double dummy = 0.0;
  243. long refValue = value;
  244. inputEvent |= InputEventType(rect, id);
  245. EditorGUI.DragNumberValue(rect, id, false, ref dummy, ref refValue, dragSensitivity);
  246. EditorGUI.BeginChangeCheck();
  247. var result = EditorGUI.DelayedIntFieldInternal(fieldRect, GUIContent.none, (int)refValue, EditorStyles.numberField);
  248. if (EditorGUI.EndChangeCheck())
  249. inputEvent |= InputEvent.KeyboardInput;
  250. return result;
  251. }
  252. internal static double DelayedAndDraggableDoubleField(GUIContent label, double value, ref InputEvent action, double dragSensitivity)
  253. {
  254. var r = EditorGUILayout.s_LastRect = EditorGUILayout.GetControlRect(false, EditorGUI.kSingleLineHeight);
  255. return DelayedAndDraggableDoubleField(r, label, value, ref action, dragSensitivity);
  256. }
  257. static void DelayedAndDraggableDoubleField(Rect rect, GUIContent label, SerializedProperty property, ref InputEvent inputEvent)
  258. {
  259. EditorGUI.BeginChangeCheck();
  260. var newValue = DelayedAndDraggableDoubleField(rect, label, property.doubleValue, ref inputEvent);
  261. if (EditorGUI.EndChangeCheck())
  262. property.doubleValue = newValue;
  263. }
  264. static double DelayedAndDraggableDoubleField(Rect rect, GUIContent label, double value, ref InputEvent inputEvent)
  265. {
  266. var dragSensitivity = NumericFieldDraggerUtility.CalculateFloatDragSensitivity(value);
  267. return DelayedAndDraggableDoubleField(rect, label, value, ref inputEvent, dragSensitivity);
  268. }
  269. static int DelayedAndDraggableIntField(Rect rect, GUIContent label, int value, ref InputEvent inputEvent)
  270. {
  271. var dragSensitivity = NumericFieldDraggerUtility.CalculateIntDragSensitivity(value);
  272. return DelayedAndDraggableIntField(rect, label, value, ref inputEvent, dragSensitivity);
  273. }
  274. internal static T Clamp<T>(this T val, T min, T max) where T : IComparable<T>
  275. {
  276. if (val.CompareTo(min) < 0) return min;
  277. if (val.CompareTo(max) > 0) return max;
  278. return val;
  279. }
  280. public static Editor GetInspectorForObjects(UnityEngine.Object[] objects, Editor previousEditor)
  281. {
  282. // create cached editor throws on assembly reload...
  283. try
  284. {
  285. if (objects.Any(x => x != null))
  286. {
  287. var director = TimelineWindow.instance.state.editSequence.director;
  288. Editor.CreateCachedEditorWithContext(objects, director, null, ref previousEditor);
  289. return previousEditor;
  290. }
  291. }
  292. catch (Exception)
  293. { }
  294. return null;
  295. }
  296. public static SerializedProperty FindExposedReferenceTableFrom(UnityEngine.Object context)
  297. {
  298. const string exposedReferencesPropertyPath = "m_ExposedReferences";
  299. if (context as IExposedPropertyTable == null) return null;
  300. var so = new SerializedObject(context);
  301. return so.FindProperty(exposedReferencesPropertyPath);
  302. }
  303. }
  304. }