Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selecting the closest layer when deleting #613

Closed
wants to merge 6 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion src/PixiEditor/ViewModels/SubViewModels/Main/LayersViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System.Collections.Immutable;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
Expand All @@ -15,6 +16,7 @@
using PixiEditor.Models.IO;
using PixiEditor.Models.Localization;
using PixiEditor.ViewModels.SubViewModels.Document;
using PixiEditor.Views.UserControls.Layers;

namespace PixiEditor.ViewModels.SubViewModels.Main;
#nullable enable
Expand Down Expand Up @@ -48,10 +50,44 @@ public bool CanDeleteSelected()
[Command.Basic("PixiEditor.Layer.DeleteSelected", "LAYER_DELETE_SELECTED", "LAYER_DELETE_SELECTED_DESCRIPTIVE", CanExecute = "PixiEditor.Layer.CanDeleteSelected", IconPath = "Trash.png")]
public void DeleteSelected()
{
var member = Owner.DocumentManagerSubViewModel.ActiveDocument?.SelectedStructureMember;
var doc = Owner.DocumentManagerSubViewModel?.ActiveDocument;

var member = doc.SelectedStructureMember;
if (member is null)
return;

member.Document.Operations.DeleteStructureMember(member.GuidValue);

if (doc.StructureHelper.GetAllLayers().Count > 1)
{
//Declare the layer that will be selected
var baseLayer = doc.StructureHelper.GetAllLayers()[doc.StructureHelper.GetAllLayers().ToArray().Length - 1];

for (int i = 0; i < doc.StructureHelper.GetAllLayers().Count; i++)
{
//Checking that the selected layer and the deleted layer are not the same
if (doc.StructureHelper.GetAllLayers()[i] == member)
{
//Selecting the new layer
if (i > 0)
{
baseLayer = doc.StructureHelper.GetAllLayers()[i-1];
}
else if (i == doc.StructureHelper.GetAllLayers().Count - 1)
{
baseLayer = doc.StructureHelper.GetAllLayers()[i];
}
else if (i == 0)
{
baseLayer = doc.StructureHelper.GetAllLayers().ToArray()[i+1];
}

break;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are calling GetAllLayers() 8 times, which is very inefficient, call it once and cast to array once.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like this?

doc.Operations.AddSoftSelectedMember(baseLayer.GuidValue);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Soft" selection is used in the case where you have multiple layers selected. To handle drawing in that case, we need to choose a single layer to draw on. Because of that there are two types of selection, "Hard" selection (only one layer can be hard-selected) and "Soft" selection (any selected layers that aren't the main one will be "soft" selected).

It looks like you are trying to hard-select baseLayer, and immediately afterwards also soft-select it, which doesn't make sense (and there is a check in DocumentUpdater.ProcessAddSoftSelectedMember which ensures that you can't soft-select a hard-selected layer)

doc.InternalSetSelectedMember(baseLayer);
}
}

[Evaluator.CanExecute("PixiEditor.Layer.HasSelectedMembers")]
Expand Down