NGUIMath.cs 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049
  1. using System;
  2. using System.Diagnostics;
  3. using UnityEngine;
  4. public static class NGUIMath
  5. {
  6. [DebuggerHidden]
  7. [DebuggerStepThrough]
  8. public static float Lerp(float from, float to, float factor)
  9. {
  10. return from * (1f - factor) + to * factor;
  11. }
  12. [DebuggerHidden]
  13. [DebuggerStepThrough]
  14. public static int ClampIndex(int val, int max)
  15. {
  16. return (val >= 0) ? ((val >= max) ? (max - 1) : val) : 0;
  17. }
  18. [DebuggerHidden]
  19. [DebuggerStepThrough]
  20. public static int RepeatIndex(int val, int max)
  21. {
  22. if (max < 1)
  23. {
  24. return 0;
  25. }
  26. while (val < 0)
  27. {
  28. val += max;
  29. }
  30. while (val >= max)
  31. {
  32. val -= max;
  33. }
  34. return val;
  35. }
  36. [DebuggerHidden]
  37. [DebuggerStepThrough]
  38. public static float WrapAngle(float angle)
  39. {
  40. while (angle > 180f)
  41. {
  42. angle -= 360f;
  43. }
  44. while (angle < -180f)
  45. {
  46. angle += 360f;
  47. }
  48. return angle;
  49. }
  50. [DebuggerHidden]
  51. [DebuggerStepThrough]
  52. public static float Wrap01(float val)
  53. {
  54. return val - (float)Mathf.FloorToInt(val);
  55. }
  56. [DebuggerHidden]
  57. [DebuggerStepThrough]
  58. public static int HexToDecimal(char ch)
  59. {
  60. switch (ch)
  61. {
  62. case '0':
  63. return 0;
  64. case '1':
  65. return 1;
  66. case '2':
  67. return 2;
  68. case '3':
  69. return 3;
  70. case '4':
  71. return 4;
  72. case '5':
  73. return 5;
  74. case '6':
  75. return 6;
  76. case '7':
  77. return 7;
  78. case '8':
  79. return 8;
  80. case '9':
  81. return 9;
  82. default:
  83. switch (ch)
  84. {
  85. case 'a':
  86. break;
  87. case 'b':
  88. return 11;
  89. case 'c':
  90. return 12;
  91. case 'd':
  92. return 13;
  93. case 'e':
  94. return 14;
  95. case 'f':
  96. return 15;
  97. default:
  98. return 15;
  99. }
  100. break;
  101. case 'A':
  102. break;
  103. case 'B':
  104. return 11;
  105. case 'C':
  106. return 12;
  107. case 'D':
  108. return 13;
  109. case 'E':
  110. return 14;
  111. case 'F':
  112. return 15;
  113. }
  114. return 10;
  115. }
  116. [DebuggerHidden]
  117. [DebuggerStepThrough]
  118. public static char DecimalToHexChar(int num)
  119. {
  120. if (num > 15)
  121. {
  122. return 'F';
  123. }
  124. if (num < 10)
  125. {
  126. return (char)(48 + num);
  127. }
  128. return (char)(65 + num - 10);
  129. }
  130. [DebuggerHidden]
  131. [DebuggerStepThrough]
  132. public static string DecimalToHex8(int num)
  133. {
  134. num &= 255;
  135. return num.ToString("X2");
  136. }
  137. [DebuggerHidden]
  138. [DebuggerStepThrough]
  139. public static string DecimalToHex24(int num)
  140. {
  141. num &= 16777215;
  142. return num.ToString("X6");
  143. }
  144. [DebuggerHidden]
  145. [DebuggerStepThrough]
  146. public static string DecimalToHex32(int num)
  147. {
  148. return num.ToString("X8");
  149. }
  150. [DebuggerHidden]
  151. [DebuggerStepThrough]
  152. public static int ColorToInt(Color c)
  153. {
  154. int num = 0;
  155. num |= Mathf.RoundToInt(c.r * 255f) << 24;
  156. num |= Mathf.RoundToInt(c.g * 255f) << 16;
  157. num |= Mathf.RoundToInt(c.b * 255f) << 8;
  158. return num | Mathf.RoundToInt(c.a * 255f);
  159. }
  160. [DebuggerHidden]
  161. [DebuggerStepThrough]
  162. public static Color IntToColor(int val)
  163. {
  164. float num = 0.003921569f;
  165. Color black = Color.black;
  166. black.r = num * (float)(val >> 24 & 255);
  167. black.g = num * (float)(val >> 16 & 255);
  168. black.b = num * (float)(val >> 8 & 255);
  169. black.a = num * (float)(val & 255);
  170. return black;
  171. }
  172. [DebuggerHidden]
  173. [DebuggerStepThrough]
  174. public static string IntToBinary(int val, int bits)
  175. {
  176. string text = string.Empty;
  177. int i = bits;
  178. while (i > 0)
  179. {
  180. if (i == 8 || i == 16 || i == 24)
  181. {
  182. text += " ";
  183. }
  184. text += (((val & 1 << --i) == 0) ? '0' : '1');
  185. }
  186. return text;
  187. }
  188. [DebuggerHidden]
  189. [DebuggerStepThrough]
  190. public static Color HexToColor(uint val)
  191. {
  192. return NGUIMath.IntToColor((int)val);
  193. }
  194. public static Rect ConvertToTexCoords(Rect rect, int width, int height)
  195. {
  196. Rect result = rect;
  197. if ((float)width != 0f && (float)height != 0f)
  198. {
  199. result.xMin = rect.xMin / (float)width;
  200. result.xMax = rect.xMax / (float)width;
  201. result.yMin = 1f - rect.yMax / (float)height;
  202. result.yMax = 1f - rect.yMin / (float)height;
  203. }
  204. return result;
  205. }
  206. public static Rect ConvertToPixels(Rect rect, int width, int height, bool round)
  207. {
  208. Rect result = rect;
  209. if (round)
  210. {
  211. result.xMin = (float)Mathf.RoundToInt(rect.xMin * (float)width);
  212. result.xMax = (float)Mathf.RoundToInt(rect.xMax * (float)width);
  213. result.yMin = (float)Mathf.RoundToInt((1f - rect.yMax) * (float)height);
  214. result.yMax = (float)Mathf.RoundToInt((1f - rect.yMin) * (float)height);
  215. }
  216. else
  217. {
  218. result.xMin = rect.xMin * (float)width;
  219. result.xMax = rect.xMax * (float)width;
  220. result.yMin = (1f - rect.yMax) * (float)height;
  221. result.yMax = (1f - rect.yMin) * (float)height;
  222. }
  223. return result;
  224. }
  225. public static Rect MakePixelPerfect(Rect rect)
  226. {
  227. rect.xMin = (float)Mathf.RoundToInt(rect.xMin);
  228. rect.yMin = (float)Mathf.RoundToInt(rect.yMin);
  229. rect.xMax = (float)Mathf.RoundToInt(rect.xMax);
  230. rect.yMax = (float)Mathf.RoundToInt(rect.yMax);
  231. return rect;
  232. }
  233. public static Rect MakePixelPerfect(Rect rect, int width, int height)
  234. {
  235. rect = NGUIMath.ConvertToPixels(rect, width, height, true);
  236. rect.xMin = (float)Mathf.RoundToInt(rect.xMin);
  237. rect.yMin = (float)Mathf.RoundToInt(rect.yMin);
  238. rect.xMax = (float)Mathf.RoundToInt(rect.xMax);
  239. rect.yMax = (float)Mathf.RoundToInt(rect.yMax);
  240. return NGUIMath.ConvertToTexCoords(rect, width, height);
  241. }
  242. public static Vector2 ConstrainRect(Vector2 minRect, Vector2 maxRect, Vector2 minArea, Vector2 maxArea)
  243. {
  244. Vector2 zero = Vector2.zero;
  245. float num = maxRect.x - minRect.x;
  246. float num2 = maxRect.y - minRect.y;
  247. float num3 = maxArea.x - minArea.x;
  248. float num4 = maxArea.y - minArea.y;
  249. if (num > num3)
  250. {
  251. float num5 = num - num3;
  252. minArea.x -= num5;
  253. maxArea.x += num5;
  254. }
  255. if (num2 > num4)
  256. {
  257. float num6 = num2 - num4;
  258. minArea.y -= num6;
  259. maxArea.y += num6;
  260. }
  261. if (minRect.x < minArea.x)
  262. {
  263. zero.x += minArea.x - minRect.x;
  264. }
  265. if (maxRect.x > maxArea.x)
  266. {
  267. zero.x -= maxRect.x - maxArea.x;
  268. }
  269. if (minRect.y < minArea.y)
  270. {
  271. zero.y += minArea.y - minRect.y;
  272. }
  273. if (maxRect.y > maxArea.y)
  274. {
  275. zero.y -= maxRect.y - maxArea.y;
  276. }
  277. return zero;
  278. }
  279. public static Bounds CalculateAbsoluteWidgetBounds(Transform trans)
  280. {
  281. if (!(trans != null))
  282. {
  283. return new Bounds(Vector3.zero, Vector3.zero);
  284. }
  285. UIWidget[] componentsInChildren = trans.GetComponentsInChildren<UIWidget>();
  286. if (componentsInChildren.Length == 0)
  287. {
  288. return new Bounds(trans.position, Vector3.zero);
  289. }
  290. Vector3 center = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  291. Vector3 point = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  292. int i = 0;
  293. int num = componentsInChildren.Length;
  294. while (i < num)
  295. {
  296. UIWidget uiwidget = componentsInChildren[i];
  297. if (uiwidget.enabled)
  298. {
  299. Vector3[] worldCorners = uiwidget.worldCorners;
  300. for (int j = 0; j < 4; j++)
  301. {
  302. Vector3 vector = worldCorners[j];
  303. if (vector.x > point.x)
  304. {
  305. point.x = vector.x;
  306. }
  307. if (vector.y > point.y)
  308. {
  309. point.y = vector.y;
  310. }
  311. if (vector.z > point.z)
  312. {
  313. point.z = vector.z;
  314. }
  315. if (vector.x < center.x)
  316. {
  317. center.x = vector.x;
  318. }
  319. if (vector.y < center.y)
  320. {
  321. center.y = vector.y;
  322. }
  323. if (vector.z < center.z)
  324. {
  325. center.z = vector.z;
  326. }
  327. }
  328. }
  329. i++;
  330. }
  331. Bounds result = new Bounds(center, Vector3.zero);
  332. result.Encapsulate(point);
  333. return result;
  334. }
  335. public static Bounds CalculateRelativeWidgetBounds(Transform trans)
  336. {
  337. return NGUIMath.CalculateRelativeWidgetBounds(trans, trans, false, true);
  338. }
  339. public static Bounds CalculateRelativeWidgetBounds(Transform trans, bool considerInactive)
  340. {
  341. return NGUIMath.CalculateRelativeWidgetBounds(trans, trans, considerInactive, true);
  342. }
  343. public static Bounds CalculateRelativeWidgetBounds(Transform relativeTo, Transform content)
  344. {
  345. return NGUIMath.CalculateRelativeWidgetBounds(relativeTo, content, false, true);
  346. }
  347. public static Bounds CalculateRelativeWidgetBounds(Transform relativeTo, Transform content, bool considerInactive, bool considerParents = true)
  348. {
  349. if (content != null && relativeTo != null)
  350. {
  351. bool flag = false;
  352. Matrix4x4 worldToLocalMatrix = relativeTo.worldToLocalMatrix;
  353. Vector3 center = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
  354. Vector3 point = new Vector3(float.MinValue, float.MinValue, float.MinValue);
  355. NGUIMath.CalculateRelativeWidgetBounds(content, considerInactive, true, ref worldToLocalMatrix, ref center, ref point, ref flag, considerParents);
  356. if (flag)
  357. {
  358. Bounds result = new Bounds(center, Vector3.zero);
  359. result.Encapsulate(point);
  360. return result;
  361. }
  362. }
  363. return new Bounds(Vector3.zero, Vector3.zero);
  364. }
  365. [DebuggerHidden]
  366. [DebuggerStepThrough]
  367. private static void CalculateRelativeWidgetBounds(Transform content, bool considerInactive, bool isRoot, ref Matrix4x4 toLocal, ref Vector3 vMin, ref Vector3 vMax, ref bool isSet, bool considerParents)
  368. {
  369. if (content == null)
  370. {
  371. return;
  372. }
  373. if (!considerInactive && !NGUITools.GetActive(content.gameObject))
  374. {
  375. return;
  376. }
  377. UIPanel uipanel = (!isRoot) ? content.GetComponent<UIPanel>() : null;
  378. if (uipanel != null && !uipanel.enabled)
  379. {
  380. return;
  381. }
  382. if (uipanel != null && uipanel.clipping != UIDrawCall.Clipping.None)
  383. {
  384. Vector3[] worldCorners = uipanel.worldCorners;
  385. for (int i = 0; i < 4; i++)
  386. {
  387. Vector3 vector = toLocal.MultiplyPoint3x4(worldCorners[i]);
  388. if (vector.x > vMax.x)
  389. {
  390. vMax.x = vector.x;
  391. }
  392. if (vector.y > vMax.y)
  393. {
  394. vMax.y = vector.y;
  395. }
  396. if (vector.z > vMax.z)
  397. {
  398. vMax.z = vector.z;
  399. }
  400. if (vector.x < vMin.x)
  401. {
  402. vMin.x = vector.x;
  403. }
  404. if (vector.y < vMin.y)
  405. {
  406. vMin.y = vector.y;
  407. }
  408. if (vector.z < vMin.z)
  409. {
  410. vMin.z = vector.z;
  411. }
  412. isSet = true;
  413. }
  414. }
  415. else
  416. {
  417. UIWidget component = content.GetComponent<UIWidget>();
  418. if (component != null && component.enabled)
  419. {
  420. Vector3[] worldCorners2 = component.worldCorners;
  421. for (int j = 0; j < 4; j++)
  422. {
  423. Vector3 vector2 = toLocal.MultiplyPoint3x4(worldCorners2[j]);
  424. if (vector2.x > vMax.x)
  425. {
  426. vMax.x = vector2.x;
  427. }
  428. if (vector2.y > vMax.y)
  429. {
  430. vMax.y = vector2.y;
  431. }
  432. if (vector2.z > vMax.z)
  433. {
  434. vMax.z = vector2.z;
  435. }
  436. if (vector2.x < vMin.x)
  437. {
  438. vMin.x = vector2.x;
  439. }
  440. if (vector2.y < vMin.y)
  441. {
  442. vMin.y = vector2.y;
  443. }
  444. if (vector2.z < vMin.z)
  445. {
  446. vMin.z = vector2.z;
  447. }
  448. isSet = true;
  449. }
  450. if (!considerParents)
  451. {
  452. return;
  453. }
  454. }
  455. int k = 0;
  456. int childCount = content.childCount;
  457. while (k < childCount)
  458. {
  459. NGUIMath.CalculateRelativeWidgetBounds(content.GetChild(k), considerInactive, false, ref toLocal, ref vMin, ref vMax, ref isSet, true);
  460. k++;
  461. }
  462. }
  463. }
  464. public static Vector3 SpringDampen(ref Vector3 velocity, float strength, float deltaTime)
  465. {
  466. if (deltaTime > 1f)
  467. {
  468. deltaTime = 1f;
  469. }
  470. float f = 1f - strength * 0.001f;
  471. int num = Mathf.RoundToInt(deltaTime * 1000f);
  472. float num2 = Mathf.Pow(f, (float)num);
  473. Vector3 a = velocity * ((num2 - 1f) / Mathf.Log(f));
  474. velocity *= num2;
  475. return a * 0.06f;
  476. }
  477. public static Vector2 SpringDampen(ref Vector2 velocity, float strength, float deltaTime)
  478. {
  479. if (deltaTime > 1f)
  480. {
  481. deltaTime = 1f;
  482. }
  483. float f = 1f - strength * 0.001f;
  484. int num = Mathf.RoundToInt(deltaTime * 1000f);
  485. float num2 = Mathf.Pow(f, (float)num);
  486. Vector2 a = velocity * ((num2 - 1f) / Mathf.Log(f));
  487. velocity *= num2;
  488. return a * 0.06f;
  489. }
  490. public static float SpringLerp(float strength, float deltaTime)
  491. {
  492. if (deltaTime > 1f)
  493. {
  494. deltaTime = 1f;
  495. }
  496. int num = Mathf.RoundToInt(deltaTime * 1000f);
  497. deltaTime = 0.001f * strength;
  498. float num2 = 0f;
  499. for (int i = 0; i < num; i++)
  500. {
  501. num2 = Mathf.Lerp(num2, 1f, deltaTime);
  502. }
  503. return num2;
  504. }
  505. public static float SpringLerp(float from, float to, float strength, float deltaTime)
  506. {
  507. if (deltaTime > 1f)
  508. {
  509. deltaTime = 1f;
  510. }
  511. int num = Mathf.RoundToInt(deltaTime * 1000f);
  512. deltaTime = 0.001f * strength;
  513. for (int i = 0; i < num; i++)
  514. {
  515. from = Mathf.Lerp(from, to, deltaTime);
  516. }
  517. return from;
  518. }
  519. public static Vector2 SpringLerp(Vector2 from, Vector2 to, float strength, float deltaTime)
  520. {
  521. return Vector2.Lerp(from, to, NGUIMath.SpringLerp(strength, deltaTime));
  522. }
  523. public static Vector3 SpringLerp(Vector3 from, Vector3 to, float strength, float deltaTime)
  524. {
  525. return Vector3.Lerp(from, to, NGUIMath.SpringLerp(strength, deltaTime));
  526. }
  527. public static Quaternion SpringLerp(Quaternion from, Quaternion to, float strength, float deltaTime)
  528. {
  529. return Quaternion.Slerp(from, to, NGUIMath.SpringLerp(strength, deltaTime));
  530. }
  531. public static float RotateTowards(float from, float to, float maxAngle)
  532. {
  533. float num = NGUIMath.WrapAngle(to - from);
  534. if (Mathf.Abs(num) > maxAngle)
  535. {
  536. num = maxAngle * Mathf.Sign(num);
  537. }
  538. return from + num;
  539. }
  540. private static float DistancePointToLineSegment(Vector2 point, Vector2 a, Vector2 b)
  541. {
  542. float sqrMagnitude = (b - a).sqrMagnitude;
  543. if (sqrMagnitude == 0f)
  544. {
  545. return (point - a).magnitude;
  546. }
  547. float num = Vector2.Dot(point - a, b - a) / sqrMagnitude;
  548. if (num < 0f)
  549. {
  550. return (point - a).magnitude;
  551. }
  552. if (num > 1f)
  553. {
  554. return (point - b).magnitude;
  555. }
  556. Vector2 b2 = a + num * (b - a);
  557. return (point - b2).magnitude;
  558. }
  559. public static float DistanceToRectangle(Vector2[] screenPoints, Vector2 mousePos)
  560. {
  561. bool flag = false;
  562. int val = 4;
  563. for (int i = 0; i < 5; i++)
  564. {
  565. Vector3 vector = screenPoints[NGUIMath.RepeatIndex(i, 4)];
  566. Vector3 vector2 = screenPoints[NGUIMath.RepeatIndex(val, 4)];
  567. if (vector.y > mousePos.y != vector2.y > mousePos.y && mousePos.x < (vector2.x - vector.x) * (mousePos.y - vector.y) / (vector2.y - vector.y) + vector.x)
  568. {
  569. flag = !flag;
  570. }
  571. val = i;
  572. }
  573. if (!flag)
  574. {
  575. float num = -1f;
  576. for (int j = 0; j < 4; j++)
  577. {
  578. Vector3 v = screenPoints[j];
  579. Vector3 v2 = screenPoints[NGUIMath.RepeatIndex(j + 1, 4)];
  580. float num2 = NGUIMath.DistancePointToLineSegment(mousePos, v, v2);
  581. if (num2 < num || num < 0f)
  582. {
  583. num = num2;
  584. }
  585. }
  586. return num;
  587. }
  588. return 0f;
  589. }
  590. public static float DistanceToRectangle(Vector3[] worldPoints, Vector2 mousePos, Camera cam)
  591. {
  592. Vector2[] array = new Vector2[4];
  593. for (int i = 0; i < 4; i++)
  594. {
  595. array[i] = cam.WorldToScreenPoint(worldPoints[i]);
  596. }
  597. return NGUIMath.DistanceToRectangle(array, mousePos);
  598. }
  599. public static Vector2 GetPivotOffset(UIWidget.Pivot pv)
  600. {
  601. Vector2 zero = Vector2.zero;
  602. if (pv == UIWidget.Pivot.Top || pv == UIWidget.Pivot.Center || pv == UIWidget.Pivot.Bottom)
  603. {
  604. zero.x = 0.5f;
  605. }
  606. else if (pv == UIWidget.Pivot.TopRight || pv == UIWidget.Pivot.Right || pv == UIWidget.Pivot.BottomRight)
  607. {
  608. zero.x = 1f;
  609. }
  610. else
  611. {
  612. zero.x = 0f;
  613. }
  614. if (pv == UIWidget.Pivot.Left || pv == UIWidget.Pivot.Center || pv == UIWidget.Pivot.Right)
  615. {
  616. zero.y = 0.5f;
  617. }
  618. else if (pv == UIWidget.Pivot.TopLeft || pv == UIWidget.Pivot.Top || pv == UIWidget.Pivot.TopRight)
  619. {
  620. zero.y = 1f;
  621. }
  622. else
  623. {
  624. zero.y = 0f;
  625. }
  626. return zero;
  627. }
  628. public static UIWidget.Pivot GetPivot(Vector2 offset)
  629. {
  630. if (offset.x == 0f)
  631. {
  632. if (offset.y == 0f)
  633. {
  634. return UIWidget.Pivot.BottomLeft;
  635. }
  636. if (offset.y == 1f)
  637. {
  638. return UIWidget.Pivot.TopLeft;
  639. }
  640. return UIWidget.Pivot.Left;
  641. }
  642. else if (offset.x == 1f)
  643. {
  644. if (offset.y == 0f)
  645. {
  646. return UIWidget.Pivot.BottomRight;
  647. }
  648. if (offset.y == 1f)
  649. {
  650. return UIWidget.Pivot.TopRight;
  651. }
  652. return UIWidget.Pivot.Right;
  653. }
  654. else
  655. {
  656. if (offset.y == 0f)
  657. {
  658. return UIWidget.Pivot.Bottom;
  659. }
  660. if (offset.y == 1f)
  661. {
  662. return UIWidget.Pivot.Top;
  663. }
  664. return UIWidget.Pivot.Center;
  665. }
  666. }
  667. public static void MoveWidget(UIRect w, float x, float y)
  668. {
  669. NGUIMath.MoveRect(w, x, y);
  670. }
  671. public static void MoveRect(UIRect rect, float x, float y)
  672. {
  673. int num = Mathf.FloorToInt(x + 0.5f);
  674. int num2 = Mathf.FloorToInt(y + 0.5f);
  675. Transform cachedTransform = rect.cachedTransform;
  676. cachedTransform.localPosition += new Vector3((float)num, (float)num2);
  677. int num3 = 0;
  678. if (rect.leftAnchor.target)
  679. {
  680. num3++;
  681. rect.leftAnchor.absolute += num;
  682. }
  683. if (rect.rightAnchor.target)
  684. {
  685. num3++;
  686. rect.rightAnchor.absolute += num;
  687. }
  688. if (rect.bottomAnchor.target)
  689. {
  690. num3++;
  691. rect.bottomAnchor.absolute += num2;
  692. }
  693. if (rect.topAnchor.target)
  694. {
  695. num3++;
  696. rect.topAnchor.absolute += num2;
  697. }
  698. if (num3 != 0)
  699. {
  700. rect.UpdateAnchors();
  701. }
  702. }
  703. public static void ResizeWidget(UIWidget w, UIWidget.Pivot pivot, float x, float y, int minWidth, int minHeight)
  704. {
  705. NGUIMath.ResizeWidget(w, pivot, x, y, 2, 2, 100000, 100000);
  706. }
  707. public static void ResizeWidget(UIWidget w, UIWidget.Pivot pivot, float x, float y, int minWidth, int minHeight, int maxWidth, int maxHeight)
  708. {
  709. if (pivot == UIWidget.Pivot.Center)
  710. {
  711. int num = Mathf.RoundToInt(x - (float)w.width);
  712. int num2 = Mathf.RoundToInt(y - (float)w.height);
  713. num -= (num & 1);
  714. num2 -= (num2 & 1);
  715. if ((num | num2) != 0)
  716. {
  717. num >>= 1;
  718. num2 >>= 1;
  719. NGUIMath.AdjustWidget(w, (float)(-(float)num), (float)(-(float)num2), (float)num, (float)num2, minWidth, minHeight);
  720. }
  721. return;
  722. }
  723. Vector3 point = new Vector3(x, y);
  724. point = Quaternion.Inverse(w.cachedTransform.localRotation) * point;
  725. switch (pivot)
  726. {
  727. case UIWidget.Pivot.TopLeft:
  728. NGUIMath.AdjustWidget(w, point.x, 0f, 0f, point.y, minWidth, minHeight, maxWidth, maxHeight);
  729. break;
  730. case UIWidget.Pivot.Top:
  731. NGUIMath.AdjustWidget(w, 0f, 0f, 0f, point.y, minWidth, minHeight, maxWidth, maxHeight);
  732. break;
  733. case UIWidget.Pivot.TopRight:
  734. NGUIMath.AdjustWidget(w, 0f, 0f, point.x, point.y, minWidth, minHeight, maxWidth, maxHeight);
  735. break;
  736. case UIWidget.Pivot.Left:
  737. NGUIMath.AdjustWidget(w, point.x, 0f, 0f, 0f, minWidth, minHeight, maxWidth, maxHeight);
  738. break;
  739. case UIWidget.Pivot.Right:
  740. NGUIMath.AdjustWidget(w, 0f, 0f, point.x, 0f, minWidth, minHeight, maxWidth, maxHeight);
  741. break;
  742. case UIWidget.Pivot.BottomLeft:
  743. NGUIMath.AdjustWidget(w, point.x, point.y, 0f, 0f, minWidth, minHeight, maxWidth, maxHeight);
  744. break;
  745. case UIWidget.Pivot.Bottom:
  746. NGUIMath.AdjustWidget(w, 0f, point.y, 0f, 0f, minWidth, minHeight, maxWidth, maxHeight);
  747. break;
  748. case UIWidget.Pivot.BottomRight:
  749. NGUIMath.AdjustWidget(w, 0f, point.y, point.x, 0f, minWidth, minHeight, maxWidth, maxHeight);
  750. break;
  751. }
  752. }
  753. public static void AdjustWidget(UIWidget w, float left, float bottom, float right, float top)
  754. {
  755. NGUIMath.AdjustWidget(w, left, bottom, right, top, 2, 2, 100000, 100000);
  756. }
  757. public static void AdjustWidget(UIWidget w, float left, float bottom, float right, float top, int minWidth, int minHeight)
  758. {
  759. NGUIMath.AdjustWidget(w, left, bottom, right, top, minWidth, minHeight, 100000, 100000);
  760. }
  761. public static void AdjustWidget(UIWidget w, float left, float bottom, float right, float top, int minWidth, int minHeight, int maxWidth, int maxHeight)
  762. {
  763. Vector2 pivotOffset = w.pivotOffset;
  764. Transform transform = w.cachedTransform;
  765. Quaternion localRotation = transform.localRotation;
  766. int num = Mathf.FloorToInt(left + 0.5f);
  767. int num2 = Mathf.FloorToInt(bottom + 0.5f);
  768. int num3 = Mathf.FloorToInt(right + 0.5f);
  769. int num4 = Mathf.FloorToInt(top + 0.5f);
  770. if (pivotOffset.x == 0.5f && (num == 0 || num3 == 0))
  771. {
  772. num = num >> 1 << 1;
  773. num3 = num3 >> 1 << 1;
  774. }
  775. if (pivotOffset.y == 0.5f && (num2 == 0 || num4 == 0))
  776. {
  777. num2 = num2 >> 1 << 1;
  778. num4 = num4 >> 1 << 1;
  779. }
  780. Vector3 vector = localRotation * new Vector3((float)num, (float)num4);
  781. Vector3 vector2 = localRotation * new Vector3((float)num3, (float)num4);
  782. Vector3 vector3 = localRotation * new Vector3((float)num, (float)num2);
  783. Vector3 vector4 = localRotation * new Vector3((float)num3, (float)num2);
  784. Vector3 vector5 = localRotation * new Vector3((float)num, 0f);
  785. Vector3 vector6 = localRotation * new Vector3((float)num3, 0f);
  786. Vector3 vector7 = localRotation * new Vector3(0f, (float)num4);
  787. Vector3 vector8 = localRotation * new Vector3(0f, (float)num2);
  788. Vector3 zero = Vector3.zero;
  789. if (pivotOffset.x == 0f && pivotOffset.y == 1f)
  790. {
  791. zero.x = vector.x;
  792. zero.y = vector.y;
  793. }
  794. else if (pivotOffset.x == 1f && pivotOffset.y == 0f)
  795. {
  796. zero.x = vector4.x;
  797. zero.y = vector4.y;
  798. }
  799. else if (pivotOffset.x == 0f && pivotOffset.y == 0f)
  800. {
  801. zero.x = vector3.x;
  802. zero.y = vector3.y;
  803. }
  804. else if (pivotOffset.x == 1f && pivotOffset.y == 1f)
  805. {
  806. zero.x = vector2.x;
  807. zero.y = vector2.y;
  808. }
  809. else if (pivotOffset.x == 0f && pivotOffset.y == 0.5f)
  810. {
  811. zero.x = vector5.x + (vector7.x + vector8.x) * 0.5f;
  812. zero.y = vector5.y + (vector7.y + vector8.y) * 0.5f;
  813. }
  814. else if (pivotOffset.x == 1f && pivotOffset.y == 0.5f)
  815. {
  816. zero.x = vector6.x + (vector7.x + vector8.x) * 0.5f;
  817. zero.y = vector6.y + (vector7.y + vector8.y) * 0.5f;
  818. }
  819. else if (pivotOffset.x == 0.5f && pivotOffset.y == 1f)
  820. {
  821. zero.x = vector7.x + (vector5.x + vector6.x) * 0.5f;
  822. zero.y = vector7.y + (vector5.y + vector6.y) * 0.5f;
  823. }
  824. else if (pivotOffset.x == 0.5f && pivotOffset.y == 0f)
  825. {
  826. zero.x = vector8.x + (vector5.x + vector6.x) * 0.5f;
  827. zero.y = vector8.y + (vector5.y + vector6.y) * 0.5f;
  828. }
  829. else if (pivotOffset.x == 0.5f && pivotOffset.y == 0.5f)
  830. {
  831. zero.x = (vector5.x + vector6.x + vector7.x + vector8.x) * 0.5f;
  832. zero.y = (vector7.y + vector8.y + vector5.y + vector6.y) * 0.5f;
  833. }
  834. minWidth = Mathf.Max(minWidth, w.minWidth);
  835. minHeight = Mathf.Max(minHeight, w.minHeight);
  836. int num5 = w.width + num3 - num;
  837. int num6 = w.height + num4 - num2;
  838. Vector3 zero2 = Vector3.zero;
  839. int num7 = num5;
  840. if (num5 < minWidth)
  841. {
  842. num7 = minWidth;
  843. }
  844. else if (num5 > maxWidth)
  845. {
  846. num7 = maxWidth;
  847. }
  848. if (num5 != num7)
  849. {
  850. if (num != 0)
  851. {
  852. zero2.x -= Mathf.Lerp((float)(num7 - num5), 0f, pivotOffset.x);
  853. }
  854. else
  855. {
  856. zero2.x += Mathf.Lerp(0f, (float)(num7 - num5), pivotOffset.x);
  857. }
  858. num5 = num7;
  859. }
  860. int num8 = num6;
  861. if (num6 < minHeight)
  862. {
  863. num8 = minHeight;
  864. }
  865. else if (num6 > maxHeight)
  866. {
  867. num8 = maxHeight;
  868. }
  869. if (num6 != num8)
  870. {
  871. if (num2 != 0)
  872. {
  873. zero2.y -= Mathf.Lerp((float)(num8 - num6), 0f, pivotOffset.y);
  874. }
  875. else
  876. {
  877. zero2.y += Mathf.Lerp(0f, (float)(num8 - num6), pivotOffset.y);
  878. }
  879. num6 = num8;
  880. }
  881. if (pivotOffset.x == 0.5f)
  882. {
  883. num5 = num5 >> 1 << 1;
  884. }
  885. if (pivotOffset.y == 0.5f)
  886. {
  887. num6 = num6 >> 1 << 1;
  888. }
  889. Vector3 localPosition = transform.localPosition + zero + localRotation * zero2;
  890. transform.localPosition = localPosition;
  891. w.SetDimensions(num5, num6);
  892. if (w.isAnchored)
  893. {
  894. transform = transform.parent;
  895. float num9 = localPosition.x - pivotOffset.x * (float)num5;
  896. float num10 = localPosition.y - pivotOffset.y * (float)num6;
  897. if (w.leftAnchor.target)
  898. {
  899. w.leftAnchor.SetHorizontal(transform, num9);
  900. }
  901. if (w.rightAnchor.target)
  902. {
  903. w.rightAnchor.SetHorizontal(transform, num9 + (float)num5);
  904. }
  905. if (w.bottomAnchor.target)
  906. {
  907. w.bottomAnchor.SetVertical(transform, num10);
  908. }
  909. if (w.topAnchor.target)
  910. {
  911. w.topAnchor.SetVertical(transform, num10 + (float)num6);
  912. }
  913. }
  914. }
  915. public static int AdjustByDPI(float height)
  916. {
  917. float num = Screen.dpi;
  918. RuntimePlatform platform = Application.platform;
  919. if (num == 0f)
  920. {
  921. num = ((platform != RuntimePlatform.Android && platform != RuntimePlatform.IPhonePlayer) ? 96f : 160f);
  922. }
  923. int num2 = Mathf.RoundToInt(height * (96f / num));
  924. if ((num2 & 1) == 1)
  925. {
  926. num2++;
  927. }
  928. return num2;
  929. }
  930. public static Vector2 ScreenToPixels(Vector2 pos, Transform relativeTo)
  931. {
  932. int layer = relativeTo.gameObject.layer;
  933. Camera camera = NGUITools.FindCameraForLayer(layer);
  934. if (camera == null)
  935. {
  936. UnityEngine.Debug.LogWarning("No camera found for layer " + layer);
  937. return pos;
  938. }
  939. Vector3 position = camera.ScreenToWorldPoint(pos);
  940. return relativeTo.InverseTransformPoint(position);
  941. }
  942. public static Vector2 ScreenToParentPixels(Vector2 pos, Transform relativeTo)
  943. {
  944. int layer = relativeTo.gameObject.layer;
  945. if (relativeTo.parent != null)
  946. {
  947. relativeTo = relativeTo.parent;
  948. }
  949. Camera camera = NGUITools.FindCameraForLayer(layer);
  950. if (camera == null)
  951. {
  952. UnityEngine.Debug.LogWarning("No camera found for layer " + layer);
  953. return pos;
  954. }
  955. Vector3 vector = camera.ScreenToWorldPoint(pos);
  956. return (!(relativeTo != null)) ? vector : relativeTo.InverseTransformPoint(vector);
  957. }
  958. public static Vector3 WorldToLocalPoint(Vector3 worldPos, Camera worldCam, Camera uiCam, Transform relativeTo)
  959. {
  960. worldPos = worldCam.WorldToViewportPoint(worldPos);
  961. worldPos = uiCam.ViewportToWorldPoint(worldPos);
  962. if (relativeTo == null)
  963. {
  964. return worldPos;
  965. }
  966. relativeTo = relativeTo.parent;
  967. if (relativeTo == null)
  968. {
  969. return worldPos;
  970. }
  971. return relativeTo.InverseTransformPoint(worldPos);
  972. }
  973. public static void OverlayPosition(this Transform trans, Vector3 worldPos, Camera worldCam, Camera myCam)
  974. {
  975. worldPos = worldCam.WorldToViewportPoint(worldPos);
  976. worldPos = myCam.ViewportToWorldPoint(worldPos);
  977. Transform parent = trans.parent;
  978. trans.localPosition = ((!(parent != null)) ? worldPos : parent.InverseTransformPoint(worldPos));
  979. }
  980. public static void OverlayPosition(this Transform trans, Vector3 worldPos, Camera worldCam)
  981. {
  982. Camera camera = NGUITools.FindCameraForLayer(trans.gameObject.layer);
  983. if (camera != null)
  984. {
  985. trans.OverlayPosition(worldPos, worldCam, camera);
  986. }
  987. }
  988. public static void OverlayPosition(this Transform trans, Transform target)
  989. {
  990. Camera camera = NGUITools.FindCameraForLayer(trans.gameObject.layer);
  991. Camera camera2 = NGUITools.FindCameraForLayer(target.gameObject.layer);
  992. if (camera != null && camera2 != null)
  993. {
  994. trans.OverlayPosition(target.position, camera2, camera);
  995. }
  996. }
  997. }