//顺序改变 private void Bitmaps_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { //Undo:撤销 //当在界面上拖动ListView改变顺序时,会因为List.Move方法而触发CollectionChanged集合改变事件 //可以根据集合改变事件new一个撤销类,再把上次的图层的顺序打进去。 //问题在于,无论List.Add还是List.Move都会触发集合改变事件, //于是记录List.Count存储到App.Model.LayersCount 里 //当新的List.Count和旧的记录数App.Model.LayersCount相等时,就知道这次的集合改变事件是List.Move触发的,打入撤销类 //如果不是就知道这是List,Add或List.Remove或别的什么方法触发的集合改变事件,就不撤销 if (undo != null) { if (undo.Layers != null) { if (App.Model.LayersCount == App.Model.Layers.Count) { if (App.Model.isCanUndo == true) { App.Model.Undos.Add(undo);//打入旧的撤销类 //更新撤销类 undo = new Undo(); undo.CollectionInstantiation(App.Model.Index, App.Model.Layers); } } } } App.Model.isReRender = true; //重新渲染 App.Model.Refresh++; //画布刷新 }
//Merge Visual:合并可见 private void LayerMergeVisualButton_Click(object sender, RoutedEventArgs e) { App.Model.isCanUndo = false;//关闭撤销 //更新撤销类 Undo undo = new Undo(); undo.CollectionInstantiation(App.Model.Index, App.Model.Layers); App.UndoAdd(undo); //新建渲染目标=>层 CanvasRenderTarget crt = new CanvasRenderTarget(App.Model.VirtualControl, App.Model.Width, App.Model.Height); using (CanvasDrawingSession ds = crt.CreateDrawingSession()) { ds.Clear(Colors.Transparent); ICanvasImage ci = App.Model.NullRenderTarget; for (int i = App.Model.Layers.Count - 1; i >= 0; i--) //自下而上渲染 { ci = App.Render(App.Model.Layers[i], ci); //渲染 } ds.DrawImage(ci); } //删掉所有可视图层 for (int i = App.Model.Layers.Count - 1; i >= 0; i--)//自下而上 { Layer L = App.Model.Layers[i]; if (L.Visual == true) { App.Model.Layers.Remove(L); } } //插入与索引 Layer l = new Layer { Name = App.resourceLoader.GetString("/Layer/NameMerge_"), Visual = true, Opacity = 100, CanvasRenderTarget = crt, }; if (App.Model.isLowView) { l.LowView(); } else { l.SquareView(); } l.SetWriteableBitmap(App.Model.VirtualControl);//刷新缩略图 App.Model.Layers.Insert(0, l); App.Model.Index = 0; App.Model.isCanUndo = true; //打开撤销 App.Model.LayersCount = App.Model.Layers.Count; //Undo:撤销 Jugde(); //判断 }
private void Undo() { if (App.Model.UndoIndex + 1 == App.Model.Undos.Count)//如果是第一次撤销 { //根据最后的一个撤销的类型,保留当前的状态为新的撤销类 Undo undo = new Undo(); switch (App.Model.Undos.Last().undeoType) { case UndeoType.Targe: undo.TargeInstantiation(App.Model.Index, App.Model.CurrentRenderTarget); break; case UndeoType.Mask: undo.MaskInstantiation(App.Model.MaskRenderTarget, App.Model.MaskAnimatedTarget); break; case UndeoType.Index: undo.IndexInstantiation(App.Model.Index); break; case UndeoType.Collection: undo.CollectionInstantiation(App.Model.Index, App.Model.Layers); break; //case UndeoTyte.LayerAdd :break; //case UndeoTyte.LayerRemove :break; case UndeoType.Visual: undo.VisualInstantiation(App.Model.Index, App.Model.Layers[App.Model.Index].Visual); break; case UndeoType.Opacity: undo.OpacityInstantiation(App.Model.Index, App.Model.Layers[App.Model.Index].Opacity); break; case UndeoType.Blend: undo.BlendInstantiation(App.Model.Index, App.Model.Layers[App.Model.Index].BlendIndex); break; case UndeoType.Tool: undo.ToolInstantiation(App.Model.Tool); break; default: break; } App.Model.Undos.Add(undo); } if (App.Model.Undos.Count > 2 && App.Model.UndoIndex >= 0) { App.Model.Undos[App.Model.UndoIndex].Perform(); //后退 App.Model.UndoIndex--; //撤销索引后退一步 App.Model.isRedo = true; //重做可用 } else { App.Model.isUndo = false; //撤销不可用 } }
public LayerControl() { this.InitializeComponent(); this.DataContext = App.Model; App.Model.Layers.CollectionChanged += Bitmaps_CollectionChanged; //挂集合变化事件 ListView.ItemsSource = App.Model.Layers; //绑定集合 //Undo:撤销 undo = new Undo(); undo.CollectionInstantiation(App.Model.Index, App.Model.Layers); }
//Merge All:全部合并 private void LayerMergeAllButton_Click(object sender, RoutedEventArgs e) { App.Model.isCanUndo = false;//关闭撤销 //更新撤销类 Undo undo = new Undo(); undo.CollectionInstantiation(App.Model.Index, App.Model.Layers); App.UndoAdd(undo); //新建渲染目标层 CanvasRenderTarget crt = new CanvasRenderTarget(App.Model.VirtualControl, App.Model.Width, App.Model.Height); using (CanvasDrawingSession ds = crt.CreateDrawingSession()) { ds.Clear(Colors.Transparent); //全部合并 ICanvasImage ci = App.Model.NullRenderTarget; for (int i = App.Model.Layers.Count - 1; i >= 0; i--) //自下而上渲染 { ci = App.Render(App.Model.Layers[i], ci); //渲染 } ds.DrawImage(ci); } //删掉所有图层 App.Model.Layers.Clear(); //插入层 Layer l = new Layer { Name = App.resourceLoader.GetString("/Layer/NameMerge_"), CanvasRenderTarget = crt, }; if (App.Model.isLowView) { l.LowView(); } else { l.SquareView(); } l.SetWriteableBitmap(App.Model.VirtualControl); App.Model.Layers.Add(l); App.Model.Index = 0; //不产生撤销类的改变索引的方法,适用于撤销的操作 App.Model.isCanUndo = true; //开启撤销 App.Model.LayersCount = App.Model.Layers.Count; //Undo:撤销 Jugde(); //判断 }