AnimationWindow.cs 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351
  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 System.IO;
  6. using System.Text;
  7. using BansheeEngine;
  8. namespace BansheeEditor
  9. {
  10. /** @addtogroup Windows
  11. * @{
  12. */
  13. /// <summary>
  14. /// Displays animation curve editor window. Allows the user to manipulate keyframes of animation curves, add/remove
  15. /// curves from an animation clip, and manipulate animation events.
  16. /// </summary>
  17. [DefaultSize(900, 500)]
  18. internal class AnimationWindow : EditorWindow
  19. {
  20. private const int FIELD_DISPLAY_WIDTH = 300;
  21. private const int DRAG_START_DISTANCE = 3;
  22. private const float DRAG_SCALE = 3.0f;
  23. private const float ZOOM_SCALE = 0.1f/120.0f; // One scroll step is usually 120 units, we want 1/10 of that
  24. private SceneObject selectedSO;
  25. /// <summary>
  26. /// Scene object for which are we currently changing the animation for.
  27. /// </summary>
  28. internal SceneObject SelectedSO
  29. {
  30. get { return selectedSO; }
  31. }
  32. #region Overrides
  33. /// <summary>
  34. /// Opens the animation window.
  35. /// </summary>
  36. [MenuItem("Windows/Animation", ButtonModifier.CtrlAlt, ButtonCode.A, 6000)]
  37. private static void OpenGameWindow()
  38. {
  39. OpenWindow<AnimationWindow>();
  40. }
  41. /// <inheritdoc/>
  42. protected override LocString GetDisplayName()
  43. {
  44. return new LocEdString("Animation");
  45. }
  46. private void OnInitialize()
  47. {
  48. Selection.OnSelectionChanged += OnSelectionChanged;
  49. UpdateSelectedSO(true);
  50. }
  51. private void OnEditorUpdate()
  52. {
  53. if (selectedSO == null)
  54. return;
  55. HandleDragAndZoomInput();
  56. }
  57. private void OnDestroy()
  58. {
  59. Selection.OnSelectionChanged -= OnSelectionChanged;
  60. if (selectedSO != null)
  61. {
  62. EditorInput.OnPointerPressed -= OnPointerPressed;
  63. EditorInput.OnPointerMoved -= OnPointerMoved;
  64. EditorInput.OnPointerReleased -= OnPointerReleased;
  65. EditorInput.OnButtonUp -= OnButtonUp;
  66. }
  67. }
  68. /// <inheritdoc/>
  69. protected override void WindowResized(int width, int height)
  70. {
  71. if (selectedSO == null)
  72. return;
  73. ResizeGUI(width, height);
  74. }
  75. #endregion
  76. #region GUI
  77. private GUIButton playButton;
  78. private GUIButton recordButton;
  79. private GUIButton prevFrameButton;
  80. private GUIIntField frameInputField;
  81. private GUIButton nextFrameButton;
  82. private GUIButton addKeyframeButton;
  83. private GUIButton addEventButton;
  84. private GUIButton optionsButton;
  85. private GUIButton addPropertyBtn;
  86. private GUIButton delPropertyBtn;
  87. private GUILayout buttonLayout;
  88. private int buttonLayoutHeight;
  89. private int scrollBarWidth;
  90. private int scrollBarHeight;
  91. private GUIResizeableScrollBarH horzScrollBar;
  92. private GUIResizeableScrollBarV vertScrollBar;
  93. private GUIPanel editorPanel;
  94. private GUIAnimFieldDisplay guiFieldDisplay;
  95. private GUICurveEditor guiCurveEditor;
  96. /// <summary>
  97. /// Recreates the entire curve editor GUI depending on the currently selected scene object.
  98. /// </summary>
  99. private void RebuildGUI()
  100. {
  101. GUI.Clear();
  102. guiCurveEditor = null;
  103. guiFieldDisplay = null;
  104. if (selectedSO == null)
  105. {
  106. GUILabel warningLbl = new GUILabel(new LocEdString("Select an object to animate in the Hierarchy or Scene windows."));
  107. GUILayoutY vertLayout = GUI.AddLayoutY();
  108. vertLayout.AddFlexibleSpace();
  109. GUILayoutX horzLayout = vertLayout.AddLayoutX();
  110. vertLayout.AddFlexibleSpace();
  111. horzLayout.AddFlexibleSpace();
  112. horzLayout.AddElement(warningLbl);
  113. horzLayout.AddFlexibleSpace();
  114. return;
  115. }
  116. // Top button row
  117. GUIContent playIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.Play),
  118. new LocEdString("Play"));
  119. GUIContent recordIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.Record),
  120. new LocEdString("Record"));
  121. GUIContent prevFrameIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.FrameBack),
  122. new LocEdString("Previous frame"));
  123. GUIContent nextFrameIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.FrameForward),
  124. new LocEdString("Next frame"));
  125. GUIContent addKeyframeIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.AddKeyframe),
  126. new LocEdString("Add keyframe"));
  127. GUIContent addEventIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.AddEvent),
  128. new LocEdString("Add event"));
  129. GUIContent optionsIcon = new GUIContent(EditorBuiltin.GetLibraryWindowIcon(LibraryWindowIcon.Options),
  130. new LocEdString("Options"));
  131. playButton = new GUIButton(playIcon);
  132. recordButton = new GUIButton(recordIcon);
  133. prevFrameButton = new GUIButton(prevFrameIcon);
  134. frameInputField = new GUIIntField();
  135. nextFrameButton = new GUIButton(nextFrameIcon);
  136. addKeyframeButton = new GUIButton(addKeyframeIcon);
  137. addEventButton = new GUIButton(addEventIcon);
  138. optionsButton = new GUIButton(optionsIcon);
  139. playButton.OnClick += () =>
  140. {
  141. // TODO
  142. // - Record current state of the scene object hierarchy
  143. // - Evaluate all curves manually and update them
  144. // - On end, restore original values of the scene object hierarchy
  145. };
  146. recordButton.OnClick += () =>
  147. {
  148. // TODO
  149. // - Every frame read back current values of all the current curve's properties and assign it to the current frame
  150. };
  151. prevFrameButton.OnClick += () =>
  152. {
  153. SetCurrentFrame(currentFrameIdx - 1);
  154. };
  155. frameInputField.OnChanged += SetCurrentFrame;
  156. nextFrameButton.OnClick += () =>
  157. {
  158. SetCurrentFrame(currentFrameIdx + 1);
  159. };
  160. addKeyframeButton.OnClick += () =>
  161. {
  162. guiCurveEditor.AddKeyFrameAtMarker();
  163. };
  164. addEventButton.OnClick += () =>
  165. {
  166. guiCurveEditor.AddEventAtMarker();
  167. };
  168. optionsButton.OnClick += () =>
  169. {
  170. Vector2I openPosition = ScreenToWindowPos(Input.PointerPosition);
  171. AnimationOptions dropDown = DropDownWindow.Open<AnimationOptions>(this, openPosition);
  172. dropDown.Initialize(this);
  173. };
  174. // Property buttons
  175. addPropertyBtn = new GUIButton(new LocEdString("Add property"));
  176. delPropertyBtn = new GUIButton(new LocEdString("Delete selected"));
  177. addPropertyBtn.OnClick += () =>
  178. {
  179. Action openPropertyWindow = () =>
  180. {
  181. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  182. FieldSelectionWindow fieldSelection = DropDownWindow.Open<FieldSelectionWindow>(this, windowPos);
  183. fieldSelection.OnFieldSelected += OnFieldAdded;
  184. };
  185. if (clipInfo.clip == null)
  186. {
  187. LocEdString title = new LocEdString("Warning");
  188. LocEdString message =
  189. new LocEdString("Selected object doesn't have an animation clip assigned. Would you like to create" +
  190. " a new animation clip?");
  191. DialogBox.Open(title, message, DialogBox.Type.YesNoCancel, type =>
  192. {
  193. if (type == DialogBox.ResultType.Yes)
  194. {
  195. string clipSavePath;
  196. if (BrowseDialog.SaveFile(ProjectLibrary.ResourceFolder, "*.asset", out clipSavePath))
  197. {
  198. clipSavePath = Path.ChangeExtension(clipSavePath, ".asset");
  199. AnimationClip newClip = new AnimationClip();
  200. ProjectLibrary.Create(newClip, clipSavePath);
  201. LoadAnimClip(newClip);
  202. Animation animation = selectedSO.GetComponent<Animation>();
  203. if (animation == null)
  204. animation = selectedSO.AddComponent<Animation>();
  205. animation.DefaultClip = newClip;
  206. EditorApplication.SetSceneDirty();
  207. openPropertyWindow();
  208. }
  209. }
  210. });
  211. }
  212. else
  213. {
  214. if (clipInfo.isImported)
  215. {
  216. LocEdString title = new LocEdString("Warning");
  217. LocEdString message =
  218. new LocEdString("You cannot add/edit/remove curves from animation clips that" +
  219. " are imported from an external file.");
  220. DialogBox.Open(title, message, DialogBox.Type.OK);
  221. }
  222. else
  223. openPropertyWindow();
  224. }
  225. };
  226. delPropertyBtn.OnClick += () =>
  227. {
  228. if (clipInfo.clip == null)
  229. return;
  230. if (clipInfo.isImported)
  231. {
  232. LocEdString title = new LocEdString("Warning");
  233. LocEdString message =
  234. new LocEdString("You cannot add/edit/remove curves from animation clips that" +
  235. " are imported from an external file.");
  236. DialogBox.Open(title, message, DialogBox.Type.OK);
  237. }
  238. else
  239. {
  240. LocEdString title = new LocEdString("Warning");
  241. LocEdString message = new LocEdString("Are you sure you want to remove all selected fields?");
  242. DialogBox.Open(title, message, DialogBox.Type.YesNo, x =>
  243. {
  244. if (x == DialogBox.ResultType.Yes)
  245. {
  246. RemoveSelectedFields();
  247. }
  248. });
  249. }
  250. };
  251. GUIPanel mainPanel = GUI.AddPanel();
  252. GUIPanel backgroundPanel = GUI.AddPanel(1);
  253. GUILayout mainLayout = mainPanel.AddLayoutY();
  254. buttonLayout = mainLayout.AddLayoutX();
  255. buttonLayout.AddSpace(5);
  256. buttonLayout.AddElement(playButton);
  257. buttonLayout.AddElement(recordButton);
  258. buttonLayout.AddSpace(5);
  259. buttonLayout.AddElement(prevFrameButton);
  260. buttonLayout.AddElement(frameInputField);
  261. buttonLayout.AddElement(nextFrameButton);
  262. buttonLayout.AddSpace(5);
  263. buttonLayout.AddElement(addKeyframeButton);
  264. buttonLayout.AddElement(addEventButton);
  265. buttonLayout.AddSpace(5);
  266. buttonLayout.AddElement(optionsButton);
  267. buttonLayout.AddFlexibleSpace();
  268. buttonLayoutHeight = playButton.Bounds.height;
  269. GUITexture buttonBackground = new GUITexture(null, EditorStyles.HeaderBackground);
  270. buttonBackground.Bounds = new Rect2I(0, 0, Width, buttonLayoutHeight);
  271. backgroundPanel.AddElement(buttonBackground);
  272. GUILayout contentLayout = mainLayout.AddLayoutX();
  273. GUILayout fieldDisplayLayout = contentLayout.AddLayoutY(GUIOption.FixedWidth(FIELD_DISPLAY_WIDTH));
  274. guiFieldDisplay = new GUIAnimFieldDisplay(fieldDisplayLayout, FIELD_DISPLAY_WIDTH,
  275. Height - buttonLayoutHeight * 2, selectedSO);
  276. guiFieldDisplay.OnEntrySelected += OnFieldSelected;
  277. GUILayout bottomButtonLayout = fieldDisplayLayout.AddLayoutX();
  278. bottomButtonLayout.AddElement(addPropertyBtn);
  279. bottomButtonLayout.AddElement(delPropertyBtn);
  280. horzScrollBar = new GUIResizeableScrollBarH();
  281. horzScrollBar.OnScrollOrResize += OnHorzScrollOrResize;
  282. vertScrollBar = new GUIResizeableScrollBarV();
  283. vertScrollBar.OnScrollOrResize += OnVertScrollOrResize;
  284. GUITexture separator = new GUITexture(null, EditorStyles.Separator, GUIOption.FixedWidth(3));
  285. contentLayout.AddElement(separator);
  286. GUILayout curveLayout = contentLayout.AddLayoutY();
  287. GUILayout curveLayoutHorz = curveLayout.AddLayoutX();
  288. GUILayout horzScrollBarLayout = curveLayout.AddLayoutX();
  289. horzScrollBarLayout.AddElement(horzScrollBar);
  290. horzScrollBarLayout.AddFlexibleSpace();
  291. editorPanel = curveLayoutHorz.AddPanel();
  292. curveLayoutHorz.AddElement(vertScrollBar);
  293. curveLayoutHorz.AddFlexibleSpace();
  294. scrollBarHeight = horzScrollBar.Bounds.height;
  295. scrollBarWidth = vertScrollBar.Bounds.width;
  296. Vector2I curveEditorSize = GetCurveEditorSize();
  297. guiCurveEditor = new GUICurveEditor(this, editorPanel, curveEditorSize.x, curveEditorSize.y);
  298. guiCurveEditor.OnFrameSelected += OnFrameSelected;
  299. guiCurveEditor.OnEventAdded += OnEventsChanged;
  300. guiCurveEditor.OnEventModified += EditorApplication.SetProjectDirty;
  301. guiCurveEditor.OnEventDeleted += OnEventsChanged;
  302. guiCurveEditor.OnCurveModified += EditorApplication.SetProjectDirty;
  303. guiCurveEditor.Redraw();
  304. horzScrollBar.SetWidth(curveEditorSize.x);
  305. vertScrollBar.SetHeight(curveEditorSize.y);
  306. UpdateScrollBarSize();
  307. }
  308. /// <summary>
  309. /// Resizes GUI elements so they fit within the provided boundaries.
  310. /// </summary>
  311. /// <param name="width">Width of the GUI bounds, in pixels.</param>
  312. /// <param name="height">Height of the GUI bounds, in pixels.</param>
  313. private void ResizeGUI(int width, int height)
  314. {
  315. guiFieldDisplay.SetSize(FIELD_DISPLAY_WIDTH, height - buttonLayoutHeight * 2);
  316. Vector2I curveEditorSize = GetCurveEditorSize();
  317. guiCurveEditor.SetSize(curveEditorSize.x, curveEditorSize.y);
  318. guiCurveEditor.Redraw();
  319. horzScrollBar.SetWidth(curveEditorSize.x);
  320. vertScrollBar.SetHeight(curveEditorSize.y);
  321. UpdateScrollBarSize();
  322. UpdateScrollBarPosition();
  323. }
  324. #endregion
  325. #region Scroll, drag, zoom
  326. private Vector2I dragStartPos;
  327. private bool isButtonHeld;
  328. private bool isDragInProgress;
  329. private float zoomAmount;
  330. /// <summary>
  331. /// Handles mouse scroll wheel and dragging events in order to zoom or drag the displayed curve editor contents.
  332. /// </summary>
  333. private void HandleDragAndZoomInput()
  334. {
  335. // Handle middle mouse dragging
  336. if (isDragInProgress)
  337. {
  338. float lengthPerPixel = guiCurveEditor.Range.x / guiCurveEditor.Width;
  339. float heightPerPixel = guiCurveEditor.Range.y / guiCurveEditor.Height;
  340. float dragX = Input.GetAxisValue(InputAxis.MouseX) * DRAG_SCALE * lengthPerPixel;
  341. float dragY = Input.GetAxisValue(InputAxis.MouseY) * DRAG_SCALE * heightPerPixel;
  342. Vector2 offset = guiCurveEditor.Offset;
  343. offset.x = Math.Max(0.0f, offset.x + dragX);
  344. offset.y -= dragY;
  345. guiCurveEditor.Offset = offset;
  346. UpdateScrollBarSize();
  347. UpdateScrollBarPosition();
  348. }
  349. // Handle zoom in/out
  350. float scroll = Input.GetAxisValue(InputAxis.MouseZ);
  351. if (scroll != 0.0f)
  352. {
  353. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  354. Vector2 curvePos;
  355. if (guiCurveEditor.WindowToCurveSpace(windowPos, out curvePos))
  356. {
  357. float zoom = scroll * ZOOM_SCALE;
  358. Zoom(curvePos, zoom);
  359. }
  360. }
  361. }
  362. /// <summary>
  363. /// Moves or resizes the vertical scroll bar under the curve editor.
  364. /// </summary>
  365. /// <param name="position">New position of the scrollbar, in range [0, 1].</param>
  366. /// <param name="size">New size of the scrollbar handle, in range [0, 1].</param>
  367. private void SetVertScrollbarProperties(float position, float size)
  368. {
  369. Vector2 visibleRange = guiCurveEditor.Range;
  370. Vector2 totalRange = GetTotalRange();
  371. visibleRange.y = totalRange.y*size;
  372. guiCurveEditor.Range = visibleRange;
  373. float scrollableRange = totalRange.y - visibleRange.y;
  374. Vector2 offset = guiCurveEditor.Offset;
  375. offset.y = -scrollableRange * (position * 2.0f - 1.0f);
  376. guiCurveEditor.Offset = offset;
  377. }
  378. /// <summary>
  379. /// Moves or resizes the horizontal scroll bar under the curve editor.
  380. /// </summary>
  381. /// <param name="position">New position of the scrollbar, in range [0, 1].</param>
  382. /// <param name="size">New size of the scrollbar handle, in range [0, 1].</param>
  383. private void SetHorzScrollbarProperties(float position, float size)
  384. {
  385. Vector2 visibleRange = guiCurveEditor.Range;
  386. Vector2 totalRange = GetTotalRange();
  387. visibleRange.x = totalRange.x * size;
  388. guiCurveEditor.Range = visibleRange;
  389. float scrollableRange = totalRange.x - visibleRange.x;
  390. Vector2 offset = guiCurveEditor.Offset;
  391. offset.x = scrollableRange * position;
  392. guiCurveEditor.Offset = offset;
  393. }
  394. /// <summary>
  395. /// Updates the size of both scrollbars depending on the currently visible curve area vs. the total curve area.
  396. /// </summary>
  397. private void UpdateScrollBarSize()
  398. {
  399. Vector2 visibleRange = guiCurveEditor.Range;
  400. Vector2 totalRange = GetTotalRange();
  401. horzScrollBar.HandleSize = visibleRange.x / totalRange.x;
  402. vertScrollBar.HandleSize = visibleRange.y / totalRange.y;
  403. }
  404. /// <summary>
  405. /// Updates the position of both scrollbars depending on the offset currently applied to the visible curve area.
  406. /// </summary>
  407. private void UpdateScrollBarPosition()
  408. {
  409. Vector2 visibleRange = guiCurveEditor.Range;
  410. Vector2 totalRange = GetTotalRange();
  411. Vector2 scrollableRange = totalRange - visibleRange;
  412. Vector2 offset = guiCurveEditor.Offset;
  413. if (scrollableRange.x > 0.0f)
  414. horzScrollBar.Position = offset.x / scrollableRange.x;
  415. else
  416. horzScrollBar.Position = 0.0f;
  417. if (scrollableRange.y > 0.0f)
  418. {
  419. float pos = offset.y/scrollableRange.y;
  420. float sign = MathEx.Sign(pos);
  421. pos = sign*MathEx.Clamp01(MathEx.Abs(pos));
  422. pos = (1.0f - pos) /2.0f;
  423. vertScrollBar.Position = pos;
  424. }
  425. else
  426. vertScrollBar.Position = 0.0f;
  427. }
  428. /// <summary>
  429. /// Calculates the width/height of the curve area depending on the current zoom level.
  430. /// </summary>
  431. /// <returns>Width/height of the curve area, in curve space (value, time).</returns>
  432. private Vector2 GetZoomedRange()
  433. {
  434. float zoomLevel = MathEx.Pow(2, zoomAmount);
  435. Vector2 optimalRange = GetOptimalRange();
  436. return optimalRange / zoomLevel;
  437. }
  438. /// <summary>
  439. /// Returns the total width/height of the contents of the curve area.
  440. /// </summary>
  441. /// <returns>Width/height of the curve area, in curve space (value, time).</returns>
  442. private Vector2 GetTotalRange()
  443. {
  444. // Return optimal range (that covers the visible curve)
  445. Vector2 optimalRange = GetOptimalRange();
  446. // Increase range in case user zoomed out
  447. Vector2 zoomedRange = GetZoomedRange();
  448. return Vector2.Max(optimalRange, zoomedRange);
  449. }
  450. /// <summary>
  451. /// Zooms in or out at the provided position in the curve display.
  452. /// </summary>
  453. /// <param name="curvePos">Position to zoom towards, relative to the curve display area, in curve space
  454. /// (value, time)</param>
  455. /// <param name="amount">Amount to zoom in (positive), or out (negative).</param>
  456. private void Zoom(Vector2 curvePos, float amount)
  457. {
  458. // Increase or decrease the visible range depending on zoom level
  459. Vector2 oldZoomedRange = GetZoomedRange();
  460. zoomAmount = MathEx.Clamp(zoomAmount + amount, -10.0f, 10.0f);
  461. Vector2 zoomedRange = GetZoomedRange();
  462. Vector2 zoomedDiff = zoomedRange - oldZoomedRange;
  463. Vector2 currentRange = guiCurveEditor.Range;
  464. Vector2 newRange = currentRange + zoomedDiff;
  465. guiCurveEditor.Range = newRange;
  466. // When zooming, make sure to focus on the point provided, so adjust the offset
  467. Vector2 rangeScale = newRange;
  468. rangeScale.x /= currentRange.x;
  469. rangeScale.y /= currentRange.y;
  470. Vector2 relativeCurvePos = curvePos - guiCurveEditor.Offset;
  471. Vector2 newCurvePos = relativeCurvePos * rangeScale;
  472. Vector2 diff = newCurvePos - relativeCurvePos;
  473. guiCurveEditor.Offset -= diff;
  474. UpdateScrollBarSize();
  475. UpdateScrollBarPosition();
  476. }
  477. #endregion
  478. #region Curve save/load
  479. private EditorAnimClipInfo clipInfo;
  480. /// <summary>
  481. /// Refreshes the contents of the curve and property display by loading animation curves from the provided
  482. /// animation clip.
  483. /// </summary>
  484. /// <param name="clip">Clip containing the animation to load.</param>
  485. private void LoadAnimClip(AnimationClip clip)
  486. {
  487. EditorPersistentData persistentData = EditorApplication.PersistentData;
  488. if (persistentData.dirtyAnimClips.TryGetValue(clip.UUID, out clipInfo))
  489. {
  490. // If an animation clip is imported, we don't care about it's cached curve values as they could have changed
  491. // since last modification, so we re-load the clip. But we persist the events as those can only be set
  492. // within the editor.
  493. if (clipInfo.isImported)
  494. {
  495. EditorAnimClipInfo newClipInfo = EditorAnimClipInfo.Create(clip);
  496. newClipInfo.events = clipInfo.events;
  497. }
  498. }
  499. else
  500. clipInfo = EditorAnimClipInfo.Create(clip);
  501. persistentData.dirtyAnimClips[clip.UUID] = clipInfo;
  502. foreach (var curve in clipInfo.curves)
  503. guiFieldDisplay.AddField(new AnimFieldInfo(curve.Key, curve.Value, !clipInfo.isImported));
  504. guiCurveEditor.Events = clipInfo.events;
  505. guiCurveEditor.DisableCurveEdit = clipInfo.isImported;
  506. SetCurrentFrame(0);
  507. FPS = clipInfo.sampleRate;
  508. }
  509. /// <summary>
  510. /// Checks if the currently selected object has changed, and rebuilds the GUI and loads the animation clip if needed.
  511. /// </summary>
  512. /// <param name="force">If true the GUI rebuild and animation clip load will be forced regardless if the active
  513. /// scene object changed.</param>
  514. private void UpdateSelectedSO(bool force)
  515. {
  516. SceneObject so = Selection.SceneObject;
  517. if (selectedSO != so || force)
  518. {
  519. if (selectedSO != null && so == null)
  520. {
  521. EditorInput.OnPointerPressed -= OnPointerPressed;
  522. EditorInput.OnPointerMoved -= OnPointerMoved;
  523. EditorInput.OnPointerReleased -= OnPointerReleased;
  524. EditorInput.OnButtonUp -= OnButtonUp;
  525. }
  526. else if (selectedSO == null && so != null)
  527. {
  528. EditorInput.OnPointerPressed += OnPointerPressed;
  529. EditorInput.OnPointerMoved += OnPointerMoved;
  530. EditorInput.OnPointerReleased += OnPointerReleased;
  531. EditorInput.OnButtonUp += OnButtonUp;
  532. }
  533. zoomAmount = 0.0f;
  534. selectedSO = so;
  535. selectedFields.Clear();
  536. clipInfo = null;
  537. RebuildGUI();
  538. // Load existing clip if one exists
  539. if (selectedSO != null)
  540. {
  541. Animation animation = selectedSO.GetComponent<Animation>();
  542. if (animation != null)
  543. {
  544. AnimationClip clip = animation.DefaultClip;
  545. if (clip != null)
  546. LoadAnimClip(clip);
  547. }
  548. }
  549. if(clipInfo == null)
  550. clipInfo = new EditorAnimClipInfo();
  551. if(selectedSO != null)
  552. UpdateDisplayedCurves(true);
  553. }
  554. }
  555. #endregion
  556. #region Curve display
  557. private int currentFrameIdx;
  558. private int fps = 1;
  559. /// <summary>
  560. /// Sampling rate of the animation in frames per second. Determines granularity at which positions keyframes can be
  561. /// placed.
  562. /// </summary>
  563. internal int FPS
  564. {
  565. get { return fps; }
  566. set { guiCurveEditor.SetFPS(value); fps = MathEx.Max(value, 1); }
  567. }
  568. /// <summary>
  569. /// Changes the currently selected frame in the curve display.
  570. /// </summary>
  571. /// <param name="frameIdx">Index of the frame to select.</param>
  572. private void SetCurrentFrame(int frameIdx)
  573. {
  574. currentFrameIdx = Math.Max(0, frameIdx);
  575. frameInputField.Value = currentFrameIdx;
  576. guiCurveEditor.SetMarkedFrame(currentFrameIdx);
  577. float time = guiCurveEditor.GetTimeForFrame(currentFrameIdx);
  578. List<GUIAnimFieldPathValue> values = new List<GUIAnimFieldPathValue>();
  579. foreach (var kvp in clipInfo.curves)
  580. {
  581. GUIAnimFieldPathValue fieldValue = new GUIAnimFieldPathValue();
  582. fieldValue.path = kvp.Key;
  583. switch (kvp.Value.type)
  584. {
  585. case SerializableProperty.FieldType.Vector2:
  586. {
  587. Vector2 value = new Vector2();
  588. for (int i = 0; i < 2; i++)
  589. value[i] = kvp.Value.curveInfos[i].curve.Evaluate(time, false);
  590. fieldValue.value = value;
  591. }
  592. break;
  593. case SerializableProperty.FieldType.Vector3:
  594. {
  595. Vector3 value = new Vector3();
  596. for (int i = 0; i < 3; i++)
  597. value[i] = kvp.Value.curveInfos[i].curve.Evaluate(time, false);
  598. fieldValue.value = value;
  599. }
  600. break;
  601. case SerializableProperty.FieldType.Vector4:
  602. {
  603. Vector4 value = new Vector4();
  604. for (int i = 0; i < 4; i++)
  605. value[i] = kvp.Value.curveInfos[i].curve.Evaluate(time, false);
  606. fieldValue.value = value;
  607. }
  608. break;
  609. case SerializableProperty.FieldType.Color:
  610. {
  611. Color value = new Color();
  612. for (int i = 0; i < 4; i++)
  613. value[i] = kvp.Value.curveInfos[i].curve.Evaluate(time, false);
  614. fieldValue.value = value;
  615. }
  616. break;
  617. case SerializableProperty.FieldType.Bool:
  618. case SerializableProperty.FieldType.Int:
  619. case SerializableProperty.FieldType.Float:
  620. fieldValue.value = kvp.Value.curveInfos[0].curve.Evaluate(time, false); ;
  621. break;
  622. }
  623. values.Add(fieldValue);
  624. }
  625. guiFieldDisplay.SetDisplayValues(values.ToArray());
  626. }
  627. /// <summary>
  628. /// Returns a list of all animation curves that should be displayed in the curve display.
  629. /// </summary>
  630. /// <returns>Array of curves to display.</returns>
  631. private CurveDrawInfo[] GetDisplayedCurves()
  632. {
  633. List<CurveDrawInfo> curvesToDisplay = new List<CurveDrawInfo>();
  634. if (selectedFields.Count == 0) // Display all if nothing is selected
  635. {
  636. if (clipInfo == null)
  637. return curvesToDisplay.ToArray();
  638. foreach (var curve in clipInfo.curves)
  639. {
  640. for (int i = 0; i < curve.Value.curveInfos.Length; i++)
  641. curvesToDisplay.Add(curve.Value.curveInfos[i]);
  642. }
  643. }
  644. else
  645. {
  646. for (int i = 0; i < selectedFields.Count; i++)
  647. {
  648. CurveDrawInfo[] curveInfos;
  649. if (TryGetCurve(selectedFields[i], out curveInfos))
  650. curvesToDisplay.AddRange(curveInfos);
  651. }
  652. }
  653. return curvesToDisplay.ToArray();
  654. }
  655. /// <summary>
  656. /// Returns width/height required to show the entire contents of the currently displayed curves.
  657. /// </summary>
  658. /// <returns>Width/height of the curve area, in curve space (value, time).</returns>
  659. private Vector2 GetOptimalRange()
  660. {
  661. CurveDrawInfo[] curvesToDisplay = GetDisplayedCurves();
  662. float xRange;
  663. float yRange;
  664. CalculateRange(curvesToDisplay, out xRange, out yRange);
  665. // Add padding to y range
  666. yRange *= 1.05f;
  667. // Don't allow zero range
  668. if (xRange == 0.0f)
  669. xRange = 60.0f;
  670. if (yRange == 0.0f)
  671. yRange = 10.0f;
  672. return new Vector2(xRange, yRange);
  673. }
  674. /// <summary>
  675. /// Calculates an unique color for each animation curve.
  676. /// </summary>
  677. private void UpdateCurveColors()
  678. {
  679. int globalCurveIdx = 0;
  680. foreach (var curveGroup in clipInfo.curves)
  681. {
  682. for (int i = 0; i < curveGroup.Value.curveInfos.Length; i++)
  683. curveGroup.Value.curveInfos[i].color = GUICurveDrawing.GetUniqueColor(globalCurveIdx++);
  684. }
  685. }
  686. /// <summary>
  687. /// Updates the curve display with currently selected curves.
  688. /// </summary>
  689. /// <param name="allowReduce">Normally the curve display will expand if newly selected curves cover a larger area
  690. /// than currently available, but the area won't be reduced if the selected curves cover
  691. /// a smaller area. Set this to true to allow the area to be reduced.</param>
  692. private void UpdateDisplayedCurves(bool allowReduce = false)
  693. {
  694. CurveDrawInfo[] curvesToDisplay = GetDisplayedCurves();
  695. guiCurveEditor.SetCurves(curvesToDisplay);
  696. Vector2 newRange = GetOptimalRange();
  697. if (!allowReduce)
  698. {
  699. // Don't reduce visible range
  700. newRange.x = Math.Max(newRange.x, guiCurveEditor.Range.x);
  701. newRange.y = Math.Max(newRange.y, guiCurveEditor.Range.y);
  702. }
  703. guiCurveEditor.Range = newRange;
  704. UpdateScrollBarSize();
  705. }
  706. #endregion
  707. #region Field display
  708. private List<string> selectedFields = new List<string>();
  709. /// <summary>
  710. /// Registers a new animation curve field.
  711. /// </summary>
  712. /// <param name="path">Path of the field, see <see cref="GUIFieldSelector.OnElementSelected"/></param>
  713. /// <param name="type">Type of the field (float, vector, etc.)</param>
  714. private void AddNewField(string path, SerializableProperty.FieldType type)
  715. {
  716. bool noSelection = selectedFields.Count == 0;
  717. switch (type)
  718. {
  719. case SerializableProperty.FieldType.Vector4:
  720. {
  721. FieldAnimCurves fieldCurves = new FieldAnimCurves();
  722. fieldCurves.type = type;
  723. fieldCurves.curveInfos = new CurveDrawInfo[4];
  724. string[] subPaths = { ".x", ".y", ".z", ".w" };
  725. for (int i = 0; i < subPaths.Length; i++)
  726. {
  727. string subFieldPath = path + subPaths[i];
  728. fieldCurves.curveInfos[i].curve = new EdAnimationCurve();
  729. selectedFields.Add(subFieldPath);
  730. }
  731. clipInfo.curves[path] = fieldCurves;
  732. }
  733. break;
  734. case SerializableProperty.FieldType.Vector3:
  735. {
  736. FieldAnimCurves fieldCurves = new FieldAnimCurves();
  737. fieldCurves.type = type;
  738. fieldCurves.curveInfos = new CurveDrawInfo[3];
  739. string[] subPaths = { ".x", ".y", ".z" };
  740. for (int i = 0; i < subPaths.Length; i++)
  741. {
  742. string subFieldPath = path + subPaths[i];
  743. fieldCurves.curveInfos[i].curve = new EdAnimationCurve();
  744. selectedFields.Add(subFieldPath);
  745. }
  746. clipInfo.curves[path] = fieldCurves;
  747. }
  748. break;
  749. case SerializableProperty.FieldType.Vector2:
  750. {
  751. FieldAnimCurves fieldCurves = new FieldAnimCurves();
  752. fieldCurves.type = type;
  753. fieldCurves.curveInfos = new CurveDrawInfo[2];
  754. string[] subPaths = { ".x", ".y" };
  755. for (int i = 0; i < subPaths.Length; i++)
  756. {
  757. string subFieldPath = path + subPaths[i];
  758. fieldCurves.curveInfos[i].curve = new EdAnimationCurve();
  759. selectedFields.Add(subFieldPath);
  760. }
  761. clipInfo.curves[path] = fieldCurves;
  762. }
  763. break;
  764. case SerializableProperty.FieldType.Color:
  765. {
  766. FieldAnimCurves fieldCurves = new FieldAnimCurves();
  767. fieldCurves.type = type;
  768. fieldCurves.curveInfos = new CurveDrawInfo[4];
  769. string[] subPaths = { ".r", ".g", ".b", ".a" };
  770. for (int i = 0; i < subPaths.Length; i++)
  771. {
  772. string subFieldPath = path + subPaths[i];
  773. fieldCurves.curveInfos[i].curve = new EdAnimationCurve();
  774. selectedFields.Add(subFieldPath);
  775. }
  776. clipInfo.curves[path] = fieldCurves;
  777. }
  778. break;
  779. default: // Primitive type
  780. {
  781. FieldAnimCurves fieldCurves = new FieldAnimCurves();
  782. fieldCurves.type = type;
  783. fieldCurves.curveInfos = new CurveDrawInfo[1];
  784. fieldCurves.curveInfos[0].curve = new EdAnimationCurve();
  785. selectedFields.Add(path);
  786. clipInfo.curves[path] = fieldCurves;
  787. }
  788. break;
  789. }
  790. UpdateCurveColors();
  791. UpdateDisplayedFields();
  792. EditorApplication.SetProjectDirty();
  793. UpdateDisplayedCurves(noSelection);
  794. }
  795. /// <summary>
  796. /// Selects a new animation curve field, making the curve display in the curve display GUI element.
  797. /// </summary>
  798. /// <param name="path">Path of the field to display.</param>
  799. /// <param name="additive">If true the field will be shown along with any already selected fields, or if false
  800. /// only the provided field will be shown.</param>
  801. private void SelectField(string path, bool additive)
  802. {
  803. if (!additive)
  804. selectedFields.Clear();
  805. bool noSelection = selectedFields.Count == 0;
  806. if (!string.IsNullOrEmpty(path))
  807. {
  808. selectedFields.RemoveAll(x => { return x == path || IsPathParent(x, path); });
  809. selectedFields.Add(path);
  810. }
  811. guiFieldDisplay.SetSelection(selectedFields.ToArray());
  812. UpdateDisplayedCurves(noSelection);
  813. }
  814. /// <summary>
  815. /// Deletes all currently selecting fields, removing them their curves permanently.
  816. /// </summary>
  817. private void RemoveSelectedFields()
  818. {
  819. for (int i = 0; i < selectedFields.Count; i++)
  820. clipInfo.curves.Remove(GetSubPathParent(selectedFields[i]));
  821. UpdateCurveColors();
  822. UpdateDisplayedFields();
  823. selectedFields.Clear();
  824. EditorApplication.SetProjectDirty();
  825. UpdateDisplayedCurves();
  826. }
  827. /// <summary>
  828. /// Updates the GUI element displaying the current animation curve fields.
  829. /// </summary>
  830. private void UpdateDisplayedFields()
  831. {
  832. List<AnimFieldInfo> existingFields = new List<AnimFieldInfo>();
  833. foreach (var KVP in clipInfo.curves)
  834. existingFields.Add(new AnimFieldInfo(KVP.Key, KVP.Value, !clipInfo.isImported));
  835. guiFieldDisplay.SetFields(existingFields.ToArray());
  836. }
  837. #endregion
  838. #region Helpers
  839. /// <summary>
  840. /// Returns the size of the curve editor GUI element.
  841. /// </summary>
  842. /// <returns>Width/height of the curve editor, in pixels.</returns>
  843. private Vector2I GetCurveEditorSize()
  844. {
  845. Vector2I output = new Vector2I();
  846. output.x = Math.Max(0, Width - FIELD_DISPLAY_WIDTH - scrollBarWidth);
  847. output.y = Math.Max(0, Height - buttonLayoutHeight - scrollBarHeight);
  848. return output;
  849. }
  850. /// <summary>
  851. /// Calculates the total range covered by a set of curves.
  852. /// </summary>
  853. /// <param name="curveInfos">Curves to calculate range for.</param>
  854. /// <param name="xRange">Maximum time value present in the curves.</param>
  855. /// <param name="yRange">Maximum absolute curve value present in the curves.</param>
  856. private static void CalculateRange(CurveDrawInfo[] curveInfos, out float xRange, out float yRange)
  857. {
  858. // Note: This only evaluates at keyframes, we should also evaluate in-between in order to account for steep
  859. // tangents
  860. xRange = 0.0f;
  861. yRange = 0.0f;
  862. foreach (var curveInfo in curveInfos)
  863. {
  864. KeyFrame[] keyframes = curveInfo.curve.KeyFrames;
  865. foreach (var key in keyframes)
  866. {
  867. xRange = Math.Max(xRange, key.time);
  868. yRange = Math.Max(yRange, Math.Abs(key.value));
  869. }
  870. }
  871. }
  872. /// <summary>
  873. /// Attempts to find a curve field at the specified path.
  874. /// </summary>
  875. /// <param name="path">Path of the curve field to look for.</param>
  876. /// <param name="curveInfos">One or multiple curves found for the specific path (one field can have multiple curves
  877. /// if it is a complex type, like a vector).</param>
  878. /// <returns>True if the curve field was found, false otherwise.</returns>
  879. private bool TryGetCurve(string path, out CurveDrawInfo[] curveInfos)
  880. {
  881. int index = path.LastIndexOf(".");
  882. string parentPath;
  883. string subPathSuffix = null;
  884. if (index == -1)
  885. {
  886. parentPath = path;
  887. }
  888. else
  889. {
  890. parentPath = path.Substring(0, index);
  891. subPathSuffix = path.Substring(index, path.Length - index);
  892. }
  893. FieldAnimCurves fieldCurves;
  894. if (clipInfo.curves.TryGetValue(parentPath, out fieldCurves))
  895. {
  896. if (!string.IsNullOrEmpty(subPathSuffix))
  897. {
  898. if (subPathSuffix == ".x" || subPathSuffix == ".r")
  899. {
  900. curveInfos = new [] { fieldCurves.curveInfos[0] };
  901. return true;
  902. }
  903. else if (subPathSuffix == ".y" || subPathSuffix == ".g")
  904. {
  905. curveInfos = new[] { fieldCurves.curveInfos[1] };
  906. return true;
  907. }
  908. else if (subPathSuffix == ".z" || subPathSuffix == ".b")
  909. {
  910. curveInfos = new[] { fieldCurves.curveInfos[2] };
  911. return true;
  912. }
  913. else if (subPathSuffix == ".w" || subPathSuffix == ".a")
  914. {
  915. curveInfos = new[] { fieldCurves.curveInfos[3] };
  916. return true;
  917. }
  918. }
  919. else
  920. {
  921. curveInfos = fieldCurves.curveInfos;
  922. return true;
  923. }
  924. }
  925. curveInfos = new CurveDrawInfo[0];
  926. return false;
  927. }
  928. /// <summary>
  929. /// Checks if one curve field path a parent of the other.
  930. /// </summary>
  931. /// <param name="child">Path to check if it is a child of <paramref name="parent"/>.</param>
  932. /// <param name="parent">Path to check if it is a parent of <paramref name="child"/>.</param>
  933. /// <returns>True if <paramref name="child"/> is a child of <paramref name="parent"/>.</returns>
  934. private bool IsPathParent(string child, string parent)
  935. {
  936. string[] childEntries = child.Split('/', '.');
  937. string[] parentEntries = parent.Split('/', '.');
  938. if (parentEntries.Length >= child.Length)
  939. return false;
  940. int compareLength = Math.Min(childEntries.Length, parentEntries.Length);
  941. for (int i = 0; i < compareLength; i++)
  942. {
  943. if (childEntries[i] != parentEntries[i])
  944. return false;
  945. }
  946. return true;
  947. }
  948. /// <summary>
  949. /// If a path has sub-elements (e.g. .x, .r), returns a path without those elements. Otherwise returns the original
  950. /// path.
  951. /// </summary>
  952. /// <param name="path">Path to check.</param>
  953. /// <returns>Path without sub-elements.</returns>
  954. private string GetSubPathParent(string path)
  955. {
  956. int index = path.LastIndexOf(".");
  957. if (index == -1)
  958. return path;
  959. return path.Substring(0, index);
  960. }
  961. #endregion
  962. #region Input callbacks
  963. /// <summary>
  964. /// Triggered when the user presses a mouse button.
  965. /// </summary>
  966. /// <param name="ev">Information about the mouse press event.</param>
  967. private void OnPointerPressed(PointerEvent ev)
  968. {
  969. guiCurveEditor.OnPointerPressed(ev);
  970. if (ev.button == PointerButton.Middle)
  971. {
  972. Vector2I windowPos = ScreenToWindowPos(ev.ScreenPos);
  973. Vector2 curvePos;
  974. if (guiCurveEditor.WindowToCurveSpace(windowPos, out curvePos))
  975. {
  976. dragStartPos = windowPos;
  977. isButtonHeld = true;
  978. }
  979. }
  980. }
  981. /// <summary>
  982. /// Triggered when the user moves the mouse.
  983. /// </summary>
  984. /// <param name="ev">Information about the mouse move event.</param>
  985. private void OnPointerMoved(PointerEvent ev)
  986. {
  987. guiCurveEditor.OnPointerMoved(ev);
  988. if (isButtonHeld)
  989. {
  990. Vector2I windowPos = ScreenToWindowPos(ev.ScreenPos);
  991. int distance = Vector2I.Distance(dragStartPos, windowPos);
  992. if (distance >= DRAG_START_DISTANCE)
  993. {
  994. isDragInProgress = true;
  995. Cursor.Hide();
  996. Rect2I clipRect;
  997. clipRect.x = ev.ScreenPos.x - 2;
  998. clipRect.y = ev.ScreenPos.y - 2;
  999. clipRect.width = 4;
  1000. clipRect.height = 4;
  1001. Cursor.ClipToRect(clipRect);
  1002. }
  1003. }
  1004. }
  1005. /// <summary>
  1006. /// Triggered when the user releases a mouse button.
  1007. /// </summary>
  1008. /// <param name="ev">Information about the mouse release event.</param>
  1009. private void OnPointerReleased(PointerEvent ev)
  1010. {
  1011. if (isDragInProgress)
  1012. {
  1013. Cursor.Show();
  1014. Cursor.ClipDisable();
  1015. }
  1016. isButtonHeld = false;
  1017. isDragInProgress = false;
  1018. guiCurveEditor.OnPointerReleased(ev);
  1019. }
  1020. /// <summary>
  1021. /// Triggered when the user releases a keyboard button.
  1022. /// </summary>
  1023. /// <param name="ev">Information about the keyboard release event.</param>
  1024. private void OnButtonUp(ButtonEvent ev)
  1025. {
  1026. guiCurveEditor.OnButtonUp(ev);
  1027. }
  1028. #endregion
  1029. #region General callbacks
  1030. /// <summary>
  1031. /// Triggered by the field selector, when user selects a new curve field.
  1032. /// </summary>
  1033. /// <param name="path">Path of the selected curve field.</param>
  1034. /// <param name="type">Type of the selected curve field (float, vector, etc.).</param>
  1035. private void OnFieldAdded(string path, SerializableProperty.FieldType type)
  1036. {
  1037. // Remove the root scene object from the path (we know which SO it is, no need to hardcode its name in the path)
  1038. string pathNoRoot = path.TrimStart('/');
  1039. int separatorIdx = pathNoRoot.IndexOf("/");
  1040. if (separatorIdx == -1 || (separatorIdx + 1) >= pathNoRoot.Length)
  1041. return;
  1042. pathNoRoot = pathNoRoot.Substring(separatorIdx + 1, pathNoRoot.Length - separatorIdx - 1);
  1043. AddNewField(pathNoRoot, type);
  1044. }
  1045. /// <summary>
  1046. /// Triggered when the user moves or resizes the horizontal scrollbar.
  1047. /// </summary>
  1048. /// <param name="position">New position of the scrollbar, in range [0, 1].</param>
  1049. /// <param name="size">New size of the scrollbar, in range [0, 1].</param>
  1050. private void OnHorzScrollOrResize(float position, float size)
  1051. {
  1052. SetHorzScrollbarProperties(position, size);
  1053. }
  1054. /// <summary>
  1055. /// Triggered when the user moves or resizes the vertical scrollbar.
  1056. /// </summary>
  1057. /// <param name="position">New position of the scrollbar, in range [0, 1].</param>
  1058. /// <param name="size">New size of the scrollbar, in range [0, 1].</param>
  1059. private void OnVertScrollOrResize(float position, float size)
  1060. {
  1061. SetVertScrollbarProperties(position, size);
  1062. }
  1063. /// <summary>
  1064. /// Triggered when the user selects a new curve field.
  1065. /// </summary>
  1066. /// <param name="path">Path of the selected curve field.</param>
  1067. private void OnFieldSelected(string path)
  1068. {
  1069. bool additive = Input.IsButtonHeld(ButtonCode.LeftShift) || Input.IsButtonHeld(ButtonCode.RightShift);
  1070. SelectField(path, additive);
  1071. }
  1072. /// <summary>
  1073. /// Triggered when the user selects a new scene object or a resource.
  1074. /// </summary>
  1075. /// <param name="sceneObjects">Newly selected scene objects.</param>
  1076. /// <param name="resourcePaths">Newly selected resources.</param>
  1077. private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
  1078. {
  1079. UpdateSelectedSO(false);
  1080. }
  1081. /// <summary>
  1082. /// Triggered when the user selects a new frame in the curve display.
  1083. /// </summary>
  1084. /// <param name="frameIdx">Index of the selected frame.</param>
  1085. private void OnFrameSelected(int frameIdx)
  1086. {
  1087. SetCurrentFrame(frameIdx);
  1088. }
  1089. /// <summary>
  1090. /// Triggered when the user changed (add, removed or modified) animation events in the curve display.
  1091. /// </summary>
  1092. private void OnEventsChanged()
  1093. {
  1094. clipInfo.events = guiCurveEditor.Events;
  1095. EditorApplication.SetProjectDirty();
  1096. }
  1097. #endregion
  1098. }
  1099. /// <summary>
  1100. /// Drop down window that displays options used by the animation window.
  1101. /// </summary>
  1102. [DefaultSize(100, 50)]
  1103. internal class AnimationOptions : DropDownWindow
  1104. {
  1105. /// <summary>
  1106. /// Initializes the drop down window by creating the necessary GUI. Must be called after construction and before
  1107. /// use.
  1108. /// </summary>
  1109. /// <param name="parent">Animation window that this drop down window is a part of.</param>
  1110. internal void Initialize(AnimationWindow parent)
  1111. {
  1112. GUIIntField fpsField = new GUIIntField(new LocEdString("FPS"), 40);
  1113. fpsField.Value = parent.FPS;
  1114. fpsField.OnChanged += x => { parent.FPS = x; };
  1115. GUILayoutY vertLayout = GUI.AddLayoutY();
  1116. vertLayout.AddFlexibleSpace();
  1117. GUILayoutX contentLayout = vertLayout.AddLayoutX();
  1118. contentLayout.AddFlexibleSpace();
  1119. contentLayout.AddElement(fpsField);
  1120. contentLayout.AddFlexibleSpace();
  1121. vertLayout.AddFlexibleSpace();
  1122. }
  1123. }
  1124. /** @} */
  1125. }