KeyframeInspectorWindow.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TreeEditor;
  4. using UnityEngine;
  5. using UnityEditor;
  6. namespace Funly.SkyStudio
  7. {
  8. public class KeyframeInspectorWindow : EditorWindow
  9. {
  10. public enum KeyType
  11. {
  12. None,
  13. Color,
  14. Numeric,
  15. SpherePoint
  16. }
  17. public static bool inspectorEnabled { get; private set; }
  18. public static SkyProfile profile { get; private set; }
  19. public static KeyType keyType { get; private set; }
  20. public static IKeyframeGroup group { get; private set; }
  21. public static IBaseKeyframe keyframe { get; private set; }
  22. private static KeyframeInspectorWindow _instance;
  23. private static float k_MinWidth = 350.0f;
  24. private static float k_MinHeight = 150.0f;
  25. public static void ShowWindow()
  26. {
  27. if (_instance == null) {
  28. _instance = CreateInstance<KeyframeInspectorWindow>();
  29. _instance.name = "Keyframe Inspector";
  30. _instance.titleContent = new GUIContent("Keyframe Inspector");
  31. _instance.minSize = new Vector2(k_MinWidth, k_MinHeight);
  32. }
  33. _instance.ShowUtility();
  34. }
  35. private void OnEnable()
  36. {
  37. inspectorEnabled = true;
  38. }
  39. private void OnDisable()
  40. {
  41. inspectorEnabled = false;
  42. }
  43. public void ForceRepaint()
  44. {
  45. Repaint();
  46. }
  47. private void Update()
  48. {
  49. // Make sure we only have 1 utility window open.
  50. if (this != _instance) {
  51. this.Close();
  52. return;
  53. }
  54. Repaint();
  55. }
  56. public static void SetKeyframeData(IBaseKeyframe keyframe, IKeyframeGroup group, KeyType keyType, SkyProfile profile)
  57. {
  58. KeyframeInspectorWindow.keyframe = keyframe;
  59. KeyframeInspectorWindow.@group = group;
  60. KeyframeInspectorWindow.keyType = keyType;
  61. KeyframeInspectorWindow.profile = profile;
  62. }
  63. private void OnGUI()
  64. {
  65. if (keyType == KeyType.None || keyframe == null)
  66. {
  67. keyType = KeyType.None;
  68. ShowEmptyState();
  69. return;
  70. }
  71. GUILayout.BeginVertical();
  72. GUILayout.Space(5);
  73. bool didModifyProfile = false;
  74. // Render time.
  75. didModifyProfile = RenderTimeValue();
  76. // Animation curve.
  77. didModifyProfile = RenderInterpolationCurveType();
  78. // Core layout for this type of keyframe.
  79. if (keyType == KeyType.Color)
  80. {
  81. didModifyProfile = RenderColorGUI();
  82. }
  83. else if (keyType == KeyType.Numeric)
  84. {
  85. didModifyProfile = RenderNumericGUI();
  86. } else if (keyType == KeyType.SpherePoint)
  87. {
  88. didModifyProfile = RenderSpherePointGUI();
  89. }
  90. GUILayout.FlexibleSpace();
  91. // Buttom buttons.
  92. GUILayout.BeginHorizontal();
  93. if (KeyFrameCount() > 1)
  94. {
  95. if (GUILayout.Button("Delete Keyframe"))
  96. {
  97. Undo.RecordObject(profile, "Deleting keyframe");
  98. @group.RemoveKeyFrame(keyframe);
  99. keyframe = null;
  100. didModifyProfile = true;
  101. Close();
  102. }
  103. }
  104. GUILayout.FlexibleSpace();
  105. if (GUILayout.Button("Close"))
  106. {
  107. Close();
  108. }
  109. GUILayout.EndHorizontal();
  110. GUILayout.Space(5);
  111. GUILayout.EndVertical();
  112. if (didModifyProfile)
  113. {
  114. @group.SortKeyframes();
  115. EditorUtility.SetDirty(profile);
  116. }
  117. }
  118. private void ShowEmptyState()
  119. {
  120. EditorGUILayout.HelpBox("No keyframe selected.", MessageType.Info);
  121. }
  122. private int KeyFrameCount()
  123. {
  124. return @group.GetKeyFrameCount();
  125. }
  126. private bool RenderColorGUI()
  127. {
  128. ColorKeyframe colorKeyFrame = keyframe as ColorKeyframe;
  129. if (colorKeyFrame == null)
  130. {
  131. return false;
  132. }
  133. bool didModify = false;
  134. EditorGUI.BeginChangeCheck();
  135. Color selectedColor = EditorGUILayout.ColorField(new GUIContent("Color"), colorKeyFrame.color);
  136. if (EditorGUI.EndChangeCheck()) {
  137. Undo.RecordObject(profile, "Keyframe color changed.");
  138. colorKeyFrame.color = selectedColor;
  139. didModify = true;
  140. }
  141. return didModify;
  142. }
  143. private bool RenderNumericGUI()
  144. {
  145. NumberKeyframe numberKeyframe = keyframe as NumberKeyframe;
  146. if (numberKeyframe == null)
  147. {
  148. return false;
  149. }
  150. NumberKeyframeGroup numberGroup = @group as NumberKeyframeGroup;
  151. if (numberGroup == null)
  152. {
  153. return false;
  154. }
  155. bool didModify = false;
  156. EditorGUI.BeginChangeCheck();
  157. float value = EditorGUILayout.FloatField(new GUIContent("Value"), numberKeyframe.value);
  158. if (EditorGUI.EndChangeCheck())
  159. {
  160. Undo.RecordObject(profile, "Keyframe numeric value changed.");
  161. numberKeyframe.value = Mathf.Clamp(value, numberGroup.minValue, numberGroup.maxValue);
  162. didModify = true;
  163. }
  164. return didModify;
  165. }
  166. private bool RenderSpherePointGUI()
  167. {
  168. bool didModify = false;
  169. SpherePointKeyframe spherePointKeyframe = keyframe as SpherePointKeyframe;
  170. if (spherePointKeyframe == null)
  171. {
  172. return false;
  173. }
  174. EditorGUILayout.BeginHorizontal();
  175. EditorGUI.BeginChangeCheck();
  176. SpherePoint selectedSpherePoint = SpherePointGUI.SpherePointField(spherePointKeyframe.spherePoint, true, keyframe.id);
  177. if (EditorGUI.EndChangeCheck())
  178. {
  179. spherePointKeyframe.spherePoint = selectedSpherePoint;
  180. didModify = true;
  181. }
  182. EditorGUILayout.EndHorizontal();
  183. EditorGUILayout.BeginHorizontal();
  184. EditorGUI.BeginChangeCheck();
  185. GUIStyle style = GUI.skin.button;
  186. EditorGUILayout.PrefixLabel(
  187. new GUIContent("Normalize Speed",
  188. "Adjust all keyframes so that there is a constant speed of animation between them."));
  189. GUILayout.Button(new GUIContent("Reposition All Keyframes..."), style);
  190. if (EditorGUI.EndChangeCheck())
  191. {
  192. Undo.RecordObject(profile, "Normalize keyframe speeds");
  193. didModify = true;
  194. SpherePointTimelineRow.RepositionKeyframesForConstantSpeed(group as SpherePointKeyframeGroup);
  195. }
  196. EditorGUILayout.EndHorizontal();
  197. return didModify;
  198. }
  199. public bool RenderInterpolationCurveType()
  200. {
  201. EditorGUI.BeginChangeCheck();
  202. InterpolationCurve selectedCurveType = (InterpolationCurve)EditorGUILayout.EnumPopup(
  203. new GUIContent("Animation Curve",
  204. "Adjust animation curve timing to the next keyframe." ), keyframe.interpolationCurve);
  205. if (EditorGUI.EndChangeCheck())
  206. {
  207. Undo.RecordObject(profile, "Curve type changed");
  208. keyframe.interpolationCurve = selectedCurveType;
  209. return true;
  210. }
  211. return false;
  212. }
  213. public bool RenderInterpolationDirectionType()
  214. {
  215. EditorGUI.BeginChangeCheck();
  216. InterpolationDirection selectedDirection = (InterpolationDirection)EditorGUILayout.EnumPopup(
  217. new GUIContent("Animation Direction",
  218. "Adjust the animation direction to control how this property animates to the next keyframe value."), keyframe.interpolationDirection);
  219. if (EditorGUI.EndChangeCheck()) {
  220. Undo.RecordObject(profile, "Direction changed");
  221. keyframe.interpolationDirection = selectedDirection;
  222. return true;
  223. }
  224. return false;
  225. }
  226. public bool RenderTimeValue()
  227. {
  228. EditorGUI.BeginChangeCheck();
  229. float time = EditorGUILayout.FloatField(
  230. new GUIContent("Time",
  231. "Time position this keyframe is at in the current day. This is a value between 0 and 1."),
  232. keyframe.time);
  233. if (EditorGUI.EndChangeCheck()) {
  234. Undo.RecordObject(profile, "Keyframe time changed");
  235. keyframe.time = Mathf.Clamp01(time);
  236. return true;
  237. }
  238. return false;
  239. }
  240. public static string GetActiveKeyframeId() {
  241. if (inspectorEnabled == false || keyType == KeyType.None || keyframe == null) {
  242. return null;
  243. }
  244. return keyframe.id;
  245. }
  246. private void UpdateTimeControllerInScene()
  247. {
  248. TimeOfDayController timeController = FindObjectOfType<TimeOfDayController>() as TimeOfDayController;
  249. if (!timeController)
  250. {
  251. return;
  252. }
  253. timeController.UpdateSkyForCurrentTime();
  254. }
  255. }
  256. }