GLoader.cs 18 KB

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