JD2022-TU1/main/tools/framework/JD.Application.TeaBox/View/Actions/QuickOpenHandler.cs

122 lines
4.5 KiB
C#

using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using JD.Asset.Types.Map;
using JD.Core.IO;
using JD.UserControls.QuickOpenDialogs;
using TeaBox;
using TeaBox.Gui;
using TeaBox.ObjectModel;
namespace JD.Application.View.Actions
{
public class QuickOpenHandler : ActionHandler
{
protected class MapEntry
{
public Map Map { get; set; }
public override string ToString()
{
return Map.Name;
}
}
private class FileEntry
{
public Map Map { get; set; }
public string FileName { get; set; }
public string Caption { get; set; }
public override string ToString()
{
if (!string.IsNullOrEmpty(Caption))
{
return Caption;
}
string relativePath = PathUtils.MakeRelative(FileName, Map.Folder);
return relativePath;
}
public Bitmap Icon
{
get; set;
}
public DocumentBinding DocumentBinding { get; set; }
}
public override bool Update(ActionContext context, ActionInfo info)
{
info.Visible = DocumentBindingService.GetAllFileFilters().Any();
return base.Update(context, info);
}
public override void Execute(ActionExecutionContext context)
{
var allMaps = Map.AllMaps.Select(map => new MapEntry{Map = map});
OpenDialog(allMaps);
}
protected void OpenDialog(IEnumerable<MapEntry> maps)
{
var dialog = new QuickOpenForm<MapEntry>(maps)
{
Image = Icon.FromHandle(Properties.ResourcesCache.Icon_16x16_Song.GetHicon()),
ShowAllOnEmptyText = true,
ShowFrame = true,
Caption = "Select a map to open ...",
HintProvider = x => x.Map.JdCatalogName,
NameProvider = map => map.Map.Name,
ImageProvider = map => Properties.ResourcesCache.Icon_16x16_Song
};
dialog.ItemAccepted += OnMapAccepted;
dialog.Show(true);
}
private void OnMapAccepted(MapEntry mapEntry)
{
var filters = DocumentBindingService.GetAllFileFilters().SelectMany(x => x.Filter.Split(';'));
var allFiles = filters.SelectMany(x => mapEntry.Map.AllFiles(x)).Select(x => new FileEntry
{
FileName = x,
Map = mapEntry.Map,
DocumentBinding = DocumentBindingService.GetBindingForFileName(x),
Icon = (Bitmap)DocumentBindingService.GetBindingForFileName(x).GetImage(string.Empty)
}).ToList();
if (File.Exists(mapEntry.Map.MainScenePath))
{
allFiles.Insert(0, new FileEntry
{
Caption = "Run in UAF",
FileName = mapEntry.Map.MainScenePath,
Icon = Properties.Resources.Icons_30x30_Play, Map = mapEntry.Map
});
}
var dialog = new QuickOpenForm<FileEntry>(allFiles)
{
Image = Icon.FromHandle(Properties.Resources.Icon_16x16_Song.GetHicon()),
ShowAllOnEmptyText = true,
ShowFrame = true,
Caption = mapEntry.ToString(),
HintProvider = x => x.DocumentBinding != null? x.DocumentBinding.DisplayName : "",
ImageProvider = x => x.Icon
};
dialog.ItemAccepted += OnFileSelected;
dialog.Show(true);
}
private void OnFileSelected(FileEntry fileEntry)
{
if (fileEntry.FileName == fileEntry.Map.MainScenePath)
{
System.Diagnostics.Process.Start(fileEntry.FileName);
}
else
{
global::TeaBox.ObjectModel.Application.Main.Open.FromFile(fileEntry.FileName, DocumentOpeningOptions.Default);
}
}
}
}