GUIGraphTime.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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 = rangeLength / drawableWidth;
  56. float time = relativeCoords.x * lengthPerPixel;
  57. return (int)(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. Rebuild();
  67. }
  68. /// <summary>
  69. /// Sets the physical size onto which to draw the timeline.
  70. /// </summary>
  71. /// <param name="width">Width in pixels.</param>
  72. /// <param name="height">Height in pixels.</param>
  73. public void SetSize(int width, int height)
  74. {
  75. this.width = width;
  76. this.height = height;
  77. canvas.SetWidth(width);
  78. canvas.SetHeight(height);
  79. tickHeight = (int)(height * TICK_HEIGHT_PCT);
  80. drawableWidth = Math.Max(0, width - PADDING * 2);
  81. tickHandler.SetRange(0.0f, GetRange(true), drawableWidth + PADDING);
  82. Rebuild();
  83. }
  84. /// <summary>
  85. /// Sets the range of values to display on the timeline.
  86. /// </summary>
  87. /// <param name="length">Amount of time to display, in seconds.</param>
  88. public void SetRange(float length)
  89. {
  90. rangeLength = Math.Max(0.0f, length);
  91. tickHandler.SetRange(0.0f, GetRange(true), drawableWidth + PADDING);
  92. Rebuild();
  93. }
  94. /// <summary>
  95. /// Number of frames per second, used for frame selection and marking.
  96. /// </summary>
  97. /// <param name="fps">Number of prames per second.</param>
  98. public void SetFPS(int fps)
  99. {
  100. this.fps = Math.Max(1, fps);
  101. tickHandler.SetRange(0.0f, GetRange(true), drawableWidth + PADDING);
  102. Rebuild();
  103. }
  104. /// <summary>
  105. /// Draws text displaying the time at the provided position.
  106. /// </summary>
  107. /// <param name="xPos">Position to draw the text at.</param>
  108. /// <param name="seconds">Time to display, in seconds.</param>
  109. /// <param name="minutes">If true the time will be displayed in minutes, otherwise in seconds.</param>
  110. private void DrawTime(int xPos, float seconds, bool minutes)
  111. {
  112. TimeSpan timeSpan = TimeSpan.FromSeconds(seconds);
  113. string timeString;
  114. if (minutes)
  115. timeString = timeSpan.TotalMinutes.ToString("#0") + ":" + timeSpan.Seconds.ToString("D2");
  116. else
  117. timeString = timeSpan.TotalSeconds.ToString("#0.00");
  118. Vector2I textBounds = GUIUtility.CalculateTextBounds(timeString, EditorBuiltin.DefaultFont,
  119. EditorStyles.DefaultFontSize);
  120. Vector2I textPosition = new Vector2I();
  121. textPosition.x = xPos - textBounds.x / 2;
  122. textPosition.y = TEXT_PADDING;
  123. canvas.DrawText(timeString, textPosition, EditorBuiltin.DefaultFont, Color.LightGray,
  124. EditorStyles.DefaultFontSize);
  125. }
  126. /// <summary>
  127. /// Draws one tick of the timeline, at the specified time.
  128. /// </summary>
  129. /// <param name="t">Time at which to draw the tick.</param>
  130. /// <param name="strength">Strength of the tick (determines size and color), in range [0, 1].</param>
  131. /// <param name="drawText">If true the text displaying the time will be drawn above this tick.</param>
  132. /// <param name="displayAsMinutes">Should the text drawn be displayed as minutes (if true), or seconds (false).
  133. /// Ignored if no text is drawn.</param>
  134. private void DrawTick(float t, float strength, bool drawText, bool displayAsMinutes)
  135. {
  136. int xPos = (int)((t / GetRange()) * drawableWidth) + PADDING;
  137. // Draw tick
  138. Vector2I start = new Vector2I(xPos, height - (int)(tickHeight * strength));
  139. Vector2I end = new Vector2I(xPos, height);
  140. Color color = Color.LightGray;
  141. color.a *= strength;
  142. canvas.DrawLine(start, end, color);
  143. // Draw text if it fits
  144. if (drawText)
  145. DrawTime(xPos, t, displayAsMinutes);
  146. }
  147. /// <summary>
  148. /// Draws a vertical frame marker at the specified time.
  149. /// </summary>
  150. /// <param name="t">Time at which to draw the marker.</param>
  151. private void DrawFrameMarker(float t)
  152. {
  153. int xPos = (int)((t / GetRange()) * drawableWidth) + PADDING;
  154. Vector2I start = new Vector2I(xPos, 0);
  155. Vector2I end = new Vector2I(xPos, height);
  156. canvas.DrawLine(start, end, Color.BansheeOrange);
  157. }
  158. /// <summary>
  159. /// Returns the range of times displayed by the timeline rounded to the multiple of FPS.
  160. /// </summary>
  161. /// <param name="padding">If true, extra range will be included to cover the right-most padding.</param>
  162. /// <returns>Time range rounded to a multiple of FPS.</returns>
  163. private float GetRange(bool padding = false)
  164. {
  165. float spf = 1.0f/fps;
  166. float range = rangeLength;
  167. if (padding)
  168. {
  169. float lengthPerPixel = rangeLength/drawableWidth;
  170. range += lengthPerPixel * PADDING;
  171. }
  172. return ((int)range / spf) * spf;
  173. }
  174. /// <summary>
  175. /// Rebuilds the internal GUI elements. Should be called whenever timeline properties change.
  176. /// </summary>
  177. private void Rebuild()
  178. {
  179. canvas.Clear();
  180. float range = GetRange();
  181. int numTickLevels = tickHandler.NumLevels;
  182. for (int i = numTickLevels - 1; i >= 0; i--)
  183. {
  184. bool drawText = i == 0;
  185. float[] ticks = tickHandler.GetTicks(i);
  186. float strength = tickHandler.GetLevelStrength(i);
  187. if (ticks.Length > 0)
  188. {
  189. float valuePerTick = range/ticks.Length;
  190. bool displayAsMinutes = TimeSpan.FromSeconds(valuePerTick).Minutes > 0;
  191. for (int j = 0; j < ticks.Length; j++)
  192. DrawTick(ticks[j], strength, drawText, displayAsMinutes);
  193. }
  194. }
  195. if (markedFrameIdx != -1)
  196. {
  197. int numFrames = (int)range * fps;
  198. float timePerFrame = range / numFrames;
  199. DrawFrameMarker(markedFrameIdx*timePerFrame);
  200. }
  201. }
  202. }
  203. /** @} */
  204. }