using UnityEngine; using UnityEngine.UI; using System; using System.Collections.Generic; namespace Coffee.UIEffects { public class GraphicConnector { private static readonly List s_Connectors = new List(); private static readonly Dictionary s_ConnectorMap = new Dictionary(); private static readonly GraphicConnector s_EmptyConnector = new GraphicConnector(); #if UNITY_EDITOR [UnityEditor.InitializeOnLoadMethod] #endif [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] private static void Init() { AddConnector(new GraphicConnector()); } protected static void AddConnector(GraphicConnector connector) { s_Connectors.Add(connector); s_Connectors.Sort((x, y) => y.priority - x.priority); } public static GraphicConnector FindConnector(Graphic graphic) { if (!graphic) return s_EmptyConnector; var type = graphic.GetType(); GraphicConnector connector = null; if (s_ConnectorMap.TryGetValue(type, out connector)) return connector; foreach (var c in s_Connectors) { if (!c.IsValid(graphic)) continue; s_ConnectorMap.Add(type, c); return c; } return s_EmptyConnector; } /// /// Connector priority. /// protected virtual int priority { get { return -1; } } /// /// Extra channel. /// public virtual AdditionalCanvasShaderChannels extraChannel { get { return AdditionalCanvasShaderChannels.TexCoord1; } } /// /// The connector is valid for the component. /// protected virtual bool IsValid(Graphic graphic) { return true; } /// /// Find effect shader. /// public virtual Shader FindShader(string shaderName) { return Shader.Find("Hidden/" + shaderName); } /// /// This function is called when the object becomes enabled and active. /// public virtual void OnEnable(Graphic graphic) { } /// /// This function is called when the behaviour becomes disabled () or inactive. /// public virtual void OnDisable(Graphic graphic) { } /// /// Mark the vertices as dirty. /// public virtual void SetVerticesDirty(Graphic graphic) { if (graphic) graphic.SetVerticesDirty(); } /// /// Mark the material as dirty. /// public virtual void SetMaterialDirty(Graphic graphic) { if (graphic) graphic.SetMaterialDirty(); } /// /// Gets position factor for area. /// public virtual void GetPositionFactor(EffectArea area, int index, Rect rect, Vector2 position, out float x, out float y) { if (area == EffectArea.Fit) { x = Mathf.Clamp01((position.x - rect.xMin) / rect.width); y = Mathf.Clamp01((position.y - rect.yMin) / rect.height); } else { x = Mathf.Clamp01(position.x / rect.width + 0.5f); y = Mathf.Clamp01(position.y / rect.height + 0.5f); } } public virtual bool IsText(Graphic graphic) { return graphic && graphic is Text; } public virtual void SetExtraChannel(ref UIVertex vertex, Vector2 value) { vertex.uv1 = value; } /// /// Normalize vertex position by local matrix. /// public virtual void GetNormalizedFactor(EffectArea area, int index, Matrix2x3 matrix, Vector2 position, out Vector2 normalizedPos) { normalizedPos = matrix * position; } } }