GUICurveEditor.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609
  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 AnimationEditor
  9. * @{
  10. */
  11. internal class GUICurveEditor
  12. {
  13. class SelectedKeyframes
  14. {
  15. public int curveIdx;
  16. public List<int> keyIndices = new List<int>();
  17. }
  18. struct DraggedKeyframe
  19. {
  20. public DraggedKeyframe(int index, KeyFrame original)
  21. {
  22. this.index = index;
  23. this.original = original;
  24. }
  25. public int index;
  26. public KeyFrame original;
  27. }
  28. class DraggedKeyframes
  29. {
  30. public int curveIdx;
  31. public List<DraggedKeyframe> keys = new List<DraggedKeyframe>();
  32. }
  33. private const int TIMELINE_HEIGHT = 20;
  34. private const int SIDEBAR_WIDTH = 30;
  35. private const int DRAG_START_DISTANCE = 3;
  36. private EditorWindow window;
  37. private GUILayout gui;
  38. private GUIPanel drawingPanel;
  39. private GUIGraphTime guiTimeline;
  40. private GUICurveDrawing guiCurveDrawing;
  41. private GUIGraphValues guiSidebar;
  42. private ContextMenu blankContextMenu;
  43. private ContextMenu keyframeContextMenu;
  44. private Vector2I contextClickPosition;
  45. private EdAnimationCurve[] curves = new EdAnimationCurve[0];
  46. private float xRange;
  47. private float yRange;
  48. private int markedFrameIdx;
  49. private List<SelectedKeyframes> selectedKeyframes = new List<SelectedKeyframes>();
  50. private bool isPointerHeld;
  51. private bool isMousePressedOverKey;
  52. private bool isMousePressedOverTangent;
  53. private bool isDragInProgress;
  54. private List<DraggedKeyframes> draggedKeyframes = new List<DraggedKeyframes>();
  55. private TangentRef draggedTangent;
  56. private Vector2I dragStart;
  57. /// <summary>
  58. /// Returns the displayed range of the curve on the x axis (time).
  59. /// </summary>
  60. public float XRange
  61. {
  62. get { return xRange; }
  63. }
  64. /// <summary>
  65. /// Returns the displayed range of the curve on the y axis.
  66. /// </summary>
  67. public float YRange
  68. {
  69. get { return yRange; }
  70. }
  71. public GUICurveEditor(EditorWindow window, GUILayout gui, int width, int height)
  72. {
  73. this.window = window;
  74. this.gui = gui;
  75. blankContextMenu = new ContextMenu();
  76. blankContextMenu.AddItem("Add keyframe", AddKeyframeAtPosition);
  77. blankContextMenu.AddItem("Add event", AddEventAtPosition);
  78. keyframeContextMenu = new ContextMenu();
  79. keyframeContextMenu.AddItem("Delete", DeleteSelectedKeyframes);
  80. keyframeContextMenu.AddItem("Tangents/Auto", () => { ChangeSelectionTangentMode(TangentMode.Auto); });
  81. keyframeContextMenu.AddItem("Tangents/Free", () => { ChangeSelectionTangentMode(TangentMode.Free); });
  82. keyframeContextMenu.AddItem("Tangents/In/Auto", () => { ChangeSelectionTangentMode(TangentMode.InAuto); });
  83. keyframeContextMenu.AddItem("Tangents/In/Free", () => { ChangeSelectionTangentMode(TangentMode.InFree); });
  84. keyframeContextMenu.AddItem("Tangents/In/Linear", () => { ChangeSelectionTangentMode(TangentMode.InLinear); });
  85. keyframeContextMenu.AddItem("Tangents/In/Step", () => { ChangeSelectionTangentMode(TangentMode.InStep); });
  86. keyframeContextMenu.AddItem("Tangents/Out/Auto", () => { ChangeSelectionTangentMode(TangentMode.OutAuto); });
  87. keyframeContextMenu.AddItem("Tangents/Out/Free", () => { ChangeSelectionTangentMode(TangentMode.OutFree); });
  88. keyframeContextMenu.AddItem("Tangents/Out/Linear", () => { ChangeSelectionTangentMode(TangentMode.OutLinear); });
  89. keyframeContextMenu.AddItem("Tangents/Out/Step", () => { ChangeSelectionTangentMode(TangentMode.OutStep); });
  90. guiTimeline = new GUIGraphTime(gui, width, TIMELINE_HEIGHT);
  91. drawingPanel = gui.AddPanel();
  92. drawingPanel.SetPosition(0, TIMELINE_HEIGHT);
  93. guiCurveDrawing = new GUICurveDrawing(drawingPanel, width, height - TIMELINE_HEIGHT, curves);
  94. guiCurveDrawing.SetRange(60.0f, 20.0f);
  95. GUIPanel sidebarPanel = gui.AddPanel(-10);
  96. sidebarPanel.SetPosition(0, TIMELINE_HEIGHT);
  97. guiSidebar = new GUIGraphValues(sidebarPanel, SIDEBAR_WIDTH, height - TIMELINE_HEIGHT);
  98. guiSidebar.SetRange(-10.0f, 10.0f);
  99. }
  100. internal void OnPointerPressed(PointerEvent ev)
  101. {
  102. if (ev.IsUsed)
  103. return;
  104. Vector2I windowPos = window.ScreenToWindowPos(ev.ScreenPos);
  105. Rect2I elementBounds = GUIUtility.CalculateBounds(gui, window.GUI);
  106. Vector2I pointerPos = windowPos - new Vector2I(elementBounds.x, elementBounds.y);
  107. Rect2I drawingBounds = drawingPanel.Bounds;
  108. Vector2I drawingPos = pointerPos - new Vector2I(drawingBounds.x, drawingBounds.y);
  109. if (ev.Button == PointerButton.Left)
  110. {
  111. Vector2 curveCoord;
  112. if (guiCurveDrawing.PixelToCurveSpace(drawingPos, out curveCoord))
  113. {
  114. KeyframeRef keyframeRef;
  115. if (!guiCurveDrawing.FindKeyFrame(drawingPos, out keyframeRef))
  116. {
  117. TangentRef tangentRef;
  118. if (guiCurveDrawing.FindTangent(drawingPos, out tangentRef))
  119. {
  120. isMousePressedOverTangent = true;
  121. dragStart = drawingPos;
  122. draggedTangent = tangentRef;
  123. }
  124. else
  125. ClearSelection();
  126. }
  127. else
  128. {
  129. if (!IsSelected(keyframeRef))
  130. {
  131. if (!Input.IsButtonHeld(ButtonCode.LeftShift) && !Input.IsButtonHeld(ButtonCode.RightShift))
  132. ClearSelection();
  133. SelectKeyframe(keyframeRef);
  134. }
  135. isMousePressedOverKey = true;
  136. dragStart = drawingPos;
  137. }
  138. guiCurveDrawing.Rebuild();
  139. }
  140. else
  141. {
  142. int frameIdx = guiTimeline.GetFrame(pointerPos);
  143. if (frameIdx != -1)
  144. SetMarkedFrame(frameIdx);
  145. }
  146. isPointerHeld = true;
  147. }
  148. else if (ev.Button == PointerButton.Right)
  149. {
  150. Vector2 curveCoord;
  151. if (guiCurveDrawing.PixelToCurveSpace(drawingPos, out curveCoord))
  152. {
  153. contextClickPosition = drawingPos;
  154. KeyframeRef keyframeRef;
  155. if (!guiCurveDrawing.FindKeyFrame(drawingPos, out keyframeRef))
  156. {
  157. ClearSelection();
  158. blankContextMenu.Open(pointerPos, gui);
  159. }
  160. else
  161. {
  162. // If clicked outside of current selection, just select the one keyframe
  163. if (!IsSelected(keyframeRef))
  164. {
  165. ClearSelection();
  166. SelectKeyframe(keyframeRef);
  167. guiCurveDrawing.Rebuild();
  168. }
  169. keyframeContextMenu.Open(pointerPos, gui);
  170. }
  171. }
  172. }
  173. }
  174. internal void OnPointerMoved(PointerEvent ev)
  175. {
  176. if (ev.Button != PointerButton.Left)
  177. return;
  178. if (isPointerHeld)
  179. {
  180. Vector2I windowPos = window.ScreenToWindowPos(ev.ScreenPos);
  181. Rect2I elementBounds = GUIUtility.CalculateBounds(gui, window.GUI);
  182. Vector2I pointerPos = windowPos - new Vector2I(elementBounds.x, elementBounds.y);
  183. if (isMousePressedOverKey || isMousePressedOverTangent)
  184. {
  185. Rect2I drawingBounds = drawingPanel.Bounds;
  186. Vector2I drawingPos = pointerPos - new Vector2I(drawingBounds.x, drawingBounds.y);
  187. if (!isDragInProgress)
  188. {
  189. int distance = Vector2I.Distance(drawingPos, dragStart);
  190. if (distance >= DRAG_START_DISTANCE)
  191. {
  192. if (isMousePressedOverKey)
  193. {
  194. draggedKeyframes.Clear();
  195. foreach (var selectedEntry in selectedKeyframes)
  196. {
  197. EdAnimationCurve curve = curves[selectedEntry.curveIdx];
  198. KeyFrame[] keyFrames = curve.KeyFrames;
  199. DraggedKeyframes newEntry = new DraggedKeyframes();
  200. newEntry.curveIdx = selectedEntry.curveIdx;
  201. draggedKeyframes.Add(newEntry);
  202. foreach (var keyframeIdx in selectedEntry.keyIndices)
  203. newEntry.keys.Add(new DraggedKeyframe(keyframeIdx, keyFrames[keyframeIdx]));
  204. }
  205. }
  206. // TODO - UNDOREDO record keyframe or tangent
  207. isDragInProgress = true;
  208. }
  209. }
  210. if (isDragInProgress)
  211. {
  212. if (isMousePressedOverKey)
  213. {
  214. Vector2 diff = Vector2.Zero;
  215. Vector2 dragStartCurve;
  216. if (guiCurveDrawing.PixelToCurveSpace(dragStart, out dragStartCurve))
  217. {
  218. Vector2 currentPosCurve;
  219. if (guiCurveDrawing.PixelToCurveSpace(drawingPos, out currentPosCurve))
  220. diff = currentPosCurve - dragStartCurve;
  221. }
  222. foreach (var draggedEntry in draggedKeyframes)
  223. {
  224. EdAnimationCurve curve = curves[draggedEntry.curveIdx];
  225. for (int i = 0; i < draggedEntry.keys.Count; i++)
  226. {
  227. DraggedKeyframe draggedKey = draggedEntry.keys[i];
  228. float newTime = draggedKey.original.time + diff.x;
  229. float newValue = draggedKey.original.value + diff.y;
  230. int newIndex = curve.UpdateKeyframe(draggedKey.index, newTime, newValue);
  231. // It's possible key changed position due to time change, but since we're moving all
  232. // keys at once they cannot change position relative to one another, otherwise we would
  233. // need to update indices for other keys as well.
  234. draggedKey.index = newIndex;
  235. draggedEntry.keys[i] = draggedKey;
  236. }
  237. curve.Apply();
  238. }
  239. // Rebuild selected keys from dragged keys (after potential sorting)
  240. ClearSelection();
  241. foreach (var draggedEntry in draggedKeyframes)
  242. {
  243. foreach (var keyframe in draggedEntry.keys)
  244. SelectKeyframe(new KeyframeRef(draggedEntry.curveIdx, keyframe.index));
  245. }
  246. guiCurveDrawing.Rebuild();
  247. }
  248. else if (isMousePressedOverTangent)
  249. {
  250. EdAnimationCurve curve = curves[draggedTangent.keyframeRef.curveIdx];
  251. KeyFrame keyframe = curve.KeyFrames[draggedTangent.keyframeRef.keyIdx];
  252. Vector2 keyframeCurveCoords = new Vector2(keyframe.time, keyframe.value);
  253. Vector2 currentPosCurve;
  254. if (guiCurveDrawing.PixelToCurveSpace(drawingPos, out currentPosCurve))
  255. {
  256. Vector2 normal = currentPosCurve - keyframeCurveCoords;
  257. normal = normal.Normalized;
  258. float tangent = EdAnimationCurve.NormalToTangent(normal);
  259. if (draggedTangent.type == TangentType.In)
  260. {
  261. if (normal.x > 0.0f)
  262. tangent = float.PositiveInfinity;
  263. keyframe.inTangent = tangent;
  264. }
  265. else
  266. {
  267. if (normal.x < 0.0f)
  268. tangent = float.PositiveInfinity;
  269. keyframe.outTangent = tangent;
  270. }
  271. curve.KeyFrames[draggedTangent.keyframeRef.keyIdx] = keyframe;
  272. curve.Apply();
  273. guiCurveDrawing.Rebuild();
  274. }
  275. }
  276. }
  277. }
  278. else // Move frame marker
  279. {
  280. int frameIdx = guiTimeline.GetFrame(pointerPos);
  281. if (frameIdx != -1)
  282. SetMarkedFrame(frameIdx);
  283. }
  284. }
  285. }
  286. internal void OnPointerReleased(PointerEvent ev)
  287. {
  288. isPointerHeld = false;
  289. isDragInProgress = false;
  290. isMousePressedOverKey = false;
  291. isMousePressedOverTangent = false;
  292. }
  293. internal void OnButtonUp(ButtonEvent ev)
  294. {
  295. if(ev.Button == ButtonCode.Delete)
  296. DeleteSelectedKeyframes();
  297. }
  298. /// <summary>
  299. /// Change the set of curves to display.
  300. /// </summary>
  301. /// <param name="curves">New set of curves to draw on the GUI element.</param>
  302. public void SetCurves(EdAnimationCurve[] curves)
  303. {
  304. this.curves = curves;
  305. guiCurveDrawing.SetCurves(curves);
  306. // TODO - Recalculate valid size
  307. Redraw();
  308. }
  309. /// <summary>
  310. /// Change the physical size of the GUI element.
  311. /// </summary>
  312. /// <param name="width">Width of the element in pixels.</param>
  313. /// <param name="height">Height of the element in pixels.</param>
  314. public void SetSize(int width, int height)
  315. {
  316. guiTimeline.SetSize(width, TIMELINE_HEIGHT);
  317. guiCurveDrawing.SetSize(width, height - TIMELINE_HEIGHT);
  318. guiSidebar.SetSize(SIDEBAR_WIDTH, height - TIMELINE_HEIGHT);
  319. Redraw();
  320. }
  321. /// <summary>
  322. /// Changes the visible range that the GUI element displays.
  323. /// </summary>
  324. /// <param name="xRange">Range of the horizontal area. Displayed area will range from [0, xRange].</param>
  325. /// <param name="yRange">Range of the vertical area. Displayed area will range from
  326. /// [-yRange, yRange]</param>
  327. public void SetRange(float xRange, float yRange)
  328. {
  329. this.xRange = xRange;
  330. this.yRange = yRange;
  331. guiTimeline.SetRange(xRange);
  332. guiCurveDrawing.SetRange(xRange, yRange * 2.0f);
  333. guiSidebar.SetRange(yRange, yRange);
  334. Redraw();
  335. }
  336. /// <summary>
  337. /// Number of frames per second, used for frame selection and marking.
  338. /// </summary>
  339. /// <param name="fps">Number of prames per second.</param>
  340. public void SetFPS(int fps)
  341. {
  342. guiTimeline.SetFPS(fps);
  343. guiCurveDrawing.SetFPS(fps);
  344. Redraw();
  345. }
  346. /// <summary>
  347. /// Sets the frame at which to display the frame marker.
  348. /// </summary>
  349. /// <param name="frameIdx">Index of the frame to display the marker on, or -1 to clear the marker.</param>
  350. public void SetMarkedFrame(int frameIdx)
  351. {
  352. markedFrameIdx = frameIdx;
  353. guiTimeline.SetMarkedFrame(frameIdx);
  354. guiCurveDrawing.SetMarkedFrame(frameIdx);
  355. Redraw();
  356. }
  357. public void AddKeyFrameAtMarker()
  358. {
  359. ClearSelection();
  360. foreach (var curve in curves)
  361. {
  362. float t = guiCurveDrawing.GetTimeForFrame(markedFrameIdx);
  363. float value = curve.Evaluate(t);
  364. curve.AddKeyframe(t, value);
  365. curve.Apply();
  366. }
  367. // TODO - UNDOREDO
  368. guiCurveDrawing.Rebuild();
  369. }
  370. public void Redraw()
  371. {
  372. guiCurveDrawing.Rebuild();
  373. guiTimeline.Rebuild();
  374. guiSidebar.Rebuild();
  375. }
  376. private void ChangeSelectionTangentMode(TangentMode mode)
  377. {
  378. foreach (var selectedEntry in selectedKeyframes)
  379. {
  380. EdAnimationCurve curve = curves[selectedEntry.curveIdx];
  381. foreach (var keyframeIdx in selectedEntry.keyIndices)
  382. {
  383. if (mode == TangentMode.Auto || mode == TangentMode.Free)
  384. curve.SetTangentMode(keyframeIdx, mode);
  385. else
  386. {
  387. TangentMode newMode = curve.TangentModes[keyframeIdx];
  388. if (mode.HasFlag((TangentMode) TangentType.In))
  389. {
  390. // Replace only the in tangent mode, keeping the out tangent as is
  391. TangentMode inFlags = (TangentMode.InAuto | TangentMode.InFree | TangentMode.InLinear |
  392. TangentMode.InAuto);
  393. newMode &= ~inFlags;
  394. newMode |= (mode & inFlags);
  395. }
  396. else
  397. {
  398. // Replace only the out tangent mode, keeping the in tangent as is
  399. TangentMode outFlags = (TangentMode.OutAuto | TangentMode.OutFree | TangentMode.OutLinear |
  400. TangentMode.OutAuto);
  401. newMode &= ~outFlags;
  402. newMode |= (mode & outFlags);
  403. }
  404. curve.SetTangentMode(keyframeIdx, newMode);
  405. }
  406. }
  407. curve.Apply();
  408. }
  409. // TODO - UNDOREDO
  410. guiCurveDrawing.Rebuild();
  411. }
  412. private void AddKeyframeAtPosition()
  413. {
  414. Vector2 curveCoord;
  415. if (guiCurveDrawing.PixelToCurveSpace(contextClickPosition, out curveCoord))
  416. {
  417. ClearSelection();
  418. foreach (var curve in curves)
  419. {
  420. float t = curveCoord.x;
  421. float value = curveCoord.y;
  422. curve.AddKeyframe(t, value);
  423. curve.Apply();
  424. }
  425. // TODO - UNDOREDO
  426. guiCurveDrawing.Rebuild();
  427. }
  428. }
  429. private void AddEventAtPosition()
  430. {
  431. // TODO
  432. }
  433. private void DeleteSelectedKeyframes()
  434. {
  435. foreach (var selectedEntry in selectedKeyframes)
  436. {
  437. EdAnimationCurve curve = curves[selectedEntry.curveIdx];
  438. // Sort keys from highest to lowest so the indices don't change
  439. selectedEntry.keyIndices.Sort((x, y) =>
  440. {
  441. return y.CompareTo(x);
  442. });
  443. foreach (var keyframeIdx in selectedEntry.keyIndices)
  444. curve.RemoveKeyframe(keyframeIdx);
  445. curve.Apply();
  446. }
  447. // TODO - UNDOREDO
  448. ClearSelection();
  449. guiCurveDrawing.Rebuild();
  450. }
  451. private void ClearSelection()
  452. {
  453. guiCurveDrawing.ClearSelectedKeyframes();
  454. selectedKeyframes.Clear();
  455. }
  456. private void SelectKeyframe(KeyframeRef keyframeRef)
  457. {
  458. guiCurveDrawing.SelectKeyframe(keyframeRef, true);
  459. if (!IsSelected(keyframeRef))
  460. {
  461. int curveIdx = selectedKeyframes.FindIndex(x =>
  462. {
  463. return x.curveIdx == keyframeRef.curveIdx;
  464. });
  465. if (curveIdx == -1)
  466. {
  467. curveIdx = selectedKeyframes.Count;
  468. SelectedKeyframes newKeyframes = new SelectedKeyframes();
  469. newKeyframes.curveIdx = keyframeRef.curveIdx;
  470. selectedKeyframes.Add(newKeyframes);
  471. }
  472. selectedKeyframes[curveIdx].keyIndices.Add(keyframeRef.keyIdx);
  473. }
  474. }
  475. private bool IsSelected(KeyframeRef keyframeRef)
  476. {
  477. int curveIdx = selectedKeyframes.FindIndex(x =>
  478. {
  479. return x.curveIdx == keyframeRef.curveIdx;
  480. });
  481. if (curveIdx == -1)
  482. return false;
  483. int keyIdx = selectedKeyframes[curveIdx].keyIndices.FindIndex(x =>
  484. {
  485. return x == keyframeRef.keyIdx;
  486. });
  487. return keyIdx != -1;
  488. }
  489. }
  490. /** @} */
  491. }