Example #1
0
        private void SetSelectedRectangle(Point beginPoint, Point endPoint)
        {
            // Make sure x/y are top-left, and width/height is bottom-right
            Point a = Utils.Min(beginPoint, endPoint);
            Point b = Utils.Max(beginPoint, endPoint);

            var newRect = Rectangle.FromLTRB(a.X, a.Y, b.X, b.Y);

            // Determine if there is a need to update shapes
            if (selectedRectangle == newRect)
            {
                return;
            }

            // Update selected rectangle
            selectedRectangle = newRect;

            // Update selected shapes
            var shapes = Canvas.Instance.GetShapesByRectangle(selectedRectangle);

            ClickData.Clear(true);
            ClickData.Set(shapes);

            // Update borders of selected shapes
            // This may be needed
            Canvas.Instance.Invalidate();
        }
Example #2
0
        private void OnMouseDown_HandleDragSelection(object sender, MouseEventArgs e, Point mouseLocation, Point snappedLocation, bool setShapes)
        {
            if (IsMouseOverSelectedRectangle(mouseLocation))
            {
                if (setShapes)
                {
                    var shapes = Canvas.Instance.GetShapesByRectangle(selectedRectangle);
                    ClickData.Set(shapes);
                }
                mouseDownLocation = snappedLocation;

                ClickData.Set(snappedLocation, ShapeClickAction.Drag);

                selectedRectangleDownLocation = selectedRectangle.Location;
                action = SelectorAction.BeginMoveSelectedShapes;
                OnMouseMove(sender, e);
            }
            else
            {
                if (!MouseWasDown)
                {
                    action = SelectorAction.BeginSelectionRectangle;

                    mouseDownLocation             = new Point();
                    selectedRectangleDownLocation = new Point();
                    ClickData.Clear(true);

                    OnMouseDown(sender, e);
                }
            }
        }
        public override void OnMouseDown(object sender, MouseEventArgs e)
        {
            // Only run during initial press
            if (ClickData.Action != ShapeClickAction.None)
            {
                return;
            }

            using (GraphicsPath path = new GraphicsPath(FillMode.Alternate))
            {
                var location = e.Location;
                ClickData.Origin = Grid.SnapToGrid(e.Location);
                var shape = Canvas.Instance.layer.GetShapeByPoint(path, location);

                switch (e.Button)
                {
                case MouseButtons.Right:
                    SharedActions.RemoveShape(shape);
                    break;

                case MouseButtons.Middle:
                    if (shape != null && shape.Type == ShapeType.Triangle)
                    {
                        SharedActions.TriangleIncrementAngle(shape);
                    }
                    break;

                case MouseButtons.Left:
                    bool createShape = true;
                    if (!KeyboardController.IsShiftDown && shape != null)
                    {
                        var action = shape.GetShapeActionByPoint(path, location);
                        if (action != ShapeClickAction.None)
                        {
                            ClickData.Set(action, shape);
                            createShape = false;
                            OnMouseMove(sender, e);
                        }
                        else
                        {
                            throw new InvalidOperationException(
                                      "Shape was found under Point, but action wasn't - This shouldn't happen."
                                      );
                        }
                    }
                    if (createShape)
                    {
                        var newSize     = new Size(20, 20);
                        var sizeSnapped = Grid.SnapToGrid(newSize, Grid.SnapSizeToGrid);
                        GenerateShape(sizeSnapped);
                    }
                    break;
                }
            }

            Canvas.Instance.Invalidate();
        }
Example #4
0
        internal void Replace(Shape shape, Shape newShape)
        {
            // If the shape was selected, select replacement shape once created.
            bool wasShapeClicked = ClickData.ContainsShapes(shape);

            // Replace old shape with new shape
            var index = shapes.IndexOf(shape);

            shapes[index] = newShape;

            if (wasShapeClicked)
            {
                ClickData.Set(newShape);
            }
        }
        private void GenerateShape(Size size)
        {
            var layer = Canvas.Instance.layer;

            // Generate shape based on either a duplicate or a new shape
            Shape shape;

            if (!ClickData.IsShapesEmpty() && KeyboardController.IsControlDown)
            {
                shape = layer.DuplicateShape(ClickData.Shapes[0], ClickData.Origin);
            }
            else
            {
                shape = layer.AddNewShape(ClickData.Origin, size,
                                          Canvas.Instance.GetSelectedColor(),
                                          Canvas.Instance.GetSelectedShapeType()
                                          );
            }

            // Force new shape to go into resize mode.
            ClickData.Set(ShapeClickAction.Resize, shape);
        }
Example #6
0
        public override void OnMouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button != MouseButtons.Left)
            {
                return;
            }

            var mouseLocation   = e.Location;
            var snappedLocation = Grid.SnapToGrid(mouseLocation);

            switch (action)
            {
            case SelectorAction.BeginSelectionRectangle:
                SetSelectedRectangle(mouseDownLocation, snappedLocation);
                break;

            case SelectorAction.BeginMoveSelectedShapes:
                Canvas.Instance.Focus();

                ClickData.Set(ShapeClickAction.Drag);
                ClickData.ShapeUpdateOffset(snappedLocation);

                Point moveTo = new Point(
                    snappedLocation.X - mouseDownLocation.X,
                    snappedLocation.Y - mouseDownLocation.Y
                    );

                selectedRectangle.X = selectedRectangleDownLocation.X + moveTo.X;
                selectedRectangle.Y = selectedRectangleDownLocation.Y + moveTo.Y;
                break;

            default:
                throw EnumNotImplementedException.Throw(action, ExceptionMessages.MSG_NOT_YET_IMPLEMENTED);
            }

            Canvas.Instance.Invalidate();
        }