GUICurveDrawing.cs 27 KB

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