Exemplo n.º 1
0
    private void onSceneGUIAppendPointsHandler(ref Vector2Array points)
    {
        HandleUtility.Repaint();
        var gameObjPos = _asTarget.gameObject.transform.position;
        //鼠标位置
        var mousePos = Event.current.mousePosition;

        mousePos = HandleUtility.GUIPointToWorldRay(mousePos).origin;
        if (Event.current.isMouse)
        {
            EventType eventType = Event.current.type;
            if (Event.current.button == 0)
            {
                if (eventType == EventType.MouseDown)
                {
                    _isMousePress = true;
                    if (_editID == -1)
                    {
                        var mouseGUI = HandleUtility.WorldToGUIPoint(mousePos);
                        //Debug.Log("Insert");
                        Undo.RecordObject(_asTarget, "add point");
                        points.Insert(points.Count, new Vector2(mousePos.x - gameObjPos.x, mousePos.y - gameObjPos.y));
                        _editID = points.Count - 1;
                    }
                }
                else if (eventType == EventType.MouseUp)
                {
                    _isMousePress = false;
                    _editID       = -1;
                }
                if (!_isMousePress)                //鼠标没有按下时
                {
                    var cpt = new Vector2(mousePos.x, mousePos.y);

                    int     nearestId      = getNearestPointId(points, new Vector2(cpt.x - gameObjPos.x, cpt.y - gameObjPos.y));
                    Vector2 nearestWorldPt = new Vector2(points[nearestId].x + gameObjPos.x, points[nearestId].y + gameObjPos.y);
                    float   d = Vector2.Distance(HandleUtility.WorldToGUIPoint(cpt), HandleUtility.WorldToGUIPoint(nearestWorldPt));
                    if (nearestId > -1 && d <= SnapDistance)
                    {
                        cpt.Set(nearestWorldPt.x, nearestWorldPt.y);
                        //Debug.Log("_editID:"+_editID);
                        _editID = nearestId;
                    }
                    else
                    {
                        _editID = -1;
                    }

                    _asTarget.point.Set(cpt.x, cpt.y);
                }
            }
        }

        editPointHandler(ref points);
        //画点列表
        drawPoints(ref points, false);
    }
Exemplo n.º 2
0
    private void onSceneGUIEditPointsHandler(ref Vector2Array points)
    {
        HandleUtility.Repaint();

        //鼠标位置
        var mousePos = Event.current.mousePosition;

        mousePos = HandleUtility.GUIPointToWorldRay(mousePos).origin;

        //寻找最近线段
        Vector2[] nearestLineSegment = new Vector2[2];
        findSetNearestLineSegment(mousePos, ref points, ref nearestLineSegment);

        if (Event.current.isMouse)
        {
            EventType eventType = Event.current.type;
            if (Event.current.button == 0)
            {
                if (eventType == EventType.MouseDown)
                {
                    _isMousePress = true;
                    var mouseGUI    = HandleUtility.WorldToGUIPoint(mousePos);
                    var nearestGUI0 = HandleUtility.WorldToGUIPoint(nearestLineSegment[0]);
                    var nearestGUI1 = HandleUtility.WorldToGUIPoint(nearestLineSegment[1]);
                    //float mouseToNearestLineSegment=HandleUtility.DistancePointToLineSegment(mouseGUI,nearestGUI0,nearestGUI1);
                    float d0 = Vector2.Distance(mouseGUI, nearestGUI0);
                    float d1 = Vector2.Distance(mouseGUI, nearestGUI1);
                    if (_editID == -1)
                    {
                        if (d0 <= SnapDistance)
                        {
                            //Debug.Log("d0<=SnapDistance");
                            if (Event.current.control)
                            {
                                deletePointWithIndex(ref points, _nearestID);
                            }
                            else
                            {
                                _editID = _nearestID;
                            }
                        }
                        else if (d1 <= SnapDistance)
                        {
                            //Debug.Log("d1<=SnapDistance");
                            int lineEndId = _nearestID + 1 <= points.Count - 1?_nearestID + 1:0;
                            if (Event.current.control)
                            {
                                deletePointWithIndex(ref points, lineEndId);
                            }
                            else
                            {
                                _editID = lineEndId;
                            }
                        }
                        else
                        {
                            //Debug.Log("Insert id:"+(_nearestID+1));
                            Undo.RecordObject(_asTarget, "add point");
                            points.Insert(_nearestID + 1, new Vector2(mousePos.x, mousePos.y));
                            _editID = _nearestID + 1;
                            findSetNearestLineSegment(mousePos, ref points, ref nearestLineSegment);
                        }
                    }
                }
                else if (eventType == EventType.MouseUp)
                {
                    _isMousePress = false;
                    _editID       = -1;
                }

                if (!_isMousePress)                //鼠标没有按下时
                //设置控制柄到最近线段的垂线
                {
                    var   perp        = getPerpendicularPt(mousePos.x, mousePos.y, nearestLineSegment[0].x, nearestLineSegment[0].y, nearestLineSegment[1].x, nearestLineSegment[1].y);
                    var   perpGUI     = HandleUtility.WorldToGUIPoint(perp);
                    var   nearestGUI0 = HandleUtility.WorldToGUIPoint(nearestLineSegment[0]);
                    var   nearestGUI1 = HandleUtility.WorldToGUIPoint(nearestLineSegment[1]);
                    float perpToNearestLineSegment = HandleUtility.DistancePointToLineSegment(perpGUI, nearestGUI0, nearestGUI1);
                    float d0  = Vector2.Distance(perpGUI, nearestGUI0);
                    float d1  = Vector2.Distance(perpGUI, nearestGUI1);
                    var   cpt = new Vector2(mousePos.x, mousePos.y);
                    //垂足不能滑出线段

                    /*if(perpToNearestLineSegment>0.01f){
                     *      if(d0<d1)perp.Set(nearestLineSegment[0].x,nearestLineSegment[0].y);
                     *      else perp.Set(nearestLineSegment[1].x,nearestLineSegment[1].y);
                     * }*/
                    //操作点贴紧端点
                    int   nearestId = getNearestPointId(points, cpt);
                    float d         = Vector2.Distance(HandleUtility.WorldToGUIPoint(cpt), HandleUtility.WorldToGUIPoint(points[nearestId]));
                    if (nearestId > -1 && d <= SnapDistance)
                    {
                        cpt.Set(points[nearestId].x, points[nearestId].y);
                        //Debug.Log("_editID:"+_editID);
                        _editID = nearestId;
                    }
                    else
                    {
                        _editID = -1;
                    }

                    _asTarget.point.Set(cpt.x, cpt.y);
                }
            }
        }
        editPointHandler(ref points);
        //画点列表
        drawPoints(ref points, false);
    }