Window.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  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.6f;
  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. }
  253. /// <summary>
  254. ///
  255. /// </summary>
  256. public void ShowModalWait()
  257. {
  258. ShowModalWait(0);
  259. }
  260. /// <summary>
  261. /// Display a modal waiting sign in the front.
  262. /// 显示一个等待标志在最前面。等待标志的资源可以通过UIConfig.windowModalWaiting。等待标志组件会设置为屏幕大小,请内部做好关联。
  263. /// 还可以设定一个requestingCmd作为等待的命令字,在CloseModalWait里传入相同的命令字ModalWait将结束,否则CloseModalWait无效。
  264. /// </summary>
  265. /// <param name="requestingCmd"></param>
  266. public void ShowModalWait(int requestingCmd)
  267. {
  268. if (requestingCmd != 0)
  269. _requestingCmd = requestingCmd;
  270. if (UIConfig.windowModalWaiting != null)
  271. {
  272. if (_modalWaitPane == null)
  273. {
  274. _modalWaitPane = UIPackage.CreateObjectFromURL(UIConfig.windowModalWaiting);
  275. _modalWaitPane.SetHome(this);
  276. }
  277. LayoutModalWaitPane();
  278. AddChild(_modalWaitPane);
  279. }
  280. }
  281. virtual protected void LayoutModalWaitPane()
  282. {
  283. if (_contentArea != null)
  284. {
  285. Vector2 pt = _frame.LocalToGlobal(Vector2.zero);
  286. pt = this.GlobalToLocal(pt);
  287. _modalWaitPane.SetXY((int)pt.x + _contentArea.x, (int)pt.y + _contentArea.y);
  288. _modalWaitPane.SetSize(_contentArea.width, _contentArea.height);
  289. }
  290. else
  291. _modalWaitPane.SetSize(this.width, this.height);
  292. }
  293. /// <summary>
  294. ///
  295. /// </summary>
  296. /// <returns></returns>
  297. public bool CloseModalWait()
  298. {
  299. return CloseModalWait(0);
  300. }
  301. /// <summary>
  302. /// Close modal waiting. If rquestingCmd is equal to the value you transfer in ShowModalWait, mowal wait will be closed.
  303. /// Otherwise, this function has no effect.
  304. /// 关闭模式等待。如果requestingCmd和ShowModalWait传入的不相同,则这个函数没有任何动作,立即返回。
  305. /// </summary>
  306. /// <param name="requestingCmd"></param>
  307. /// <returns></returns>
  308. public bool CloseModalWait(int requestingCmd)
  309. {
  310. if (requestingCmd != 0)
  311. {
  312. if (_requestingCmd != requestingCmd)
  313. return false;
  314. }
  315. _requestingCmd = 0;
  316. if (_modalWaitPane != null && _modalWaitPane.parent != null)
  317. RemoveChild(_modalWaitPane);
  318. return true;
  319. }
  320. /// <summary>
  321. ///
  322. /// </summary>
  323. public bool modalWaiting
  324. {
  325. get { return (_modalWaitPane != null) && _modalWaitPane.inContainer; }
  326. }
  327. /// <summary>
  328. ///
  329. /// </summary>
  330. public void Init()
  331. {
  332. if (_inited || _loading)
  333. return;
  334. if (_uiSources.Count > 0)
  335. {
  336. _loading = false;
  337. int cnt = _uiSources.Count;
  338. for (int i = 0; i < cnt; i++)
  339. {
  340. IUISource lib = _uiSources[i];
  341. if (!lib.loaded)
  342. {
  343. lib.Load(__uiLoadComplete);
  344. _loading = true;
  345. }
  346. }
  347. if (!_loading)
  348. _init();
  349. }
  350. else
  351. _init();
  352. }
  353. /// <summary>
  354. ///
  355. /// </summary>
  356. virtual protected void OnInit()
  357. {
  358. #if FAIRYGUI_TOLUA
  359. CallLua("OnInit");
  360. #endif
  361. #if FAIRYGUI_PUERTS
  362. if (__onInit != null)
  363. __onInit();
  364. #endif
  365. }
  366. /// <summary>
  367. ///
  368. /// </summary>
  369. virtual protected void OnShown()
  370. {
  371. #if FAIRYGUI_TOLUA
  372. CallLua("OnShown");
  373. #endif
  374. #if FAIRYGUI_PUERTS
  375. if (__onShown != null)
  376. __onShown();
  377. #endif
  378. }
  379. /// <summary>
  380. ///
  381. /// </summary>
  382. virtual protected void OnHide()
  383. {
  384. #if FAIRYGUI_TOLUA
  385. CallLua("OnHide");
  386. #endif
  387. #if FAIRYGUI_PUERTS
  388. if (__onHide != null)
  389. __onHide();
  390. #endif
  391. }
  392. /// <summary>
  393. ///
  394. /// </summary>
  395. virtual protected void DoShowAnimation()
  396. {
  397. #if FAIRYGUI_TOLUA
  398. if (!CallLua("DoShowAnimation"))
  399. OnShown();
  400. #elif FAIRYGUI_PUERTS
  401. if (__doShowAnimation != null)
  402. __doShowAnimation();
  403. else
  404. OnShown();
  405. #else
  406. OnShown();
  407. #endif
  408. }
  409. /// <summary>
  410. ///
  411. /// </summary>
  412. virtual protected void DoHideAnimation()
  413. {
  414. #if FAIRYGUI_TOLUA
  415. if (!CallLua("DoHideAnimation"))
  416. HideImmediately();
  417. #elif FAIRYGUI_PUERTS
  418. if (__doHideAnimation != null)
  419. __doHideAnimation();
  420. else
  421. HideImmediately();
  422. #else
  423. HideImmediately();
  424. #endif
  425. }
  426. void __uiLoadComplete()
  427. {
  428. int cnt = _uiSources.Count;
  429. for (int i = 0; i < cnt; i++)
  430. {
  431. IUISource lib = _uiSources[i];
  432. if (!lib.loaded)
  433. return;
  434. }
  435. _loading = false;
  436. _init();
  437. }
  438. void _init()
  439. {
  440. _inited = true;
  441. OnInit();
  442. if (this.isShowing)
  443. DoShowAnimation();
  444. }
  445. override public void Dispose()
  446. {
  447. if (_modalWaitPane != null && _modalWaitPane.parent == null)
  448. _modalWaitPane.Dispose();
  449. #if FAIRYGUI_PUERTS
  450. __onInit = null;
  451. __onShown = null;
  452. __onHide = null;
  453. __doShowAnimation = null;
  454. __doHideAnimation = null;
  455. #endif
  456. base.Dispose();
  457. }
  458. virtual protected void closeEventHandler(EventContext context)
  459. {
  460. Hide();
  461. }
  462. void __addedToStage()
  463. {
  464. if (!_inited)
  465. Init();
  466. else
  467. DoShowAnimation();
  468. }
  469. void __removeFromStage()
  470. {
  471. CloseModalWait();
  472. OnHide();
  473. }
  474. private void __touchBegin(EventContext context)
  475. {
  476. if (this.isShowing && bringToFontOnClick)
  477. {
  478. BringToFront();
  479. }
  480. }
  481. private void __dragStart(EventContext context)
  482. {
  483. context.PreventDefault();
  484. this.StartDrag((int)context.data);
  485. }
  486. }
  487. }