GUIGraphValues.cs 4.4 KB

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