AnimationWindow.cs 34 KB

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