UIRect.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. using System;
  2. using UnityEngine;
  3. public abstract class UIRect : MonoBehaviour
  4. {
  5. public GameObject cachedGameObject
  6. {
  7. get
  8. {
  9. if (this.mGo == null)
  10. {
  11. this.mGo = base.gameObject;
  12. }
  13. return this.mGo;
  14. }
  15. }
  16. public Transform cachedTransform
  17. {
  18. get
  19. {
  20. if (this.mTrans == null)
  21. {
  22. this.mTrans = base.transform;
  23. }
  24. return this.mTrans;
  25. }
  26. }
  27. public Camera anchorCamera
  28. {
  29. get
  30. {
  31. if (!this.mAnchorsCached)
  32. {
  33. this.ResetAnchors();
  34. }
  35. return this.mCam;
  36. }
  37. }
  38. public bool isFullyAnchored
  39. {
  40. get
  41. {
  42. return this.leftAnchor.target && this.rightAnchor.target && this.topAnchor.target && this.bottomAnchor.target;
  43. }
  44. }
  45. public virtual bool isAnchoredHorizontally
  46. {
  47. get
  48. {
  49. return this.leftAnchor.target || this.rightAnchor.target;
  50. }
  51. }
  52. public virtual bool isAnchoredVertically
  53. {
  54. get
  55. {
  56. return this.bottomAnchor.target || this.topAnchor.target;
  57. }
  58. }
  59. public virtual bool canBeAnchored
  60. {
  61. get
  62. {
  63. return true;
  64. }
  65. }
  66. public UIRect parent
  67. {
  68. get
  69. {
  70. if (!this.mParentFound)
  71. {
  72. this.mParentFound = true;
  73. this.mParent = NGUITools.FindInParents<UIRect>(this.cachedTransform.parent);
  74. }
  75. return this.mParent;
  76. }
  77. }
  78. public UIRoot root
  79. {
  80. get
  81. {
  82. if (this.parent != null)
  83. {
  84. return this.mParent.root;
  85. }
  86. if (!this.mRootSet)
  87. {
  88. this.mRootSet = true;
  89. this.mRoot = NGUITools.FindInParents<UIRoot>(this.cachedTransform);
  90. }
  91. return this.mRoot;
  92. }
  93. }
  94. public bool isAnchored
  95. {
  96. get
  97. {
  98. return (this.leftAnchor.target || this.rightAnchor.target || this.topAnchor.target || this.bottomAnchor.target) && this.canBeAnchored;
  99. }
  100. }
  101. public abstract float alpha { get; set; }
  102. public abstract float CalculateFinalAlpha(int frameID);
  103. public abstract Vector3[] localCorners { get; }
  104. public abstract Vector3[] worldCorners { get; }
  105. protected float cameraRayDistance
  106. {
  107. get
  108. {
  109. if (this.anchorCamera == null)
  110. {
  111. return 0f;
  112. }
  113. if (!this.mCam.orthographic)
  114. {
  115. Transform cachedTransform = this.cachedTransform;
  116. Transform transform = this.mCam.transform;
  117. Plane plane = new Plane(cachedTransform.rotation * Vector3.back, cachedTransform.position);
  118. Ray ray = new Ray(transform.position, transform.rotation * Vector3.forward);
  119. float result;
  120. if (plane.Raycast(ray, out result))
  121. {
  122. return result;
  123. }
  124. }
  125. return Mathf.Lerp(this.mCam.nearClipPlane, this.mCam.farClipPlane, 0.5f);
  126. }
  127. }
  128. public virtual void Invalidate(bool includeChildren)
  129. {
  130. this.mChanged = true;
  131. if (includeChildren)
  132. {
  133. for (int i = 0; i < this.mChildren.size; i++)
  134. {
  135. this.mChildren.buffer[i].Invalidate(true);
  136. }
  137. }
  138. }
  139. public virtual Vector3[] GetSides(Transform relativeTo)
  140. {
  141. if (this.anchorCamera != null)
  142. {
  143. return this.mCam.GetSides(this.cameraRayDistance, relativeTo);
  144. }
  145. Vector3 position = this.cachedTransform.position;
  146. for (int i = 0; i < 4; i++)
  147. {
  148. UIRect.mSides[i] = position;
  149. }
  150. if (relativeTo != null)
  151. {
  152. for (int j = 0; j < 4; j++)
  153. {
  154. UIRect.mSides[j] = relativeTo.InverseTransformPoint(UIRect.mSides[j]);
  155. }
  156. }
  157. return UIRect.mSides;
  158. }
  159. protected Vector3 GetLocalPos(UIRect.AnchorPoint ac, Transform trans)
  160. {
  161. if (this.anchorCamera == null || ac.targetCam == null)
  162. {
  163. return this.cachedTransform.localPosition;
  164. }
  165. Rect rect = ac.targetCam.rect;
  166. Vector3 vector = ac.targetCam.WorldToViewportPoint(ac.target.position);
  167. Vector3 vector2 = new Vector3(vector.x * rect.width + rect.x, vector.y * rect.height + rect.y, vector.z);
  168. vector2 = this.mCam.ViewportToWorldPoint(vector2);
  169. if (trans != null)
  170. {
  171. vector2 = trans.InverseTransformPoint(vector2);
  172. }
  173. vector2.x = Mathf.Floor(vector2.x + 0.5f);
  174. vector2.y = Mathf.Floor(vector2.y + 0.5f);
  175. return vector2;
  176. }
  177. protected virtual void OnEnable()
  178. {
  179. this.mUpdateFrame = -1;
  180. if (this.updateAnchors == UIRect.AnchorUpdate.OnEnable)
  181. {
  182. this.mAnchorsCached = false;
  183. this.mUpdateAnchors = true;
  184. }
  185. if (this.mStarted)
  186. {
  187. this.OnInit();
  188. }
  189. this.mUpdateFrame = -1;
  190. }
  191. protected virtual void OnInit()
  192. {
  193. this.mChanged = true;
  194. this.mRootSet = false;
  195. this.mParentFound = false;
  196. if (this.parent != null)
  197. {
  198. this.mParent.mChildren.Add(this);
  199. }
  200. }
  201. protected virtual void OnDisable()
  202. {
  203. if (this.mParent)
  204. {
  205. this.mParent.mChildren.Remove(this);
  206. }
  207. this.mParent = null;
  208. this.mRoot = null;
  209. this.mRootSet = false;
  210. this.mParentFound = false;
  211. }
  212. protected void Start()
  213. {
  214. this.mStarted = true;
  215. this.OnInit();
  216. this.OnStart();
  217. }
  218. public void Update()
  219. {
  220. if (!this.mAnchorsCached)
  221. {
  222. this.ResetAnchors();
  223. }
  224. int frameCount = Time.frameCount;
  225. if (this.mUpdateFrame != frameCount)
  226. {
  227. if (this.updateAnchors == UIRect.AnchorUpdate.OnUpdate || this.mUpdateAnchors)
  228. {
  229. this.mUpdateFrame = frameCount;
  230. this.mUpdateAnchors = false;
  231. bool flag = false;
  232. if (this.leftAnchor.target)
  233. {
  234. flag = true;
  235. if (this.leftAnchor.rect != null && this.leftAnchor.rect.mUpdateFrame != frameCount)
  236. {
  237. this.leftAnchor.rect.Update();
  238. }
  239. }
  240. if (this.bottomAnchor.target)
  241. {
  242. flag = true;
  243. if (this.bottomAnchor.rect != null && this.bottomAnchor.rect.mUpdateFrame != frameCount)
  244. {
  245. this.bottomAnchor.rect.Update();
  246. }
  247. }
  248. if (this.rightAnchor.target)
  249. {
  250. flag = true;
  251. if (this.rightAnchor.rect != null && this.rightAnchor.rect.mUpdateFrame != frameCount)
  252. {
  253. this.rightAnchor.rect.Update();
  254. }
  255. }
  256. if (this.topAnchor.target)
  257. {
  258. flag = true;
  259. if (this.topAnchor.rect != null && this.topAnchor.rect.mUpdateFrame != frameCount)
  260. {
  261. this.topAnchor.rect.Update();
  262. }
  263. }
  264. if (flag)
  265. {
  266. this.OnAnchor();
  267. }
  268. }
  269. this.OnUpdate();
  270. }
  271. }
  272. public void UpdateAnchors()
  273. {
  274. if (this.isAnchored && this.updateAnchors != UIRect.AnchorUpdate.OnStart)
  275. {
  276. this.OnAnchor();
  277. }
  278. }
  279. protected abstract void OnAnchor();
  280. public void SetAnchor(Transform t)
  281. {
  282. this.leftAnchor.target = t;
  283. this.rightAnchor.target = t;
  284. this.topAnchor.target = t;
  285. this.bottomAnchor.target = t;
  286. this.ResetAnchors();
  287. this.UpdateAnchors();
  288. }
  289. public void SetAnchor(GameObject go)
  290. {
  291. Transform target = (!(go != null)) ? null : go.transform;
  292. this.leftAnchor.target = target;
  293. this.rightAnchor.target = target;
  294. this.topAnchor.target = target;
  295. this.bottomAnchor.target = target;
  296. this.ResetAnchors();
  297. this.UpdateAnchors();
  298. }
  299. public void SetAnchor(GameObject go, int left, int bottom, int right, int top)
  300. {
  301. Transform target = (!(go != null)) ? null : go.transform;
  302. this.leftAnchor.target = target;
  303. this.rightAnchor.target = target;
  304. this.topAnchor.target = target;
  305. this.bottomAnchor.target = target;
  306. this.leftAnchor.relative = 0f;
  307. this.rightAnchor.relative = 1f;
  308. this.bottomAnchor.relative = 0f;
  309. this.topAnchor.relative = 1f;
  310. this.leftAnchor.absolute = left;
  311. this.rightAnchor.absolute = right;
  312. this.bottomAnchor.absolute = bottom;
  313. this.topAnchor.absolute = top;
  314. this.ResetAnchors();
  315. this.UpdateAnchors();
  316. }
  317. public void ResetAnchors()
  318. {
  319. this.mAnchorsCached = true;
  320. this.leftAnchor.rect = ((!this.leftAnchor.target) ? null : this.leftAnchor.target.GetComponent<UIRect>());
  321. this.bottomAnchor.rect = ((!this.bottomAnchor.target) ? null : this.bottomAnchor.target.GetComponent<UIRect>());
  322. this.rightAnchor.rect = ((!this.rightAnchor.target) ? null : this.rightAnchor.target.GetComponent<UIRect>());
  323. this.topAnchor.rect = ((!this.topAnchor.target) ? null : this.topAnchor.target.GetComponent<UIRect>());
  324. this.mCam = NGUITools.FindCameraForLayer(this.cachedGameObject.layer);
  325. this.FindCameraFor(this.leftAnchor);
  326. this.FindCameraFor(this.bottomAnchor);
  327. this.FindCameraFor(this.rightAnchor);
  328. this.FindCameraFor(this.topAnchor);
  329. this.mUpdateAnchors = true;
  330. }
  331. public void ResetAndUpdateAnchors()
  332. {
  333. this.ResetAnchors();
  334. this.UpdateAnchors();
  335. }
  336. public abstract void SetRect(float x, float y, float width, float height);
  337. private void FindCameraFor(UIRect.AnchorPoint ap)
  338. {
  339. if (ap.target == null || ap.rect != null)
  340. {
  341. ap.targetCam = null;
  342. }
  343. else
  344. {
  345. ap.targetCam = NGUITools.FindCameraForLayer(ap.target.gameObject.layer);
  346. }
  347. }
  348. public virtual void ParentHasChanged()
  349. {
  350. this.mParentFound = false;
  351. UIRect y = NGUITools.FindInParents<UIRect>(this.cachedTransform.parent);
  352. if (this.mParent != y)
  353. {
  354. if (this.mParent)
  355. {
  356. this.mParent.mChildren.Remove(this);
  357. }
  358. this.mParent = y;
  359. if (this.mParent)
  360. {
  361. this.mParent.mChildren.Add(this);
  362. }
  363. this.mRootSet = false;
  364. }
  365. }
  366. protected abstract void OnStart();
  367. protected virtual void OnUpdate()
  368. {
  369. }
  370. public UIRect.AnchorPoint leftAnchor = new UIRect.AnchorPoint();
  371. public UIRect.AnchorPoint rightAnchor = new UIRect.AnchorPoint(1f);
  372. public UIRect.AnchorPoint bottomAnchor = new UIRect.AnchorPoint();
  373. public UIRect.AnchorPoint topAnchor = new UIRect.AnchorPoint(1f);
  374. public UIRect.AnchorUpdate updateAnchors = UIRect.AnchorUpdate.OnUpdate;
  375. protected GameObject mGo;
  376. protected Transform mTrans;
  377. protected BetterList<UIRect> mChildren = new BetterList<UIRect>();
  378. protected bool mChanged = true;
  379. protected bool mStarted;
  380. protected bool mParentFound;
  381. [NonSerialized]
  382. private bool mUpdateAnchors = true;
  383. [NonSerialized]
  384. private int mUpdateFrame = -1;
  385. [NonSerialized]
  386. private bool mAnchorsCached;
  387. [NonSerialized]
  388. private UIRoot mRoot;
  389. [NonSerialized]
  390. private UIRect mParent;
  391. [NonSerialized]
  392. private bool mRootSet;
  393. [NonSerialized]
  394. protected Camera mCam;
  395. [NonSerialized]
  396. public float finalAlpha = 1f;
  397. protected static Vector3[] mSides = new Vector3[4];
  398. [Serializable]
  399. public class AnchorPoint
  400. {
  401. public AnchorPoint()
  402. {
  403. }
  404. public AnchorPoint(float relative)
  405. {
  406. this.relative = relative;
  407. }
  408. public void Set(float relative, float absolute)
  409. {
  410. this.relative = relative;
  411. this.absolute = Mathf.FloorToInt(absolute + 0.5f);
  412. }
  413. public void Set(Transform target, float relative, float absolute)
  414. {
  415. this.target = target;
  416. this.relative = relative;
  417. this.absolute = Mathf.FloorToInt(absolute + 0.5f);
  418. }
  419. public void SetToNearest(float abs0, float abs1, float abs2)
  420. {
  421. this.SetToNearest(0f, 0.5f, 1f, abs0, abs1, abs2);
  422. }
  423. public void SetToNearest(float rel0, float rel1, float rel2, float abs0, float abs1, float abs2)
  424. {
  425. float num = Mathf.Abs(abs0);
  426. float num2 = Mathf.Abs(abs1);
  427. float num3 = Mathf.Abs(abs2);
  428. if (num < num2 && num < num3)
  429. {
  430. this.Set(rel0, abs0);
  431. }
  432. else if (num2 < num && num2 < num3)
  433. {
  434. this.Set(rel1, abs1);
  435. }
  436. else
  437. {
  438. this.Set(rel2, abs2);
  439. }
  440. }
  441. public void SetHorizontal(Transform parent, float localPos)
  442. {
  443. if (this.rect)
  444. {
  445. Vector3[] sides = this.rect.GetSides(parent);
  446. float num = Mathf.Lerp(sides[0].x, sides[2].x, this.relative);
  447. this.absolute = Mathf.FloorToInt(localPos - num + 0.5f);
  448. }
  449. else
  450. {
  451. Vector3 position = this.target.position;
  452. if (parent != null)
  453. {
  454. position = parent.InverseTransformPoint(position);
  455. }
  456. this.absolute = Mathf.FloorToInt(localPos - position.x + 0.5f);
  457. }
  458. }
  459. public void SetVertical(Transform parent, float localPos)
  460. {
  461. if (this.rect)
  462. {
  463. Vector3[] sides = this.rect.GetSides(parent);
  464. float num = Mathf.Lerp(sides[3].y, sides[1].y, this.relative);
  465. this.absolute = Mathf.FloorToInt(localPos - num + 0.5f);
  466. }
  467. else
  468. {
  469. Vector3 position = this.target.position;
  470. if (parent != null)
  471. {
  472. position = parent.InverseTransformPoint(position);
  473. }
  474. this.absolute = Mathf.FloorToInt(localPos - position.y + 0.5f);
  475. }
  476. }
  477. public Vector3[] GetSides(Transform relativeTo)
  478. {
  479. if (this.target != null)
  480. {
  481. if (this.rect != null)
  482. {
  483. return this.rect.GetSides(relativeTo);
  484. }
  485. if (this.target.GetComponent<Camera>() != null)
  486. {
  487. return this.target.GetComponent<Camera>().GetSides(relativeTo);
  488. }
  489. }
  490. return null;
  491. }
  492. public Transform target;
  493. public float relative;
  494. public int absolute;
  495. [NonSerialized]
  496. public UIRect rect;
  497. [NonSerialized]
  498. public Camera targetCam;
  499. }
  500. public enum AnchorUpdate
  501. {
  502. OnEnable,
  503. OnUpdate,
  504. OnStart
  505. }
  506. }