Esempio n. 1
0
        private static bool HitTestForGuideLine(IList <GuideLine> guides, double x, double y, double treshold, out GuideLine hitGuide)
        {
            var point = new GuidePoint(x, y);

            foreach (var guide in guides)
            {
                var nearest  = GuideHelpers.NearestPointOnLine(guide.Point0, guide.Point1, point);
                var distance = GuideHelpers.Distance(nearest, point);
                if (distance < treshold)
                {
                    hitGuide = guide;
                    return(true);
                }
            }

            hitGuide = null;
            return(false);
        }
Esempio n. 2
0
        public void GuideMove(double x, double y)
        {
            double sx = _state.EnableSnap && _state.SnapX != 0 ? Snap(x, _state.SnapX) : x;
            double sy = _state.EnableSnap && _state.SnapY != 0 ? Snap(y, _state.SnapY) : y;

            _state.GuidePosition = new GuidePoint(sx, sy);
            _measure.Point1      = new GuidePoint(sx, sy);

            TryToSnapToGuideLine();

            if (_state.HaveSnapPoint)
            {
                _state.GuidePosition = new GuidePoint(_state.SnapPoint.X, _state.SnapPoint.Y);
                _measure.Point1      = new GuidePoint(_state.SnapPoint.X, _state.SnapPoint.Y);
            }

            _measure.Distance = GuideHelpers.Distance(_measure.Point0, _measure.Point1);
            _measure.Angle    = GuideHelpers.LineSegmentAngle(_measure.Point0, _measure.Point1);

            _invalidate();
        }
Esempio n. 3
0
        public void SpiroMove(double x, double y)
        {
            double sx = _state.EnableSnap && _state.SnapX != 0 ? Snap(x, _state.SnapX) : x;
            double sy = _state.EnableSnap && _state.SnapY != 0 ? Snap(y, _state.SnapY) : y;

            if (_state.EnableSnap)
            {
                _state.GuidePosition = new GuidePoint(sx, sy);
                _measure.Point1      = new GuidePoint(x, y);

                TryToSnapToGuideLine();

                if (_state.HaveSnapPoint)
                {
                    _state.GuidePosition = new GuidePoint(_state.SnapPoint.X, _state.SnapPoint.Y);
                    _measure.Point1      = new GuidePoint(_state.SnapPoint.X, _state.SnapPoint.Y);

                    sx = _state.SnapPoint.X;
                    sy = _state.SnapPoint.Y;
                }

                _measure.Point1 = new GuidePoint(sx, sy);

                _measure.Distance = GuideHelpers.Distance(_measure.Point0, _measure.Point1);
                _measure.Angle    = GuideHelpers.LineSegmentAngle(_measure.Point0, _measure.Point1);
            }

            if (_state.Shape != null)
            {
                if (_state.Shape.Points.Count > 1)
                {
                    SetPointPosition(_state.Shape, _state.Shape.Points.Count - 1, sx, sy);
                    RunSpiro(_state.Shape);
                }

                _invalidate();
            }
            else
            {
                if (_state.Mode == EditorMode.Create)
                {
                    // Dehover shape and dehover point.
                    Deselect();

                    SpiroShape hitShape;
                    int        hitShapePointIndex;
                    var        result = HitTestForPoint(_drawing.Shapes, x, y, _state.HitTresholdSquared, out hitShape, out hitShapePointIndex);
                    if (result)
                    {
                        // Hover shape and point.
                        Select(hitShape, hitShapePointIndex);
                    }
                    else
                    {
                        if (HitTestForShape(_drawing.Shapes, x, y, _state.HitTreshold, out hitShape, out hitShapePointIndex))
                        {
                            // Hover shape and dehover point.
                            Select(hitShape);
                        }
                    }
                }
                else if (_state.Mode == EditorMode.Move)
                {
                    // Calculate move offset.
                    double dx = sx - _startX;
                    double dy = sy - _startY;
                    // Update start position.
                    _startX = sx;
                    _startY = sy;

                    for (int i = 0; i < _state.HitListShapes.Count; i++)
                    {
                        var shape = _state.HitListShapes[i];
                        var index = _state.HitListPoints[i];

                        // Move selected shape.
                        if (index == -1)
                        {
                            // Move all shape points.
                            for (int j = 0; j < shape.Points.Count; j++)
                            {
                                SetPointPositionDelta(shape, j, dx, dy);
                            }
                            RunSpiro(shape);
                        }

                        // Move selected point.
                        if (index != -1)
                        {
                            SetPointPosition(shape, index, sx, sy);
                            RunSpiro(shape);
                        }
                    }

                    _invalidate();
                }
                else if (_state.Mode == EditorMode.Selected)
                {
                    _invalidate();
                }
            }
        }
Esempio n. 4
0
        public void SpiroLeftDown(double x, double y)
        {
            double sx = _state.EnableSnap && _state.SnapX != 0 ? Snap(x, _state.SnapX) : x;
            double sy = _state.EnableSnap && _state.SnapY != 0 ? Snap(y, _state.SnapY) : y;

            if (_state.EnableSnap)
            {
                _state.GuidePosition = new GuidePoint(sx, sy);

                if (_state.Shape == null)
                {
                    _measure.Point0 = new GuidePoint(x, y);
                }

                _measure.Point1 = new GuidePoint(x, y);

                TryToSnapToGuideLine();

                if (_state.HaveSnapPoint)
                {
                    _state.GuidePosition = new GuidePoint(_state.SnapPoint.X, _state.SnapPoint.Y);
                    _measure.Point1      = new GuidePoint(_state.SnapPoint.X, _state.SnapPoint.Y);

                    sx = _state.SnapPoint.X;
                    sy = _state.SnapPoint.Y;
                }

                if (_state.Shape == null)
                {
                    _measure.Point0 = new GuidePoint(sx, sy);
                }

                _measure.Point1 = new GuidePoint(sx, sy);

                _measure.Distance = GuideHelpers.Distance(_measure.Point0, _measure.Point1);
                _measure.Angle    = GuideHelpers.LineSegmentAngle(_measure.Point0, _measure.Point1);
            }

            _startX = sx;
            _startY = sy;

            if (_state.Shape == null)
            {
                SpiroShape hitShape;
                int        hitShapePointIndex;

                // Hit test for point.
                var result = HitTestForPoint(_drawing.Shapes, x, y, _state.HitTresholdSquared, out hitShape, out hitShapePointIndex);
                if (result)
                {
                    Deselect();
                    Select(hitShape, hitShapePointIndex);
                    // Begin point move.
                    _state.Mode = EditorMode.Move;
                    return;
                }
                else
                {
                    // Hit test for shape and nearest point.
                    if (HitTestForShape(_drawing.Shapes, x, y, _state.HitTreshold, out hitShape, out hitShapePointIndex))
                    {
                        Select(hitShape);
                        // Begin point move.
                        _state.Mode = EditorMode.Move;
                        return;
                    }

                    if (_state.HitListShapes.Count > 0)
                    {
                        Deselect();
                        return;
                    }
                }
            }

            if (_state.Mode == EditorMode.Create)
            {
                Create(sx, sy);
            }
        }
Esempio n. 5
0
        public static bool TryToSnapToGuideLine(IList <GuideLine> guides, GuideSnapMode mode, double treshold, GuidePoint point, out GuidePoint snap, out GuideSnapMode result)
        {
            snap   = default(GuidePoint);
            result = default(GuideSnapMode);

            if (guides.Count == 0 || mode == GuideSnapMode.None)
            {
                return(false);
            }

            if (mode.HasFlag(GuideSnapMode.Point))
            {
                foreach (var guide in guides)
                {
                    var distance0 = GuideHelpers.Distance(guide.Point0, point);
                    if (distance0 < treshold)
                    {
                        snap   = new GuidePoint(guide.Point0.X, guide.Point0.Y);
                        result = GuideSnapMode.Point;
                        return(true);
                    }

                    var distance1 = GuideHelpers.Distance(guide.Point1, point);
                    if (distance1 < treshold)
                    {
                        snap   = new GuidePoint(guide.Point1.X, guide.Point1.Y);
                        result = GuideSnapMode.Point;
                        return(true);
                    }
                }
            }

            if (mode.HasFlag(GuideSnapMode.Middle))
            {
                foreach (var guide in guides)
                {
                    var middle   = GuideHelpers.Middle(guide.Point0, guide.Point1);
                    var distance = GuideHelpers.Distance(middle, point);
                    if (distance < treshold)
                    {
                        snap   = middle;
                        result = GuideSnapMode.Middle;
                        return(true);
                    }
                }
            }

            if (mode.HasFlag(GuideSnapMode.Nearest))
            {
                foreach (var guide in guides)
                {
                    var nearest  = GuideHelpers.NearestPointOnLine(guide.Point0, guide.Point1, point);
                    var distance = GuideHelpers.Distance(nearest, point);
                    if (distance < treshold)
                    {
                        snap   = nearest;
                        result = GuideSnapMode.Nearest;
                        return(true);
                    }
                }
            }

            if (mode.HasFlag(GuideSnapMode.Intersection))
            {
                foreach (var guide0 in guides)
                {
                    foreach (var guide1 in guides)
                    {
                        if (guide0 == guide1)
                        {
                            continue;
                        }

                        GuidePoint intersection;
                        if (GuideHelpers.LineLineIntersection(guide0.Point0, guide0.Point1, guide1.Point0, guide1.Point1, out intersection))
                        {
                            var distance = GuideHelpers.Distance(intersection, point);
                            if (distance < treshold)
                            {
                                snap   = intersection;
                                result = GuideSnapMode.Intersection;
                                return(true);
                            }
                        }
                    }
                }
            }

            double horizontal = default(double);
            double vertical   = default(double);

            if (mode.HasFlag(GuideSnapMode.Horizontal))
            {
                foreach (var guide in guides)
                {
                    if (point.Y >= guide.Point0.Y - treshold && point.Y <= guide.Point0.Y + treshold)
                    {
                        snap       = new GuidePoint(point.X, guide.Point0.Y);
                        result    |= GuideSnapMode.Horizontal;
                        horizontal = guide.Point0.Y;
                        break;
                    }

                    if (point.Y >= guide.Point1.Y - treshold && point.Y <= guide.Point1.Y + treshold)
                    {
                        snap       = new GuidePoint(point.X, guide.Point1.Y);
                        result    |= GuideSnapMode.Horizontal;
                        horizontal = guide.Point1.Y;
                        break;
                    }
                }
            }

            if (mode.HasFlag(GuideSnapMode.Vertical))
            {
                foreach (var guide in guides)
                {
                    if (point.X >= guide.Point0.X - treshold && point.X <= guide.Point0.X + treshold)
                    {
                        snap     = new GuidePoint(guide.Point0.X, point.Y);
                        result  |= GuideSnapMode.Vertical;
                        vertical = guide.Point0.X;
                        break;
                    }

                    if (point.X >= guide.Point1.X - treshold && point.X <= guide.Point1.X + treshold)
                    {
                        snap     = new GuidePoint(guide.Point1.X, point.Y);
                        result  |= GuideSnapMode.Vertical;
                        vertical = guide.Point1.X;
                        break;
                    }
                }
            }

            if (result.HasFlag(GuideSnapMode.Horizontal) || result.HasFlag(GuideSnapMode.Vertical))
            {
                snap = new GuidePoint(
                    result.HasFlag(GuideSnapMode.Vertical) ? vertical : point.X,
                    result.HasFlag(GuideSnapMode.Horizontal) ? horizontal : point.Y);
                return(true);
            }

            return(false);
        }