UIFont.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. [ExecuteInEditMode]
  5. [AddComponentMenu("NGUI/UI/NGUI Font")]
  6. public class UIFont : MonoBehaviour
  7. {
  8. public BMFont bmFont
  9. {
  10. get
  11. {
  12. return (!(this.mReplacement != null)) ? this.mFont : this.mReplacement.bmFont;
  13. }
  14. set
  15. {
  16. if (this.mReplacement != null)
  17. {
  18. this.mReplacement.bmFont = value;
  19. }
  20. else
  21. {
  22. this.mFont = value;
  23. }
  24. }
  25. }
  26. public int texWidth
  27. {
  28. get
  29. {
  30. return (!(this.mReplacement != null)) ? ((this.mFont == null) ? 1 : this.mFont.texWidth) : this.mReplacement.texWidth;
  31. }
  32. set
  33. {
  34. if (this.mReplacement != null)
  35. {
  36. this.mReplacement.texWidth = value;
  37. }
  38. else if (this.mFont != null)
  39. {
  40. this.mFont.texWidth = value;
  41. }
  42. }
  43. }
  44. public int texHeight
  45. {
  46. get
  47. {
  48. return (!(this.mReplacement != null)) ? ((this.mFont == null) ? 1 : this.mFont.texHeight) : this.mReplacement.texHeight;
  49. }
  50. set
  51. {
  52. if (this.mReplacement != null)
  53. {
  54. this.mReplacement.texHeight = value;
  55. }
  56. else if (this.mFont != null)
  57. {
  58. this.mFont.texHeight = value;
  59. }
  60. }
  61. }
  62. public bool hasSymbols
  63. {
  64. get
  65. {
  66. return (!(this.mReplacement != null)) ? (this.mSymbols != null && this.mSymbols.Count != 0) : this.mReplacement.hasSymbols;
  67. }
  68. }
  69. public List<BMSymbol> symbols
  70. {
  71. get
  72. {
  73. return (!(this.mReplacement != null)) ? this.mSymbols : this.mReplacement.symbols;
  74. }
  75. }
  76. public UIAtlas atlas
  77. {
  78. get
  79. {
  80. return (!(this.mReplacement != null)) ? this.mAtlas : this.mReplacement.atlas;
  81. }
  82. set
  83. {
  84. if (this.mReplacement != null)
  85. {
  86. this.mReplacement.atlas = value;
  87. }
  88. else if (this.mAtlas != value)
  89. {
  90. if (value == null)
  91. {
  92. if (this.mAtlas != null)
  93. {
  94. this.mMat = this.mAtlas.spriteMaterial;
  95. }
  96. if (this.sprite != null)
  97. {
  98. this.mUVRect = this.uvRect;
  99. }
  100. }
  101. this.mPMA = -1;
  102. this.mAtlas = value;
  103. this.MarkAsChanged();
  104. }
  105. }
  106. }
  107. public Material material
  108. {
  109. get
  110. {
  111. if (this.mReplacement != null)
  112. {
  113. return this.mReplacement.material;
  114. }
  115. if (this.mAtlas != null)
  116. {
  117. return this.mAtlas.spriteMaterial;
  118. }
  119. if (this.mMat != null)
  120. {
  121. if (this.mDynamicFont != null && this.mMat != this.mDynamicFont.material)
  122. {
  123. this.mMat.mainTexture = this.mDynamicFont.material.mainTexture;
  124. }
  125. return this.mMat;
  126. }
  127. if (this.mDynamicFont != null)
  128. {
  129. return this.mDynamicFont.material;
  130. }
  131. return null;
  132. }
  133. set
  134. {
  135. if (this.mReplacement != null)
  136. {
  137. this.mReplacement.material = value;
  138. }
  139. else if (this.mMat != value)
  140. {
  141. this.mPMA = -1;
  142. this.mMat = value;
  143. this.MarkAsChanged();
  144. }
  145. }
  146. }
  147. [Obsolete("Use UIFont.premultipliedAlphaShader instead")]
  148. public bool premultipliedAlpha
  149. {
  150. get
  151. {
  152. return this.premultipliedAlphaShader;
  153. }
  154. }
  155. public bool premultipliedAlphaShader
  156. {
  157. get
  158. {
  159. if (this.mReplacement != null)
  160. {
  161. return this.mReplacement.premultipliedAlphaShader;
  162. }
  163. if (this.mAtlas != null)
  164. {
  165. return this.mAtlas.premultipliedAlpha;
  166. }
  167. if (this.mPMA == -1)
  168. {
  169. Material material = this.material;
  170. this.mPMA = ((!(material != null) || !(material.shader != null) || !material.shader.name.Contains("Premultiplied")) ? 0 : 1);
  171. }
  172. return this.mPMA == 1;
  173. }
  174. }
  175. public bool packedFontShader
  176. {
  177. get
  178. {
  179. if (this.mReplacement != null)
  180. {
  181. return this.mReplacement.packedFontShader;
  182. }
  183. if (this.mAtlas != null)
  184. {
  185. return false;
  186. }
  187. if (this.mPacked == -1)
  188. {
  189. Material material = this.material;
  190. this.mPacked = ((!(material != null) || !(material.shader != null) || !material.shader.name.Contains("Packed")) ? 0 : 1);
  191. }
  192. return this.mPacked == 1;
  193. }
  194. }
  195. public Texture2D texture
  196. {
  197. get
  198. {
  199. if (this.mReplacement != null)
  200. {
  201. return this.mReplacement.texture;
  202. }
  203. Material material = this.material;
  204. return (!(material != null)) ? null : (material.mainTexture as Texture2D);
  205. }
  206. }
  207. public Rect uvRect
  208. {
  209. get
  210. {
  211. if (this.mReplacement != null)
  212. {
  213. return this.mReplacement.uvRect;
  214. }
  215. return (!(this.mAtlas != null) || this.sprite == null) ? new Rect(0f, 0f, 1f, 1f) : this.mUVRect;
  216. }
  217. set
  218. {
  219. if (this.mReplacement != null)
  220. {
  221. this.mReplacement.uvRect = value;
  222. }
  223. else if (this.sprite == null && this.mUVRect != value)
  224. {
  225. this.mUVRect = value;
  226. this.MarkAsChanged();
  227. }
  228. }
  229. }
  230. public string spriteName
  231. {
  232. get
  233. {
  234. return (!(this.mReplacement != null)) ? this.mFont.spriteName : this.mReplacement.spriteName;
  235. }
  236. set
  237. {
  238. if (this.mReplacement != null)
  239. {
  240. this.mReplacement.spriteName = value;
  241. }
  242. else if (this.mFont.spriteName != value)
  243. {
  244. this.mFont.spriteName = value;
  245. this.MarkAsChanged();
  246. }
  247. }
  248. }
  249. public bool isValid
  250. {
  251. get
  252. {
  253. return this.mDynamicFont != null || this.mFont.isValid;
  254. }
  255. }
  256. [Obsolete("Use UIFont.defaultSize instead")]
  257. public int size
  258. {
  259. get
  260. {
  261. return this.defaultSize;
  262. }
  263. set
  264. {
  265. this.defaultSize = value;
  266. }
  267. }
  268. public int defaultSize
  269. {
  270. get
  271. {
  272. if (this.mReplacement != null)
  273. {
  274. return this.mReplacement.defaultSize;
  275. }
  276. if (this.isDynamic || this.mFont == null)
  277. {
  278. return this.mDynamicFontSize;
  279. }
  280. return this.mFont.charSize;
  281. }
  282. set
  283. {
  284. if (this.mReplacement != null)
  285. {
  286. this.mReplacement.defaultSize = value;
  287. }
  288. else
  289. {
  290. this.mDynamicFontSize = value;
  291. }
  292. }
  293. }
  294. public UISpriteData sprite
  295. {
  296. get
  297. {
  298. if (this.mReplacement != null)
  299. {
  300. return this.mReplacement.sprite;
  301. }
  302. if (this.mSprite == null && this.mAtlas != null && !string.IsNullOrEmpty(this.mFont.spriteName))
  303. {
  304. this.mSprite = this.mAtlas.GetSprite(this.mFont.spriteName);
  305. if (this.mSprite == null)
  306. {
  307. this.mSprite = this.mAtlas.GetSprite(base.name);
  308. }
  309. if (this.mSprite == null)
  310. {
  311. this.mFont.spriteName = null;
  312. }
  313. else
  314. {
  315. this.UpdateUVRect();
  316. }
  317. int i = 0;
  318. int count = this.mSymbols.Count;
  319. while (i < count)
  320. {
  321. this.symbols[i].MarkAsChanged();
  322. i++;
  323. }
  324. }
  325. return this.mSprite;
  326. }
  327. }
  328. public UIFont replacement
  329. {
  330. get
  331. {
  332. return this.mReplacement;
  333. }
  334. set
  335. {
  336. UIFont uifont = value;
  337. if (uifont == this)
  338. {
  339. uifont = null;
  340. }
  341. if (this.mReplacement != uifont)
  342. {
  343. if (uifont != null && uifont.replacement == this)
  344. {
  345. uifont.replacement = null;
  346. }
  347. if (this.mReplacement != null)
  348. {
  349. this.MarkAsChanged();
  350. }
  351. this.mReplacement = uifont;
  352. if (uifont != null)
  353. {
  354. this.mPMA = -1;
  355. this.mMat = null;
  356. this.mFont = null;
  357. this.mDynamicFont = null;
  358. }
  359. this.MarkAsChanged();
  360. }
  361. }
  362. }
  363. public bool isDynamic
  364. {
  365. get
  366. {
  367. return (!(this.mReplacement != null)) ? (this.mDynamicFont != null) : this.mReplacement.isDynamic;
  368. }
  369. }
  370. public Font dynamicFont
  371. {
  372. get
  373. {
  374. return (!(this.mReplacement != null)) ? this.mDynamicFont : this.mReplacement.dynamicFont;
  375. }
  376. set
  377. {
  378. if (this.mReplacement != null)
  379. {
  380. this.mReplacement.dynamicFont = value;
  381. }
  382. else if (this.mDynamicFont != value)
  383. {
  384. if (this.mDynamicFont != null)
  385. {
  386. this.material = null;
  387. }
  388. this.mDynamicFont = value;
  389. this.MarkAsChanged();
  390. }
  391. }
  392. }
  393. public FontStyle dynamicFontStyle
  394. {
  395. get
  396. {
  397. return (!(this.mReplacement != null)) ? this.mDynamicFontStyle : this.mReplacement.dynamicFontStyle;
  398. }
  399. set
  400. {
  401. if (this.mReplacement != null)
  402. {
  403. this.mReplacement.dynamicFontStyle = value;
  404. }
  405. else if (this.mDynamicFontStyle != value)
  406. {
  407. this.mDynamicFontStyle = value;
  408. this.MarkAsChanged();
  409. }
  410. }
  411. }
  412. private void Trim()
  413. {
  414. Texture texture = this.mAtlas.texture;
  415. if (texture != null && this.mSprite != null)
  416. {
  417. Rect rect = NGUIMath.ConvertToPixels(this.mUVRect, this.texture.width, this.texture.height, true);
  418. Rect rect2 = new Rect((float)this.mSprite.x, (float)this.mSprite.y, (float)this.mSprite.width, (float)this.mSprite.height);
  419. int xMin = Mathf.RoundToInt(rect2.xMin - rect.xMin);
  420. int yMin = Mathf.RoundToInt(rect2.yMin - rect.yMin);
  421. int xMax = Mathf.RoundToInt(rect2.xMax - rect.xMin);
  422. int yMax = Mathf.RoundToInt(rect2.yMax - rect.yMin);
  423. this.mFont.Trim(xMin, yMin, xMax, yMax);
  424. }
  425. }
  426. private bool References(UIFont font)
  427. {
  428. return !(font == null) && (font == this || (this.mReplacement != null && this.mReplacement.References(font)));
  429. }
  430. public static bool CheckIfRelated(UIFont a, UIFont b)
  431. {
  432. return !(a == null) && !(b == null) && ((a.isDynamic && b.isDynamic && a.dynamicFont.fontNames[0] == b.dynamicFont.fontNames[0]) || a == b || a.References(b) || b.References(a));
  433. }
  434. private Texture dynamicTexture
  435. {
  436. get
  437. {
  438. if (this.mReplacement)
  439. {
  440. return this.mReplacement.dynamicTexture;
  441. }
  442. if (this.isDynamic)
  443. {
  444. return this.mDynamicFont.material.mainTexture;
  445. }
  446. return null;
  447. }
  448. }
  449. public void MarkAsChanged()
  450. {
  451. if (this.mReplacement != null)
  452. {
  453. this.mReplacement.MarkAsChanged();
  454. }
  455. this.mSprite = null;
  456. UILabel[] array = NGUITools.FindActive<UILabel>();
  457. int i = 0;
  458. int num = array.Length;
  459. while (i < num)
  460. {
  461. UILabel uilabel = array[i];
  462. if (uilabel.enabled && NGUITools.GetActive(uilabel.gameObject) && UIFont.CheckIfRelated(this, uilabel.bitmapFont))
  463. {
  464. UIFont bitmapFont = uilabel.bitmapFont;
  465. uilabel.bitmapFont = null;
  466. uilabel.bitmapFont = bitmapFont;
  467. }
  468. i++;
  469. }
  470. int j = 0;
  471. int count = this.symbols.Count;
  472. while (j < count)
  473. {
  474. this.symbols[j].MarkAsChanged();
  475. j++;
  476. }
  477. }
  478. public void UpdateUVRect()
  479. {
  480. if (this.mAtlas == null)
  481. {
  482. return;
  483. }
  484. Texture texture = this.mAtlas.texture;
  485. if (texture != null)
  486. {
  487. this.mUVRect = new Rect((float)(this.mSprite.x - this.mSprite.paddingLeft), (float)(this.mSprite.y - this.mSprite.paddingTop), (float)(this.mSprite.width + this.mSprite.paddingLeft + this.mSprite.paddingRight), (float)(this.mSprite.height + this.mSprite.paddingTop + this.mSprite.paddingBottom));
  488. this.mUVRect = NGUIMath.ConvertToTexCoords(this.mUVRect, texture.width, texture.height);
  489. if (this.mSprite.hasPadding)
  490. {
  491. this.Trim();
  492. }
  493. }
  494. }
  495. private BMSymbol GetSymbol(string sequence, bool createIfMissing)
  496. {
  497. int i = 0;
  498. int count = this.mSymbols.Count;
  499. while (i < count)
  500. {
  501. BMSymbol bmsymbol = this.mSymbols[i];
  502. if (bmsymbol.sequence == sequence)
  503. {
  504. return bmsymbol;
  505. }
  506. i++;
  507. }
  508. if (createIfMissing)
  509. {
  510. BMSymbol bmsymbol2 = new BMSymbol();
  511. bmsymbol2.sequence = sequence;
  512. this.mSymbols.Add(bmsymbol2);
  513. return bmsymbol2;
  514. }
  515. return null;
  516. }
  517. public BMSymbol MatchSymbol(string text, int offset, int textLength)
  518. {
  519. int count = this.mSymbols.Count;
  520. if (count == 0)
  521. {
  522. return null;
  523. }
  524. textLength -= offset;
  525. for (int i = 0; i < count; i++)
  526. {
  527. BMSymbol bmsymbol = this.mSymbols[i];
  528. int length = bmsymbol.length;
  529. if (length != 0 && textLength >= length)
  530. {
  531. bool flag = true;
  532. for (int j = 0; j < length; j++)
  533. {
  534. if (text[offset + j] != bmsymbol.sequence[j])
  535. {
  536. flag = false;
  537. break;
  538. }
  539. }
  540. if (flag && bmsymbol.Validate(this.atlas))
  541. {
  542. return bmsymbol;
  543. }
  544. }
  545. }
  546. return null;
  547. }
  548. public void AddSymbol(string sequence, string spriteName)
  549. {
  550. BMSymbol symbol = this.GetSymbol(sequence, true);
  551. symbol.spriteName = spriteName;
  552. this.MarkAsChanged();
  553. }
  554. public void RemoveSymbol(string sequence)
  555. {
  556. BMSymbol symbol = this.GetSymbol(sequence, false);
  557. if (symbol != null)
  558. {
  559. this.symbols.Remove(symbol);
  560. }
  561. this.MarkAsChanged();
  562. }
  563. public void RenameSymbol(string before, string after)
  564. {
  565. BMSymbol symbol = this.GetSymbol(before, false);
  566. if (symbol != null)
  567. {
  568. symbol.sequence = after;
  569. }
  570. this.MarkAsChanged();
  571. }
  572. public bool UsesSprite(string s)
  573. {
  574. if (!string.IsNullOrEmpty(s))
  575. {
  576. if (s.Equals(this.spriteName))
  577. {
  578. return true;
  579. }
  580. int i = 0;
  581. int count = this.symbols.Count;
  582. while (i < count)
  583. {
  584. BMSymbol bmsymbol = this.symbols[i];
  585. if (s.Equals(bmsymbol.spriteName))
  586. {
  587. return true;
  588. }
  589. i++;
  590. }
  591. }
  592. return false;
  593. }
  594. [HideInInspector]
  595. [SerializeField]
  596. private Material mMat;
  597. [HideInInspector]
  598. [SerializeField]
  599. private Rect mUVRect = new Rect(0f, 0f, 1f, 1f);
  600. [HideInInspector]
  601. [SerializeField]
  602. private BMFont mFont = new BMFont();
  603. [HideInInspector]
  604. [SerializeField]
  605. private UIAtlas mAtlas;
  606. [HideInInspector]
  607. [SerializeField]
  608. private UIFont mReplacement;
  609. [HideInInspector]
  610. [SerializeField]
  611. private List<BMSymbol> mSymbols = new List<BMSymbol>();
  612. [HideInInspector]
  613. [SerializeField]
  614. private Font mDynamicFont;
  615. [HideInInspector]
  616. [SerializeField]
  617. private int mDynamicFontSize = 16;
  618. [HideInInspector]
  619. [SerializeField]
  620. private FontStyle mDynamicFontStyle;
  621. [NonSerialized]
  622. private UISpriteData mSprite;
  623. private int mPMA = -1;
  624. private int mPacked = -1;
  625. }