This commit is contained in:
CortexCore
2025-04-14 15:39:28 +08:00
parent c1273357de
commit d8b8ddb8b6
23 changed files with 447 additions and 116 deletions

View File

@@ -0,0 +1,69 @@
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;
namespace Net.BITKit.UX
{
public class AdaptiveGrid : VisualElement
{
public new class UxmlFactory : UxmlFactory<AdaptiveGrid, UxmlTraits>
{
}
public AdaptiveGrid()
{
style.flexDirection = FlexDirection.Row;
style.flexWrap = Wrap.Wrap;
RegisterCallback<GeometryChangedEvent>(_ => UpdateLayout());
RegisterCallback<AttachToPanelEvent>(_ => UpdateLayout());
RegisterCallback<DetachFromPanelEvent>(_ => UpdateLayout());
}
private void UpdateLayout()
{
var width = resolvedStyle.width;
if (childCount == 0 || width <= 0) return;
var sampleVisualElement = contentContainer[0];
var max = sampleVisualElement.resolvedStyle.maxWidth.value;
var min = sampleVisualElement.resolvedStyle.minHeight.value;
if (max == 0)
{
max = sampleVisualElement.resolvedStyle.width;
}
if(min == 0)
{
min = max * 0.8f;
}
var count= Mathf.FloorToInt(width / min);
var remainder = width % count;
var itemWidth = (width - remainder) / count;
var itemHeight = itemWidth * 0.8f;
var spacing = (width - itemWidth * count) / (count - 1);
spacing = spacing < 0 ? 0 : spacing;
for (var i = 0; i < childCount; i++)
{
var child =contentContainer[i];
child.style.width = itemWidth-spacing;
child.style.height = itemHeight-spacing;
if (i % count != 0)
{
child.style.marginRight = spacing;
child.style.marginBottom = spacing;
}
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: bc041fa6dfb99954099521c02c33ea1c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -39,10 +39,9 @@ namespace BITKit.UX
{
UXService = uxService;
uxService.Register(this);
InitializeAsync().Forget();
}
private async UniTask InitializeAsync()
public override async UniTask InitializeAsync()
{
await _isBusy;
using var b = _isBusy.GetHandle();

View File

@@ -33,14 +33,6 @@ namespace BITKit.UX
_windowEntryGroup.OnStateChanged += OnWindowEntry;
_ticker.Add(OnTick);
}
private void OnWindowExit(IUXPanel obj)
{
BITAppForUnity.AllowCursor.RemoveElement(_windowEntryGroup);
if (obj.AllowInput is false)
BITInputSystem.AllowInput.RemoveDisableElements(_windowEntryGroup);
}
private void OnWindowEntry(IUXPanel prev, IUXPanel next)
{
BITAppForUnity.AllowCursor.SetElements(_windowEntryGroup, next is { AllowCursor: true });