GUIGraphTime.cs 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. /// Renders a timeline that may be used as a header for a graph display. User can set the range of the times to display,
  12. /// as well as its physical dimensions.
  13. /// </summary>
  14. public class GUIGraphTime
  15. {
  16. public const int PADDING = 30;
  17. private const float TICK_HEIGHT_PCT = 0.4f;
  18. private const int TEXT_PADDING = 2;
  19. private int tickHeight;
  20. private int drawableWidth;
  21. private float rangeLength = 60.0f;
  22. private GUICanvas canvas;
  23. private GUIGraphTicks tickHandler;
  24. private int width;
  25. private int height;
  26. private int fps = 1;
  27. private int markedFrameIdx = -1;
  28. /// <summary>
  29. /// Constructs a new timeline and adds it to the specified layout.
  30. /// </summary>
  31. /// <param name="layout">Layout to add the timeline GUI to.</param>
  32. /// <param name="width">Width of the timeline in pixels.</param>
  33. /// <param name="height">Height of the timeline in pixels.</param>
  34. public GUIGraphTime(GUILayout layout, int width, int height)
  35. {
  36. canvas = new GUICanvas();
  37. layout.AddElement(canvas);
  38. tickHandler = new GUIGraphTicks(GUITickStepType.Time);
  39. SetSize(width, height);
  40. }
  41. /// <summary>
  42. /// Uses the assigned FPS, range and physical size to calculate the frame that is under the provided coordinates.
  43. /// </summary>
  44. /// <param name="windowCoords">Coordinate relative to the window the GUI element is on.</param>
  45. /// <returns>Frame that was clicked on, or -1 if the coordinates are outside of valid bounds. </returns>
  46. public int GetFrame(Vector2I windowCoords)
  47. {
  48. Rect2I bounds = canvas.Bounds;
  49. if (windowCoords.x < (bounds.x + PADDING) || windowCoords.x >= (bounds.x + bounds.width - PADDING) ||
  50. windowCoords.y < bounds.y || windowCoords.y >= (bounds.y + bounds.height))
  51. {
  52. return -1;
  53. }
  54. Vector2I relativeCoords = windowCoords - new Vector2I(bounds.x + PADDING, bounds.y);
  55. float lengthPerPixel = GetRange() / drawableWidth;
  56. float time = relativeCoords.x * lengthPerPixel;
  57. return MathEx.RoundToInt(time * fps);
  58. }
  59. /// <summary>
  60. /// Sets the frame at which to display the frame marker.
  61. /// </summary>
  62. /// <param name="frameIdx">Index of the frame to display the marker on, or -1 to clear the marker.</param>
  63. public void SetMarkedFrame(int frameIdx)
  64. {
  65. markedFrameIdx = frameIdx;
  66. }
  67. /// <summary>
  68. /// Sets the physical size onto which to draw the timeline.
  69. /// </summary>
  70. /// <param name="width">Width in pixels.</param>
  71. /// <param name="height">Height in pixels.</param>
  72. public void SetSize(int width, int height)
  73. {
  74. this.width = width;
  75. this.height = height;
  76. canvas.SetWidth(width);
  77. canvas.SetHeight(height);
  78. tickHeight = (int)(height * TICK_HEIGHT_PCT);
  79. drawableWidth = Math.Max(0, width - PADDING * 2);
  80. tickHandler.SetRange(0.0f, GetRange(true), drawableWidth + PADDING);
  81. }
  82. /// <summary>
  83. /// Sets the range of values to display on the timeline.
  84. /// </summary>
  85. /// <param name="length">Amount of time to display, in seconds.</param>
  86. public void SetRange(float length)
  87. {
  88. rangeLength = Math.Max(0.0f, length);
  89. tickHandler.SetRange(0.0f, GetRange(true), drawableWidth + PADDING);
  90. }
  91. /// <summary>
  92. /// Number of frames per second, used for frame selection and marking.
  93. /// </summary>
  94. /// <param name="fps">Number of prames per second.</param>
  95. public void SetFPS(int fps)
  96. {
  97. this.fps = Math.Max(1, fps);
  98. tickHandler.SetRange(0.0f, GetRange(true), drawableWidth + PADDING);
  99. }
  100. /// <summary>
  101. /// Draws text displaying the time at the provided position.
  102. /// </summary>
  103. /// <param name="xPos">Position to draw the text at.</param>
  104. /// <param name="seconds">Time to display, in seconds.</param>
  105. /// <param name="minutes">If true the time will be displayed in minutes, otherwise in seconds.</param>
  106. private void DrawTime(int xPos, float seconds, bool minutes)
  107. {
  108. TimeSpan timeSpan = TimeSpan.FromSeconds(seconds);
  109. string timeString;
  110. if (minutes)
  111. timeString = timeSpan.TotalMinutes.ToString("#0") + ":" + timeSpan.Seconds.ToString("D2");
  112. else
  113. timeString = timeSpan.TotalSeconds.ToString("#0.00");
  114. Vector2I textBounds = GUIUtility.CalculateTextBounds(timeString, EditorBuiltin.DefaultFont,
  115. EditorStyles.DefaultFontSize);
  116. Vector2I textPosition = new Vector2I();
  117. textPosition.x = xPos - textBounds.x / 2;
  118. textPosition.y = TEXT_PADDING;
  119. canvas.DrawText(timeString, textPosition, EditorBuiltin.DefaultFont, Color.LightGray,
  120. EditorStyles.DefaultFontSize);
  121. }
  122. /// <summary>
  123. /// Draws one tick of the timeline, at the specified time.
  124. /// </summary>
  125. /// <param name="t">Time at which to draw the tick.</param>
  126. /// <param name="strength">Strength of the tick (determines size and color), in range [0, 1].</param>
  127. /// <param name="drawText">If true the text displaying the time will be drawn above this tick.</param>
  128. /// <param name="displayAsMinutes">Should the text drawn be displayed as minutes (if true), or seconds (false).
  129. /// Ignored if no text is drawn.</param>
  130. private void DrawTick(float t, float strength, bool drawText, bool displayAsMinutes)
  131. {
  132. int xPos = (int)((t / GetRange()) * drawableWidth) + PADDING;
  133. // Draw tick
  134. Vector2I start = new Vector2I(xPos, height - (int)(tickHeight * strength));
  135. Vector2I end = new Vector2I(xPos, height);
  136. Color color = Color.LightGray;
  137. color.a *= strength;
  138. canvas.DrawLine(start, end, color);
  139. // Draw text if it fits
  140. if (drawText)
  141. DrawTime(xPos, t, displayAsMinutes);
  142. }
  143. /// <summary>
  144. /// Draws a vertical frame marker at the specified time.
  145. /// </summary>
  146. /// <param name="t">Time at which to draw the marker.</param>
  147. private void DrawFrameMarker(float t)
  148. {
  149. int xPos = (int)((t / GetRange()) * drawableWidth) + PADDING;
  150. Vector2I start = new Vector2I(xPos, 0);
  151. Vector2I end = new Vector2I(xPos, height);
  152. canvas.DrawLine(start, end, Color.BansheeOrange);
  153. }
  154. /// <summary>
  155. /// Returns the range of times displayed by the timeline rounded to the multiple of FPS.
  156. /// </summary>
  157. /// <param name="padding">If true, extra range will be included to cover the right-most padding.</param>
  158. /// <returns>Time range rounded to a multiple of FPS.</returns>
  159. private float GetRange(bool padding = false)
  160. {
  161. float spf = 1.0f/fps;
  162. float range = rangeLength;
  163. if (padding)
  164. {
  165. float lengthPerPixel = rangeLength/drawableWidth;
  166. range += lengthPerPixel * PADDING;
  167. }
  168. return ((int)range / spf) * spf;
  169. }
  170. /// <summary>
  171. /// Rebuilds the internal GUI elements. Should be called whenever timeline properties change.
  172. /// </summary>
  173. public void Rebuild()
  174. {
  175. canvas.Clear();
  176. float range = GetRange();
  177. int numTickLevels = tickHandler.NumLevels;
  178. for (int i = numTickLevels - 1; i >= 0; i--)
  179. {
  180. bool drawText = i == 0;
  181. float[] ticks = tickHandler.GetTicks(i);
  182. float strength = tickHandler.GetLevelStrength(i);
  183. if (ticks.Length > 0)
  184. {
  185. float valuePerTick = range/ticks.Length;
  186. bool displayAsMinutes = TimeSpan.FromSeconds(valuePerTick).Minutes > 0;
  187. for (int j = 0; j < ticks.Length; j++)
  188. DrawTick(ticks[j], strength, drawText, displayAsMinutes);
  189. }
  190. }
  191. if (markedFrameIdx != -1)
  192. {
  193. int numFrames = (int)range * fps;
  194. float timePerFrame = range / numFrames;
  195. DrawFrameMarker(markedFrameIdx*timePerFrame);
  196. }
  197. }
  198. }
  199. /** @} */
  200. }