Skip to content

Commit

Permalink
Merge pull request #20 from zhaozilong1988/release/v202312070022
Browse files Browse the repository at this point in the history
New Features: Magnetic scroll, Flick detecting, Enhance IUITableViewDraggable
  • Loading branch information
zhaozilong1988 authored Dec 9, 2023
2 parents e43012b + 80ddd34 commit 0930608
Show file tree
Hide file tree
Showing 15 changed files with 498 additions and 266 deletions.
6 changes: 3 additions & 3 deletions Assets/Scenes/SampleScene.unity
Original file line number Diff line number Diff line change
Expand Up @@ -1734,7 +1734,7 @@ MonoBehaviour:
_content: {fileID: 879252848}
_direction: 3
_ignoreCellLifeCycle: 0
tag: 0
_tag: 0
keepClickEvenIfBeginDrag: 0
_clickableCell: {fileID: 952083237558582737, guid: f63765b6dadb145f8a2fa180f201cf54,
type: 3}
Expand Down Expand Up @@ -2548,7 +2548,7 @@ MonoBehaviour:
_content: {fileID: 1187111710}
_direction: 0
_ignoreCellLifeCycle: 0
tag: 0
_tag: 0
keepClickEvenIfBeginDrag: 0
_gridCell: {fileID: 8669581284404242529, guid: 34756287df3954d08a084bfea8364c58,
type: 3}
Expand Down Expand Up @@ -2858,7 +2858,7 @@ MonoBehaviour:
_content: {fileID: 686237908}
_direction: 0
_ignoreCellLifeCycle: 0
tag: 0
_tag: 0
keepClickEvenIfBeginDrag: 0
_imageCellPrefab: {fileID: 6296921478661641863, guid: 5a54b65bbf2034e0c90a804a1cfa9477,
type: 3}
Expand Down
48 changes: 28 additions & 20 deletions Assets/Scenes/Scripts/SampleGridView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ protected override void Awake()
// Setup table view
dataSource = this;
@delegate = this;
draggable = this;
clickable = this;
}

Expand All @@ -49,7 +48,9 @@ protected override void Update()
public void SwitchDragMode()
{
_mode = _mode != Mode.Draggable ? Mode.Draggable : Mode.Normal;
scrollRect.vertical = _mode != Mode.Draggable;
var undraggable = _mode != Mode.Draggable;
scrollRect.vertical = undraggable;
draggable = undraggable ? null : this;
ReloadData();
}

Expand Down Expand Up @@ -86,9 +87,9 @@ public int NumberOfColumnPerRow(UITableView tableView, int rowIndex)
return _columnNumber;
}

public UITableViewCellAlignment AlignmentOfCellsAtLastRow(UITableView grid)
public UITableViewAlignment AlignmentOfCellsAtLastRow(UITableView grid)
{
return UITableViewCellAlignment.Center;
return UITableViewAlignment.Center;
}

public void CellAtIndexInTableViewWillAppear(UITableView tableView, int index)
Expand All @@ -107,36 +108,43 @@ public Camera TableViewCameraForInteractive(UITableView tableView)
return null;
}

public bool TableViewOnBeginDragCellAt(UITableView tableView, int draggedIndex, PointerEventData eventData)
public bool TableViewDragCellMovable(UITableView tableView)
{
var isDraggable = _dataList[draggedIndex] >= 0 && _mode == Mode.Draggable;
if (isDraggable) {
tableView.GetLoadedCell(draggedIndex).rectTransform.SetAsLastSibling();
}
return isDraggable;
return true;
}

public void TableViewOnDragCellAt(UITableView tableView, int draggedIndex, PointerEventData eventData)
public void TableViewOnBeginDrag(UITableView tableView, int? draggedIndex, PointerEventData eventData)
{
foreach (var cell in GetAllLoadedCells<SampleGridCell>()) {
if (!draggedIndex.HasValue)
return;
if (_dataList[draggedIndex.Value] >= 0 && _mode == Mode.Draggable)
tableView.GetLoadedCell(draggedIndex.Value).rectTransform.SetAsLastSibling();
}

public void TableViewOnDrag(UITableView tableView, int? draggedIndex, PointerEventData eventData)
{
if (!draggedIndex.HasValue)
return;
foreach (var cell in GetAllLoadedCells<SampleGridCell>())
cell.SetMergeable(false);
}
if (tableView.TryFindMostIntersectedCell<SampleGridCell>(draggedIndex, out var mostIntersectedCellIndex, out var maxArea) && maxArea > 150f) {
if (tableView.TryFindMostIntersectedCell<SampleGridCell>(draggedIndex.Value, out var mostIntersectedCellIndex, out var maxArea) && maxArea > 150f)
tableView.GetLoadedCell<SampleGridCell>(mostIntersectedCellIndex).SetMergeable(true);
}
}

public void TableViewOnEndDragCellAt(UITableView tableView, int draggedIndex, PointerEventData eventData)
public void TableViewOnEndDrag(UITableView tableView, int? draggedIndex, PointerEventData eventData)
{
if (!draggedIndex.HasValue) {
return;
}
foreach (var cell in GetAllLoadedCells<SampleGridCell>()) {
cell.SetMergeable(false);
}
if (tableView.TryFindMostIntersectedCell<SampleGridCell>(draggedIndex, out var mostIntersectedCellIndex, out var maxArea) && maxArea > 150f) {
if (tableView.TryFindMostIntersectedCell<SampleGridCell>(draggedIndex.Value, out var mostIntersectedCellIndex, out var maxArea) && maxArea > 150f) {
var swap = _dataList[mostIntersectedCellIndex];
_dataList[mostIntersectedCellIndex] = _dataList[draggedIndex];
_dataList[draggedIndex] = swap;
_dataList[mostIntersectedCellIndex] = _dataList[draggedIndex.Value];
_dataList[draggedIndex.Value] = swap;
ReloadDataAt(mostIntersectedCellIndex);
ReloadDataAt(draggedIndex);
ReloadDataAt(draggedIndex.Value);
}
RefreshAllLoadedCells();
}
Expand Down
2 changes: 1 addition & 1 deletion Assets/Scenes/Scripts/SampleScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void OnClickDeleteCell()
void OnClickScrollToCell(int index)
{
Debug.Log("Scroll to cell at index of " + index);
_tableView.ScrollToCellAt(index, 0.3f, withMargin: true, onScrollingStopped: () => {
_tableView.ScrollToCellAt(index, 0.3f, withMargin: true, onScrollingStopped: interrupted => {
Debug.Log("Scrolling has finished");
});

Expand Down
2 changes: 1 addition & 1 deletion Assets/UIKit/UITableView/IUIGridViewDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public interface IUIGridViewDataSource : IUITableViewDataSource
/// <summary>
/// Alignment of cells at last row in grid view (table view).
/// </summary>
UITableViewCellAlignment AlignmentOfCellsAtLastRow(UITableView grid);
UITableViewAlignment AlignmentOfCellsAtLastRow(UITableView grid);
}
}
10 changes: 7 additions & 3 deletions Assets/UIKit/UITableView/IUITableViewDraggable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ namespace UIKit
/// </summary>
public interface IUITableViewDraggable : IUITableViewInteractable
{
/// <summary>
/// The cell will be dragged to move position if this returns true.
/// </summary>
bool TableViewDragCellMovable(UITableView tableView);
/// <summary>
/// Called on the drag UITableViewCell at index when dragging is about to begin
/// </summary>
bool TableViewOnBeginDragCellAt(UITableView tableView, int draggedIndex, PointerEventData eventData);
void TableViewOnBeginDrag(UITableView tableView, int? draggedIndex, PointerEventData eventData);
/// <summary>
/// Called on the drag UITableViewCell at index when a drag is happening
/// </summary>
void TableViewOnDragCellAt(UITableView tableView, int draggedIndex, PointerEventData eventData);
void TableViewOnDrag(UITableView tableView, int? draggedIndex, PointerEventData eventData);
/// <summary>
/// Called on the drag UITableViewCell at index when a drag finishes
/// </summary>
void TableViewOnEndDragCellAt(UITableView tableView, int draggedIndex, PointerEventData eventData);
void TableViewOnEndDrag(UITableView tableView, int? draggedIndex, PointerEventData eventData);
}
}
26 changes: 26 additions & 0 deletions Assets/UIKit/UITableView/IUITableViewFlickable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
namespace UIKit
{
/// <summary>
/// Implement this if you want receive events when table view is flicked.
/// </summary>
public interface IUITableViewFlickable : IUITableViewInteractable
{
/// <summary>
/// The flick will not be triggered if flicked distance outside of this range.
/// </summary>
/// <returns>Flick trigger range: units/sec</returns>
(float lower, float upper) FlickDistanceRangeOfTriggerFlickInTableView(UITableView tableView);
/// <summary>
/// The flick will not be triggered if flicked time outside of this range.
/// </summary>
/// <returns>Flick trigger time: sec</returns>
float FlickTimeOfTriggerFlickInTableView(UITableView tableView);
/// <summary>
/// The flick is triggered.
/// </summary>
/// <param name="tableView">tableView</param>
/// <param name="indexOfFlickedCell">flicked cell</param>
/// <param name="direction">flicked direction</param>
void TableViewOnDidFlick(UITableView tableView, int? indexOfFlickedCell, UITableViewDirection direction);
}
}
11 changes: 11 additions & 0 deletions Assets/UIKit/UITableView/IUITableViewFlickable.cs.meta

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

33 changes: 33 additions & 0 deletions Assets/UIKit/UITableView/IUITableViewMagneticAlignment.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using UnityEngine;

namespace UIKit
{
/// <summary>
/// Implement this if you want a magnetic alignment when scrolling stops.
/// </summary>
public interface IUITableViewMagneticAlignment
{
/// <summary>
/// When the scrolling speed is below this threshold, magnetic alignment will be triggered.
/// </summary>
/// <returns>Speed:n units/sec</returns>
float SpeedOfTriggerMagneticAlignmentInTableView(UITableView tableView);
/// <summary>
/// The speed used during magnetic alignment completion.
/// </summary>
/// <returns>Speed:n units/sec</returns>
float SpeedOfCompleteMagneticAlignmentInTableView(UITableView tableView);
/// <summary>
/// The calibration point for where the magnetic alignment will be aimed.
/// </summary>
/// <returns>Calibration point(Vector.zero: for top or rightmost ~ Vector.one: for bottom or leftmost)</returns>
Vector2 CalibrationPointOfMagneticAlignmentInTableView(UITableView tableView);
/// <summary>
/// Notify when magnetic alignment state changed.
/// </summary>
/// <param name="tableView">tableView</param>
/// <param name="ofCellIndex">Index of cell which is aimed by magnetic alignment</param>
/// <param name="state">UITableViewMagneticState</param>
void MagneticStateDidChangeInTableView(UITableView tableView, int ofCellIndex, UITableViewMagneticState state);
}
}
11 changes: 11 additions & 0 deletions Assets/UIKit/UITableView/IUITableViewMagneticAlignment.cs.meta

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

Loading

0 comments on commit 0930608

Please sign in to comment.