예제 #1
0
        /// <summary>  Draws only a GUI elements.  </summary>
        /// <remarks> Needs to be invoked only in OnGUI</remarks>
        public virtual void DrawGUI()
        {
            if (_oldGUIRect != GUIRect)
            {
                _rebuildConcent = true;
                _oldGUIRect     = GUIRect;
            }
            if (_contentStyle == null)
            {
                _contentStyle = new GUIStyle(GUI.skin.label);
            }

            if (_startOld != SourceData.Start)
            {
                _startOld       = SourceData.Start;
                _startOldString = _startOld.ToStringSpecial();
                _rebuildConcent = true;
            }
            if (_endOld != SourceData.End)
            {
                _endOld         = SourceData.End;
                _endOldString   = _endOld.ToStringSpecial();
                _rebuildConcent = true;
            }
            if (_oldName != SourceData.Name)
            {
                _oldName        = SourceData.Name;
                _rebuildConcent = true;
            }
            Rect rect = TextureRect;

            if (_rebuildConcent)
            {
                string        fromTo  = "From " + _startOldString + " to " + _endOldString;
                StringBuilder newText = new StringBuilder(SourceData.Name);
                _content = new GUIContent(SourceData.Name, fromTo);

                _contentSize = _contentStyle.CalcSize(_content);
                while (_contentSize.x > rect.width - _borderThickness)
                {
                    newText.Remove(newText.Length - 1, 1);
                    if (newText.Length > 0)
                    {
                        _content = new GUIContent(newText + "...", SourceData.Name + Environment.NewLine + fromTo);
                    }
                    else
                    {
                        _content     = new GUIContent(string.Empty, SourceData.Name + Environment.NewLine + fromTo);
                        _contentSize = rect.size;
                        break;
                    }

                    _contentSize    = _contentStyle.CalcSize(_content);
                    _rebuildConcent = false;
                }
            }



            // label
            GUI.Label(new Rect(rect.x + rect.width / 2 - _contentSize.x / 2, Owner.Rect.y + rect.height / 2 - _contentSize.y / 2 + 1, _contentSize.x, _contentSize.y), _content, _contentStyle);

            // context menu

            _mousePos = Event.current.mousePosition;


            bool GUIContainsMouse = _oldGUIRect.Contains(_mousePos);

            if (Event.current.type == EventType.ContextClick && GUIContainsMouse)
            {
                GenericMenu menu = new GenericMenu();
                menu.AddItem(new GUIContent("Edit"), false, ContextEditCallback, null);
                menu.AddItem(new GUIContent("Remove"), false, ContextRemoveCallback, null);
                menu.AddSeparator("");
                menu.AddItem(new GUIContent("Copy"), false, ContextCopyCallback, null);
                menu.AddItem(new GUIContent("Cut"), false, ContextCutCallback, null);
                if (Owner is ILineCopy && (Owner as ILineCopy).CopyDataAvailable)
                {
                    menu.AddItem(new GUIContent("Paste"), false, ContextPasteCallback, null);
                }
                else
                {
                    menu.AddItem(new GUIContent("Paste"), false, null, null);
                }
                // menu.AddItem(new GUIContent("SubMenu/MenuItem3"), false, Callback, "item 3");
                menu.ShowAsContext();
                Event.current.Use();
            }

            // single click handle
            if (Event.current.isMouse && Event.current.type == EventType.MouseDown && GUIContainsMouse)
            {
                Select();
                // double click handle
                if (Event.current.clickCount == 2)
                {
                    _mousePressedOnCenter    = false;
                    _mousePressedOnLeftEdge  = false;
                    _mousePressedOnRightEdge = false;
                    ContextEditCallback(null);
                    return;
                }
            }
            if (_isSelected)
            {
                EditorGUIUtility.AddCursorRect(_mousePressedOnLeftEdge ? _leftEdgeRectSecond : _leftEdgeRect, MouseCursor.SplitResizeLeftRight);
                EditorGUIUtility.AddCursorRect(_mousePressedOnRightEdge ? _rightEdgeRectSecond : _rightEdgeRect, MouseCursor.SplitResizeLeftRight);
                EditorGUIUtility.AddCursorRect(_centerRect, MouseCursor.Pan);

                bool mouseOnLeftEdge  = _mousePressedOnLeftEdge ? _leftEdgeRectSecond.Contains(_mousePos) : _leftEdgeRect.Contains(_mousePos);
                bool mouseOnRightEdge = _mousePressedOnRightEdge ? _rightEdgeRectSecond.Contains(_mousePos) : _rightEdgeRect.Contains(_mousePos);
                bool mouseOnCenter    = _centerRect.Contains(_mousePos);

                if (Event.current.type == EventType.MouseDown && mouseOnLeftEdge)
                {
                    _mousePressedOnLeftEdge = true;
                    _diffAtGrab             = _mousePos.x - _timeLinePanel.TimeToWorldX(SourceData.Start);
                }
                else if (Event.current.type == EventType.MouseDown && mouseOnRightEdge)
                {
                    _mousePressedOnRightEdge = true;
                    _diffAtGrab = _timeLinePanel.TimeToWorldX(SourceData.End) - _mousePos.x;
                }
                else if (Event.current.type == EventType.MouseDown && mouseOnCenter)
                {
                    _mousePressedOnCenter = true;
                    _diffAtGrab           = _mousePos.x - _timeLinePanel.TimeToWorldX(SourceData.Start);
                }
                else if (Event.current.type == EventType.MouseUp)
                {
                    _mousePressedOnLeftEdge  = false;
                    _mousePressedOnRightEdge = false;
                    _mousePressedOnCenter    = false;
                }
            }
            else
            {
                _mousePressedOnCenter    = false;
                _mousePressedOnLeftEdge  = false;
                _mousePressedOnRightEdge = false;
            }
        }
예제 #2
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);
        }