using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
namespace Coffee.UIEffects
{
///
/// Base class for effects that modify the generated Mesh.
/// It works well not only for standard Graphic components (Image, RawImage, Text, etc.) but also for TextMeshPro and TextMeshProUGUI.
///
[RequireComponent(typeof(Graphic))]
[RequireComponent(typeof(RectTransform))]
[ExecuteInEditMode]
public abstract class BaseMeshEffect : UIBehaviour, IMeshModifier
{
RectTransform _rectTransform;
Graphic _graphic;
GraphicConnector _connector;
///
/// The Graphic attached to this GameObject.
///
protected GraphicConnector connector
{
get { return _connector ?? (_connector = GraphicConnector.FindConnector(graphic)); }
}
///
/// The Graphic attached to this GameObject.
///
public Graphic graphic
{
get { return _graphic ? _graphic : _graphic = GetComponent(); }
}
///
/// The RectTransform attached to this GameObject.
///
protected RectTransform rectTransform
{
get { return _rectTransform ? _rectTransform : _rectTransform = GetComponent(); }
}
internal readonly List syncEffects = new List(0);
///
/// Call used to modify mesh. (legacy)
///
/// Mesh.
public virtual void ModifyMesh(Mesh mesh)
{
}
///
/// Call used to modify mesh.
///
/// VertexHelper.
public virtual void ModifyMesh(VertexHelper vh)
{
ModifyMesh(vh, graphic);
}
public virtual void ModifyMesh(VertexHelper vh, Graphic graphic)
{
}
///
/// Mark the vertices as dirty.
///
protected virtual void SetVerticesDirty()
{
connector.SetVerticesDirty(graphic);
foreach (var effect in syncEffects)
{
effect.SetVerticesDirty();
}
// #if TMP_PRESENT
// if (textMeshPro)
// {
// foreach (var info in textMeshPro.textInfo.meshInfo)
// {
// var mesh = info.mesh;
// if (mesh)
// {
// mesh.Clear();
// mesh.vertices = info.vertices;
// mesh.uv = info.uvs0;
// mesh.uv2 = info.uvs2;
// mesh.colors32 = info.colors32;
// mesh.normals = info.normals;
// mesh.tangents = info.tangents;
// mesh.triangles = info.triangles;
// }
// }
//
// if (canvasRenderer)
// {
// canvasRenderer.SetMesh(textMeshPro.mesh);
//
// GetComponentsInChildren(false, s_SubMeshUIs);
// foreach (var sm in s_SubMeshUIs)
// {
// sm.canvasRenderer.SetMesh(sm.mesh);
// }
//
// s_SubMeshUIs.Clear();
// }
//
// textMeshPro.havePropertiesChanged = true;
// }
// else
// #endif
// if (graphic)
// {
// graphic.SetVerticesDirty();
// }
}
//################################
// Protected Members.
//################################
///
/// Should the effect modify the mesh directly for TMPro?
///
// protected virtual bool isLegacyMeshModifier
// {
// get { return false; }
// }
// protected virtual void Initialize()
// {
// if (_initialized) return;
//
// _initialized = true;
// _graphic = _graphic ? _graphic : GetComponent();
//
// _connector = GraphicConnector.FindConnector(_graphic);
//
// // _canvasRenderer = _canvasRenderer ?? GetComponent ();
// _rectTransform = _rectTransform ? _rectTransform : GetComponent();
// // #if TMP_PRESENT
// // _textMeshPro = _textMeshPro ?? GetComponent ();
// // #endif
// }
///
/// This function is called when the object becomes enabled and active.
///
protected override void OnEnable()
{
connector.OnEnable(graphic);
SetVerticesDirty();
// SetVerticesDirty();
// #if TMP_PRESENT
// if (textMeshPro)
// {
// TMPro_EventManager.TEXT_CHANGED_EVENT.Add (OnTextChanged);
// }
// #endif
//
// #if UNITY_EDITOR && TMP_PRESENT
// if (graphic && textMeshPro)
// {
// GraphicRebuildTracker.TrackGraphic (graphic);
// }
// #endif
//
// #if UNITY_5_6_OR_NEWER
// if (graphic)
// {
// AdditionalCanvasShaderChannels channels = requiredChannels;
// var canvas = graphic.canvas;
// if (canvas && (canvas.additionalShaderChannels & channels) != channels)
// {
// Debug.LogWarningFormat (this, "Enable {1} of Canvas.additionalShaderChannels to use {0}.", GetType ().Name, channels);
// }
// }
// #endif
}
///
/// This function is called when the behaviour becomes disabled () or inactive.
///
protected override void OnDisable()
{
connector.OnDisable(graphic);
SetVerticesDirty();
}
///
/// Mark the effect parameters as dirty.
///
protected virtual void SetEffectParamsDirty()
{
if (!isActiveAndEnabled) return;
SetVerticesDirty();
}
///
/// Callback for when properties have been changed by animation.
///
protected override void OnDidApplyAnimationProperties()
{
if (!isActiveAndEnabled) return;
SetEffectParamsDirty();
}
#if UNITY_EDITOR
protected override void Reset()
{
if (!isActiveAndEnabled) return;
SetVerticesDirty();
}
///
/// This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).
///
protected override void OnValidate()
{
if (!isActiveAndEnabled) return;
SetEffectParamsDirty();
}
#endif
}
}