namespace UIWidgets
{
using System;
using System.Collections;
using System.Collections.Generic;
using UIWidgets.Attributes;
using UIWidgets.Styles;
using UnityEngine;
using UnityEngine.EventSystems;
///
/// Base class for Time widget.
///
[DataBindSupport]
public abstract class TimeBase : UIBehaviourConditional, IStylable
{
#region Interactable
[SerializeField]
bool interactable = true;
///
/// Is widget interactable.
///
/// true if interactable; otherwise, false.
public bool Interactable
{
get
{
return interactable;
}
set
{
if (interactable != value)
{
interactable = value;
InteractableChanged();
}
}
}
///
/// If the canvas groups allow interaction.
///
protected bool GroupsAllowInteraction = true;
///
/// The CanvasGroup cache.
///
protected List CanvasGroupCache = new List();
///
/// Process the CanvasGroupChanged event.
///
protected override void OnCanvasGroupChanged()
{
var groupAllowInteraction = true;
var t = transform;
while (t != null)
{
t.GetComponents(CanvasGroupCache);
var shouldBreak = false;
foreach (var canvas_group in CanvasGroupCache)
{
if (!canvas_group.interactable)
{
groupAllowInteraction = false;
shouldBreak = true;
}
shouldBreak |= canvas_group.ignoreParentGroups;
}
if (shouldBreak)
{
break;
}
t = t.parent;
}
if (groupAllowInteraction != GroupsAllowInteraction)
{
GroupsAllowInteraction = groupAllowInteraction;
InteractableChanged();
}
}
///
/// Returns true if the GameObject and the Component are active.
///
/// true if the GameObject and the Component are active; otherwise false.
public override bool IsActive()
{
return base.IsActive() && GroupsAllowInteraction && Interactable && isInited;
}
///
/// Process interactable change.
///
protected virtual void InteractableChanged()
{
if (!base.IsActive() || !isInited)
{
return;
}
OnInteractableChange(GroupsAllowInteraction && Interactable);
}
///
/// Process interactable change.
///
/// Current interactable state.
protected virtual void OnInteractableChange(bool interactableState)
{
}
#endregion
[SerializeField]
bool currentTimeAsDefault = false;
///
/// The time as text.
///
[SerializeField]
[EditorConditionBool("currentTimeAsDefault", false)]
protected string timeText = DateTime.Now.TimeOfDay.ToString();
///
/// The minimum time as text.
///
[SerializeField]
protected string timeMinText = "00:00:00";
///
/// The maximum time as text.
///
[SerializeField]
protected string timeMaxText = "23:59:59";
///
/// The time.
///
[SerializeField]
protected TimeSpan time = DateTime.Now.TimeOfDay;
///
/// The time.
///
[DataBindField]
public virtual TimeSpan Time
{
get
{
return time;
}
set
{
Init();
if (time == value)
{
return;
}
time = value;
if (time.Ticks < 0)
{
time += new TimeSpan(1, 0, 0, 0);
}
if (time.Days > 0)
{
time -= new TimeSpan(time.Days, 0, 0, 0);
}
time = Clamp(time);
UpdateInputs();
OnTimeChanged.Invoke(time);
}
}
///
/// The minimum time.
///
[SerializeField]
protected TimeSpan timeMin = new TimeSpan(0, 0, 0);
///
/// The minimum time.
///
/// The time minimum.
public TimeSpan TimeMin
{
get
{
return timeMin;
}
set
{
timeMin = value;
Time = Clamp(Time);
}
}
///
/// The maximum time.
///
[SerializeField]
protected TimeSpan timeMax = new TimeSpan(23, 59, 59);
///
/// The maximum time.
///
/// The time max.
public TimeSpan TimeMax
{
get
{
return timeMax;
}
set
{
timeMax = value;
Time = Clamp(Time);
}
}
///
/// The OnTimeChanged event.
///
[DataBindEvent("Time")]
public TimeEvent OnTimeChanged = new TimeEvent();
bool isInited;
///
/// Start this instance.
///
protected override void Start()
{
base.Start();
Init();
}
///
/// Init this instance.
///
public virtual void Init()
{
if (isInited)
{
return;
}
isInited = true;
if (currentTimeAsDefault)
{
time = DateTime.Now.TimeOfDay;
}
else
{
if (!TimeSpan.TryParse(timeText, out time))
{
Debug.LogWarning("TimeText cannot be parsed.");
}
}
if (!TimeSpan.TryParse(timeMinText, out timeMin))
{
Debug.LogWarning("TimeMinText cannot be parsed.");
}
if (!TimeSpan.TryParse(timeMaxText, out timeMax))
{
Debug.LogWarning("TimeMaxText cannot be parsed.");
}
InitInput();
UpdateInputs();
AddListeners();
InteractableChanged();
Time = Clamp(Time);
}
///
/// Init the input.
///
protected abstract void InitInput();
///
/// Clamp the specified time.
///
/// Time.
/// Clamped time.
protected virtual TimeSpan Clamp(TimeSpan t)
{
if (t < timeMin)
{
t = timeMin;
}
if (t > timeMax)
{
t = timeMax;
}
return t;
}
///
/// Add the listeners.
///
protected abstract void AddListeners();
///
/// Removes the listeners.
///
protected abstract void RemoveListeners();
///
/// Process the destroy event.
///
protected override void OnDestroy()
{
base.OnDestroy();
RemoveListeners();
}
///
/// Updates the inputs.
///
public abstract void UpdateInputs();
#region IStylable implementation
///
public abstract bool SetStyle(Style style);
///
public abstract bool GetStyle(Style style);
#endregion
}
}