using UnityEngine;
using UnityEngine.UI;
using EnhancedUI.EnhancedScroller;
using EnhancedUI;
using System;
namespace EnhancedScrollerDemos.ExpandingCells
{
///
/// This is the view of our cell which handles how the cell looks.
///
public class CellView : EnhancedScrollerCellView
{
private Tween tween;
private LayoutElement layoutElement;
private Data data;
public Text dataIndexText;
public Text headerText;
public Text descriptionText;
public Action initializeTween;
public Action updateTween;
public Action endTween;
private void Start()
{
tween = GetComponent();
layoutElement = GetComponent();
}
///
/// This function just takes the Demo data and displays it
///
///
public void SetData(Data data, int dataIndex, float collapsedSize, float expandedSize, Action initializeTween, Action updateTween, Action endTween)
{
this.dataIndex = dataIndex;
this.initializeTween = initializeTween;
this.updateTween = updateTween;
this.endTween = endTween;
dataIndexText.text = dataIndex.ToString();
headerText.text = data.headerText;
descriptionText.text = data.descriptionText;
descriptionText.enabled = data.isExpanded;
this.data = data;
}
///
/// Called when the cell is selected
///
public void CellButton_Clicked()
{
if (initializeTween != null)
{
// start the tweening process by telling the controller
// to prepare all the cells
initializeTween(dataIndex, cellIndex);
}
}
///
/// Called from the controller to kick off the cell's tweening
///
public void BeginTween()
{
// hide the description text so that it does not float outside
// of the cell's view bounds while tweening
descriptionText.enabled = false;
if (!data.isExpanded)
{
// collapse cell view
layoutElement.minHeight = data.expandedSize;
// if this is an immediate tween, just call the TweenCompleted method
if (data.tweenType == Tween.TweenType.immediate)
{
TweenCompleted();
return;
}
StartCoroutine(tween.TweenPosition(data.tweenType, data.tweenTimeCollapse, data.expandedSize, data.collapsedSize, TweenUpdated, TweenCompleted));
}
else
{
// expand cell view
layoutElement.minHeight = data.collapsedSize;
// if this is an immediate tween, just call the TweenCompleted method
if (data.tweenType == Tween.TweenType.immediate)
{
TweenCompleted();
return;
}
StartCoroutine(tween.TweenPosition(data.tweenType, data.tweenTimeExpand, data.collapsedSize, data.expandedSize, TweenUpdated, TweenCompleted));
}
}
///
/// Called when the cell's size changes
///
/// The new cell size
/// The change in the cell's size
private void TweenUpdated(float newValue, float delta)
{
// update the size of the cell view
layoutElement.minHeight += delta; // newValue;
if (updateTween != null)
{
// call the update tween on the controller
// in order to update the last padder
updateTween(dataIndex, cellIndex, newValue, delta);
}
}
///
/// Called when the cell size has finished tweening
///
private void TweenCompleted()
{
if (data.isExpanded)
{
// show the description text if the cell is expanded
descriptionText.enabled = true;
}
if (endTween != null)
{
// tween is completed, so now we can reload the scroller
endTween(dataIndex, cellIndex);
}
}
}
}