GUICurveDrawing.cs 25 KB

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