GUITimeline.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. // TODO DOC
  11. public class GUITimeline
  12. {
  13. private const float LARGE_TICK_HEIGHT_PCT = 0.5f;
  14. private const float SMALL_TICK_HEIGHT_PCT = 0.25f;
  15. private GUICanvas canvas;
  16. private int width;
  17. private int height;
  18. private float rangeStart = 0.0f;
  19. private float rangeEnd = 60.0f;
  20. private int fps = 60;
  21. public GUITimeline(GUILayout layout, int width, int height)
  22. {
  23. canvas = new GUICanvas();
  24. layout.AddElement(canvas);
  25. SetSize(width, height);
  26. }
  27. public void SetSize(int width, int height)
  28. {
  29. this.width = width;
  30. this.height = height;
  31. canvas.SetWidth(width);
  32. canvas.SetHeight(height);
  33. Rebuild();
  34. }
  35. public void SetRange(float start, float end)
  36. {
  37. rangeStart = start;
  38. rangeEnd = end;
  39. Rebuild();
  40. }
  41. public void SetFPS(int fps)
  42. {
  43. this.fps = fps;
  44. Rebuild();
  45. }
  46. private void Rebuild()
  47. {
  48. canvas.Clear();
  49. canvas.DrawLine(new Vector2I(50, 20), new Vector2I(50, 40), Color.White);
  50. canvas.DrawLine(new Vector2I(100, 20), new Vector2I(100, 40), Color.White);
  51. return;
  52. // TODO - Calculate interval sizes based on set range, width and FPS
  53. // - Dynamically change tick heights?
  54. // - Draw text (and convert seconds to minutes/hours as needed)
  55. float largeTickInterval = 50.0f; // TODO - Must be multiple of small tick interval
  56. float smallTickInterval = 10.0f; // TODO
  57. float offsetLarge = MathEx.CeilToInt(rangeStart / largeTickInterval) * largeTickInterval - rangeStart;
  58. float offsetSmall = MathEx.CeilToInt(rangeStart / smallTickInterval) * smallTickInterval - rangeStart;
  59. int largeTickHeight = (int)(height * LARGE_TICK_HEIGHT_PCT);
  60. int smallTickHeight = (int)(height * SMALL_TICK_HEIGHT_PCT);
  61. bool drawSmallTicks = true; // TODO
  62. float length = rangeEnd - rangeStart;
  63. for (float t = offsetSmall; t <= length; t += smallTickInterval)
  64. {
  65. Debug.Log(t + " - " + length + " - " + width);
  66. float distanceToLargeTick = MathEx.CeilToInt(t / largeTickInterval) * largeTickInterval - t;
  67. if (MathEx.ApproxEquals(distanceToLargeTick, 0.0f))
  68. {
  69. int xPos = (int)((t/length)*width);
  70. Vector2I start = new Vector2I(xPos, height - largeTickHeight);
  71. Vector2I end = new Vector2I(xPos, height);
  72. canvas.DrawLine(start, end, Color.DarkGray);
  73. TimeSpan intervalSpan = TimeSpan.FromSeconds(largeTickInterval);
  74. TimeSpan timeSpan = TimeSpan.FromSeconds(rangeStart + t);
  75. string timeString;
  76. if(intervalSpan.Minutes > 0)
  77. timeString = timeSpan.ToString(@"m\:ss");
  78. else
  79. timeString = timeSpan.ToString(@"ss\:fff");
  80. Vector2I textBounds = GUIUtility.CalculateTextBounds(timeString, Builtin.DefaultFont,
  81. EditorStyles.DefaultFontSize);
  82. Vector2I textPosition = new Vector2I();
  83. textPosition.x = -textBounds.x/2;
  84. //canvas.DrawText(timeString, textPosition, Builtin.DefaultFont, Color.DarkGray,
  85. // EditorStyles.DefaultFontSize);
  86. }
  87. else
  88. {
  89. if (drawSmallTicks)
  90. {
  91. int xPos = (int)((t / length) * width);
  92. Vector2I start = new Vector2I(xPos, height - smallTickHeight);
  93. Vector2I end = new Vector2I(xPos, height);
  94. canvas.DrawLine(start, end, Color.LightGray);
  95. }
  96. }
  97. }
  98. }
  99. }
  100. /** @} */
  101. }