AnimationWindow.cs 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. /** @addtogroup Windows
  9. * @{
  10. */
  11. /// <summary>
  12. /// Displays animation curve editor window.
  13. /// </summary>
  14. [DefaultSize(900, 500)]
  15. internal class AnimationWindow : EditorWindow
  16. {
  17. private const int FIELD_DISPLAY_WIDTH = 200;
  18. private const int DRAG_START_DISTANCE = 3;
  19. private const float DRAG_SCALE = 10.0f;
  20. private const float ZOOM_SCALE = 15.0f;
  21. private bool isInitialized;
  22. private SceneObject selectedSO;
  23. #region Overrides
  24. /// <summary>
  25. /// Opens the animation window.
  26. /// </summary>
  27. [MenuItem("Windows/Animation", ButtonModifier.CtrlAlt, ButtonCode.A, 6000)]
  28. private static void OpenGameWindow()
  29. {
  30. OpenWindow<AnimationWindow>();
  31. }
  32. /// <inheritdoc/>
  33. protected override LocString GetDisplayName()
  34. {
  35. return new LocEdString("Animation");
  36. }
  37. private void OnInitialize()
  38. {
  39. Selection.OnSelectionChanged += OnSelectionChanged;
  40. EditorInput.OnPointerPressed += OnPointerPressed;
  41. EditorInput.OnPointerMoved += OnPointerMoved;
  42. EditorInput.OnPointerReleased += OnPointerReleased;
  43. EditorInput.OnButtonUp += OnButtonUp;
  44. RebuildGUI();
  45. }
  46. private void OnEditorUpdate()
  47. {
  48. if (!isInitialized)
  49. return;
  50. HandleDragAndZoomInput();
  51. }
  52. private void OnDestroy()
  53. {
  54. Selection.OnSelectionChanged -= OnSelectionChanged;
  55. EditorInput.OnPointerPressed -= OnPointerPressed;
  56. EditorInput.OnPointerMoved -= OnPointerMoved;
  57. EditorInput.OnPointerReleased -= OnPointerReleased;
  58. EditorInput.OnButtonUp -= OnButtonUp;
  59. }
  60. protected override void WindowResized(int width, int height)
  61. {
  62. if (!isInitialized)
  63. return;
  64. ResizeGUI(width, height);
  65. }
  66. #endregion
  67. #region GUI
  68. private GUIButton playButton;
  69. private GUIButton recordButton;
  70. private GUIButton prevFrameButton;
  71. private GUIIntField frameInputField;
  72. private GUIButton nextFrameButton;
  73. private GUIButton addKeyframeButton;
  74. private GUIButton addEventButton;
  75. private GUIButton optionsButton;
  76. private GUIButton addPropertyBtn;
  77. private GUIButton delPropertyBtn;
  78. private GUILayout buttonLayout;
  79. private int buttonLayoutHeight;
  80. private int scrollBarWidth;
  81. private int scrollBarHeight;
  82. private GUIResizeableScrollBarH horzScrollBar;
  83. private GUIResizeableScrollBarV vertScrollBar;
  84. private GUIPanel editorPanel;
  85. private GUIAnimFieldDisplay guiFieldDisplay;
  86. private GUICurveEditor guiCurveEditor;
  87. private void RebuildGUI()
  88. {
  89. GUI.Clear();
  90. selectedFields.Clear();
  91. curves.Clear();
  92. isInitialized = false;
  93. selectedSO = Selection.SceneObject;
  94. if (selectedSO == null)
  95. {
  96. GUILabel warningLbl = new GUILabel(new LocEdString("Select an object to animate in the Hierarchy or Scene windows."));
  97. GUILayoutY vertLayout = GUI.AddLayoutY();
  98. vertLayout.AddFlexibleSpace();
  99. GUILayoutX horzLayout = vertLayout.AddLayoutX();
  100. vertLayout.AddFlexibleSpace();
  101. horzLayout.AddFlexibleSpace();
  102. horzLayout.AddElement(warningLbl);
  103. horzLayout.AddFlexibleSpace();
  104. return;
  105. }
  106. // TODO - Retrieve Animation & AnimationClip from the selected object, fill curves dictionary
  107. // - If not available, show a button to create new animation clip
  108. // Top button row
  109. GUIContent playIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.Play),
  110. new LocEdString("Play"));
  111. GUIContent recordIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.Record),
  112. new LocEdString("Record"));
  113. GUIContent prevFrameIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.FrameBack),
  114. new LocEdString("Previous frame"));
  115. GUIContent nextFrameIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.FrameForward),
  116. new LocEdString("Next frame"));
  117. GUIContent addKeyframeIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.AddKeyframe),
  118. new LocEdString("Add keyframe"));
  119. GUIContent addEventIcon = new GUIContent(EditorBuiltin.GetAnimationWindowIcon(AnimationWindowIcon.AddEvent),
  120. new LocEdString("Add event"));
  121. GUIContent optionsIcon = new GUIContent(EditorBuiltin.GetLibraryWindowIcon(LibraryWindowIcon.Options),
  122. new LocEdString("Options"));
  123. playButton = new GUIButton(playIcon);
  124. recordButton = new GUIButton(recordIcon);
  125. prevFrameButton = new GUIButton(prevFrameIcon);
  126. frameInputField = new GUIIntField();
  127. nextFrameButton = new GUIButton(nextFrameIcon);
  128. addKeyframeButton = new GUIButton(addKeyframeIcon);
  129. addEventButton = new GUIButton(addEventIcon);
  130. optionsButton = new GUIButton(optionsIcon);
  131. playButton.OnClick += () =>
  132. {
  133. // TODO
  134. // - Record current state of the scene object hierarchy
  135. // - Evaluate all curves manually and update them
  136. // - On end, restore original values of the scene object hierarchy
  137. };
  138. recordButton.OnClick += () =>
  139. {
  140. // TODO
  141. // - Every frame read back current values of all the current curve's properties and assign it to the current frame
  142. };
  143. prevFrameButton.OnClick += () =>
  144. {
  145. SetCurrentFrame(currentFrameIdx - 1);
  146. };
  147. frameInputField.OnChanged += SetCurrentFrame;
  148. nextFrameButton.OnClick += () =>
  149. {
  150. SetCurrentFrame(currentFrameIdx + 1);
  151. };
  152. addKeyframeButton.OnClick += () =>
  153. {
  154. guiCurveEditor.AddKeyFrameAtMarker();
  155. // TODO - Update local curves?
  156. };
  157. addEventButton.OnClick += () =>
  158. {
  159. // TODO - Add event
  160. };
  161. optionsButton.OnClick += () =>
  162. {
  163. Vector2I openPosition = ScreenToWindowPos(Input.PointerPosition);
  164. AnimationOptions dropDown = DropDownWindow.Open<AnimationOptions>(this, openPosition);
  165. dropDown.Initialize(this);
  166. };
  167. // Property buttons
  168. addPropertyBtn = new GUIButton(new LocEdString("Add property"));
  169. delPropertyBtn = new GUIButton(new LocEdString("Delete selected"));
  170. addPropertyBtn.OnClick += () =>
  171. {
  172. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  173. FieldSelectionWindow fieldSelection = DropDownWindow.Open<FieldSelectionWindow>(this, windowPos);
  174. fieldSelection.OnFieldSelected += OnFieldAdded;
  175. };
  176. delPropertyBtn.OnClick += () =>
  177. {
  178. LocEdString title = new LocEdString("Warning");
  179. LocEdString message = new LocEdString("Are you sure you want to remove all selected fields?");
  180. DialogBox.Open(title, message, DialogBox.Type.YesNo, x =>
  181. {
  182. if (x == DialogBox.ResultType.Yes)
  183. {
  184. RemoveSelectedFields();
  185. }
  186. });
  187. };
  188. GUILayout mainLayout = GUI.AddLayoutY();
  189. buttonLayout = mainLayout.AddLayoutX();
  190. buttonLayout.AddSpace(5);
  191. buttonLayout.AddElement(playButton);
  192. buttonLayout.AddElement(recordButton);
  193. buttonLayout.AddSpace(5);
  194. buttonLayout.AddElement(prevFrameButton);
  195. buttonLayout.AddElement(frameInputField);
  196. buttonLayout.AddElement(nextFrameButton);
  197. buttonLayout.AddSpace(5);
  198. buttonLayout.AddElement(addKeyframeButton);
  199. buttonLayout.AddElement(addEventButton);
  200. buttonLayout.AddSpace(5);
  201. buttonLayout.AddElement(optionsButton);
  202. buttonLayout.AddFlexibleSpace();
  203. buttonLayoutHeight = playButton.Bounds.height;
  204. GUILayout contentLayout = mainLayout.AddLayoutX();
  205. GUILayout fieldDisplayLayout = contentLayout.AddLayoutY(GUIOption.FixedWidth(FIELD_DISPLAY_WIDTH));
  206. guiFieldDisplay = new GUIAnimFieldDisplay(fieldDisplayLayout, FIELD_DISPLAY_WIDTH,
  207. Height - buttonLayoutHeight * 2, selectedSO);
  208. guiFieldDisplay.OnEntrySelected += OnFieldSelected;
  209. GUILayout bottomButtonLayout = fieldDisplayLayout.AddLayoutX();
  210. bottomButtonLayout.AddElement(addPropertyBtn);
  211. bottomButtonLayout.AddElement(delPropertyBtn);
  212. horzScrollBar = new GUIResizeableScrollBarH();
  213. horzScrollBar.OnScrollOrResize += OnHorzScrollOrResize;
  214. vertScrollBar = new GUIResizeableScrollBarV();
  215. vertScrollBar.OnScrollOrResize += OnVertScrollOrResize;
  216. GUILayout curveLayout = contentLayout.AddLayoutY();
  217. GUILayout curveLayoutHorz = curveLayout.AddLayoutX();
  218. GUILayout horzScrollBarLayout = curveLayout.AddLayoutX();
  219. horzScrollBarLayout.AddElement(horzScrollBar);
  220. horzScrollBarLayout.AddFlexibleSpace();
  221. editorPanel = curveLayoutHorz.AddPanel();
  222. curveLayoutHorz.AddElement(vertScrollBar);
  223. curveLayoutHorz.AddFlexibleSpace();
  224. scrollBarHeight = horzScrollBar.Bounds.height;
  225. scrollBarWidth = vertScrollBar.Bounds.width;
  226. Vector2I curveEditorSize = GetCurveEditorSize();
  227. guiCurveEditor = new GUICurveEditor(this, editorPanel, curveEditorSize.x, curveEditorSize.y);
  228. guiCurveEditor.OnFrameSelected += OnFrameSelected;
  229. guiCurveEditor.Redraw();
  230. horzScrollBar.SetWidth(curveEditorSize.x);
  231. vertScrollBar.SetHeight(curveEditorSize.y);
  232. SetCurrentFrame(currentFrameIdx);
  233. UpdateScrollBarSize();
  234. visibleOffset = new Vector2(0.0f, 0.0f);
  235. isInitialized = true;
  236. }
  237. private void ResizeGUI(int width, int height)
  238. {
  239. guiFieldDisplay.SetSize(FIELD_DISPLAY_WIDTH, height - buttonLayoutHeight * 2);
  240. Vector2I curveEditorSize = GetCurveEditorSize();
  241. guiCurveEditor.SetSize(curveEditorSize.x, curveEditorSize.y);
  242. guiCurveEditor.Redraw();
  243. horzScrollBar.SetWidth(curveEditorSize.x);
  244. vertScrollBar.SetHeight(curveEditorSize.y);
  245. UpdateScrollBarSize();
  246. UpdateScrollBarPosition();
  247. }
  248. #endregion
  249. #region Scroll, drag, zoom
  250. private Vector2I dragStartPos;
  251. private bool isButtonHeld;
  252. private bool isDragInProgress;
  253. private Vector2 minimalRange;
  254. private Vector2 visibleOffset;
  255. private void HandleDragAndZoomInput()
  256. {
  257. // Handle middle mouse dragging
  258. if (isDragInProgress)
  259. {
  260. float dragX = Input.GetAxisValue(InputAxis.MouseX) * DRAG_SCALE;
  261. float dragY = Input.GetAxisValue(InputAxis.MouseY) * DRAG_SCALE;
  262. Vector2 totalRange = new Vector2(guiCurveEditor.XRange, guiCurveEditor.YRange);
  263. Vector2 visibleRange = GetVisibleRange();
  264. Vector2 offset = visibleOffset;
  265. if (dragX > 0.0f)
  266. {
  267. offset.x += dragX;
  268. float visibleRight = offset.x + visibleRange.x;
  269. if (visibleRight > totalRange.x)
  270. totalRange.x = visibleRight;
  271. }
  272. else
  273. {
  274. float actualDragX = offset.x - Math.Max(0.0f, offset.x + dragX);
  275. offset.x -= actualDragX;
  276. float visibleRight = offset.x + visibleRange.x;
  277. totalRange.x = Math.Max(minimalRange.x, visibleRight);
  278. }
  279. if (dragY > 0.0f)
  280. {
  281. offset.y += dragY;
  282. float visibleTop = offset.y + visibleRange.y;
  283. if (visibleTop > totalRange.y)
  284. totalRange.y = visibleTop;
  285. }
  286. else
  287. {
  288. offset.y += dragY;
  289. float visibleYMax = Math.Abs(offset.y) + visibleRange.y;
  290. totalRange.y = Math.Max(minimalRange.y, visibleYMax);
  291. }
  292. SetTotalRange(totalRange.x, totalRange.y);
  293. SetVisibleOffset(offset);
  294. UpdateScrollBarPosition();
  295. }
  296. // Handle zoom in/out
  297. float scroll = Input.GetAxisValue(InputAxis.MouseZ);
  298. if (scroll != 0.0f)
  299. {
  300. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  301. Vector2 curvePos;
  302. if (guiCurveEditor.WindowToCurveSpace(windowPos, out curvePos))
  303. {
  304. float zoom = scroll * ZOOM_SCALE;
  305. Zoom(curvePos, zoom);
  306. }
  307. }
  308. }
  309. private void SetVertScrollbarProperties(float position, float size)
  310. {
  311. Vector2 visibleRange = GetVisibleRange();
  312. float scrollableRange = guiCurveEditor.YRange - visibleRange.y;
  313. Vector2 offset = visibleOffset;
  314. offset.y = scrollableRange * position;
  315. SetVisibleOffset(offset);
  316. int height = (int)(guiCurveEditor.Height / size);
  317. guiCurveEditor.SetSize(guiCurveEditor.Width, height);
  318. }
  319. private void SetHorzScrollbarProperties(float position, float size)
  320. {
  321. Vector2 visibleRange = GetVisibleRange();
  322. float scrollableRange = guiCurveEditor.XRange - visibleRange.x;
  323. Vector2 offset = visibleOffset;
  324. offset.x = scrollableRange * position;
  325. SetVisibleOffset(offset);
  326. int width = (int)(guiCurveEditor.Width / size);
  327. guiCurveEditor.SetSize(width, guiCurveEditor.Height);
  328. }
  329. private Vector2 GetVisibleRange()
  330. {
  331. float unitsPerXPixel = guiCurveEditor.XRange / guiCurveEditor.Width;
  332. float unitsPerYPixel = guiCurveEditor.YRange / guiCurveEditor.Height;
  333. Vector2I visibleSize = GetCurveEditorSize();
  334. return new Vector2(unitsPerXPixel * visibleSize.x, unitsPerYPixel * visibleSize.y);
  335. }
  336. private void SetVisibleOffset(Vector2 offset)
  337. {
  338. visibleOffset = offset;
  339. float pixelsPerXUnit = guiCurveEditor.Width / guiCurveEditor.XRange;
  340. float pixelsPerYUnit = guiCurveEditor.Height / (guiCurveEditor.YRange * 2.0f);
  341. int x = (int)(pixelsPerXUnit * visibleOffset.x);
  342. int y = (int)(pixelsPerYUnit * visibleOffset.y);
  343. guiCurveEditor.SetPosition(x, y);
  344. }
  345. // Increases range without zooming in (increasing width/height accordingly)
  346. private void SetTotalRange(float x, float y)
  347. {
  348. float pixelsPerXUnit = guiCurveEditor.Width / guiCurveEditor.XRange;
  349. float pixelsPerYUnit = guiCurveEditor.Height / (guiCurveEditor.YRange * 2.0f);
  350. int width = (int)(pixelsPerXUnit * x);
  351. int height = (int)(pixelsPerYUnit * y);
  352. guiCurveEditor.SetRange(x, y);
  353. guiCurveEditor.SetSize(width, height);
  354. UpdateScrollBarSize();
  355. }
  356. private void UpdateScrollBarSize()
  357. {
  358. Vector2 visibleRange = GetVisibleRange();
  359. Vector2 totalRange = new Vector2(guiCurveEditor.XRange, guiCurveEditor.YRange);
  360. horzScrollBar.HandleSize = visibleRange.x / totalRange.x;
  361. vertScrollBar.HandleSize = visibleRange.y / totalRange.y;
  362. }
  363. private void UpdateScrollBarPosition()
  364. {
  365. Vector2 visibleRange = GetVisibleRange();
  366. Vector2 totalRange = new Vector2(guiCurveEditor.XRange, guiCurveEditor.YRange);
  367. Vector2 scrollableRange = totalRange - visibleRange;
  368. horzScrollBar.Position = visibleOffset.x / scrollableRange.x;
  369. vertScrollBar.Position = visibleOffset.y / scrollableRange.y;
  370. }
  371. private void Zoom(Vector2 curvePos, float amount)
  372. {
  373. Vector2 relativePos = curvePos - visibleOffset;
  374. Vector2 visibleRange = GetVisibleRange();
  375. relativePos.x /= visibleRange.x;
  376. relativePos.y /= visibleRange.y;
  377. relativePos.x = relativePos.x * 2.0f - 1.0f;
  378. relativePos.y = relativePos.y * 2.0f - 1.0f;
  379. Vector2 offset = visibleOffset;
  380. offset.x += relativePos.x * amount;
  381. offset.y += relativePos.y * amount;
  382. offset.x = Math.Max(0.0f, offset.x);
  383. SetVisibleOffset(offset);
  384. UpdateScrollBarPosition();
  385. int width = guiCurveEditor.Width + (int)amount;
  386. int height = guiCurveEditor.Height + (int)amount;
  387. // If we aren't at the minimum size, modify size and offset
  388. Vector2I visibleSize = GetCurveEditorSize();
  389. if (width > visibleSize.x || height > visibleSize.y)
  390. {
  391. width = Math.Max(width, visibleSize.x);
  392. height = Math.Max(height, visibleSize.y);
  393. guiCurveEditor.SetSize(width, height);
  394. UpdateScrollBarSize();
  395. }
  396. else // Otherwise start increasing range for zoom in
  397. {
  398. float unitsPerXPixel = guiCurveEditor.XRange / guiCurveEditor.Width;
  399. float unitsPerYPixel = guiCurveEditor.YRange / guiCurveEditor.Height;
  400. float rangeX = guiCurveEditor.XRange + unitsPerXPixel * amount;
  401. float rangeY = guiCurveEditor.YRange + unitsPerYPixel * amount;
  402. SetTotalRange(rangeX, rangeY);
  403. }
  404. }
  405. #endregion
  406. #region Curve display
  407. /// <summary>
  408. /// A set of animation curves for a field of a certain type.
  409. /// </summary>
  410. private struct FieldCurves
  411. {
  412. public SerializableProperty.FieldType type;
  413. public EdAnimationCurve[] curves;
  414. }
  415. private int currentFrameIdx;
  416. private int fps = 1;
  417. private Dictionary<string, FieldCurves> curves = new Dictionary<string, FieldCurves>();
  418. internal int FPS
  419. {
  420. get { return fps; }
  421. set { guiCurveEditor.SetFPS(value); fps = MathEx.Max(value, 1); }
  422. }
  423. private void SetCurrentFrame(int frameIdx)
  424. {
  425. currentFrameIdx = Math.Max(0, frameIdx);
  426. frameInputField.Value = currentFrameIdx;
  427. guiCurveEditor.SetMarkedFrame(currentFrameIdx);
  428. float time = guiCurveEditor.GetTimeForFrame(currentFrameIdx);
  429. List<GUIAnimFieldPathValue> values = new List<GUIAnimFieldPathValue>();
  430. foreach (var kvp in curves)
  431. {
  432. SerializableProperty property = GUIAnimFieldDisplay.FindProperty(selectedSO, kvp.Key);
  433. if (property != null)
  434. {
  435. GUIAnimFieldPathValue fieldValue = new GUIAnimFieldPathValue();
  436. fieldValue.path = kvp.Key;
  437. switch (kvp.Value.type)
  438. {
  439. case SerializableProperty.FieldType.Vector2:
  440. {
  441. Vector2 value = new Vector2();
  442. for (int i = 0; i < 2; i++)
  443. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  444. fieldValue.value = value;
  445. }
  446. break;
  447. case SerializableProperty.FieldType.Vector3:
  448. {
  449. Vector3 value = new Vector3();
  450. for (int i = 0; i < 3; i++)
  451. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  452. fieldValue.value = value;
  453. }
  454. break;
  455. case SerializableProperty.FieldType.Vector4:
  456. {
  457. Vector4 value = new Vector4();
  458. for (int i = 0; i < 4; i++)
  459. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  460. fieldValue.value = value;
  461. }
  462. break;
  463. case SerializableProperty.FieldType.Color:
  464. {
  465. Color value = new Color();
  466. for (int i = 0; i < 4; i++)
  467. value[i] = kvp.Value.curves[i].Evaluate(time, false);
  468. fieldValue.value = value;
  469. }
  470. break;
  471. case SerializableProperty.FieldType.Bool:
  472. case SerializableProperty.FieldType.Int:
  473. case SerializableProperty.FieldType.Float:
  474. fieldValue.value = kvp.Value.curves[0].Evaluate(time, false); ;
  475. break;
  476. }
  477. values.Add(fieldValue);
  478. }
  479. }
  480. guiFieldDisplay.SetDisplayValues(values.ToArray());
  481. }
  482. private void UpdateDisplayedCurves()
  483. {
  484. List<EdAnimationCurve> curvesToDisplay = new List<EdAnimationCurve>();
  485. for (int i = 0; i < selectedFields.Count; i++)
  486. {
  487. EdAnimationCurve curve;
  488. if (TryGetCurve(selectedFields[i], out curve))
  489. curvesToDisplay.Add(curve);
  490. }
  491. guiCurveEditor.SetCurves(curvesToDisplay.ToArray());
  492. float xRange;
  493. float yRange;
  494. CalculateRange(curvesToDisplay, out xRange, out yRange);
  495. // Don't allow zero range
  496. if (xRange == 0.0f)
  497. xRange = 60.0f;
  498. if (yRange == 0.0f)
  499. yRange = 10.0f;
  500. // Add padding to y range
  501. yRange *= 1.05f;
  502. // Don't reduce visible range
  503. minimalRange.x = Math.Max(xRange, minimalRange.x);
  504. minimalRange.y = Math.Max(yRange, minimalRange.y);
  505. xRange = Math.Max(xRange, guiCurveEditor.XRange);
  506. yRange = Math.Max(yRange, guiCurveEditor.YRange);
  507. guiCurveEditor.SetRange(xRange, yRange);
  508. UpdateScrollBarSize();
  509. }
  510. #endregion
  511. #region Field display
  512. private List<string> selectedFields = new List<string>();
  513. private void AddNewField(string path, SerializableProperty.FieldType type)
  514. {
  515. guiFieldDisplay.AddField(path);
  516. switch (type)
  517. {
  518. case SerializableProperty.FieldType.Vector4:
  519. {
  520. FieldCurves fieldCurves = new FieldCurves();
  521. fieldCurves.type = type;
  522. fieldCurves.curves = new EdAnimationCurve[4];
  523. string[] subPaths = { ".x", ".y", ".z", ".w" };
  524. for (int i = 0; i < subPaths.Length; i++)
  525. {
  526. string subFieldPath = path + subPaths[i];
  527. fieldCurves.curves[i] = new EdAnimationCurve();
  528. selectedFields.Add(subFieldPath);
  529. }
  530. curves[path] = fieldCurves;
  531. }
  532. break;
  533. case SerializableProperty.FieldType.Vector3:
  534. {
  535. FieldCurves fieldCurves = new FieldCurves();
  536. fieldCurves.type = type;
  537. fieldCurves.curves = new EdAnimationCurve[3];
  538. string[] subPaths = { ".x", ".y", ".z" };
  539. for (int i = 0; i < subPaths.Length; i++)
  540. {
  541. string subFieldPath = path + subPaths[i];
  542. fieldCurves.curves[i] = new EdAnimationCurve();
  543. selectedFields.Add(subFieldPath);
  544. }
  545. curves[path] = fieldCurves;
  546. }
  547. break;
  548. case SerializableProperty.FieldType.Vector2:
  549. {
  550. FieldCurves fieldCurves = new FieldCurves();
  551. fieldCurves.type = type;
  552. fieldCurves.curves = new EdAnimationCurve[2];
  553. string[] subPaths = { ".x", ".y" };
  554. for (int i = 0; i < subPaths.Length; i++)
  555. {
  556. string subFieldPath = path + subPaths[i];
  557. fieldCurves.curves[i] = new EdAnimationCurve();
  558. selectedFields.Add(subFieldPath);
  559. }
  560. curves[path] = fieldCurves;
  561. }
  562. break;
  563. case SerializableProperty.FieldType.Color:
  564. {
  565. FieldCurves fieldCurves = new FieldCurves();
  566. fieldCurves.type = type;
  567. fieldCurves.curves = new EdAnimationCurve[4];
  568. string[] subPaths = { ".r", ".g", ".b", ".a" };
  569. for (int i = 0; i < subPaths.Length; i++)
  570. {
  571. string subFieldPath = path + subPaths[i];
  572. fieldCurves.curves[i] = new EdAnimationCurve();
  573. selectedFields.Add(subFieldPath);
  574. }
  575. curves[path] = fieldCurves;
  576. }
  577. break;
  578. default: // Primitive type
  579. {
  580. FieldCurves fieldCurves = new FieldCurves();
  581. fieldCurves.type = type;
  582. fieldCurves.curves = new EdAnimationCurve[1];
  583. fieldCurves.curves[0] = new EdAnimationCurve();
  584. selectedFields.Add(path);
  585. curves[path] = fieldCurves;
  586. }
  587. break;
  588. }
  589. UpdateDisplayedCurves();
  590. }
  591. private void SelectField(string path, bool additive)
  592. {
  593. if (!additive)
  594. selectedFields.Clear();
  595. if (!string.IsNullOrEmpty(path))
  596. {
  597. selectedFields.RemoveAll(x => { return x == path || IsPathParent(x, path); });
  598. selectedFields.Add(path);
  599. }
  600. guiFieldDisplay.SetSelection(selectedFields.ToArray());
  601. UpdateDisplayedCurves();
  602. }
  603. private void RemoveSelectedFields()
  604. {
  605. for (int i = 0; i < selectedFields.Count; i++)
  606. {
  607. selectedFields.Remove(selectedFields[i]);
  608. curves.Remove(GetSubPathParent(selectedFields[i]));
  609. }
  610. List<string> existingFields = new List<string>();
  611. foreach (var KVP in curves)
  612. existingFields.Add(KVP.Key);
  613. guiFieldDisplay.SetFields(existingFields.ToArray());
  614. selectedFields.Clear();
  615. UpdateDisplayedCurves();
  616. }
  617. #endregion
  618. #region Helpers
  619. private Vector2I GetCurveEditorSize()
  620. {
  621. Vector2I output = new Vector2I();
  622. output.x = Math.Max(0, Width - FIELD_DISPLAY_WIDTH - scrollBarWidth);
  623. output.y = Math.Max(0, Height - buttonLayoutHeight - scrollBarHeight);
  624. return output;
  625. }
  626. private static void CalculateRange(List<EdAnimationCurve> curves, out float xRange, out float yRange)
  627. {
  628. xRange = 0.0f;
  629. yRange = 0.0f;
  630. foreach (var curve in curves)
  631. {
  632. KeyFrame[] keyframes = curve.KeyFrames;
  633. foreach (var key in keyframes)
  634. {
  635. xRange = Math.Max(xRange, key.time);
  636. yRange = Math.Max(yRange, Math.Abs(key.value));
  637. }
  638. }
  639. }
  640. private bool TryGetCurve(string path, out EdAnimationCurve curve)
  641. {
  642. int index = path.LastIndexOf(".");
  643. string parentPath;
  644. string subPathSuffix = null;
  645. if (index == -1)
  646. {
  647. parentPath = path;
  648. }
  649. else
  650. {
  651. parentPath = path.Substring(0, index);
  652. subPathSuffix = path.Substring(index, path.Length - index);
  653. }
  654. FieldCurves fieldCurves;
  655. if (curves.TryGetValue(parentPath, out fieldCurves))
  656. {
  657. if (!string.IsNullOrEmpty(subPathSuffix))
  658. {
  659. if (subPathSuffix == ".x" || subPathSuffix == ".r")
  660. {
  661. curve = fieldCurves.curves[0];
  662. return true;
  663. }
  664. else if (subPathSuffix == ".y" || subPathSuffix == ".g")
  665. {
  666. curve = fieldCurves.curves[1];
  667. return true;
  668. }
  669. else if (subPathSuffix == ".z" || subPathSuffix == ".b")
  670. {
  671. curve = fieldCurves.curves[2];
  672. return true;
  673. }
  674. else if (subPathSuffix == ".w" || subPathSuffix == ".a")
  675. {
  676. curve = fieldCurves.curves[3];
  677. return true;
  678. }
  679. }
  680. else
  681. {
  682. curve = fieldCurves.curves[0];
  683. return true;
  684. }
  685. }
  686. curve = null;
  687. return false;
  688. }
  689. private bool IsPathParent(string child, string parent)
  690. {
  691. string[] childEntries = child.Split('/', '.');
  692. string[] parentEntries = parent.Split('/', '.');
  693. if (parentEntries.Length >= child.Length)
  694. return false;
  695. int compareLength = Math.Min(childEntries.Length, parentEntries.Length);
  696. for (int i = 0; i < compareLength; i++)
  697. {
  698. if (childEntries[i] != parentEntries[i])
  699. return false;
  700. }
  701. return true;
  702. }
  703. private string GetSubPathParent(string path)
  704. {
  705. int index = path.LastIndexOf(".");
  706. if (index == -1)
  707. return path;
  708. return path.Substring(0, index);
  709. }
  710. #endregion
  711. #region Input callbacks
  712. private void OnPointerPressed(PointerEvent ev)
  713. {
  714. if (!isInitialized)
  715. return;
  716. guiCurveEditor.OnPointerPressed(ev);
  717. if (ev.button == PointerButton.Middle)
  718. {
  719. Vector2I windowPos = ScreenToWindowPos(ev.ScreenPos);
  720. Vector2 curvePos;
  721. if (guiCurveEditor.WindowToCurveSpace(windowPos, out curvePos))
  722. {
  723. dragStartPos = windowPos;
  724. isButtonHeld = true;
  725. }
  726. }
  727. }
  728. private void OnPointerMoved(PointerEvent ev)
  729. {
  730. if (!isInitialized)
  731. return;
  732. guiCurveEditor.OnPointerMoved(ev);
  733. if (isButtonHeld)
  734. {
  735. Vector2I windowPos = ScreenToWindowPos(ev.ScreenPos);
  736. int distance = Vector2I.Distance(dragStartPos, windowPos);
  737. if (distance >= DRAG_START_DISTANCE)
  738. {
  739. isDragInProgress = true;
  740. Cursor.Hide();
  741. Rect2I clipRect;
  742. clipRect.x = ev.ScreenPos.x - 2;
  743. clipRect.y = ev.ScreenPos.y - 2;
  744. clipRect.width = 4;
  745. clipRect.height = 4;
  746. Cursor.ClipToRect(clipRect);
  747. }
  748. }
  749. }
  750. private void OnPointerReleased(PointerEvent ev)
  751. {
  752. if (isDragInProgress)
  753. {
  754. Cursor.Show();
  755. Cursor.ClipDisable();
  756. }
  757. isButtonHeld = false;
  758. isDragInProgress = false;
  759. if (!isInitialized)
  760. return;
  761. guiCurveEditor.OnPointerReleased(ev);
  762. }
  763. private void OnButtonUp(ButtonEvent ev)
  764. {
  765. if (!isInitialized)
  766. return;
  767. guiCurveEditor.OnButtonUp(ev);
  768. }
  769. #endregion
  770. #region General callbacks
  771. private void OnFieldAdded(string path, SerializableProperty.FieldType type)
  772. {
  773. AddNewField(path, type);
  774. }
  775. private void OnHorzScrollOrResize(float position, float size)
  776. {
  777. SetHorzScrollbarProperties(position, size);
  778. }
  779. private void OnVertScrollOrResize(float position, float size)
  780. {
  781. SetVertScrollbarProperties(position, size);
  782. }
  783. private void OnFieldSelected(string path)
  784. {
  785. bool additive = Input.IsButtonHeld(ButtonCode.LeftShift) || Input.IsButtonHeld(ButtonCode.RightShift);
  786. SelectField(path, additive);
  787. }
  788. private void OnSelectionChanged(SceneObject[] sceneObjects, string[] resourcePaths)
  789. {
  790. RebuildGUI();
  791. }
  792. private void OnFrameSelected(int frameIdx)
  793. {
  794. SetCurrentFrame(frameIdx);
  795. }
  796. #endregion
  797. }
  798. /// <summary>
  799. /// Drop down window that displays options used by the animation window.
  800. /// </summary>
  801. [DefaultSize(100, 50)]
  802. internal class AnimationOptions : DropDownWindow
  803. {
  804. /// <summary>
  805. /// Initializes the drop down window by creating the necessary GUI. Must be called after construction and before
  806. /// use.
  807. /// </summary>
  808. /// <param name="parent">Animation window that this drop down window is a part of.</param>
  809. internal void Initialize(AnimationWindow parent)
  810. {
  811. GUIIntField fpsField = new GUIIntField(new LocEdString("FPS"), 40);
  812. fpsField.Value = parent.FPS;
  813. fpsField.OnChanged += x => { parent.FPS = x; };
  814. GUILayoutY vertLayout = GUI.AddLayoutY();
  815. vertLayout.AddFlexibleSpace();
  816. GUILayoutX contentLayout = vertLayout.AddLayoutX();
  817. contentLayout.AddFlexibleSpace();
  818. contentLayout.AddElement(fpsField);
  819. contentLayout.AddFlexibleSpace();
  820. vertLayout.AddFlexibleSpace();
  821. }
  822. }
  823. /** @} */
  824. }