namespace UIWidgets
{
using System;
using UnityEngine;
using UnityEngine.EventSystems;
///
/// Base class for the data view.
///
/// Type of the data.
/// Type of the points.
[RequireComponent(typeof(RectTransform))]
public abstract class TrackDataViewBase : MonoBehaviour, IMovableToCache
where TData : class, ITrackData
where TPoint : IComparable
{
///
/// Resizable component.
///
[NonSerialized]
protected Resizable Resizable;
///
/// Show dialog to edit data on double click.
///
[SerializeField]
[Tooltip("Show dialog to edit data on double click.")]
public bool AllowDialog = true;
///
/// Owner.
///
public TracksViewBase Owner;
///
/// Data.
///
public TData Data
{
get;
protected set;
}
RectTransform rt;
///
/// RectTransform.
///
public RectTransform RectTransform
{
get
{
if (rt == null)
{
rt = transform as RectTransform;
}
return rt;
}
}
///
/// Start this instance.
///
protected virtual void Start()
{
Init();
}
bool isInited;
///
/// Init this instance.
///
public virtual void Init()
{
if (isInited)
{
return;
}
isInited = true;
AddListeners();
}
///
/// Add listeners.
///
protected virtual void AddListeners()
{
var click = Utilities.GetOrAddComponent(this);
click.DoubleClickEvent.AddListener(OnDoubleClick);
Resizable = GetComponent();
if (Resizable != null)
{
var directions = Resizable.ResizeDirections;
directions.Top = false;
directions.TopLeft = false;
directions.TopRight = false;
directions.Bottom = false;
directions.BottomLeft = false;
directions.BottomRight = false;
Resizable.ResizeDirections = directions;
Resizable.OnEndResize.AddListener(OnEndResize);
}
}
///
/// Remove listeners.
///
protected virtual void RemoveListeners()
{
var click = GetComponent();
if (click != null)
{
click.DoubleClickEvent.RemoveListener(OnDoubleClick);
}
Resizable = GetComponent();
if (Resizable != null)
{
Resizable.OnEndResize.RemoveListener(OnEndResize);
}
}
///
/// Process destroy event.
///
protected virtual void OnDestroy()
{
RemoveListeners();
}
///
/// Process end resize event.
///
/// Resizable component.
protected virtual void OnEndResize(Resizable resizable)
{
var start = RectTransform.localPosition.x;
var end = RectTransform.localPosition.x + RectTransform.rect.width;
Data.ChangePoints(Owner.Position2Point(start), Owner.Position2Point(end));
}
///
/// Enable resizable component.
///
public void EnableResizable()
{
if (Resizable != null)
{
Resizable.Interactable = true;
}
}
///
/// Disable resizable component.
///
public void DisableResizable()
{
if (Resizable != null)
{
Resizable.Interactable = false;
}
}
///
/// Process double click event.
///
/// Event data.
protected virtual void OnDoubleClick(PointerEventData eventData)
{
if (!Owner.IsInteractable())
{
return;
}
if (!AllowDialog)
{
return;
}
Owner.OpenEditTrackDataDialog(Data);
}
///
/// Process move to cache event.
///
public virtual void MovedToCache()
{
}
///
/// Set data.
///
/// Track.
/// Data.
/// Base vertical position.
public abstract void SetData(Track track, TData data, float baseVerticalPostion);
///
/// Set position.
///
public virtual void SetPosition()
{
if (Owner == null)
{
return;
}
if (Data == null)
{
return;
}
var pos = RectTransform.localPosition;
pos.x = Owner.Point2Position(Data.StartPoint);
RectTransform.localPosition = pos;
}
///
/// Set size and position.
///
/// Base vertical position.
public virtual void SetSizeAndPosition(float baseVerticalPostion)
{
if (Owner == null)
{
return;
}
if (Data == null)
{
return;
}
var start = Owner.Point2Position(Data.StartPoint);
var width = Owner.Point2Position(Data.EndPoint) - start;
var height = (Data.Order * (Owner.DefaultItemSize.y + Owner.ItemsSpacing)) + baseVerticalPostion;
RectTransform.localPosition = new Vector2(start, -height);
RectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width);
}
}
}