2019-09-30 16:55:33 -04:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using UnityEngine;
|
|
|
|
using UnityEngine.Serialization;
|
|
|
|
|
|
|
|
namespace UnityAtoms
|
|
|
|
{
|
2019-10-15 16:21:56 -04:00
|
|
|
/// <summary>
|
|
|
|
/// None generic base class for Variables. Inherits from `BaseAtom`.
|
|
|
|
/// </summary>
|
2019-10-14 10:51:54 -04:00
|
|
|
[EditorIcon("atom-icon-teal")]
|
2019-10-14 20:16:11 -04:00
|
|
|
public abstract class AtomBaseVariable : BaseAtom
|
2019-09-30 16:55:33 -04:00
|
|
|
{
|
2019-10-15 16:21:56 -04:00
|
|
|
/// <summary>
|
|
|
|
/// The Variable value as an `object`.abstract Beware of boxing! 🥊
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The Variable value as an `object`.</value>
|
2019-09-30 16:55:33 -04:00
|
|
|
public abstract object BaseValue { get; set; }
|
2019-10-15 16:21:56 -04:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Abstract method that could be implemented to reset the Variable value.
|
|
|
|
/// </summary>
|
2019-10-14 10:39:22 -04:00
|
|
|
public abstract void Reset(bool shouldTriggerEvents = false);
|
2019-09-30 16:55:33 -04:00
|
|
|
}
|
|
|
|
|
2019-10-15 16:21:56 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Generic base class for Variables. Inherits from `AtomBaseVariable`.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The Variable value type.</typeparam>
|
2019-10-14 10:51:54 -04:00
|
|
|
[EditorIcon("atom-icon-teal")]
|
2019-09-30 16:55:33 -04:00
|
|
|
public abstract class AtomBaseVariable<T> : AtomBaseVariable
|
|
|
|
{
|
2019-10-15 16:21:56 -04:00
|
|
|
/// <summary>
|
|
|
|
/// The Variable value as an `object`.abstract Beware of boxing! 🥊
|
|
|
|
/// </summary>
|
|
|
|
/// <value>The Variable value as an `object`.</value>
|
2019-09-30 16:55:33 -04:00
|
|
|
public override object BaseValue
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _value;
|
|
|
|
}
|
|
|
|
set
|
|
|
|
{
|
|
|
|
Value = (T)value;
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 16:21:56 -04:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The Variable value as a property.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns>Get or set the Variable value.</returns>
|
2019-09-30 16:55:33 -04:00
|
|
|
public virtual T Value { get { return _value; } set { throw new NotImplementedException(); } }
|
|
|
|
|
|
|
|
[FormerlySerializedAs("value")]
|
|
|
|
[SerializeField]
|
|
|
|
protected T _value;
|
|
|
|
|
|
|
|
protected bool Equals(AtomBaseVariable<T> other)
|
|
|
|
{
|
|
|
|
return EqualityComparer<T>.Default.Equals(_value, other._value);
|
|
|
|
}
|
|
|
|
|
2019-10-15 16:21:56 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Determines equality between Variables.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="obj">The other Variable to compare as an `object`.</param>
|
|
|
|
/// <returns>`true` if they are equal, otherwise `false`.</returns>
|
2019-09-30 16:55:33 -04:00
|
|
|
public override bool Equals(object obj)
|
|
|
|
{
|
|
|
|
if (ReferenceEquals(null, obj)) return false;
|
|
|
|
if (ReferenceEquals(this, obj)) return true;
|
|
|
|
if (obj.GetType() != GetType()) return false;
|
|
|
|
return Equals((AtomBaseVariable<T>)obj);
|
|
|
|
}
|
|
|
|
|
2019-10-15 16:21:56 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Get an unique hash code for this Variable based on the Variable's value.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
2019-09-30 16:55:33 -04:00
|
|
|
public override int GetHashCode()
|
|
|
|
{
|
|
|
|
unchecked
|
|
|
|
{
|
|
|
|
return EqualityComparer<T>.Default.GetHashCode(_value);
|
|
|
|
}
|
|
|
|
}
|
2019-10-15 16:21:56 -04:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Equal operator.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="left">The first Variable to compare.</param>
|
|
|
|
/// <param name="right">The second Variable to compare.</param>
|
|
|
|
/// <returns>`true` if eqaul, otherwise `false`.</returns>
|
2019-09-30 16:55:33 -04:00
|
|
|
public static bool operator ==(AtomBaseVariable<T> left, AtomBaseVariable<T> right) { return Equals(left, right); }
|
2019-10-15 16:21:56 -04:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// None equality operator.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="left">The first Variable to compare.</param>
|
|
|
|
/// <param name="right">The second Variable to compare.</param>
|
|
|
|
/// <returns>`true` if not eqaul, otherwise `false`.</returns>
|
2019-09-30 16:55:33 -04:00
|
|
|
public static bool operator !=(AtomBaseVariable<T> left, AtomBaseVariable<T> right) { return !Equals(left, right); }
|
|
|
|
|
2019-10-15 16:21:56 -04:00
|
|
|
/// <summary>
|
|
|
|
/// Not implemented.abstract Throws Exception
|
|
|
|
/// </summary>
|
2019-10-14 10:39:22 -04:00
|
|
|
public override void Reset(bool shouldTriggerEvents = false)
|
2019-09-30 16:55:33 -04:00
|
|
|
{
|
|
|
|
throw new NotImplementedException();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|