Пример #1
0
        public EditorState()
        {
            _tool = EditorTool.Spiro;
            _mode = EditorMode.Create;

            _displayKnots = true;
            _displayGuides = true;

            _snapX = 15.0;
            _snapY = 15.0;
            _enableSnap = true;

            _shape = null;
            _hitTreshold = 7;
            _hitTresholdSquared = 49;
            _hitSetShapes = new HashSet<SpiroShape>();
            _hitSetPoints = new HashSet<int>();
            _hitListShapes = new ObservableCollection<SpiroShape>();
            _hitListPoints = new ObservableCollection<int>();
            _isStroked = true;
            _isFilled = false;
            _isClosed = true;
            _isTagged = false;
            _pointType = SpiroPointType.G4;

            _isCaptured = false;
            _snapTreshold = 10.0;
            _snapMode = GuideSnapMode.Point | GuideSnapMode.Middle | GuideSnapMode.Intersection | GuideSnapMode.Horizontal | GuideSnapMode.Vertical;
            _snapPointRadius = 3.5;
            _snapPoint = default(GuidePoint);
            _haveSnapPoint = false;
        }
Пример #2
0
        private static bool TryToRunSpiro(SpiroShape shape, IBezierContext bc)
        {
            if (shape == null || bc == null)
            {
                return(false);
            }

            try
            {
                var points = shape.Points.ToArray();
                if (shape.IsTagged)
                {
                    return(Spiro.TaggedSpiroCPsToBezier0(points, bc));
                }
                else
                {
                    return(Spiro.SpiroCPsToBezier0(points, points.Length, shape.IsClosed, bc));
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }

            return(false);
        }
Пример #3
0
        public EditorState()
        {
            _tool = EditorTool.Spiro;
            _mode = EditorMode.Create;

            _displayKnots  = true;
            _displayGuides = true;

            _snapX      = 15.0;
            _snapY      = 15.0;
            _enableSnap = true;

            _shape              = null;
            _hitTreshold        = 7;
            _hitTresholdSquared = 49;
            _hitSetShapes       = new HashSet <SpiroShape>();
            _hitSetPoints       = new HashSet <int>();
            _hitListShapes      = new ObservableCollection <SpiroShape>();
            _hitListPoints      = new ObservableCollection <int>();
            _isStroked          = true;
            _isFilled           = false;
            _isClosed           = true;
            _isTagged           = false;
            _pointType          = SpiroPointType.G4;

            _isCaptured      = false;
            _snapTreshold    = 10.0;
            _snapMode        = GuideSnapMode.Point | GuideSnapMode.Middle | GuideSnapMode.Intersection | GuideSnapMode.Horizontal | GuideSnapMode.Vertical;
            _snapPointRadius = 3.5;
            _snapPoint       = default(GuidePoint);
            _haveSnapPoint   = false;
        }
Пример #4
0
        private void NewPointAt(SpiroShape shape, double x, double y, int index)
        {
            var point = new SpiroControlPoint();

            point.X    = x;
            point.Y    = y;
            point.Type = _state.PointType;
            shape.Points.Insert(index, point);
        }
Пример #5
0
        private void NewPoint(SpiroShape shape, double x, double y)
        {
            var point = new SpiroControlPoint();

            point.X    = x;
            point.Y    = y;
            point.Type = _state.PointType;
            shape.Points.Add(point);
        }
Пример #6
0
        private void SetPointPosition(SpiroShape shape, int index, double x, double y)
        {
            var old   = shape.Points[index];
            var point = new SpiroControlPoint();

            point.X             = x;
            point.Y             = y;
            point.Type          = old.Type;
            shape.Points[index] = point;
        }
Пример #7
0
        private void SetPointType(SpiroShape shape, int index, SpiroPointType type)
        {
            var old   = shape.Points[index];
            var point = new SpiroControlPoint();

            point.X             = old.X;
            point.Y             = old.Y;
            point.Type          = type;
            shape.Points[index] = point;
        }
Пример #8
0
        private void InsertPoint(double x, double y, SpiroShape hitShape, int hitShapePointIndex)
        {
            // Insert new point.
            SpiroShape shape = hitShape;
            int        index = hitShapePointIndex + 1;

            NewPointAt(shape, x, y, index);

            RunSpiro(shape);
        }
Пример #9
0
        private void RemovePoint(SpiroShape shape, int index)
        {
            shape.Points.RemoveAt(index);

            if (shape.Points.Count == 0)
            {
                RemoveShape(shape);
            }
            else
            {
                RunSpiro(shape);
            }
        }
Пример #10
0
        private void SetPointPositionDelta(SpiroShape shape, int index, double dx, double dy)
        {
            var    old   = shape.Points[index];
            double x     = old.X + dx;
            double y     = old.Y + dy;
            double sx    = _state.EnableSnap && _state.SnapX != 0 ? Snap(x, _state.SnapX) : x;
            double sy    = _state.EnableSnap && _state.SnapY != 0 ? Snap(y, _state.SnapY) : y;
            var    point = new SpiroControlPoint();

            point.X             = sx;
            point.Y             = sy;
            point.Type          = old.Type;
            shape.Points[index] = point;
        }
Пример #11
0
        private void Select(SpiroShape hitShape, int hitShapePointIndex)
        {
            if (!_state.HitSetShapes.Contains(hitShape))
            {
                // Select shape.
                _state.HitSetShapes.Add(hitShape);
                _state.HitListShapes.Add(hitShape);
                // Select point.
                _state.HitSetPoints.Add(hitShapePointIndex);
                _state.HitListPoints.Add(hitShapePointIndex);
            }

            // Update point type.
            if (hitShapePointIndex != -1)
            {
                _state.PointType = hitShape.Points[hitShapePointIndex].Type;
            }

            _invalidate();
        }
Пример #12
0
        private void RunSpiro(SpiroShape shape)
        {
            if (shape == null)
            {
                return;
            }

            var bc     = new PathBezierContext();
            var result = TryToRunSpiro(shape, bc);

            if (_data.ContainsKey(shape))
            {
                _data[shape]  = result ? bc.ToString() : null;
                _knots[shape] = result ? bc.GetKnots() : null;
            }
            else
            {
                _data.Add(shape, result ? bc.ToString() : null);
                _knots.Add(shape, result ? bc.GetKnots() : null);
            }
        }
Пример #13
0
        private void Select(SpiroShape hitShape)
        {
            if (!_state.HitSetShapes.Contains(hitShape))
            {
                // Select shape.
                _state.HitSetShapes.Add(hitShape);
                _state.HitListShapes.Add(hitShape);
                // Deselect point.
                _state.HitSetPoints.Add(-1);
                _state.HitListPoints.Add(-1);
            }

            // Update shape info.
            if (hitShape != null)
            {
                _state.IsStroked = hitShape.IsStroked;
                _state.IsFilled  = hitShape.IsFilled;
                _state.IsClosed  = hitShape.IsClosed;
                _state.IsTagged  = hitShape.IsTagged;
            }

            _invalidate();
        }
Пример #14
0
 private void SetPointPositionDelta(SpiroShape shape, int index, double dx, double dy)
 {
     var old = shape.Points[index];
     double x = old.X + dx;
     double y = old.Y + dy;
     double sx = _state.EnableSnap && _state.SnapX != 0 ? Snap(x, _state.SnapX) : x;
     double sy = _state.EnableSnap && _state.SnapY != 0 ? Snap(y, _state.SnapY) : y;
     var point = new SpiroControlPoint();
     point.X = sx;
     point.Y = sy;
     point.Type = old.Type;
     shape.Points[index] = point;
 }
Пример #15
0
 private void SetPointPosition(SpiroShape shape, int index, double x, double y)
 {
     var old = shape.Points[index];
     var point = new SpiroControlPoint();
     point.X = x;
     point.Y = y;
     point.Type = old.Type;
     shape.Points[index] = point;
 }
Пример #16
0
        private void Select(SpiroShape hitShape)
        {
            if (!_state.HitSetShapes.Contains(hitShape))
            {
                // Select shape.
                _state.HitSetShapes.Add(hitShape);
                _state.HitListShapes.Add(hitShape);
                // Deselect point.
                _state.HitSetPoints.Add(-1);
                _state.HitListPoints.Add(-1);
            }

            // Update shape info.
            if (hitShape != null)
            {
                _state.IsStroked = hitShape.IsStroked;
                _state.IsFilled = hitShape.IsFilled;
                _state.IsClosed = hitShape.IsClosed;
                _state.IsTagged = hitShape.IsTagged;
            }

            _invalidate();
        }
Пример #17
0
        private void Select(SpiroShape hitShape, int hitShapePointIndex)
        {
            if (!_state.HitSetShapes.Contains(hitShape))
            {
                // Select shape.
                _state.HitSetShapes.Add(hitShape);
                _state.HitListShapes.Add(hitShape);
                // Select point.
                _state.HitSetPoints.Add(hitShapePointIndex);
                _state.HitListPoints.Add(hitShapePointIndex);
            }

            // Update point type.
            if (hitShapePointIndex != -1)
            {
                _state.PointType = hitShape.Points[hitShapePointIndex].Type;
            }

            _invalidate();
        }
Пример #18
0
        private void RunSpiro(SpiroShape shape)
        {
            if (shape == null)
                return;

            var bc = new PathBezierContext();
            var result = TryToRunSpiro(shape, bc);
            if (_data.ContainsKey(shape))
            {
                _data[shape] = result ? bc.ToString() : null;
                _knots[shape] = result ? bc.GetKnots() : null;
            }
            else
            {
                _data.Add(shape, result ? bc.ToString() : null);
                _knots.Add(shape, result ? bc.GetKnots() : null);
            }
        }
Пример #19
0
 private void RemoveShape(SpiroShape shape)
 {
     _drawing.Shapes.Remove(shape);
     _data.Remove(shape);
     _knots.Remove(shape);
 }
Пример #20
0
        private static bool HitTestForShape(IList<SpiroShape> shapes, double x, double y, double treshold, out SpiroShape hitShape, out int hitShapePointIndex)
        {
            for (int i = 0; i < shapes.Count; i++)
            {
                var bc = new HitTestBezierContext(x, y);
                var shape = shapes[i];
                int knontIndex;

                try
                {
                    if (!TryToRunSpiro(shape, bc))
                        continue;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Debug.WriteLine(ex.StackTrace);
                    continue;
                }

                if (bc.Report(out knontIndex) < treshold)
                {
                    hitShape = shape;
                    hitShapePointIndex = knontIndex;
                    return true;
                }
            }

            hitShape = null;
            hitShapePointIndex = -1;
            return false;
        }
Пример #21
0
 private void NewPointAt(SpiroShape shape, double x, double y, int index)
 {
     var point = new SpiroControlPoint();
     point.X = x;
     point.Y = y;
     point.Type = _state.PointType;
     shape.Points.Insert(index, point);
 }
Пример #22
0
 private void NewPoint(SpiroShape shape, double x, double y)
 {
     var point = new SpiroControlPoint();
     point.X = x;
     point.Y = y;
     point.Type = _state.PointType;
     shape.Points.Add(point);
 }
Пример #23
0
        private static bool HitTestForPoint(IList <SpiroShape> shapes, double x, double y, double tresholdSquared, out SpiroShape hitShape, out int hitShapePointIndex)
        {
            foreach (var shape in shapes)
            {
                for (int i = 0; i < shape.Points.Count; i++)
                {
                    var point    = shape.Points[i];
                    var distance = DistanceSquared(x, y, point.X, point.Y);
                    if (distance < tresholdSquared)
                    {
                        hitShape           = shape;
                        hitShapePointIndex = i;
                        return(true);
                    }
                }
            }

            hitShape           = null;
            hitShapePointIndex = -1;
            return(false);
        }
Пример #24
0
 private void SetPointType(SpiroShape shape, int index, SpiroPointType type)
 {
     var old = shape.Points[index];
     var point = new SpiroControlPoint();
     point.X = old.X;
     point.Y = old.Y;
     point.Type = type;
     shape.Points[index] = point;
 }
Пример #25
0
        private void RemovePoint(SpiroShape shape, int index)
        {
            shape.Points.RemoveAt(index);

            if (shape.Points.Count == 0)
            {
                RemoveShape(shape);
            }
            else
            {
                RunSpiro(shape);
            }
        }
Пример #26
0
        private static bool HitTestForPoint(IList<SpiroShape> shapes, double x, double y, double tresholdSquared, out SpiroShape hitShape, out int hitShapePointIndex)
        {
            foreach (var shape in shapes)
            {
                for (int i = 0; i < shape.Points.Count; i++)
                {
                    var point = shape.Points[i];
                    var distance = DistanceSquared(x, y, point.X, point.Y);
                    if (distance < tresholdSquared)
                    {
                        hitShape = shape;
                        hitShapePointIndex = i;
                        return true;
                    }
                }
            }

            hitShape = null;
            hitShapePointIndex = -1;
            return false;
        }
Пример #27
0
 private void RemoveShape(SpiroShape shape)
 {
     _drawing.Shapes.Remove(shape);
     _data.Remove(shape);
     _knots.Remove(shape);
 }
Пример #28
0
        private static bool TryToRunSpiro(SpiroShape shape, IBezierContext bc)
        {
            if (shape == null || bc == null)
                return false;

            try
            {
                var points = shape.Points.ToArray();
                if (shape.IsTagged)
                    return Spiro.TaggedSpiroCPsToBezier0(points, bc);
                else
                    return Spiro.SpiroCPsToBezier0(points, points.Length, shape.IsClosed, bc);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
            }

            return false;
        }
Пример #29
0
        private static bool HitTestForShape(IList <SpiroShape> shapes, double x, double y, double treshold, out SpiroShape hitShape, out int hitShapePointIndex)
        {
            for (int i = 0; i < shapes.Count; i++)
            {
                var bc    = new HitTestBezierContext(x, y);
                var shape = shapes[i];
                int knontIndex;

                try
                {
                    if (!TryToRunSpiro(shape, bc))
                    {
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    Debug.WriteLine(ex.StackTrace);
                    continue;
                }

                if (bc.Report(out knontIndex) < treshold)
                {
                    hitShape           = shape;
                    hitShapePointIndex = knontIndex;
                    return(true);
                }
            }

            hitShape           = null;
            hitShapePointIndex = -1;
            return(false);
        }
Пример #30
0
        public static IList <SpiroShape> ToShapes(string plate)
        {
            if (string.IsNullOrEmpty(plate))
            {
                return(null);
            }

            var shapes    = new ObservableCollection <SpiroShape>();
            var newLine   = Environment.NewLine.ToCharArray();
            var separator = new char[] { ' ', '\t' };
            var trim      = new char[] { '(', ')' };
            var options   = StringSplitOptions.RemoveEmptyEntries;
            var lines     = plate.Split(newLine, options).Select(x => x.Trim().Trim(trim).Split(separator, options));

            SpiroShape shape = null;

            foreach (var line in lines)
            {
                if (line.Length == 0 || line[0] == "plate")
                {
                    continue;
                }

                switch (line[0][0])
                {
                case 'v':
                {
                    if (shape == null)
                    {
                        shape = NewShape();
                    }

                    if (line.Length == 3)
                    {
                        shape.Points.Add(NewPoint(SpiroPointType.Corner, line[1], line[2]));
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                break;

                case 'o':
                {
                    if (shape == null)
                    {
                        shape = NewShape();
                    }

                    if (line.Length == 3)
                    {
                        shape.Points.Add(NewPoint(SpiroPointType.G4, line[1], line[2]));
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                break;

                case 'c':
                {
                    if (shape == null)
                    {
                        shape = NewShape();
                    }

                    if (line.Length == 3)
                    {
                        shape.Points.Add(NewPoint(SpiroPointType.G2, line[1], line[2]));
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                break;

                case '[':
                {
                    if (shape == null)
                    {
                        shape = NewShape();
                    }

                    if (line.Length == 3)
                    {
                        shape.Points.Add(NewPoint(SpiroPointType.Left, line[1], line[2]));
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                break;

                case ']':
                {
                    if (shape == null)
                    {
                        shape = NewShape();
                    }

                    if (line.Length == 3)
                    {
                        shape.Points.Add(NewPoint(SpiroPointType.Right, line[1], line[2]));
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                break;

                case 'z':
                {
                    if (shape == null)
                    {
                        shape = NewShape();
                    }

                    if (line.Length == 1)
                    {
                        shape.Points.Add(NewPoint(SpiroPointType.End, "0", "0"));
                    }
                    else if (line.Length == 3)
                    {
                        shape.Points.Add(NewPoint(SpiroPointType.End, line[1], line[2]));
                    }
                    else
                    {
                        throw new FormatException();
                    }

                    shapes.Add(shape);
                    shape = null;
                }
                break;

                case '{':
                {
                    if (shape == null)
                    {
                        shape = NewShape();
                    }

                    if (line.Length == 3)
                    {
                        shape.Points.Add(NewPoint(SpiroPointType.OpenContour, line[1], line[2]));
                    }
                    else
                    {
                        throw new FormatException();
                    }
                }
                break;

                case '}':
                {
                    if (shape == null)
                    {
                        shape = NewShape();
                    }

                    if (line.Length == 3)
                    {
                        shape.Points.Add(NewPoint(SpiroPointType.EndOpenContour, line[1], line[2]));
                    }
                    else
                    {
                        throw new FormatException();
                    }

                    shapes.Add(shape);
                    shape = null;
                }
                break;

                default:
                    throw new FormatException();
                }
            }

            if (shape != null)
            {
                shape.IsTagged = false;
                shapes.Add(shape);
                shape = null;
            }

            return(shapes);
        }
Пример #31
0
        private void InsertPoint(double x, double y, SpiroShape hitShape, int hitShapePointIndex)
        {
            // Insert new point.
            SpiroShape shape = hitShape;
            int index = hitShapePointIndex + 1;
            NewPointAt(shape, x, y, index);

            RunSpiro(shape);
        }