using Cryville.Common.Pdt; using System; using System.Collections; using UnityEngine; namespace Cryville.Crtr.Components { public abstract class SpriteBase : SkinComponent { public SpriteBase() : base() { /* SubmitProperty("bound", new Property(typeof(BoundInfo), null, v => Bound = (BoundInfo)v)); SubmitProperty("transparent", new Property(typeof(bool), () => transparent, v => transparent = (bool)v)); SubmitProperty("pivot", new Property(typeof(Vector2), () => Pivot, v => Pivot = (Vector2)v)); SubmitProperty("scale", new Property(typeof(Vector2), () => Scale, v => Scale = (Vector2)v)); SubmitProperty("ui", new Property(typeof(bool), () => UI, v => UI = (bool)v)); SubmitProperty("zindex", new Property(typeof(short), () => ZIndex, v => ZIndex = (short)v)); */ SubmitProperty("bound", new op_set_bound(this)); SubmitProperty("transparent", new PropOp.Boolean(v => transparent = v)); SubmitProperty("pivot", new PropOp.Vector2(v => Pivot = v)); SubmitProperty("scale", new PropOp.Vector2(v => Scale = v)); SubmitProperty("ui", new PropOp.Boolean(v => UI = v)); SubmitProperty("zindex", new PropOp.Integer(v => ZIndex = (short)v)); } #pragma warning disable IDE1006 class op_set_bound : PdtOperator { readonly SpriteBase _self; public op_set_bound(SpriteBase self) : base(2) { _self = self; } protected unsafe override void Execute() { _self.SetBound( *(Vector2*)GetOperand(0).TrustedAsOfLength(sizeof(Vector2)), *(Vector3*)GetOperand(1).TrustedAsOfLength(sizeof(Vector3)) ); } } #pragma warning restore IDE1006 protected MeshWrapper mesh = new MeshWrapper(); protected override void OnDestroy() { mesh.Destroy(); } Vector2 _scale = Vector2.one; public Vector2 Scale { get { return _scale; } set { _scale = value; UpdateScale(); } } protected virtual void UpdateScale() { if (!mesh.Initialized) return; var s = BaseScale; var ss = new Vector3(_scale.x, 1, _scale.y); s.Scale(ss); mesh.MeshTransform.localScale = s; OnPivotUpdate(); } protected abstract Vector3 BaseScale { get; } protected virtual Vector3 BaseSize { get { return Vector3.one; } } Vector2 _pivot; public Vector2 Pivot { get { return _pivot; } set { _pivot = value; OnPivotUpdate(); } } protected void OnPivotUpdate() { if (!mesh.Initialized) return; var r = new Vector3(_pivot.x - BasePivot.x, 0, _pivot.y - BasePivot.y); r.Scale(mesh.MeshTransform.localScale); r.Scale(BaseSize); mesh.MeshTransform.localPosition = -r; } protected virtual Vector2 BasePivot { get { return Vector2.zero; } } #if false [Obsolete] public struct BoundInfo : IConstructable { public Vector2 Pivot; public Vector3 Position; public void Load(object data, IEvaluator etor) { var d = (IList)data; Pivot = (Vector2)etor.Cast(typeof(Vector2), d[0]); Position = (Vector3)etor.Cast(typeof(Vector3), d[1]); } } [Obsolete] public BoundInfo Bound { set { var r = Quaternion.Inverse(transform.rotation); var da = value.Pivot - Pivot; var dp = r * (value.Position - transform.localPosition); if (da.x != 0) _scale.x = dp.x / da.x; if (da.y != 0) _scale.y = dp.z / da.y; } } #endif public void SetBound(Vector2 piv, Vector3 pos) { var r = Quaternion.Inverse(transform.rotation); var da = piv - Pivot; var dp = r * (pos - transform.localPosition); if (da.x != 0) _scale.x = dp.x / da.x; if (da.y != 0) _scale.y = dp.z / da.y; } short _zindex; public short ZIndex { get { return _zindex; } set { _zindex = value; UpdateZIndex(); } } protected void UpdateZIndex() { if (!mesh.Initialized) return; mesh.Renderer.sortingOrder = _zindex; } static readonly Quaternion uirot = Quaternion.Euler(new Vector3(-90, 0, 0)); bool _ui; public bool UI { get { return _ui; } set { _ui = value; if (_ui) transform.localRotation = uirot; } } public bool transparent = false; protected void InternalInit(string meshName = "quad") { mesh.Init(transform, transparent); mesh.Mesh = GenericResources.Meshes[meshName]; UpdateScale(); UpdateZIndex(); } } }