Window.cs 16 KB

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