GUITimelineBase.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using bs;
  5. namespace bs.Editor
  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. return frameIdx / (float)fps;
  83. }
  84. /// <summary>
  85. /// Sets the frame at which to display the frame marker.
  86. /// </summary>
  87. /// <param name="frameIdx">Index of the frame to display the marker on, or -1 to clear the marker.</param>
  88. public void SetMarkedFrame(int frameIdx)
  89. {
  90. markedFrameIdx = frameIdx;
  91. }
  92. /// <summary>
  93. /// Sets the physical size onto which to draw the timeline.
  94. /// </summary>
  95. /// <param name="width">Width in pixels.</param>
  96. /// <param name="height">Height in pixels.</param>
  97. public void SetSize(int width, int height)
  98. {
  99. this.width = width;
  100. this.height = height;
  101. canvas.SetWidth(width);
  102. canvas.SetHeight(height);
  103. drawableWidth = Math.Max(0, width - PADDING * 2);
  104. }
  105. /// <summary>
  106. /// Sets the range of values to display on the timeline.
  107. /// </summary>
  108. /// <param name="length">Amount of time to display, in seconds.</param>
  109. public void SetRange(float length)
  110. {
  111. rangeLength = Math.Max(0.0f, length);
  112. }
  113. /// <summary>
  114. /// Returns the offset at which the displayed timeline values start at.
  115. /// </summary>
  116. /// <param name="offset">Value to start the timeline values at, in seconds.</param>
  117. public void SetOffset(float offset)
  118. {
  119. rangeOffset = offset;
  120. }
  121. /// <summary>
  122. /// Number of frames per second, used for frame selection and marking.
  123. /// </summary>
  124. /// <param name="fps">Number of prames per second.</param>
  125. public void SetFPS(int fps)
  126. {
  127. this.fps = Math.Max(1, fps);
  128. }
  129. /// <summary>
  130. /// Returns the range of times displayed by the timeline rounded to the multiple of FPS.
  131. /// </summary>
  132. /// <param name="padding">If true, extra range will be included to cover the right-most padding.</param>
  133. /// <returns>Time range rounded to a multiple of FPS.</returns>
  134. protected float GetRange(bool padding = false)
  135. {
  136. float spf = 1.0f / fps;
  137. float range = rangeLength;
  138. if (padding)
  139. {
  140. float lengthPerPixel = rangeLength / drawableWidth;
  141. range += lengthPerPixel * PADDING;
  142. }
  143. return MathEx.Max(1.0f, range / spf) * spf;
  144. }
  145. /// <summary>
  146. /// Draws a vertical frame marker at the specified time.
  147. /// </summary>
  148. /// <param name="t">Time at which to draw the marker.</param>
  149. private void DrawFrameMarker(float t)
  150. {
  151. int xPos = (int)(((t - rangeOffset) / GetRange()) * drawableWidth) + PADDING;
  152. Vector2I start = new Vector2I(xPos, 0);
  153. Vector2I end = new Vector2I(xPos, height);
  154. canvas.DrawLine(start, end, Color.BansheeOrange);
  155. }
  156. /// <summary>
  157. /// Draws the frame marker at the currently selected frame.
  158. /// </summary>
  159. protected void DrawFrameMarker()
  160. {
  161. if (markedFrameIdx != -1)
  162. DrawFrameMarker(markedFrameIdx / (float)fps);
  163. }
  164. /// <summary>
  165. /// Rebuilds the internal GUI elements. Should be called whenever timeline properties change.
  166. /// </summary>
  167. public virtual void Rebuild()
  168. {
  169. canvas.Clear();
  170. }
  171. }
  172. /** @} */
  173. }