using UnityEngine;

namespace mgaia.tk2d
{
    /// <summary>
    /// Behaviour utilizado para atualizar as cores de uma tk2dTextMesh em conjunto com um animator da Unity
    /// </summary>
    [RequireComponent(typeof(tk2dTextMesh))]
    [ExecuteInEditMode]
    [DisallowMultipleComponent]
    public class Tk2dTextColorAdapter : MonoBehaviour
    {
        private tk2dTextMesh mTextMesh;
        private Color mOldColor;
        private Color mInitialColor;

        private void Awake()
        {
            mTextMesh = GetComponent<tk2dTextMesh>();
            mInitialColor = mTextMesh.color;
        }

        private void OnStartAnimation()
        {
            mTextMesh.color = mInitialColor;

            UpdateColor();
        }

        private void Update()
        {
            UpdateColor();
        }

        private void UpdateColor()
        {
            if (!mOldColor.Equals(mTextMesh.SerializedColor))
            {
                mOldColor = mTextMesh.SerializedColor;

                mTextMesh.color = mTextMesh.SerializedColor;
            }
        }
    }
}