UIWidget.cs 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine;
  4. [ExecuteInEditMode]
  5. [AddComponentMenu("NGUI/UI/NGUI Widget")]
  6. public class UIWidget : UIRect
  7. {
  8. public UIDrawCall.OnRenderCallback onRender
  9. {
  10. get
  11. {
  12. return this.mOnRender;
  13. }
  14. set
  15. {
  16. if (this.mOnRender != value)
  17. {
  18. if (this.drawCall != null && this.drawCall.onRender != null && this.mOnRender != null)
  19. {
  20. UIDrawCall uidrawCall = this.drawCall;
  21. uidrawCall.onRender = (UIDrawCall.OnRenderCallback)Delegate.Remove(uidrawCall.onRender, this.mOnRender);
  22. }
  23. this.mOnRender = value;
  24. if (this.drawCall != null)
  25. {
  26. UIDrawCall uidrawCall2 = this.drawCall;
  27. uidrawCall2.onRender = (UIDrawCall.OnRenderCallback)Delegate.Combine(uidrawCall2.onRender, value);
  28. }
  29. }
  30. }
  31. }
  32. public Vector4 drawRegion
  33. {
  34. get
  35. {
  36. return this.mDrawRegion;
  37. }
  38. set
  39. {
  40. if (this.mDrawRegion != value)
  41. {
  42. this.mDrawRegion = value;
  43. if (this.autoResizeBoxCollider)
  44. {
  45. this.ResizeCollider();
  46. }
  47. this.MarkAsChanged();
  48. }
  49. }
  50. }
  51. public Vector2 pivotOffset
  52. {
  53. get
  54. {
  55. return NGUIMath.GetPivotOffset(this.pivot);
  56. }
  57. }
  58. public int width
  59. {
  60. get
  61. {
  62. return this.mWidth;
  63. }
  64. set
  65. {
  66. int minWidth = this.minWidth;
  67. if (value < minWidth)
  68. {
  69. value = minWidth;
  70. }
  71. if (this.mWidth != value && this.keepAspectRatio != UIWidget.AspectRatioSource.BasedOnHeight)
  72. {
  73. if (this.isAnchoredHorizontally)
  74. {
  75. if (this.leftAnchor.target != null && this.rightAnchor.target != null)
  76. {
  77. if (this.mPivot == UIWidget.Pivot.BottomLeft || this.mPivot == UIWidget.Pivot.Left || this.mPivot == UIWidget.Pivot.TopLeft)
  78. {
  79. NGUIMath.AdjustWidget(this, 0f, 0f, (float)(value - this.mWidth), 0f);
  80. }
  81. else if (this.mPivot == UIWidget.Pivot.BottomRight || this.mPivot == UIWidget.Pivot.Right || this.mPivot == UIWidget.Pivot.TopRight)
  82. {
  83. NGUIMath.AdjustWidget(this, (float)(this.mWidth - value), 0f, 0f, 0f);
  84. }
  85. else
  86. {
  87. int num = value - this.mWidth;
  88. num -= (num & 1);
  89. if (num != 0)
  90. {
  91. NGUIMath.AdjustWidget(this, (float)(-(float)num) * 0.5f, 0f, (float)num * 0.5f, 0f);
  92. }
  93. }
  94. }
  95. else if (this.leftAnchor.target != null)
  96. {
  97. NGUIMath.AdjustWidget(this, 0f, 0f, (float)(value - this.mWidth), 0f);
  98. }
  99. else
  100. {
  101. NGUIMath.AdjustWidget(this, (float)(this.mWidth - value), 0f, 0f, 0f);
  102. }
  103. }
  104. else
  105. {
  106. this.SetDimensions(value, this.mHeight);
  107. }
  108. }
  109. }
  110. }
  111. public int height
  112. {
  113. get
  114. {
  115. return this.mHeight;
  116. }
  117. set
  118. {
  119. int minHeight = this.minHeight;
  120. if (value < minHeight)
  121. {
  122. value = minHeight;
  123. }
  124. if (this.mHeight != value && this.keepAspectRatio != UIWidget.AspectRatioSource.BasedOnWidth)
  125. {
  126. if (this.isAnchoredVertically)
  127. {
  128. if (this.bottomAnchor.target != null && this.topAnchor.target != null)
  129. {
  130. if (this.mPivot == UIWidget.Pivot.BottomLeft || this.mPivot == UIWidget.Pivot.Bottom || this.mPivot == UIWidget.Pivot.BottomRight)
  131. {
  132. NGUIMath.AdjustWidget(this, 0f, 0f, 0f, (float)(value - this.mHeight));
  133. }
  134. else if (this.mPivot == UIWidget.Pivot.TopLeft || this.mPivot == UIWidget.Pivot.Top || this.mPivot == UIWidget.Pivot.TopRight)
  135. {
  136. NGUIMath.AdjustWidget(this, 0f, (float)(this.mHeight - value), 0f, 0f);
  137. }
  138. else
  139. {
  140. int num = value - this.mHeight;
  141. num -= (num & 1);
  142. if (num != 0)
  143. {
  144. NGUIMath.AdjustWidget(this, 0f, (float)(-(float)num) * 0.5f, 0f, (float)num * 0.5f);
  145. }
  146. }
  147. }
  148. else if (this.bottomAnchor.target != null)
  149. {
  150. NGUIMath.AdjustWidget(this, 0f, 0f, 0f, (float)(value - this.mHeight));
  151. }
  152. else
  153. {
  154. NGUIMath.AdjustWidget(this, 0f, (float)(this.mHeight - value), 0f, 0f);
  155. }
  156. }
  157. else
  158. {
  159. this.SetDimensions(this.mWidth, value);
  160. }
  161. }
  162. }
  163. }
  164. public Color color
  165. {
  166. get
  167. {
  168. return this.mColor;
  169. }
  170. set
  171. {
  172. if (this.mColor != value)
  173. {
  174. bool includeChildren = this.mColor.a != value.a;
  175. this.mColor = value;
  176. this.Invalidate(includeChildren);
  177. }
  178. }
  179. }
  180. public override float alpha
  181. {
  182. get
  183. {
  184. return this.mColor.a;
  185. }
  186. set
  187. {
  188. if (this.mColor.a != value)
  189. {
  190. this.mColor.a = value;
  191. this.Invalidate(true);
  192. }
  193. }
  194. }
  195. public bool isVisible
  196. {
  197. get
  198. {
  199. return this.mIsVisibleByPanel && this.mIsVisibleByAlpha && this.mIsInFront && this.finalAlpha > 0.001f && NGUITools.GetActive(this);
  200. }
  201. }
  202. public bool hasVertices
  203. {
  204. get
  205. {
  206. return this.geometry != null && this.geometry.hasVertices;
  207. }
  208. }
  209. public UIWidget.Pivot rawPivot
  210. {
  211. get
  212. {
  213. return this.mPivot;
  214. }
  215. set
  216. {
  217. if (this.mPivot != value)
  218. {
  219. this.mPivot = value;
  220. if (this.autoResizeBoxCollider)
  221. {
  222. this.ResizeCollider();
  223. }
  224. this.MarkAsChanged();
  225. }
  226. }
  227. }
  228. public UIWidget.Pivot pivot
  229. {
  230. get
  231. {
  232. return this.mPivot;
  233. }
  234. set
  235. {
  236. if (this.mPivot != value)
  237. {
  238. Vector3 vector = this.worldCorners[0];
  239. this.mPivot = value;
  240. this.mChanged = true;
  241. Vector3 vector2 = this.worldCorners[0];
  242. Transform cachedTransform = base.cachedTransform;
  243. Vector3 vector3 = cachedTransform.position;
  244. float z = cachedTransform.localPosition.z;
  245. vector3.x += vector.x - vector2.x;
  246. vector3.y += vector.y - vector2.y;
  247. base.cachedTransform.position = vector3;
  248. vector3 = base.cachedTransform.localPosition;
  249. vector3.x = Mathf.Round(vector3.x);
  250. vector3.y = Mathf.Round(vector3.y);
  251. vector3.z = z;
  252. base.cachedTransform.localPosition = vector3;
  253. }
  254. }
  255. }
  256. public int depth
  257. {
  258. get
  259. {
  260. return this.mDepth;
  261. }
  262. set
  263. {
  264. if (this.mDepth != value)
  265. {
  266. if (this.panel != null)
  267. {
  268. this.panel.RemoveWidget(this);
  269. }
  270. this.mDepth = value;
  271. if (this.panel != null)
  272. {
  273. this.panel.AddWidget(this);
  274. if (!Application.isPlaying)
  275. {
  276. this.panel.SortWidgets();
  277. this.panel.RebuildAllDrawCalls();
  278. }
  279. }
  280. }
  281. }
  282. }
  283. public int raycastDepth
  284. {
  285. get
  286. {
  287. if (this.panel == null)
  288. {
  289. this.CreatePanel();
  290. }
  291. return (!(this.panel != null)) ? this.mDepth : (this.mDepth + this.panel.depth * 1000);
  292. }
  293. }
  294. public override Vector3[] localCorners
  295. {
  296. get
  297. {
  298. Vector2 pivotOffset = this.pivotOffset;
  299. float num = -pivotOffset.x * (float)this.mWidth;
  300. float num2 = -pivotOffset.y * (float)this.mHeight;
  301. float x = num + (float)this.mWidth;
  302. float y = num2 + (float)this.mHeight;
  303. this.mCorners[0] = new Vector3(num, num2);
  304. this.mCorners[1] = new Vector3(num, y);
  305. this.mCorners[2] = new Vector3(x, y);
  306. this.mCorners[3] = new Vector3(x, num2);
  307. return this.mCorners;
  308. }
  309. }
  310. public virtual Vector2 localSize
  311. {
  312. get
  313. {
  314. Vector3[] localCorners = this.localCorners;
  315. return localCorners[2] - localCorners[0];
  316. }
  317. }
  318. public Vector3 localCenter
  319. {
  320. get
  321. {
  322. Vector3[] localCorners = this.localCorners;
  323. return Vector3.Lerp(localCorners[0], localCorners[2], 0.5f);
  324. }
  325. }
  326. public override Vector3[] worldCorners
  327. {
  328. get
  329. {
  330. Vector2 pivotOffset = this.pivotOffset;
  331. float num = -pivotOffset.x * (float)this.mWidth;
  332. float num2 = -pivotOffset.y * (float)this.mHeight;
  333. float x = num + (float)this.mWidth;
  334. float y = num2 + (float)this.mHeight;
  335. Transform cachedTransform = base.cachedTransform;
  336. this.mCorners[0] = cachedTransform.TransformPoint(num, num2, 0f);
  337. this.mCorners[1] = cachedTransform.TransformPoint(num, y, 0f);
  338. this.mCorners[2] = cachedTransform.TransformPoint(x, y, 0f);
  339. this.mCorners[3] = cachedTransform.TransformPoint(x, num2, 0f);
  340. return this.mCorners;
  341. }
  342. }
  343. public Vector3 worldCenter
  344. {
  345. get
  346. {
  347. return base.cachedTransform.TransformPoint(this.localCenter);
  348. }
  349. }
  350. public virtual Vector4 drawingDimensions
  351. {
  352. get
  353. {
  354. Vector2 pivotOffset = this.pivotOffset;
  355. float num = -pivotOffset.x * (float)this.mWidth;
  356. float num2 = -pivotOffset.y * (float)this.mHeight;
  357. float num3 = num + (float)this.mWidth;
  358. float num4 = num2 + (float)this.mHeight;
  359. return new Vector4((this.mDrawRegion.x != 0f) ? Mathf.Lerp(num, num3, this.mDrawRegion.x) : num, (this.mDrawRegion.y != 0f) ? Mathf.Lerp(num2, num4, this.mDrawRegion.y) : num2, (this.mDrawRegion.z != 1f) ? Mathf.Lerp(num, num3, this.mDrawRegion.z) : num3, (this.mDrawRegion.w != 1f) ? Mathf.Lerp(num2, num4, this.mDrawRegion.w) : num4);
  360. }
  361. }
  362. public virtual Material material
  363. {
  364. get
  365. {
  366. return null;
  367. }
  368. set
  369. {
  370. throw new NotImplementedException(base.GetType() + " has no material setter");
  371. }
  372. }
  373. public virtual Texture mainTexture
  374. {
  375. get
  376. {
  377. Material material = this.material;
  378. return (!(material != null)) ? null : material.mainTexture;
  379. }
  380. set
  381. {
  382. throw new NotImplementedException(base.GetType() + " has no mainTexture setter");
  383. }
  384. }
  385. public virtual Shader shader
  386. {
  387. get
  388. {
  389. Material material = this.material;
  390. return (!(material != null)) ? null : material.shader;
  391. }
  392. set
  393. {
  394. throw new NotImplementedException(base.GetType() + " has no shader setter");
  395. }
  396. }
  397. [Obsolete("There is no relative scale anymore. Widgets now have width and height instead")]
  398. public Vector2 relativeSize
  399. {
  400. get
  401. {
  402. return Vector2.one;
  403. }
  404. }
  405. public bool hasBoxCollider
  406. {
  407. get
  408. {
  409. BoxCollider x = base.GetComponent<Collider>() as BoxCollider;
  410. return x != null || base.GetComponent<BoxCollider2D>() != null;
  411. }
  412. }
  413. public void SetDimensions(int w, int h)
  414. {
  415. if (this.mWidth != w || this.mHeight != h)
  416. {
  417. this.mWidth = w;
  418. this.mHeight = h;
  419. if (this.keepAspectRatio == UIWidget.AspectRatioSource.BasedOnWidth)
  420. {
  421. this.mHeight = Mathf.RoundToInt((float)this.mWidth / this.aspectRatio);
  422. }
  423. else if (this.keepAspectRatio == UIWidget.AspectRatioSource.BasedOnHeight)
  424. {
  425. this.mWidth = Mathf.RoundToInt((float)this.mHeight * this.aspectRatio);
  426. }
  427. else if (this.keepAspectRatio == UIWidget.AspectRatioSource.Free)
  428. {
  429. this.aspectRatio = (float)this.mWidth / (float)this.mHeight;
  430. }
  431. this.mMoved = true;
  432. if (this.autoResizeBoxCollider)
  433. {
  434. this.ResizeCollider();
  435. }
  436. this.MarkAsChanged();
  437. }
  438. }
  439. public override Vector3[] GetSides(Transform relativeTo)
  440. {
  441. Vector2 pivotOffset = this.pivotOffset;
  442. float num = -pivotOffset.x * (float)this.mWidth;
  443. float num2 = -pivotOffset.y * (float)this.mHeight;
  444. float num3 = num + (float)this.mWidth;
  445. float num4 = num2 + (float)this.mHeight;
  446. float x = (num + num3) * 0.5f;
  447. float y = (num2 + num4) * 0.5f;
  448. Transform cachedTransform = base.cachedTransform;
  449. this.mCorners[0] = cachedTransform.TransformPoint(num, y, 0f);
  450. this.mCorners[1] = cachedTransform.TransformPoint(x, num4, 0f);
  451. this.mCorners[2] = cachedTransform.TransformPoint(num3, y, 0f);
  452. this.mCorners[3] = cachedTransform.TransformPoint(x, num2, 0f);
  453. if (relativeTo != null)
  454. {
  455. for (int i = 0; i < 4; i++)
  456. {
  457. this.mCorners[i] = relativeTo.InverseTransformPoint(this.mCorners[i]);
  458. }
  459. }
  460. return this.mCorners;
  461. }
  462. public override float CalculateFinalAlpha(int frameID)
  463. {
  464. if (this.mAlphaFrameID != frameID)
  465. {
  466. this.mAlphaFrameID = frameID;
  467. this.UpdateFinalAlpha(frameID);
  468. }
  469. return this.finalAlpha;
  470. }
  471. protected void UpdateFinalAlpha(int frameID)
  472. {
  473. if (!this.mIsVisibleByAlpha || !this.mIsInFront)
  474. {
  475. this.finalAlpha = 0f;
  476. }
  477. else
  478. {
  479. UIRect parent = base.parent;
  480. this.finalAlpha = ((!(base.parent != null)) ? this.mColor.a : (parent.CalculateFinalAlpha(frameID) * this.mColor.a));
  481. }
  482. }
  483. public override void Invalidate(bool includeChildren)
  484. {
  485. this.mChanged = true;
  486. this.mAlphaFrameID = -1;
  487. if (this.panel != null)
  488. {
  489. bool visibleByPanel = (!this.hideIfOffScreen && !this.panel.hasCumulativeClipping) || this.panel.IsVisible(this);
  490. this.UpdateVisibility(this.CalculateCumulativeAlpha(Time.frameCount) > 0.001f, visibleByPanel);
  491. this.UpdateFinalAlpha(Time.frameCount);
  492. if (includeChildren)
  493. {
  494. base.Invalidate(true);
  495. }
  496. }
  497. }
  498. public float CalculateCumulativeAlpha(int frameID)
  499. {
  500. UIRect parent = base.parent;
  501. return (!(parent != null)) ? this.mColor.a : (parent.CalculateFinalAlpha(frameID) * this.mColor.a);
  502. }
  503. public override void SetRect(float x, float y, float width, float height)
  504. {
  505. Vector2 pivotOffset = this.pivotOffset;
  506. float num = Mathf.Lerp(x, x + width, pivotOffset.x);
  507. float num2 = Mathf.Lerp(y, y + height, pivotOffset.y);
  508. int num3 = Mathf.FloorToInt(width + 0.5f);
  509. int num4 = Mathf.FloorToInt(height + 0.5f);
  510. if (pivotOffset.x == 0.5f)
  511. {
  512. num3 = num3 >> 1 << 1;
  513. }
  514. if (pivotOffset.y == 0.5f)
  515. {
  516. num4 = num4 >> 1 << 1;
  517. }
  518. Transform transform = base.cachedTransform;
  519. Vector3 localPosition = transform.localPosition;
  520. localPosition.x = Mathf.Floor(num + 0.5f);
  521. localPosition.y = Mathf.Floor(num2 + 0.5f);
  522. if (num3 < this.minWidth)
  523. {
  524. num3 = this.minWidth;
  525. }
  526. if (num4 < this.minHeight)
  527. {
  528. num4 = this.minHeight;
  529. }
  530. transform.localPosition = localPosition;
  531. this.width = num3;
  532. this.height = num4;
  533. if (base.isAnchored)
  534. {
  535. transform = transform.parent;
  536. if (this.leftAnchor.target)
  537. {
  538. this.leftAnchor.SetHorizontal(transform, x);
  539. }
  540. if (this.rightAnchor.target)
  541. {
  542. this.rightAnchor.SetHorizontal(transform, x + width);
  543. }
  544. if (this.bottomAnchor.target)
  545. {
  546. this.bottomAnchor.SetVertical(transform, y);
  547. }
  548. if (this.topAnchor.target)
  549. {
  550. this.topAnchor.SetVertical(transform, y + height);
  551. }
  552. }
  553. }
  554. public void ResizeCollider()
  555. {
  556. if (NGUITools.GetActive(this))
  557. {
  558. NGUITools.UpdateWidgetCollider(base.gameObject);
  559. }
  560. }
  561. [DebuggerHidden]
  562. [DebuggerStepThrough]
  563. public static int FullCompareFunc(UIWidget left, UIWidget right)
  564. {
  565. int num = UIPanel.CompareFunc(left.panel, right.panel);
  566. return (num != 0) ? num : UIWidget.PanelCompareFunc(left, right);
  567. }
  568. [DebuggerHidden]
  569. [DebuggerStepThrough]
  570. public static int PanelCompareFunc(UIWidget left, UIWidget right)
  571. {
  572. if (left.mDepth < right.mDepth)
  573. {
  574. return -1;
  575. }
  576. if (left.mDepth > right.mDepth)
  577. {
  578. return 1;
  579. }
  580. Material material = left.material;
  581. Material material2 = right.material;
  582. if (material == material2)
  583. {
  584. return 0;
  585. }
  586. if (material != null)
  587. {
  588. return -1;
  589. }
  590. if (material2 != null)
  591. {
  592. return 1;
  593. }
  594. return (material.GetInstanceID() >= material2.GetInstanceID()) ? 1 : -1;
  595. }
  596. public Bounds CalculateBounds()
  597. {
  598. return this.CalculateBounds(null);
  599. }
  600. public Bounds CalculateBounds(Transform relativeParent)
  601. {
  602. if (relativeParent == null)
  603. {
  604. Vector3[] localCorners = this.localCorners;
  605. Bounds result = new Bounds(localCorners[0], Vector3.zero);
  606. for (int i = 1; i < 4; i++)
  607. {
  608. result.Encapsulate(localCorners[i]);
  609. }
  610. return result;
  611. }
  612. Matrix4x4 worldToLocalMatrix = relativeParent.worldToLocalMatrix;
  613. Vector3[] worldCorners = this.worldCorners;
  614. Bounds result2 = new Bounds(worldToLocalMatrix.MultiplyPoint3x4(worldCorners[0]), Vector3.zero);
  615. for (int j = 1; j < 4; j++)
  616. {
  617. result2.Encapsulate(worldToLocalMatrix.MultiplyPoint3x4(worldCorners[j]));
  618. }
  619. return result2;
  620. }
  621. public void SetDirty()
  622. {
  623. if (this.drawCall != null)
  624. {
  625. this.drawCall.isDirty = true;
  626. }
  627. else if (this.isVisible && this.hasVertices)
  628. {
  629. this.CreatePanel();
  630. }
  631. }
  632. public void RemoveFromPanel()
  633. {
  634. if (this.panel != null)
  635. {
  636. this.panel.RemoveWidget(this);
  637. this.panel = null;
  638. }
  639. this.drawCall = null;
  640. }
  641. public virtual void MarkAsChanged()
  642. {
  643. if (NGUITools.GetActive(this))
  644. {
  645. this.mChanged = true;
  646. if (this.panel != null && base.enabled && NGUITools.GetActive(base.gameObject) && !this.mPlayMode)
  647. {
  648. this.SetDirty();
  649. this.CheckLayer();
  650. }
  651. }
  652. }
  653. public UIPanel CreatePanel()
  654. {
  655. if (this.mStarted && this.panel == null && base.enabled && NGUITools.GetActive(base.gameObject))
  656. {
  657. this.panel = UIPanel.Find(base.cachedTransform, true, base.cachedGameObject.layer);
  658. if (this.panel != null)
  659. {
  660. this.mParentFound = false;
  661. this.panel.AddWidget(this);
  662. this.CheckLayer();
  663. this.Invalidate(true);
  664. }
  665. }
  666. return this.panel;
  667. }
  668. public void CheckLayer()
  669. {
  670. if (this.panel != null && this.panel.gameObject.layer != base.gameObject.layer)
  671. {
  672. UnityEngine.Debug.LogWarning("You can't place widgets on a layer different than the UIPanel that manages them.\nIf you want to move widgets to a different layer, parent them to a new panel instead.", this);
  673. base.gameObject.layer = this.panel.gameObject.layer;
  674. }
  675. }
  676. public override void ParentHasChanged()
  677. {
  678. base.ParentHasChanged();
  679. if (this.panel != null)
  680. {
  681. UIPanel y = UIPanel.Find(base.cachedTransform, true, base.cachedGameObject.layer);
  682. if (this.panel != y)
  683. {
  684. this.RemoveFromPanel();
  685. this.CreatePanel();
  686. }
  687. }
  688. }
  689. protected virtual void Awake()
  690. {
  691. this.mGo = base.gameObject;
  692. this.mPlayMode = Application.isPlaying;
  693. }
  694. protected override void OnInit()
  695. {
  696. base.OnInit();
  697. this.RemoveFromPanel();
  698. this.mMoved = true;
  699. if (this.mWidth == 100 && this.mHeight == 100 && base.cachedTransform.localScale.magnitude > 8f)
  700. {
  701. this.UpgradeFrom265();
  702. base.cachedTransform.localScale = Vector3.one;
  703. }
  704. base.Update();
  705. }
  706. protected virtual void UpgradeFrom265()
  707. {
  708. Vector3 localScale = base.cachedTransform.localScale;
  709. this.mWidth = Mathf.Abs(Mathf.RoundToInt(localScale.x));
  710. this.mHeight = Mathf.Abs(Mathf.RoundToInt(localScale.y));
  711. NGUITools.UpdateWidgetCollider(base.gameObject, true);
  712. }
  713. protected override void OnStart()
  714. {
  715. this.CreatePanel();
  716. }
  717. protected override void OnAnchor()
  718. {
  719. Transform cachedTransform = base.cachedTransform;
  720. Transform parent = cachedTransform.parent;
  721. Vector3 localPosition = cachedTransform.localPosition;
  722. Vector2 pivotOffset = this.pivotOffset;
  723. float num;
  724. float num2;
  725. float num3;
  726. float num4;
  727. if (this.leftAnchor.target == this.bottomAnchor.target && this.leftAnchor.target == this.rightAnchor.target && this.leftAnchor.target == this.topAnchor.target)
  728. {
  729. Vector3[] sides = this.leftAnchor.GetSides(parent);
  730. if (sides != null)
  731. {
  732. num = NGUIMath.Lerp(sides[0].x, sides[2].x, this.leftAnchor.relative) + (float)this.leftAnchor.absolute;
  733. num2 = NGUIMath.Lerp(sides[0].x, sides[2].x, this.rightAnchor.relative) + (float)this.rightAnchor.absolute;
  734. num3 = NGUIMath.Lerp(sides[3].y, sides[1].y, this.bottomAnchor.relative) + (float)this.bottomAnchor.absolute;
  735. num4 = NGUIMath.Lerp(sides[3].y, sides[1].y, this.topAnchor.relative) + (float)this.topAnchor.absolute;
  736. this.mIsInFront = true;
  737. }
  738. else
  739. {
  740. Vector3 localPos = base.GetLocalPos(this.leftAnchor, parent);
  741. num = localPos.x + (float)this.leftAnchor.absolute;
  742. num3 = localPos.y + (float)this.bottomAnchor.absolute;
  743. num2 = localPos.x + (float)this.rightAnchor.absolute;
  744. num4 = localPos.y + (float)this.topAnchor.absolute;
  745. this.mIsInFront = (!this.hideIfOffScreen || localPos.z >= 0f);
  746. }
  747. }
  748. else
  749. {
  750. this.mIsInFront = true;
  751. if (this.leftAnchor.target)
  752. {
  753. Vector3[] sides2 = this.leftAnchor.GetSides(parent);
  754. if (sides2 != null)
  755. {
  756. num = NGUIMath.Lerp(sides2[0].x, sides2[2].x, this.leftAnchor.relative) + (float)this.leftAnchor.absolute;
  757. }
  758. else
  759. {
  760. num = base.GetLocalPos(this.leftAnchor, parent).x + (float)this.leftAnchor.absolute;
  761. }
  762. }
  763. else
  764. {
  765. num = localPosition.x - pivotOffset.x * (float)this.mWidth;
  766. }
  767. if (this.rightAnchor.target)
  768. {
  769. Vector3[] sides3 = this.rightAnchor.GetSides(parent);
  770. if (sides3 != null)
  771. {
  772. num2 = NGUIMath.Lerp(sides3[0].x, sides3[2].x, this.rightAnchor.relative) + (float)this.rightAnchor.absolute;
  773. }
  774. else
  775. {
  776. num2 = base.GetLocalPos(this.rightAnchor, parent).x + (float)this.rightAnchor.absolute;
  777. }
  778. }
  779. else
  780. {
  781. num2 = localPosition.x - pivotOffset.x * (float)this.mWidth + (float)this.mWidth;
  782. }
  783. if (this.bottomAnchor.target)
  784. {
  785. Vector3[] sides4 = this.bottomAnchor.GetSides(parent);
  786. if (sides4 != null)
  787. {
  788. num3 = NGUIMath.Lerp(sides4[3].y, sides4[1].y, this.bottomAnchor.relative) + (float)this.bottomAnchor.absolute;
  789. }
  790. else
  791. {
  792. num3 = base.GetLocalPos(this.bottomAnchor, parent).y + (float)this.bottomAnchor.absolute;
  793. }
  794. }
  795. else
  796. {
  797. num3 = localPosition.y - pivotOffset.y * (float)this.mHeight;
  798. }
  799. if (this.topAnchor.target)
  800. {
  801. Vector3[] sides5 = this.topAnchor.GetSides(parent);
  802. if (sides5 != null)
  803. {
  804. num4 = NGUIMath.Lerp(sides5[3].y, sides5[1].y, this.topAnchor.relative) + (float)this.topAnchor.absolute;
  805. }
  806. else
  807. {
  808. num4 = base.GetLocalPos(this.topAnchor, parent).y + (float)this.topAnchor.absolute;
  809. }
  810. }
  811. else
  812. {
  813. num4 = localPosition.y - pivotOffset.y * (float)this.mHeight + (float)this.mHeight;
  814. }
  815. }
  816. Vector3 vector = new Vector3(Mathf.Lerp(num, num2, pivotOffset.x), Mathf.Lerp(num3, num4, pivotOffset.y), localPosition.z);
  817. int num5 = Mathf.FloorToInt(num2 - num + 0.5f);
  818. int num6 = Mathf.FloorToInt(num4 - num3 + 0.5f);
  819. if (this.keepAspectRatio != UIWidget.AspectRatioSource.Free && this.aspectRatio != 0f)
  820. {
  821. if (this.keepAspectRatio == UIWidget.AspectRatioSource.BasedOnHeight)
  822. {
  823. num5 = Mathf.RoundToInt((float)num6 * this.aspectRatio);
  824. }
  825. else
  826. {
  827. num6 = Mathf.RoundToInt((float)num5 / this.aspectRatio);
  828. }
  829. }
  830. if (num5 < this.minWidth)
  831. {
  832. num5 = this.minWidth;
  833. }
  834. if (num6 < this.minHeight)
  835. {
  836. num6 = this.minHeight;
  837. }
  838. if (Vector3.SqrMagnitude(localPosition - vector) > 0.001f)
  839. {
  840. base.cachedTransform.localPosition = vector;
  841. if (this.mIsInFront)
  842. {
  843. this.mChanged = true;
  844. }
  845. }
  846. if (this.mWidth != num5 || this.mHeight != num6)
  847. {
  848. this.mWidth = num5;
  849. this.mHeight = num6;
  850. if (this.mIsInFront)
  851. {
  852. this.mChanged = true;
  853. }
  854. if (this.autoResizeBoxCollider)
  855. {
  856. this.ResizeCollider();
  857. }
  858. }
  859. }
  860. protected override void OnUpdate()
  861. {
  862. if (this.panel == null)
  863. {
  864. this.CreatePanel();
  865. }
  866. }
  867. private void OnApplicationPause(bool paused)
  868. {
  869. if (!paused)
  870. {
  871. this.MarkAsChanged();
  872. }
  873. }
  874. protected override void OnDisable()
  875. {
  876. this.RemoveFromPanel();
  877. base.OnDisable();
  878. }
  879. private void OnDestroy()
  880. {
  881. this.RemoveFromPanel();
  882. }
  883. public bool UpdateVisibility(bool visibleByAlpha, bool visibleByPanel)
  884. {
  885. if (this.mIsVisibleByAlpha != visibleByAlpha || this.mIsVisibleByPanel != visibleByPanel)
  886. {
  887. this.mChanged = true;
  888. this.mIsVisibleByAlpha = visibleByAlpha;
  889. this.mIsVisibleByPanel = visibleByPanel;
  890. return true;
  891. }
  892. return false;
  893. }
  894. public bool UpdateTransform(int frame)
  895. {
  896. if (!this.mMoved && !this.panel.widgetsAreStatic && base.cachedTransform.hasChanged)
  897. {
  898. this.mTrans.hasChanged = false;
  899. this.mLocalToPanel = this.panel.worldToLocal * base.cachedTransform.localToWorldMatrix;
  900. this.mMatrixFrame = frame;
  901. Vector2 pivotOffset = this.pivotOffset;
  902. float num = -pivotOffset.x * (float)this.mWidth;
  903. float num2 = -pivotOffset.y * (float)this.mHeight;
  904. float x = num + (float)this.mWidth;
  905. float y = num2 + (float)this.mHeight;
  906. Transform cachedTransform = base.cachedTransform;
  907. Vector3 vector = cachedTransform.TransformPoint(num, num2, 0f);
  908. Vector3 vector2 = cachedTransform.TransformPoint(x, y, 0f);
  909. vector = this.panel.worldToLocal.MultiplyPoint3x4(vector);
  910. vector2 = this.panel.worldToLocal.MultiplyPoint3x4(vector2);
  911. if (Vector3.SqrMagnitude(this.mOldV0 - vector) > 1E-06f || Vector3.SqrMagnitude(this.mOldV1 - vector2) > 1E-06f)
  912. {
  913. this.mMoved = true;
  914. this.mOldV0 = vector;
  915. this.mOldV1 = vector2;
  916. }
  917. }
  918. if (this.mMoved && this.onChange != null)
  919. {
  920. this.onChange();
  921. }
  922. return this.mMoved || this.mChanged;
  923. }
  924. public bool UpdateGeometry(int frame)
  925. {
  926. float num = this.CalculateFinalAlpha(frame);
  927. if (this.mIsVisibleByAlpha && this.mLastAlpha != num)
  928. {
  929. this.mChanged = true;
  930. }
  931. this.mLastAlpha = num;
  932. if (this.mChanged)
  933. {
  934. this.mChanged = false;
  935. if (this.mIsVisibleByAlpha && num > 0.001f && this.shader != null)
  936. {
  937. bool hasVertices = this.geometry.hasVertices;
  938. if (this.fillGeometry)
  939. {
  940. this.geometry.Clear();
  941. this.OnFill(this.geometry.verts, this.geometry.uvs, this.geometry.cols);
  942. }
  943. if (this.geometry.hasVertices)
  944. {
  945. if (this.mMatrixFrame != frame)
  946. {
  947. this.mLocalToPanel = this.panel.worldToLocal * base.cachedTransform.localToWorldMatrix;
  948. this.mMatrixFrame = frame;
  949. }
  950. this.geometry.ApplyTransform(this.mLocalToPanel);
  951. this.mMoved = false;
  952. return true;
  953. }
  954. return hasVertices;
  955. }
  956. else if (this.geometry.hasVertices)
  957. {
  958. if (this.fillGeometry)
  959. {
  960. this.geometry.Clear();
  961. }
  962. this.mMoved = false;
  963. return true;
  964. }
  965. }
  966. else if (this.mMoved && this.geometry.hasVertices)
  967. {
  968. if (this.mMatrixFrame != frame)
  969. {
  970. this.mLocalToPanel = this.panel.worldToLocal * base.cachedTransform.localToWorldMatrix;
  971. this.mMatrixFrame = frame;
  972. }
  973. this.geometry.ApplyTransform(this.mLocalToPanel);
  974. this.mMoved = false;
  975. return true;
  976. }
  977. this.mMoved = false;
  978. return false;
  979. }
  980. public void WriteToBuffers(BetterList<Vector3> v, BetterList<Vector2> u, BetterList<Color32> c, BetterList<Vector3> n, BetterList<Vector4> t)
  981. {
  982. this.geometry.WriteToBuffers(v, u, c, n, t);
  983. }
  984. public virtual void MakePixelPerfect()
  985. {
  986. Vector3 localPosition = base.cachedTransform.localPosition;
  987. localPosition.z = Mathf.Round(localPosition.z);
  988. localPosition.x = Mathf.Round(localPosition.x);
  989. localPosition.y = Mathf.Round(localPosition.y);
  990. base.cachedTransform.localPosition = localPosition;
  991. Vector3 localScale = base.cachedTransform.localScale;
  992. base.cachedTransform.localScale = new Vector3(Mathf.Sign(localScale.x), Mathf.Sign(localScale.y), 1f);
  993. }
  994. public virtual int minWidth
  995. {
  996. get
  997. {
  998. return 2;
  999. }
  1000. }
  1001. public virtual int minHeight
  1002. {
  1003. get
  1004. {
  1005. return 2;
  1006. }
  1007. }
  1008. public virtual Vector4 border
  1009. {
  1010. get
  1011. {
  1012. return Vector4.zero;
  1013. }
  1014. set
  1015. {
  1016. }
  1017. }
  1018. public virtual void OnFill(BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols)
  1019. {
  1020. }
  1021. [HideInInspector]
  1022. [SerializeField]
  1023. protected Color mColor = Color.white;
  1024. [HideInInspector]
  1025. [SerializeField]
  1026. protected UIWidget.Pivot mPivot = UIWidget.Pivot.Center;
  1027. [HideInInspector]
  1028. [SerializeField]
  1029. protected int mWidth = 100;
  1030. [HideInInspector]
  1031. [SerializeField]
  1032. protected int mHeight = 100;
  1033. [HideInInspector]
  1034. [SerializeField]
  1035. protected int mDepth;
  1036. public UIWidget.OnDimensionsChanged onChange;
  1037. public UIWidget.OnPostFillCallback onPostFill;
  1038. public UIDrawCall.OnRenderCallback mOnRender;
  1039. public bool autoResizeBoxCollider;
  1040. public bool hideIfOffScreen;
  1041. public UIWidget.AspectRatioSource keepAspectRatio;
  1042. public float aspectRatio = 1f;
  1043. public UIWidget.HitCheck hitCheck;
  1044. [NonSerialized]
  1045. public UIPanel panel;
  1046. [NonSerialized]
  1047. public UIGeometry geometry = new UIGeometry();
  1048. [NonSerialized]
  1049. public bool fillGeometry = true;
  1050. [NonSerialized]
  1051. protected bool mPlayMode = true;
  1052. [NonSerialized]
  1053. protected Vector4 mDrawRegion = new Vector4(0f, 0f, 1f, 1f);
  1054. [NonSerialized]
  1055. private Matrix4x4 mLocalToPanel;
  1056. [NonSerialized]
  1057. private bool mIsVisibleByAlpha = true;
  1058. [NonSerialized]
  1059. private bool mIsVisibleByPanel = true;
  1060. [NonSerialized]
  1061. private bool mIsInFront = true;
  1062. [NonSerialized]
  1063. private float mLastAlpha;
  1064. [NonSerialized]
  1065. private bool mMoved;
  1066. [NonSerialized]
  1067. public UIDrawCall drawCall;
  1068. [NonSerialized]
  1069. protected Vector3[] mCorners = new Vector3[4];
  1070. [NonSerialized]
  1071. private int mAlphaFrameID = -1;
  1072. private int mMatrixFrame = -1;
  1073. private Vector3 mOldV0;
  1074. private Vector3 mOldV1;
  1075. public enum Pivot
  1076. {
  1077. TopLeft,
  1078. Top,
  1079. TopRight,
  1080. Left,
  1081. Center,
  1082. Right,
  1083. BottomLeft,
  1084. Bottom,
  1085. BottomRight
  1086. }
  1087. public delegate void OnDimensionsChanged();
  1088. public delegate void OnPostFillCallback(UIWidget widget, int bufferOffset, BetterList<Vector3> verts, BetterList<Vector2> uvs, BetterList<Color32> cols);
  1089. public enum AspectRatioSource
  1090. {
  1091. Free,
  1092. BasedOnWidth,
  1093. BasedOnHeight
  1094. }
  1095. public delegate bool HitCheck(Vector3 worldPos);
  1096. }