GLoader.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701
  1. using System;
  2. using UnityEngine;
  3. using FairyGUI.Utils;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. /// GLoader class
  8. /// </summary>
  9. public class GLoader : GObject, IAnimationGear, IColorGear
  10. {
  11. /// <summary>
  12. /// Display an error sign if the loader fails to load the content.
  13. /// UIConfig.loaderErrorSign muse be set.
  14. /// </summary>
  15. public bool showErrorSign;
  16. string _url;
  17. AlignType _align;
  18. VertAlignType _verticalAlign;
  19. bool _autoSize;
  20. FillType _fill;
  21. bool _shrinkOnly;
  22. bool _updatingLayout;
  23. PackageItem _contentItem;
  24. Action<NTexture> _reloadDelegate;
  25. MovieClip _content;
  26. GObject _errorSign;
  27. GComponent _content2;
  28. #if FAIRYGUI_PUERTS
  29. public Action __loadExternal;
  30. public Action<NTexture> __freeExternal;
  31. #endif
  32. public GLoader()
  33. {
  34. _url = string.Empty;
  35. _align = AlignType.Left;
  36. _verticalAlign = VertAlignType.Top;
  37. showErrorSign = true;
  38. _reloadDelegate = OnExternalReload;
  39. }
  40. override protected void CreateDisplayObject()
  41. {
  42. displayObject = new Container("GLoader");
  43. displayObject.gOwner = this;
  44. _content = new MovieClip();
  45. ((Container)displayObject).AddChild(_content);
  46. ((Container)displayObject).opaque = true;
  47. }
  48. override public void Dispose()
  49. {
  50. if (_disposed) return;
  51. if (_content.texture != null)
  52. {
  53. if (_contentItem == null)
  54. {
  55. _content.texture.onSizeChanged -= _reloadDelegate;
  56. try
  57. {
  58. FreeExternal(_content.texture);
  59. }
  60. catch (Exception err)
  61. {
  62. Debug.LogWarning(err);
  63. }
  64. }
  65. }
  66. if (_errorSign != null)
  67. _errorSign.Dispose();
  68. if (_content2 != null)
  69. _content2.Dispose();
  70. _content.Dispose();
  71. base.Dispose();
  72. }
  73. /// <summary>
  74. ///
  75. /// </summary>
  76. public string url
  77. {
  78. get { return _url; }
  79. set
  80. {
  81. if (_url == value)
  82. return;
  83. ClearContent();
  84. _url = value;
  85. LoadContent();
  86. UpdateGear(7);
  87. }
  88. }
  89. override public string icon
  90. {
  91. get { return _url; }
  92. set { this.url = value; }
  93. }
  94. /// <summary>
  95. ///
  96. /// </summary>
  97. public AlignType align
  98. {
  99. get { return _align; }
  100. set
  101. {
  102. if (_align != value)
  103. {
  104. _align = value;
  105. UpdateLayout();
  106. }
  107. }
  108. }
  109. /// <summary>
  110. ///
  111. /// </summary>
  112. public VertAlignType verticalAlign
  113. {
  114. get { return _verticalAlign; }
  115. set
  116. {
  117. if (_verticalAlign != value)
  118. {
  119. _verticalAlign = value;
  120. UpdateLayout();
  121. }
  122. }
  123. }
  124. /// <summary>
  125. ///
  126. /// </summary>
  127. public FillType fill
  128. {
  129. get { return _fill; }
  130. set
  131. {
  132. if (_fill != value)
  133. {
  134. _fill = value;
  135. UpdateLayout();
  136. }
  137. }
  138. }
  139. /// <summary>
  140. ///
  141. /// </summary>
  142. public bool shrinkOnly
  143. {
  144. get { return _shrinkOnly; }
  145. set
  146. {
  147. if (_shrinkOnly != value)
  148. {
  149. _shrinkOnly = value;
  150. UpdateLayout();
  151. }
  152. }
  153. }
  154. /// <summary>
  155. ///
  156. /// </summary>
  157. public bool autoSize
  158. {
  159. get { return _autoSize; }
  160. set
  161. {
  162. if (_autoSize != value)
  163. {
  164. _autoSize = value;
  165. UpdateLayout();
  166. }
  167. }
  168. }
  169. /// <summary>
  170. ///
  171. /// </summary>
  172. public bool playing
  173. {
  174. get { return _content.playing; }
  175. set
  176. {
  177. _content.playing = value;
  178. UpdateGear(5);
  179. }
  180. }
  181. /// <summary>
  182. ///
  183. /// </summary>
  184. public int frame
  185. {
  186. get { return _content.frame; }
  187. set
  188. {
  189. _content.frame = value;
  190. UpdateGear(5);
  191. }
  192. }
  193. /// <summary>
  194. ///
  195. /// </summary>
  196. public float timeScale
  197. {
  198. get { return _content.timeScale; }
  199. set { _content.timeScale = value; }
  200. }
  201. /// <summary>
  202. ///
  203. /// </summary>
  204. public bool ignoreEngineTimeScale
  205. {
  206. get { return _content.ignoreEngineTimeScale; }
  207. set { _content.ignoreEngineTimeScale = value; }
  208. }
  209. /// <summary>
  210. ///
  211. /// </summary>
  212. /// <param name="time"></param>
  213. public void Advance(float time)
  214. {
  215. _content.Advance(time);
  216. }
  217. /// <summary>
  218. ///
  219. /// </summary>
  220. public Material material
  221. {
  222. get { return _content.material; }
  223. set { _content.material = value; }
  224. }
  225. /// <summary>
  226. ///
  227. /// </summary>
  228. public string shader
  229. {
  230. get { return _content.shader; }
  231. set { _content.shader = value; }
  232. }
  233. /// <summary>
  234. ///
  235. /// </summary>
  236. public Color color
  237. {
  238. get { return _content.color; }
  239. set
  240. {
  241. if (_content.color != value)
  242. {
  243. _content.color = value;
  244. UpdateGear(4);
  245. }
  246. }
  247. }
  248. /// <summary>
  249. ///
  250. /// </summary>
  251. public FillMethod fillMethod
  252. {
  253. get { return _content.fillMethod; }
  254. set { _content.fillMethod = value; }
  255. }
  256. /// <summary>
  257. ///
  258. /// </summary>
  259. public int fillOrigin
  260. {
  261. get { return _content.fillOrigin; }
  262. set { _content.fillOrigin = value; }
  263. }
  264. /// <summary>
  265. ///
  266. /// </summary>
  267. public bool fillClockwise
  268. {
  269. get { return _content.fillClockwise; }
  270. set { _content.fillClockwise = value; }
  271. }
  272. /// <summary>
  273. ///
  274. /// </summary>
  275. public float fillAmount
  276. {
  277. get { return _content.fillAmount; }
  278. set { _content.fillAmount = value; }
  279. }
  280. /// <summary>
  281. ///
  282. /// </summary>
  283. public Image image
  284. {
  285. get { return _content; }
  286. }
  287. /// <summary>
  288. ///
  289. /// </summary>
  290. public MovieClip movieClip
  291. {
  292. get { return _content; }
  293. }
  294. /// <summary>
  295. ///
  296. /// </summary>
  297. public GComponent component
  298. {
  299. get { return _content2; }
  300. }
  301. /// <summary>
  302. ///
  303. /// </summary>
  304. public NTexture texture
  305. {
  306. get
  307. {
  308. return _content.texture;
  309. }
  310. set
  311. {
  312. this.url = null;
  313. _content.texture = value;
  314. if (value != null)
  315. {
  316. sourceWidth = value.width;
  317. sourceHeight = value.height;
  318. }
  319. else
  320. {
  321. sourceWidth = sourceHeight = 0;
  322. }
  323. UpdateLayout();
  324. }
  325. }
  326. override public IFilter filter
  327. {
  328. get { return _content.filter; }
  329. set { _content.filter = value; }
  330. }
  331. override public BlendMode blendMode
  332. {
  333. get { return _content.blendMode; }
  334. set { _content.blendMode = value; }
  335. }
  336. /// <summary>
  337. ///
  338. /// </summary>
  339. protected void LoadContent()
  340. {
  341. ClearContent();
  342. if (string.IsNullOrEmpty(_url))
  343. return;
  344. if (_url.StartsWith(UIPackage.URL_PREFIX))
  345. LoadFromPackage(_url);
  346. else
  347. LoadExternal();
  348. }
  349. protected void LoadFromPackage(string itemURL)
  350. {
  351. _contentItem = UIPackage.GetItemByURL(itemURL);
  352. if (_contentItem != null)
  353. {
  354. _contentItem = _contentItem.getBranch();
  355. sourceWidth = _contentItem.width;
  356. sourceHeight = _contentItem.height;
  357. _contentItem = _contentItem.getHighResolution();
  358. _contentItem.Load();
  359. if (_contentItem.type == PackageItemType.Image)
  360. {
  361. _content.texture = _contentItem.texture;
  362. _content.textureScale = new Vector2(_contentItem.width / (float)sourceWidth, _contentItem.height / (float)sourceHeight);
  363. _content.scale9Grid = _contentItem.scale9Grid;
  364. _content.scaleByTile = _contentItem.scaleByTile;
  365. _content.tileGridIndice = _contentItem.tileGridIndice;
  366. UpdateLayout();
  367. }
  368. else if (_contentItem.type == PackageItemType.MovieClip)
  369. {
  370. _content.interval = _contentItem.interval;
  371. _content.swing = _contentItem.swing;
  372. _content.repeatDelay = _contentItem.repeatDelay;
  373. _content.frames = _contentItem.frames;
  374. UpdateLayout();
  375. }
  376. else if (_contentItem.type == PackageItemType.Component)
  377. {
  378. GObject obj = UIPackage.CreateObjectFromURL(itemURL);
  379. if (obj == null)
  380. SetErrorState();
  381. else if (!(obj is GComponent))
  382. {
  383. obj.Dispose();
  384. SetErrorState();
  385. }
  386. else
  387. {
  388. _content2 = (GComponent)obj;
  389. ((Container)displayObject).AddChild(_content2.displayObject);
  390. UpdateLayout();
  391. }
  392. }
  393. else
  394. {
  395. if (_autoSize)
  396. this.SetSize(_contentItem.width, _contentItem.height);
  397. SetErrorState();
  398. Debug.LogWarning("Unsupported type of GLoader: " + _contentItem.type);
  399. }
  400. }
  401. else
  402. SetErrorState();
  403. }
  404. virtual protected void LoadExternal()
  405. {
  406. #if FAIRYGUI_PUERTS
  407. if (__loadExternal != null) {
  408. __loadExternal();
  409. return;
  410. }
  411. #endif
  412. Texture2D tex = (Texture2D)Resources.Load(_url, typeof(Texture2D));
  413. if (tex != null)
  414. onExternalLoadSuccess(new NTexture(tex));
  415. else
  416. onExternalLoadFailed();
  417. }
  418. virtual protected void FreeExternal(NTexture texture)
  419. {
  420. #if FAIRYGUI_PUERTS
  421. if (__freeExternal != null) {
  422. __freeExternal(texture);
  423. return;
  424. }
  425. #endif
  426. }
  427. public void onExternalLoadSuccess(NTexture texture)
  428. {
  429. _content.texture = texture;
  430. sourceWidth = texture.width;
  431. sourceHeight = texture.height;
  432. _content.scale9Grid = null;
  433. _content.scaleByTile = false;
  434. texture.onSizeChanged += _reloadDelegate;
  435. UpdateLayout();
  436. }
  437. public void onExternalLoadFailed()
  438. {
  439. SetErrorState();
  440. }
  441. void OnExternalReload(NTexture texture)
  442. {
  443. sourceWidth = texture.width;
  444. sourceHeight = texture.height;
  445. UpdateLayout();
  446. }
  447. private void SetErrorState()
  448. {
  449. if (!showErrorSign || !Application.isPlaying)
  450. return;
  451. if (_errorSign == null)
  452. {
  453. if (UIConfig.loaderErrorSign != null)
  454. _errorSign = UIPackage.CreateObjectFromURL(UIConfig.loaderErrorSign);
  455. else
  456. return;
  457. }
  458. if (_errorSign != null)
  459. {
  460. _errorSign.SetSize(this.width, this.height);
  461. ((Container)displayObject).AddChild(_errorSign.displayObject);
  462. }
  463. }
  464. protected void ClearErrorState()
  465. {
  466. if (_errorSign != null && _errorSign.displayObject.parent != null)
  467. ((Container)displayObject).RemoveChild(_errorSign.displayObject);
  468. }
  469. protected void UpdateLayout()
  470. {
  471. if (_content2 == null && _content.texture == null && _content.frames == null)
  472. {
  473. if (_autoSize)
  474. {
  475. _updatingLayout = true;
  476. SetSize(50, 30);
  477. _updatingLayout = false;
  478. }
  479. return;
  480. }
  481. float contentWidth = sourceWidth;
  482. float contentHeight = sourceHeight;
  483. if (_autoSize)
  484. {
  485. _updatingLayout = true;
  486. if (contentWidth == 0)
  487. contentWidth = 50;
  488. if (contentHeight == 0)
  489. contentHeight = 30;
  490. SetSize(contentWidth, contentHeight);
  491. _updatingLayout = false;
  492. if (_width == contentWidth && _height == contentHeight)
  493. {
  494. if (_content2 != null)
  495. {
  496. _content2.SetXY(0, 0);
  497. _content2.SetScale(1, 1);
  498. }
  499. else
  500. {
  501. _content.SetXY(0, 0);
  502. _content.SetSize(contentWidth, contentHeight);
  503. }
  504. InvalidateBatchingState();
  505. return;
  506. }
  507. //如果不相等,可能是由于大小限制造成的,要后续处理
  508. }
  509. float sx = 1, sy = 1;
  510. if (_fill != FillType.None)
  511. {
  512. sx = this.width / sourceWidth;
  513. sy = this.height / sourceHeight;
  514. if (sx != 1 || sy != 1)
  515. {
  516. if (_fill == FillType.ScaleMatchHeight)
  517. sx = sy;
  518. else if (_fill == FillType.ScaleMatchWidth)
  519. sy = sx;
  520. else if (_fill == FillType.Scale)
  521. {
  522. if (sx > sy)
  523. sx = sy;
  524. else
  525. sy = sx;
  526. }
  527. else if (_fill == FillType.ScaleNoBorder)
  528. {
  529. if (sx > sy)
  530. sy = sx;
  531. else
  532. sx = sy;
  533. }
  534. if (_shrinkOnly)
  535. {
  536. if (sx > 1)
  537. sx = 1;
  538. if (sy > 1)
  539. sy = 1;
  540. }
  541. contentWidth = sourceWidth * sx;
  542. contentHeight = sourceHeight * sy;
  543. }
  544. }
  545. if (_content2 != null)
  546. _content2.SetScale(sx, sy);
  547. else
  548. _content.size = new Vector2(contentWidth, contentHeight);
  549. float nx;
  550. float ny;
  551. if (_align == AlignType.Center)
  552. nx = (this.width - contentWidth) / 2;
  553. else if (_align == AlignType.Right)
  554. nx = this.width - contentWidth;
  555. else
  556. nx = 0;
  557. if (_verticalAlign == VertAlignType.Middle)
  558. ny = (this.height - contentHeight) / 2;
  559. else if (_verticalAlign == VertAlignType.Bottom)
  560. ny = this.height - contentHeight;
  561. else
  562. ny = 0;
  563. if (_content2 != null)
  564. _content2.SetXY(nx, ny);
  565. else
  566. _content.SetXY(nx, ny);
  567. InvalidateBatchingState();
  568. }
  569. private void ClearContent()
  570. {
  571. ClearErrorState();
  572. if (_content.texture != null)
  573. {
  574. if (_contentItem == null)
  575. {
  576. _content.texture.onSizeChanged -= _reloadDelegate;
  577. FreeExternal(_content.texture);
  578. }
  579. _content.texture = null;
  580. }
  581. _content.frames = null;
  582. if (_content2 != null)
  583. {
  584. _content2.Dispose();
  585. _content2 = null;
  586. }
  587. _contentItem = null;
  588. }
  589. override protected void HandleSizeChanged()
  590. {
  591. base.HandleSizeChanged();
  592. if (!_updatingLayout)
  593. UpdateLayout();
  594. }
  595. override public void Setup_BeforeAdd(ByteBuffer buffer, int beginPos)
  596. {
  597. base.Setup_BeforeAdd(buffer, beginPos);
  598. buffer.Seek(beginPos, 5);
  599. _url = buffer.ReadS();
  600. _align = (AlignType)buffer.ReadByte();
  601. _verticalAlign = (VertAlignType)buffer.ReadByte();
  602. _fill = (FillType)buffer.ReadByte();
  603. _shrinkOnly = buffer.ReadBool();
  604. _autoSize = buffer.ReadBool();
  605. showErrorSign = buffer.ReadBool();
  606. _content.playing = buffer.ReadBool();
  607. _content.frame = buffer.ReadInt();
  608. if (buffer.ReadBool())
  609. _content.color = buffer.ReadColor();
  610. _content.fillMethod = (FillMethod)buffer.ReadByte();
  611. if (_content.fillMethod != FillMethod.None)
  612. {
  613. _content.fillOrigin = buffer.ReadByte();
  614. _content.fillClockwise = buffer.ReadBool();
  615. _content.fillAmount = buffer.ReadFloat();
  616. }
  617. if (!string.IsNullOrEmpty(_url))
  618. LoadContent();
  619. }
  620. }
  621. }