Files
Net.Like.Xue.Tokyo/Packages-Local/Com.Project.B.Unity/UX/UXSnapshots.cs
2025-06-24 23:49:13 +08:00

83 lines
2.9 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.IO;
using BITKit;
using BITKit.Mod;
using BITKit.UX;
using Cysharp.Threading.Tasks;
using Net.Project.B.World;
using UnityEngine.Networking;
using UnityEngine.UIElements;
namespace Net.Project.B.UX
{
public class UXSnapshots : UIToolKitPanel, IUXSnapshot
{
private readonly IUXSnapshotWindow _snapshotWindow;
private readonly ISnapshotService _snapshotService;
protected override string DocumentPath => "ui_snapshots";
public override bool AllowCursor => true;
public override bool CloseWhenClickOutside => true;
[UXBindPath("snapshots-container")]
private VisualElement _snapshotsContainer;
private VisualTreeAsset _snapshotTemplate;
public UXSnapshots(IUXService uxService, ISnapshotService snapshotService, IUXSnapshotWindow snapshotWindow) : base(uxService)
{
_snapshotService = snapshotService;
_snapshotWindow = snapshotWindow;
OnInitiatedAsync += InitiatedAsync;
}
private async UniTask InitiatedAsync()
{
_snapshotTemplate =await ModService.LoadAsset<VisualTreeAsset>("ui_snapshots-template");
}
public override async UniTask EntryAsync()
{
await base.EntryAsync();
_snapshotsContainer.Clear();
foreach (var scopeData in await _snapshotService.GetSnapshots())
{
if(File.Exists(scopeData.FileName) is false)continue;
var visualElement = _snapshotsContainer.Create(_snapshotTemplate);
using var handle = UnityWebRequestTexture.GetTexture($"file://" + scopeData.FileName);
await handle.SendWebRequest();
var texture = DownloadHandlerTexture.GetContent(handle);
await UniTask.SwitchToMainThread();
var button = visualElement.Get<Button>();
button.style.backgroundImage = new(texture);
visualElement.Get<Label>().text = scopeData.Name;
visualElement.Get<Label>(1).text = scopeData.Description;
button.clicked += () =>
{
_snapshotWindow.Open(scopeData);
};
button.RegisterCallback<MouseDownEvent>(evt =>
{
if(evt.button is not 1)return;
ContextMenuBuilder.Create()
.BuildText("操作")
.BuildAction("打开所在文件夹",()=>{BITApp.Open(Path.GetDirectoryName(scopeData.FileName));})
.BuildAction("删除",()=>{File.Delete(scopeData.FileName);visualElement.RemoveFromHierarchy();})
.Build();
});
}
}
}
}