Example #1
0
        public List <AssetViewModel> PasteAssets(List <AssetItem> assets, [CanBeNull] ProjectViewModel project)
        {
            var viewModels = new List <AssetViewModel>();

            // Don't touch the action stack in this case.
            if (assets.Count == 0)
            {
                return(viewModels);
            }

            var fixedAssets = new List <AssetItem>();

            using (var transaction = UndoRedoService.CreateTransaction())
            {
                // Clean collision by renaming pasted asset if an asset with the same name already exists in that location.
                AssetCollision.Clean(null, assets, fixedAssets, AssetResolver.FromPackage(Package), false, false);

                // Temporarily add the new asset to the package
                fixedAssets.ForEach(x => Package.Assets.Add(x));

                // Find which assets are referencing the pasted assets in order to fix the reference link.
                var assetsToFix = GetReferencers(Session.DependencyManager, Session, fixedAssets);

                // Remove temporarily added assets - they will be properly re-added with the correct action stack entry when creating the view model
                fixedAssets.ForEach(x => Package.Assets.Remove(x));

                // Create directories and view models, actually add assets to package.
                foreach (var asset in fixedAssets)
                {
                    var location       = asset.Location.GetFullDirectory() ?? "";
                    var assetDirectory = project == null?
                                         GetOrCreateAssetDirectory(location, true) :
                                             project.GetOrCreateProjectDirectory(location, true);

                    var assetViewModel = CreateAsset(assetDirectory, asset, true, null);
                    viewModels.Add(assetViewModel);
                }

                // Fix references in the assets that references what we pasted.
                // We wrap this operation in an action item so the action stack can properly re-execute it.
                var fixReferencesAction = new FixAssetReferenceOperation(assetsToFix, false, true);
                fixReferencesAction.FixAssetReferences();
                UndoRedoService.PushOperation(fixReferencesAction);

                UndoRedoService.SetName(transaction, "Paste assets");
            }
            return(viewModels);
        }
Example #2
0
        /// <inheritdoc />
        protected override async Task Delete()
        {
            var elementsToDelete = GetCommonRoots(SelectedItems);
            var ask = UIEditorSettings.AskBeforeDeletingUIElements.GetValue();

            if (ask)
            {
                var confirmMessage = Tr._p("Message", "Are you sure you want to delete this UI element?");
                if (elementsToDelete.Count > 1)
                {
                    confirmMessage = string.Format(Tr._p("Message", "Are you sure you want to delete these {0} UI elements?"), elementsToDelete.Count);
                }
                var buttons = DialogHelper.CreateButtons(new[] { Tr._p("Button", "Delete"), Tr._p("Button", "Cancel") }, 1, 2);
                var result  = await ServiceProvider.Get <IDialogService>().CheckedMessageBox(confirmMessage, false, DialogHelper.DontAskAgain, buttons, MessageBoxImage.Question);

                if (result.Result != 1)
                {
                    return;
                }
                if (result.IsChecked == true)
                {
                    UIEditorSettings.AskBeforeDeletingUIElements.SetValue(false);
                    UIEditorSettings.Save();
                }
            }

            var hadActiveRoot = elementsToDelete.Any(x => ReferenceEquals(x, ActiveRoot));
            var asset         = elementsToDelete.First().Asset; // all selected items are from the same asset

            using (var transaction = UndoRedoService.CreateTransaction())
            {
                ClearSelection();
                HashSet <Tuple <Guid, Guid> > mapping;
                asset.AssetHierarchyPropertyGraph.DeleteParts(elementsToDelete.Select(x => x.UIElementDesign), out mapping);
                var operation = new DeletedPartsTrackingOperation <UIElementDesign, UIElement>(asset, mapping);
                UndoRedoService.PushOperation(operation);
                UndoRedoService.SetName(transaction, "Delete selected UI elements");
            }
            // Clear active root if it was deleted
            if (hadActiveRoot)
            {
                ActiveRoot = null;
            }
        }