From 8d093c55382ee3f25c5cf10ea611560772ea39c8 Mon Sep 17 00:00:00 2001 From: rowan Date: Wed, 6 Aug 2025 14:06:22 -0400 Subject: [PATCH] fix vertical scrolling --- .../UI/Elements/Layout/DynamicLayout.cs | 13 +++++--- .../Scripts/UI/Elements/Layout/FixedLayout.cs | 18 ++++++----- Assets/Scripts/UI/Elements/Layout/ILayout.cs | 1 - .../UI/Elements/RecycleView/RecycleView.cs | 31 ++++++++++++++++--- .../RecycleVirtualizationController.cs | 1 + Assets/UI/Test/Resources/UserProfileList.uxml | 2 +- 6 files changed, 47 insertions(+), 19 deletions(-) diff --git a/Assets/Scripts/UI/Elements/Layout/DynamicLayout.cs b/Assets/Scripts/UI/Elements/Layout/DynamicLayout.cs index c2ae2fc..b88c518 100644 --- a/Assets/Scripts/UI/Elements/Layout/DynamicLayout.cs +++ b/Assets/Scripts/UI/Elements/Layout/DynamicLayout.cs @@ -10,11 +10,12 @@ namespace KitsuneCafe.UI public class DynamicLayout : ILayout { public float DefaultItemSize { get; set; } - public float GutterSize { get; set; } - public FlowDirection Direction { get; set; } - public float ContentSize { get; private set; } + public float GutterSize { get; set; } = 0; + public FlowDirection Direction { get; set; } = FlowDirection.Vertical; + public int Buffer { get; set; } = 0; + public bool Wrap { get; set; } = false; public int Count { get; set; } - public int Buffer { get; set; } + public float ContentSize { get; private set; } private readonly List positions = new(); private readonly List sizes = new(); @@ -132,8 +133,10 @@ namespace KitsuneCafe.UI } count += halfBuffer; + var last = Math.Min(Count, first + count); - return new Range(first, Math.Min(Count, first + count)); + Debug.Log($"{first}:{last}"); + return new Range(first, last); } } } diff --git a/Assets/Scripts/UI/Elements/Layout/FixedLayout.cs b/Assets/Scripts/UI/Elements/Layout/FixedLayout.cs index 523f8ee..e79341b 100644 --- a/Assets/Scripts/UI/Elements/Layout/FixedLayout.cs +++ b/Assets/Scripts/UI/Elements/Layout/FixedLayout.cs @@ -6,14 +6,14 @@ namespace KitsuneCafe.UI { public class FixedLayout : ILayout { - public FlowDirection Direction { get; set; } - public float ContentSize { get; private set; } - - + public FlowDirection Direction { get; set; } = FlowDirection.Vertical; public float ItemSize { get; set; } - public float GutterSize { get; set; } + public float GutterSize { get; set; } = 0; + public int Buffer { get; set; } = 0; + public bool Wrap { get; set; } = false; + public int Count { get; set; } - public int Buffer { get; set; } + public float ContentSize { get; private set; } public FixedLayout(FlowDirection direction, float itemSize, float gutterSize) { @@ -43,10 +43,12 @@ namespace KitsuneCafe.UI public Range GetVisibleRange(float offset, float containerSize) { var size = ItemSize + GutterSize; - var first = Mathf.FloorToInt(offset / (ItemSize + GutterSize)); - var count = Mathf.CeilToInt(containerSize / size) + Buffer; + var halfBuffer = Mathf.CeilToInt(Buffer / 2); + var first = Mathf.FloorToInt(offset / (ItemSize + GutterSize)) - halfBuffer; + var count = Mathf.CeilToInt(containerSize / size) + halfBuffer; var last = Math.Min(Count, first + count); + Debug.Log($"{first}:{last}"); return new Range(first, last); } } diff --git a/Assets/Scripts/UI/Elements/Layout/ILayout.cs b/Assets/Scripts/UI/Elements/Layout/ILayout.cs index 3023f60..125f3e5 100644 --- a/Assets/Scripts/UI/Elements/Layout/ILayout.cs +++ b/Assets/Scripts/UI/Elements/Layout/ILayout.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using UnityEngine.UIElements; namespace KitsuneCafe.UI diff --git a/Assets/Scripts/UI/Elements/RecycleView/RecycleView.cs b/Assets/Scripts/UI/Elements/RecycleView/RecycleView.cs index 801e6a0..70b63ca 100644 --- a/Assets/Scripts/UI/Elements/RecycleView/RecycleView.cs +++ b/Assets/Scripts/UI/Elements/RecycleView/RecycleView.cs @@ -158,6 +158,29 @@ namespace KitsuneCafe.UI } } + private bool wrap = false; + + [UxmlAttribute, CreateProperty] + public bool Wrap + { + get => wrap; + set + { + if (wrap != value) + { + wrap = value; + if (virtualizationController.Layout is FixedLayout fixedLayout) + { + fixedLayout.Wrap = wrap; + } + else if (virtualizationController.Layout is DynamicLayout dynamicLayout) + { + dynamicLayout.Wrap = wrap; + } + } + } + } + [CreateProperty] public ICollectionDataSource DataSource @@ -197,6 +220,8 @@ namespace KitsuneCafe.UI private ILayout CreateLayout() { + UpdateDirection(); + return isDynamicSize switch { true => new DynamicLayout(Direction, itemSize) { Buffer = bufferCount, GutterSize = gutter }, @@ -211,17 +236,15 @@ namespace KitsuneCafe.UI { case FlowDirection.Vertical: scrollView.mode = ScrollViewMode.Vertical; - scrollView.verticalScrollerVisibility = ScrollerVisibility.Auto; - scrollView.horizontalScrollerVisibility = ScrollerVisibility.Hidden; scrollView.horizontalScroller.valueChanged -= virtualizationController.OnScrolled; scrollView.verticalScroller.valueChanged += virtualizationController.OnScrolled; + Debug.Log("vertical"); break; case FlowDirection.Horizontal: scrollView.mode = ScrollViewMode.Horizontal; - scrollView.verticalScrollerVisibility = ScrollerVisibility.Hidden; - scrollView.horizontalScrollerVisibility = ScrollerVisibility.Auto; scrollView.verticalScroller.valueChanged -= virtualizationController.OnScrolled; scrollView.horizontalScroller.valueChanged += virtualizationController.OnScrolled; + Debug.Log("horizontal"); break; } } diff --git a/Assets/Scripts/UI/Elements/RecycleView/RecycleVirtualizationController.cs b/Assets/Scripts/UI/Elements/RecycleView/RecycleVirtualizationController.cs index ef5a5dc..4f75519 100644 --- a/Assets/Scripts/UI/Elements/RecycleView/RecycleVirtualizationController.cs +++ b/Assets/Scripts/UI/Elements/RecycleView/RecycleVirtualizationController.cs @@ -106,6 +106,7 @@ namespace KitsuneCafe.UI public void OnScrolled(float scrollOffset) { + Debug.Log("scrolling"); lastKnownScrollOffset = scrollOffset; ScheduleUpdate(); } diff --git a/Assets/UI/Test/Resources/UserProfileList.uxml b/Assets/UI/Test/Resources/UserProfileList.uxml index 690114e..b859b6a 100644 --- a/Assets/UI/Test/Resources/UserProfileList.uxml +++ b/Assets/UI/Test/Resources/UserProfileList.uxml @@ -1,3 +1,3 @@ - +