Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="bezier"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public void TryToConnectPoint4(XBezier bezier, double x, double y)
        {
            var result = ShapeBounds.HitTest(_editor.Project.CurrentContainer, new Vector2(x, y), _editor.Project.Options.HitTreshold);

            if (result != null && result is XPoint)
            {
                bezier.Point4 = result as XPoint;
            }
        }
Beispiel #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="bezier"></param>
        /// <param name="point"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        public static bool Contains(XBezier bezier, Vector2 point, double dx, double dy)
        {
            Vector2[] vertices = new Vector2[4];
            int       k;

            Vector2[] convexHull;

            vertices[0] = new Vector2(bezier.Point1.X + dx, bezier.Point1.Y + dy);
            vertices[1] = new Vector2(bezier.Point2.X + dx, bezier.Point2.Y + dy);
            vertices[2] = new Vector2(bezier.Point3.X + dx, bezier.Point3.Y + dy);
            vertices[3] = new Vector2(bezier.Point4.X + dx, bezier.Point4.Y + dy);

            mc.ConvexHull(vertices, out convexHull, out k);

            return(Contains(point.X, point.Y, convexHull, k));
        }
Beispiel #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="bezier"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        public static Vector2[] GetVertices(XBezier bezier, double dx, double dy)
        {
            Vector2[] vertices = new Vector2[4];
            int       k;

            Vector2[] convexHull;

            vertices[0] = new Vector2(bezier.Point1.X + dx, bezier.Point1.Y + dy);
            vertices[1] = new Vector2(bezier.Point2.X + dx, bezier.Point2.Y + dy);
            vertices[2] = new Vector2(bezier.Point3.X + dx, bezier.Point3.Y + dy);
            vertices[3] = new Vector2(bezier.Point4.X + dx, bezier.Point4.Y + dy);

            mc.ConvexHull(vertices, out convexHull, out k);

            return(convexHull.Take(k).ToArray());
        }
Beispiel #4
0
        /// <summary>
        /// Creates a new instance of the XBezier class.
        /// </summary>
        /// <param name="point1"></param>
        /// <param name="point2"></param>
        /// <param name="point3"></param>
        /// <param name="point4"></param>
        /// <param name="isStroked"></param>
        /// <param name="isFilled"></param>
        /// <returns></returns>
        public XBezier Bezier(
            XPoint point1,
            XPoint point2,
            XPoint point3,
            XPoint point4,
            bool isStroked = true,
            bool isFilled  = false)
        {
            var bezier = XBezier.Create(
                point1,
                point2,
                point3,
                point4,
                Context.Editor.Project.CurrentStyleLibrary.CurrentStyle,
                Context.Editor.Project.Options.PointShape,
                isStroked,
                isFilled);

            Context.Editor.AddWithHistory(bezier);
            return(bezier);
        }
Beispiel #5
0
        /// <summary>
        /// Creates a new instance of the XBezier class.
        /// </summary>
        /// <param name="x1"></param>
        /// <param name="y1"></param>
        /// <param name="x2"></param>
        /// <param name="y2"></param>
        /// <param name="x3"></param>
        /// <param name="y3"></param>
        /// <param name="x4"></param>
        /// <param name="y4"></param>
        /// <param name="isStroked"></param>
        /// <param name="isFilled"></param>
        /// <returns></returns>
        public XBezier Bezier(
            double x1      = 30, double y1 = 30,
            double x2      = 30, double y2 = 60,
            double x3      = 60, double y3 = 60,
            double x4      = 60, double y4 = 30,
            bool isStroked = true,
            bool isFilled  = false)
        {
            var bezier = XBezier.Create(
                x1, y1,
                x2, y2,
                x3, y3,
                x4, y4,
                Context.Editor.Project.CurrentStyleLibrary.CurrentStyle,
                Context.Editor.Project.Options.PointShape,
                isStroked,
                isFilled);

            Context.Editor.AddWithHistory(bezier);
            return(bezier);
        }
Beispiel #6
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="gfx"></param>
        /// <param name="bezier"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <param name="db"></param>
        /// <param name="r"></param>
        public void Draw(object gfx, XBezier bezier, double dx, double dy, ImmutableArray <ShapeProperty> db, Record r)
        {
            var _gfx = gfx as Graphics;

            Brush brush = ToSolidBrush(bezier.Style.Fill);
            Pen   pen   = ToPen(bezier.Style, _scaleToPage);

            if (bezier.IsFilled)
            {
                var path = new GraphicsPath();
                path.AddBezier(
                    _scaleToPage(bezier.Point1.X),
                    _scaleToPage(bezier.Point1.Y),
                    _scaleToPage(bezier.Point2.X),
                    _scaleToPage(bezier.Point2.Y),
                    _scaleToPage(bezier.Point3.X),
                    _scaleToPage(bezier.Point3.Y),
                    _scaleToPage(bezier.Point4.X),
                    _scaleToPage(bezier.Point4.Y));
                _gfx.FillPath(brush, path);
            }

            if (bezier.IsStroked)
            {
                _gfx.DrawBezier(
                    pen,
                    _scaleToPage(bezier.Point1.X),
                    _scaleToPage(bezier.Point1.Y),
                    _scaleToPage(bezier.Point2.X),
                    _scaleToPage(bezier.Point2.Y),
                    _scaleToPage(bezier.Point3.X),
                    _scaleToPage(bezier.Point3.Y),
                    _scaleToPage(bezier.Point4.X),
                    _scaleToPage(bezier.Point4.Y));
            }

            brush.Dispose();
            pen.Dispose();
        }
Beispiel #7
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="x"></param>
 /// <param name="y"></param>
 public override void LeftDown(double x, double y)
 {
     double sx = _editor.Project.Options.SnapToGrid ? Editor.Snap(x, _editor.Project.Options.SnapX) : x;
     double sy = _editor.Project.Options.SnapToGrid ? Editor.Snap(y, _editor.Project.Options.SnapY) : y;
     switch (_currentState)
     {
         case State.None:
             {
                 _shape = XBezier.Create(
                     sx, sy,
                     _editor.Project.CurrentStyleLibrary.CurrentStyle,
                     _editor.Project.Options.PointShape,
                     _editor.Project.Options.DefaultIsStroked,
                     _editor.Project.Options.DefaultIsFilled);
                 if (_editor.Project.Options.TryToConnect)
                 {
                     TryToConnectPoint1(_shape as XBezier, sx, sy);
                 }
                 _editor.Project.CurrentContainer.WorkingLayer.Shapes = _editor.Project.CurrentContainer.WorkingLayer.Shapes.Add(_shape);
                 _editor.Project.CurrentContainer.WorkingLayer.Invalidate();
                 ToStateOne();
                 Move(_shape as XBezier);
                 _editor.Project.CurrentContainer.HelperLayer.Invalidate();
                 _currentState = State.One;
                 _editor.CancelAvailable = true;
             }
             break;
         case State.One:
             {
                 var bezier = _shape as XBezier;
                 if (bezier != null)
                 {
                     bezier.Point3.X = sx;
                     bezier.Point3.Y = sy;
                     bezier.Point4.X = sx;
                     bezier.Point4.Y = sy;
                     if (_editor.Project.Options.TryToConnect)
                     {
                         TryToConnectPoint4(_shape as XBezier, sx, sy);
                     }
                     _editor.Project.CurrentContainer.WorkingLayer.Invalidate();
                     ToStateTwo();
                     Move(_shape as XBezier);
                     _editor.Project.CurrentContainer.HelperLayer.Invalidate();
                     _currentState = State.Two;
                 }
             }
             break;
         case State.Two:
             {
                 var bezier = _shape as XBezier;
                 if (bezier != null)
                 {
                     bezier.Point2.X = sx;
                     bezier.Point2.Y = sy;
                     if (_editor.Project.Options.TryToConnect)
                     {
                         TryToConnectPoint2(_shape as XBezier, sx, sy);
                     }
                     _editor.Project.CurrentContainer.WorkingLayer.Invalidate();
                     ToStateThree();
                     Move(_shape as XBezier);
                     _editor.Project.CurrentContainer.HelperLayer.Invalidate();
                     _currentState = State.Three;
                 }
             }
             break;
         case State.Three:
             {
                 var bezier = _shape as XBezier;
                 if (bezier != null)
                 {
                     bezier.Point3.X = sx;
                     bezier.Point3.Y = sy;
                     if (_editor.Project.Options.TryToConnect)
                     {
                         TryToConnectPoint3(_shape as XBezier, sx, sy);
                     }
                     _editor.Project.CurrentContainer.WorkingLayer.Shapes = _editor.Project.CurrentContainer.WorkingLayer.Shapes.Remove(_shape);
                     Remove();
                     Finalize(_shape as XBezier);
                     _editor.AddWithHistory(_shape);
                     _currentState = State.None;
                     _editor.CancelAvailable = false;
                 }
             }
             break;
     }
 }
Beispiel #8
0
 /// <summary>
 /// 
 /// </summary>
 /// <param name="bezier"></param>
 /// <param name="x"></param>
 /// <param name="y"></param>
 public void TryToConnectPoint4(XBezier bezier, double x, double y)
 {
     var result = ShapeBounds.HitTest(_editor.Project.CurrentContainer, new Vector2(x, y), _editor.Project.Options.HitTreshold);
     if (result != null && result is XPoint)
     {
         bezier.Point4 = result as XPoint;
     }
 }
Beispiel #9
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="gfx"></param>
        /// <param name="bezier"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <param name="db"></param>
        /// <param name="r"></param>
        public void Draw(object gfx, XBezier bezier, double dx, double dy, ImmutableArray<ShapeProperty> db, Record r)
        {
            var _gfx = gfx as Graphics;

            Brush brush = ToSolidBrush(bezier.Style.Fill);
            Pen pen = ToPen(bezier.Style, _scaleToPage);

            if (bezier.IsFilled)
            {
                var path = new GraphicsPath();
                path.AddBezier(
                    _scaleToPage(bezier.Point1.X),
                    _scaleToPage(bezier.Point1.Y),
                    _scaleToPage(bezier.Point2.X),
                    _scaleToPage(bezier.Point2.Y),
                    _scaleToPage(bezier.Point3.X),
                    _scaleToPage(bezier.Point3.Y),
                    _scaleToPage(bezier.Point4.X),
                    _scaleToPage(bezier.Point4.Y));
                _gfx.FillPath(brush, path);
            }

            if (bezier.IsStroked)
            {
                _gfx.DrawBezier(
                    pen,
                    _scaleToPage(bezier.Point1.X),
                    _scaleToPage(bezier.Point1.Y),
                    _scaleToPage(bezier.Point2.X),
                    _scaleToPage(bezier.Point2.Y),
                    _scaleToPage(bezier.Point3.X),
                    _scaleToPage(bezier.Point3.Y),
                    _scaleToPage(bezier.Point4.X),
                    _scaleToPage(bezier.Point4.Y));
            }

            brush.Dispose();
            pen.Dispose();
        }
Beispiel #10
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="bezier"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <param name="db"></param>
        /// <param name="r"></param>
        public void Draw(object dc, XBezier bezier, double dx, double dy, ImmutableArray<Property> db, Record r)
        {
            var _dc = dc as DrawingContext;

            var style = bezier.Style;
            if (style == null)
                return;

            double thickness = style.Thickness / _state.Zoom;
            double half = thickness / 2.0;

            Tuple<Brush, Pen> cache = null;
            Brush fill;
            Pen stroke;
            if (_enableStyleCache
                && _styleCache.TryGetValue(style, out cache))
            {
                fill = cache.Item1;
                stroke = cache.Item2;
            }
            else
            {
                fill = CreateBrush(style.Fill);
                stroke = CreatePen(style, thickness);
                if (_enableStyleCache)
                    _styleCache.Add(style, Tuple.Create(fill, stroke));
            }

            PathGeometry pg = null;
            if (_enableBezierCache
                && _bezierCache.TryGetValue(bezier, out pg))
            {
                var pf = pg.Figures[0];
                pf.StartPoint = new Point(bezier.Point1.X + dx, bezier.Point1.Y + dy);
                pf.IsFilled = bezier.IsFilled;
                var bs = pf.Segments[0] as BezierSegment;
                bs.Point1 = new Point(bezier.Point2.X + dx, bezier.Point2.Y + dy);
                bs.Point2 = new Point(bezier.Point3.X + dx, bezier.Point3.Y + dy);
                bs.Point3 = new Point(bezier.Point4.X + dx, bezier.Point4.Y + dy);
                bs.IsStroked = bezier.IsStroked;
            }
            else
            {
                var pf = new PathFigure()
                {
                    StartPoint = new Point(bezier.Point1.X + dx, bezier.Point1.Y + dy),
                    IsFilled = bezier.IsFilled
                };
                var bs = new BezierSegment(
                        new Point(bezier.Point2.X + dx, bezier.Point2.Y + dy),
                        new Point(bezier.Point3.X + dx, bezier.Point3.Y + dy),
                        new Point(bezier.Point4.X + dx, bezier.Point4.Y + dy),
                        bezier.IsStroked);
                //bs.Freeze();
                pf.Segments.Add(bs);
                //pf.Freeze();
                pg = new PathGeometry();
                pg.Figures.Add(pf);
                //pg.Freeze();

                if (_enableBezierCache)
                    _bezierCache.Add(bezier, pg);
            }

            DrawPathGeometryInternal(_dc, half, fill, stroke, bezier.IsStroked, bezier.IsFilled, pg);
        }
Beispiel #11
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        public override void LeftDown(double x, double y)
        {
            double sx = _editor.Project.Options.SnapToGrid ? Editor.Snap(x, _editor.Project.Options.SnapX) : x;
            double sy = _editor.Project.Options.SnapToGrid ? Editor.Snap(y, _editor.Project.Options.SnapY) : y;

            switch (_currentState)
            {
            case State.None:
            {
                _shape = XBezier.Create(
                    sx, sy,
                    _editor.Project.CurrentStyleLibrary.CurrentStyle,
                    _editor.Project.Options.PointShape,
                    _editor.Project.Options.DefaultIsStroked,
                    _editor.Project.Options.DefaultIsFilled);
                if (_editor.Project.Options.TryToConnect)
                {
                    TryToConnectPoint1(_shape as XBezier, sx, sy);
                }
                _editor.Project.CurrentContainer.WorkingLayer.Shapes = _editor.Project.CurrentContainer.WorkingLayer.Shapes.Add(_shape);
                _editor.Project.CurrentContainer.WorkingLayer.Invalidate();
                ToStateOne();
                Move(_shape as XBezier);
                _editor.Project.CurrentContainer.HelperLayer.Invalidate();
                _currentState           = State.One;
                _editor.CancelAvailable = true;
            }
            break;

            case State.One:
            {
                var bezier = _shape as XBezier;
                if (bezier != null)
                {
                    bezier.Point3.X = sx;
                    bezier.Point3.Y = sy;
                    bezier.Point4.X = sx;
                    bezier.Point4.Y = sy;
                    if (_editor.Project.Options.TryToConnect)
                    {
                        TryToConnectPoint4(_shape as XBezier, sx, sy);
                    }
                    _editor.Project.CurrentContainer.WorkingLayer.Invalidate();
                    ToStateTwo();
                    Move(_shape as XBezier);
                    _editor.Project.CurrentContainer.HelperLayer.Invalidate();
                    _currentState = State.Two;
                }
            }
            break;

            case State.Two:
            {
                var bezier = _shape as XBezier;
                if (bezier != null)
                {
                    bezier.Point2.X = sx;
                    bezier.Point2.Y = sy;
                    if (_editor.Project.Options.TryToConnect)
                    {
                        TryToConnectPoint2(_shape as XBezier, sx, sy);
                    }
                    _editor.Project.CurrentContainer.WorkingLayer.Invalidate();
                    ToStateThree();
                    Move(_shape as XBezier);
                    _editor.Project.CurrentContainer.HelperLayer.Invalidate();
                    _currentState = State.Three;
                }
            }
            break;

            case State.Three:
            {
                var bezier = _shape as XBezier;
                if (bezier != null)
                {
                    bezier.Point3.X = sx;
                    bezier.Point3.Y = sy;
                    if (_editor.Project.Options.TryToConnect)
                    {
                        TryToConnectPoint3(_shape as XBezier, sx, sy);
                    }
                    _editor.Project.CurrentContainer.WorkingLayer.Shapes = _editor.Project.CurrentContainer.WorkingLayer.Shapes.Remove(_shape);
                    Remove();
                    Finalize(_shape as XBezier);
                    _editor.AddWithHistory(_shape);
                    _currentState           = State.None;
                    _editor.CancelAvailable = false;
                }
            }
            break;
            }
        }
Beispiel #12
0
        public void Draw(object ds, XBezier bezier, double dx, double dy, ImmutableArray<Property> db, Record r)
        {
            var _ds = ds as CanvasDrawingSession;

            double thickness = bezier.Style.Thickness / _state.Zoom;
            var brush = ToColor(bezier.Style.Fill);
            var pen = ToColor(bezier.Style.Stroke);
            var ss = CreateStrokeStyle(bezier.Style);

            CanvasGeometry g;
            using (var builder = new CanvasPathBuilder(_ds))
            {
                builder.BeginFigure((float)bezier.Point1.X, (float)bezier.Point1.Y);
                builder.AddCubicBezier(
                    new N.Vector2(
                        (float)bezier.Point2.X,
                        (float)bezier.Point2.Y),
                    new N.Vector2(
                        (float)bezier.Point3.X,
                        (float)bezier.Point3.Y),
                    new N.Vector2(
                        (float)bezier.Point4.X,
                        (float)bezier.Point4.Y));
                builder.EndFigure(CanvasFigureLoop.Open);
                g = CanvasGeometry.CreatePath(builder);
            }

            if (bezier.IsFilled)
            {
                _ds.FillGeometry(g, brush);
            }

            if (bezier.IsStroked)
            {
                _ds.DrawGeometry(g, pen, (float)thickness, ss);
            }

            g.Dispose();
            ss.Dispose();
        }
Beispiel #13
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="dc"></param>
        /// <param name="bezier"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <param name="db"></param>
        /// <param name="r"></param>
        public void Draw(object dc, XBezier bezier, double dx, double dy, ImmutableArray<ShapeProperty> db, Record r)
        {
            if (!bezier.IsFilled && !bezier.IsStroked)
                return;

            var _dc = dc as DrawingContext;

            Brush brush = ToSolidBrush(bezier.Style.Fill);
            Pen pen = ToPen(bezier.Style, _scaleToPage);

            var sg = new StreamGeometry();
            using (var sgc = sg.Open())
            {
                sgc.BeginFigure(
                    new Point(bezier.Point1.X, bezier.Point1.Y),
                    bezier.IsFilled);

                sgc.BezierTo(
                    new Point(bezier.Point2.X, bezier.Point2.Y),
                    new Point(bezier.Point3.X, bezier.Point3.Y),
                    new Point(bezier.Point4.X, bezier.Point4.Y));

                sgc.EndFigure(false);
            }

            _dc.DrawGeometry(
                bezier.IsFilled ? brush : null,
                bezier.IsStroked ? pen : null,
                sg);

            // TODO: sg.Dispose();
            // TODO: brush.Dispose();
            // TODO: pen.Dispose();
        }