Exemplo n.º 1
0
        public static void Distribute(IEnumerable <IBaseShape> shapes, DistributeMode mode, IHistory history)
        {
            var groupBox = new GroupBox(shapes.ToList());

            if (groupBox.Boxes.Length <= 2)
            {
                return;
            }

            var boxes = groupBox.Boxes.ToList();

            double sw = 0.0;
            double sh = 0.0;

            foreach (var box in boxes)
            {
                sw += box.Bounds.Width;
                sh += box.Bounds.Height;
            }

            double gaph = (groupBox.Bounds.Width - sw) / (groupBox.Boxes.Length - 1);
            double gapv = (groupBox.Bounds.Height - sh) / (groupBox.Boxes.Length - 1);

            switch (mode)
            {
            case DistributeMode.Horizontal:
            {
                boxes.Sort(ShapeBox.CompareLeft);
                double offset = boxes[0].Bounds.Left + boxes[0].Bounds.Width + gaph;
                for (int i = 1; i <= boxes.Count - 2; i++)
                {
                    var    box = boxes[i];
                    double dx  = offset - box.Bounds.Left;
                    double dy  = 0.0;
                    box.MoveByWithHistory(dx, dy, history);
                    offset += box.Bounds.Width + gaph;
                }
            }
            break;

            case DistributeMode.Vertical:
            {
                boxes.Sort(ShapeBox.CompareTop);
                double offset = boxes[0].Bounds.Top + boxes[0].Bounds.Height + gapv;
                for (int i = 1; i <= boxes.Count - 2; i++)
                {
                    var    box = boxes[i];
                    double dx  = 0.0;
                    double dy  = offset - box.Bounds.Top;
                    box.MoveByWithHistory(dx, dy, history);
                    offset += box.Bounds.Height + gapv;
                }
            }
            break;
            }
        }
Exemplo n.º 2
0
        public static void Distribute(IToolContext context, DistributeMode mode)
        {
            if (context.DocumentContainer?.ContainerView?.SelectionState != null)
            {
                var shapes = new List <IBaseShape>(context.DocumentContainer.ContainerView.SelectionState?.Shapes);
                var boxes  = new List <Box>();

                foreach (var shape in shapes)
                {
                    if (!(shape is IPointShape))
                    {
                        boxes.Add(new Box(shape));
                    }
                }

                if (boxes.Count > 2)
                {
                    context.DocumentContainer?.ContainerView?.SelectionState?.Dehover();
                    context.DocumentContainer?.ContainerView?.SelectionState?.Clear();

                    var bounds = new Bounds(boxes);

                    double sw = 0.0;
                    double sh = 0.0;

                    foreach (var box in boxes)
                    {
                        sw += box.w;
                        sh += box.h;
                    }

                    double gaph = (bounds.w - sw) / (boxes.Count - 1);
                    double gapv = (bounds.h - sh) / (boxes.Count - 1);

                    switch (mode)
                    {
                    case DistributeMode.Horizontal:
                    {
                        boxes.Sort(Box.CompareHorizontalLeft);
                        boxes[0].shape.Select(context.DocumentContainer.ContainerView.SelectionState);
                        double offset = boxes[0].ax + boxes[0].w + gaph;
                        for (int i = 1; i <= boxes.Count - 2; i++)
                        {
                            var    box = boxes[i];
                            double dx  = offset - box.ax;
                            box.shape.Move(context.DocumentContainer.ContainerView.SelectionState, dx, 0.0);
                            box.shape.Select(context.DocumentContainer.ContainerView.SelectionState);
                            offset += box.w + gaph;
                        }
                        boxes[boxes.Count - 1].shape.Select(context.DocumentContainer.ContainerView.SelectionState);
                    }
                    break;

                    case DistributeMode.Vertical:
                    {
                        boxes.Sort(Box.CompareVerticalTop);
                        boxes[0].shape.Select(context.DocumentContainer.ContainerView.SelectionState);
                        double offset = boxes[0].ay + boxes[0].h + gapv;
                        for (int i = 1; i <= boxes.Count - 2; i++)
                        {
                            var    box = boxes[i];
                            double dy  = offset - box.ay;
                            box.shape.Move(context.DocumentContainer.ContainerView.SelectionState, 0.0, dy);
                            box.shape.Select(context.DocumentContainer.ContainerView.SelectionState);
                            offset += box.h + gapv;
                        }
                        boxes[boxes.Count - 1].shape.Select(context.DocumentContainer.ContainerView.SelectionState);
                    }
                    break;
                    }

                    context.DocumentContainer?.ContainerView?.InputService?.Redraw?.Invoke();
                }
            }
        }
Exemplo n.º 3
0
        public static void Distribute(IEnumerable <IBaseShape> shapes, DistributeMode mode, IHistory history)
        {
            var boxes = new List <ShapeBox>();

            foreach (var shape in shapes)
            {
                boxes.Add(new ShapeBox(shape));
            }

            if (boxes.Count <= 2)
            {
                return;
            }

            var bounds = new GroupBox(boxes);

            double sw = 0.0;
            double sh = 0.0;

            foreach (var box in boxes)
            {
                sw += box.Width;
                sh += box.Height;
            }

            double gaph = (bounds.Width - sw) / (boxes.Count - 1);
            double gapv = (bounds.Height - sh) / (boxes.Count - 1);

            switch (mode)
            {
            case DistributeMode.Horizontal:
            {
                boxes.Sort(ShapeBox.CompareLeft);
                double offset = boxes[0].Left + boxes[0].Width + gaph;
                for (int i = 1; i <= boxes.Count - 2; i++)
                {
                    var    box = boxes[i];
                    double dx  = offset - box.Left;
                    double dy  = 0.0;
                    MoveShapeByWithHistory(box.Shape, dx, dy, history);
                    offset += box.Width + gaph;
                }
            }
            break;

            case DistributeMode.Vertical:
            {
                boxes.Sort(ShapeBox.CompareTop);
                double offset = boxes[0].Top + boxes[0].Height + gapv;
                for (int i = 1; i <= boxes.Count - 2; i++)
                {
                    var    box = boxes[i];
                    double dx  = 0.0;
                    double dy  = offset - box.Top;
                    MoveShapeByWithHistory(box.Shape, dx, dy, history);
                    offset += box.Height + gapv;
                }
            }
            break;
            }
        }