Window.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. /// Window class.
  8. /// 窗口使用前首先要设置窗口中需要显示的内容,这通常是在编辑器里制作好的,可以直接使用Window.contentPane进行设置。
  9. /// 建议把设置contentPane等初始化操作放置到Window.onInit方法中。
  10. /// 另外,FairyGUI还提供了一套机制用于窗口动态创建。动态创建是指初始时仅指定窗口需要使用的资源,等窗口需要显示时才实际开始构建窗口的内容。
  11. /// 首先需要在窗口的构造函数中调用Window.addUISource。这个方法需要一个IUISource类型的参数,而IUISource是一个接口,
  12. /// 用户需要自行实现载入相关UI包的逻辑。当窗口第一次显示之前,IUISource的加载方法将会被调用,并等待载入完成后才返回执行Window.OnInit,然后窗口才会显示。
  13. ///
  14. /// 如果你需要窗口显示时播放动画效果,那么覆盖doShowAnimation编写你的动画代码,并且在动画结束后调用onShown。覆盖onShown编写其他需要在窗口显示时处理的业务逻辑。
  15. /// 如果你需要窗口隐藏时播放动画效果,那么覆盖doHideAnimation编写你的动画代码,并且在动画结束时调用Window.hideImmediately(注意不是直接调用onHide!)。覆盖onHide编写其他需要在窗口隐藏时处理的业务逻辑。
  16. /// </summary>
  17. public class Window : GComponent
  18. {
  19. /// <summary>
  20. ///
  21. /// </summary>
  22. public bool bringToFontOnClick;
  23. GComponent _frame;
  24. GComponent _contentPane;
  25. GObject _modalWaitPane;
  26. GObject _closeButton;
  27. GObject _dragArea;
  28. GObject _contentArea;
  29. bool _modal;
  30. List<IUISource> _uiSources;
  31. bool _inited;
  32. bool _loading;
  33. protected int _requestingCmd;
  34. #if FAIRYGUI_PUERTS
  35. public Action __onInit;
  36. public Action __onShown;
  37. public Action __onHide;
  38. public Action __doShowAnimation;
  39. public Action __doHideAnimation;
  40. #endif
  41. public Window()
  42. : base()
  43. {
  44. _uiSources = new List<IUISource>();
  45. this.tabStopChildren = true;
  46. bringToFontOnClick = UIConfig.bringWindowToFrontOnClick;
  47. displayObject.onAddedToStage.Add(__addedToStage);
  48. displayObject.onRemovedFromStage.Add(__removeFromStage);
  49. displayObject.onTouchBegin.AddCapture(__touchBegin);
  50. this.gameObjectName = "Window";
  51. SetHome(GRoot.inst);
  52. }
  53. /// <summary>
  54. /// Set a UISource to this window. It must call before the window is shown. When the window is first time to show,
  55. /// UISource.Load is called. Only after all UISource is loaded, the window will continue to init.
  56. /// 为窗口添加一个源。这个方法建议在构造函数调用。当窗口第一次显示前,UISource的Load方法将被调用,然后只有所有的UISource
  57. /// 都ready后,窗口才会继续初始化和显示。
  58. /// </summary>
  59. /// <param name="source"></param>
  60. public void AddUISource(IUISource source)
  61. {
  62. _uiSources.Add(source);
  63. }
  64. /// <summary>
  65. ///
  66. /// </summary>
  67. public GComponent contentPane
  68. {
  69. set
  70. {
  71. if (_contentPane != value)
  72. {
  73. if (_contentPane != null)
  74. RemoveChild(_contentPane);
  75. _contentPane = value;
  76. if (_contentPane != null)
  77. {
  78. this.gameObjectName = "Window - " + _contentPane.gameObjectName;
  79. _contentPane.gameObjectName = "ContentPane";
  80. AddChild(_contentPane);
  81. this.SetSize(_contentPane.width, _contentPane.height);
  82. _contentPane.AddRelation(this, RelationType.Size);
  83. _contentPane.fairyBatching = true;
  84. _frame = _contentPane.GetChild("frame") as GComponent;
  85. if (_frame != null)
  86. {
  87. this.closeButton = _frame.GetChild("closeButton");
  88. this.dragArea = _frame.GetChild("dragArea");
  89. this.contentArea = _frame.GetChild("contentArea");
  90. }
  91. }
  92. else
  93. {
  94. _frame = null;
  95. this.gameObjectName = "Window";
  96. }
  97. }
  98. }
  99. get
  100. {
  101. return _contentPane;
  102. }
  103. }
  104. /// <summary>
  105. ///
  106. /// </summary>
  107. public GComponent frame
  108. {
  109. get { return _frame; }
  110. }
  111. /// <summary>
  112. ///
  113. /// </summary>
  114. public GObject closeButton
  115. {
  116. get { return _closeButton; }
  117. set
  118. {
  119. if (_closeButton != null)
  120. _closeButton.onClick.Remove(closeEventHandler);
  121. _closeButton = value;
  122. if (_closeButton != null)
  123. _closeButton.onClick.Add(closeEventHandler);
  124. }
  125. }
  126. /// <summary>
  127. ///
  128. /// </summary>
  129. public GObject dragArea
  130. {
  131. get { return _dragArea; }
  132. set
  133. {
  134. if (_dragArea != value)
  135. {
  136. if (_dragArea != null)
  137. {
  138. _dragArea.draggable = false;
  139. _dragArea.onDragStart.Remove(__dragStart);
  140. }
  141. _dragArea = value;
  142. if (_dragArea != null)
  143. {
  144. GGraph graph = _dragArea as GGraph;
  145. if (graph != null && graph.shape.isEmpty)
  146. graph.DrawRect(_dragArea.width, _dragArea.height, 0, Color.clear, Color.clear);
  147. _dragArea.draggable = true;
  148. _dragArea.onDragStart.Add(__dragStart);
  149. }
  150. }
  151. }
  152. }
  153. /// <summary>
  154. ///
  155. /// </summary>
  156. public GObject contentArea
  157. {
  158. get { return _contentArea; }
  159. set { _contentArea = value; }
  160. }
  161. /// <summary>
  162. ///
  163. /// </summary>
  164. public GObject modalWaitingPane
  165. {
  166. get { return _modalWaitPane; }
  167. }
  168. /// <summary>
  169. ///
  170. /// </summary>
  171. public void Show()
  172. {
  173. GRoot.inst.ShowWindow(this);
  174. }
  175. /// <summary>
  176. ///
  177. /// </summary>
  178. /// <param name="r"></param>
  179. public void ShowOn(GRoot r)
  180. {
  181. r.ShowWindow(this);
  182. }
  183. /// <summary>
  184. ///
  185. /// </summary>
  186. public void Hide()
  187. {
  188. if (this.isShowing)
  189. DoHideAnimation();
  190. }
  191. /// <summary>
  192. /// Hide window immediately, no OnHide will be called.
  193. /// </summary>
  194. public void HideImmediately()
  195. {
  196. this.root.HideWindowImmediately(this);
  197. }
  198. /// <summary>
  199. /// Make the window be center of the screen.
  200. /// </summary>
  201. /// <param name="r"></param>
  202. /// <param name="restraint">Add relations to ensure keeping center on screen size changed.</param>
  203. public void CenterOn(GRoot r, bool restraint)
  204. {
  205. this.SetXY((int)((r.width - this.width) / 2), (int)((r.height - this.height) / 2));
  206. if (restraint)
  207. {
  208. this.AddRelation(r, RelationType.Center_Center);
  209. this.AddRelation(r, RelationType.Middle_Middle);
  210. }
  211. }
  212. /// <summary>
  213. /// Switch show and hide status.
  214. /// </summary>
  215. public void ToggleStatus()
  216. {
  217. if (isTop)
  218. Hide();
  219. else
  220. Show();
  221. }
  222. /// <summary>
  223. ///
  224. /// </summary>
  225. public bool isShowing
  226. {
  227. get { return parent != null; }
  228. }
  229. /// <summary>
  230. ///
  231. /// </summary>
  232. public bool isTop
  233. {
  234. get { return parent != null && parent.GetChildIndex(this) == parent.numChildren - 1; }
  235. }
  236. /// <summary>
  237. ///
  238. /// </summary>
  239. public bool modal
  240. {
  241. get { return _modal; }
  242. set { _modal = value; }
  243. }
  244. /// <summary>
  245. ///
  246. /// </summary>
  247. public void BringToFront()
  248. {
  249. this.root.BringToFront(this);
  250. }
  251. /// <summary>
  252. ///
  253. /// </summary>
  254. public void ShowModalWait()
  255. {
  256. ShowModalWait(0);
  257. }
  258. /// <summary>
  259. /// Display a modal waiting sign in the front.
  260. /// 显示一个等待标志在最前面。等待标志的资源可以通过UIConfig.windowModalWaiting。等待标志组件会设置为屏幕大小,请内部做好关联。
  261. /// 还可以设定一个requestingCmd作为等待的命令字,在CloseModalWait里传入相同的命令字ModalWait将结束,否则CloseModalWait无效。
  262. /// </summary>
  263. /// <param name="requestingCmd"></param>
  264. public void ShowModalWait(int requestingCmd)
  265. {
  266. if (requestingCmd != 0)
  267. _requestingCmd = requestingCmd;
  268. if (UIConfig.windowModalWaiting != null)
  269. {
  270. if (_modalWaitPane == null)
  271. {
  272. _modalWaitPane = UIPackage.CreateObjectFromURL(UIConfig.windowModalWaiting);
  273. _modalWaitPane.SetHome(this);
  274. }
  275. LayoutModalWaitPane();
  276. AddChild(_modalWaitPane);
  277. }
  278. }
  279. virtual protected void LayoutModalWaitPane()
  280. {
  281. if (_contentArea != null)
  282. {
  283. Vector2 pt = _frame.LocalToGlobal(Vector2.zero);
  284. pt = this.GlobalToLocal(pt);
  285. _modalWaitPane.SetXY((int)pt.x + _contentArea.x, (int)pt.y + _contentArea.y);
  286. _modalWaitPane.SetSize(_contentArea.width, _contentArea.height);
  287. }
  288. else
  289. _modalWaitPane.SetSize(this.width, this.height);
  290. }
  291. /// <summary>
  292. ///
  293. /// </summary>
  294. /// <returns></returns>
  295. public bool CloseModalWait()
  296. {
  297. return CloseModalWait(0);
  298. }
  299. /// <summary>
  300. /// Close modal waiting. If rquestingCmd is equal to the value you transfer in ShowModalWait, mowal wait will be closed.
  301. /// Otherwise, this function has no effect.
  302. /// 关闭模式等待。如果requestingCmd和ShowModalWait传入的不相同,则这个函数没有任何动作,立即返回。
  303. /// </summary>
  304. /// <param name="requestingCmd"></param>
  305. /// <returns></returns>
  306. public bool CloseModalWait(int requestingCmd)
  307. {
  308. if (requestingCmd != 0)
  309. {
  310. if (_requestingCmd != requestingCmd)
  311. return false;
  312. }
  313. _requestingCmd = 0;
  314. if (_modalWaitPane != null && _modalWaitPane.parent != null)
  315. RemoveChild(_modalWaitPane);
  316. return true;
  317. }
  318. /// <summary>
  319. ///
  320. /// </summary>
  321. public bool modalWaiting
  322. {
  323. get { return (_modalWaitPane != null) && _modalWaitPane.inContainer; }
  324. }
  325. /// <summary>
  326. ///
  327. /// </summary>
  328. public void Init()
  329. {
  330. if (_inited || _loading)
  331. return;
  332. if (_uiSources.Count > 0)
  333. {
  334. _loading = false;
  335. int cnt = _uiSources.Count;
  336. for (int i = 0; i < cnt; i++)
  337. {
  338. IUISource lib = _uiSources[i];
  339. if (!lib.loaded)
  340. {
  341. lib.Load(__uiLoadComplete);
  342. _loading = true;
  343. }
  344. }
  345. if (!_loading)
  346. _init();
  347. }
  348. else
  349. _init();
  350. }
  351. /// <summary>
  352. ///
  353. /// </summary>
  354. virtual protected void OnInit()
  355. {
  356. #if FAIRYGUI_TOLUA
  357. CallLua("OnInit");
  358. #endif
  359. #if FAIRYGUI_PUERTS
  360. if (__onInit != null)
  361. __onInit();
  362. #endif
  363. }
  364. /// <summary>
  365. ///
  366. /// </summary>
  367. virtual protected void OnShown()
  368. {
  369. #if FAIRYGUI_TOLUA
  370. CallLua("OnShown");
  371. #endif
  372. #if FAIRYGUI_PUERTS
  373. if (__onShown != null)
  374. __onShown();
  375. #endif
  376. }
  377. /// <summary>
  378. ///
  379. /// </summary>
  380. virtual protected void OnHide()
  381. {
  382. #if FAIRYGUI_TOLUA
  383. CallLua("OnHide");
  384. #endif
  385. #if FAIRYGUI_PUERTS
  386. if (__onHide != null)
  387. __onHide();
  388. #endif
  389. }
  390. /// <summary>
  391. ///
  392. /// </summary>
  393. virtual protected void DoShowAnimation()
  394. {
  395. #if FAIRYGUI_TOLUA
  396. if (!CallLua("DoShowAnimation"))
  397. OnShown();
  398. #elif FAIRYGUI_PUERTS
  399. if (__doShowAnimation != null)
  400. __doShowAnimation();
  401. else
  402. OnShown();
  403. #else
  404. OnShown();
  405. #endif
  406. }
  407. /// <summary>
  408. ///
  409. /// </summary>
  410. virtual protected void DoHideAnimation()
  411. {
  412. #if FAIRYGUI_TOLUA
  413. if (!CallLua("DoHideAnimation"))
  414. HideImmediately();
  415. #elif FAIRYGUI_PUERTS
  416. if (__doHideAnimation != null)
  417. __doHideAnimation();
  418. else
  419. HideImmediately();
  420. #else
  421. HideImmediately();
  422. #endif
  423. }
  424. void __uiLoadComplete()
  425. {
  426. int cnt = _uiSources.Count;
  427. for (int i = 0; i < cnt; i++)
  428. {
  429. IUISource lib = _uiSources[i];
  430. if (!lib.loaded)
  431. return;
  432. }
  433. _loading = false;
  434. _init();
  435. }
  436. void _init()
  437. {
  438. _inited = true;
  439. OnInit();
  440. if (this.isShowing)
  441. DoShowAnimation();
  442. }
  443. override public void Dispose()
  444. {
  445. if (_modalWaitPane != null && _modalWaitPane.parent == null)
  446. _modalWaitPane.Dispose();
  447. //正在加载资源的异步过程中发生意外关闭 应该取消正在加载的load
  448. if (_loading)
  449. {
  450. for (int i = 0; i < _uiSources.Count; ++i)
  451. {
  452. _uiSources[i].Cancel();
  453. }
  454. }
  455. #if FAIRYGUI_PUERTS
  456. __onInit = null;
  457. __onShown = null;
  458. __onHide = null;
  459. __doShowAnimation = null;
  460. __doHideAnimation = null;
  461. #endif
  462. base.Dispose();
  463. }
  464. virtual protected void closeEventHandler(EventContext context)
  465. {
  466. Hide();
  467. }
  468. void __addedToStage()
  469. {
  470. if (!_inited)
  471. Init();
  472. else
  473. DoShowAnimation();
  474. }
  475. void __removeFromStage()
  476. {
  477. CloseModalWait();
  478. OnHide();
  479. }
  480. private void __touchBegin(EventContext context)
  481. {
  482. if (this.isShowing && bringToFontOnClick)
  483. {
  484. BringToFront();
  485. }
  486. }
  487. private void __dragStart(EventContext context)
  488. {
  489. context.PreventDefault();
  490. this.StartDrag((int)context.data);
  491. }
  492. }
  493. }