Rectangle shape.
Inheritance: XText
Example #1
0
 /// <summary>
 /// Initialize new instance of <see cref="RectangleSelection"/> class.
 /// </summary>
 /// <param name="layer">The selection shapes layer.</param>
 /// <param name="shape">The selected shape.</param>
 /// <param name="style">The selection shapes style.</param>
 /// <param name="point">The selection point shape.</param>
 public RectangleSelection(XLayer layer, XRectangle shape, ShapeStyle style, BaseShape point)
 {
     _layer = layer;
     _rectangle = shape;
     _style = style;
     _point = point;
 }
Example #2
0
        /// <inheritdoc/>
        public override void LeftDown(double x, double y)
        {
            base.LeftDown(x, y);
            var editor = _serviceProvider.GetService<ProjectEditor>();
            double sx = editor.Project.Options.SnapToGrid ? ProjectEditor.Snap(x, editor.Project.Options.SnapX) : x;
            double sy = editor.Project.Options.SnapToGrid ? ProjectEditor.Snap(y, editor.Project.Options.SnapY) : y;
            switch (_currentState)
            {
                case ToolState.None:
                    {
                        var style = editor.Project.CurrentStyleLibrary.Selected;
                        _rectangle = XRectangle.Create(
                            sx, sy,
                            editor.Project.Options.CloneStyle ? style.Clone() : style,
                            editor.Project.Options.PointShape,
                            editor.Project.Options.DefaultIsStroked,
                            editor.Project.Options.DefaultIsFilled);

                        var result = editor.TryToGetConnectionPoint(sx, sy);
                        if (result != null)
                        {
                            _rectangle.TopLeft = result;
                        }

                        editor.Project.CurrentContainer.WorkingLayer.Shapes = editor.Project.CurrentContainer.WorkingLayer.Shapes.Add(_rectangle);
                        editor.Project.CurrentContainer.WorkingLayer.Invalidate();
                        ToStateOne();
                        Move(_rectangle);
                        _currentState = ToolState.One;
                        editor.CancelAvailable = true;
                    }
                    break;
                case ToolState.One:
                    {
                        if (_rectangle != null)
                        {
                            _rectangle.BottomRight.X = sx;
                            _rectangle.BottomRight.Y = sy;

                            var result = editor.TryToGetConnectionPoint(sx, sy);
                            if (result != null)
                            {
                                _rectangle.BottomRight = result;
                            }

                            editor.Project.CurrentContainer.WorkingLayer.Shapes = editor.Project.CurrentContainer.WorkingLayer.Shapes.Remove(_rectangle);
                            Remove();
                            Finalize(_rectangle);
                            editor.Project.AddShape(editor.Project.CurrentContainer.CurrentLayer, _rectangle);
                            _currentState = ToolState.None;
                            editor.CancelAvailable = false;
                        }
                    }
                    break;
            }
        }
Example #3
0
        /// <summary>
        /// Transfer selection state to <see cref="ToolState.One"/>.
        /// </summary>
        public void ToStateOne()
        {
            _helperRectangle = XRectangle.Create(0, 0, _style, null);
            _topLeftHelperPoint = XPoint.Create(0, 0, _point);
            _bottomRightHelperPoint = XPoint.Create(0, 0, _point);

            _layer.Shapes = _layer.Shapes.Add(_helperRectangle);
            _layer.Shapes = _layer.Shapes.Add(_topLeftHelperPoint);
            _layer.Shapes = _layer.Shapes.Add(_bottomRightHelperPoint);
        }
Example #4
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rectangle"></param>
        /// <param name="v"></param>
        /// <param name="threshold"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        public static BaseShape HitTestRectangle(XRectangle rectangle, Vector2 v, double threshold, double dx, double dy)
        {
            if (ShapeBounds.GetPointBounds(rectangle.TopLeft, threshold, dx, dy).Contains(v))
            {
                return rectangle.TopLeft;
            }

            if (ShapeBounds.GetPointBounds(rectangle.BottomRight, threshold, dx, dy).Contains(v))
            {
                return rectangle.BottomRight;
            }

            if (ShapeBounds.GetRectangleBounds(rectangle, dx, dy).Contains(v))
            {
                return rectangle;
            }

            return null;
        }
Example #5
0
        /// <inheritdoc/>
        public override void Draw(object dc, XRectangle rectangle, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
        {
            var _dc = dc as DrawingContext;

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

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

            Tuple<Brush, Pen> styleCached = _styleCache.Get(style);
            Brush fill;
            Pen stroke;
            if (styleCached != null)
            {
                fill = styleCached.Item1;
                stroke = styleCached.Item2;
            }
            else
            {
                fill = CreateBrush(style.Fill);
                stroke = CreatePen(style, thickness);
                _styleCache.Set(style, Tuple.Create(fill, stroke));
            }

            var rect = CreateRect(rectangle.TopLeft, rectangle.BottomRight, dx, dy);

            DrawRectangleInternal(_dc, half, fill, stroke, rectangle.IsStroked, rectangle.IsFilled, ref rect);

            if (rectangle.IsGrid)
            {
                DrawGridInternal(_dc, half, stroke, ref rect, rectangle.OffsetX, rectangle.OffsetY, rectangle.CellWidth, rectangle.CellHeight, true);
            }
        }
Example #6
0
        /// <inheritdoc/>
        public override void Draw(object dc, XRectangle rectangle, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
        {
            var canvas = dc as SKCanvas;

            using (SKPaint brush = ToSKPaintBrush(rectangle.Style.Fill))
            using (SKPaint pen = ToSKPaintPen(rectangle.Style, _scaleToPage, _sourceDpi, _targetDpi))
            {
                var rect = CreateRect(rectangle.TopLeft, rectangle.BottomRight, dx, dy, _scaleToPage);
                DrawRectangleInternal(canvas, brush, pen, rectangle.IsStroked, rectangle.IsFilled, ref rect);
                if (rectangle.IsGrid)
                {
                    DrawGridInternal(
                        canvas,
                        pen,
                        ref rect,
                        rectangle.OffsetX,
                        rectangle.OffsetY,
                        rectangle.CellWidth,
                        rectangle.CellHeight,
                        isStroked: true);
                }
            }
        }
Example #7
0
        /// <inheritdoc/>
        public override void LeftDown(double x, double y)
        {
            base.LeftDown(x, y);
            var editor = _serviceProvider.GetService<ProjectEditor>();
            switch (_currentState)
            {
                case ToolState.None:
                    {
                        editor.Dehover(editor.Project.CurrentContainer.CurrentLayer);
                        if (editor.Renderers[0].State.SelectedShape == null
                            && editor.Renderers[0].State.SelectedShapes != null)
                        {
                            var result = ShapeHitTestPoint.HitTest(editor.Project.CurrentContainer.CurrentLayer.Shapes, new Vector2(x, y), editor.Project.Options.HitThreshold);
                            if (result != null)
                            {
                                _startX = editor.Project.Options.SnapToGrid ? ProjectEditor.Snap(x, editor.Project.Options.SnapX) : x;
                                _startY = editor.Project.Options.SnapToGrid ? ProjectEditor.Snap(y, editor.Project.Options.SnapY) : y;
                                _historyX = _startX;
                                _historyY = _startY;
                                GenerateMoveSelectionCache();
                                _currentState = ToolState.One;
                                editor.CancelAvailable = true;
                                break;
                            }
                        }

                        if (editor.TryToSelectShape(editor.Project.CurrentContainer.CurrentLayer, x, y))
                        {
                            _startX = editor.Project.Options.SnapToGrid ? ProjectEditor.Snap(x, editor.Project.Options.SnapX) : x;
                            _startY = editor.Project.Options.SnapToGrid ? ProjectEditor.Snap(y, editor.Project.Options.SnapY) : y;
                            _historyX = _startX;
                            _historyY = _startY;
                            GenerateMoveSelectionCache();
                            _currentState = ToolState.One;
                            editor.CancelAvailable = true;
                            break;
                        }

                        _rectangle = XRectangle.Create(
                            x, y,
                            editor.Project.Options.SelectionStyle,
                            null,
                            true, true);
                        editor.Project.CurrentContainer.WorkingLayer.Shapes = editor.Project.CurrentContainer.WorkingLayer.Shapes.Add(_rectangle);
                        editor.Project.CurrentContainer.WorkingLayer.Invalidate();
                        _currentState = ToolState.One;
                        editor.CancelAvailable = true;
                    }
                    break;
                case ToolState.One:
                    {
                        if (_rectangle != null)
                        {
                            _rectangle.BottomRight.X = x;
                            _rectangle.BottomRight.Y = y;
                            editor.Project.CurrentContainer.WorkingLayer.Shapes = editor.Project.CurrentContainer.WorkingLayer.Shapes.Remove(_rectangle);
                            editor.Project.CurrentContainer.WorkingLayer.Invalidate();
                            _currentState = ToolState.None;
                            editor.CancelAvailable = false;
                        }
                    }
                    break;
            }
        }
Example #8
0
 /// <inheritdoc/>
 public override void Draw(object dc, XRectangle rectangle, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
 {
     // TODO: Implement Draw rectangle.
 }
Example #9
0
        /// <inheritdoc/>
        public override void Draw(object dc, XRectangle rectangle, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
        {
            var _dc = dc as AM.DrawingContext;

            AM.IBrush brush = ToBrush(rectangle.Style.Fill);
            AM.Pen pen = ToPen(rectangle.Style, _scaleToPage);

            var rect = CreateRect(rectangle.TopLeft, rectangle.BottomRight, dx, dy);

            DrawRectangleInternal(
                _dc,
                brush,
                pen,
                rectangle.IsStroked,
                rectangle.IsFilled,
                ref rect);

            if (rectangle.IsGrid)
            {
                DrawGridInternal(
                    _dc,
                    pen,
                    ref rect,
                    rectangle.OffsetX, rectangle.OffsetY,
                    rectangle.CellWidth, rectangle.CellHeight,
                    true);
            }
        }
Example #10
0
 /// <summary>
 /// Draws a <see cref="XRectangle"/> shape using drawing context.
 /// </summary>
 /// <param name="dc">The native drawing context.</param>
 /// <param name="rectangle">The <see cref="XRectangle"/> shape.</param>
 /// <param name="dx">The X coordinate offset.</param>
 /// <param name="dy">The Y coordinate offset.</param>
 /// <param name="db">The properties database.</param>
 /// <param name="r">The data record.</param>
 public abstract void Draw(object dc, XRectangle rectangle, double dx, double dy, ImmutableArray<XProperty> db, XRecord r);
Example #11
0
        /// <summary>
        /// Remove selection.
        /// </summary>
        public void Remove()
        {
            if (_helperRectangle != null)
            {
                _layer.Shapes = _layer.Shapes.Remove(_helperRectangle);
                _helperRectangle = null;
            }

            if (_topLeftHelperPoint != null)
            {
                _layer.Shapes = _layer.Shapes.Remove(_topLeftHelperPoint);
                _topLeftHelperPoint = null;
            }

            if (_bottomRightHelperPoint != null)
            {
                _layer.Shapes = _layer.Shapes.Remove(_bottomRightHelperPoint);
                _bottomRightHelperPoint = null;
            }

            _layer.Invalidate();
        }
Example #12
0
        /// <inheritdoc/>
        public override void Draw(object dc, XRectangle rectangle, double dx, double dy, ImmutableArray<XProperty> db, XRecord r)
        {
            var _gfx = dc as Graphics;

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

            var rect = CreateRect(
                rectangle.TopLeft,
                rectangle.BottomRight,
                dx, dy);

            if (rectangle.IsFilled)
            {
                _gfx.FillRectangle(
                    brush,
                    _scaleToPage(rect.X),
                    _scaleToPage(rect.Y),
                    _scaleToPage(rect.Width),
                    _scaleToPage(rect.Height));
            }

            if (rectangle.IsStroked)
            {
                _gfx.DrawRectangle(
                    pen,
                    _scaleToPage(rect.X),
                    _scaleToPage(rect.Y),
                    _scaleToPage(rect.Width),
                    _scaleToPage(rect.Height));
            }

            if (rectangle.IsGrid)
            {
                DrawGridInternal(
                    _gfx,
                    pen,
                    ref rect,
                    rectangle.OffsetX, rectangle.OffsetY,
                    rectangle.CellWidth, rectangle.CellHeight,
                    true);
            }

            brush.Dispose();
            pen.Dispose();
        }
Example #13
0
 public void Inherits_From_XText()
 {
     var target = new XRectangle();
     Assert.True(target is XText);
 }
Example #14
0
 /// <summary>
 /// Get the bounding rectangle for <see cref="XRectangle"/> shape.
 /// </summary>
 /// <param name="rectangle"></param>
 /// <param name="dx"></param>
 /// <param name="dy"></param>
 /// <returns></returns>
 public static Rect2 GetRectangleBounds(XRectangle rectangle, double dx, double dy)
 {
     return Rect2.Create(rectangle.TopLeft, rectangle.BottomRight, dx, dy);
 }
Example #15
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="rectangle"></param>
        /// <param name="rect"></param>
        /// <param name="selected"></param>
        /// <param name="dx"></param>
        /// <param name="dy"></param>
        /// <returns></returns>
        public static bool HitTestRectangle(XRectangle rectangle, Rect2 rect, ISet<BaseShape> selected, double dx, double dy)
        {
            if (ShapeBounds.GetRectangleBounds(rectangle, dx, dy).IntersectsWith(rect))
            {
                if (selected != null)
                {
                    selected.Add(rectangle);
                    return false;
                }
                else
                {
                    return true;
                }
            }

            return false;
        }