123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576 |
- using System;
- using UnityEngine;
- public abstract class UIRect : MonoBehaviour
- {
- public GameObject cachedGameObject
- {
- get
- {
- if (this.mGo == null)
- {
- this.mGo = base.gameObject;
- }
- return this.mGo;
- }
- }
- public Transform cachedTransform
- {
- get
- {
- if (this.mTrans == null)
- {
- this.mTrans = base.transform;
- }
- return this.mTrans;
- }
- }
- public Camera anchorCamera
- {
- get
- {
- if (!this.mAnchorsCached)
- {
- this.ResetAnchors();
- }
- return this.mCam;
- }
- }
- public bool isFullyAnchored
- {
- get
- {
- return this.leftAnchor.target && this.rightAnchor.target && this.topAnchor.target && this.bottomAnchor.target;
- }
- }
- public virtual bool isAnchoredHorizontally
- {
- get
- {
- return this.leftAnchor.target || this.rightAnchor.target;
- }
- }
- public virtual bool isAnchoredVertically
- {
- get
- {
- return this.bottomAnchor.target || this.topAnchor.target;
- }
- }
- public virtual bool canBeAnchored
- {
- get
- {
- return true;
- }
- }
- public UIRect parent
- {
- get
- {
- if (!this.mParentFound)
- {
- this.mParentFound = true;
- this.mParent = NGUITools.FindInParents<UIRect>(this.cachedTransform.parent);
- }
- return this.mParent;
- }
- }
- public UIRoot root
- {
- get
- {
- if (this.parent != null)
- {
- return this.mParent.root;
- }
- if (!this.mRootSet)
- {
- this.mRootSet = true;
- this.mRoot = NGUITools.FindInParents<UIRoot>(this.cachedTransform);
- }
- return this.mRoot;
- }
- }
- public bool isAnchored
- {
- get
- {
- return (this.leftAnchor.target || this.rightAnchor.target || this.topAnchor.target || this.bottomAnchor.target) && this.canBeAnchored;
- }
- }
- public abstract float alpha { get; set; }
- public abstract float CalculateFinalAlpha(int frameID);
- public abstract Vector3[] localCorners { get; }
- public abstract Vector3[] worldCorners { get; }
- protected float cameraRayDistance
- {
- get
- {
- if (this.anchorCamera == null)
- {
- return 0f;
- }
- if (!this.mCam.orthographic)
- {
- Transform cachedTransform = this.cachedTransform;
- Transform transform = this.mCam.transform;
- Plane plane = new Plane(cachedTransform.rotation * Vector3.back, cachedTransform.position);
- Ray ray = new Ray(transform.position, transform.rotation * Vector3.forward);
- float result;
- if (plane.Raycast(ray, out result))
- {
- return result;
- }
- }
- return Mathf.Lerp(this.mCam.nearClipPlane, this.mCam.farClipPlane, 0.5f);
- }
- }
- public virtual void Invalidate(bool includeChildren)
- {
- this.mChanged = true;
- if (includeChildren)
- {
- for (int i = 0; i < this.mChildren.size; i++)
- {
- this.mChildren.buffer[i].Invalidate(true);
- }
- }
- }
- public virtual Vector3[] GetSides(Transform relativeTo)
- {
- if (this.anchorCamera != null)
- {
- return this.mCam.GetSides(this.cameraRayDistance, relativeTo);
- }
- Vector3 position = this.cachedTransform.position;
- for (int i = 0; i < 4; i++)
- {
- UIRect.mSides[i] = position;
- }
- if (relativeTo != null)
- {
- for (int j = 0; j < 4; j++)
- {
- UIRect.mSides[j] = relativeTo.InverseTransformPoint(UIRect.mSides[j]);
- }
- }
- return UIRect.mSides;
- }
- protected Vector3 GetLocalPos(UIRect.AnchorPoint ac, Transform trans)
- {
- if (this.anchorCamera == null || ac.targetCam == null)
- {
- return this.cachedTransform.localPosition;
- }
- Rect rect = ac.targetCam.rect;
- Vector3 vector = ac.targetCam.WorldToViewportPoint(ac.target.position);
- Vector3 vector2 = new Vector3(vector.x * rect.width + rect.x, vector.y * rect.height + rect.y, vector.z);
- vector2 = this.mCam.ViewportToWorldPoint(vector2);
- if (trans != null)
- {
- vector2 = trans.InverseTransformPoint(vector2);
- }
- vector2.x = Mathf.Floor(vector2.x + 0.5f);
- vector2.y = Mathf.Floor(vector2.y + 0.5f);
- return vector2;
- }
- protected virtual void OnEnable()
- {
- this.mUpdateFrame = -1;
- if (this.updateAnchors == UIRect.AnchorUpdate.OnEnable)
- {
- this.mAnchorsCached = false;
- this.mUpdateAnchors = true;
- }
- if (this.mStarted)
- {
- this.OnInit();
- }
- this.mUpdateFrame = -1;
- }
- protected virtual void OnInit()
- {
- this.mChanged = true;
- this.mRootSet = false;
- this.mParentFound = false;
- if (this.parent != null)
- {
- this.mParent.mChildren.Add(this);
- }
- }
- protected virtual void OnDisable()
- {
- if (this.mParent)
- {
- this.mParent.mChildren.Remove(this);
- }
- this.mParent = null;
- this.mRoot = null;
- this.mRootSet = false;
- this.mParentFound = false;
- }
- protected void Start()
- {
- this.mStarted = true;
- this.OnInit();
- this.OnStart();
- }
- public void Update()
- {
- if (!this.mAnchorsCached)
- {
- this.ResetAnchors();
- }
- int frameCount = Time.frameCount;
- if (this.mUpdateFrame != frameCount)
- {
- if (this.updateAnchors == UIRect.AnchorUpdate.OnUpdate || this.mUpdateAnchors)
- {
- this.mUpdateFrame = frameCount;
- this.mUpdateAnchors = false;
- bool flag = false;
- if (this.leftAnchor.target)
- {
- flag = true;
- if (this.leftAnchor.rect != null && this.leftAnchor.rect.mUpdateFrame != frameCount)
- {
- this.leftAnchor.rect.Update();
- }
- }
- if (this.bottomAnchor.target)
- {
- flag = true;
- if (this.bottomAnchor.rect != null && this.bottomAnchor.rect.mUpdateFrame != frameCount)
- {
- this.bottomAnchor.rect.Update();
- }
- }
- if (this.rightAnchor.target)
- {
- flag = true;
- if (this.rightAnchor.rect != null && this.rightAnchor.rect.mUpdateFrame != frameCount)
- {
- this.rightAnchor.rect.Update();
- }
- }
- if (this.topAnchor.target)
- {
- flag = true;
- if (this.topAnchor.rect != null && this.topAnchor.rect.mUpdateFrame != frameCount)
- {
- this.topAnchor.rect.Update();
- }
- }
- if (flag)
- {
- this.OnAnchor();
- }
- }
- this.OnUpdate();
- }
- }
- public void UpdateAnchors()
- {
- if (this.isAnchored && this.updateAnchors != UIRect.AnchorUpdate.OnStart)
- {
- this.OnAnchor();
- }
- }
- protected abstract void OnAnchor();
- public void SetAnchor(Transform t)
- {
- this.leftAnchor.target = t;
- this.rightAnchor.target = t;
- this.topAnchor.target = t;
- this.bottomAnchor.target = t;
- this.ResetAnchors();
- this.UpdateAnchors();
- }
- public void SetAnchor(GameObject go)
- {
- Transform target = (!(go != null)) ? null : go.transform;
- this.leftAnchor.target = target;
- this.rightAnchor.target = target;
- this.topAnchor.target = target;
- this.bottomAnchor.target = target;
- this.ResetAnchors();
- this.UpdateAnchors();
- }
- public void SetAnchor(GameObject go, int left, int bottom, int right, int top)
- {
- Transform target = (!(go != null)) ? null : go.transform;
- this.leftAnchor.target = target;
- this.rightAnchor.target = target;
- this.topAnchor.target = target;
- this.bottomAnchor.target = target;
- this.leftAnchor.relative = 0f;
- this.rightAnchor.relative = 1f;
- this.bottomAnchor.relative = 0f;
- this.topAnchor.relative = 1f;
- this.leftAnchor.absolute = left;
- this.rightAnchor.absolute = right;
- this.bottomAnchor.absolute = bottom;
- this.topAnchor.absolute = top;
- this.ResetAnchors();
- this.UpdateAnchors();
- }
- public void ResetAnchors()
- {
- this.mAnchorsCached = true;
- this.leftAnchor.rect = ((!this.leftAnchor.target) ? null : this.leftAnchor.target.GetComponent<UIRect>());
- this.bottomAnchor.rect = ((!this.bottomAnchor.target) ? null : this.bottomAnchor.target.GetComponent<UIRect>());
- this.rightAnchor.rect = ((!this.rightAnchor.target) ? null : this.rightAnchor.target.GetComponent<UIRect>());
- this.topAnchor.rect = ((!this.topAnchor.target) ? null : this.topAnchor.target.GetComponent<UIRect>());
- this.mCam = NGUITools.FindCameraForLayer(this.cachedGameObject.layer);
- this.FindCameraFor(this.leftAnchor);
- this.FindCameraFor(this.bottomAnchor);
- this.FindCameraFor(this.rightAnchor);
- this.FindCameraFor(this.topAnchor);
- this.mUpdateAnchors = true;
- }
- public void ResetAndUpdateAnchors()
- {
- this.ResetAnchors();
- this.UpdateAnchors();
- }
- public abstract void SetRect(float x, float y, float width, float height);
- private void FindCameraFor(UIRect.AnchorPoint ap)
- {
- if (ap.target == null || ap.rect != null)
- {
- ap.targetCam = null;
- }
- else
- {
- ap.targetCam = NGUITools.FindCameraForLayer(ap.target.gameObject.layer);
- }
- }
- public virtual void ParentHasChanged()
- {
- this.mParentFound = false;
- UIRect y = NGUITools.FindInParents<UIRect>(this.cachedTransform.parent);
- if (this.mParent != y)
- {
- if (this.mParent)
- {
- this.mParent.mChildren.Remove(this);
- }
- this.mParent = y;
- if (this.mParent)
- {
- this.mParent.mChildren.Add(this);
- }
- this.mRootSet = false;
- }
- }
- protected abstract void OnStart();
- protected virtual void OnUpdate()
- {
- }
- public UIRect.AnchorPoint leftAnchor = new UIRect.AnchorPoint();
- public UIRect.AnchorPoint rightAnchor = new UIRect.AnchorPoint(1f);
- public UIRect.AnchorPoint bottomAnchor = new UIRect.AnchorPoint();
- public UIRect.AnchorPoint topAnchor = new UIRect.AnchorPoint(1f);
- public UIRect.AnchorUpdate updateAnchors = UIRect.AnchorUpdate.OnUpdate;
- protected GameObject mGo;
- protected Transform mTrans;
- protected BetterList<UIRect> mChildren = new BetterList<UIRect>();
- protected bool mChanged = true;
- protected bool mStarted;
- protected bool mParentFound;
- [NonSerialized]
- private bool mUpdateAnchors = true;
- [NonSerialized]
- private int mUpdateFrame = -1;
- [NonSerialized]
- private bool mAnchorsCached;
- [NonSerialized]
- private UIRoot mRoot;
- [NonSerialized]
- private UIRect mParent;
- [NonSerialized]
- private bool mRootSet;
- [NonSerialized]
- protected Camera mCam;
- [NonSerialized]
- public float finalAlpha = 1f;
- protected static Vector3[] mSides = new Vector3[4];
- [Serializable]
- public class AnchorPoint
- {
- public AnchorPoint()
- {
- }
- public AnchorPoint(float relative)
- {
- this.relative = relative;
- }
- public void Set(float relative, float absolute)
- {
- this.relative = relative;
- this.absolute = Mathf.FloorToInt(absolute + 0.5f);
- }
- public void Set(Transform target, float relative, float absolute)
- {
- this.target = target;
- this.relative = relative;
- this.absolute = Mathf.FloorToInt(absolute + 0.5f);
- }
- public void SetToNearest(float abs0, float abs1, float abs2)
- {
- this.SetToNearest(0f, 0.5f, 1f, abs0, abs1, abs2);
- }
- public void SetToNearest(float rel0, float rel1, float rel2, float abs0, float abs1, float abs2)
- {
- float num = Mathf.Abs(abs0);
- float num2 = Mathf.Abs(abs1);
- float num3 = Mathf.Abs(abs2);
- if (num < num2 && num < num3)
- {
- this.Set(rel0, abs0);
- }
- else if (num2 < num && num2 < num3)
- {
- this.Set(rel1, abs1);
- }
- else
- {
- this.Set(rel2, abs2);
- }
- }
- public void SetHorizontal(Transform parent, float localPos)
- {
- if (this.rect)
- {
- Vector3[] sides = this.rect.GetSides(parent);
- float num = Mathf.Lerp(sides[0].x, sides[2].x, this.relative);
- this.absolute = Mathf.FloorToInt(localPos - num + 0.5f);
- }
- else
- {
- Vector3 position = this.target.position;
- if (parent != null)
- {
- position = parent.InverseTransformPoint(position);
- }
- this.absolute = Mathf.FloorToInt(localPos - position.x + 0.5f);
- }
- }
- public void SetVertical(Transform parent, float localPos)
- {
- if (this.rect)
- {
- Vector3[] sides = this.rect.GetSides(parent);
- float num = Mathf.Lerp(sides[3].y, sides[1].y, this.relative);
- this.absolute = Mathf.FloorToInt(localPos - num + 0.5f);
- }
- else
- {
- Vector3 position = this.target.position;
- if (parent != null)
- {
- position = parent.InverseTransformPoint(position);
- }
- this.absolute = Mathf.FloorToInt(localPos - position.y + 0.5f);
- }
- }
- public Vector3[] GetSides(Transform relativeTo)
- {
- if (this.target != null)
- {
- if (this.rect != null)
- {
- return this.rect.GetSides(relativeTo);
- }
- if (this.target.GetComponent<Camera>() != null)
- {
- return this.target.GetComponent<Camera>().GetSides(relativeTo);
- }
- }
- return null;
- }
- public Transform target;
- public float relative;
- public int absolute;
- [NonSerialized]
- public UIRect rect;
- [NonSerialized]
- public Camera targetCam;
- }
- public enum AnchorUpdate
- {
- OnEnable,
- OnUpdate,
- OnStart
- }
- }
|