private void DrawPoint(int index, CurvePoint point)
        {
            Handles.color = point.Mode.GetPointColor();
            var handle         = point.GlobalPosition;
            var size           = HandleUtility.GetHandleSize(handle);
            var handleRotation = Tools.pivotRotation == PivotRotation.Local ?
                                 point.transform.rotation : Quaternion.identity;

            if (Handles.Button(handle, point.transform.rotation, size * HANDLE_SIZE, size * PICK_SIZE, Handles.DotHandleCap))
            {
                _selectedIndex   = index;
                _selectedTangent = TangentPoint.None;
                Repaint();
            }

            if (_selectedIndex == index && _selectedTangent == TangentPoint.None)
            {
                EditorGUI.BeginChangeCheck();
                handle = Handles.DoPositionHandle(handle, handleRotation);

                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(_curve, "Move point position");
                    EditorUtility.SetDirty(_curve);
                    point.Position = point.transform.InverseTransformPoint(handle);
                }
            }
        }
        private Vector3 DrawTangentPoint(int index, TangentPoint tangentPoint)
        {
            var point          = _curve.points[index];
            var handle         = point.GetTangent(tangentPoint) + point.GlobalPosition;
            var size           = HandleUtility.GetHandleSize(handle);
            var handleRotation = Tools.pivotRotation == PivotRotation.Local ?
                                 point.transform.rotation : Quaternion.identity;

            Handles.color = Color.gray;
            Handles.DrawLine(point.GlobalPosition, handle);

            Handles.color = point.Mode.GetTangentColor();
            if (point.Mode != CurveMode.StraightLine &&
                Handles.Button(handle, handleRotation, size * HANDLE_SIZE, size * PICK_SIZE, Handles.DotHandleCap))
            {
                _selectedIndex   = index;
                _selectedTangent = tangentPoint;
                Repaint();
            }

            if (_selectedIndex == index && _selectedTangent == tangentPoint)
            {
                EditorGUI.BeginChangeCheck();
                handle = Handles.DoPositionHandle(handle, handleRotation);

                if (EditorGUI.EndChangeCheck())
                {
                    Undo.RecordObject(_curve, "Move tangent point");
                    EditorUtility.SetDirty(_curve);
                    point.SetTangent(tangentPoint, handle - point.GlobalPosition);
                    point.EnforceTangents(tangentPoint);
                }
            }

            return(handle);
        }
Example #3
0
            /// <inheritdoc />
            public override bool OnMouseDown(Vector2 location, MouseButton button)
            {
                if (base.OnMouseDown(location, button))
                {
                    // Clear flags
                    _isMovingSelection = false;
                    _isMovingTangent   = false;
                    _rightMouseDown    = false;
                    _leftMouseDown     = false;
                    return(true);
                }

                // Cache data
                _isMovingSelection = false;
                _isMovingTangent   = false;
                _mousePos          = location;
                if (button == MouseButton.Left)
                {
                    _leftMouseDown    = true;
                    _leftMouseDownPos = location;
                }
                if (button == MouseButton.Right)
                {
                    _rightMouseDown    = true;
                    _rightMouseDownPos = location;
                    _movingViewLastPos = location;
                }

                // Check if any node is under the mouse
                var underMouse = GetChildAt(location);

                if (underMouse is KeyframePoint keyframe)
                {
                    if (_leftMouseDown)
                    {
                        // Check if user is pressing control
                        if (Root.GetKey(KeyboardKeys.Control))
                        {
                            // Add to selection
                            keyframe.IsSelected = true;
                            _editor.UpdateTangents();
                        }
                        // Check if node isn't selected
                        else if (!keyframe.IsSelected)
                        {
                            // Select node
                            _editor.ClearSelection();
                            keyframe.IsSelected = true;
                            _editor.UpdateTangents();
                        }

                        // Start moving selected nodes
                        StartMouseCapture();
                        _isMovingSelection = true;
                        _movedKeyframes    = false;
                        var viewRect = _editor._mainPanel.GetClientArea();
                        _movingSelectionStart = PointToKeyframes(location, ref viewRect);
                        if (_movingSelectionOffsets == null || _movingSelectionOffsets.Length != _editor._points.Count)
                        {
                            _movingSelectionOffsets = new Vector2[_editor._points.Count];
                        }
                        for (int i = 0; i < _movingSelectionOffsets.Length; i++)
                        {
                            _movingSelectionOffsets[i] = _editor._points[i].Point - _movingSelectionStart;
                        }
                        _editor.OnEditingStart();
                        Focus();
                        Tooltip?.Hide();
                        return(true);
                    }
                }
                else if (underMouse is TangentPoint tangent && tangent.Visible)
                {
                    if (_leftMouseDown)
                    {
                        // Start moving tangent
                        StartMouseCapture();
                        _isMovingTangent = true;
                        _movedKeyframes  = false;
                        _movingTangent   = tangent;
                        _editor.OnEditingStart();
                        Focus();
                        Tooltip?.Hide();
                        return(true);
                    }
                }
            /// <inheritdoc />
            public override bool OnMouseDown(Vector2 location, MouseButton button)
            {
                if (base.OnMouseDown(location, button))
                {
                    // Clear flags
                    _isMovingSelection = false;
                    _isMovingTangent   = false;
                    _rightMouseDown    = false;
                    _leftMouseDown     = false;
                    return(true);
                }

                // Cache data
                _isMovingSelection = false;
                _isMovingTangent   = false;
                _mousePos          = location;
                if (button == MouseButton.Left)
                {
                    _leftMouseDown    = true;
                    _leftMouseDownPos = location;
                }
                if (button == MouseButton.Right)
                {
                    _rightMouseDown    = true;
                    _rightMouseDownPos = location;
                    _movingViewLastPos = location;
                }

                // Check if any node is under the mouse
                var underMouse = GetChildAt(location);

                if (underMouse is KeyframePoint keyframe)
                {
                    if (_leftMouseDown)
                    {
                        if (Root.GetKey(KeyboardKeys.Control))
                        {
                            // Toggle selection
                            keyframe.IsSelected = !keyframe.IsSelected;
                            _editor.UpdateTangents();
                        }
                        else if (Root.GetKey(KeyboardKeys.Shift))
                        {
                            // Select range
                            keyframe.IsSelected = true;
                            int selectionStart = 0;
                            for (; selectionStart < _editor._points.Count; selectionStart++)
                            {
                                if (_editor._points[selectionStart].IsSelected)
                                {
                                    break;
                                }
                            }
                            int selectionEnd = _editor._points.Count - 1;
                            for (; selectionEnd > selectionStart; selectionEnd--)
                            {
                                if (_editor._points[selectionEnd].IsSelected)
                                {
                                    break;
                                }
                            }
                            selectionStart++;
                            for (; selectionStart < selectionEnd; selectionStart++)
                            {
                                _editor._points[selectionStart].IsSelected = true;
                            }
                            _editor.UpdateTangents();
                        }
                        else if (!keyframe.IsSelected)
                        {
                            // Select node
                            if (_editor.KeyframesEditorContext != null)
                            {
                                _editor.KeyframesEditorContext.OnKeyframesDeselect(_editor);
                            }
                            else
                            {
                                _editor.ClearSelection();
                            }
                            keyframe.IsSelected = true;
                            _editor.UpdateTangents();
                        }
                        if (_editor.ShowCollapsed)
                        {
                            // Synchronize selection for curve points when collapsed so all points fo the keyframe are selected
                            for (var i = 0; i < _editor._points.Count; i++)
                            {
                                var p = _editor._points[i];
                                if (p.Index == keyframe.Index)
                                {
                                    p.IsSelected = keyframe.IsSelected;
                                }
                            }
                        }
                        StartMouseCapture();
                        Focus();
                        Tooltip?.Hide();
                        return(true);
                    }
                }
                else if (underMouse is TangentPoint tangent && tangent.Visible)
                {
                    if (_leftMouseDown)
                    {
                        // Start moving tangent
                        StartMouseCapture();
                        _isMovingTangent = true;
                        _movedKeyframes  = false;
                        _movingTangent   = tangent;
                        _editor.OnEditingStart();
                        Focus();
                        Tooltip?.Hide();
                        return(true);
                    }
                }