AnimationWindow.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. /** @addtogroup Windows
  9. * @{
  10. */
  11. /// <summary>
  12. /// Displays animation curve editor window.
  13. /// </summary>
  14. [DefaultSize(900, 500)]
  15. internal class AnimationWindow : EditorWindow
  16. {
  17. private const int FIELD_DISPLAY_WIDTH = 200;
  18. private bool isInitialized;
  19. private GUIFloatField lengthField;
  20. private GUIIntField fpsField;
  21. private GUIFloatField yRangeField;
  22. private GUIButton addKeyframeBtn;
  23. private GUIButton addPropertyBtn;
  24. private GUIButton delPropertyBtn;
  25. private GUILayout buttonLayout;
  26. private int buttonLayoutHeight;
  27. private GUIPanel editorPanel;
  28. private GUIAnimFieldDisplay guiFieldDisplay;
  29. private GUICurveEditor guiCurveEditor;
  30. private Dictionary<string, EdAnimationCurve> curves = new Dictionary<string, EdAnimationCurve>();
  31. private List<string> selectedFields = new List<string>();
  32. /// <summary>
  33. /// Opens the animation window.
  34. /// </summary>
  35. [MenuItem("Windows/Animation", ButtonModifier.CtrlAlt, ButtonCode.A, 6000)]
  36. private static void OpenGameWindow()
  37. {
  38. OpenWindow<AnimationWindow>();
  39. }
  40. /// <inheritdoc/>
  41. protected override LocString GetDisplayName()
  42. {
  43. return new LocEdString("Animation");
  44. }
  45. private void OnInitialize()
  46. {
  47. Selection.OnSelectionChanged += OnSelectionChanged;
  48. EditorInput.OnPointerPressed += OnPointerPressed;
  49. EditorInput.OnPointerMoved += OnPointerMoved;
  50. EditorInput.OnPointerReleased += OnPointerReleased;
  51. EditorInput.OnButtonUp += OnButtonUp;
  52. Rebuild();
  53. }
  54. private void OnEditorUpdate()
  55. {
  56. }
  57. private void OnDestroy()
  58. {
  59. Selection.OnSelectionChanged -= OnSelectionChanged;
  60. EditorInput.OnPointerPressed -= OnPointerPressed;
  61. EditorInput.OnPointerMoved -= OnPointerMoved;
  62. EditorInput.OnPointerReleased -= OnPointerReleased;
  63. EditorInput.OnButtonUp -= OnButtonUp;
  64. }
  65. protected override void WindowResized(int width, int height)
  66. {
  67. if (!isInitialized)
  68. return;
  69. guiFieldDisplay.SetSize(FIELD_DISPLAY_WIDTH, height - buttonLayoutHeight*2);
  70. int curveEditorWidth = Math.Max(0, width - FIELD_DISPLAY_WIDTH);
  71. guiCurveEditor.SetSize(curveEditorWidth, height - buttonLayoutHeight);
  72. guiCurveEditor.Redraw();
  73. }
  74. private void Rebuild()
  75. {
  76. GUI.Clear();
  77. selectedFields.Clear();
  78. curves.Clear();
  79. isInitialized = false;
  80. SceneObject selectedSO = Selection.SceneObject;
  81. if (selectedSO == null)
  82. {
  83. GUILabel warningLbl = new GUILabel(new LocEdString("Select an object to animate in the Hierarchy or Scene windows."));
  84. GUILayoutY vertLayout = GUI.AddLayoutY();
  85. vertLayout.AddFlexibleSpace();
  86. GUILayoutX horzLayout = vertLayout.AddLayoutX();
  87. vertLayout.AddFlexibleSpace();
  88. horzLayout.AddFlexibleSpace();
  89. horzLayout.AddElement(warningLbl);
  90. horzLayout.AddFlexibleSpace();
  91. return;
  92. }
  93. // TODO - Retrieve Animation & AnimationClip from the selected object, fill curves dictionary
  94. // - If not available, show a button to create new animation clip
  95. lengthField = new GUIFloatField(new LocEdString("Length"), 50);
  96. fpsField = new GUIIntField(new LocEdString("FPS"), 50);
  97. yRangeField = new GUIFloatField(new LocEdString("Y range"), 50);
  98. addKeyframeBtn = new GUIButton(new LocEdString("Add keyframe"));
  99. addPropertyBtn = new GUIButton(new LocEdString("Add property"));
  100. delPropertyBtn = new GUIButton(new LocEdString("Delete selected"));
  101. lengthField.Value = 60.0f;
  102. fpsField.Value = 1;
  103. yRangeField.Value = 20.0f;
  104. addPropertyBtn.OnClick += () =>
  105. {
  106. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  107. FieldSelectionWindow fieldSelection = DropDownWindow.Open<FieldSelectionWindow>(this, windowPos);
  108. fieldSelection.OnFieldSelected += OnFieldAdded;
  109. };
  110. delPropertyBtn.OnClick += () =>
  111. {
  112. LocEdString title = new LocEdString("Warning");
  113. LocEdString message = new LocEdString("Are you sure you want to remove all selected fields?");
  114. DialogBox.Open(title, message, DialogBox.Type.YesNo, x =>
  115. {
  116. if (x == DialogBox.ResultType.Yes)
  117. {
  118. RemoveSelectedFields();
  119. }
  120. });
  121. };
  122. lengthField.OnChanged += x =>
  123. {
  124. guiCurveEditor.SetRange(lengthField.Value, yRangeField.Value);
  125. };
  126. fpsField.OnChanged += x =>
  127. {
  128. guiCurveEditor.SetFPS(x);
  129. };
  130. yRangeField.OnChanged += x =>
  131. {
  132. guiCurveEditor.SetRange(lengthField.Value, yRangeField.Value);
  133. };
  134. addKeyframeBtn.OnClick += () =>
  135. {
  136. guiCurveEditor.AddKeyFrameAtMarker();
  137. };
  138. GUILayout mainLayout = GUI.AddLayoutY();
  139. buttonLayout = mainLayout.AddLayoutX();
  140. buttonLayout.AddSpace(5);
  141. buttonLayout.AddElement(lengthField);
  142. buttonLayout.AddSpace(5);
  143. buttonLayout.AddElement(yRangeField);
  144. buttonLayout.AddSpace(5);
  145. buttonLayout.AddElement(fpsField);
  146. buttonLayout.AddSpace(5);
  147. buttonLayout.AddElement(addKeyframeBtn);
  148. buttonLayout.AddSpace(5);
  149. buttonLayoutHeight = lengthField.Bounds.height;
  150. GUILayout contentLayout = mainLayout.AddLayoutX();
  151. GUILayout fieldDisplayLayout = contentLayout.AddLayoutY(GUIOption.FixedWidth(FIELD_DISPLAY_WIDTH));
  152. guiFieldDisplay = new GUIAnimFieldDisplay(fieldDisplayLayout, FIELD_DISPLAY_WIDTH,
  153. Height - buttonLayoutHeight * 2, selectedSO);
  154. guiFieldDisplay.OnEntrySelected += OnFieldSelected;
  155. GUILayout bottomButtonLayout = fieldDisplayLayout.AddLayoutX();
  156. bottomButtonLayout.AddElement(addPropertyBtn);
  157. bottomButtonLayout.AddElement(delPropertyBtn);
  158. GUILayout curveLayout = contentLayout.AddLayoutY();
  159. editorPanel = curveLayout.AddPanel();
  160. int curveEditorWidth = Math.Max(0, Width - FIELD_DISPLAY_WIDTH);
  161. guiCurveEditor = new GUICurveEditor(this, editorPanel, curveEditorWidth, Height - buttonLayoutHeight);
  162. guiCurveEditor.Redraw();
  163. isInitialized = true;
  164. }
  165. private void OnPointerPressed(PointerEvent ev)
  166. {
  167. if (!isInitialized)
  168. return;
  169. guiCurveEditor.OnPointerPressed(ev);
  170. }
  171. private void OnPointerMoved(PointerEvent ev)
  172. {
  173. if (!isInitialized)
  174. return;
  175. guiCurveEditor.OnPointerMoved(ev);
  176. }
  177. private void OnPointerReleased(PointerEvent ev)
  178. {
  179. if (!isInitialized)
  180. return;
  181. guiCurveEditor.OnPointerReleased(ev);
  182. }
  183. private void OnButtonUp(ButtonEvent ev)
  184. {
  185. if (!isInitialized)
  186. return;
  187. guiCurveEditor.OnButtonUp(ev);
  188. }
  189. private void UpdateDisplayedCurves()
  190. {
  191. List<EdAnimationCurve> curvesToDisplay = new List<EdAnimationCurve>();
  192. for (int i = 0; i < selectedFields.Count; i++)
  193. {
  194. EdAnimationCurve curve;
  195. if(curves.TryGetValue(selectedFields[i], out curve))
  196. curvesToDisplay.Add(curve);
  197. }
  198. guiCurveEditor.SetCurves(curvesToDisplay.ToArray());
  199. float xRange;
  200. float yRange;
  201. CalculateRange(curvesToDisplay, out xRange, out yRange);
  202. // Don't allow zero range
  203. if (xRange == 0.0f)
  204. xRange = 60.0f;
  205. if (yRange == 0.0f)
  206. yRange = 10.0f;
  207. // Add padding to y range
  208. yRange *= 1.05f;
  209. // Don't reduce visible range
  210. xRange = Math.Max(xRange, guiCurveEditor.XRange);
  211. yRange = Math.Max(yRange, guiCurveEditor.YRange);
  212. guiCurveEditor.SetRange(xRange, yRange);
  213. guiCurveEditor.Redraw();
  214. }
  215. private static void CalculateRange(List<EdAnimationCurve> curves, out float xRange, out float yRange)
  216. {
  217. xRange = 0.0f;
  218. yRange = 0.0f;
  219. foreach (var curve in curves)
  220. {
  221. KeyFrame[] keyframes = curve.KeyFrames;
  222. foreach (var key in keyframes)
  223. {
  224. xRange = Math.Max(xRange, key.time);
  225. yRange = Math.Max(yRange, Math.Abs(key.value));
  226. }
  227. }
  228. }
  229. private void OnFieldAdded(string path, SerializableProperty.FieldType type)
  230. {
  231. guiFieldDisplay.AddField(path);
  232. switch (type)
  233. {
  234. case SerializableProperty.FieldType.Vector4:
  235. {
  236. string[] subPaths = { ".x", ".y", ".z", ".w" };
  237. for (int i = 0; i < subPaths.Length; i++)
  238. {
  239. string subFieldPath = path + subPaths[i];
  240. curves[subFieldPath] = new EdAnimationCurve();
  241. selectedFields.Add(subFieldPath);
  242. }
  243. }
  244. break;
  245. case SerializableProperty.FieldType.Vector3:
  246. {
  247. string[] subPaths = { ".x", ".y", ".z" };
  248. for (int i = 0; i < subPaths.Length; i++)
  249. {
  250. string subFieldPath = path + subPaths[i];
  251. curves[subFieldPath] = new EdAnimationCurve();
  252. selectedFields.Add(subFieldPath);
  253. }
  254. }
  255. break;
  256. case SerializableProperty.FieldType.Vector2:
  257. {
  258. string[] subPaths = { ".x", ".y" };
  259. for (int i = 0; i < subPaths.Length; i++)
  260. {
  261. string subFieldPath = path + subPaths[i];
  262. curves[subFieldPath] = new EdAnimationCurve();
  263. selectedFields.Add(subFieldPath);
  264. }
  265. }
  266. break;
  267. case SerializableProperty.FieldType.Color:
  268. {
  269. string[] subPaths = { ".r", ".g", ".b", ".a" };
  270. for (int i = 0; i < subPaths.Length; i++)
  271. {
  272. string subFieldPath = path + subPaths[i];
  273. curves[subFieldPath] = new EdAnimationCurve();
  274. selectedFields.Add(subFieldPath);
  275. }
  276. }
  277. break;
  278. default: // Primitive type
  279. {
  280. curves[path] = new EdAnimationCurve();
  281. selectedFields.Add(path);
  282. }
  283. break;
  284. }
  285. UpdateDisplayedCurves();
  286. }
  287. private bool IsPathParent(string child, string parent)
  288. {
  289. string[] childEntries = child.Split('/', '.');
  290. string[] parentEntries = parent.Split('/', '.');
  291. if (parentEntries.Length >= child.Length)
  292. return false;
  293. int compareLength = Math.Min(childEntries.Length, parentEntries.Length);
  294. for (int i = 0; i < compareLength; i++)
  295. {
  296. if (childEntries[i] != parentEntries[i])
  297. return false;
  298. }
  299. return true;
  300. }
  301. private void OnFieldSelected(string path)
  302. {
  303. if (!Input.IsButtonHeld(ButtonCode.LeftShift) && !Input.IsButtonHeld(ButtonCode.RightShift))
  304. selectedFields.Clear();
  305. if (!string.IsNullOrEmpty(path))
  306. {
  307. selectedFields.RemoveAll(x => { return x == path || IsPathParent(x, path); });
  308. selectedFields.Add(path);
  309. }
  310. guiFieldDisplay.SetSelection(selectedFields.ToArray());
  311. UpdateDisplayedCurves();
  312. }
  313. private void RemoveSelectedFields()
  314. {
  315. for (int i = 0; i < selectedFields.Count; i++)
  316. {
  317. selectedFields.Remove(selectedFields[i]);
  318. curves.Remove(selectedFields[i]);
  319. }
  320. List<string> existingFields = new List<string>();
  321. foreach(var KVP in curves)
  322. existingFields.Add(KVP.Key);
  323. guiFieldDisplay.SetFields(existingFields.ToArray());
  324. selectedFields.Clear();
  325. UpdateDisplayedCurves();
  326. }
  327. private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
  328. {
  329. Rebuild();
  330. }
  331. }
  332. /** @} */
  333. }