GUIGraphValues.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. internal class GUIGraphValues
  11. {
  12. private static readonly Color COLOR_DARK_GRAY = new Color(40.0f / 255.0f, 40.0f / 255.0f, 40.0f / 255.0f, 1.0f);
  13. private GUITicks tickHandler;
  14. private GUICanvas canvas;
  15. private int width = 20;
  16. private int height = 20;
  17. private float rangeStart = -1.0f;
  18. private float rangeEnd = 1.0f;
  19. private float maxTextHeight;
  20. public GUIGraphValues(GUILayout layout, int width, int height)
  21. {
  22. canvas = new GUICanvas();
  23. layout.AddElement(canvas);
  24. tickHandler = new GUITicks();
  25. maxTextHeight = GUIUtility.CalculateTextBounds("99:999", EditorBuiltin.DefaultFont,
  26. EditorStyles.DefaultFontSize).y;
  27. SetSize(width, height);
  28. }
  29. public void SetSize(int width, int height)
  30. {
  31. this.width = width;
  32. this.height = height;
  33. canvas.SetWidth(width);
  34. canvas.SetHeight(height);
  35. tickHandler.SetRange(rangeStart, rangeEnd, height);
  36. Rebuild();
  37. }
  38. public void SetRange(float start, float end)
  39. {
  40. if (start > end)
  41. {
  42. float temp = start;
  43. start = end;
  44. end = temp;
  45. }
  46. rangeStart = start;
  47. rangeEnd = end;
  48. tickHandler.SetRange(rangeStart, rangeEnd, height);
  49. Rebuild();
  50. }
  51. private void DrawTime(int yPos, float seconds, bool minutes)
  52. {
  53. TimeSpan timeSpan = TimeSpan.FromSeconds(seconds);
  54. string timeString;
  55. if (minutes)
  56. timeString = timeSpan.TotalMinutes.ToString("#0") + ":" + timeSpan.Seconds.ToString("D2");
  57. else
  58. timeString = timeSpan.TotalSeconds.ToString("#0.00");
  59. Vector2I textBounds = GUIUtility.CalculateTextBounds(timeString, EditorBuiltin.DefaultFont,
  60. EditorStyles.DefaultFontSize);
  61. Vector2I textPosition = new Vector2I();
  62. textPosition.x = width - textBounds.x;
  63. textPosition.y = yPos - textBounds.y;
  64. canvas.DrawText(timeString, textPosition, EditorBuiltin.DefaultFont, Color.LightGray,
  65. EditorStyles.DefaultFontSize);
  66. }
  67. private void Rebuild()
  68. {
  69. canvas.Clear();
  70. int heightOffset = height/2;
  71. float pixelsPerHeight;
  72. if (rangeEnd != rangeStart)
  73. pixelsPerHeight = height/(rangeEnd - rangeStart);
  74. else
  75. pixelsPerHeight = 0;
  76. int numTickLevels = tickHandler.NumLevels;
  77. for (int i = numTickLevels - 1; i >= 0; i--)
  78. {
  79. float[] ticks = tickHandler.GetTicks(i);
  80. float strength = tickHandler.GetLevelStrength(i);
  81. if (ticks.Length > 0)
  82. {
  83. float valuePerTick = (rangeEnd - rangeStart)/ticks.Length;
  84. bool displayAsMinutes = TimeSpan.FromSeconds(valuePerTick).Minutes > 0;
  85. for (int j = 0; j < ticks.Length; j++)
  86. {
  87. int yPos = (int) (ticks[j]*pixelsPerHeight);
  88. yPos = heightOffset - yPos; // Offset and flip height (canvas Y goes down)
  89. Vector2I start = new Vector2I(0, yPos);
  90. Vector2I end = new Vector2I((int) (width*strength), yPos);
  91. Color color = Color.LightGray;
  92. color.a *= strength;
  93. canvas.DrawLine(start, end, color);
  94. // Draw text for the highest level ticks
  95. if (i == 0)
  96. DrawTime(yPos, ticks[j], displayAsMinutes);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. /** @} */
  103. }