AnimationWindow.cs 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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. vertScrollBar.Position = offset.y / (scrollableRange.y * 0.5f);
  324. else
  325. vertScrollBar.Position = 0.0f;
  326. }
  327. private Vector2 GetZoomedRange()
  328. {
  329. float zoomLevel = MathEx.Pow(2, zoomAmount);
  330. Vector2 optimalRange = GetOptimalRange();
  331. return optimalRange / zoomLevel;
  332. }
  333. private Vector2 GetTotalRange()
  334. {
  335. // Return optimal range (that covers the visible curve)
  336. Vector2 totalRange = GetOptimalRange();
  337. // Increase range in case user zoomed out
  338. Vector2 zoomedRange = GetZoomedRange();
  339. totalRange = Vector2.Max(totalRange, zoomedRange);
  340. // Increase range in case user dragged outside of the optimal range
  341. Vector2 visibleRange = guiCurveEditor.Range;
  342. Vector2 draggedRange = guiCurveEditor.Offset;
  343. draggedRange.x += visibleRange.x;
  344. draggedRange.y = Math.Abs(draggedRange.y) + visibleRange.y;
  345. return Vector2.Max(totalRange, draggedRange);
  346. }
  347. private void Zoom(Vector2 curvePos, float amount)
  348. {
  349. // Increase or decrease the visible range depending on zoom level
  350. Vector2 oldZoomedRange = GetZoomedRange();
  351. zoomAmount = MathEx.Clamp(zoomAmount + amount, -10.0f, 10.0f);
  352. Vector2 zoomedRange = GetZoomedRange();
  353. Vector2 zoomedDiff = zoomedRange - oldZoomedRange;
  354. zoomedDiff.y *= 0.5f;
  355. Vector2 currentRange = guiCurveEditor.Range;
  356. Vector2 newRange = currentRange + zoomedDiff;
  357. guiCurveEditor.Range = newRange;
  358. // When zooming, make sure to focus on the point provided, so adjust the offset
  359. Vector2 rangeScale = newRange;
  360. rangeScale.x /= currentRange.x;
  361. rangeScale.y /= currentRange.y;
  362. Vector2 relativeCurvePos = curvePos - guiCurveEditor.Offset;
  363. Vector2 newCurvePos = relativeCurvePos * rangeScale;
  364. Vector2 diff = newCurvePos - relativeCurvePos;
  365. guiCurveEditor.Offset -= diff;
  366. UpdateScrollBarSize();
  367. UpdateScrollBarPosition();
  368. }
  369. #endregion
  370. #region Curve display
  371. /// <summary>
  372. /// A set of animation curves for a field of a certain type.
  373. /// </summary>
  374. private struct FieldCurves
  375. {
  376. public SerializableProperty.FieldType type;
  377. public EdAnimationCurve[] curves;
  378. }
  379. private int currentFrameIdx;
  380. private int fps = 1;
  381. private Dictionary<string, FieldCurves> curves = new Dictionary<string, FieldCurves>();
  382. internal int FPS
  383. {
  384. get { return fps; }
  385. set { guiCurveEditor.SetFPS(value); fps = MathEx.Max(value, 1); }
  386. }
  387. private void SetCurrentFrame(int frameIdx)
  388. {
  389. currentFrameIdx = Math.Max(0, frameIdx);
  390. frameInputField.Value = currentFrameIdx;
  391. guiCurveEditor.SetMarkedFrame(currentFrameIdx);
  392. float time = guiCurveEditor.GetTimeForFrame(currentFrameIdx);
  393. List<GUIAnimFieldPathValue> values = new List<GUIAnimFieldPathValue>();
  394. foreach (var kvp in curves)
  395. {
  396. SerializableProperty property = GUIAnimFieldDisplay.FindProperty(selectedSO, kvp.Key);
  397. if (property != null)
  398. {
  399. GUIAnimFieldPathValue fieldValue = new GUIAnimFieldPathValue();
  400. fieldValue.path = kvp.Key;
  401. switch (kvp.Value.type)
  402. {
  403. case SerializableProperty.FieldType.Vector2:
  404. {
  405. Vector2 value = new Vector2();
  406. for (int i = 0; i < 2; i++)
  407. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  408. fieldValue.value = value;
  409. }
  410. break;
  411. case SerializableProperty.FieldType.Vector3:
  412. {
  413. Vector3 value = new Vector3();
  414. for (int i = 0; i < 3; i++)
  415. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  416. fieldValue.value = value;
  417. }
  418. break;
  419. case SerializableProperty.FieldType.Vector4:
  420. {
  421. Vector4 value = new Vector4();
  422. for (int i = 0; i < 4; i++)
  423. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  424. fieldValue.value = value;
  425. }
  426. break;
  427. case SerializableProperty.FieldType.Color:
  428. {
  429. Color value = new Color();
  430. for (int i = 0; i < 4; i++)
  431. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  432. fieldValue.value = value;
  433. }
  434. break;
  435. case SerializableProperty.FieldType.Bool:
  436. case SerializableProperty.FieldType.Int:
  437. case SerializableProperty.FieldType.Float:
  438. fieldValue.value = kvp.Value.curves[0].Evaluate(time, false); ;
  439. break;
  440. }
  441. values.Add(fieldValue);
  442. }
  443. }
  444. guiFieldDisplay.SetDisplayValues(values.ToArray());
  445. }
  446. private Vector2 GetOptimalRange()
  447. {
  448. List<EdAnimationCurve> displayedCurves = new List<EdAnimationCurve>();
  449. for (int i = 0; i < selectedFields.Count; i++)
  450. {
  451. EdAnimationCurve curve;
  452. if (TryGetCurve(selectedFields[i], out curve))
  453. displayedCurves.Add(curve);
  454. }
  455. float xRange;
  456. float yRange;
  457. CalculateRange(displayedCurves, out xRange, out yRange);
  458. // Add padding to y range
  459. yRange *= 1.05f;
  460. // Don't allow zero range
  461. if (xRange == 0.0f)
  462. xRange = 60.0f;
  463. if (yRange == 0.0f)
  464. yRange = 10.0f;
  465. return new Vector2(xRange, yRange);
  466. }
  467. private void UpdateDisplayedCurves()
  468. {
  469. List<EdAnimationCurve> curvesToDisplay = new List<EdAnimationCurve>();
  470. for (int i = 0; i < selectedFields.Count; i++)
  471. {
  472. EdAnimationCurve curve;
  473. if (TryGetCurve(selectedFields[i], out curve))
  474. curvesToDisplay.Add(curve);
  475. }
  476. guiCurveEditor.SetCurves(curvesToDisplay.ToArray());
  477. Vector2 newRange = GetOptimalRange();
  478. // Don't reduce visible range
  479. newRange.x = Math.Max(newRange.x, guiCurveEditor.Range.x);
  480. newRange.y = Math.Max(newRange.y, guiCurveEditor.Range.y);
  481. guiCurveEditor.Range = newRange;
  482. UpdateScrollBarSize();
  483. }
  484. #endregion
  485. #region Field display
  486. private List<string> selectedFields = new List<string>();
  487. private void AddNewField(string path, SerializableProperty.FieldType type)
  488. {
  489. guiFieldDisplay.AddField(path);
  490. switch (type)
  491. {
  492. case SerializableProperty.FieldType.Vector4:
  493. {
  494. FieldCurves fieldCurves = new FieldCurves();
  495. fieldCurves.type = type;
  496. fieldCurves.curves = new EdAnimationCurve[4];
  497. string[] subPaths = { ".x", ".y", ".z", ".w" };
  498. for (int i = 0; i < subPaths.Length; i++)
  499. {
  500. string subFieldPath = path + subPaths[i];
  501. fieldCurves.curves[i] = new EdAnimationCurve();
  502. selectedFields.Add(subFieldPath);
  503. }
  504. curves[path] = fieldCurves;
  505. }
  506. break;
  507. case SerializableProperty.FieldType.Vector3:
  508. {
  509. FieldCurves fieldCurves = new FieldCurves();
  510. fieldCurves.type = type;
  511. fieldCurves.curves = new EdAnimationCurve[3];
  512. string[] subPaths = { ".x", ".y", ".z" };
  513. for (int i = 0; i < subPaths.Length; i++)
  514. {
  515. string subFieldPath = path + subPaths[i];
  516. fieldCurves.curves[i] = new EdAnimationCurve();
  517. selectedFields.Add(subFieldPath);
  518. }
  519. curves[path] = fieldCurves;
  520. }
  521. break;
  522. case SerializableProperty.FieldType.Vector2:
  523. {
  524. FieldCurves fieldCurves = new FieldCurves();
  525. fieldCurves.type = type;
  526. fieldCurves.curves = new EdAnimationCurve[2];
  527. string[] subPaths = { ".x", ".y" };
  528. for (int i = 0; i < subPaths.Length; i++)
  529. {
  530. string subFieldPath = path + subPaths[i];
  531. fieldCurves.curves[i] = new EdAnimationCurve();
  532. selectedFields.Add(subFieldPath);
  533. }
  534. curves[path] = fieldCurves;
  535. }
  536. break;
  537. case SerializableProperty.FieldType.Color:
  538. {
  539. FieldCurves fieldCurves = new FieldCurves();
  540. fieldCurves.type = type;
  541. fieldCurves.curves = new EdAnimationCurve[4];
  542. string[] subPaths = { ".r", ".g", ".b", ".a" };
  543. for (int i = 0; i < subPaths.Length; i++)
  544. {
  545. string subFieldPath = path + subPaths[i];
  546. fieldCurves.curves[i] = new EdAnimationCurve();
  547. selectedFields.Add(subFieldPath);
  548. }
  549. curves[path] = fieldCurves;
  550. }
  551. break;
  552. default: // Primitive type
  553. {
  554. FieldCurves fieldCurves = new FieldCurves();
  555. fieldCurves.type = type;
  556. fieldCurves.curves = new EdAnimationCurve[1];
  557. fieldCurves.curves[0] = new EdAnimationCurve();
  558. selectedFields.Add(path);
  559. curves[path] = fieldCurves;
  560. }
  561. break;
  562. }
  563. UpdateDisplayedCurves();
  564. }
  565. private void SelectField(string path, bool additive)
  566. {
  567. if (!additive)
  568. selectedFields.Clear();
  569. if (!string.IsNullOrEmpty(path))
  570. {
  571. selectedFields.RemoveAll(x => { return x == path || IsPathParent(x, path); });
  572. selectedFields.Add(path);
  573. }
  574. guiFieldDisplay.SetSelection(selectedFields.ToArray());
  575. UpdateDisplayedCurves();
  576. }
  577. private void RemoveSelectedFields()
  578. {
  579. for (int i = 0; i < selectedFields.Count; i++)
  580. {
  581. selectedFields.Remove(selectedFields[i]);
  582. curves.Remove(GetSubPathParent(selectedFields[i]));
  583. }
  584. List<string> existingFields = new List<string>();
  585. foreach (var KVP in curves)
  586. existingFields.Add(KVP.Key);
  587. guiFieldDisplay.SetFields(existingFields.ToArray());
  588. selectedFields.Clear();
  589. UpdateDisplayedCurves();
  590. }
  591. #endregion
  592. #region Helpers
  593. private Vector2I GetCurveEditorSize()
  594. {
  595. Vector2I output = new Vector2I();
  596. output.x = Math.Max(0, Width - FIELD_DISPLAY_WIDTH - scrollBarWidth);
  597. output.y = Math.Max(0, Height - buttonLayoutHeight - scrollBarHeight);
  598. return output;
  599. }
  600. private static void CalculateRange(List<EdAnimationCurve> curves, out float xRange, out float yRange)
  601. {
  602. xRange = 0.0f;
  603. yRange = 0.0f;
  604. foreach (var curve in curves)
  605. {
  606. KeyFrame[] keyframes = curve.KeyFrames;
  607. foreach (var key in keyframes)
  608. {
  609. xRange = Math.Max(xRange, key.time);
  610. yRange = Math.Max(yRange, Math.Abs(key.value));
  611. }
  612. }
  613. }
  614. private bool TryGetCurve(string path, out EdAnimationCurve curve)
  615. {
  616. int index = path.LastIndexOf(".");
  617. string parentPath;
  618. string subPathSuffix = null;
  619. if (index == -1)
  620. {
  621. parentPath = path;
  622. }
  623. else
  624. {
  625. parentPath = path.Substring(0, index);
  626. subPathSuffix = path.Substring(index, path.Length - index);
  627. }
  628. FieldCurves fieldCurves;
  629. if (curves.TryGetValue(parentPath, out fieldCurves))
  630. {
  631. if (!string.IsNullOrEmpty(subPathSuffix))
  632. {
  633. if (subPathSuffix == ".x" || subPathSuffix == ".r")
  634. {
  635. curve = fieldCurves.curves[0];
  636. return true;
  637. }
  638. else if (subPathSuffix == ".y" || subPathSuffix == ".g")
  639. {
  640. curve = fieldCurves.curves[1];
  641. return true;
  642. }
  643. else if (subPathSuffix == ".z" || subPathSuffix == ".b")
  644. {
  645. curve = fieldCurves.curves[2];
  646. return true;
  647. }
  648. else if (subPathSuffix == ".w" || subPathSuffix == ".a")
  649. {
  650. curve = fieldCurves.curves[3];
  651. return true;
  652. }
  653. }
  654. else
  655. {
  656. curve = fieldCurves.curves[0];
  657. return true;
  658. }
  659. }
  660. curve = null;
  661. return false;
  662. }
  663. private bool IsPathParent(string child, string parent)
  664. {
  665. string[] childEntries = child.Split('/', '.');
  666. string[] parentEntries = parent.Split('/', '.');
  667. if (parentEntries.Length >= child.Length)
  668. return false;
  669. int compareLength = Math.Min(childEntries.Length, parentEntries.Length);
  670. for (int i = 0; i < compareLength; i++)
  671. {
  672. if (childEntries[i] != parentEntries[i])
  673. return false;
  674. }
  675. return true;
  676. }
  677. private string GetSubPathParent(string path)
  678. {
  679. int index = path.LastIndexOf(".");
  680. if (index == -1)
  681. return path;
  682. return path.Substring(0, index);
  683. }
  684. #endregion
  685. #region Input callbacks
  686. private void OnPointerPressed(PointerEvent ev)
  687. {
  688. if (!isInitialized)
  689. return;
  690. guiCurveEditor.OnPointerPressed(ev);
  691. if (ev.button == PointerButton.Middle)
  692. {
  693. Vector2I windowPos = ScreenToWindowPos(ev.ScreenPos);
  694. Vector2 curvePos;
  695. if (guiCurveEditor.WindowToCurveSpace(windowPos, out curvePos))
  696. {
  697. dragStartPos = windowPos;
  698. isButtonHeld = true;
  699. }
  700. }
  701. }
  702. private void OnPointerMoved(PointerEvent ev)
  703. {
  704. if (!isInitialized)
  705. return;
  706. guiCurveEditor.OnPointerMoved(ev);
  707. if (isButtonHeld)
  708. {
  709. Vector2I windowPos = ScreenToWindowPos(ev.ScreenPos);
  710. int distance = Vector2I.Distance(dragStartPos, windowPos);
  711. if (distance >= DRAG_START_DISTANCE)
  712. {
  713. isDragInProgress = true;
  714. Cursor.Hide();
  715. Rect2I clipRect;
  716. clipRect.x = ev.ScreenPos.x - 2;
  717. clipRect.y = ev.ScreenPos.y - 2;
  718. clipRect.width = 4;
  719. clipRect.height = 4;
  720. Cursor.ClipToRect(clipRect);
  721. }
  722. }
  723. }
  724. private void OnPointerReleased(PointerEvent ev)
  725. {
  726. if (isDragInProgress)
  727. {
  728. Cursor.Show();
  729. Cursor.ClipDisable();
  730. }
  731. isButtonHeld = false;
  732. isDragInProgress = false;
  733. if (!isInitialized)
  734. return;
  735. guiCurveEditor.OnPointerReleased(ev);
  736. }
  737. private void OnButtonUp(ButtonEvent ev)
  738. {
  739. if (!isInitialized)
  740. return;
  741. guiCurveEditor.OnButtonUp(ev);
  742. }
  743. #endregion
  744. #region General callbacks
  745. private void OnFieldAdded(string path, SerializableProperty.FieldType type)
  746. {
  747. AddNewField(path, type);
  748. }
  749. private void OnHorzScrollOrResize(float position, float size)
  750. {
  751. SetHorzScrollbarProperties(position, size);
  752. }
  753. private void OnVertScrollOrResize(float position, float size)
  754. {
  755. SetVertScrollbarProperties(position, size);
  756. }
  757. private void OnFieldSelected(string path)
  758. {
  759. bool additive = Input.IsButtonHeld(ButtonCode.LeftShift) || Input.IsButtonHeld(ButtonCode.RightShift);
  760. SelectField(path, additive);
  761. }
  762. private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
  763. {
  764. RebuildGUI();
  765. }
  766. private void OnFrameSelected(int frameIdx)
  767. {
  768. SetCurrentFrame(frameIdx);
  769. }
  770. #endregion
  771. }
  772. /// <summary>
  773. /// Drop down window that displays options used by the animation window.
  774. /// </summary>
  775. [DefaultSize(100, 50)]
  776. internal class AnimationOptions : DropDownWindow
  777. {
  778. /// <summary>
  779. /// Initializes the drop down window by creating the necessary GUI. Must be called after construction and before
  780. /// use.
  781. /// </summary>
  782. /// <param name="parent">Animation window that this drop down window is a part of.</param>
  783. internal void Initialize(AnimationWindow parent)
  784. {
  785. GUIIntField fpsField = new GUIIntField(new LocEdString("FPS"), 40);
  786. fpsField.Value = parent.FPS;
  787. fpsField.OnChanged += x => { parent.FPS = x; };
  788. GUILayoutY vertLayout = GUI.AddLayoutY();
  789. vertLayout.AddFlexibleSpace();
  790. GUILayoutX contentLayout = vertLayout.AddLayoutX();
  791. contentLayout.AddFlexibleSpace();
  792. contentLayout.AddElement(fpsField);
  793. contentLayout.AddFlexibleSpace();
  794. vertLayout.AddFlexibleSpace();
  795. }
  796. }
  797. /** @} */
  798. }