JD2022-TU1/main/tools/modules/JD.AssetBrowser/ViewModel/DefaultContextMenuProvider.cs

108 lines
4.1 KiB
C#

using System.Collections.Generic;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using JD.AssetBrowser.Actions;
using JD.AssetBrowser.Actions.Asset;
using JD.AssetBrowser.Properties;
using JD.AssetBrowser.Utilities;
using JD.Core.Drawing;
using JD.Perforce;
using Plume.Interfaces.WPF;
using Plume.Interfaces.WPF.Actions;
using Plume.Interfaces.WPF.FileEditor;
using TeaBox.Actions;
using Ubisoft;
using Control = System.Windows.Controls.Control;
using MenuItem = System.Windows.Controls.MenuItem;
namespace JD.AssetBrowser.ViewModel
{
public class DefaultContextMenuProvider : IContextMenuProvider
{
public IEnumerable<Control> ContextMenuItems(AssetViewModel asset)
{
var openFileMenu = new MenuItem { Header = "Open", Command = new RelayCommand(() => OpenFile(asset)) };
asset.DefaultMenuAction = openFileMenu;
yield return openFileMenu;
var fileEditorManager = ((IWpfApplication) Application.Current).FileEditorManager;
var editors = fileEditorManager.EditorsFor(asset.AbsolutePath);
foreach (var editor in editors)
{
var menuItem = new MenuItem
{
Header = "Open with " + editor.Name,
Icon = editor.Icon.ToImage(20,20),
Tag = editor,
Command = new RelayCommand(() => OpenFileWith(asset, editor))
};
menuItem.CommandParameter = menuItem;
yield return menuItem;
}
yield return new Separator();
yield return GetMenuFromAction<ShowInExplorerAction>(asset);
yield return GetMenuFromAction<ShowInP4vAction>(asset);
//if (asset.Model.MetadataPath.Exists())
//{
// yield return new Separator();
// var regenerate = new MenuItem { Header = "Regenerate Asset", Command = new RelayCommand(() => RegenerateAsset(asset)) };
// yield return regenerate;
//}
yield return new Separator();
yield return GetMenuFromAction<CopyFullPathAction>(asset);
yield return GetMenuFromAction<CopyLocalPathAction>(asset);
yield return new Separator();
yield return GetMenuFromAction<ShowDependenciesAction>(asset);
yield return GetMenuFromAction<RenameOrMoveAssetAction>(asset);
yield return GetMenuFromAction<DeleteAssetAction>(asset);
}
MenuItem GetMenuFromAction<TAction>(AssetViewModel asset) where TAction: IAction
{
var actionManager = ((IWpfApplication)Application.Current).ActionManager;
var showDependenciesAction = actionManager.Get<TAction>().ToMenuItem();
showDependenciesAction.CommandParameter = asset;
return showDependenciesAction;
}
private void OpenFile(AssetViewModel asset)
{
var processStartInfo = new ProcessStartInfo(asset.AbsolutePath) { WindowStyle = ProcessWindowStyle.Hidden };
Process.Start(processStartInfo);
}
private void OpenFileWith(AssetViewModel asset, IFileEditor editor)
{
var processStartInfo = new ProcessStartInfo(editor.ApplicationPath, asset.AbsolutePath) { WindowStyle = ProcessWindowStyle.Hidden };
Process.Start(processStartInfo);
}
//private void RegenerateAsset(AssetViewModel asset)
//{
// var assetCreationDialog = new AssetCreationDialog();
// var creator = AssetCreationManager.Instance.GetCreatorForAsset(asset.Model, true);
// if (creator != null)
// {
// assetCreationDialog.ViewModel.AssetModel = creator;
// assetCreationDialog.ShowDialog();
// }
// else
// {
// var notificationManager = ((IWpfApplication)System.Windows.Application.Current).NotificationManager;
// notificationManager.ShowError("Unable to regenerate the asset");
// }
//}
}
}