CurveEditorWindow.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. //********************************** Banshee Engine (www.banshee5d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. using System;
  4. using BansheeEngine;
  5. namespace BansheeEditor
  6. {
  7. /// <summary>
  8. /// Opens a window that allows the user to edit a single animation curve or two curves representing a range.
  9. /// </summary>
  10. public class CurveEditorWindow : ModalWindow
  11. {
  12. private EdAnimationCurve curveA;
  13. private EdAnimationCurve curveB;
  14. private GUICurveEditor curveEditor;
  15. private GUIButton guiOK;
  16. private GUIButton guiCancel;
  17. private Action<bool, AnimationCurve, AnimationCurve> closedCallbackRange;
  18. private Action<bool, AnimationCurve> closedCallback;
  19. /// <summary>
  20. /// Shows the curve editor window that allows the user to edit a single curve.
  21. /// </summary>
  22. /// <param name="curve">Curve to initialize the window with.</param>
  23. /// <param name="closedCallback">Optional callback to trigger when the user finishes editing the curve or
  24. /// cancels out of the dialog.</param>
  25. /// <returns>An instance of the curve editor window.</returns>
  26. public static CurveEditorWindow Show(AnimationCurve curve, Action<bool, AnimationCurve> closedCallback = null)
  27. {
  28. CurveEditorWindow picker = new CurveEditorWindow(curve, closedCallback);
  29. return picker;
  30. }
  31. /// <summary>
  32. /// Shows the curve editor window that allows the user to edit a curve range (two curves).
  33. /// </summary>
  34. /// <param name="curveA">First curve of the range to display/edit.</param>
  35. /// <param name="curveB">Second curve of the range to display/edit.</param>
  36. /// <param name="closedCallback">Optional callback to trigger when the user finishes editing the curve or
  37. /// cancels out of the dialog.</param>
  38. /// <returns>An instance of the curve editor window.</returns>
  39. public static CurveEditorWindow Show(AnimationCurve curveA, AnimationCurve curveB,
  40. Action<bool, AnimationCurve, AnimationCurve> closedCallback = null)
  41. {
  42. CurveEditorWindow picker = new CurveEditorWindow(curveA, curveB, closedCallback);
  43. return picker;
  44. }
  45. #region Overrides
  46. private CurveEditorWindow(AnimationCurve curve, Action<bool, AnimationCurve> closedCallback = null)
  47. : base(false)
  48. {
  49. Title = new LocString("Curve editor");
  50. Width = 600;
  51. Height = 460;
  52. curveA = new EdAnimationCurve(curve ?? new AnimationCurve(new KeyFrame[] {}), null);
  53. this.closedCallback = closedCallback;
  54. }
  55. private CurveEditorWindow(AnimationCurve curveA, AnimationCurve curveB,
  56. Action<bool, AnimationCurve, AnimationCurve> closedCallback = null)
  57. : base(false)
  58. {
  59. Title = new LocString("Curve editor");
  60. Width = 600;
  61. Height = 460;
  62. this.curveA = new EdAnimationCurve(curveA ?? new AnimationCurve(new KeyFrame[] {}), null);
  63. this.curveB = new EdAnimationCurve(curveB ?? new AnimationCurve(new KeyFrame[] {}), null);
  64. this.closedCallbackRange = closedCallback;
  65. }
  66. private void OnInitialize()
  67. {
  68. GUILayout vertLayout = GUI.AddLayoutY();
  69. GUILayout editorPanel = vertLayout.AddPanel(GUIOption.FixedHeight(400));
  70. GUILayout buttonLayout = vertLayout.AddLayoutX(GUIOption.FixedHeight(40));
  71. guiOK = new GUIButton(new LocEdString("OK"));
  72. guiCancel = new GUIButton(new LocEdString("Cancel"));
  73. guiOK.OnClick += OnOK;
  74. guiCancel.OnClick += OnCancel;
  75. CurveDrawOptions drawOptions = CurveDrawOptions.DrawKeyframes | CurveDrawOptions.DrawMarkers;
  76. if (curveB != null)
  77. drawOptions |= CurveDrawOptions.DrawRange;
  78. curveEditor = new GUICurveEditor(editorPanel, 600, 400, false, drawOptions);
  79. curveEditor.Redraw();
  80. CurveDrawInfo[] drawinfo;
  81. if (curveB != null)
  82. {
  83. drawinfo = new []
  84. {
  85. new CurveDrawInfo(curveA, Color.BansheeOrange),
  86. new CurveDrawInfo(curveB, Color.Green),
  87. };
  88. }
  89. else
  90. {
  91. drawinfo = new [] { new CurveDrawInfo(curveA, Color.BansheeOrange), };
  92. }
  93. curveEditor.SetCurves(drawinfo);
  94. curveEditor.CenterAndResize(true);
  95. buttonLayout.AddFlexibleSpace();
  96. buttonLayout.AddElement(guiOK);
  97. buttonLayout.AddSpace(10);
  98. buttonLayout.AddElement(guiCancel);
  99. buttonLayout.AddFlexibleSpace();
  100. EditorInput.OnPointerPressed += OnPointerPressed;
  101. EditorInput.OnPointerDoubleClick += OnPointerDoubleClicked;
  102. EditorInput.OnPointerMoved += OnPointerMoved;
  103. EditorInput.OnPointerReleased += OnPointerReleased;
  104. EditorInput.OnButtonUp += OnButtonUp;
  105. }
  106. private void OnEditorUpdate()
  107. {
  108. curveEditor.HandleDragAndZoomInput();
  109. }
  110. private void OnDestroy()
  111. {
  112. EditorInput.OnPointerPressed -= OnPointerPressed;
  113. EditorInput.OnPointerDoubleClick -= OnPointerDoubleClicked;
  114. EditorInput.OnPointerMoved -= OnPointerMoved;
  115. EditorInput.OnPointerReleased -= OnPointerReleased;
  116. EditorInput.OnButtonUp -= OnButtonUp;
  117. }
  118. #endregion
  119. #region GUI
  120. /// <summary>
  121. /// Triggered when the user finishes editing the curve(s) and closes the dialog.
  122. /// </summary>
  123. void OnOK()
  124. {
  125. if (curveB != null)
  126. closedCallbackRange?.Invoke(true, curveA.Normal, curveB.Normal);
  127. else
  128. closedCallback?.Invoke(true, curveA.Normal);
  129. Close();
  130. }
  131. /// <summary>
  132. /// Triggered when the user cancels editing the curve(s) closes the dialog.
  133. /// </summary>
  134. void OnCancel()
  135. {
  136. if (curveB != null)
  137. closedCallbackRange?.Invoke(false, curveA.Normal, curveB.Normal);
  138. else
  139. closedCallback?.Invoke(false, curveA.Normal);
  140. Close();
  141. }
  142. #endregion
  143. #region Input callbacks
  144. /// <summary>
  145. /// Triggered when the user presses a mouse button.
  146. /// </summary>
  147. /// <param name="ev">Information about the mouse press event.</param>
  148. private void OnPointerPressed(PointerEvent ev)
  149. {
  150. curveEditor.OnPointerPressed(ev);
  151. }
  152. /// <summary>
  153. /// Triggered when the user double clicks the left mouse button.
  154. /// </summary>
  155. /// <param name="ev">Information about the mouse event.</param>
  156. private void OnPointerDoubleClicked(PointerEvent ev)
  157. {
  158. curveEditor.OnPointerDoubleClicked(ev);
  159. }
  160. /// <summary>
  161. /// Triggered when the user moves the mouse.
  162. /// </summary>
  163. /// <param name="ev">Information about the mouse move event.</param>
  164. private void OnPointerMoved(PointerEvent ev)
  165. {
  166. curveEditor.OnPointerMoved(ev);
  167. }
  168. /// <summary>
  169. /// Triggered when the user releases a mouse button.
  170. /// </summary>
  171. /// <param name="ev">Information about the mouse release event.</param>
  172. private void OnPointerReleased(PointerEvent ev)
  173. {
  174. curveEditor.OnPointerReleased(ev);
  175. }
  176. /// <summary>
  177. /// Triggered when the user releases a keyboard button.
  178. /// </summary>
  179. /// <param name="ev">Information about the keyboard release event.</param>
  180. private void OnButtonUp(ButtonEvent ev)
  181. {
  182. curveEditor.OnButtonUp(ev);
  183. }
  184. #endregion
  185. }
  186. /** @} */
  187. }