AnimationWindow.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using BansheeEngine;
  4. namespace BansheeEditor
  5. {
  6. /** @addtogroup Windows
  7. * @{
  8. */
  9. /// <summary>
  10. /// Displays animation curve editor window.
  11. /// </summary>
  12. internal class AnimationWindow : EditorWindow
  13. {
  14. private GUITimeline timeline;
  15. private GUICurveDrawing curveDrawing;
  16. private GUIGraphValues sidebar;
  17. private GUIFloatField lengthField;
  18. private GUIIntField fpsField;
  19. private GUIFloatField yRangeField;
  20. private GUILayout buttonLayout;
  21. /// <summary>
  22. /// Opens the animation window.
  23. /// </summary>
  24. [MenuItem("Windows/Animation", ButtonModifier.CtrlAlt, ButtonCode.A, 6000)]
  25. private static void OpenGameWindow()
  26. {
  27. OpenWindow<AnimationWindow>();
  28. }
  29. /// <inheritdoc/>
  30. protected override LocString GetDisplayName()
  31. {
  32. return new LocEdString("Animation");
  33. }
  34. private void OnInitialize()
  35. {
  36. lengthField = new GUIFloatField(new LocEdString("Length"), 50);
  37. fpsField = new GUIIntField(new LocEdString("FPS"), 50);
  38. yRangeField = new GUIFloatField(new LocEdString("Y range"), 50);
  39. lengthField.Value = 60.0f;
  40. fpsField.Value = 1;
  41. yRangeField.Value = 20.0f;
  42. lengthField.OnChanged += x =>
  43. {
  44. timeline.SetRange(lengthField.Value);
  45. curveDrawing.SetRange(lengthField.Value, yRangeField.Value);
  46. };
  47. fpsField.OnChanged += x =>
  48. {
  49. timeline.SetFPS(x);
  50. curveDrawing.SetFPS(x);
  51. };
  52. yRangeField.OnChanged += x =>
  53. {
  54. curveDrawing.SetRange(lengthField.Value, x);
  55. sidebar.SetRange(x * -0.5f, x * 0.5f);
  56. };
  57. GUILayout mainLayout = GUI.AddLayoutY();
  58. buttonLayout = mainLayout.AddLayoutX();
  59. buttonLayout.AddSpace(5);
  60. buttonLayout.AddElement(lengthField);
  61. buttonLayout.AddSpace(5);
  62. buttonLayout.AddElement(yRangeField);
  63. buttonLayout.AddSpace(5);
  64. buttonLayout.AddElement(fpsField);
  65. buttonLayout.AddSpace(5);
  66. timeline = new GUITimeline(mainLayout, Width, 20);
  67. EdAnimationCurve[] curves = CreateDummyCurves();
  68. curveDrawing = new GUICurveDrawing(mainLayout, Width, Height - 20, curves);
  69. curveDrawing.SetRange(60.0f, 20.0f);
  70. GUIPanel sidebarPanel = GUI.AddPanel(-10);
  71. sidebarPanel.SetPosition(0, 20 + buttonLayout.Bounds.height);
  72. sidebar = new GUIGraphValues(sidebarPanel, 30, Height - 20 - buttonLayout.Bounds.height);
  73. curveDrawing.SetSize(Width, Height - 20 - buttonLayout.Bounds.height);
  74. // TODO - Calculate min/max Y and range to set as default
  75. // - Also recalculate whenever curves change and increase as needed
  76. }
  77. private EdAnimationCurve[] CreateDummyCurves()
  78. {
  79. EdAnimationCurve[] curves = new EdAnimationCurve[1];
  80. curves[0] = new EdAnimationCurve();
  81. curves[0].AddKeyframe(0.0f, 1.0f);
  82. curves[0].AddKeyframe(10.0f, 5.0f);
  83. curves[0].AddKeyframe(15.0f, -2.0f);
  84. curves[0].AddKeyframe(20.0f, 3.0f, TangentMode.InStep);
  85. return curves;
  86. }
  87. protected override void WindowResized(int width, int height)
  88. {
  89. timeline.SetSize(width, 20);
  90. curveDrawing.SetSize(width, height - 20 - buttonLayout.Bounds.height);
  91. sidebar.SetSize(30, height - 20 - buttonLayout.Bounds.height);
  92. }
  93. private void OnEditorUpdate()
  94. {
  95. if (Input.IsPointerButtonHeld(PointerButton.Left))
  96. {
  97. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  98. Vector2 curveCoord;
  99. if (curveDrawing.GetCurveCoordinates(windowPos, out curveCoord))
  100. {
  101. Debug.Log("Click coord: " + curveCoord);
  102. }
  103. else
  104. {
  105. int frameIdx = timeline.GetFrame(windowPos);
  106. timeline.SetMarkedFrame(frameIdx);
  107. curveDrawing.SetMarkedFrame(frameIdx);
  108. }
  109. }
  110. }
  111. }
  112. /** @} */
  113. }