namespace UIWidgets
{
using System.Collections;
using UIWidgets.Attributes;
using UIWidgets.Styles;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;
using UnityEngine.UI;
///
/// Switch direction.
///
public enum SwitchDirection
{
///
/// Left to right.
///
LeftToRight = 0,
///
/// Right to left.
///
RightToLeft = 1,
///
/// Bottom to top.
///
BottomToTop = 2,
///
/// Top to bottom.
///
TopToBottom = 3,
}
///
/// Switch.
///
[DataBindSupport]
public class Switch : Selectable, ISubmitHandler, IPointerClickHandler, IStylable, IValidateable
{
///
/// Is on?
///
[SerializeField]
protected bool isOn;
///
/// Gets or sets a value indicating whether this instance is on.
///
/// true if this instance is on; otherwise, false.
[DataBindField]
public virtual bool IsOn
{
get
{
return isOn;
}
set
{
if (isOn != value)
{
isOn = value;
if (Group != null)
{
if (isOn || (!Group.AnySwitchesOn() && !Group.AllowSwitchOff))
{
isOn = true;
Group.NotifySwitchOn(this);
}
}
Changed();
}
}
}
///
/// Switch group.
///
[SerializeField]
protected SwitchGroup Group;
///
/// Gets or sets the switch group.
///
/// The switch group.
public virtual SwitchGroup SwitchGroup
{
get
{
return Group;
}
set
{
if (Group != null)
{
Group.UnregisterSwitch(this);
}
Group = value;
if (Group != null)
{
Group.RegisterSwitch(this);
if (IsOn)
{
Group.NotifySwitchOn(this);
}
}
}
}
///
/// The direction.
///
[SerializeField]
protected SwitchDirection direction = SwitchDirection.LeftToRight;
///
/// Gets or sets the direction.
///
/// The direction.
public SwitchDirection Direction
{
get
{
return direction;
}
set
{
direction = value;
SetMarkPosition(false);
}
}
///
/// The mark.
///
[SerializeField]
public RectTransform Mark;
///
/// The mark.
///
[SerializeField]
public Graphic MarkGraphic;
///
/// The background.
///
[SerializeField]
public Graphic Background;
[SerializeField]
Color markOnColor = new Color(1f, 1f, 1f, 1f);
///
/// Gets or sets the color of the mark for On state.
///
/// The color of the mark for On state.
public Color MarkOnColor
{
get
{
return markOnColor;
}
set
{
markOnColor = value;
SetMarkColor();
}
}
[SerializeField]
Color markOffColor = new Color(1f, 215f / 255f, 115f / 255f, 1f);
///
/// Gets or sets the color of the mark for Off State.
///
/// The color of the mark for Off State.
public Color MarkOffColor
{
get
{
return markOffColor;
}
set
{
markOffColor = value;
SetMarkColor();
}
}
[SerializeField]
Color backgroundOnColor = new Color(1f, 1f, 1f, 1f);
///
/// Gets or sets the color of the background for On state.
///
/// The color of the background on.
public Color BackgroundOnColor
{
get
{
return backgroundOnColor;
}
set
{
backgroundOnColor = value;
SetBackgroundColor();
}
}
[SerializeField]
Color backgroundOffColor = new Color(1f, 215f / 255f, 115f / 255f, 1f);
///
/// Gets or sets the color of the background for Off State.
///
/// The color of the background off.
public Color BackgroundOffColor
{
get
{
return backgroundOffColor;
}
set
{
backgroundOffColor = value;
SetBackgroundColor();
}
}
///
/// The duration of the animation.
///
[SerializeField]
public float AnimationDuration = 0.3f;
///
/// Use unscaled time.
///
[SerializeField]
public bool UnscaledTime;
///
/// Callback executed when the IsOn of the switch is changed.
///
[SerializeField]
[DataBindEvent("IsOn")]
public SwitchEvent OnValueChanged = new SwitchEvent();
///
/// Set IsOn without OnValueChanged invoke. Not recommended for use.
///
/// If set to true value.
/// Change state with animation.
public void SetStatus(bool value, bool animate = true)
{
if (isOn == value)
{
return;
}
isOn = value;
if (Group != null)
{
if (isOn || (!Group.AnySwitchesOn() && !Group.AllowSwitchOff))
{
isOn = true;
Group.NotifySwitchOn(this, false, animate);
}
}
SetMarkPosition(animate);
SetBackgroundColor();
SetMarkColor();
}
///
/// Awake this instance.
///
protected override void Awake()
{
base.Awake();
SwitchGroup = Group;
}
///
/// Changed this instance.
///
protected virtual void Changed()
{
SetMarkPosition();
SetBackgroundColor();
SetMarkColor();
OnValueChanged.Invoke(IsOn);
}
///
/// The current coroutine.
///
protected IEnumerator CurrentCoroutine;
///
/// Sets the mark position.
///
/// If set to true animate.
protected virtual void SetMarkPosition(bool animate = true)
{
if (CurrentCoroutine != null)
{
StopCoroutine(CurrentCoroutine);
CurrentCoroutine = null;
}
if (animate && gameObject.activeInHierarchy)
{
CurrentCoroutine = AnimateSwitch(IsOn, AnimationDuration);
StartCoroutine(CurrentCoroutine);
}
else
{
SetMarkPosition(GetPosition(IsOn));
}
}
///
/// Animates the switch.
///
/// The switch.
/// IsOn.
/// Time.
protected virtual IEnumerator AnimateSwitch(bool state, float time)
{
SetMarkPosition(GetPosition(!IsOn));
var prev_position = GetPosition(!IsOn);
var next_position = GetPosition(IsOn);
var end_time = UtilitiesTime.GetTime(UnscaledTime) + time;
while (UtilitiesTime.GetTime(UnscaledTime) <= end_time)
{
var distance = 1 - ((end_time - UtilitiesTime.GetTime(UnscaledTime)) / time);
var pos = Mathf.Lerp(prev_position, next_position, distance);
SetMarkPosition(pos);
yield return null;
}
SetMarkPosition(GetPosition(IsOn));
}
///
/// Get time.
///
/// Time.
[System.Obsolete("Use Utilities.GetTime(UnscaledTime).")]
protected float GetTime()
{
return Utilities.GetTime(UnscaledTime);
}
///
/// Sets the mark position.
///
/// Position.
protected virtual void SetMarkPosition(float position)
{
if (Mark == null)
{
return;
}
if (IsHorizontal())
{
Mark.anchorMin = new Vector2(position, Mark.anchorMin.y);
Mark.anchorMax = new Vector2(position, Mark.anchorMax.y);
Mark.pivot = new Vector2(position, Mark.pivot.y);
}
else
{
Mark.anchorMin = new Vector2(Mark.anchorMin.x, position);
Mark.anchorMax = new Vector2(Mark.anchorMax.x, position);
Mark.pivot = new Vector2(Mark.pivot.x, position);
}
}
///
/// Determines whether this instance is horizontal.
///
/// true if this instance is horizontal; otherwise, false.
protected bool IsHorizontal()
{
return Direction == SwitchDirection.LeftToRight || Direction == SwitchDirection.RightToLeft;
}
///
/// Gets the position.
///
/// The position.
/// State.
protected float GetPosition(bool state)
{
switch (Direction)
{
case SwitchDirection.LeftToRight:
case SwitchDirection.BottomToTop:
return state ? 1f : 0f;
case SwitchDirection.RightToLeft:
case SwitchDirection.TopToBottom:
return state ? 0f : 1f;
}
return 0f;
}
///
/// Sets the color of the background.
///
protected virtual void SetBackgroundColor()
{
if (Background == null)
{
return;
}
Background.color = IsOn ? BackgroundOnColor : BackgroundOffColor;
}
///
/// Sets the color of the mark.
///
protected virtual void SetMarkColor()
{
if (MarkGraphic == null)
{
return;
}
MarkGraphic.color = IsOn ? MarkOnColor : MarkOffColor;
}
///
/// Calls the changed.
///
protected virtual void CallChanged()
{
if (!IsActive() || !IsInteractable())
{
return;
}
IsOn = !IsOn;
}
///
/// Called by a BaseInputModule when an OnSubmit event occurs.
///
/// Event data.
public virtual void OnSubmit(BaseEventData eventData)
{
CallChanged();
}
///
/// Called by a BaseInputModule when an OnPointerClick event occurs.
///
/// Event data.
public virtual void OnPointerClick(PointerEventData eventData)
{
if (eventData.button != PointerEventData.InputButton.Left)
{
return;
}
CallChanged();
}
///
/// Process the dimensions changed event.
///
protected override void OnRectTransformDimensionsChange()
{
base.OnRectTransformDimensionsChange();
if (!IsActive())
{
return;
}
SetMarkPosition(false);
SetBackgroundColor();
SetMarkColor();
}
///
/// Process the enable event.
///
protected override void OnEnable()
{
base.OnEnable();
SetMarkPosition(false);
SetBackgroundColor();
SetMarkColor();
}
#if UNITY_EDITOR
///
/// Handle validate event.
///
public virtual void Validate()
{
SetMarkPosition(false);
SetBackgroundColor();
SetMarkColor();
}
#endif
#region IStylable implementation
///
public virtual bool SetStyle(Style style)
{
style.Switch.Border.ApplyTo(GetComponent());
style.Switch.BackgroundDefault.ApplyTo(Background as Image);
if (Mark != null)
{
style.Switch.MarkDefault.ApplyTo(Mark.GetComponent());
}
backgroundOnColor = style.Switch.BackgroundOnColor;
backgroundOffColor = style.Switch.BackgroundOffColor;
markOnColor = style.Switch.MarkOnColor;
markOffColor = style.Switch.MarkOffColor;
SetBackgroundColor();
SetMarkColor();
return true;
}
///
public virtual bool GetStyle(Style style)
{
style.Switch.Border.GetFrom(GetComponent());
style.Switch.BackgroundDefault.GetFrom(Background as Image);
if (Mark != null)
{
style.Switch.MarkDefault.GetFrom(Mark.GetComponent());
}
style.Switch.BackgroundOnColor = backgroundOnColor;
style.Switch.BackgroundOffColor = backgroundOffColor;
style.Switch.MarkOnColor = markOnColor;
style.Switch.MarkOffColor = markOffColor;
return true;
}
#endregion
}
}