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

96 lines
3.0 KiB
C#

using System.Collections;
using System.Collections.Generic;
using System.IO;
using BITKit;
using BITKit.UX;
using Cysharp.Threading.Tasks;
using Net.BITKit.Localization;
using Net.Project.B.World;
using UnityEngine;
using UnityEngine.InputSystem;
using UnityEngine.Networking;
using UnityEngine.UIElements;
namespace Net.Project.B.UX
{
public class UXSnapshotWindow:UIToolKitPanel, IUXSnapshotWindow
{
private readonly ILocalizationService _localizationService;
private readonly ISnapshotService _snapshotService;
protected override string DocumentPath => "ui_snapshot_window";
public override bool IsWindow => true;
public override bool CloseWhenClickOutside => true;
public override bool AllowCursor => true;
private float _currentOpacity = 1;
[UXBindPath("open-button",true)]
private Button _openButton;
[UXBindPath("delete-button",true)]
private Button _deleteButton;
public UXSnapshotWindow(IUXService uxService, ISnapshotService snapshotService, ILocalizationService localizationService) : base(uxService)
{
_snapshotService = snapshotService;
_localizationService = localizationService;
_snapshotService.OnSnapshot += Open;
_snapshotService.OnSnapshot += x =>
{
_currentOpacity = 1;
};
}
public async void Open(SnapshotData scopeData)
{
UXService.Entry(this);
await WaitUtilInitialized.Task;
await UniTask.SwitchToMainThread();
if (_openButton is not null)
{
_openButton.clickable = null;
_openButton.clicked += new BITApp.OpenPath()
{
path = scopeData.FileName
}.Execute;
}
if (_deleteButton is not null)
{
_deleteButton.clickable = null;
_deleteButton.clicked += new FileInfo(scopeData.FileName).Delete;
_deleteButton.clicked += UXService.Return;
}
RootVisualElement.Get<Label>().text =_localizationService.GetLocalizedString(scopeData.Name);
RootVisualElement.Get<Label>(1).text =_localizationService.GetLocalizedString(scopeData.Description);
using var handle = UnityWebRequestTexture.GetTexture($"file://" + scopeData.FileName);
await handle.SendWebRequest();
var texture = DownloadHandlerTexture.GetContent(handle);
RootVisualElement.Get<VisualElement>().style.backgroundImage = new(texture);
}
public override void OnTick(float deltaTime)
{
base.OnTick(deltaTime);
if (_currentOpacity != 0)
{
RootVisualElement.Get<VisualElement>(1).style.opacity =
_currentOpacity = Mathf.MoveTowards(_currentOpacity, 0, deltaTime);
}
}
}
}