namespace UIWidgets { using System; using UnityEngine; using UnityEngine.EventSystems; /// /// Base class for TrackView. /// /// Data type. /// Point type. public abstract class TrackViewBase : MonoBehaviour, IMovableToCache where TData : class, ITrackData where TPoint : IComparable { RectTransform rt; /// /// RectTransform. /// public RectTransform RectTransform { get { if (rt == null) { rt = transform as RectTransform; } return rt; } } /// /// Track. /// public Track Track { get; protected set; } /// /// Show dialog to edit track or add data on double click. /// [SerializeField] [Tooltip("Show dialog to edit track or add data on double click.")] public bool AllowDialog = true; /// /// Name. /// [SerializeField] public TextAdapter Name; /// /// Owner. /// public TracksViewBase Owner; /// /// Start this instance. /// protected virtual void Start() { Init(); } bool isInited; /// /// Init this instance. /// public virtual void Init() { if (isInited) { return; } isInited = true; var click = Utilities.GetOrAddComponent(this); click.DoubleClickEvent.AddListener(OnDoubleClick); } /// /// Process double click event. /// /// Event data. protected virtual void OnDoubleClick(PointerEventData eventData) { if (!Owner.IsInteractable()) { return; } if (!AllowDialog) { return; } Owner.OpenEditTrackDialog(Track); } /// /// Process destroy event. /// protected virtual void OnDestroy() { var click = GetComponent(); if (click != null) { click.DoubleClickEvent.RemoveListener(OnDoubleClick); } } /// /// Set data. /// /// Track. public virtual void SetData(Track track) { Track = track; if (Name != null) { Name.Value = Track.Name; } } /// /// Set width. /// /// Width. public virtual void SetWidth(float width) { RectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Horizontal, width); } /// /// Set height. /// /// Height. public virtual void SetHeight(float height) { RectTransform.SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height); } /// /// Set size. /// /// Size. public virtual void SetSize(Vector2 size) { SetWidth(size.x); SetHeight(size.y); } /// /// Set vertical position. /// /// Vertical position. public virtual void SetVerticalPosition(float position) { var pos = RectTransform.localPosition; pos.y = -position; RectTransform.localPosition = pos; } /// /// Set horizontal position. /// /// Position. public virtual void SetHorizontalPosition(float position) { var pos = RectTransform.localPosition; pos.x = position; RectTransform.localPosition = pos; } /// /// Set position. /// /// Position. public virtual void SetPosition(Vector2 position) { position.y *= -1; RectTransform.localPosition = position; } /// /// Process move to cache. /// public virtual void MovedToCache() { } } }