Esempio n. 1
0
        /// <summary> Draws the component [have to be invoked in OnGUI]..</summary>
        /// <param name="rect">X position for the timeline.</param>
        /// <param name="scrollDelta">Scroll delta.</param>
        /// <returns>Real component size.</returns>
        public Rect GUIDraw(float xPos, float scrollDelta)
        {
            base.Draw(_rect, _rectWasChanged);

            _mousePos = Event.current.mousePosition;
            Rect GUITextureSoundRectTemp = GUITextureSoundRect;

            if (_allowDrag && _rect.Contains(Event.current.mousePosition))
            {
                if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
                {
                    _wasMouseDown = true;
                    if (GUITextureSoundRectTemp.Contains(_mousePos))
                    {
                        _diffAtGrab = _mousePos.x - _timeLinePanel.TimeToWorldX((float)_audioClipStartTime);
                    }
                }
                if (_wasMouseDown && Event.current.type == EventType.MouseDrag)
                {
                    _wasMouseDown = false;
                    if (!_dragOutsideAudioClip && GUITextureSoundRectTemp.Contains(_mousePos))
                    {
                        _dragOnAudioClip = true;
                    }
                    else
                    {
                        _dragOutsideAudioClip = true;
                    }
                }

                if (((_wasMouseDown && Event.current.type == EventType.MouseUp) || _dragOutsideAudioClip))
                {
                    _timeLinePanel.TimeLineEngine.SetTime(_timeLinePanel.WorldXToTime(Event.current.mousePosition.x));
                }
            }
            if (Event.current.type == EventType.MouseUp)
            {
                _dragOnAudioClip      = false;
                _dragOutsideAudioClip = false;
                _wasMouseDown         = false;
            }


            if (_soundWavesTexture == null || _durationWasChanged || _rectWasChanged || _lastXPos != xPos || scrollDelta != 0)
            {
                _lastXPos           = xPos;
                _rectWasChanged     = false;
                _durationWasChanged = false;

                int textureWidth  = (int)_rect.width;
                int textureHeight = (int)_rect.height;

                int soundWavesTextureWidth  = textureWidth - BorderThickness * 2;
                int soundWavesTextureHeight = textureHeight - BorderThickness * 2;
                _soundWavesTexture = new Texture2D(soundWavesTextureWidth, soundWavesTextureHeight, TextureFormat.ARGB32, false);
                bool valid = true;

                Rect textureSoundRect = TextureSoundRect;
                if (_audioClip == null || textureSoundRect.width == 0)
                {
                    valid = false;
                }

                Color[] soundWavesBackgroundColors;
                soundWavesBackgroundColors = new Color[soundWavesTextureWidth * soundWavesTextureHeight];
                soundWavesBackgroundColors.Fill(NormalColor);
                _soundWavesTexture.SetPixels(0, 0, soundWavesTextureWidth, soundWavesTextureHeight, soundWavesBackgroundColors);

                if (valid)
                {
                    soundWavesBackgroundColors = new Color[(int)(textureSoundRect.width * textureSoundRect.height)];
                    soundWavesBackgroundColors.Fill(new Color32(211, 211, 211, 255));

                    _soundWavesTexture.SetPixels((int)textureSoundRect.x, (int)textureSoundRect.y, (int)textureSoundRect.width, (int)textureSoundRect.height, soundWavesBackgroundColors);
                    if (_audioClip != null)
                    {
                        AudioUtils.DoRenderPreview(_audioClip, _soundWavesTexture, _timeLinePanel.TimeLineEngine, _lastXPos, _audioClipStartTime);
                    }
                    else
                    {
                        _soundWavesTexture.Apply();
                    }
                }
                else
                {
                    _soundWavesTexture.Apply();
                }

                _soundWavesTextureStyle.normal.background = _soundWavesTexture;
                // refresh
                SetTimeFromSec((float)AudioClipStartTime.TotalSeconds);
            }

            GUI.Box(new Rect(_rect.x + BorderThickness, _rect.y + BorderThickness, _rect.width - BorderThickness * 2, _rect.height - BorderThickness * 2), GUIContent.none, _soundWavesTextureStyle);
            if (_dragOnAudioClip)
            {
                EditorGUIUtility.AddCursorRect(GUITextureSoundRectTemp, MouseCursor.Pan);
            }
            return(_controlRect);
        }