You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

161 lines
6.0 KiB

using UnityEngine;
using EnhancedUI;
using EnhancedUI.EnhancedScroller;
/// <summary>
/// Set up our demo script as a delegate for the scroller by inheriting from the IEnhancedScrollerDelegate interface
///
/// EnhancedScroller delegates will handle telling the scroller:
/// - How many cells it should allocate room for (GetNumberOfCells)
/// - What each cell size is (GetCellSize)
/// - What the cell at a given index should be (GetCell)
/// </summary>
public class EScrController : MonoBehaviour, IEnhancedScrollerDelegate
{
/// <summary>
/// Internal representation of our data. Note that the scroller will never see
/// this, so it separates the data from the layout using MVC principles.
/// </summary>
private SmallList<int> _data;
protected int iType = -1;
/// <summary>
/// This is our scroller we will be a delegate for
/// </summary>
public EnhancedScroller scroller;
/// <summary>
/// This will be the prefab of each cell in our scroller. Note that you can use more
/// than one kind of cell, but this example just has the one type.
/// </summary>
[SerializeField]
protected EnhancedScrollerCellView cellViewPrefab;
protected float fSize = 100f;
/// <summary>
/// Be sure to set up your references to the scroller after the Awake function. The
/// scroller does some internal configuration in its own Awake function. If you need to
/// do this in the Awake function, you can set up the script order through the Unity editor.
/// In this case, be sure to set the EnhancedScroller's script before your delegate.
///
/// In this example, we are calling our initializations in the delegate's Start function,
/// but it could have been done later, perhaps in the Update function.
/// </summary>
protected void Start()
{
// tell the scroller that this script will be its delegate
scroller.Delegate = this;
if (scroller.scrollDirection == EnhancedScroller.ScrollDirectionEnum.Vertical)
{
fSize = cellViewPrefab.GetComponent<RectTransform>().sizeDelta.y;
}
else
{
fSize = cellViewPrefab.GetComponent<RectTransform>().sizeDelta.x;
}
}
public void SetType(int itype)
{
iType = itype;
int icnt = scroller.GetCellViewsCount();
for (int i = 0; i < icnt; i++)
(scroller.GetCellViewsAtCellIndex(i) as EScrCell).SetType(itype);
}
public void ReleaseResources()
{
int icnt = scroller.GetCellViewsCount();
for (int i = 0; i < icnt; i++)
scroller.GetCellViewsAtCellIndex(i).ReleaseResources();
}
public void Localize()
{
int icnt = scroller.GetCellViewsCount();
for (int i = 0; i < icnt; i++)
scroller.GetCellViewsAtCellIndex(i).Localize();
}
public void SetFloat(float fvalue)
{
int icnt = scroller.GetCellViewsCount();
for (int i = 0; i < icnt; i++)
scroller.GetCellViewsAtCellIndex(i).SetFloat(fvalue);
}
object intent;
public void LoadDatas(SmallList<int> datalist)
{
intent = null;
_data = datalist;
scroller.ReloadData();
}
public void LoadDatas(SmallList<int> datalist, object intent)
{
this.intent = intent;
_data = datalist;
scroller.ReloadData();
}
#region EnhancedScroller Handlers
/// <summary>
/// This tells the scroller the number of cells that should have room allocated. This should be the length of your data array.
/// </summary>
/// <param name="scroller">The scroller that is requesting the data size</param>
/// <returns>The number of cells</returns>
public int GetNumberOfCells(EnhancedScroller scroller)
{
if (_data == null)
return 0;
// in this example, we just pass the number of our data elements
return _data.Count;
}
/// <summary>
/// This tells the scroller what the size of a given cell will be. Cells can be any size and do not have
/// to be uniform. For vertical scrollers the cell size will be the height. For horizontal scrollers the
/// cell size will be the width.
/// </summary>
/// <param name="scroller">The scroller requesting the cell size</param>
/// <param name="dataIndex">The index of the data that the scroller is requesting</param>
/// <returns>The size of the cell</returns>
public float GetCellViewSize(EnhancedScroller scroller, int dataIndex)
{
return fSize;
}
/// <summary>
/// Gets the cell to be displayed. You can have numerous cell types, allowing variety in your list.
/// Some examples of this would be headers, footers, and other grouping cells.
/// </summary>
/// <param name="scroller">The scroller requesting the cell</param>
/// <param name="dataIndex">The index of the data that the scroller is requesting</param>
/// <param name="cellIndex">The index of the list. This will likely be different from the dataIndex if the scroller is looping</param>
/// <returns>The cell for the scroller to use</returns>
public EnhancedScrollerCellView GetCellView(EnhancedScroller scroller, int dataIndex, int cellIndex)
{
// first, we get a cell from the scroller by passing a prefab.
// if the scroller finds one it can recycle it will do so, otherwise
// it will create a new cell.
EScrCell cellview = scroller.GetCellView(cellViewPrefab) as EScrCell;
// set the name of the game object to the cell's data index.
// this is optional, but it helps up debug the objects in
// the scene hierarchy.
cellview.name = FormatString.StringFormat("Cell{0:0000}", dataIndex);
cellview.SetType(iType);
// in this example, we just pass the data to our cell's view which will update its UI
if(intent != null && cellview is IOnRecieveIntent reciever)
reciever.OnRecieveIntent(intent);
cellview.SetData(_data[dataIndex]);
// return the cell to the scroller
return cellview;
}
#endregion
}