Skip to content

Commit

Permalink
New feature: export kit to file
Browse files Browse the repository at this point in the history
  • Loading branch information
jskeet committed Oct 12, 2019
1 parent ac08b7a commit 02e1008
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions Drums/VDrumExplorer.Wpf/DataExplorerCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ public static class DataExplorerCommands
public static RoutedCommand OpenCopyInKitExplorer { get; } = new RoutedCommand(nameof(OpenCopyInKitExplorer), typeof(DataExplorerCommands));
public static RoutedCommand CopyKit { get; } = new RoutedCommand(nameof(CopyKit), typeof(DataExplorerCommands));
public static RoutedCommand ImportKitFromFile { get; } = new RoutedCommand(nameof(ImportKitFromFile), typeof(DataExplorerCommands));
public static RoutedCommand ExportKit { get; } = new RoutedCommand(nameof(ExportKit), typeof(DataExplorerCommands));
}
}
2 changes: 1 addition & 1 deletion Drums/VDrumExplorer.Wpf/KitExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace VDrumExplorer.Wpf
public class KitExplorer : DataExplorer
{
private readonly Kit kit;
private const string SaveFileFilter = "V-Drum Explorer kit files|*.vkit";
internal const string SaveFileFilter = "V-Drum Explorer kit files|*.vkit";

internal KitExplorer(ILogger logger, Kit kit, SysExClient midiClient, string fileName)
: base(logger, kit.Schema, kit.Data, kit.KitRoot, midiClient, fileName, SaveFileFilter, "Kit explorer")
Expand Down
30 changes: 25 additions & 5 deletions Drums/VDrumExplorer.Wpf/ModuleExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ internal ModuleExplorer(ILogger logger, Module module, SysExClient midiClient, s
CommandBindings.Add(new CommandBinding(DataExplorerCommands.OpenCopyInKitExplorer, OpenCopyInKitExplorer));
CommandBindings.Add(new CommandBinding(DataExplorerCommands.ImportKitFromFile, ImportKitFromFile));
CommandBindings.Add(new CommandBinding(DataExplorerCommands.CopyKit, CopyKit));
CommandBindings.Add(new CommandBinding(DataExplorerCommands.ExportKit, ExportKit));
}

private void AddKitContextMenus()
Expand Down Expand Up @@ -68,8 +69,9 @@ void AddKitMenu(TreeViewItem node, VisualTreeNode vtn)
Items =
{
new MenuItem { Header = "Open copy in Kit Explorer", Command = DataExplorerCommands.OpenCopyInKitExplorer, CommandParameter = vtn },
new MenuItem { Header = "Import from file", Command = DataExplorerCommands.ImportKitFromFile, CommandParameter = node },
new MenuItem { Header = "Copy to another kit", Command = DataExplorerCommands.CopyKit, CommandParameter = vtn },
new MenuItem { Header = "Import from file", Command = DataExplorerCommands.ImportKitFromFile, CommandParameter = node },
new MenuItem { Header = "Export to file", Command = DataExplorerCommands.ExportKit, CommandParameter = vtn },
}
};
}
Expand All @@ -79,13 +81,31 @@ void AddKitMenu(TreeViewItem node, VisualTreeNode vtn)

private void OpenCopyInKitExplorer(object sender, ExecutedRoutedEventArgs e)
{
var kitNode = (VisualTreeNode) e.Parameter;
var kit = CreateKit((VisualTreeNode) e.Parameter);
new KitExplorer(Logger, kit, MidiClient, fileName: null).Show();
}

private void ExportKit(object sender, ExecutedRoutedEventArgs e)
{
var kit = CreateKit((VisualTreeNode) e.Parameter);
var dialog = new SaveFileDialog { Filter = KitExplorer.SaveFileFilter };
var result = dialog.ShowDialog();
if (result != true)
{
return;
}
using (var stream = File.Create(dialog.FileName))
{
kit.Save(stream);
}
}

private Kit CreateKit(VisualTreeNode kitRoot)
{
// We clone the data from kitNode downwards, but relocating it as if it were the first kit.
var firstKitNode = Schema.KitRoots[1];
var clonedData = kitNode.Context.CloneData(Data, firstKitNode.Context.Address);
var kit = new Kit(Schema, clonedData, kitNode.KitNumber.Value);
new KitExplorer(Logger, kit, MidiClient, fileName: null).Show();
var clonedData = kitRoot.Context.CloneData(Data, firstKitNode.Context.Address);
return new Kit(Schema, clonedData, kitRoot.KitNumber.Value);
}

private void ImportKitFromFile(object sender, ExecutedRoutedEventArgs e)
Expand Down

0 comments on commit 02e1008

Please sign in to comment.