GUITimelineBase.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. /// Finds the pixel offset relative to the GUI element's origin, of the specified time.
  56. /// </summary>
  57. /// <param name="time">Time value to return the offset for.</param>
  58. /// <returns>Offset in pixels relative to GUI element's origin.</returns>
  59. public int GetOffset(float time)
  60. {
  61. return (int)(((time - rangeOffset) / GetRange()) * drawableWidth) + PADDING;
  62. }
  63. /// <summary>
  64. /// Returns time for a frame with the specified index. Depends on set range and FPS.
  65. /// </summary>
  66. /// <param name="frameIdx">Index of the frame (not a key-frame) to get the time for.</param>
  67. /// <returns>Time of the frame with the provided index. </returns>
  68. public float GetTimeForFrame(int frameIdx)
  69. {
  70. float range = GetRange();
  71. int numFrames = (int)range * fps;
  72. float timePerFrame = range / numFrames;
  73. return frameIdx * timePerFrame;
  74. }
  75. /// <summary>
  76. /// Sets the frame at which to display the frame marker.
  77. /// </summary>
  78. /// <param name="frameIdx">Index of the frame to display the marker on, or -1 to clear the marker.</param>
  79. public void SetMarkedFrame(int frameIdx)
  80. {
  81. markedFrameIdx = frameIdx;
  82. }
  83. /// <summary>
  84. /// Sets the physical size onto which to draw the timeline.
  85. /// </summary>
  86. /// <param name="width">Width in pixels.</param>
  87. /// <param name="height">Height in pixels.</param>
  88. public void SetSize(int width, int height)
  89. {
  90. this.width = width;
  91. this.height = height;
  92. canvas.SetWidth(width);
  93. canvas.SetHeight(height);
  94. drawableWidth = Math.Max(0, width - PADDING * 2);
  95. }
  96. /// <summary>
  97. /// Sets the range of values to display on the timeline.
  98. /// </summary>
  99. /// <param name="length">Amount of time to display, in seconds.</param>
  100. public void SetRange(float length)
  101. {
  102. rangeLength = Math.Max(0.0f, length);
  103. }
  104. /// <summary>
  105. /// Returns the offset at which the displayed timeline values start at.
  106. /// </summary>
  107. /// <param name="offset">Value to start the timeline values at, in seconds.</param>
  108. public void SetOffset(float offset)
  109. {
  110. rangeOffset = offset;
  111. }
  112. /// <summary>
  113. /// Number of frames per second, used for frame selection and marking.
  114. /// </summary>
  115. /// <param name="fps">Number of prames per second.</param>
  116. public void SetFPS(int fps)
  117. {
  118. this.fps = Math.Max(1, fps);
  119. }
  120. /// <summary>
  121. /// Draws a vertical frame marker at the specified time.
  122. /// </summary>
  123. /// <param name="t">Time at which to draw the marker.</param>
  124. private void DrawFrameMarker(float t)
  125. {
  126. int xPos = (int)(((t - rangeOffset) / GetRange()) * drawableWidth) + PADDING;
  127. Vector2I start = new Vector2I(xPos, 0);
  128. Vector2I end = new Vector2I(xPos, height);
  129. canvas.DrawLine(start, end, Color.BansheeOrange);
  130. }
  131. /// <summary>
  132. /// Returns the range of times displayed by the timeline rounded to the multiple of FPS.
  133. /// </summary>
  134. /// <param name="padding">If true, extra range will be included to cover the right-most padding.</param>
  135. /// <returns>Time range rounded to a multiple of FPS.</returns>
  136. protected float GetRange(bool padding = false)
  137. {
  138. float spf = 1.0f / fps;
  139. float range = rangeLength;
  140. if (padding)
  141. {
  142. float lengthPerPixel = rangeLength / drawableWidth;
  143. range += lengthPerPixel * PADDING;
  144. }
  145. return ((int)range / spf) * spf;
  146. }
  147. /// <summary>
  148. /// Draws the frame marker at the currently selected frame.
  149. /// </summary>
  150. protected void DrawFrameMarker()
  151. {
  152. if (markedFrameIdx != -1)
  153. {
  154. float range = GetRange();
  155. int numFrames = (int)range * fps;
  156. float timePerFrame = range / numFrames;
  157. DrawFrameMarker(markedFrameIdx * timePerFrame);
  158. }
  159. }
  160. /// <summary>
  161. /// Rebuilds the internal GUI elements. Should be called whenever timeline properties change.
  162. /// </summary>
  163. public virtual void Rebuild()
  164. {
  165. canvas.Clear();
  166. }
  167. }
  168. /** @} */
  169. }