AnimationWindow.cs 34 KB

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