1
This commit is contained in:
69
Src/Unity/Scripts/UX/Library/AdaptiveGrid.cs
Normal file
69
Src/Unity/Scripts/UX/Library/AdaptiveGrid.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
11
Src/Unity/Scripts/UX/Library/AdaptiveGrid.cs.meta
Normal file
11
Src/Unity/Scripts/UX/Library/AdaptiveGrid.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: bc041fa6dfb99954099521c02c33ea1c
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
@@ -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();
|
||||
|
@@ -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 });
|
||||
|
Reference in New Issue
Block a user