Add undo/redo support for the DisableSound commands.

This commit is contained in:
GlenCFL 2018-12-05 06:51:10 -05:00
parent 19957659d1
commit 8acc1333a3
4 changed files with 107 additions and 13 deletions

View File

@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
namespace Filtration.ObjectModel.Commands.ItemFilterScript
{
public class AddBlockItemToBlocksCommand : IUndoableCommand
{
private readonly List<Tuple<ObservableCollection<IItemFilterBlockItem>, IItemFilterBlockItem>> _input;
public AddBlockItemToBlocksCommand(List<Tuple<ObservableCollection<IItemFilterBlockItem>, IItemFilterBlockItem>> input)
{
_input = input;
}
public void Execute()
{
foreach (var v in _input)
{
var blockItems = v.Item1;
var item = v.Item2;
blockItems.Add(item);
}
}
public void Undo()
{
foreach (var v in _input)
{
var blockItems = v.Item1;
var item = v.Item2;
blockItems.Remove(item);
}
}
public void Redo() => Execute();
}
}

View File

@ -0,0 +1,48 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections.ObjectModel;
namespace Filtration.ObjectModel.Commands.ItemFilterScript
{
public class RemoveBlockItemFromBlocksCommand : IUndoableCommand
{
private readonly List<Tuple<ObservableCollection<IItemFilterBlockItem>, IItemFilterBlockItem>> _input;
public RemoveBlockItemFromBlocksCommand(List<Tuple<ObservableCollection<IItemFilterBlockItem>, IItemFilterBlockItem>> input)
{
_input = input;
}
public void Execute()
{
foreach (var pair in _input)
{
var blockItems = pair.Item1;
var blockItem = pair.Item2;
for (var i = 0; i < blockItems.Count; i++)
{
if (blockItems[i] == blockItem)
{
blockItems.RemoveAt(i--);
}
}
}
}
public void Undo()
{
foreach (var pair in _input)
{
var blockItems = pair.Item1;
var blockItem = pair.Item2;
blockItems.Add(blockItem);
}
}
public void Redo() => Execute();
}
}

View File

@ -114,11 +114,13 @@
<Compile Include="BlockItemTypes\WidthBlockItem.cs" /> <Compile Include="BlockItemTypes\WidthBlockItem.cs" />
<Compile Include="Commands\CommandManager.cs" /> <Compile Include="Commands\CommandManager.cs" />
<Compile Include="Commands\ICommand.cs" /> <Compile Include="Commands\ICommand.cs" />
<Compile Include="Commands\ItemFilterScript\AddBlockItemToBlocksCommand.cs" />
<Compile Include="Commands\ItemFilterScript\MoveBlocksToIndexCommand.cs" /> <Compile Include="Commands\ItemFilterScript\MoveBlocksToIndexCommand.cs" />
<Compile Include="Commands\ItemFilterScript\MoveBlocksToBottomCommand.cs" /> <Compile Include="Commands\ItemFilterScript\MoveBlocksToBottomCommand.cs" />
<Compile Include="Commands\ItemFilterScript\AddCommentBlockCommand.cs" /> <Compile Include="Commands\ItemFilterScript\AddCommentBlockCommand.cs" />
<Compile Include="Commands\ItemFilterScript\MoveBlocksToTopCommand.cs" /> <Compile Include="Commands\ItemFilterScript\MoveBlocksToTopCommand.cs" />
<Compile Include="Commands\ItemFilterScript\PasteBlocksCommand.cs" /> <Compile Include="Commands\ItemFilterScript\PasteBlocksCommand.cs" />
<Compile Include="Commands\ItemFilterScript\RemoveBlockItemFromBlocksCommand.cs" />
<Compile Include="Commands\ItemFilterScript\RemoveBlocksCommand.cs" /> <Compile Include="Commands\ItemFilterScript\RemoveBlocksCommand.cs" />
<Compile Include="Commands\ItemFilterScript\SetScriptDescriptionCommand.cs" /> <Compile Include="Commands\ItemFilterScript\SetScriptDescriptionCommand.cs" />
<Compile Include="Commands\ItemFilterScript\AddBlockCommand.cs" /> <Compile Include="Commands\ItemFilterScript\AddBlockCommand.cs" />

View File

@ -1294,23 +1294,24 @@ namespace Filtration.ViewModels
{ {
ValidateSelectedBlocks(); ValidateSelectedBlocks();
var modified = false; var input = new List<Tuple<ObservableCollection<IItemFilterBlockItem>, IItemFilterBlockItem>>();
foreach (var block in SelectedBlockViewModels.OfType<IItemFilterBlockViewModel>()) foreach (var block in SelectedBlockViewModels.OfType<IItemFilterBlockViewModel>())
{ {
var blockItems= block.Block.BlockItems; var blockItems = block.Block.BlockItems;
for (var i = 0; i < blockItems.Count; i++) for (var i = 0; i < blockItems.Count; i++)
{ {
if (blockItems[i] is DisableDropSoundBlockItem) var blockItem = blockItems[i];
if (blockItem is DisableDropSoundBlockItem)
{ {
blockItems.RemoveAt(i--); input.Add(new Tuple<ObservableCollection<IItemFilterBlockItem>, IItemFilterBlockItem>(blockItems, blockItem));
modified = true;
} }
} }
} }
if (modified) if (input.Count > 0)
{ {
_scriptCommandManager.ExecuteCommand(new RemoveBlockItemFromBlocksCommand(input));
SetDirtyFlag(); SetDirtyFlag();
} }
} }
@ -1319,7 +1320,7 @@ namespace Filtration.ViewModels
{ {
ValidateSelectedBlocks(); ValidateSelectedBlocks();
var modified = false; var input = new List<Tuple<ObservableCollection<IItemFilterBlockItem>, IItemFilterBlockItem>>();
foreach (var block in SelectedBlockViewModels.OfType<IItemFilterBlockViewModel>()) foreach (var block in SelectedBlockViewModels.OfType<IItemFilterBlockViewModel>())
{ {
@ -1335,12 +1336,13 @@ namespace Filtration.ViewModels
if (!found) { if (!found) {
var item = new DisableDropSoundBlockItem(true); var item = new DisableDropSoundBlockItem(true);
blockItems.Add(item); input.Add(new Tuple<ObservableCollection<IItemFilterBlockItem>, IItemFilterBlockItem>(blockItems, item));
modified = true;
} }
} }
if (modified) { if (input.Count > 0)
{
_scriptCommandManager.ExecuteCommand(new AddBlockItemToBlocksCommand(input));
SetDirtyFlag(); SetDirtyFlag();
} }
} }