AnimationWindow.cs 52 KB

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