fix vertical scrolling

This commit is contained in:
Rowan 2025-08-06 14:06:22 -04:00
parent 843e0220c2
commit 8d093c5538
6 changed files with 47 additions and 19 deletions

View file

@ -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<float> positions = new();
private readonly List<float> 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);
}
}
}

View file

@ -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);
}
}

View file

@ -1,5 +1,4 @@
using System;
using System.Collections.Generic;
using UnityEngine.UIElements;
namespace KitsuneCafe.UI

View file

@ -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;
}
}

View file

@ -106,6 +106,7 @@ namespace KitsuneCafe.UI
public void OnScrolled(float scrollOffset)
{
Debug.Log("scrolling");
lastKnownScrollOffset = scrollOffset;
ScheduleUpdate();
}

View file

@ -1,3 +1,3 @@
<ui:UXML xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ui="UnityEngine.UIElements" xmlns:uie="UnityEditor.UIElements" noNamespaceSchemaLocation="../../../../UIElementsSchema/UIElements.xsd" editor-extension-mode="False">
<KitsuneCafe.UI.RecycleView name="recycle-view" direction="Horizontal" a="Stupid" is-dynamic-size="true" default-item-size="50" fixed-item-size="22" gutter="0" style="flex-grow: 1; flex-direction: row;" />
<KitsuneCafe.UI.RecycleView name="recycle-view" a="Stupid" is-dynamic-size="true" default-item-size="50" fixed-item-size="22" gutter="0" direction="Horizontal" style="flex-grow: 1; flex-direction: row;" />
</ui:UXML>