AnimationWindow.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using System.Collections.Generic;
  5. using BansheeEngine;
  6. namespace BansheeEditor
  7. {
  8. /** @addtogroup Windows
  9. * @{
  10. */
  11. /// <summary>
  12. /// Displays animation curve editor window.
  13. /// </summary>
  14. internal class AnimationWindow : EditorWindow
  15. {
  16. private GUIFloatField lengthField;
  17. private GUIIntField fpsField;
  18. private GUIFloatField yRangeField;
  19. private GUIButton addKeyframeBtn;
  20. private GUILayout buttonLayout;
  21. private int buttonLayoutHeight;
  22. private GUIPanel editorPanel;
  23. private GUICurveEditor guiCurveEditor;
  24. /// <summary>
  25. /// Opens the animation window.
  26. /// </summary>
  27. [MenuItem("Windows/Animation", ButtonModifier.CtrlAlt, ButtonCode.A, 6000)]
  28. private static void OpenGameWindow()
  29. {
  30. OpenWindow<AnimationWindow>();
  31. }
  32. /// <inheritdoc/>
  33. protected override LocString GetDisplayName()
  34. {
  35. return new LocEdString("Animation");
  36. }
  37. private void OnInitialize()
  38. {
  39. lengthField = new GUIFloatField(new LocEdString("Length"), 50);
  40. fpsField = new GUIIntField(new LocEdString("FPS"), 50);
  41. yRangeField = new GUIFloatField(new LocEdString("Y range"), 50);
  42. addKeyframeBtn = new GUIButton(new LocEdString("Add keyframe"));
  43. lengthField.Value = 60.0f;
  44. fpsField.Value = 1;
  45. yRangeField.Value = 20.0f;
  46. lengthField.OnChanged += x =>
  47. {
  48. guiCurveEditor.SetRange(lengthField.Value, yRangeField.Value);
  49. };
  50. fpsField.OnChanged += x =>
  51. {
  52. guiCurveEditor.SetFPS(x);
  53. };
  54. yRangeField.OnChanged += x =>
  55. {
  56. guiCurveEditor.SetRange(lengthField.Value, yRangeField.Value);
  57. };
  58. addKeyframeBtn.OnClick += () =>
  59. {
  60. guiCurveEditor.AddKeyFrameAtMarker();
  61. };
  62. GUILayout mainLayout = GUI.AddLayoutY();
  63. buttonLayout = mainLayout.AddLayoutX();
  64. buttonLayout.AddSpace(5);
  65. buttonLayout.AddElement(lengthField);
  66. buttonLayout.AddSpace(5);
  67. buttonLayout.AddElement(yRangeField);
  68. buttonLayout.AddSpace(5);
  69. buttonLayout.AddElement(fpsField);
  70. buttonLayout.AddSpace(5);
  71. buttonLayout.AddElement(addKeyframeBtn);
  72. buttonLayout.AddSpace(5);
  73. editorPanel = mainLayout.AddPanel();
  74. buttonLayoutHeight = lengthField.Bounds.height;
  75. guiCurveEditor = new GUICurveEditor(editorPanel, Width, Height - buttonLayoutHeight);
  76. guiCurveEditor.SetCurves(CreateDummyCurves());
  77. guiCurveEditor.Redraw();
  78. }
  79. private EdAnimationCurve[] CreateDummyCurves()
  80. {
  81. EdAnimationCurve[] curves = new EdAnimationCurve[1];
  82. curves[0] = new EdAnimationCurve();
  83. curves[0].AddKeyframe(0.0f, 1.0f);
  84. curves[0].AddKeyframe(10.0f, 5.0f);
  85. curves[0].AddKeyframe(15.0f, -2.0f);
  86. curves[0].AddKeyframe(20.0f, 3.0f, TangentMode.InStep);
  87. return curves;
  88. }
  89. protected override void WindowResized(int width, int height)
  90. {
  91. guiCurveEditor.SetSize(width, height - buttonLayoutHeight);
  92. guiCurveEditor.Redraw();
  93. }
  94. private void OnEditorUpdate()
  95. {
  96. Vector2I windowPos = ScreenToWindowPos(Input.PointerPosition);
  97. Rect2I curveEditorBounds = editorPanel.Bounds;
  98. Vector2I offset = new Vector2I(curveEditorBounds.x, curveEditorBounds.y);
  99. guiCurveEditor.HandleInput(windowPos - offset);
  100. }
  101. }
  102. /** @} */
  103. }