AnimationWindow.cs 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979
  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 const int DRAG_START_DISTANCE = 3;
  19. private const float DRAG_SCALE = 10.0f;
  20. private const float ZOOM_SCALE = 15.0f;
  21. private bool isInitialized;
  22. private SceneObject selectedSO;
  23. #region Overrides
  24. /// <summary>
  25. /// Opens the animation window.
  26. /// </summary>
  27. [MenuItem("Windows/Animation", ButtonModifier.CtrlAlt, ButtonCode.A, 6000)]
  28. private static void OpenGameWindow()
  29. {
  30. OpenWindow<AnimationWindow>();
  31. }
  32. /// <inheritdoc/>
  33. protected override LocString GetDisplayName()
  34. {
  35. return new LocEdString("Animation");
  36. }
  37. private void OnInitialize()
  38. {
  39. Selection.OnSelectionChanged += OnSelectionChanged;
  40. EditorInput.OnPointerPressed += OnPointerPressed;
  41. EditorInput.OnPointerMoved += OnPointerMoved;
  42. EditorInput.OnPointerReleased += OnPointerReleased;
  43. EditorInput.OnButtonUp += OnButtonUp;
  44. RebuildGUI();
  45. }
  46. private void OnEditorUpdate()
  47. {
  48. if (!isInitialized)
  49. return;
  50. HandleDragAndZoomInput();
  51. }
  52. private void OnDestroy()
  53. {
  54. Selection.OnSelectionChanged -= OnSelectionChanged;
  55. EditorInput.OnPointerPressed -= OnPointerPressed;
  56. EditorInput.OnPointerMoved -= OnPointerMoved;
  57. EditorInput.OnPointerReleased -= OnPointerReleased;
  58. EditorInput.OnButtonUp -= OnButtonUp;
  59. }
  60. protected override void WindowResized(int width, int height)
  61. {
  62. if (!isInitialized)
  63. return;
  64. ResizeGUI(width, height);
  65. }
  66. #endregion
  67. #region GUI
  68. private GUIButton playButton;
  69. private GUIButton recordButton;
  70. private GUIButton prevFrameButton;
  71. private GUIIntField frameInputField;
  72. private GUIButton nextFrameButton;
  73. private GUIButton addKeyframeButton;
  74. private GUIButton addEventButton;
  75. private GUIButton optionsButton;
  76. private GUIButton addPropertyBtn;
  77. private GUIButton delPropertyBtn;
  78. private GUILayout buttonLayout;
  79. private int buttonLayoutHeight;
  80. private int scrollBarWidth;
  81. private int scrollBarHeight;
  82. private GUIResizeableScrollBarH horzScrollBar;
  83. private GUIResizeableScrollBarV vertScrollBar;
  84. private GUIPanel editorPanel;
  85. private GUIAnimFieldDisplay guiFieldDisplay;
  86. private GUICurveEditor guiCurveEditor;
  87. private void RebuildGUI()
  88. {
  89. GUI.Clear();
  90. selectedFields.Clear();
  91. curves.Clear();
  92. isInitialized = false;
  93. selectedSO = Selection.SceneObject;
  94. if (selectedSO == null)
  95. {
  96. GUILabel warningLbl = new GUILabel(new LocEdString("Select an object to animate in the Hierarchy or Scene windows."));
  97. GUILayoutY vertLayout = GUI.AddLayoutY();
  98. vertLayout.AddFlexibleSpace();
  99. GUILayoutX horzLayout = vertLayout.AddLayoutX();
  100. vertLayout.AddFlexibleSpace();
  101. horzLayout.AddFlexibleSpace();
  102. horzLayout.AddElement(warningLbl);
  103. horzLayout.AddFlexibleSpace();
  104. return;
  105. }
  106. // TODO - Retrieve Animation & AnimationClip from the selected object, fill curves dictionary
  107. // - If not available, show a button to create new animation clip
  108. // Top button row
  109. GUIContent playIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.Play),
  110. new LocEdString("Play"));
  111. GUIContent recordIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.Record),
  112. new LocEdString("Record"));
  113. GUIContent prevFrameIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.FrameBack),
  114. new LocEdString("Previous frame"));
  115. GUIContent nextFrameIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.FrameForward),
  116. new LocEdString("Next frame"));
  117. GUIContent addKeyframeIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.AddKeyframe),
  118. new LocEdString("Add keyframe"));
  119. GUIContent addEventIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.AddEvent),
  120. new LocEdString("Add event"));
  121. GUIContent optionsIcon = new GUIContent(EditorBuiltin.GetLibraryWindowIcon(LibraryWindowIcon.Options),
  122. new LocEdString("Options"));
  123. playButton = new GUIButton(playIcon);
  124. recordButton = new GUIButton(recordIcon);
  125. prevFrameButton = new GUIButton(prevFrameIcon);
  126. frameInputField = new GUIIntField();
  127. nextFrameButton = new GUIButton(nextFrameIcon);
  128. addKeyframeButton = new GUIButton(addKeyframeIcon);
  129. addEventButton = new GUIButton(addEventIcon);
  130. optionsButton = new GUIButton(optionsIcon);
  131. playButton.OnClick += () =>
  132. {
  133. // TODO
  134. // - Record current state of the scene object hierarchy
  135. // - Evaluate all curves manually and update them
  136. // - On end, restore original values of the scene object hierarchy
  137. };
  138. recordButton.OnClick += () =>
  139. {
  140. // TODO
  141. // - Every frame read back current values of all the current curve's properties and assign it to the current frame
  142. };
  143. prevFrameButton.OnClick += () =>
  144. {
  145. SetCurrentFrame(currentFrameIdx - 1);
  146. };
  147. frameInputField.OnChanged += SetCurrentFrame;
  148. nextFrameButton.OnClick += () =>
  149. {
  150. SetCurrentFrame(currentFrameIdx + 1);
  151. };
  152. addKeyframeButton.OnClick += () =>
  153. {
  154. guiCurveEditor.AddKeyFrameAtMarker();
  155. };
  156. addEventButton.OnClick += () =>
  157. {
  158. // TODO - Add event
  159. };
  160. optionsButton.OnClick += () =>
  161. {
  162. Vector2I openPosition = ScreenToWindowPos(Input.PointerPosition);
  163. AnimationOptions dropDown = DropDownWindow.Open<AnimationOptions>(this, openPosition);
  164. dropDown.Initialize(this);
  165. };
  166. // Property buttons
  167. addPropertyBtn = new GUIButton(new LocEdString("Add property"));
  168. delPropertyBtn = new GUIButton(new LocEdString("Delete selected"));
  169. addPropertyBtn.OnClick += () =>
  170. {
  171. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  172. FieldSelectionWindow fieldSelection = DropDownWindow.Open<FieldSelectionWindow>(this, windowPos);
  173. fieldSelection.OnFieldSelected += OnFieldAdded;
  174. };
  175. delPropertyBtn.OnClick += () =>
  176. {
  177. LocEdString title = new LocEdString("Warning");
  178. LocEdString message = new LocEdString("Are you sure you want to remove all selected fields?");
  179. DialogBox.Open(title, message, DialogBox.Type.YesNo, x =>
  180. {
  181. if (x == DialogBox.ResultType.Yes)
  182. {
  183. RemoveSelectedFields();
  184. }
  185. });
  186. };
  187. GUILayout mainLayout = GUI.AddLayoutY();
  188. buttonLayout = mainLayout.AddLayoutX();
  189. buttonLayout.AddSpace(5);
  190. buttonLayout.AddElement(playButton);
  191. buttonLayout.AddElement(recordButton);
  192. buttonLayout.AddSpace(5);
  193. buttonLayout.AddElement(prevFrameButton);
  194. buttonLayout.AddElement(frameInputField);
  195. buttonLayout.AddElement(nextFrameButton);
  196. buttonLayout.AddSpace(5);
  197. buttonLayout.AddElement(addKeyframeButton);
  198. buttonLayout.AddElement(addEventButton);
  199. buttonLayout.AddSpace(5);
  200. buttonLayout.AddElement(optionsButton);
  201. buttonLayout.AddFlexibleSpace();
  202. buttonLayoutHeight = playButton.Bounds.height;
  203. GUILayout contentLayout = mainLayout.AddLayoutX();
  204. GUILayout fieldDisplayLayout = contentLayout.AddLayoutY(GUIOption.FixedWidth(FIELD_DISPLAY_WIDTH));
  205. guiFieldDisplay = new GUIAnimFieldDisplay(fieldDisplayLayout, FIELD_DISPLAY_WIDTH,
  206. Height - buttonLayoutHeight * 2, selectedSO);
  207. guiFieldDisplay.OnEntrySelected += OnFieldSelected;
  208. GUILayout bottomButtonLayout = fieldDisplayLayout.AddLayoutX();
  209. bottomButtonLayout.AddElement(addPropertyBtn);
  210. bottomButtonLayout.AddElement(delPropertyBtn);
  211. horzScrollBar = new GUIResizeableScrollBarH();
  212. horzScrollBar.OnScrollOrResize += OnHorzScrollOrResize;
  213. vertScrollBar = new GUIResizeableScrollBarV();
  214. vertScrollBar.OnScrollOrResize += OnVertScrollOrResize;
  215. GUILayout curveLayout = contentLayout.AddLayoutY();
  216. GUILayout curveLayoutHorz = curveLayout.AddLayoutX();
  217. GUILayout horzScrollBarLayout = curveLayout.AddLayoutX();
  218. horzScrollBarLayout.AddElement(horzScrollBar);
  219. horzScrollBarLayout.AddFlexibleSpace();
  220. editorPanel = curveLayoutHorz.AddPanel();
  221. curveLayoutHorz.AddElement(vertScrollBar);
  222. curveLayoutHorz.AddFlexibleSpace();
  223. scrollBarHeight = horzScrollBar.Bounds.height;
  224. scrollBarWidth = vertScrollBar.Bounds.width;
  225. Vector2I curveEditorSize = GetCurveEditorSize();
  226. guiCurveEditor = new GUICurveEditor(this, editorPanel, curveEditorSize.x, curveEditorSize.y);
  227. guiCurveEditor.OnFrameSelected += OnFrameSelected;
  228. guiCurveEditor.Redraw();
  229. horzScrollBar.SetWidth(curveEditorSize.x);
  230. vertScrollBar.SetHeight(curveEditorSize.y);
  231. SetCurrentFrame(currentFrameIdx);
  232. UpdateScrollBarSize();
  233. isInitialized = true;
  234. }
  235. private void ResizeGUI(int width, int height)
  236. {
  237. guiFieldDisplay.SetSize(FIELD_DISPLAY_WIDTH, height - buttonLayoutHeight * 2);
  238. Vector2I curveEditorSize = GetCurveEditorSize();
  239. guiCurveEditor.SetSize(curveEditorSize.x, curveEditorSize.y);
  240. guiCurveEditor.Redraw();
  241. horzScrollBar.SetWidth(curveEditorSize.x);
  242. vertScrollBar.SetHeight(curveEditorSize.y);
  243. UpdateScrollBarSize();
  244. UpdateScrollBarPosition();
  245. }
  246. #endregion
  247. #region Scroll, drag, zoom
  248. private Vector2I dragStartPos;
  249. private bool isButtonHeld;
  250. private bool isDragInProgress;
  251. private float zoomAmount;
  252. private void HandleDragAndZoomInput()
  253. {
  254. // Handle middle mouse dragging
  255. if (isDragInProgress)
  256. {
  257. float dragX = Input.GetAxisValue(InputAxis.MouseX) * DRAG_SCALE;
  258. float dragY = Input.GetAxisValue(InputAxis.MouseY) * DRAG_SCALE;
  259. Vector2 offset = guiCurveEditor.Offset;
  260. offset.x = Math.Max(0.0f, offset.x + dragX);
  261. offset.y += dragY;
  262. guiCurveEditor.Offset = offset;
  263. UpdateScrollBarSize();
  264. UpdateScrollBarPosition();
  265. }
  266. // Handle zoom in/out
  267. float scroll = Input.GetAxisValue(InputAxis.MouseZ);
  268. if (scroll != 0.0f)
  269. {
  270. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  271. Vector2 curvePos;
  272. if (guiCurveEditor.WindowToCurveSpace(windowPos, out curvePos))
  273. {
  274. float zoom = scroll * ZOOM_SCALE;
  275. Zoom(curvePos, zoom);
  276. }
  277. }
  278. }
  279. private void SetVertScrollbarProperties(float position, float size)
  280. {
  281. Vector2 visibleRange = guiCurveEditor.Range;
  282. Vector2 totalRange = GetTotalRange();
  283. visibleRange.y = totalRange.y*size;
  284. guiCurveEditor.Range = visibleRange;
  285. float scrollableRange = totalRange.y - visibleRange.y;
  286. Vector2 offset = guiCurveEditor.Offset;
  287. offset.y = scrollableRange * (position * 2.0f - 1.0f);
  288. guiCurveEditor.Offset = offset;
  289. }
  290. private void SetHorzScrollbarProperties(float position, float size)
  291. {
  292. Vector2 visibleRange = guiCurveEditor.Range;
  293. Vector2 totalRange = GetTotalRange();
  294. visibleRange.x = totalRange.x * size;
  295. guiCurveEditor.Range = visibleRange;
  296. float scrollableRange = totalRange.x - visibleRange.x;
  297. Vector2 offset = guiCurveEditor.Offset;
  298. offset.x = scrollableRange * position;
  299. guiCurveEditor.Offset = offset;
  300. }
  301. private void UpdateScrollBarSize()
  302. {
  303. Vector2 visibleRange = guiCurveEditor.Range;
  304. Vector2 totalRange = GetTotalRange();
  305. horzScrollBar.HandleSize = visibleRange.x / totalRange.x;
  306. vertScrollBar.HandleSize = visibleRange.y / totalRange.y;
  307. }
  308. private void UpdateScrollBarPosition()
  309. {
  310. Vector2 visibleRange = guiCurveEditor.Range;
  311. Vector2 totalRange = GetTotalRange();
  312. Vector2 scrollableRange = totalRange - visibleRange;
  313. Vector2 offset = guiCurveEditor.Offset;
  314. // Transform Y from [-x, +x] range to [0, x]
  315. offset.y += visibleRange.y;
  316. offset.y /= 2.0f;
  317. horzScrollBar.Position = offset.x / scrollableRange.x;
  318. vertScrollBar.Position = offset.y / scrollableRange.y;
  319. }
  320. private Vector2 GetZoomedRange()
  321. {
  322. float zoomLevel = MathEx.Pow(2, zoomAmount);
  323. Vector2 optimalRange = GetOptimalRange();
  324. return optimalRange / zoomLevel;
  325. }
  326. private Vector2 GetTotalRange()
  327. {
  328. Vector2 visibleRange = guiCurveEditor.Range;
  329. Vector2 totalRange = guiCurveEditor.Offset;
  330. totalRange.x += visibleRange.x;
  331. totalRange.y = Math.Abs(totalRange.y) + visibleRange.y;
  332. Vector2 optimalRange = GetOptimalRange();
  333. return Vector2.Max(totalRange, optimalRange);
  334. }
  335. private void Zoom(Vector2 curvePos, float amount)
  336. {
  337. Vector2 oldZoomedRange = GetZoomedRange();
  338. zoomAmount += amount;
  339. Vector2 zoomedRange = GetZoomedRange();
  340. Vector2 zoomedDiff = zoomedRange - oldZoomedRange;
  341. zoomedDiff.y *= 0.5f;
  342. Vector2 currentRange = guiCurveEditor.Range;
  343. Vector2 newRange = currentRange + zoomedDiff;
  344. Vector2 offset = guiCurveEditor.Offset;
  345. Vector2 relativePos = curvePos - offset;
  346. relativePos.x /= currentRange.x;
  347. relativePos.y /= currentRange.y;
  348. relativePos.x = relativePos.x * 2.0f - 1.0f;
  349. relativePos.y = relativePos.y * 2.0f - 1.0f;
  350. offset.x += relativePos.x * zoomedDiff.x;
  351. offset.y += relativePos.y * zoomedDiff.y * 2.0f;
  352. guiCurveEditor.Offset = offset;
  353. guiCurveEditor.Range = newRange;
  354. UpdateScrollBarSize();
  355. UpdateScrollBarPosition();
  356. }
  357. #endregion
  358. #region Curve display
  359. /// <summary>
  360. /// A set of animation curves for a field of a certain type.
  361. /// </summary>
  362. private struct FieldCurves
  363. {
  364. public SerializableProperty.FieldType type;
  365. public EdAnimationCurve[] curves;
  366. }
  367. private int currentFrameIdx;
  368. private int fps = 1;
  369. private Dictionary<string, FieldCurves> curves = new Dictionary<string, FieldCurves>();
  370. internal int FPS
  371. {
  372. get { return fps; }
  373. set { guiCurveEditor.SetFPS(value); fps = MathEx.Max(value, 1); }
  374. }
  375. private void SetCurrentFrame(int frameIdx)
  376. {
  377. currentFrameIdx = Math.Max(0, frameIdx);
  378. frameInputField.Value = currentFrameIdx;
  379. guiCurveEditor.SetMarkedFrame(currentFrameIdx);
  380. float time = guiCurveEditor.GetTimeForFrame(currentFrameIdx);
  381. List<GUIAnimFieldPathValue> values = new List<GUIAnimFieldPathValue>();
  382. foreach (var kvp in curves)
  383. {
  384. SerializableProperty property = GUIAnimFieldDisplay.FindProperty(selectedSO, kvp.Key);
  385. if (property != null)
  386. {
  387. GUIAnimFieldPathValue fieldValue = new GUIAnimFieldPathValue();
  388. fieldValue.path = kvp.Key;
  389. switch (kvp.Value.type)
  390. {
  391. case SerializableProperty.FieldType.Vector2:
  392. {
  393. Vector2 value = new Vector2();
  394. for (int i = 0; i < 2; i++)
  395. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  396. fieldValue.value = value;
  397. }
  398. break;
  399. case SerializableProperty.FieldType.Vector3:
  400. {
  401. Vector3 value = new Vector3();
  402. for (int i = 0; i < 3; i++)
  403. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  404. fieldValue.value = value;
  405. }
  406. break;
  407. case SerializableProperty.FieldType.Vector4:
  408. {
  409. Vector4 value = new Vector4();
  410. for (int i = 0; i < 4; i++)
  411. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  412. fieldValue.value = value;
  413. }
  414. break;
  415. case SerializableProperty.FieldType.Color:
  416. {
  417. Color value = new Color();
  418. for (int i = 0; i < 4; i++)
  419. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  420. fieldValue.value = value;
  421. }
  422. break;
  423. case SerializableProperty.FieldType.Bool:
  424. case SerializableProperty.FieldType.Int:
  425. case SerializableProperty.FieldType.Float:
  426. fieldValue.value = kvp.Value.curves[0].Evaluate(time, false); ;
  427. break;
  428. }
  429. values.Add(fieldValue);
  430. }
  431. }
  432. guiFieldDisplay.SetDisplayValues(values.ToArray());
  433. }
  434. private Vector2 GetOptimalRange()
  435. {
  436. List<EdAnimationCurve> displayedCurves = new List<EdAnimationCurve>();
  437. for (int i = 0; i < selectedFields.Count; i++)
  438. {
  439. EdAnimationCurve curve;
  440. if (TryGetCurve(selectedFields[i], out curve))
  441. displayedCurves.Add(curve);
  442. }
  443. float xRange;
  444. float yRange;
  445. CalculateRange(displayedCurves, out xRange, out yRange);
  446. // Add padding to y range
  447. yRange *= 1.05f;
  448. // Don't allow zero range
  449. if (xRange == 0.0f)
  450. xRange = 60.0f;
  451. if (yRange == 0.0f)
  452. yRange = 10.0f;
  453. return new Vector2(xRange, yRange);
  454. }
  455. private void UpdateDisplayedCurves()
  456. {
  457. List<EdAnimationCurve> curvesToDisplay = new List<EdAnimationCurve>();
  458. for (int i = 0; i < selectedFields.Count; i++)
  459. {
  460. EdAnimationCurve curve;
  461. if (TryGetCurve(selectedFields[i], out curve))
  462. curvesToDisplay.Add(curve);
  463. }
  464. guiCurveEditor.SetCurves(curvesToDisplay.ToArray());
  465. Vector2 newRange = GetOptimalRange();
  466. // Don't reduce visible range
  467. newRange.x = Math.Max(newRange.x, guiCurveEditor.Range.x);
  468. newRange.y = Math.Max(newRange.y, guiCurveEditor.Range.y);
  469. guiCurveEditor.Range = newRange;
  470. UpdateScrollBarSize();
  471. }
  472. #endregion
  473. #region Field display
  474. private List<string> selectedFields = new List<string>();
  475. private void AddNewField(string path, SerializableProperty.FieldType type)
  476. {
  477. guiFieldDisplay.AddField(path);
  478. switch (type)
  479. {
  480. case SerializableProperty.FieldType.Vector4:
  481. {
  482. FieldCurves fieldCurves = new FieldCurves();
  483. fieldCurves.type = type;
  484. fieldCurves.curves = new EdAnimationCurve[4];
  485. string[] subPaths = { ".x", ".y", ".z", ".w" };
  486. for (int i = 0; i < subPaths.Length; i++)
  487. {
  488. string subFieldPath = path + subPaths[i];
  489. fieldCurves.curves[i] = new EdAnimationCurve();
  490. selectedFields.Add(subFieldPath);
  491. }
  492. curves[path] = fieldCurves;
  493. }
  494. break;
  495. case SerializableProperty.FieldType.Vector3:
  496. {
  497. FieldCurves fieldCurves = new FieldCurves();
  498. fieldCurves.type = type;
  499. fieldCurves.curves = new EdAnimationCurve[3];
  500. string[] subPaths = { ".x", ".y", ".z" };
  501. for (int i = 0; i < subPaths.Length; i++)
  502. {
  503. string subFieldPath = path + subPaths[i];
  504. fieldCurves.curves[i] = new EdAnimationCurve();
  505. selectedFields.Add(subFieldPath);
  506. }
  507. curves[path] = fieldCurves;
  508. }
  509. break;
  510. case SerializableProperty.FieldType.Vector2:
  511. {
  512. FieldCurves fieldCurves = new FieldCurves();
  513. fieldCurves.type = type;
  514. fieldCurves.curves = new EdAnimationCurve[2];
  515. string[] subPaths = { ".x", ".y" };
  516. for (int i = 0; i < subPaths.Length; i++)
  517. {
  518. string subFieldPath = path + subPaths[i];
  519. fieldCurves.curves[i] = new EdAnimationCurve();
  520. selectedFields.Add(subFieldPath);
  521. }
  522. curves[path] = fieldCurves;
  523. }
  524. break;
  525. case SerializableProperty.FieldType.Color:
  526. {
  527. FieldCurves fieldCurves = new FieldCurves();
  528. fieldCurves.type = type;
  529. fieldCurves.curves = new EdAnimationCurve[4];
  530. string[] subPaths = { ".r", ".g", ".b", ".a" };
  531. for (int i = 0; i < subPaths.Length; i++)
  532. {
  533. string subFieldPath = path + subPaths[i];
  534. fieldCurves.curves[i] = new EdAnimationCurve();
  535. selectedFields.Add(subFieldPath);
  536. }
  537. curves[path] = fieldCurves;
  538. }
  539. break;
  540. default: // Primitive type
  541. {
  542. FieldCurves fieldCurves = new FieldCurves();
  543. fieldCurves.type = type;
  544. fieldCurves.curves = new EdAnimationCurve[1];
  545. fieldCurves.curves[0] = new EdAnimationCurve();
  546. selectedFields.Add(path);
  547. curves[path] = fieldCurves;
  548. }
  549. break;
  550. }
  551. UpdateDisplayedCurves();
  552. }
  553. private void SelectField(string path, bool additive)
  554. {
  555. if (!additive)
  556. selectedFields.Clear();
  557. if (!string.IsNullOrEmpty(path))
  558. {
  559. selectedFields.RemoveAll(x => { return x == path || IsPathParent(x, path); });
  560. selectedFields.Add(path);
  561. }
  562. guiFieldDisplay.SetSelection(selectedFields.ToArray());
  563. UpdateDisplayedCurves();
  564. }
  565. private void RemoveSelectedFields()
  566. {
  567. for (int i = 0; i < selectedFields.Count; i++)
  568. {
  569. selectedFields.Remove(selectedFields[i]);
  570. curves.Remove(GetSubPathParent(selectedFields[i]));
  571. }
  572. List<string> existingFields = new List<string>();
  573. foreach (var KVP in curves)
  574. existingFields.Add(KVP.Key);
  575. guiFieldDisplay.SetFields(existingFields.ToArray());
  576. selectedFields.Clear();
  577. UpdateDisplayedCurves();
  578. }
  579. #endregion
  580. #region Helpers
  581. private Vector2I GetCurveEditorSize()
  582. {
  583. Vector2I output = new Vector2I();
  584. output.x = Math.Max(0, Width - FIELD_DISPLAY_WIDTH - scrollBarWidth);
  585. output.y = Math.Max(0, Height - buttonLayoutHeight - scrollBarHeight);
  586. return output;
  587. }
  588. private static void CalculateRange(List<EdAnimationCurve> curves, out float xRange, out float yRange)
  589. {
  590. xRange = 0.0f;
  591. yRange = 0.0f;
  592. foreach (var curve in curves)
  593. {
  594. KeyFrame[] keyframes = curve.KeyFrames;
  595. foreach (var key in keyframes)
  596. {
  597. xRange = Math.Max(xRange, key.time);
  598. yRange = Math.Max(yRange, Math.Abs(key.value));
  599. }
  600. }
  601. }
  602. private bool TryGetCurve(string path, out EdAnimationCurve curve)
  603. {
  604. int index = path.LastIndexOf(".");
  605. string parentPath;
  606. string subPathSuffix = null;
  607. if (index == -1)
  608. {
  609. parentPath = path;
  610. }
  611. else
  612. {
  613. parentPath = path.Substring(0, index);
  614. subPathSuffix = path.Substring(index, path.Length - index);
  615. }
  616. FieldCurves fieldCurves;
  617. if (curves.TryGetValue(parentPath, out fieldCurves))
  618. {
  619. if (!string.IsNullOrEmpty(subPathSuffix))
  620. {
  621. if (subPathSuffix == ".x" || subPathSuffix == ".r")
  622. {
  623. curve = fieldCurves.curves[0];
  624. return true;
  625. }
  626. else if (subPathSuffix == ".y" || subPathSuffix == ".g")
  627. {
  628. curve = fieldCurves.curves[1];
  629. return true;
  630. }
  631. else if (subPathSuffix == ".z" || subPathSuffix == ".b")
  632. {
  633. curve = fieldCurves.curves[2];
  634. return true;
  635. }
  636. else if (subPathSuffix == ".w" || subPathSuffix == ".a")
  637. {
  638. curve = fieldCurves.curves[3];
  639. return true;
  640. }
  641. }
  642. else
  643. {
  644. curve = fieldCurves.curves[0];
  645. return true;
  646. }
  647. }
  648. curve = null;
  649. return false;
  650. }
  651. private bool IsPathParent(string child, string parent)
  652. {
  653. string[] childEntries = child.Split('/', '.');
  654. string[] parentEntries = parent.Split('/', '.');
  655. if (parentEntries.Length >= child.Length)
  656. return false;
  657. int compareLength = Math.Min(childEntries.Length, parentEntries.Length);
  658. for (int i = 0; i < compareLength; i++)
  659. {
  660. if (childEntries[i] != parentEntries[i])
  661. return false;
  662. }
  663. return true;
  664. }
  665. private string GetSubPathParent(string path)
  666. {
  667. int index = path.LastIndexOf(".");
  668. if (index == -1)
  669. return path;
  670. return path.Substring(0, index);
  671. }
  672. #endregion
  673. #region Input callbacks
  674. private void OnPointerPressed(PointerEvent ev)
  675. {
  676. if (!isInitialized)
  677. return;
  678. guiCurveEditor.OnPointerPressed(ev);
  679. if (ev.button == PointerButton.Middle)
  680. {
  681. Vector2I windowPos = ScreenToWindowPos(ev.ScreenPos);
  682. Vector2 curvePos;
  683. if (guiCurveEditor.WindowToCurveSpace(windowPos, out curvePos))
  684. {
  685. dragStartPos = windowPos;
  686. isButtonHeld = true;
  687. }
  688. }
  689. }
  690. private void OnPointerMoved(PointerEvent ev)
  691. {
  692. if (!isInitialized)
  693. return;
  694. guiCurveEditor.OnPointerMoved(ev);
  695. if (isButtonHeld)
  696. {
  697. Vector2I windowPos = ScreenToWindowPos(ev.ScreenPos);
  698. int distance = Vector2I.Distance(dragStartPos, windowPos);
  699. if (distance >= DRAG_START_DISTANCE)
  700. {
  701. isDragInProgress = true;
  702. Cursor.Hide();
  703. Rect2I clipRect;
  704. clipRect.x = ev.ScreenPos.x - 2;
  705. clipRect.y = ev.ScreenPos.y - 2;
  706. clipRect.width = 4;
  707. clipRect.height = 4;
  708. Cursor.ClipToRect(clipRect);
  709. }
  710. }
  711. }
  712. private void OnPointerReleased(PointerEvent ev)
  713. {
  714. if (isDragInProgress)
  715. {
  716. Cursor.Show();
  717. Cursor.ClipDisable();
  718. }
  719. isButtonHeld = false;
  720. isDragInProgress = false;
  721. if (!isInitialized)
  722. return;
  723. guiCurveEditor.OnPointerReleased(ev);
  724. }
  725. private void OnButtonUp(ButtonEvent ev)
  726. {
  727. if (!isInitialized)
  728. return;
  729. guiCurveEditor.OnButtonUp(ev);
  730. }
  731. #endregion
  732. #region General callbacks
  733. private void OnFieldAdded(string path, SerializableProperty.FieldType type)
  734. {
  735. AddNewField(path, type);
  736. }
  737. private void OnHorzScrollOrResize(float position, float size)
  738. {
  739. SetHorzScrollbarProperties(position, size);
  740. }
  741. private void OnVertScrollOrResize(float position, float size)
  742. {
  743. SetVertScrollbarProperties(position, size);
  744. }
  745. private void OnFieldSelected(string path)
  746. {
  747. bool additive = Input.IsButtonHeld(ButtonCode.LeftShift) || Input.IsButtonHeld(ButtonCode.RightShift);
  748. SelectField(path, additive);
  749. }
  750. private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
  751. {
  752. RebuildGUI();
  753. }
  754. private void OnFrameSelected(int frameIdx)
  755. {
  756. SetCurrentFrame(frameIdx);
  757. }
  758. #endregion
  759. }
  760. /// <summary>
  761. /// Drop down window that displays options used by the animation window.
  762. /// </summary>
  763. [DefaultSize(100, 50)]
  764. internal class AnimationOptions : DropDownWindow
  765. {
  766. /// <summary>
  767. /// Initializes the drop down window by creating the necessary GUI. Must be called after construction and before
  768. /// use.
  769. /// </summary>
  770. /// <param name="parent">Animation window that this drop down window is a part of.</param>
  771. internal void Initialize(AnimationWindow parent)
  772. {
  773. GUIIntField fpsField = new GUIIntField(new LocEdString("FPS"), 40);
  774. fpsField.Value = parent.FPS;
  775. fpsField.OnChanged += x => { parent.FPS = x; };
  776. GUILayoutY vertLayout = GUI.AddLayoutY();
  777. vertLayout.AddFlexibleSpace();
  778. GUILayoutX contentLayout = vertLayout.AddLayoutX();
  779. contentLayout.AddFlexibleSpace();
  780. contentLayout.AddElement(fpsField);
  781. contentLayout.AddFlexibleSpace();
  782. vertLayout.AddFlexibleSpace();
  783. }
  784. }
  785. /** @} */
  786. }