Exemplo n.º 1
0
        public static void DragCurve(Curve curve)
        /// Backup curve to return the removed nodes when dragging cursor returned to curve field
        {
            if (UI.current.layout)
            {
                return;
            }
            if (UI.current.optimizeElements && !UI.current.IsInWindow())
            {
                return;
            }

            //moving
            bool isDragging = false;
            bool isReleased = false;
            int  dragNum    = -1;

            Curve.Node[] originalPoints = null;

            for (int i = 0; i < curve.points.Length; i++)
            {
                bool    newDragging = false; bool newReleased = false;
                Vector2 newPos = DragPoint(curve, i, ref newDragging, ref newReleased);

                if (newDragging)
                {
                    originalPoints = new Curve.Node[curve.points.Length];                     //curve.GetPositions();
                    for (int p = 0; p < originalPoints.Length; p++)
                    {
                        originalPoints[p] = new Curve.Node(curve.points[p]);
                    }

                    curve.points[i].pos = newPos;
                }

                if (newDragging || newReleased)
                {
                    dragNum = i;
                }

                isDragging = isDragging || newDragging;
                isReleased = isReleased || newReleased;
            }

            //adding
            if (ClickedNearCurve(curve))
            {
                Vector2 addPos   = ToCurve(UI.current.mousePos);
                int     addedNum = AddPoint(curve, addPos);

                //starting drag
                DragPoint(curve, addedNum, ref isDragging, ref isReleased);                   //just to start drag
            }

            //removing
            if (isDragging)
            {
                //calc if node should be removed
                bool isRemoved = false;
                if (dragNum != 0 && dragNum != curve.points.Length - 1)             //ignoring first and last
                {
                    Vector2 pos = ToCell(curve.points[dragNum].pos);
                    if (!Cell.current.InternalRect.Extended(10).Contains(pos))
                    {
                        isRemoved = true;
                    }
                }

                //removing
                if (isRemoved)
                {
                    UI.current.MarkChanged(completeUndo: true);
                    ArrayTools.RemoveAt(ref curve.points, dragNum);
                }

                //clamping if cursor is too close to the field to remove
                else
                {
                    ClampPoint(curve, dragNum);
                }
            }

            //if returned dragging to field
            else if (DragDrop.obj != null && DragDrop.obj.GetType() == typeof((Curve, Curve.Node, int)))
            {
                (Curve curve, Curve.Node node, int num)dragObj = ((Curve, Curve.Node, int))DragDrop.obj;
                if (dragObj.curve == curve && !curve.points.Contains(dragObj.node))
                {
                    DragDrop.TryDrag(dragObj, UI.current.mousePos);
                    //to make it repaint

                    if (Cell.current.InternalRect.Extended(10).Contains(UI.current.mousePos))
                    {
                        ArrayTools.Insert(ref curve.points, dragObj.num, dragObj.node);
                        dragObj.node.pos = ToCurve(UI.current.mousePos);
                        ClampPoint(dragObj.curve, dragObj.num);                         //this will place it between prev and next points
                    }

                    DragDrop.TryRelease(dragObj, UI.current.mousePos);
                    //otherwise it will not be released forever
                }
            }

            if (Cell.current.valChanged)
            {
                curve.Refresh();
            }
        }
Exemplo n.º 2
0
        private static Rect NodeRect(Curve.Node node)
        {
            Vector2 pos = ToCell(node.pos);

            return(new Rect(pos.x - moveRange, pos.y - moveRange, 1 + moveRange * 2, 1 + moveRange * 2));
        }
Exemplo n.º 3
0
        private static bool ClickedNearCurve(Curve curve)
        {
            //TODO: to optimize A LOT. Calculating AddCursorRect each frame are damn slow!

            if (UI.current.layout)
            {
                return(false);
            }
            //if (Event.current.type != EventType.MouseDown) return false;
            //if (!Cell.current.Rect.Extended(10).Contains(UI.mousePos)) return false;

            for (int s = 0; s < curve.points.Length - 1; s++)
            {
                Curve.Node prevNode = curve.points[s];
                Curve.Node nextNode = curve.points[s + 1];
                float      range    = nextNode.pos.x - prevNode.pos.x;
                float      start    = prevNode.pos.x;

                int numAddRects = (int)(range * Cell.current.finalSize.x / addRectDensity) + 2;

                for (int i = 0; i < numAddRects - 1; i++)
                {
                    float   time = 1f * i / (numAddRects - 1);
                    float   val  = Den.Tools.Curve.EvaluatePrecise(prevNode, nextNode, time);
                    Vector2 prev = ToCell(new Vector2(start + time * range, val));

                    time = 1f * (i + 1) / (numAddRects - 1);
                    val  = Den.Tools.Curve.EvaluatePrecise(prevNode, nextNode, time);
                    Vector2 next = ToCell(new Vector2(start + time * range, val));

                    Rect rect = new Rect(
                        prev.x,
                        Mathf.Min(next.y, prev.y),
                        next.x - prev.x,
                        Mathf.Abs(next.y - prev.y));

                    if (rect.y < Cell.current.worldPosition.y)
                    {
                        rect.y = Cell.current.worldPosition.y;
                    }
                    if (rect.max.y > Cell.current.worldPosition.y + Cell.current.finalSize.y)
                    {
                        rect.max = new Vector2(rect.max.x, Cell.current.worldPosition.y + Cell.current.finalSize.y);
                    }
                    if (rect.height < 0)
                    {
                        rect.y += rect.height; rect.height = 0;
                    }

                    rect = rect.Extended(addRange);

                                        #if UNITY_EDITOR
                    Rect dispRect = UI.current.scrollZoom.ToScreen(rect.position, rect.size);
                    UnityEditor.EditorGUIUtility.AddCursorRect(dispRect, UnityEditor.MouseCursor.ArrowPlus);
                    //UnityEditor.EditorGUI.DrawRect(dispRect,Color.red);
                                        #endif

                    if (Event.current.type == EventType.MouseDown && rect.Contains(UI.current.mousePos) && Event.current.button == 0)
                    {
                        //excluding points that should be dragged instead
                        for (int n = 0; n < curve.points.Length; n++)
                        {
                            Vector2 nPos  = ToCell(curve.points[n].pos);
                            Rect    nRect = new Rect(nPos.x - moveRange, nPos.y - moveRange, 1 + moveRange * 2, 1 + moveRange * 2);
                            if (nRect.Contains(UI.current.mousePos))
                            {
                                return(false);
                            }
                        }

                        return(true);
                    }
                }
            }

            return(false);
        }