public void UpdateItemsSource() { if (RecipeItemsSource != null) { _lastSelectedNode = null; _mRecipe = RecipeItemsSource; if (_undoRedoHelper?.IsPaused != true) { _undoRedoHelper = new UndoRedoHelper <MRecipe>(ref _mRecipe); MRecipe.BeginGetNodes(_mRecipe, (baseNode) => { _undoRedoHelper.SaveStateOnPropertyChange(baseNode); }); _undoRedoHelper.SaveState(); } NotifyPropertyChanged(nameof(MRecipe)); } }
public MRecipeTreeViewUserControl() { InitializeComponent(); UndoCommand = new DelegateCommand( () => { Undo(); } ); RedoCommand = new DelegateCommand( () => { Redo(); } ); //ItemSelectedCommand = new DelegateCommand<MRecipeBaseNode>( // (selectedItem)=> { // NotifyPropertyChanged(nameof(CanPasteItem)); // NotifyPropertyChanged(nameof(CanCreateChild)); // // run receipe selected callback on a different thread // SelectNode(selectedItem); // } //); RightClickContextMenuCommand = new DelegateCommand <MRecipeBaseNode>( (selectedItem) => { _lastSelectedNode = selectedItem; NotifyPropertyChanged(nameof(CanPasteItem)); NotifyPropertyChanged(nameof(CanCreateChild)); } ); NewItemCommand = new DelegateCommand <MRecipeBaseNode>( (node) => { if (node is MRecipePlate plate) { (node.Parent as MRecipe).Plates.Add( new MRecipePlate() { Parent = node.Parent } ); } else if (node is MRecipeDevice device) { (node.Parent as MRecipePlate).Devices.Add( new MRecipeDevice() { Parent = node.Parent } ); } else if (node is MRecipeDeviceLayer layer) { (node.Parent as MRecipeDevice).Layers.Add( new MRecipeDeviceLayer() { Parent = node.Parent } ); } else { return; } lock (_undoRedoHelper) _undoRedoHelper.SaveState(); SelectNode(node); } ); CreateChildItemCommand = new DelegateCommand <MRecipeBaseNode>( (node) => { if (node is MRecipePlate plate) { plate.Devices.Add( new MRecipeDevice() { Parent = node } ); } else if (node is MRecipeDevice device) { device.Layers.Add( new MRecipeDeviceLayer() { Parent = node } ); } else { return; } lock (_undoRedoHelper) _undoRedoHelper.SaveState(); SelectNode(node); } ); DuplicateItemCommand = new DelegateCommand <MRecipeBaseNode>( (node) => { CopyItem((MRecipeBaseNode)node.Clone(), (MRecipeBaseNode)node, false); lock (_undoRedoHelper) _undoRedoHelper.SaveState(); SelectNode(node); } ); DuplicateItemAsArrayCommand = new DelegateCommand <MRecipeBaseNode>( (node) => { var dialog = new PasteAsArrayDialog(); dialog.Owner = Application.Current.MainWindow; if (dialog.ShowDialog() != true) { return; } try { lock (_undoRedoHelper) _undoRedoHelper?.Pause(); MArrayInfo.BeginGetAll(dialog.ArrayInfo, (positionInfo) => { var clone = (MRecipeBaseNode)node.Clone(); clone.TransformInfo.OffsetX += positionInfo.XInfo.Offset; clone.TransformInfo.OffsetY += positionInfo.XInfo.Offset; clone.TransformInfo.OffsetZ += positionInfo.XInfo.Offset; CopyItem(clone, node, false); }); } finally { lock (_undoRedoHelper) { _undoRedoHelper?.Resume(); _undoRedoHelper.SaveState(); } } SelectNode(node); } ); CopyItemCommand = new DelegateCommand <MRecipeBaseNode>( (node) => { if (node != null) { _copiedItem = node; } } ); PasteItemCommand = new DelegateCommand <MRecipeBaseNode>( (node) => { if (node == null) { return; } // update selected node _lastSelectedNode = node; if (CanPasteItem == false) { return; } CopyItem((MRecipeBaseNode)_copiedItem.Clone(), _lastSelectedNode, false); SelectNode(node); } ); PasteItemAsArrayCommand = new DelegateCommand <MRecipeBaseNode>( (node) => { if (node == null) { return; } // update selected node _lastSelectedNode = node; if (CanPasteItem == false) { return; } var dialog = new PasteAsArrayDialog(); dialog.Owner = Application.Current.MainWindow; if (dialog.ShowDialog() != true) { return; } var selectedNode = _lastSelectedNode; try { lock (_undoRedoHelper) _undoRedoHelper?.Pause(); for (int x = 0; x < dialog.ArrayInfo.NumX; x++) { for (int y = 0; y < dialog.ArrayInfo.NumY; y++) { for (int z = 0; z < dialog.ArrayInfo.NumZ; z++) { var clone = (MRecipeBaseNode)_copiedItem.Clone(); clone.TransformInfo.OffsetX += x * dialog.ArrayInfo.PitchX; clone.TransformInfo.OffsetY += y * dialog.ArrayInfo.PitchY; clone.TransformInfo.OffsetZ += z * dialog.ArrayInfo.PitchZ; CopyItem(clone, selectedNode, false); } } } } finally { lock (_undoRedoHelper) { _undoRedoHelper?.Resume(); _undoRedoHelper.SaveState(); } } SelectNode(node); } ); DeleteItemCommand = new DelegateCommand <MRecipeBaseNode>( (node) => { if (node is MRecipePlate plate) { (node.Parent as MRecipe).Plates.Remove( plate ); } else if (node is MRecipeDevice device) { (node.Parent as MRecipePlate).Devices.Remove( device ); } else if (node is MRecipeDeviceLayer layer) { (node.Parent as MRecipeDevice).Layers.Remove( layer ); } else { return; } lock (_undoRedoHelper) _undoRedoHelper.SaveState(); SelectNode(node); } ); }