Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Assets/Editor/BaseScrollEditor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using UnityEditor.UIElements;
using UnityEngine.UIElements;

namespace SimpleScroll.Editor
{
public abstract class BaseScrollEditor : UnityEditor.Editor
{
public override VisualElement CreateInspectorGUI()
{
var container = new VisualElement();
container.Add(new PropertyField(serializedObject.FindProperty("_scroller._axis")));
container.Add(new PropertyField(serializedObject.FindProperty("_viewport")));
container.Add(new PropertyField(serializedObject.FindProperty("_content")));
container.Add(new PropertyField(serializedObject.FindProperty("_contentPadding")));
container.Add(new PropertyField(serializedObject.FindProperty("_space")));
CreateInherentProperties(container);
container.Add(new PropertyField(serializedObject.FindProperty("_isScrollable"), "Scrollable"));
var isDraggable = new PropertyField(serializedObject.FindProperty("_isDraggable"), "Draggable");
var scroller = new ScrollerDrawer().CreatePropertyGUI(serializedObject.FindProperty("_scroller"));
isDraggable.RegisterValueChangeCallback(evt => { scroller.SetEnabled(evt.changedProperty.boolValue); });
container.Add(isDraggable);
container.Add(scroller);
container.Add(new PropertyField(serializedObject.FindProperty("_scroller._onValueChanged")));
return container;
}

protected virtual void CreateInherentProperties(VisualElement container)
{
}
}
}
3 changes: 3 additions & 0 deletions Assets/Editor/BaseScrollEditor.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 2 additions & 10 deletions Assets/Editor/CarouselScrollEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@
namespace SimpleScroll.Editor
{
[CustomEditor(typeof(CarouselScroll))]
public class CarouselScrollEditor : UnityEditor.Editor
public class CarouselScrollEditor : BaseScrollEditor
{
public override VisualElement CreateInspectorGUI()
protected override void CreateInherentProperties(VisualElement container)
{
var container = new VisualElement();
container.Add(new PropertyField(serializedObject.FindProperty("_scroller._axis")));
container.Add(new PropertyField(serializedObject.FindProperty("_viewport")));
container.Add(new PropertyField(serializedObject.FindProperty("_content")));
container.Add(new PropertyField(serializedObject.FindProperty("_space")));
container.Add(new PropertyField(serializedObject.FindProperty("_cellSize")));
container.Add(new PropertyField(serializedObject.FindProperty("_loop")));
container.Add(new PropertyField(serializedObject.FindProperty("_indicator")));
container.Add(new ScrollerDrawer().CreatePropertyGUI(serializedObject.FindProperty("_scroller")));
container.Add(new PropertyField(serializedObject.FindProperty("_scroller._onValueChanged")));
return container;
}
}
}
Expand Down
13 changes: 2 additions & 11 deletions Assets/Editor/FixedGridScroll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,13 @@
namespace SimpleScroll.Editor
{
[CustomEditor(typeof(FixedGridScroll))]
public class FixedGridScrollEditor : UnityEditor.Editor
public class FixedGridScrollEditor : BaseScrollEditor
{
public override VisualElement CreateInspectorGUI()
protected override void CreateInherentProperties(VisualElement container)
{
var container = new VisualElement();
container.Add(new PropertyField(serializedObject.FindProperty("_scroller._axis")));
container.Add(new PropertyField(serializedObject.FindProperty("_viewport")));
container.Add(new PropertyField(serializedObject.FindProperty("_content")));
container.Add(new PropertyField(serializedObject.FindProperty("_contentPadding")));
container.Add(new PropertyField(serializedObject.FindProperty("_space")));
container.Add(new PropertyField(serializedObject.FindProperty("_cellSize")));
container.Add(new PropertyField(serializedObject.FindProperty("_column")));
container.Add(new PropertyField(serializedObject.FindProperty("_scrollbar")));
container.Add(new ScrollerDrawer().CreatePropertyGUI(serializedObject.FindProperty("_scroller")));
container.Add(new PropertyField(serializedObject.FindProperty("_scroller._onValueChanged")));
return container;
}
}
}
Expand Down
13 changes: 2 additions & 11 deletions Assets/Editor/FixedListScrollEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,12 @@
namespace SimpleScroll.Editor
{
[CustomEditor(typeof(FixedListScroll))]
public class FixedListScrollEditor : UnityEditor.Editor
public class FixedListScrollEditor : BaseScrollEditor
{
public override VisualElement CreateInspectorGUI()
protected override void CreateInherentProperties(VisualElement container)
{
var container = new VisualElement();
container.Add(new PropertyField(serializedObject.FindProperty("_scroller._axis")));
container.Add(new PropertyField(serializedObject.FindProperty("_viewport")));
container.Add(new PropertyField(serializedObject.FindProperty("_content")));
container.Add(new PropertyField(serializedObject.FindProperty("_contentPadding")));
container.Add(new PropertyField(serializedObject.FindProperty("_space")));
container.Add(new PropertyField(serializedObject.FindProperty("_cellSize")));
container.Add(new PropertyField(serializedObject.FindProperty("_scrollbar")));
container.Add(new ScrollerDrawer().CreatePropertyGUI(serializedObject.FindProperty("_scroller")));
container.Add(new PropertyField(serializedObject.FindProperty("_scroller._onValueChanged")));
return container;
}
}
}
Expand Down
13 changes: 2 additions & 11 deletions Assets/Editor/LazyLayoutListScrollEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,12 @@
namespace SimpleScroll.Editor
{
[CustomEditor(typeof(LazyLayoutListScroll))]
public class LazyLayoutListScrollEditor : UnityEditor.Editor
public class LazyLayoutListScrollEditor : BaseScrollEditor
{
public override VisualElement CreateInspectorGUI()
protected override void CreateInherentProperties(VisualElement container)
{
var container = new VisualElement();
container.Add(new PropertyField(serializedObject.FindProperty("_scroller._axis")));
container.Add(new PropertyField(serializedObject.FindProperty("_viewport")));
container.Add(new PropertyField(serializedObject.FindProperty("_content")));
container.Add(new PropertyField(serializedObject.FindProperty("_contentPadding")));
container.Add(new PropertyField(serializedObject.FindProperty("_space")));
container.Add(new PropertyField(serializedObject.FindProperty("_defaultCellSize")));
container.Add(new PropertyField(serializedObject.FindProperty("_scrollbar")));
container.Add(new ScrollerDrawer().CreatePropertyGUI(serializedObject.FindProperty("_scroller")));
container.Add(new PropertyField(serializedObject.FindProperty("_scroller._onValueChanged")));
return container;
}
}
}
Expand Down
13 changes: 2 additions & 11 deletions Assets/Editor/SizedListScrollEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,11 @@
namespace SimpleScroll.Editor
{
[CustomEditor(typeof(SizedListScroll))]
public class SizedListScrollEditor : UnityEditor.Editor
public class SizedListScrollEditor : BaseScrollEditor
{
public override VisualElement CreateInspectorGUI()
protected override void CreateInherentProperties(VisualElement container)
{
var container = new VisualElement();
container.Add(new PropertyField(serializedObject.FindProperty("_scroller._axis")));
container.Add(new PropertyField(serializedObject.FindProperty("_viewport")));
container.Add(new PropertyField(serializedObject.FindProperty("_content")));
container.Add(new PropertyField(serializedObject.FindProperty("_contentPadding")));
container.Add(new PropertyField(serializedObject.FindProperty("_space")));
container.Add(new PropertyField(serializedObject.FindProperty("_scrollbar")));
container.Add(new ScrollerDrawer().CreatePropertyGUI(serializedObject.FindProperty("_scroller")));
container.Add(new PropertyField(serializedObject.FindProperty("_scroller._onValueChanged")));
return container;
}
}
}
Expand Down
93 changes: 57 additions & 36 deletions Assets/Runtime/BaseScroll.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,56 @@ public abstract class BaseScroll<TDataSource> : UIBehaviour, IBeginDragHandler,
[SerializeField] private RectTransform _content;
[SerializeField] private Scroller _scroller;
[SerializeField] private Scrollbar _scrollbar;
[SerializeField] private bool _isDraggable = true;
[SerializeField] private bool _isScrollable = true;

private bool _isDirty;
private int _pointerId = int.MinValue;
private int _dataCount;
private bool _isResized;

internal Scroller Scroller => _scroller;
internal Scroller Scroller
{
get
{
_scroller ??= new Scroller();
return _scroller;
}
}

protected RectTransform Content => _content;
internal CellViewPool CellViewPool { get; } = new();
protected TDataSource DataSource { get; private set; }
protected float ViewportSize { get; private set; }
protected float ViewportHalf { get; private set; }
public Range VisibleRange { get; private set; } = Range.Empty;
public ScrollEvent OnValueChanged => Scroller?.OnValueChanged;
public bool IsDraggable { get; set; } = true;
public bool IsScrollable { get; set; } = true;
public bool IsDragging => Scroller?.IsDragging ?? false;
public bool IsScrolling => Scroller?.IsScrolling ?? false;
public float ScrollPosition => Scroller?.ScrollPosition ?? 0f;
public float NormalizedPosition => Scroller?.NormalizedPosition ?? 0f;
public ScrollEvent OnValueChanged => _scroller.OnValueChanged;
public bool IsDragging => Scroller.IsDragging;
public bool IsScrolling => Scroller.IsScrolling;
public bool IsIdling => Scroller.IsIdling;
public float ScrollPosition => Scroller.ScrollPosition;
public float NormalizedPosition => Scroller.NormalizedPosition;

public bool IsDraggable
{
get => _isDraggable;
set => _isDraggable = value;
}

public bool IsScrollable
{
get => _isScrollable;
set => _isScrollable = value;
}

public event Action<Range> OnVisibleRangeChanged;

public event Action<ScrollStatus> OnScrollStateChanged
{
add => Scroller.OnScrollStateChanged += value;
remove => Scroller.OnScrollStateChanged -= value;
}

protected override void OnEnable()
{
_pointerId = int.MinValue;
Expand All @@ -55,9 +82,6 @@ protected override void OnEnable()

protected override void OnDisable()
{
#if UNITY_EDITOR
_tracker.Clear();
#endif
if (_scrollbar == null) return;
_scrollbar.onValueChanged.RemoveListener(OnScrollbarValueChanged);
}
Expand All @@ -69,25 +93,24 @@ protected override void OnRectTransformDimensionsChange()

private void OnScrollbarValueChanged(float normalizedPosition)
{
if (_scroller == null) return;
_scroller.OnScroll();
Scroller.OnScroll();
SetNormalizedPosition(normalizedPosition);
}

private void UpdateScrollbar()
{
if (_scrollbar == null || _scroller == null) return;
_scrollbar.SetValueWithoutNotify(_scroller.NormalizedPosition);
if (_scrollbar == null) return;
_scrollbar.SetValueWithoutNotify(Scroller.NormalizedPosition);
}

private void Resize()
{
if (DataSource == null || _scroller == null) return;
ViewportSize = _viewport.rect.size[_scroller.Axis];
if (DataSource == null) return;
ViewportSize = _viewport.rect.size[Scroller.Axis];
ViewportHalf = ViewportSize * 0.5f;

var scrollSize = GetScrollSize();
_scroller.Initialize(scrollSize);
Scroller.Initialize(scrollSize);
_isResized = true;

if (_scrollbar == null) return;
Expand Down Expand Up @@ -136,8 +159,7 @@ protected void UpdatePosition(float targetPosition)
_isDirty = false;
}

if (_scroller == null) return;
var scrollDelta = _scroller.Update(targetPosition);
var scrollDelta = Scroller.Update(targetPosition);
UpdateScrollbar();
var visibleRange = Reposition(scrollDelta, _isResized);
_isResized = false;
Expand All @@ -147,43 +169,44 @@ protected void UpdatePosition(float targetPosition)
OnVisibleRangeChanged?.Invoke(visibleRange);
}

if (_scroller.IsScrolling)
if (Scroller.IsScrolling)
{
_scroller.Stop();
Scroller.Stop();
}
}

private void SetNormalizedPosition(float normalizedPosition)
{
_scroller.NormalizedPosition = normalizedPosition;
OnNormalizePositionChanged(_scroller.NormalizedPosition);
Scroller.NormalizedPosition = normalizedPosition;
OnNormalizePositionChanged(Scroller.NormalizedPosition);
}

void IBeginDragHandler.OnBeginDrag(PointerEventData e)
{
if (DataSource == null || _pointerId != int.MinValue) return;
if (DataSource == null || _pointerId != int.MinValue || !IsDraggable) return;
_pointerId = e.pointerId;
_scroller?.OnBeginDrag(e);
Scroller.OnBeginDrag(e);
}

void IDragHandler.OnDrag(PointerEventData e)
{
if (DataSource == null || _pointerId != e.pointerId || !IsDraggable) return;
_scroller?.OnDrag(e);
Scroller.OnDrag(e);
}

void IEndDragHandler.OnEndDrag(PointerEventData e)
{
if (DataSource == null || _pointerId != e.pointerId) return;
_pointerId = int.MinValue;
Comment thread
kurobon-jp marked this conversation as resolved.
var targetPosition = _scroller?.OnEndDrag(e) ?? 0f;
if (!IsDraggable) return;
var targetPosition = Scroller.OnEndDrag(e);
OnDrag(targetPosition);
}

void IScrollHandler.OnScroll(PointerEventData e)
{
if (DataSource == null || !IsScrollable) return;
_scroller?.OnScroll();
Scroller.OnScroll();
OnScroll(e.scrollDelta.GetAxialValue());
}

Expand All @@ -199,9 +222,8 @@ protected virtual void OnNormalizePositionChanged(float normalizedPosition)

public void StopScroll()
{
if (_scroller == null) return;
var velocity = _scroller.Velocity;
_scroller.Stop();
var velocity = Scroller.Velocity;
Scroller.Stop();
OnStopScroll(velocity);
}

Expand All @@ -217,8 +239,7 @@ protected void SetDirty()

private void SetAxisPivotAndDeltaSize()
{
if (_scroller == null) return;
var axis = _scroller.Axis;
var axis = Scroller.Axis;
if (_viewport != null)
{
_viewport.SetPivot(0.5f, axis);
Expand All @@ -235,9 +256,9 @@ private void SetAxisPivotAndDeltaSize()

protected override void OnValidate()
{
if (IsDestroyed() || _scroller == null) return;
if (IsDestroyed()) return;
_tracker.Clear();
var axis = _scroller.Axis;
var axis = Scroller.Axis;
var pivot = axis == 0
? DrivenTransformProperties.PivotX
: DrivenTransformProperties.PivotY;
Expand Down
Loading