GUIGraphTime.cs 9.6 KB

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