AnimationWindow.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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(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.OnSelectionChanged += OnFieldSelectionChanged;
  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. guiCurveEditor.Redraw();
  200. }
  201. private void OnFieldAdded(string path, SerializableProperty.FieldType type)
  202. {
  203. guiFieldDisplay.AddField(path);
  204. switch (type)
  205. {
  206. case SerializableProperty.FieldType.Vector4:
  207. {
  208. string[] subPaths = { ".x", ".y", ".z", ".w" };
  209. for (int i = 0; i < subPaths.Length; i++)
  210. {
  211. string subFieldPath = path + subPaths[i];
  212. curves[subFieldPath] = new EdAnimationCurve();
  213. selectedFields.Add(subFieldPath);
  214. }
  215. }
  216. break;
  217. case SerializableProperty.FieldType.Vector3:
  218. {
  219. string[] subPaths = { ".x", ".y", ".z" };
  220. for (int i = 0; i < subPaths.Length; i++)
  221. {
  222. string subFieldPath = path + subPaths[i];
  223. curves[subFieldPath] = new EdAnimationCurve();
  224. selectedFields.Add(subFieldPath);
  225. }
  226. }
  227. break;
  228. case SerializableProperty.FieldType.Vector2:
  229. {
  230. string[] subPaths = { ".x", ".y" };
  231. for (int i = 0; i < subPaths.Length; i++)
  232. {
  233. string subFieldPath = path + subPaths[i];
  234. curves[subFieldPath] = new EdAnimationCurve();
  235. selectedFields.Add(subFieldPath);
  236. }
  237. }
  238. break;
  239. case SerializableProperty.FieldType.Color:
  240. {
  241. string[] subPaths = { ".r", ".g", ".b", ".a" };
  242. for (int i = 0; i < subPaths.Length; i++)
  243. {
  244. string subFieldPath = path + subPaths[i];
  245. curves[subFieldPath] = new EdAnimationCurve();
  246. selectedFields.Add(subFieldPath);
  247. }
  248. }
  249. break;
  250. default: // Primitive type
  251. {
  252. curves[path] = new EdAnimationCurve();
  253. selectedFields.Add(path);
  254. }
  255. break;
  256. }
  257. UpdateDisplayedCurves();
  258. }
  259. private void OnFieldSelectionChanged(string path, bool selected)
  260. {
  261. if (selected)
  262. selectedFields.Add(path);
  263. else
  264. selectedFields.Remove(path);
  265. UpdateDisplayedCurves();
  266. }
  267. private void RemoveSelectedFields()
  268. {
  269. for (int i = 0; i < selectedFields.Count; i++)
  270. {
  271. selectedFields.Remove(selectedFields[i]);
  272. curves.Remove(selectedFields[i]);
  273. }
  274. List<string> existingFields = new List<string>();
  275. foreach(var KVP in curves)
  276. existingFields.Add(KVP.Key);
  277. guiFieldDisplay.SetFields(existingFields.ToArray());
  278. selectedFields.Clear();
  279. UpdateDisplayedCurves();
  280. }
  281. private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
  282. {
  283. Rebuild();
  284. }
  285. }
  286. /** @} */
  287. }