AnimationWindow.cs 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412
  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. // TODO - Add event
  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 List<AnimationEvent> events = new List<AnimationEvent>();
  403. private bool clipIsImported;
  404. private void LoadFromClip(AnimationClip clip)
  405. {
  406. curves.Clear();
  407. selectedFields.Clear();
  408. guiFieldDisplay.SetFields(new string[0]);
  409. clipIsImported = IsClipImported(clip);
  410. AnimationCurves clipCurves = clip.Curves;
  411. EditorCurveData editorCurveData = null;
  412. string resourcePath = ProjectLibrary.GetPath(clip);
  413. if (!string.IsNullOrEmpty(resourcePath))
  414. {
  415. LibraryEntry entry = ProjectLibrary.GetEntry(resourcePath);
  416. string clipName = PathEx.GetTail(resourcePath);
  417. if (entry != null && entry.Type == LibraryEntryType.File)
  418. {
  419. FileEntry fileEntry = (FileEntry)entry;
  420. ResourceMeta[] metas = fileEntry.ResourceMetas;
  421. for (int i = 0; i < metas.Length; i++)
  422. {
  423. if (clipName == metas[i].SubresourceName)
  424. {
  425. editorCurveData = metas[i].EditorData as EditorCurveData;
  426. break;
  427. }
  428. }
  429. }
  430. }
  431. if(editorCurveData == null)
  432. editorCurveData = new EditorCurveData();
  433. Action<NamedVector3Curve[], EditorVector3CurveTangents[], string> loadVector3Curve =
  434. (curves, tangents, subPath) =>
  435. {
  436. foreach (var curveEntry in curves)
  437. {
  438. TangentMode[] tangentsX = null;
  439. TangentMode[] tangentsY = null;
  440. TangentMode[] tangentsZ = null;
  441. foreach (var tangentEntry in tangents)
  442. {
  443. if (tangentEntry.name == curveEntry.Name)
  444. {
  445. tangentsX = tangentEntry.tangentsX;
  446. tangentsY = tangentEntry.tangentsY;
  447. tangentsZ = tangentEntry.tangentsZ;
  448. break;
  449. }
  450. }
  451. FieldCurves fieldCurves = new FieldCurves();
  452. fieldCurves.type = SerializableProperty.FieldType.Vector3;
  453. fieldCurves.curves = new EdAnimationCurve[3];
  454. fieldCurves.curves[0] = new EdAnimationCurve(curveEntry.X, tangentsX);
  455. fieldCurves.curves[1] = new EdAnimationCurve(curveEntry.Y, tangentsY);
  456. fieldCurves.curves[2] = new EdAnimationCurve(curveEntry.Z, tangentsZ);
  457. string curvePath = curveEntry.Name.TrimEnd('/') + subPath;
  458. guiFieldDisplay.AddField(curvePath);
  459. this.curves[curvePath] = fieldCurves;
  460. }
  461. };
  462. loadVector3Curve(clipCurves.PositionCurves, editorCurveData.positionCurves, "/Position");
  463. loadVector3Curve(clipCurves.RotationCurves, editorCurveData.rotationCurves, "/Rotation");
  464. loadVector3Curve(clipCurves.ScaleCurves, editorCurveData.scaleCurves, "/Scale");
  465. // Find which individual float curves belong to the same field
  466. Dictionary<string, Tuple<int, bool>> suffixToIdxMapping = new Dictionary<string, Tuple<int, bool>>();
  467. suffixToIdxMapping[".x"] = Tuple.Create(0, true);
  468. suffixToIdxMapping[".y"] = Tuple.Create(1, true);
  469. suffixToIdxMapping[".z"] = Tuple.Create(2, true);
  470. suffixToIdxMapping[".w"] = Tuple.Create(3, true);
  471. suffixToIdxMapping[".r"] = Tuple.Create(0, false);
  472. suffixToIdxMapping[".g"] = Tuple.Create(1, false);
  473. suffixToIdxMapping[".b"] = Tuple.Create(2, false);
  474. suffixToIdxMapping[".a"] = Tuple.Create(3, false);
  475. Dictionary<string, Tuple<int, int, bool>[]> floatCurveMapping = new Dictionary<string, Tuple<int, int, bool>[]>();
  476. {
  477. int curveIdx = 0;
  478. foreach (var curveEntry in clipCurves.FloatCurves)
  479. {
  480. string path = curveEntry.Name;
  481. string pathNoSuffix = null;
  482. string pathSuffix;
  483. if (path.Length >= 2)
  484. {
  485. pathSuffix = path.Substring(path.Length - 2, 2);
  486. pathNoSuffix = path.Substring(0, path.Length - 2);
  487. }
  488. else
  489. pathSuffix = "";
  490. int tangentIdx = -1;
  491. int currentTangentIdx = 0;
  492. foreach (var tangentEntry in editorCurveData.floatCurves)
  493. {
  494. if (tangentEntry.name == curveEntry.Name)
  495. {
  496. tangentIdx = currentTangentIdx;
  497. break;
  498. }
  499. currentTangentIdx++;
  500. }
  501. Tuple<int, bool> suffixInfo;
  502. if (suffixToIdxMapping.TryGetValue(pathSuffix, out suffixInfo))
  503. {
  504. Tuple<int, int, bool>[] curveInfo;
  505. if (!floatCurveMapping.TryGetValue(pathNoSuffix, out curveInfo))
  506. curveInfo = new Tuple<int, int, bool>[4];
  507. curveInfo[suffixInfo.Item1] = Tuple.Create(curveIdx, tangentIdx, suffixInfo.Item2);
  508. floatCurveMapping[pathNoSuffix] = curveInfo;
  509. }
  510. else
  511. {
  512. Tuple<int, int, bool>[] curveInfo = new Tuple<int, int, bool>[4];
  513. curveInfo[0] = Tuple.Create(curveIdx, tangentIdx, suffixInfo.Item2);
  514. floatCurveMapping[path] = curveInfo;
  515. }
  516. curveIdx++;
  517. }
  518. }
  519. foreach (var KVP in floatCurveMapping)
  520. {
  521. int numCurves = 0;
  522. for (int i = 0; i < 4; i++)
  523. {
  524. if (KVP.Value[i] == null)
  525. continue;
  526. numCurves++;
  527. }
  528. if (numCurves == 0)
  529. continue; // Invalid curve
  530. FieldCurves fieldCurves = new FieldCurves();
  531. // Deduce type (note that all single value types are assumed to be float even if their source type is int or bool)
  532. if (numCurves == 1)
  533. fieldCurves.type = SerializableProperty.FieldType.Float;
  534. else if (numCurves == 2)
  535. fieldCurves.type = SerializableProperty.FieldType.Vector2;
  536. else if (numCurves == 3)
  537. fieldCurves.type = SerializableProperty.FieldType.Vector3;
  538. else // 4 curves
  539. {
  540. bool isVector = KVP.Value[0].Item3;
  541. if (isVector)
  542. fieldCurves.type = SerializableProperty.FieldType.Vector4;
  543. else
  544. fieldCurves.type = SerializableProperty.FieldType.Color;
  545. }
  546. fieldCurves.curves = new EdAnimationCurve[numCurves];
  547. for (int i = 0; i < numCurves; i++)
  548. {
  549. int curveIdx = KVP.Value[i].Item1;
  550. int tangentIdx = KVP.Value[i].Item2;
  551. TangentMode[] tangents = null;
  552. if (tangentIdx != -1)
  553. tangents = editorCurveData.floatCurves[tangentIdx].tangents;
  554. fieldCurves.curves[i] = new EdAnimationCurve(clipCurves.FloatCurves[curveIdx].Curve, tangents);
  555. }
  556. string curvePath = KVP.Key;
  557. guiFieldDisplay.AddField(curvePath);
  558. curves[curvePath] = fieldCurves;
  559. }
  560. // Add events
  561. events.AddRange(clip.Events);
  562. }
  563. private void SaveToClip(AnimationClip clip, string saveToPath)
  564. {
  565. if (!clipIsImported)
  566. {
  567. List<NamedVector3Curve> positionCurves = new List<NamedVector3Curve>();
  568. List<NamedVector3Curve> rotationCurves = new List<NamedVector3Curve>();
  569. List<NamedVector3Curve> scaleCurves = new List<NamedVector3Curve>();
  570. List<NamedFloatCurve> floatCurves = new List<NamedFloatCurve>();
  571. List<EditorVector3CurveTangents> positionTangents = new List<EditorVector3CurveTangents>();
  572. List<EditorVector3CurveTangents> rotationTangents = new List<EditorVector3CurveTangents>();
  573. List<EditorVector3CurveTangents> scaleTangents = new List<EditorVector3CurveTangents>();
  574. List<EditorFloatCurveTangents> floatTangents = new List<EditorFloatCurveTangents>();
  575. foreach (var kvp in curves)
  576. {
  577. string[] pathEntries = kvp.Key.Split('/');
  578. if (pathEntries.Length == 0)
  579. continue;
  580. string lastEntry = pathEntries[pathEntries.Length - 1];
  581. if (lastEntry == "Position" || lastEntry == "Rotation" || lastEntry == "Scale")
  582. {
  583. StringBuilder sb = new StringBuilder();
  584. for (int i = 0; i < pathEntries.Length - 2; i++)
  585. sb.Append(pathEntries[i] + "/");
  586. if (pathEntries.Length > 1)
  587. sb.Append(pathEntries[pathEntries.Length - 2]);
  588. string curvePath = sb.ToString();
  589. NamedVector3Curve curve = new NamedVector3Curve(curvePath,
  590. new AnimationCurve(kvp.Value.curves[0].KeyFrames),
  591. new AnimationCurve(kvp.Value.curves[1].KeyFrames),
  592. new AnimationCurve(kvp.Value.curves[2].KeyFrames));
  593. EditorVector3CurveTangents tangents = new EditorVector3CurveTangents();
  594. tangents.name = curvePath;
  595. tangents.tangentsX = kvp.Value.curves[0].TangentModes;
  596. tangents.tangentsY = kvp.Value.curves[1].TangentModes;
  597. tangents.tangentsZ = kvp.Value.curves[2].TangentModes;
  598. if (lastEntry == "Position")
  599. {
  600. positionCurves.Add(curve);
  601. positionTangents.Add(tangents);
  602. }
  603. else if (lastEntry == "Rotation")
  604. {
  605. rotationCurves.Add(curve);
  606. rotationTangents.Add(tangents);
  607. }
  608. else if (lastEntry == "Scale")
  609. {
  610. scaleCurves.Add(curve);
  611. scaleTangents.Add(tangents);
  612. }
  613. }
  614. else
  615. {
  616. Action<int, string> addCurve = (idx, subPath) =>
  617. {
  618. string path = kvp.Key + subPath;
  619. NamedFloatCurve curve = new NamedFloatCurve(path,
  620. new AnimationCurve(kvp.Value.curves[idx].KeyFrames));
  621. EditorFloatCurveTangents tangents = new EditorFloatCurveTangents();
  622. tangents.name = path;
  623. tangents.tangents = kvp.Value.curves[idx].TangentModes;
  624. floatCurves.Add(curve);
  625. floatTangents.Add(tangents);
  626. };
  627. switch (kvp.Value.type)
  628. {
  629. case SerializableProperty.FieldType.Vector2:
  630. addCurve(0, ".x");
  631. addCurve(1, ".y");
  632. break;
  633. case SerializableProperty.FieldType.Vector3:
  634. addCurve(0, ".x");
  635. addCurve(1, ".y");
  636. addCurve(2, ".z");
  637. break;
  638. case SerializableProperty.FieldType.Vector4:
  639. addCurve(0, ".x");
  640. addCurve(1, ".y");
  641. addCurve(2, ".z");
  642. addCurve(3, ".w");
  643. break;
  644. case SerializableProperty.FieldType.Color:
  645. addCurve(0, ".r");
  646. addCurve(1, ".g");
  647. addCurve(2, ".b");
  648. addCurve(3, ".a");
  649. break;
  650. case SerializableProperty.FieldType.Bool:
  651. case SerializableProperty.FieldType.Int:
  652. case SerializableProperty.FieldType.Float:
  653. addCurve(0, "");
  654. break;
  655. }
  656. }
  657. }
  658. AnimationCurves newClipCurves = new AnimationCurves();
  659. newClipCurves.PositionCurves = positionCurves.ToArray();
  660. newClipCurves.RotationCurves = rotationCurves.ToArray();
  661. newClipCurves.ScaleCurves = scaleCurves.ToArray();
  662. newClipCurves.FloatCurves = floatCurves.ToArray();
  663. clip.Curves = newClipCurves;
  664. clip.Events = events.ToArray();
  665. string resourcePath = ProjectLibrary.GetPath(clip);
  666. if (string.IsNullOrEmpty(resourcePath))
  667. ProjectLibrary.Create(clip, saveToPath);
  668. else
  669. ProjectLibrary.Save(clip);
  670. // Save tangents for editor only use
  671. LibraryEntry entry = ProjectLibrary.GetEntry(resourcePath);
  672. string clipName = PathEx.GetTail(resourcePath);
  673. if (entry != null && entry.Type == LibraryEntryType.File)
  674. {
  675. FileEntry fileEntry = (FileEntry)entry;
  676. ResourceMeta[] metas = fileEntry.ResourceMetas;
  677. for (int i = 0; i < metas.Length; i++)
  678. {
  679. if (clipName == metas[i].SubresourceName)
  680. {
  681. EditorCurveData newCurveData = new EditorCurveData();
  682. newCurveData.positionCurves = positionTangents.ToArray();
  683. newCurveData.rotationCurves = rotationTangents.ToArray();
  684. newCurveData.scaleCurves = scaleTangents.ToArray();
  685. newCurveData.floatCurves = floatTangents.ToArray();
  686. ProjectLibrary.SetEditorData(resourcePath, newCurveData);
  687. break;
  688. }
  689. }
  690. }
  691. }
  692. else
  693. {
  694. string resourcePath = ProjectLibrary.GetPath(clip);
  695. LibraryEntry entry = ProjectLibrary.GetEntry(resourcePath);
  696. if (entry != null && entry.Type == LibraryEntryType.File)
  697. {
  698. FileEntry fileEntry = (FileEntry) entry;
  699. MeshImportOptions meshImportOptions = (MeshImportOptions)fileEntry.Options;
  700. string clipName = PathEx.GetTail(resourcePath);
  701. List<ImportedAnimationEvents> newEvents = new List<ImportedAnimationEvents>();
  702. newEvents.AddRange(meshImportOptions.AnimationEvents);
  703. bool isExisting = false;
  704. for (int i = 0; i < newEvents.Count; i++)
  705. {
  706. if (newEvents[i].name == clipName)
  707. {
  708. newEvents[i].events = events.ToArray();
  709. isExisting = true;
  710. break;
  711. }
  712. }
  713. if (!isExisting)
  714. {
  715. ImportedAnimationEvents newEntry = new ImportedAnimationEvents();
  716. newEntry.name = clipName;
  717. newEntry.events = events.ToArray();
  718. newEvents.Add(newEntry);
  719. }
  720. meshImportOptions.AnimationEvents = newEvents.ToArray();
  721. ProjectLibrary.Reimport(resourcePath, meshImportOptions, true);
  722. }
  723. }
  724. }
  725. private bool IsClipImported(AnimationClip clip)
  726. {
  727. string resourcePath = ProjectLibrary.GetPath(clip);
  728. return ProjectLibrary.IsSubresource(resourcePath);
  729. }
  730. #endregion
  731. #region Curve display
  732. private int currentFrameIdx;
  733. private int fps = 1;
  734. internal int FPS
  735. {
  736. get { return fps; }
  737. set { guiCurveEditor.SetFPS(value); fps = MathEx.Max(value, 1); }
  738. }
  739. private void SetCurrentFrame(int frameIdx)
  740. {
  741. currentFrameIdx = Math.Max(0, frameIdx);
  742. frameInputField.Value = currentFrameIdx;
  743. guiCurveEditor.SetMarkedFrame(currentFrameIdx);
  744. float time = guiCurveEditor.GetTimeForFrame(currentFrameIdx);
  745. List<GUIAnimFieldPathValue> values = new List<GUIAnimFieldPathValue>();
  746. foreach (var kvp in curves)
  747. {
  748. SerializableProperty property = GUIAnimFieldDisplay.FindProperty(selectedSO, kvp.Key);
  749. if (property != null)
  750. {
  751. GUIAnimFieldPathValue fieldValue = new GUIAnimFieldPathValue();
  752. fieldValue.path = kvp.Key;
  753. switch (kvp.Value.type)
  754. {
  755. case SerializableProperty.FieldType.Vector2:
  756. {
  757. Vector2 value = new Vector2();
  758. for (int i = 0; i < 2; i++)
  759. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  760. fieldValue.value = value;
  761. }
  762. break;
  763. case SerializableProperty.FieldType.Vector3:
  764. {
  765. Vector3 value = new Vector3();
  766. for (int i = 0; i < 3; i++)
  767. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  768. fieldValue.value = value;
  769. }
  770. break;
  771. case SerializableProperty.FieldType.Vector4:
  772. {
  773. Vector4 value = new Vector4();
  774. for (int i = 0; i < 4; i++)
  775. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  776. fieldValue.value = value;
  777. }
  778. break;
  779. case SerializableProperty.FieldType.Color:
  780. {
  781. Color value = new Color();
  782. for (int i = 0; i < 4; i++)
  783. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  784. fieldValue.value = value;
  785. }
  786. break;
  787. case SerializableProperty.FieldType.Bool:
  788. case SerializableProperty.FieldType.Int:
  789. case SerializableProperty.FieldType.Float:
  790. fieldValue.value = kvp.Value.curves[0].Evaluate(time, false); ;
  791. break;
  792. }
  793. values.Add(fieldValue);
  794. }
  795. }
  796. guiFieldDisplay.SetDisplayValues(values.ToArray());
  797. }
  798. private Vector2 GetOptimalRange()
  799. {
  800. List<EdAnimationCurve> displayedCurves = new List<EdAnimationCurve>();
  801. for (int i = 0; i < selectedFields.Count; i++)
  802. {
  803. EdAnimationCurve curve;
  804. if (TryGetCurve(selectedFields[i], out curve))
  805. displayedCurves.Add(curve);
  806. }
  807. float xRange;
  808. float yRange;
  809. CalculateRange(displayedCurves, out xRange, out yRange);
  810. // Add padding to y range
  811. yRange *= 1.05f;
  812. // Don't allow zero range
  813. if (xRange == 0.0f)
  814. xRange = 60.0f;
  815. if (yRange == 0.0f)
  816. yRange = 10.0f;
  817. return new Vector2(xRange, yRange);
  818. }
  819. private void UpdateDisplayedCurves()
  820. {
  821. List<EdAnimationCurve> curvesToDisplay = new List<EdAnimationCurve>();
  822. for (int i = 0; i < selectedFields.Count; i++)
  823. {
  824. EdAnimationCurve curve;
  825. if (TryGetCurve(selectedFields[i], out curve))
  826. curvesToDisplay.Add(curve);
  827. }
  828. guiCurveEditor.SetCurves(curvesToDisplay.ToArray());
  829. Vector2 newRange = GetOptimalRange();
  830. // Don't reduce visible range
  831. newRange.x = Math.Max(newRange.x, guiCurveEditor.Range.x);
  832. newRange.y = Math.Max(newRange.y, guiCurveEditor.Range.y);
  833. guiCurveEditor.Range = newRange;
  834. UpdateScrollBarSize();
  835. }
  836. #endregion
  837. #region Field display
  838. private List<string> selectedFields = new List<string>();
  839. private void AddNewField(string path, SerializableProperty.FieldType type)
  840. {
  841. guiFieldDisplay.AddField(path);
  842. switch (type)
  843. {
  844. case SerializableProperty.FieldType.Vector4:
  845. {
  846. FieldCurves fieldCurves = new FieldCurves();
  847. fieldCurves.type = type;
  848. fieldCurves.curves = new EdAnimationCurve[4];
  849. string[] subPaths = { ".x", ".y", ".z", ".w" };
  850. for (int i = 0; i < subPaths.Length; i++)
  851. {
  852. string subFieldPath = path + subPaths[i];
  853. fieldCurves.curves[i] = new EdAnimationCurve();
  854. selectedFields.Add(subFieldPath);
  855. }
  856. curves[path] = fieldCurves;
  857. }
  858. break;
  859. case SerializableProperty.FieldType.Vector3:
  860. {
  861. FieldCurves fieldCurves = new FieldCurves();
  862. fieldCurves.type = type;
  863. fieldCurves.curves = new EdAnimationCurve[3];
  864. string[] subPaths = { ".x", ".y", ".z" };
  865. for (int i = 0; i < subPaths.Length; i++)
  866. {
  867. string subFieldPath = path + subPaths[i];
  868. fieldCurves.curves[i] = new EdAnimationCurve();
  869. selectedFields.Add(subFieldPath);
  870. }
  871. curves[path] = fieldCurves;
  872. }
  873. break;
  874. case SerializableProperty.FieldType.Vector2:
  875. {
  876. FieldCurves fieldCurves = new FieldCurves();
  877. fieldCurves.type = type;
  878. fieldCurves.curves = new EdAnimationCurve[2];
  879. string[] subPaths = { ".x", ".y" };
  880. for (int i = 0; i < subPaths.Length; i++)
  881. {
  882. string subFieldPath = path + subPaths[i];
  883. fieldCurves.curves[i] = new EdAnimationCurve();
  884. selectedFields.Add(subFieldPath);
  885. }
  886. curves[path] = fieldCurves;
  887. }
  888. break;
  889. case SerializableProperty.FieldType.Color:
  890. {
  891. FieldCurves fieldCurves = new FieldCurves();
  892. fieldCurves.type = type;
  893. fieldCurves.curves = new EdAnimationCurve[4];
  894. string[] subPaths = { ".r", ".g", ".b", ".a" };
  895. for (int i = 0; i < subPaths.Length; i++)
  896. {
  897. string subFieldPath = path + subPaths[i];
  898. fieldCurves.curves[i] = new EdAnimationCurve();
  899. selectedFields.Add(subFieldPath);
  900. }
  901. curves[path] = fieldCurves;
  902. }
  903. break;
  904. default: // Primitive type
  905. {
  906. FieldCurves fieldCurves = new FieldCurves();
  907. fieldCurves.type = type;
  908. fieldCurves.curves = new EdAnimationCurve[1];
  909. fieldCurves.curves[0] = new EdAnimationCurve();
  910. selectedFields.Add(path);
  911. curves[path] = fieldCurves;
  912. }
  913. break;
  914. }
  915. UpdateDisplayedCurves();
  916. }
  917. private void SelectField(string path, bool additive)
  918. {
  919. if (!additive)
  920. selectedFields.Clear();
  921. if (!string.IsNullOrEmpty(path))
  922. {
  923. selectedFields.RemoveAll(x => { return x == path || IsPathParent(x, path); });
  924. selectedFields.Add(path);
  925. }
  926. guiFieldDisplay.SetSelection(selectedFields.ToArray());
  927. UpdateDisplayedCurves();
  928. }
  929. private void RemoveSelectedFields()
  930. {
  931. for (int i = 0; i < selectedFields.Count; i++)
  932. {
  933. selectedFields.Remove(selectedFields[i]);
  934. curves.Remove(GetSubPathParent(selectedFields[i]));
  935. }
  936. List<string> existingFields = new List<string>();
  937. foreach (var KVP in curves)
  938. existingFields.Add(KVP.Key);
  939. guiFieldDisplay.SetFields(existingFields.ToArray());
  940. selectedFields.Clear();
  941. UpdateDisplayedCurves();
  942. }
  943. #endregion
  944. #region Helpers
  945. private Vector2I GetCurveEditorSize()
  946. {
  947. Vector2I output = new Vector2I();
  948. output.x = Math.Max(0, Width - FIELD_DISPLAY_WIDTH - scrollBarWidth);
  949. output.y = Math.Max(0, Height - buttonLayoutHeight - scrollBarHeight);
  950. return output;
  951. }
  952. private static void CalculateRange(List<EdAnimationCurve> curves, out float xRange, out float yRange)
  953. {
  954. xRange = 0.0f;
  955. yRange = 0.0f;
  956. foreach (var curve in curves)
  957. {
  958. KeyFrame[] keyframes = curve.KeyFrames;
  959. foreach (var key in keyframes)
  960. {
  961. xRange = Math.Max(xRange, key.time);
  962. yRange = Math.Max(yRange, Math.Abs(key.value));
  963. }
  964. }
  965. }
  966. private bool TryGetCurve(string path, out EdAnimationCurve curve)
  967. {
  968. int index = path.LastIndexOf(".");
  969. string parentPath;
  970. string subPathSuffix = null;
  971. if (index == -1)
  972. {
  973. parentPath = path;
  974. }
  975. else
  976. {
  977. parentPath = path.Substring(0, index);
  978. subPathSuffix = path.Substring(index, path.Length - index);
  979. }
  980. FieldCurves fieldCurves;
  981. if (curves.TryGetValue(parentPath, out fieldCurves))
  982. {
  983. if (!string.IsNullOrEmpty(subPathSuffix))
  984. {
  985. if (subPathSuffix == ".x" || subPathSuffix == ".r")
  986. {
  987. curve = fieldCurves.curves[0];
  988. return true;
  989. }
  990. else if (subPathSuffix == ".y" || subPathSuffix == ".g")
  991. {
  992. curve = fieldCurves.curves[1];
  993. return true;
  994. }
  995. else if (subPathSuffix == ".z" || subPathSuffix == ".b")
  996. {
  997. curve = fieldCurves.curves[2];
  998. return true;
  999. }
  1000. else if (subPathSuffix == ".w" || subPathSuffix == ".a")
  1001. {
  1002. curve = fieldCurves.curves[3];
  1003. return true;
  1004. }
  1005. }
  1006. else
  1007. {
  1008. curve = fieldCurves.curves[0];
  1009. return true;
  1010. }
  1011. }
  1012. curve = null;
  1013. return false;
  1014. }
  1015. private bool IsPathParent(string child, string parent)
  1016. {
  1017. string[] childEntries = child.Split('/', '.');
  1018. string[] parentEntries = parent.Split('/', '.');
  1019. if (parentEntries.Length >= child.Length)
  1020. return false;
  1021. int compareLength = Math.Min(childEntries.Length, parentEntries.Length);
  1022. for (int i = 0; i < compareLength; i++)
  1023. {
  1024. if (childEntries[i] != parentEntries[i])
  1025. return false;
  1026. }
  1027. return true;
  1028. }
  1029. private string GetSubPathParent(string path)
  1030. {
  1031. int index = path.LastIndexOf(".");
  1032. if (index == -1)
  1033. return path;
  1034. return path.Substring(0, index);
  1035. }
  1036. #endregion
  1037. #region Input callbacks
  1038. private void OnPointerPressed(PointerEvent ev)
  1039. {
  1040. if (!isInitialized)
  1041. return;
  1042. guiCurveEditor.OnPointerPressed(ev);
  1043. if (ev.button == PointerButton.Middle)
  1044. {
  1045. Vector2I windowPos = ScreenToWindowPos(ev.ScreenPos);
  1046. Vector2 curvePos;
  1047. if (guiCurveEditor.WindowToCurveSpace(windowPos, out curvePos))
  1048. {
  1049. dragStartPos = windowPos;
  1050. isButtonHeld = true;
  1051. }
  1052. }
  1053. }
  1054. private void OnPointerMoved(PointerEvent ev)
  1055. {
  1056. if (!isInitialized)
  1057. return;
  1058. guiCurveEditor.OnPointerMoved(ev);
  1059. if (isButtonHeld)
  1060. {
  1061. Vector2I windowPos = ScreenToWindowPos(ev.ScreenPos);
  1062. int distance = Vector2I.Distance(dragStartPos, windowPos);
  1063. if (distance >= DRAG_START_DISTANCE)
  1064. {
  1065. isDragInProgress = true;
  1066. Cursor.Hide();
  1067. Rect2I clipRect;
  1068. clipRect.x = ev.ScreenPos.x - 2;
  1069. clipRect.y = ev.ScreenPos.y - 2;
  1070. clipRect.width = 4;
  1071. clipRect.height = 4;
  1072. Cursor.ClipToRect(clipRect);
  1073. }
  1074. }
  1075. }
  1076. private void OnPointerReleased(PointerEvent ev)
  1077. {
  1078. if (isDragInProgress)
  1079. {
  1080. Cursor.Show();
  1081. Cursor.ClipDisable();
  1082. }
  1083. isButtonHeld = false;
  1084. isDragInProgress = false;
  1085. if (!isInitialized)
  1086. return;
  1087. guiCurveEditor.OnPointerReleased(ev);
  1088. }
  1089. private void OnButtonUp(ButtonEvent ev)
  1090. {
  1091. if (!isInitialized)
  1092. return;
  1093. guiCurveEditor.OnButtonUp(ev);
  1094. }
  1095. #endregion
  1096. #region General callbacks
  1097. private void OnFieldAdded(string path, SerializableProperty.FieldType type)
  1098. {
  1099. AddNewField(path, type);
  1100. }
  1101. private void OnHorzScrollOrResize(float position, float size)
  1102. {
  1103. SetHorzScrollbarProperties(position, size);
  1104. }
  1105. private void OnVertScrollOrResize(float position, float size)
  1106. {
  1107. SetVertScrollbarProperties(position, size);
  1108. }
  1109. private void OnFieldSelected(string path)
  1110. {
  1111. bool additive = Input.IsButtonHeld(ButtonCode.LeftShift) || Input.IsButtonHeld(ButtonCode.RightShift);
  1112. SelectField(path, additive);
  1113. }
  1114. private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
  1115. {
  1116. RebuildGUI();
  1117. }
  1118. private void OnFrameSelected(int frameIdx)
  1119. {
  1120. SetCurrentFrame(frameIdx);
  1121. }
  1122. #endregion
  1123. }
  1124. /// <summary>
  1125. /// Drop down window that displays options used by the animation window.
  1126. /// </summary>
  1127. [DefaultSize(100, 50)]
  1128. internal class AnimationOptions : DropDownWindow
  1129. {
  1130. /// <summary>
  1131. /// Initializes the drop down window by creating the necessary GUI. Must be called after construction and before
  1132. /// use.
  1133. /// </summary>
  1134. /// <param name="parent">Animation window that this drop down window is a part of.</param>
  1135. internal void Initialize(AnimationWindow parent)
  1136. {
  1137. GUIIntField fpsField = new GUIIntField(new LocEdString("FPS"), 40);
  1138. fpsField.Value = parent.FPS;
  1139. fpsField.OnChanged += x => { parent.FPS = x; };
  1140. GUILayoutY vertLayout = GUI.AddLayoutY();
  1141. vertLayout.AddFlexibleSpace();
  1142. GUILayoutX contentLayout = vertLayout.AddLayoutX();
  1143. contentLayout.AddFlexibleSpace();
  1144. contentLayout.AddElement(fpsField);
  1145. contentLayout.AddFlexibleSpace();
  1146. vertLayout.AddFlexibleSpace();
  1147. }
  1148. }
  1149. /** @} */
  1150. }