GUITimelineBase.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /** @addtogroup AnimationEditor
  8. * @{
  9. */
  10. /// <summary>
  11. /// Base class that can be implemented by objects needing to elements along draw a horizontal timeline.
  12. /// </summary>
  13. public class GUITimelineBase
  14. {
  15. public const int PADDING = 30;
  16. protected int drawableWidth;
  17. protected float rangeLength = 60.0f;
  18. protected float rangeOffset = 0.0f;
  19. protected GUICanvas canvas;
  20. protected int width;
  21. protected int height;
  22. protected int fps = 1;
  23. protected int markedFrameIdx = -1;
  24. /// <summary>
  25. /// Constructs a new timeline and adds it to the specified layout.
  26. /// </summary>
  27. /// <param name="layout">Layout to add the timeline GUI to.</param>
  28. /// <param name="width">Width of the timeline in pixels.</param>
  29. /// <param name="height">Height of the timeline in pixels.</param>
  30. public GUITimelineBase(GUILayout layout, int width, int height)
  31. {
  32. canvas = new GUICanvas();
  33. layout.AddElement(canvas);
  34. SetSize(width, height);
  35. }
  36. /// <summary>
  37. /// Uses the assigned FPS, range and physical size to calculate the frame that is under the provided coordinates.
  38. /// </summary>
  39. /// <param name="pixelCoords">Coordinate relative to the layout the GUI element is on.</param>
  40. /// <returns>Frame that was clicked on, or -1 if the coordinates are outside of valid bounds. </returns>
  41. public int GetFrame(Vector2I pixelCoords)
  42. {
  43. Rect2I bounds = canvas.Bounds;
  44. if (pixelCoords.x < (bounds.x + PADDING) || pixelCoords.x >= (bounds.x + bounds.width - PADDING) ||
  45. pixelCoords.y < bounds.y || pixelCoords.y >= (bounds.y + bounds.height))
  46. {
  47. return -1;
  48. }
  49. Vector2I relativeCoords = pixelCoords - new Vector2I(bounds.x + PADDING, bounds.y);
  50. float lengthPerPixel = GetRange() / drawableWidth;
  51. float time = rangeOffset + relativeCoords.x * lengthPerPixel;
  52. return MathEx.RoundToInt(time * fps);
  53. }
  54. /// <summary>
  55. /// Returns the time at the specified pixel value along the timeline.
  56. /// </summary>
  57. /// <param name="pixel">Coordinate relative to this GUI element, in pixels.</param>
  58. /// <returns>Time along the curve at the specified coordinates.</returns>
  59. public float GetTime(int pixel)
  60. {
  61. Rect2I bounds = canvas.Bounds;
  62. int relativeCoords = pixel - (bounds.x + PADDING);
  63. float lengthPerPixel = GetRange() / drawableWidth;
  64. return rangeOffset + relativeCoords * lengthPerPixel;
  65. }
  66. /// <summary>
  67. /// Finds the pixel offset relative to the GUI element's origin, of the specified time.
  68. /// </summary>
  69. /// <param name="time">Time value to return the offset for.</param>
  70. /// <returns>Offset in pixels relative to GUI element's origin.</returns>
  71. public int GetOffset(float time)
  72. {
  73. return (int)(((time - rangeOffset) / GetRange()) * drawableWidth) + PADDING;
  74. }
  75. /// <summary>
  76. /// Returns time for a frame with the specified index. Depends on set range and FPS.
  77. /// </summary>
  78. /// <param name="frameIdx">Index of the frame (not a key-frame) to get the time for.</param>
  79. /// <returns>Time of the frame with the provided index. </returns>
  80. public float GetTimeForFrame(int frameIdx)
  81. {
  82. float range = GetRange();
  83. int numFrames = (int)(range * fps);
  84. float timePerFrame = range / numFrames;
  85. return frameIdx * timePerFrame;
  86. }
  87. /// <summary>
  88. /// Sets the frame at which to display the frame marker.
  89. /// </summary>
  90. /// <param name="frameIdx">Index of the frame to display the marker on, or -1 to clear the marker.</param>
  91. public void SetMarkedFrame(int frameIdx)
  92. {
  93. markedFrameIdx = frameIdx;
  94. }
  95. /// <summary>
  96. /// Sets the physical size onto which to draw the timeline.
  97. /// </summary>
  98. /// <param name="width">Width in pixels.</param>
  99. /// <param name="height">Height in pixels.</param>
  100. public void SetSize(int width, int height)
  101. {
  102. this.width = width;
  103. this.height = height;
  104. canvas.SetWidth(width);
  105. canvas.SetHeight(height);
  106. drawableWidth = Math.Max(0, width - PADDING * 2);
  107. }
  108. /// <summary>
  109. /// Sets the range of values to display on the timeline.
  110. /// </summary>
  111. /// <param name="length">Amount of time to display, in seconds.</param>
  112. public void SetRange(float length)
  113. {
  114. rangeLength = Math.Max(0.0f, length);
  115. }
  116. /// <summary>
  117. /// Returns the offset at which the displayed timeline values start at.
  118. /// </summary>
  119. /// <param name="offset">Value to start the timeline values at, in seconds.</param>
  120. public void SetOffset(float offset)
  121. {
  122. rangeOffset = offset;
  123. }
  124. /// <summary>
  125. /// Number of frames per second, used for frame selection and marking.
  126. /// </summary>
  127. /// <param name="fps">Number of prames per second.</param>
  128. public void SetFPS(int fps)
  129. {
  130. this.fps = Math.Max(1, fps);
  131. }
  132. /// <summary>
  133. /// Draws a vertical frame marker at the specified time.
  134. /// </summary>
  135. /// <param name="t">Time at which to draw the marker.</param>
  136. private void DrawFrameMarker(float t)
  137. {
  138. int xPos = (int)(((t - rangeOffset) / GetRange()) * drawableWidth) + PADDING;
  139. Vector2I start = new Vector2I(xPos, 0);
  140. Vector2I end = new Vector2I(xPos, height);
  141. canvas.DrawLine(start, end, Color.BansheeOrange);
  142. }
  143. /// <summary>
  144. /// Returns the range of times displayed by the timeline rounded to the multiple of FPS.
  145. /// </summary>
  146. /// <param name="padding">If true, extra range will be included to cover the right-most padding.</param>
  147. /// <returns>Time range rounded to a multiple of FPS.</returns>
  148. protected float GetRange(bool padding = false)
  149. {
  150. float spf = 1.0f / fps;
  151. float range = rangeLength;
  152. if (padding)
  153. {
  154. float lengthPerPixel = rangeLength / drawableWidth;
  155. range += lengthPerPixel * PADDING;
  156. }
  157. return MathEx.Max(1.0f, range / spf) * spf;
  158. }
  159. /// <summary>
  160. /// Draws the frame marker at the currently selected frame.
  161. /// </summary>
  162. protected void DrawFrameMarker()
  163. {
  164. if (markedFrameIdx != -1)
  165. {
  166. float range = GetRange();
  167. int numFrames = (int)(range * fps);
  168. float timePerFrame = range / numFrames;
  169. DrawFrameMarker(markedFrameIdx * timePerFrame);
  170. }
  171. }
  172. /// <summary>
  173. /// Rebuilds the internal GUI elements. Should be called whenever timeline properties change.
  174. /// </summary>
  175. public virtual void Rebuild()
  176. {
  177. canvas.Clear();
  178. }
  179. }
  180. /** @} */
  181. }