-
-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #159 from PixiEditor/undo-selection
Undo selection
- Loading branch information
Showing
11 changed files
with
229 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using PixiEditor.Models.Tools.ToolSettings.Settings; | ||
using PixiEditor.Models.Tools.ToolSettings.Toolbars; | ||
|
||
namespace PixiEditor.Helpers.Extensions | ||
{ | ||
public static class ToolbarHelpers | ||
{ | ||
public static EnumSetting<TEnum> GetEnumSetting<TEnum>(this Toolbar toolbar, string name) | ||
where TEnum : struct, Enum | ||
{ | ||
return toolbar.GetSetting<EnumSetting<TEnum>>(name); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
using System.Collections.Generic; | ||
using PixiEditor.Models.DataHolders; | ||
using PixiEditor.Models.Enums; | ||
using PixiEditor.Models.Position; | ||
using PixiEditor.Models.Undo; | ||
|
||
namespace PixiEditor.Helpers | ||
{ | ||
public static class SelectionHelpers | ||
{ | ||
public static void AddSelectionUndoStep(Document document, IEnumerable<Coordinates> oldPoints, SelectionType mode) | ||
{ | ||
#pragma warning disable SA1117 // Parameters should be on same line or separate lines. Justification: Making it readable | ||
if (mode == SelectionType.New && document.ActiveSelection.SelectedPoints.Count != 0) | ||
{ | ||
// Add empty selection as the old one get's fully deleted first | ||
document.UndoManager.AddUndoChange( | ||
new Change( | ||
SetSelectionProcess, new object[] { document, new List<Coordinates>(oldPoints) }, | ||
SetSelectionProcess, new object[] { document, new List<Coordinates>() })); | ||
document.UndoManager.AddUndoChange( | ||
new Change( | ||
SetSelectionProcess, new object[] { document, new List<Coordinates>() }, | ||
SetSelectionProcess, new object[] { document, new List<Coordinates>(document.ActiveSelection.SelectedPoints) })); | ||
} | ||
else | ||
{ | ||
document.UndoManager.AddUndoChange( | ||
new Change( | ||
SetSelectionProcess, new object[] { document, new List<Coordinates>(oldPoints) }, | ||
SetSelectionProcess, new object[] { document, new List<Coordinates>(document.ActiveSelection.SelectedPoints) })); | ||
#pragma warning restore SA1117 // Parameters should be on same line or separate lines | ||
} | ||
} | ||
|
||
private static void SetSelectionProcess(object[] arguments) | ||
{ | ||
Document document = (Document)arguments[0]; | ||
|
||
document.ActiveSelection.SetSelection((IEnumerable<Coordinates>)arguments[1], SelectionType.New); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
PixiEditor/Models/Tools/ToolSettings/Settings/EnumSetting.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
using System.Windows.Controls; | ||
using System.Windows.Controls.Primitives; | ||
using System.Windows.Data; | ||
|
||
namespace PixiEditor.Models.Tools.ToolSettings.Settings | ||
{ | ||
public class EnumSetting<TEnum> : Setting<TEnum, ComboBox> | ||
where TEnum : struct, Enum | ||
{ | ||
private int selectedIndex = 0; | ||
|
||
/// <summary> | ||
/// Gets or sets the selected Index of the <see cref="ComboBox"/>. | ||
/// </summary> | ||
public int SelectedIndex | ||
{ | ||
get => selectedIndex; | ||
set | ||
{ | ||
if (SetProperty(ref selectedIndex, value)) | ||
{ | ||
RaisePropertyChanged(nameof(Value)); | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the selected value of the <see cref="ComboBox"/>. | ||
/// </summary> | ||
public new TEnum Value | ||
{ | ||
get => (TEnum)(SettingControl.SelectedItem as ComboBoxItem).Tag; | ||
set | ||
{ | ||
SettingControl.SelectedItem = SettingControl.Items.Cast<ComboBoxItem>().First(x => x.Tag == (object)value); | ||
RaisePropertyChanged(nameof(Value)); | ||
} | ||
} | ||
|
||
public EnumSetting(string name, string label) | ||
: base(name) | ||
{ | ||
SettingControl = GenerateDropdown(); | ||
|
||
Label = label; | ||
} | ||
|
||
public EnumSetting(string name, string label, TEnum defaultValue) | ||
: this(name, label) | ||
{ | ||
Value = defaultValue; | ||
} | ||
|
||
private static ComboBox GenerateDropdown() | ||
{ | ||
ComboBox combobox = new ComboBox | ||
{ | ||
VerticalAlignment = VerticalAlignment.Center | ||
}; | ||
|
||
GenerateItems(combobox); | ||
|
||
Binding binding = new Binding(nameof(SelectedIndex)) | ||
{ | ||
Mode = BindingMode.TwoWay | ||
}; | ||
|
||
combobox.SetBinding(Selector.SelectedIndexProperty, binding); | ||
|
||
return combobox; | ||
} | ||
|
||
private static void GenerateItems(ComboBox comboBox) | ||
{ | ||
string[] names = Enum.GetNames<TEnum>(); | ||
TEnum[] values = Enum.GetValues<TEnum>(); | ||
|
||
for (int i = 0; i < names.Length; i++) | ||
{ | ||
ComboBoxItem item = new ComboBoxItem | ||
{ | ||
Content = names[i], | ||
Tag = values[i] | ||
}; | ||
|
||
comboBox.Items.Add(item); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 3 additions & 2 deletions
5
PixiEditor/Models/Tools/ToolSettings/Toolbars/SelectToolToolbar.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,13 @@ | ||
using PixiEditor.Models.Tools.ToolSettings.Settings; | ||
using PixiEditor.Models.Enums; | ||
using PixiEditor.Models.Tools.ToolSettings.Settings; | ||
|
||
namespace PixiEditor.Models.Tools.ToolSettings.Toolbars | ||
{ | ||
public class SelectToolToolbar : Toolbar | ||
{ | ||
public SelectToolToolbar() | ||
{ | ||
Settings.Add(new DropdownSetting("SelectMode", new[] { "New", "Add", "Subtract" }, "Selection type")); | ||
Settings.Add(new EnumSetting<SelectionType>("SelectMode", "Selection type")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters