GUICurveDrawing.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  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. /// <summary>
  12. /// Draws one or multiple curves over the specified physical area. User can specify horizontal and vertical range to
  13. /// display, as well as physical size of the GUI area.
  14. /// </summary>
  15. internal class GUICurveDrawing
  16. {
  17. private const int LINE_SPLIT_WIDTH = 2;
  18. private const int TANGENT_LINE_DISTANCE = 30;
  19. private static readonly Color COLOR_MID_GRAY = new Color(90.0f / 255.0f, 90.0f / 255.0f, 90.0f / 255.0f, 1.0f);
  20. private static readonly Color COLOR_DARK_GRAY = new Color(40.0f / 255.0f, 40.0f / 255.0f, 40.0f / 255.0f, 1.0f);
  21. private EdAnimationCurve[] curves;
  22. private bool[][] selectedKeyframes;
  23. private int width;
  24. private int height;
  25. private float xRange = 60.0f;
  26. private float yRange = 20.0f;
  27. private int fps = 1;
  28. private int markedFrameIdx = 0;
  29. private int drawableWidth;
  30. private GUICanvas canvas;
  31. private GUIGraphTicks tickHandler;
  32. /// <summary>
  33. /// Creates a new curve drawing GUI element.
  34. /// </summary>
  35. /// <param name="layout">Layout into which to add the GUI element.</param>
  36. /// <param name="width">Width of the element in pixels.</param>
  37. /// <param name="height">Height of the element in pixels.</param>
  38. /// <param name="curves">Initial set of curves to display. </param>
  39. public GUICurveDrawing(GUILayout layout, int width, int height, EdAnimationCurve[] curves)
  40. {
  41. canvas = new GUICanvas();
  42. layout.AddElement(canvas);
  43. tickHandler = new GUIGraphTicks(GUITickStepType.Time);
  44. this.curves = curves;
  45. SetSize(width, height);
  46. ClearSelectedKeyframes(); // Makes sure the array is initialized
  47. Rebuild();
  48. }
  49. /// <summary>
  50. /// Change the set of curves to display.
  51. /// </summary>
  52. /// <param name="curves">New set of curves to draw on the GUI element.</param>
  53. public void SetCurves(EdAnimationCurve[] curves)
  54. {
  55. this.curves = curves;
  56. }
  57. /// <summary>
  58. /// Change the physical size of the GUI element.
  59. /// </summary>
  60. /// <param name="width">Width of the element in pixels.</param>
  61. /// <param name="height">Height of the element in pixels.</param>
  62. public void SetSize(int width, int height)
  63. {
  64. this.width = width;
  65. this.height = height;
  66. canvas.SetWidth(width);
  67. canvas.SetHeight(height);
  68. drawableWidth = Math.Max(0, width - GUIGraphTime.PADDING * 2);
  69. }
  70. /// <summary>
  71. /// Changes the visible range that the GUI element displays.
  72. /// </summary>
  73. /// <param name="xRange">Range of the horizontal area. Displayed area will range from [0, xRange].</param>
  74. /// <param name="yRange">Range of the vertical area. Displayed area will range from
  75. /// [-yRange * 0.5, yRange * 0.5]</param>
  76. public void SetRange(float xRange, float yRange)
  77. {
  78. this.xRange = xRange;
  79. this.yRange = yRange;
  80. }
  81. /// <summary>
  82. /// Number of frames per second, used for frame selection and marking.
  83. /// </summary>
  84. /// <param name="fps">Number of prames per second.</param>
  85. public void SetFPS(int fps)
  86. {
  87. this.fps = Math.Max(1, fps);
  88. }
  89. /// <summary>
  90. /// Sets the frame at which to display the frame marker.
  91. /// </summary>
  92. /// <param name="frameIdx">Index of the frame to display the marker on, or -1 to clear the marker.</param>
  93. public void SetMarkedFrame(int frameIdx)
  94. {
  95. markedFrameIdx = frameIdx;
  96. }
  97. /// <summary>
  98. /// Marks the specified key-frame as selected, changing the way it is displayed.
  99. /// </summary>
  100. /// <param name="keyframeRef">Keyframe reference containing the curve and keyframe index.</param>
  101. /// <param name="selected">True to select it, false to deselect it.</param>
  102. public void SelectKeyframe(KeyframeRef keyframeRef, bool selected)
  103. {
  104. if (selectedKeyframes == null)
  105. return;
  106. if (keyframeRef.curveIdx < 0 || keyframeRef.curveIdx >= selectedKeyframes.Length)
  107. return;
  108. if (keyframeRef.keyIdx < 0 || keyframeRef.keyIdx >= selectedKeyframes[keyframeRef.curveIdx].Length)
  109. return;
  110. selectedKeyframes[keyframeRef.curveIdx][keyframeRef.keyIdx] = selected;
  111. }
  112. /// <summary>
  113. /// Clears any key-frames that were marked as selected.
  114. /// </summary>
  115. public void ClearSelectedKeyframes()
  116. {
  117. selectedKeyframes = new bool[curves.Length][];
  118. for (int i = 0; i < curves.Length; i++)
  119. {
  120. KeyFrame[] keyframes = curves[i].KeyFrames;
  121. selectedKeyframes[i] = new bool[keyframes.Length];
  122. }
  123. }
  124. /// <summary>
  125. /// Returns time for a frame with the specified index. Depends on set range and FPS.
  126. /// </summary>
  127. /// <param name="frameIdx">Index of the frame (not a key-frame) to get the time for.</param>
  128. /// <returns>Time of the frame with the provided index. </returns>
  129. public float GetTimeForFrame(int frameIdx)
  130. {
  131. float range = GetRange();
  132. int numFrames = (int)range * fps;
  133. float timePerFrame = range / numFrames;
  134. return frameIdx* timePerFrame;
  135. }
  136. /// <summary>
  137. /// Attempts to find a keyframe under the provided coordinates.
  138. /// </summary>
  139. /// <param name="pixelCoords">Coordinates relative to this GUI element in pixels.</param>
  140. /// <param name="keyframe">Output object containing keyframe index and index of the curve it belongs to. Only valid
  141. /// if method returns true.</param>
  142. /// <returns>True if there is a keyframe under the coordinates, false otherwise.</returns>
  143. public bool FindKeyFrame(Vector2I pixelCoords, out KeyframeRef keyframe)
  144. {
  145. keyframe = new KeyframeRef();
  146. float nearestDistance = float.MaxValue;
  147. for (int i = 0; i < curves.Length; i++)
  148. {
  149. EdAnimationCurve curve = curves[i];
  150. KeyFrame[] keyframes = curve.KeyFrames;
  151. for (int j = 0; j < keyframes.Length; j++)
  152. {
  153. Vector2 keyframeCurveCoords = new Vector2(keyframes[j].time, keyframes[j].value);
  154. Vector2I keyframeCoords = CurveToPixelSpace(keyframeCurveCoords);
  155. float distanceToKey = Vector2I.Distance(pixelCoords, keyframeCoords);
  156. if (distanceToKey < nearestDistance)
  157. {
  158. nearestDistance = distanceToKey;
  159. keyframe.keyIdx = j;
  160. keyframe.curveIdx = i;
  161. }
  162. }
  163. }
  164. // We're not near any keyframe
  165. if (nearestDistance > 5.0f)
  166. return false;
  167. return true;
  168. }
  169. /// <summary>
  170. /// Attempts to find a a tangent handle under the provided coordinates.
  171. /// </summary>
  172. /// <param name="pixelCoords">Coordinates relative to this GUI element in pixels.</param>
  173. /// <param name="tangent">Output object containing keyframe information and tangent type. Only valid if method
  174. /// returns true.</param>
  175. /// <returns>True if there is a tangent handle under the coordinates, false otherwise.</returns>
  176. public bool FindTangent(Vector2I pixelCoords, out TangentRef tangent)
  177. {
  178. tangent = new TangentRef();
  179. float nearestDistance = float.MaxValue;
  180. for (int i = 0; i < curves.Length; i++)
  181. {
  182. EdAnimationCurve curve = curves[i];
  183. KeyFrame[] keyframes = curve.KeyFrames;
  184. for (int j = 0; j < keyframes.Length; j++)
  185. {
  186. if (!IsSelected(i, j))
  187. continue;
  188. TangentMode tangentMode = curve.TangentModes[j];
  189. if (IsTangentDisplayed(tangentMode, TangentType.In))
  190. {
  191. Vector2I tangentCoords = GetTangentPosition(keyframes[j], TangentType.In);
  192. float distanceToHandle = Vector2I.Distance(pixelCoords, tangentCoords);
  193. if (distanceToHandle < nearestDistance)
  194. {
  195. nearestDistance = distanceToHandle;
  196. tangent.keyframeRef.keyIdx = j;
  197. tangent.keyframeRef.curveIdx = i;
  198. tangent.type = TangentType.In;
  199. }
  200. ; }
  201. if (IsTangentDisplayed(tangentMode, TangentType.Out))
  202. {
  203. Vector2I tangentCoords = GetTangentPosition(keyframes[j], TangentType.Out);
  204. float distanceToHandle = Vector2I.Distance(pixelCoords, tangentCoords);
  205. if (distanceToHandle < nearestDistance)
  206. {
  207. nearestDistance = distanceToHandle;
  208. tangent.keyframeRef.keyIdx = j;
  209. tangent.keyframeRef.curveIdx = i;
  210. tangent.type = TangentType.Out;
  211. }
  212. }
  213. }
  214. }
  215. // We're not near any keyframe
  216. if (nearestDistance > 5.0f)
  217. return false;
  218. return true;
  219. }
  220. /// <summary>
  221. /// Converts pixel coordinates into coordinates in curve space.
  222. /// </summary>
  223. /// <param name="pixelCoords">Coordinates relative to this GUI element, in pixels.</param>
  224. /// <param name="curveCoords">Curve coordinates within the range as specified by <see cref="SetRange"/>. Only
  225. /// valid when function returns true.</param>
  226. /// <returns>True if the window coordinates were within the curve area, false otherwise.</returns>
  227. public bool PixelToCurveSpace(Vector2I pixelCoords, out Vector2 curveCoords)
  228. {
  229. Rect2I bounds = canvas.Bounds;
  230. // Check if outside of curve drawing bounds
  231. if (pixelCoords.x < (bounds.x + GUIGraphTime.PADDING) || pixelCoords.x >= (bounds.x + bounds.width - GUIGraphTime.PADDING) ||
  232. pixelCoords.y < bounds.y || pixelCoords.y >= (bounds.y + bounds.height))
  233. {
  234. curveCoords = new Vector2();
  235. return false;
  236. }
  237. // Find time and value of the place under the coordinates
  238. Vector2I relativeCoords = pixelCoords - new Vector2I(bounds.x + GUIGraphTime.PADDING, bounds.y);
  239. float lengthPerPixel = GetRange() / drawableWidth;
  240. float heightPerPixel = yRange / height;
  241. float yOffset = yRange / 2.0f;
  242. float t = relativeCoords.x * lengthPerPixel;
  243. float value = yOffset - relativeCoords.y * heightPerPixel;
  244. curveCoords = new Vector2();
  245. curveCoords.x = t;
  246. curveCoords.y = value;
  247. return true;
  248. }
  249. /// <summary>
  250. /// Converts coordinate in curve space (time, value) into pixel coordinates relative to this element's origin.
  251. /// </summary>
  252. /// <param name="curveCoords">Time and value of the location to convert.</param>
  253. /// <returns>Coordinates relative to this element's origin, in pixels.</returns>
  254. public Vector2I CurveToPixelSpace(Vector2 curveCoords)
  255. {
  256. int heightOffset = height / 2; // So that y = 0 is at center of canvas
  257. Vector2I pixelCoords = new Vector2I();
  258. pixelCoords.x = (int)((curveCoords.x / GetRange()) * drawableWidth) + GUIGraphTime.PADDING;
  259. pixelCoords.y = heightOffset - (int)((curveCoords.y / yRange) * height);
  260. return pixelCoords;
  261. }
  262. /// <summary>
  263. /// Draws a vertical frame marker on the curve area.
  264. /// </summary>
  265. /// <param name="t">Time at which to draw the marker.</param>
  266. /// <param name="color">Color with which to draw the marker.</param>
  267. private void DrawFrameMarker(float t, Color color)
  268. {
  269. int xPos = (int)((t / GetRange()) * drawableWidth) + GUIGraphTime.PADDING;
  270. Vector2I start = new Vector2I(xPos, 0);
  271. Vector2I end = new Vector2I(xPos, height);
  272. canvas.DrawLine(start, end, color);
  273. }
  274. /// <summary>
  275. /// Draws a horizontal line representing the line at y = 0.
  276. /// </summary>
  277. private void DrawCenterLine()
  278. {
  279. int heightOffset = height / 2; // So that y = 0 is at center of canvas
  280. Vector2I start = new Vector2I(0, heightOffset);
  281. Vector2I end = new Vector2I(width, heightOffset);
  282. canvas.DrawLine(start, end, COLOR_DARK_GRAY);
  283. }
  284. /// <summary>
  285. /// Draws a diamond shape of the specified size at the coordinates.
  286. /// </summary>
  287. /// <param name="center">Position at which to place the diamond's center, in pixel coordinates.</param>
  288. /// <param name="size">Determines number of pixels to extend the diamond in each direction.</param>
  289. /// <param name="innerColor">Color of the diamond's background.</param>
  290. /// <param name="outerColor">Color of the diamond's outline.</param>
  291. private void DrawDiamond(Vector2I center, int size, Color innerColor, Color outerColor)
  292. {
  293. Vector2I a = new Vector2I(center.x - size, center.y);
  294. Vector2I b = new Vector2I(center.x, center.y - size);
  295. Vector2I c = new Vector2I(center.x + size, center.y);
  296. Vector2I d = new Vector2I(center.x, center.y + size);
  297. // Draw diamond shape
  298. Vector2I[] linePoints = new Vector2I[] { a, b, c, d, a };
  299. Vector2I[] trianglePoints = new Vector2I[] { b, c, a, d };
  300. canvas.DrawTriangleStrip(trianglePoints, innerColor, 101);
  301. canvas.DrawPolyLine(linePoints, outerColor, 100);
  302. }
  303. /// <summary>
  304. /// Draws a keyframe a the specified time and value.
  305. /// </summary>
  306. /// <param name="t">Time to draw the keyframe at.</param>
  307. /// <param name="y">Y value to draw the keyframe at.</param>
  308. /// <param name="selected">Determines should the keyframe be drawing using the selected color scheme, or normally.
  309. /// </param>
  310. private void DrawKeyframe(float t, float y, bool selected)
  311. {
  312. Vector2I pixelCoords = CurveToPixelSpace(new Vector2(t, y));
  313. if (selected)
  314. DrawDiamond(pixelCoords, 3, Color.White, Color.BansheeOrange);
  315. else
  316. DrawDiamond(pixelCoords, 3, Color.White, Color.Black);
  317. }
  318. /// <summary>
  319. /// Draws zero, one or two tangents for the specified keyframe. Whether tangents are drawn depends on the provided
  320. /// mode.
  321. /// </summary>
  322. /// <param name="keyFrame">Keyframe to draw the tangents for.</param>
  323. /// <param name="tangentMode">Type of tangents in the keyframe.</param>
  324. private void DrawTangents(KeyFrame keyFrame, TangentMode tangentMode)
  325. {
  326. Vector2I keyframeCoords = CurveToPixelSpace(new Vector2(keyFrame.time, keyFrame.value));
  327. if (IsTangentDisplayed(tangentMode, TangentType.In))
  328. {
  329. Vector2I tangentCoords = GetTangentPosition(keyFrame, TangentType.In);
  330. canvas.DrawLine(keyframeCoords, tangentCoords, Color.LightGray);
  331. DrawDiamond(tangentCoords, 2, Color.Green, Color.Black);
  332. }
  333. if (IsTangentDisplayed(tangentMode, TangentType.Out))
  334. {
  335. Vector2I tangentCoords = GetTangentPosition(keyFrame, TangentType.Out);
  336. canvas.DrawLine(keyframeCoords, tangentCoords, Color.LightGray);
  337. DrawDiamond(tangentCoords, 2, Color.Green, Color.Black);
  338. }
  339. }
  340. /// <summary>
  341. /// Returns the position of the tangent, in element's pixel space.
  342. /// </summary>
  343. /// <param name="keyFrame">Keyframe that the tangent belongs to.</param>
  344. /// <param name="type">Which tangent to retrieve the position for.</param>
  345. /// <returns>Position of the tangent, relative to the this GUI element's origin, in pixels.</returns>
  346. private Vector2I GetTangentPosition(KeyFrame keyFrame, TangentType type)
  347. {
  348. Vector2I position = CurveToPixelSpace(new Vector2(keyFrame.time, keyFrame.value));
  349. Vector2 normal;
  350. if (type == TangentType.In)
  351. normal = -EdAnimationCurve.TangentToNormal(keyFrame.inTangent);
  352. else
  353. normal = EdAnimationCurve.TangentToNormal(keyFrame.outTangent);
  354. // X/Y ranges aren't scaled 1:1, adjust normal accordingly
  355. normal.x /= GetRange();
  356. normal.y /= yRange;
  357. normal = Vector2.Normalize(normal);
  358. // Convert normal (in percentage) to pixel values
  359. Vector2I offset = new Vector2I((int)(normal.x * TANGENT_LINE_DISTANCE),
  360. (int)(-normal.y * TANGENT_LINE_DISTANCE));
  361. return position + offset;
  362. }
  363. /// <summary>
  364. /// Checks if the tangent should be displayed, depending on the active tangent mode.
  365. /// </summary>
  366. /// <param name="mode">Tangent mode for the keyframe.</param>
  367. /// <param name="type">Which tangent to check for.</param>
  368. /// <returns>True if the tangent should be displayed.</returns>
  369. private bool IsTangentDisplayed(TangentMode mode, TangentType type)
  370. {
  371. if (mode == TangentMode.Auto)
  372. return false;
  373. else if (mode == TangentMode.Free)
  374. return true;
  375. if (type == TangentType.In)
  376. return !mode.HasFlag(TangentMode.InAuto);
  377. else
  378. return !mode.HasFlag(TangentMode.OutAuto);
  379. }
  380. /// <summary>
  381. /// Returns the range of times displayed by the timeline rounded to the multiple of FPS.
  382. /// </summary>
  383. /// <param name="padding">If true, extra range will be included to cover the right-most padding.</param>
  384. /// <returns>Time range rounded to a multiple of FPS.</returns>
  385. private float GetRange(bool padding = false)
  386. {
  387. float spf = 1.0f / fps;
  388. float range = xRange;
  389. if (padding)
  390. {
  391. float lengthPerPixel = xRange / drawableWidth;
  392. range += lengthPerPixel * GUIGraphTime.PADDING;
  393. }
  394. return ((int)range / spf) * spf;
  395. }
  396. /// <summary>
  397. /// Rebuilds the internal GUI elements. Should be called whenever timeline properties change.
  398. /// </summary>
  399. public void Rebuild()
  400. {
  401. canvas.Clear();
  402. if (curves == null)
  403. return;
  404. tickHandler.SetRange(0.0f, GetRange(true), drawableWidth + GUIGraphTime.PADDING);
  405. // Draw vertical frame markers
  406. int numTickLevels = tickHandler.NumLevels;
  407. for (int i = numTickLevels - 1; i >= 0; i--)
  408. {
  409. float[] ticks = tickHandler.GetTicks(i);
  410. float strength = tickHandler.GetLevelStrength(i);
  411. for (int j = 0; j < ticks.Length; j++)
  412. {
  413. Color color = COLOR_DARK_GRAY;
  414. color.a *= strength;
  415. DrawFrameMarker(ticks[j], color);
  416. }
  417. }
  418. // Draw center line
  419. DrawCenterLine();
  420. // Draw curves
  421. int curveIdx = 0;
  422. foreach (var curve in curves)
  423. {
  424. Color color = GetUniqueColor(curveIdx);
  425. DrawCurve(curve, color);
  426. // Draw keyframes
  427. KeyFrame[] keyframes = curve.KeyFrames;
  428. for (int i = 0; i < keyframes.Length; i++)
  429. {
  430. bool isSelected = IsSelected(curveIdx, i);
  431. DrawKeyframe(keyframes[i].time, keyframes[i].value, isSelected);
  432. if (isSelected)
  433. DrawTangents(keyframes[i], curve.TangentModes[i]);
  434. }
  435. curveIdx++;
  436. }
  437. // Draw selected frame marker
  438. if (markedFrameIdx != -1)
  439. DrawFrameMarker(GetTimeForFrame(markedFrameIdx), Color.BansheeOrange);
  440. }
  441. /// <summary>
  442. /// Generates a unique color based on the provided index.
  443. /// </summary>
  444. /// <param name="idx">Index to use for generating a color. Should be less than 30 in order to guarantee reasonably
  445. /// different colors.</param>
  446. /// <returns>Unique color.</returns>
  447. private Color GetUniqueColor(int idx)
  448. {
  449. const int COLOR_SPACING = 359 / 15;
  450. float hue = ((idx * COLOR_SPACING) % 359) / 359.0f;
  451. return Color.HSV2RGB(new Color(hue, 175.0f / 255.0f, 175.0f / 255.0f));
  452. }
  453. /// <summary>
  454. /// Checks is the provided key-frame currently marked as selected.
  455. /// </summary>
  456. /// <param name="curveIdx">Index of the curve the keyframe is on.</param>
  457. /// <param name="keyIdx">Index of the keyframe.</param>
  458. /// <returns>True if selected, false otherwise.</returns>
  459. private bool IsSelected(int curveIdx, int keyIdx)
  460. {
  461. if (selectedKeyframes == null)
  462. return false;
  463. if (curveIdx < 0 || curveIdx >= selectedKeyframes.Length)
  464. return false;
  465. if (keyIdx < 0 || keyIdx >= selectedKeyframes[curveIdx].Length)
  466. return false;
  467. return selectedKeyframes[curveIdx][keyIdx];
  468. }
  469. /// <summary>
  470. /// Draws the curve using the provided color.
  471. /// </summary>
  472. /// <param name="curve">Curve to draw within the currently set range. </param>
  473. /// <param name="color">Color to draw the curve with.</param>
  474. private void DrawCurve(EdAnimationCurve curve, Color color)
  475. {
  476. float range = GetRange();
  477. float lengthPerPixel = range / drawableWidth;
  478. float pixelsPerHeight = height/yRange;
  479. int heightOffset = height/2; // So that y = 0 is at center of canvas
  480. KeyFrame[] keyframes = curve.KeyFrames;
  481. if (keyframes.Length < 0)
  482. return;
  483. // Draw start line
  484. {
  485. float start = MathEx.Clamp(keyframes[0].time, 0.0f, range);
  486. int startPixel = (int)(start / lengthPerPixel);
  487. int xPosStart = 0;
  488. int xPosEnd = GUIGraphTime.PADDING + startPixel;
  489. int yPos = (int)(curve.Evaluate(0.0f, false) * pixelsPerHeight);
  490. yPos = heightOffset - yPos; // Offset and flip height (canvas Y goes down)
  491. Vector2I a = new Vector2I(xPosStart, yPos);
  492. Vector2I b = new Vector2I(xPosEnd, yPos);
  493. canvas.DrawLine(a, b, COLOR_MID_GRAY);
  494. }
  495. List<Vector2I> linePoints = new List<Vector2I>();
  496. // Draw in between keyframes
  497. for (int i = 0; i < keyframes.Length - 1; i++)
  498. {
  499. float start = MathEx.Clamp(keyframes[i].time, 0.0f, range);
  500. float end = MathEx.Clamp(keyframes[i + 1].time, 0.0f, range);
  501. int startPixel = (int)(start / lengthPerPixel);
  502. int endPixel = (int)(end / lengthPerPixel);
  503. bool isStep = keyframes[i].outTangent == float.PositiveInfinity ||
  504. keyframes[i + 1].inTangent == float.PositiveInfinity;
  505. // If step tangent, draw the required lines without sampling, as the sampling will miss the step
  506. if (isStep)
  507. {
  508. // Line from left to right frame
  509. int xPos = startPixel;
  510. int yPosStart = (int)(curve.Evaluate(start, false) * pixelsPerHeight);
  511. yPosStart = heightOffset - yPosStart; // Offset and flip height (canvas Y goes down)
  512. linePoints.Add(new Vector2I(GUIGraphTime.PADDING + xPos, yPosStart));
  513. xPos = endPixel;
  514. linePoints.Add(new Vector2I(GUIGraphTime.PADDING + xPos, yPosStart));
  515. // Line representing the step
  516. int yPosEnd = (int)(curve.Evaluate(end, false) * pixelsPerHeight);
  517. yPosEnd = heightOffset - yPosEnd; // Offset and flip height (canvas Y goes down)
  518. linePoints.Add(new Vector2I(GUIGraphTime.PADDING + xPos, yPosEnd));
  519. }
  520. else // Draw normally
  521. {
  522. int numSplits;
  523. float timeIncrement;
  524. if (startPixel != endPixel)
  525. {
  526. float fNumSplits = (endPixel - startPixel)/(float) LINE_SPLIT_WIDTH;
  527. numSplits = MathEx.FloorToInt(fNumSplits);
  528. float remainder = fNumSplits - numSplits;
  529. float lengthRounded = (end - start)*(numSplits/fNumSplits);
  530. timeIncrement = lengthRounded/numSplits;
  531. numSplits += MathEx.CeilToInt(remainder) + 1;
  532. }
  533. else
  534. {
  535. numSplits = 1;
  536. timeIncrement = 0.0f;
  537. }
  538. for (int j = 0; j < numSplits; j++)
  539. {
  540. int xPos = Math.Min(startPixel + j * LINE_SPLIT_WIDTH, endPixel);
  541. float t = Math.Min(start + j * timeIncrement, end);
  542. int yPos = (int)(curve.Evaluate(t, false) * pixelsPerHeight);
  543. yPos = heightOffset - yPos; // Offset and flip height (canvas Y goes down)
  544. linePoints.Add(new Vector2I(GUIGraphTime.PADDING + xPos, yPos));
  545. }
  546. }
  547. }
  548. canvas.DrawPolyLine(linePoints.ToArray(), color);
  549. // Draw end line
  550. {
  551. float end = MathEx.Clamp(keyframes[keyframes.Length - 1].time, 0.0f, range);
  552. int endPixel = (int)(end / lengthPerPixel);
  553. int xPosStart = GUIGraphTime.PADDING + endPixel;
  554. int xPosEnd = width;
  555. int yPos = (int)(curve.Evaluate(range, false) * pixelsPerHeight);
  556. yPos = heightOffset - yPos; // Offset and flip height (canvas Y goes down)
  557. Vector2I a = new Vector2I(xPosStart, yPos);
  558. Vector2I b = new Vector2I(xPosEnd, yPos);
  559. canvas.DrawLine(a, b, COLOR_MID_GRAY);
  560. }
  561. }
  562. }
  563. /** }@ */
  564. }