Window.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  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. GRoot.inst.modalLayer.color = Color.black;
  175. GRoot.inst.modalLayer.alpha = 0.8f;
  176. }
  177. /// <summary>
  178. ///
  179. /// </summary>
  180. /// <param name="r"></param>
  181. public void ShowOn(GRoot r)
  182. {
  183. r.ShowWindow(this);
  184. }
  185. /// <summary>
  186. ///
  187. /// </summary>
  188. public void Hide()
  189. {
  190. if (this.isShowing)
  191. DoHideAnimation();
  192. }
  193. /// <summary>
  194. /// Hide window immediately, no OnHide will be called.
  195. /// </summary>
  196. public void HideImmediately()
  197. {
  198. this.root.HideWindowImmediately(this);
  199. }
  200. /// <summary>
  201. /// Make the window be center of the screen.
  202. /// </summary>
  203. /// <param name="r"></param>
  204. /// <param name="restraint">Add relations to ensure keeping center on screen size changed.</param>
  205. public void CenterOn(GRoot r, bool restraint)
  206. {
  207. this.SetXY((int)((r.width - this.width) / 2), (int)((r.height - this.height) / 2));
  208. if (restraint)
  209. {
  210. this.AddRelation(r, RelationType.Center_Center);
  211. this.AddRelation(r, RelationType.Middle_Middle);
  212. }
  213. }
  214. /// <summary>
  215. /// Switch show and hide status.
  216. /// </summary>
  217. public void ToggleStatus()
  218. {
  219. if (isTop)
  220. Hide();
  221. else
  222. Show();
  223. }
  224. /// <summary>
  225. ///
  226. /// </summary>
  227. public bool isShowing
  228. {
  229. get { return parent != null; }
  230. }
  231. /// <summary>
  232. ///
  233. /// </summary>
  234. public bool isTop
  235. {
  236. get { return this == GRoot.inst.GetTopWindow(); }
  237. }
  238. /// <summary>
  239. ///
  240. /// </summary>
  241. public bool modal
  242. {
  243. get { return _modal; }
  244. set { _modal = value; }
  245. }
  246. /// <summary>
  247. ///
  248. /// </summary>
  249. public void BringToFront()
  250. {
  251. this.root.BringToFront(this);
  252. this.root.AdjustModalLayer();
  253. }
  254. /// <summary>
  255. ///
  256. /// </summary>
  257. public void ShowModalWait()
  258. {
  259. ShowModalWait(0);
  260. }
  261. /// <summary>
  262. /// Display a modal waiting sign in the front.
  263. /// 显示一个等待标志在最前面。等待标志的资源可以通过UIConfig.windowModalWaiting。等待标志组件会设置为屏幕大小,请内部做好关联。
  264. /// 还可以设定一个requestingCmd作为等待的命令字,在CloseModalWait里传入相同的命令字ModalWait将结束,否则CloseModalWait无效。
  265. /// </summary>
  266. /// <param name="requestingCmd"></param>
  267. public void ShowModalWait(int requestingCmd)
  268. {
  269. if (requestingCmd != 0)
  270. _requestingCmd = requestingCmd;
  271. if (UIConfig.windowModalWaiting != null)
  272. {
  273. if (_modalWaitPane == null)
  274. {
  275. _modalWaitPane = UIPackage.CreateObjectFromURL(UIConfig.windowModalWaiting);
  276. _modalWaitPane.SetHome(this);
  277. }
  278. LayoutModalWaitPane();
  279. AddChild(_modalWaitPane);
  280. }
  281. }
  282. virtual protected void LayoutModalWaitPane()
  283. {
  284. if (_contentArea != null)
  285. {
  286. Vector2 pt = _frame.LocalToGlobal(Vector2.zero);
  287. pt = this.GlobalToLocal(pt);
  288. _modalWaitPane.SetXY((int)pt.x + _contentArea.x, (int)pt.y + _contentArea.y);
  289. _modalWaitPane.SetSize(_contentArea.width, _contentArea.height);
  290. }
  291. else
  292. _modalWaitPane.SetSize(this.width, this.height);
  293. }
  294. /// <summary>
  295. ///
  296. /// </summary>
  297. /// <returns></returns>
  298. public bool CloseModalWait()
  299. {
  300. return CloseModalWait(0);
  301. }
  302. /// <summary>
  303. /// Close modal waiting. If rquestingCmd is equal to the value you transfer in ShowModalWait, mowal wait will be closed.
  304. /// Otherwise, this function has no effect.
  305. /// 关闭模式等待。如果requestingCmd和ShowModalWait传入的不相同,则这个函数没有任何动作,立即返回。
  306. /// </summary>
  307. /// <param name="requestingCmd"></param>
  308. /// <returns></returns>
  309. public bool CloseModalWait(int requestingCmd)
  310. {
  311. if (requestingCmd != 0)
  312. {
  313. if (_requestingCmd != requestingCmd)
  314. return false;
  315. }
  316. _requestingCmd = 0;
  317. if (_modalWaitPane != null && _modalWaitPane.parent != null)
  318. RemoveChild(_modalWaitPane);
  319. return true;
  320. }
  321. /// <summary>
  322. ///
  323. /// </summary>
  324. public bool modalWaiting
  325. {
  326. get { return (_modalWaitPane != null) && _modalWaitPane.inContainer; }
  327. }
  328. /// <summary>
  329. ///
  330. /// </summary>
  331. public void Init()
  332. {
  333. if (_inited || _loading)
  334. return;
  335. if (_uiSources.Count > 0)
  336. {
  337. _loading = false;
  338. int cnt = _uiSources.Count;
  339. for (int i = 0; i < cnt; i++)
  340. {
  341. IUISource lib = _uiSources[i];
  342. if (!lib.loaded)
  343. {
  344. lib.Load(__uiLoadComplete);
  345. _loading = true;
  346. }
  347. }
  348. if (!_loading)
  349. _init();
  350. }
  351. else
  352. _init();
  353. }
  354. /// <summary>
  355. ///
  356. /// </summary>
  357. virtual protected void OnInit()
  358. {
  359. #if FAIRYGUI_TOLUA
  360. CallLua("OnInit");
  361. #endif
  362. #if FAIRYGUI_PUERTS
  363. if (__onInit != null)
  364. __onInit();
  365. #endif
  366. }
  367. /// <summary>
  368. ///
  369. /// </summary>
  370. virtual protected void OnShown()
  371. {
  372. #if FAIRYGUI_TOLUA
  373. CallLua("OnShown");
  374. #endif
  375. #if FAIRYGUI_PUERTS
  376. if (__onShown != null)
  377. __onShown();
  378. #endif
  379. }
  380. /// <summary>
  381. ///
  382. /// </summary>
  383. virtual protected void OnHide()
  384. {
  385. #if FAIRYGUI_TOLUA
  386. CallLua("OnHide");
  387. #endif
  388. #if FAIRYGUI_PUERTS
  389. if (__onHide != null)
  390. __onHide();
  391. #endif
  392. }
  393. /// <summary>
  394. ///
  395. /// </summary>
  396. virtual protected void DoShowAnimation()
  397. {
  398. #if FAIRYGUI_TOLUA
  399. if (!CallLua("DoShowAnimation"))
  400. OnShown();
  401. #elif FAIRYGUI_PUERTS
  402. if (__doShowAnimation != null)
  403. __doShowAnimation();
  404. else
  405. OnShown();
  406. #else
  407. OnShown();
  408. #endif
  409. }
  410. /// <summary>
  411. ///
  412. /// </summary>
  413. virtual protected void DoHideAnimation()
  414. {
  415. #if FAIRYGUI_TOLUA
  416. if (!CallLua("DoHideAnimation"))
  417. HideImmediately();
  418. #elif FAIRYGUI_PUERTS
  419. if (__doHideAnimation != null)
  420. __doHideAnimation();
  421. else
  422. HideImmediately();
  423. #else
  424. HideImmediately();
  425. #endif
  426. }
  427. void __uiLoadComplete()
  428. {
  429. int cnt = _uiSources.Count;
  430. for (int i = 0; i < cnt; i++)
  431. {
  432. IUISource lib = _uiSources[i];
  433. if (!lib.loaded)
  434. return;
  435. }
  436. _loading = false;
  437. _init();
  438. }
  439. void _init()
  440. {
  441. _inited = true;
  442. OnInit();
  443. if (this.isShowing)
  444. DoShowAnimation();
  445. }
  446. override public void Dispose()
  447. {
  448. if (_modalWaitPane != null && _modalWaitPane.parent == null)
  449. _modalWaitPane.Dispose();
  450. #if FAIRYGUI_PUERTS
  451. __onInit = null;
  452. __onShown = null;
  453. __onHide = null;
  454. __doShowAnimation = null;
  455. __doHideAnimation = null;
  456. #endif
  457. base.Dispose();
  458. }
  459. virtual protected void closeEventHandler(EventContext context)
  460. {
  461. Hide();
  462. }
  463. void __addedToStage()
  464. {
  465. if (!_inited)
  466. Init();
  467. else
  468. DoShowAnimation();
  469. }
  470. void __removeFromStage()
  471. {
  472. CloseModalWait();
  473. OnHide();
  474. }
  475. private void __touchBegin(EventContext context)
  476. {
  477. if (this.isShowing && bringToFontOnClick)
  478. {
  479. BringToFront();
  480. }
  481. }
  482. private void __dragStart(EventContext context)
  483. {
  484. context.PreventDefault();
  485. this.StartDrag((int)context.data);
  486. }
  487. }
  488. }