namespace UIWidgets
{
using System;
using System.Globalization;
using UIWidgets.Attributes;
using UnityEngine;
///
/// DateTime widget.
///
[DataBindSupport]
public abstract class DateTimeWidgetBase : MonoBehaviourConditional
{
[SerializeField]
bool currentDateTimeAsDefault = false;
///
/// The current DateTime.
///
[SerializeField]
protected DateTime dateTime;
///
/// The current DateTime.
///
/// The date time.
[DataBindField]
public virtual DateTime DateTime
{
get
{
return dateTime;
}
set
{
Init();
if (dateTime != value)
{
dateTime = value;
UpdateWidgets();
OnDateTimeChanged.Invoke(dateTime);
}
}
}
///
/// The current date time.
///
[SerializeField]
[EditorConditionBool("currentDateTimeAsDefault", false)]
protected string currentDateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
///
/// The format.
///
[SerializeField]
[EditorConditionBool("currentDateTimeAsDefault", false)]
protected string format = "yyyy-MM-dd HH:mm:ss";
///
/// Event called when date-time changed.
///
[DataBindEvent("DateTime")]
public CalendarDateEvent OnDateTimeChanged = new CalendarDateEvent();
bool isInited;
///
/// Culture to parse date.
///
public abstract CultureInfo Culture
{
get;
set;
}
///
/// Start this instance.
///
public virtual void Start()
{
Init();
}
///
/// Init this instance.
///
public virtual void Init()
{
if (isInited)
{
return;
}
isInited = true;
dateTime = currentDateTimeAsDefault ? DateTime.Now : DateTime.ParseExact(currentDateTime, format, Culture);
UpdateWidgets();
AddListeners();
}
///
/// Updates the widgets.
///
protected abstract void UpdateWidgets();
///
/// Adds the listeners.
///
protected virtual void AddListeners()
{
}
///
/// Removes the listeners.
///
protected virtual void RemoveListeners()
{
}
///
/// Process the destroy event.
///
protected virtual void OnDestroy()
{
RemoveListeners();
}
}
}