GUIGraphTime.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. /// 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 : GUITimelineBase
  15. {
  16. private const float TICK_HEIGHT_PCT = 0.4f;
  17. private const int TEXT_PADDING = 2;
  18. private GUIGraphTicks tickHandler;
  19. /// <summary>
  20. /// Constructs a new timeline and adds it to the specified layout.
  21. /// </summary>
  22. /// <param name="layout">Layout to add the timeline GUI to.</param>
  23. /// <param name="width">Width of the timeline in pixels.</param>
  24. /// <param name="height">Height of the timeline in pixels.</param>
  25. public GUIGraphTime(GUILayout layout, int width, int height)
  26. :base(layout, width, height)
  27. {
  28. tickHandler = new GUIGraphTicks(GUITickStepType.Time);
  29. }
  30. /// <summary>
  31. /// Draws text displaying the time at the provided position.
  32. /// </summary>
  33. /// <param name="xPos">Position to draw the text at.</param>
  34. /// <param name="seconds">Time to display, in seconds.</param>
  35. /// <param name="minutes">If true the time will be displayed in minutes, otherwise in seconds.</param>
  36. private void DrawTime(int xPos, float seconds, bool minutes)
  37. {
  38. TimeSpan timeSpan = TimeSpan.FromSeconds(seconds);
  39. string timeString;
  40. if (minutes)
  41. timeString = timeSpan.TotalMinutes.ToString("#0") + ":" + timeSpan.Seconds.ToString("D2");
  42. else
  43. timeString = timeSpan.TotalSeconds.ToString("#0.00");
  44. Vector2I textBounds = GUIUtility.CalculateTextBounds(timeString, EditorBuiltin.DefaultFont,
  45. EditorStyles.DefaultFontSize);
  46. Vector2I textPosition = new Vector2I();
  47. textPosition.x = xPos - textBounds.x / 2;
  48. textPosition.y = TEXT_PADDING;
  49. canvas.DrawText(timeString, textPosition, EditorBuiltin.DefaultFont, Color.LightGray,
  50. EditorStyles.DefaultFontSize);
  51. }
  52. /// <summary>
  53. /// Draws one tick of the timeline, at the specified time.
  54. /// </summary>
  55. /// <param name="t">Time at which to draw the tick.</param>
  56. /// <param name="strength">Strength of the tick (determines size and color), in range [0, 1].</param>
  57. /// <param name="drawText">If true the text displaying the time will be drawn above this tick.</param>
  58. /// <param name="displayAsMinutes">Should the text drawn be displayed as minutes (if true), or seconds (false).
  59. /// Ignored if no text is drawn.</param>
  60. private void DrawTick(float t, float strength, bool drawText, bool displayAsMinutes)
  61. {
  62. int xPos = (int)(((t - rangeOffset) / GetRange()) * drawableWidth) + PADDING;
  63. // Draw tick
  64. float tickHeight = (int)(height * TICK_HEIGHT_PCT);
  65. Vector2I start = new Vector2I(xPos, height - (int)(tickHeight * strength));
  66. Vector2I end = new Vector2I(xPos, height);
  67. Color color = Color.LightGray;
  68. color.a *= strength;
  69. canvas.DrawLine(start, end, color);
  70. // Draw text if it fits
  71. if (drawText)
  72. DrawTime(xPos, t, displayAsMinutes);
  73. }
  74. /// <inheritdoc/>
  75. public override void Rebuild()
  76. {
  77. canvas.Clear();
  78. tickHandler.SetRange(rangeOffset, rangeOffset + GetRange(true), drawableWidth + PADDING);
  79. float range = GetRange();
  80. int numTickLevels = tickHandler.NumLevels;
  81. for (int i = numTickLevels - 1; i >= 0; i--)
  82. {
  83. bool drawText = i == 0;
  84. float[] ticks = tickHandler.GetTicks(i);
  85. float strength = tickHandler.GetLevelStrength(i);
  86. if (ticks.Length > 0)
  87. {
  88. float valuePerTick = range/ticks.Length;
  89. bool displayAsMinutes = TimeSpan.FromSeconds(valuePerTick).Minutes > 0;
  90. for (int j = 0; j < ticks.Length; j++)
  91. DrawTick(ticks[j], strength, drawText, displayAsMinutes);
  92. }
  93. }
  94. DrawFrameMarker();
  95. }
  96. }
  97. /** @} */
  98. }