This commit is contained in:
CortexCore
2024-11-13 17:47:45 +08:00
parent c4af12acd7
commit 416e3322db
208 changed files with 2591757 additions and 1497 deletions

View File

@@ -1,12 +1,12 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.Remoting.Contexts;
using System.ComponentModel.Design;
using UnityEngine;
using UnityEngine.UIElements;
using UnityEngine.InputSystem;
using BITKit;
using Cysharp.Threading.Tasks;
using BITKit.Mod;
using Object = UnityEngine.Object;
namespace BITKit.UX.Internal
{
public class ContextMenu : IContextMenu
@@ -20,16 +20,16 @@ namespace BITKit.UX.Internal
{
public Label(string text)
{
this.text = text;
_text = text;
}
string text;
private readonly string _text;
public override VisualElement GetVisualElement()
{
UnityEngine.UIElements.Label label = new(text);
UnityEngine.UIElements.Label label = new(_text);
return label;
}
}
[System.Serializable]
[Serializable]
public class TestContextMenu
{
public void Execute()
@@ -45,19 +45,20 @@ namespace BITKit.UX.Internal
{
public Button(string text, params Action[] actions)
{
this.text = text;
_text = text;
foreach (var x in actions)
{
action += x;
_action += x;
}
}
string text;
Action action;
private readonly string _text;
private readonly Action _action;
public override VisualElement GetVisualElement()
{
UnityEngine.UIElements.Button button = new();
button.clicked += action;
button.text = text;
button.clicked += _action;
button.text = _text;
return button;
}
}
@@ -77,14 +78,14 @@ namespace BITKit.UX
}
public static ContextMenuBuilder BuildAction(this ContextMenuBuilder self, string text, Action action)
{
self.Add(new Internal.Button(text, action, self.Excute));
self.Add(new Internal.Button(text, action, self.Execute));
return self;
}
}
public class ContextMenuBuilder
{
readonly List<IContextMenu> contexts = new();
public event Action OnExcuted;
private readonly List<IContextMenu> _contexts = new();
public event Action OnExecuted;
private ContextMenuBuilder()
{
@@ -97,53 +98,89 @@ namespace BITKit.UX
{
UXContextMenu.Singleton.Create(this);
}
internal void Excute()
internal void Execute()
{
OnExcuted?.Invoke();
OnExecuted?.Invoke();
}
public void Add(IContextMenu x) => contexts.Add(x);
public IEnumerable<IContextMenu> GetContextMenus() => contexts.ToArray();
public void Add(IContextMenu x) => _contexts.Add(x);
public IEnumerable<IContextMenu> GetContextMenus() => _contexts.ToArray();
}
public class UXContextMenu : MonoBehaviour
public class UXContextMenu:IDisposable
{
internal static UXContextMenu Singleton;
[SerializeField] private UIDocument document;
private VisualElement root;
private VisualElement container;
private void Awake()
private readonly IUXService _uxService;
private VisualElement _root;
private VisualElement _container;
private bool _isInitialized = false;
public UXContextMenu(IUXService uxService)
{
_uxService = uxService;
Singleton = this;
root = document.rootVisualElement;
container = document.rootVisualElement[1];
root.Q("background-image").RegisterCallback<MouseDownEvent>(x =>
uxService.OnPanelChanged += OnPanelChanged;
InitializeAsync();
}
private async void InitializeAsync()
{
var go = new GameObject(nameof(UXContextMenu));
Object.DontDestroyOnLoad(go);
var document = go.AddComponent<UIDocument>();
document.sortingOrder = 1;
try
{
var panelSettings =await ModService.LoadAsset<PanelSettings>("ux_panel_settings");
document.panelSettings = panelSettings;
}
catch (Exception)
{
BIT4Log.Warning<UXService>("未找到ux_panel_settings");
throw;
}
document.visualTreeAsset = await ModService.LoadAsset<VisualTreeAsset>("ux_context_menu");
_root = document.rootVisualElement;
_container = _root.Q<VisualElement>("menu-container");
_root.Q("background-image").RegisterCallback<MouseDownEvent>(_ =>
{
Close();
});
_isInitialized = true;
Close();
}
public void Create(ContextMenuBuilder builder)
{
var pos = Mouse.current.position.ReadValue();
pos.y = Screen.height - pos.y;
pos = RuntimePanelUtils.ScreenToPanel(root.panel, pos);
pos = RuntimePanelUtils.ScreenToPanel(_root.panel, pos);
container.style.position = Position.Absolute;
container.style.left = pos.x;
container.style.top = pos.y;
container.Clear();
_container.style.position = Position.Absolute;
_container.style.left = pos.x;
_container.style.top = pos.y;
_container.Clear();
root.SetActive(true);
_root.SetActive(true);
foreach (var context in builder.GetContextMenus())
{
container.Add(context.GetVisualElement());
_container.Add(context.GetVisualElement());
}
builder.OnExcuted += Close;
builder.OnExecuted += Close;
}
void Close()
private void Close()
{
container.Clear();
root.SetActive(false);
if(_isInitialized is false)return;
_container.Clear();
_root.SetActive(false);
}
public void Dispose()
{
_uxService.OnPanelChanged -= OnPanelChanged;
}
private void OnPanelChanged(IUXPanel arg1, IUXPanel arg2)
{
Close();
}
}
}