AnimationWindow.cs 51 KB

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