AnimationWindow.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 GUIFloatField lengthField;
  17. private GUIIntField fpsField;
  18. private GUIFloatField yRangeField;
  19. /// <summary>
  20. /// Opens the animation window.
  21. /// </summary>
  22. [MenuItem("Windows/Animation", ButtonModifier.CtrlAlt, ButtonCode.A, 6000)]
  23. private static void OpenGameWindow()
  24. {
  25. OpenWindow<AnimationWindow>();
  26. }
  27. /// <inheritdoc/>
  28. protected override LocString GetDisplayName()
  29. {
  30. return new LocEdString("Animation");
  31. }
  32. private void OnInitialize()
  33. {
  34. lengthField = new GUIFloatField(new LocEdString("Length"), 50);
  35. fpsField = new GUIIntField(new LocEdString("FPS"), 50);
  36. yRangeField = new GUIFloatField(new LocEdString("Y range"), 50);
  37. lengthField.Value = 60.0f;
  38. fpsField.Value = 1;
  39. yRangeField.Value = 20.0f;
  40. lengthField.OnChanged += x =>
  41. {
  42. timeline.SetRange(lengthField.Value);
  43. curveDrawing.SetRange(lengthField.Value, yRangeField.Value);
  44. };
  45. fpsField.OnChanged += x =>
  46. {
  47. timeline.SetFPS(x);
  48. curveDrawing.SetFPS(x);
  49. };
  50. yRangeField.OnChanged += x =>
  51. {
  52. curveDrawing.SetRange(lengthField.Value, x);
  53. };
  54. GUILayout mainLayout = GUI.AddLayoutY();
  55. GUILayout buttonLayout = mainLayout.AddLayoutX();
  56. buttonLayout.AddSpace(5);
  57. buttonLayout.AddElement(lengthField);
  58. buttonLayout.AddSpace(5);
  59. buttonLayout.AddElement(yRangeField);
  60. buttonLayout.AddSpace(5);
  61. buttonLayout.AddElement(fpsField);
  62. buttonLayout.AddSpace(5);
  63. timeline = new GUITimeline(mainLayout, Width, 20);
  64. EdAnimationCurve[] curves = CreateDummyCurves();
  65. curveDrawing = new GUICurveDrawing(mainLayout, Width, Height - 20, curves);
  66. curveDrawing.SetRange(60.0f, 20.0f);
  67. // TODO - Calculate min/max Y and range to set as default
  68. // - Also recalculate whenever curves change and increase as needed
  69. }
  70. private EdAnimationCurve[] CreateDummyCurves()
  71. {
  72. EdAnimationCurve[] curves = new EdAnimationCurve[1];
  73. curves[0] = new EdAnimationCurve();
  74. curves[0].AddKeyframe(0.0f, 1.0f);
  75. curves[0].AddKeyframe(10.0f, 5.0f);
  76. curves[0].AddKeyframe(15.0f, -2.0f);
  77. curves[0].AddKeyframe(20.0f, 3.0f, TangentMode.InStep);
  78. return curves;
  79. }
  80. protected override void WindowResized(int width, int height)
  81. {
  82. timeline.SetSize(width, 20);
  83. curveDrawing.SetSize(width, height - 20);
  84. }
  85. private void OnEditorUpdate()
  86. {
  87. if (Input.IsPointerButtonHeld(PointerButton.Left))
  88. {
  89. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  90. Vector2 curveCoord;
  91. if (curveDrawing.GetCurveCoordinates(windowPos, out curveCoord))
  92. {
  93. Debug.Log("Click coord: " + curveCoord);
  94. }
  95. else
  96. {
  97. int frameIdx = timeline.GetFrame(windowPos);
  98. timeline.SetMarkedFrame(frameIdx);
  99. curveDrawing.SetMarkedFrame(frameIdx);
  100. }
  101. }
  102. }
  103. }
  104. /** @} */
  105. }