FPlayAudioEventEditor.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using UnityEngine;
  2. using UnityEditor;
  3. using UnityEditorInternal;
  4. using Flux;
  5. namespace FluxEditor
  6. {
  7. [FEditor(typeof(FPlayAudioEvent))]
  8. public class FPlayAudioEventEditor : FEventEditor {
  9. private float[] _clipMinMaxData = null;
  10. private FPlayAudioEvent _playAudioEvt = null;
  11. private AudioClip _currentClip = null;
  12. private SerializedProperty _startOffset;
  13. private int _frameMouseDown = int.MaxValue;
  14. public override float Height {
  15. get {
  16. return base.Height+base.Height;
  17. }
  18. }
  19. public override void Init( FObject obj, FEditor owner )
  20. {
  21. base.Init( obj, owner);
  22. _playAudioEvt = (FPlayAudioEvent)obj;
  23. _startOffset = new SerializedObject( _playAudioEvt ).FindProperty( "_startOffset" );
  24. }
  25. protected override void RenderEvent( FrameRange viewRange, FrameRange validKeyframeRange )
  26. {
  27. int startOffsetHandleId = EditorGUIUtility.GetControlID(FocusType.Passive);
  28. switch( Event.current.type )
  29. {
  30. case EventType.MouseDown:
  31. if( Event.current.alt && EditorGUIUtility.hotControl == 0 && _eventRect.Contains(Event.current.mousePosition) )
  32. {
  33. EditorGUIUtility.hotControl = startOffsetHandleId;
  34. _frameMouseDown = SequenceEditor.GetFrameForX( Event.current.mousePosition.x ) - _playAudioEvt.Start;
  35. Event.current.Use();
  36. }
  37. break;
  38. case EventType.MouseDrag:
  39. if( EditorGUIUtility.hotControl == startOffsetHandleId )
  40. {
  41. int mouseCurrentFrame = Mathf.Clamp( SequenceEditor.GetFrameForX( Event.current.mousePosition.x ) - _playAudioEvt.Start, 0, _playAudioEvt.Length );
  42. int mouseDelta = _frameMouseDown - mouseCurrentFrame;
  43. if( mouseDelta != 0 )
  44. {
  45. _frameMouseDown = mouseCurrentFrame;
  46. _startOffset.serializedObject.Update();
  47. _startOffset.intValue = Mathf.Clamp( _startOffset.intValue + mouseDelta, 0, _playAudioEvt.GetMaxStartOffset() );
  48. _startOffset.serializedObject.ApplyModifiedProperties();
  49. }
  50. Event.current.Use();
  51. }
  52. break;
  53. case EventType.MouseUp:
  54. if( EditorGUIUtility.hotControl == startOffsetHandleId )
  55. {
  56. EditorGUIUtility.hotControl = 0;
  57. Event.current.Use();
  58. }
  59. break;
  60. }
  61. int frameRangeLength = _playAudioEvt.Length;
  62. base.RenderEvent( viewRange, validKeyframeRange );
  63. if( frameRangeLength != _playAudioEvt.Length )
  64. {
  65. _startOffset.serializedObject.Update();
  66. _startOffset.intValue = Mathf.Clamp( _startOffset.intValue, 0, _playAudioEvt.GetMaxStartOffset() );
  67. _startOffset.serializedObject.ApplyModifiedProperties();
  68. }
  69. if( _currentClip != _playAudioEvt.AudioClip || _clipMinMaxData == null )
  70. {
  71. _clipMinMaxData = FUtility.GetAudioMinMaxData(_playAudioEvt.AudioClip);
  72. _currentClip = _playAudioEvt.AudioClip;
  73. }
  74. if( Event.current.type == EventType.Repaint && _clipMinMaxData != null )
  75. {
  76. float border = 2;
  77. Rect waveformRect = _eventRect;
  78. waveformRect.xMin += border;
  79. waveformRect.xMax -= border;
  80. FrameRange visibleEvtRange = _playAudioEvt.FrameRange;
  81. float startOffset = _playAudioEvt.StartOffset;
  82. if( viewRange.Start > visibleEvtRange.Start )
  83. {
  84. startOffset += viewRange.Start - visibleEvtRange.Start;
  85. visibleEvtRange.Start = viewRange.Start;
  86. }
  87. if( viewRange.End < visibleEvtRange.End )
  88. {
  89. visibleEvtRange.End = viewRange.End;
  90. }
  91. int numChannels = _currentClip.channels;
  92. int numSamples = _clipMinMaxData.Length / (2 * numChannels);
  93. Rect fullWaveformRect = _eventRect;
  94. fullWaveformRect.width = SequenceEditor.PixelsPerFrame*_currentClip.length*SequenceEditor.Sequence.FrameRate;
  95. GUI.BeginClip(waveformRect);
  96. fullWaveformRect.yMin -= 5;
  97. fullWaveformRect.yMax += 15;
  98. fullWaveformRect.x = -startOffset*SequenceEditor.PixelsPerFrame;
  99. AudioCurveRendering.DrawMinMaxFilledCurve( fullWaveformRect, delegate(float x, out Color col, out float minValue, out float maxValue) {
  100. col = new Color(1f, 0.55f, 0f, 1f);
  101. float p = Mathf.Clamp(x * (numSamples - 2), 0.0f, numSamples - 2);
  102. int i = (int)Mathf.Floor(p);
  103. minValue = 10;
  104. maxValue = -10;
  105. for( int channel = 0; channel != numChannels; ++channel )
  106. {
  107. int offset1 = (i * numChannels + channel) * 2;
  108. int offset2 = offset1 + numChannels * 2;
  109. minValue = Mathf.Min(minValue, _clipMinMaxData[offset1 + 1], _clipMinMaxData[offset2 + 1]);
  110. maxValue = Mathf.Max(maxValue, _clipMinMaxData[offset1 + 0], _clipMinMaxData[offset2 + 0]);
  111. if (minValue > maxValue)
  112. {
  113. float tmp = minValue;
  114. minValue = maxValue;
  115. maxValue = tmp;
  116. }
  117. }
  118. });
  119. GUI.EndClip();
  120. }
  121. }
  122. }
  123. }