GUICurveEditor.cs 22 KB

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