1
This commit is contained in:
@@ -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,25 +98,48 @@ 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;
|
||||
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("UXConsole");
|
||||
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();
|
||||
});
|
||||
@@ -125,25 +149,35 @@ namespace BITKit.UX
|
||||
{
|
||||
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);
|
||||
_container.Clear();
|
||||
_root.SetActive(false);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_uxService.OnPanelChanged -= OnPanelChanged;
|
||||
}
|
||||
|
||||
private void OnPanelChanged(IUXPanel arg1, IUXPanel arg2)
|
||||
{
|
||||
Close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -103,7 +103,9 @@ namespace BITKit.UX
|
||||
|
||||
public void Entry(IUXPanel panel) => _entryQueue.Push(panel);
|
||||
public void Entry(string panelName) => _entryQueueByName.TryAdd(panelName);
|
||||
|
||||
public IUXPanel CurrentPanel => _currentPanel;
|
||||
public event Action<IUXPanel, IUXPanel> OnPanelChanged;
|
||||
|
||||
public void Return()
|
||||
{
|
||||
if(_windowEntryGroup.TryGetEntried(out _))
|
||||
@@ -124,6 +126,7 @@ namespace BITKit.UX
|
||||
|
||||
private void OnEntry(IUXPanel obj)
|
||||
{
|
||||
OnPanelChanged?.Invoke(_currentPanel,obj);
|
||||
_currentPanel = obj;
|
||||
}
|
||||
private void OnTick(float delta)
|
||||
|
Reference in New Issue
Block a user