Exemplo n.º 1
0
        public override async void OnDrawBegin(IModel sender, PointerPoint args)
        {
            var layer = sender.CurrentLayer;

            if (Clipper.IsCliping)
            {
                layer.getRect(out Rect orec, out WriteableBitmap obb);
                var Bitmap = new WriteableBitmap((int)DrawRect.Width, (int)DrawRect.Height);
                IGrap.copyImg(obb, Bitmap, (int)orec.X, (int)orec.Y);

                layer.Child = Clipper.createPolygon(Bitmap);

                o = (Point)layer.Child.Tag;
                a = (Vec2)args.Position;


                obmp = layer.Bitmap;
                cbmp = await(tt = layer.Child.Render());
                if (layer.Bitmap != null)
                {
                    layer.Bitmap = layer.Bitmap.Clone();
                    IGrap.delImg(cbmp, layer.Bitmap, (int)(o.X - orec.X), (int)(o.Y - orec.Y));
                    layer.Bitmap.Invalidate();
                }
                //((DrawPanel)sender).X.Source = cbmp;
            }
            else
            {
                o = layer.Point;
                a = args.Position;
            }
        }
Exemplo n.º 2
0
        public override async void OnToolState(IModel sender, bool state)
        {
            var layer = sender.CurrentLayer;

            if (!layer.IsEdit || !layer.IsShow)
            {
                return;
            }
            if (layer.Bitmap == null)
            {
                return;
            }
            Debug.WriteLine("state:" + state);
            if (state)
            {
                layer.getRect(out orec, out obb);
                if (Clipper.IsCliping)
                {
                    VModel.vm.Loading = true;
                    //拷贝选区
                    var Bitmap = new WriteableBitmap((int)DrawRect.Width, (int)DrawRect.Height);
                    IGrap.copyImg(obb, Bitmap, (int)orec.X, (int)orec.Y);
                    layer.Child = Clipper.createPolygon(Bitmap);


                    var p  = (Point)layer.Child.Tag;
                    var xb = await(layer.Child.Render());
                    Clipper.Points.Clear();
                    layer.Child = null;
                    if (xb != null)
                    {
                        var i  = sender.Layers.IndexOf(layer);
                        var nb = obb;
                        var ob = obb;
                        if (layer.Bitmap != null)
                        {
                            nb = layer.Bitmap.Clone();
                            IGrap.delImg(xb, nb, (int)(p.X - orec.X), (int)(p.Y - orec.Y));
                            nb.Invalidate();
                        }
                        obb  = xb;
                        orec = new Rect(p.X, p.Y, xb.PixelWidth, xb.PixelHeight);
                        Exec.Do(new Exec()
                        {
                            exec = () => {
                                sender.Layers[i].Bitmap = nb;
                                sender.Layers.Insert(i, new LayerModel()
                                {
                                    Bitmap = xb,
                                    X      = p.X,
                                    Y      = p.Y
                                });
                                sender.CurrentLayer = sender.Layers[i];
                            },
                            undo = () => {
                                sender.Layers.RemoveAt(i);
                                sender.CurrentLayer        = sender.Layers[i];
                                sender.CurrentLayer.Bitmap = ob;
                            }
                        });
                    }

                    VModel.vm.Loading = false;
                }
                sender.ElemArea.Child = CreateRect(sender);
                ch = false;
            }
            else
            {
                sender.ElemArea.Child = null;

                if (!ch)
                {
                    return;
                }
                var vc = new CompositeTransform()
                {
                    Rotation = com.Rotation,
                    ScaleX   = com.ScaleX,
                    ScaleY   = com.ScaleY,
                    CenterX  = com.TranslateX + layer.W / 2,
                    CenterY  = com.TranslateY + layer.H / 2
                };

                var or = orec;
                var ob = obb;
                layer.getRect(out Rect nr, out WriteableBitmap nb);
                if (com.ScaleX != 1 || com.ScaleY != 1 || com.Rotation != 0)
                {
                    var elem = (FrameworkElement)((DrawPanel)sender).ITEMS.ContainerFromItem(layer);

                    VModel.vm.Loading = true;
                    var b = await(elem).Render();
                    VModel.vm.Loading = false;

                    var vr = vc.TransformBounds(nr);
                    nr = RectHelper.Intersect(DrawRect, vr);
                    if (nr.IsEmpty)
                    {
                        nb = null;
                        layer.setRect(nr, nb);
                    }
                    else
                    {
                        nb = new WriteableBitmap((int)nr.Width, (int)nr.Height);
                        IGrap.addImg(b, nb, (int)(vr.X - nr.X), (int)(vr.Y - nr.Y));
                        layer.setRect(nr, nb);
                    }
                    com.Rotation = 0;
                    com.ScaleX   = 1;
                    com.ScaleY   = 1;
                }
                var i = sender.Layers.IndexOf(sender.CurrentLayer);
                Exec.Save(new Exec()
                {
                    exec = () => {
                        sender.Layers[i].setRect(nr, nb);
                    },
                    undo = () => {
                        sender.Layers[i].setRect(or, ob);
                    }
                });
            }
        }