GRoot.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace FairyGUI
  5. {
  6. /// <summary>
  7. /// GRoot is a topmost component of UI display list.You dont need to create GRoot. It is created automatically.
  8. /// </summary>
  9. public class GRoot : GComponent
  10. {
  11. /// <summary>
  12. ///
  13. /// </summary>
  14. public static float contentScaleFactor
  15. {
  16. get { return UIContentScaler.scaleFactor; }
  17. }
  18. /// <summary>
  19. ///
  20. /// </summary>
  21. public static int contentScaleLevel
  22. {
  23. get { return UIContentScaler.scaleLevel; }
  24. }
  25. GGraph _modalLayer;
  26. GObject _modalWaitPane;
  27. List<GObject> _popupStack;
  28. List<GObject> _justClosedPopups;
  29. HashSet<GObject> _specialPopups;
  30. GObject _tooltipWin;
  31. GObject _defaultTooltipWin;
  32. internal static GRoot _inst;
  33. public static GRoot inst
  34. {
  35. get
  36. {
  37. if (_inst == null)
  38. Stage.Instantiate();
  39. return _inst;
  40. }
  41. }
  42. public GRoot()
  43. {
  44. this.name = this.rootContainer.name = this.rootContainer.gameObject.name = "GRoot";
  45. this.opaque = false;
  46. _popupStack = new List<GObject>();
  47. _justClosedPopups = new List<GObject>();
  48. _specialPopups = new HashSet<GObject>();
  49. Stage.inst.onTouchBegin.AddCapture(__stageTouchBegin);
  50. Stage.inst.onTouchEnd.AddCapture(__stageTouchEnd);
  51. }
  52. override public void Dispose()
  53. {
  54. base.Dispose();
  55. Stage.inst.onTouchBegin.RemoveCapture(__stageTouchBegin);
  56. Stage.inst.onTouchEnd.RemoveCapture(__stageTouchEnd);
  57. }
  58. /// <summary>
  59. /// Set content scale factor.
  60. /// </summary>
  61. /// <param name="designResolutionX">Design resolution of x axis.</param>
  62. /// <param name="designResolutionY">Design resolution of y axis.</param>
  63. public void SetContentScaleFactor(int designResolutionX, int designResolutionY)
  64. {
  65. SetContentScaleFactor(designResolutionX, designResolutionY, UIContentScaler.ScreenMatchMode.MatchWidthOrHeight);
  66. }
  67. /// <summary>
  68. /// Set content scale factor.
  69. /// </summary>
  70. /// <param name="designResolutionX">Design resolution of x axis.</param>
  71. /// <param name="designResolutionY">Design resolution of y axis.</param>
  72. /// <param name="screenMatchMode">Match mode.</param>
  73. public void SetContentScaleFactor(int designResolutionX, int designResolutionY, UIContentScaler.ScreenMatchMode screenMatchMode)
  74. {
  75. UIContentScaler scaler = Stage.inst.gameObject.GetComponent<UIContentScaler>();
  76. scaler.designResolutionX = designResolutionX;
  77. scaler.designResolutionY = designResolutionY;
  78. scaler.scaleMode = UIContentScaler.ScaleMode.ScaleWithScreenSize;
  79. scaler.screenMatchMode = screenMatchMode;
  80. scaler.ApplyChange();
  81. ApplyContentScaleFactor();
  82. }
  83. /// <summary>
  84. ///
  85. /// </summary>
  86. /// <param name="constantScaleFactor"></param>
  87. public void SetContentScaleFactor(float constantScaleFactor)
  88. {
  89. UIContentScaler scaler = Stage.inst.gameObject.GetComponent<UIContentScaler>();
  90. scaler.scaleMode = UIContentScaler.ScaleMode.ConstantPixelSize;
  91. scaler.constantScaleFactor = constantScaleFactor;
  92. scaler.ApplyChange();
  93. ApplyContentScaleFactor();
  94. }
  95. /// <summary>
  96. /// This is called after screen size changed.
  97. /// </summary>
  98. public void ApplyContentScaleFactor()
  99. {
  100. this.SetSize(Mathf.CeilToInt(Stage.inst.width / UIContentScaler.scaleFactor), Mathf.CeilToInt(Stage.inst.height / UIContentScaler.scaleFactor));
  101. this.SetScale(UIContentScaler.scaleFactor, UIContentScaler.scaleFactor);
  102. }
  103. /// <summary>
  104. /// Display a window.
  105. /// </summary>
  106. /// <param name="win"></param>
  107. public void ShowWindow(Window win)
  108. {
  109. AddChild(win);
  110. AdjustModalLayer();
  111. }
  112. /// <summary>
  113. /// Call window.Hide
  114. /// 关闭一个窗口。将调用Window.Hide方法。
  115. /// </summary>
  116. /// <param name="win"></param>
  117. public void HideWindow(Window win)
  118. {
  119. win.Hide();
  120. }
  121. /// <summary>
  122. /// Remove a window from stage immediatelly. window.Hide/window.OnHide will never be called.
  123. ///立刻关闭一个窗口。不会调用Window.Hide方法,Window.OnHide也不会被调用。
  124. /// </summary>
  125. /// <param name="win"></param>
  126. public void HideWindowImmediately(Window win)
  127. {
  128. HideWindowImmediately(win, false);
  129. }
  130. /// <summary>
  131. /// Remove a window from stage immediatelly. window.Hide/window.OnHide will never be called.
  132. /// 立刻关闭一个窗口。不会调用Window.Hide方法,Window.OnHide也不会被调用。
  133. /// </summary>
  134. /// <param name="win"></param>
  135. /// <param name="dispose">True to dispose the window.</param>
  136. public void HideWindowImmediately(Window win, bool dispose)
  137. {
  138. if (win.parent == this)
  139. RemoveChild(win, dispose);
  140. else if (dispose)
  141. win.Dispose();
  142. AdjustModalLayer();
  143. }
  144. /// <summary>
  145. /// 将一个窗口提到所有窗口的最前面
  146. /// </summary>
  147. /// <param name="win"></param>
  148. public void BringToFront(Window win)
  149. {
  150. int cnt = this.numChildren;
  151. int i;
  152. if (_modalLayer != null && _modalLayer.parent != null && !win.modal)
  153. i = GetChildIndex(_modalLayer) - 1;
  154. else
  155. i = cnt - 1;
  156. for (; i >= 0; i--)
  157. {
  158. GObject g = GetChildAt(i);
  159. if (g == win)
  160. return;
  161. if (g is Window)
  162. break;
  163. }
  164. if (i >= 0)
  165. SetChildIndex(win, i);
  166. }
  167. /// <summary>
  168. /// Display a modal layer and a waiting sign in the front.
  169. /// 显示一个半透明层和一个等待标志在最前面。半透明层的颜色可以通过UIConfig.modalLayerColor设定。
  170. /// 等待标志的资源可以通过UIConfig.globalModalWaiting。等待标志组件会设置为屏幕大小,请内部做好关联。
  171. /// </summary>
  172. public void ShowModalWait()
  173. {
  174. if (UIConfig.globalModalWaiting != null)
  175. {
  176. if (_modalWaitPane == null || _modalWaitPane.isDisposed)
  177. {
  178. _modalWaitPane = UIPackage.CreateObjectFromURL(UIConfig.globalModalWaiting);
  179. _modalWaitPane.SetHome(this);
  180. }
  181. _modalWaitPane.SetSize(this.width, this.height);
  182. _modalWaitPane.AddRelation(this, RelationType.Size);
  183. AddChild(_modalWaitPane);
  184. }
  185. }
  186. /// <summary>
  187. /// Hide modal layer and waiting sign.
  188. /// </summary>
  189. public void CloseModalWait()
  190. {
  191. if (_modalWaitPane != null && _modalWaitPane.parent != null)
  192. RemoveChild(_modalWaitPane);
  193. }
  194. /// <summary>
  195. /// Close all windows except modal windows.
  196. /// </summary>
  197. public void CloseAllExceptModals()
  198. {
  199. GObject[] arr = _children.ToArray();
  200. foreach (GObject g in arr)
  201. {
  202. if ((g is Window) && !(g as Window).modal)
  203. HideWindowImmediately(g as Window);
  204. }
  205. }
  206. /// <summary>
  207. /// Close all windows.
  208. /// </summary>
  209. public void CloseAllWindows()
  210. {
  211. GObject[] arr = _children.ToArray();
  212. foreach (GObject g in arr)
  213. {
  214. if (g is Window)
  215. HideWindowImmediately(g as Window);
  216. }
  217. }
  218. /// <summary>
  219. /// Get window on top.
  220. /// </summary>
  221. /// <returns></returns>
  222. public Window GetTopWindow()
  223. {
  224. int cnt = this.numChildren;
  225. for (int i = cnt - 1; i >= 0; i--)
  226. {
  227. GObject g = this.GetChildAt(i);
  228. if (g is Window)
  229. {
  230. return (Window)(g);
  231. }
  232. }
  233. return null;
  234. }
  235. /// <summary>
  236. ///
  237. /// </summary>
  238. public GGraph modalLayer
  239. {
  240. get
  241. {
  242. if (_modalLayer == null || _modalLayer.isDisposed)
  243. CreateModalLayer();
  244. return _modalLayer;
  245. }
  246. }
  247. void CreateModalLayer()
  248. {
  249. _modalLayer = new GGraph();
  250. _modalLayer.DrawRect(this.width, this.height, 0, Color.white, UIConfig.modalLayerColor);
  251. _modalLayer.AddRelation(this, RelationType.Size);
  252. _modalLayer.name = _modalLayer.gameObjectName = "ModalLayer";
  253. _modalLayer.SetHome(this);
  254. }
  255. /// <summary>
  256. /// Return true if a modal window is on stage.
  257. /// </summary>
  258. public bool hasModalWindow
  259. {
  260. get { return _modalLayer != null && _modalLayer.parent != null; }
  261. }
  262. /// <summary>
  263. /// Return true if modal waiting layer is on stage.
  264. /// </summary>
  265. public bool modalWaiting
  266. {
  267. get
  268. {
  269. return (_modalWaitPane != null) && _modalWaitPane.onStage;
  270. }
  271. }
  272. /// <summary>
  273. /// Get current touch target. (including hover)
  274. /// </summary>
  275. public GObject touchTarget
  276. {
  277. get
  278. {
  279. return DisplayObjectToGObject(Stage.inst.touchTarget);
  280. }
  281. }
  282. /// <summary>
  283. ///
  284. /// </summary>
  285. /// <param name="obj"></param>
  286. /// <returns></returns>
  287. public GObject DisplayObjectToGObject(DisplayObject obj)
  288. {
  289. while (obj != null)
  290. {
  291. if (obj.gOwner != null)
  292. return obj.gOwner;
  293. obj = obj.parent;
  294. }
  295. return null;
  296. }
  297. private void AdjustModalLayer()
  298. {
  299. if (_modalLayer == null || _modalLayer.isDisposed)
  300. CreateModalLayer();
  301. int cnt = this.numChildren;
  302. if (_modalWaitPane != null && _modalWaitPane.parent != null)
  303. SetChildIndex(_modalWaitPane, cnt - 1);
  304. for (int i = cnt - 1; i >= 0; i--)
  305. {
  306. GObject g = this.GetChildAt(i);
  307. if ((g is Window) && (g as Window).modal)
  308. {
  309. if (_modalLayer.parent == null)
  310. AddChildAt(_modalLayer, i);
  311. else
  312. SetChildIndexBefore(_modalLayer, i);
  313. return;
  314. }
  315. }
  316. if (_modalLayer.parent != null)
  317. RemoveChild(_modalLayer);
  318. }
  319. /// <summary>
  320. /// Show a popup object.
  321. /// 显示一个popup。
  322. /// popup的特点是点击popup对象外的区域,popup对象将自动消失。
  323. /// </summary>
  324. /// <param name="popup"></param>
  325. public void ShowPopup(GObject popup)
  326. {
  327. ShowPopup(popup, null, PopupDirection.Auto, false);
  328. }
  329. /// <summary>
  330. /// Show a popup object along with the specific target object.
  331. /// 显示一个popup。将popup显示在指定对象的上边或者下边。
  332. /// popup的特点是点击popup对象外的区域,popup对象将自动消失。
  333. /// </summary>
  334. /// <param name="popup"></param>
  335. /// <param name="target"></param>
  336. public void ShowPopup(GObject popup, GObject target)
  337. {
  338. ShowPopup(popup, target, PopupDirection.Auto, false);
  339. }
  340. [Obsolete]
  341. public void ShowPopup(GObject popup, GObject target, object downward)
  342. {
  343. ShowPopup(popup, target,
  344. downward == null ? PopupDirection.Auto : ((bool)downward == true ? PopupDirection.Down : PopupDirection.Up),
  345. false);
  346. }
  347. /// <summary>
  348. /// Show a popup object along with the specific target object.
  349. /// 显示一个popup。将popup显示在指定对象的上方或者下方。
  350. /// popup的特点是点击popup对象外的区域,popup对象将自动消失。
  351. /// </summary>
  352. /// <param name="popup"></param>
  353. /// <param name="target"></param>
  354. /// <param name="dir"></param>
  355. public void ShowPopup(GObject popup, GObject target, PopupDirection dir)
  356. {
  357. ShowPopup(popup, target, dir, false);
  358. }
  359. /// <summary>
  360. /// Show a popup object along with the specific target object.
  361. /// 显示一个popup。将popup显示在指定对象的上方或者下方。
  362. /// popup的特点是点击popup对象外的区域,popup对象将自动消失。
  363. /// 默认情况下,popup在touchEnd事件中关闭;特别设置closeUntilUpEvent=true则可使该popup在touchEnd中才关闭。
  364. /// </summary>
  365. /// <param name="popup"></param>
  366. /// <param name="target"></param>
  367. /// <param name="dir"></param>
  368. /// <param name="closeUntilUpEvent"></param>
  369. public void ShowPopup(GObject popup, GObject target, PopupDirection dir, bool closeUntilUpEvent)
  370. {
  371. if (_popupStack.Count > 0)
  372. {
  373. int k = _popupStack.IndexOf(popup);
  374. if (k != -1)
  375. {
  376. for (int i = _popupStack.Count - 1; i >= k; i--)
  377. {
  378. int last = _popupStack.Count - 1;
  379. GObject obj = _popupStack[last];
  380. ClosePopup(obj);
  381. _popupStack.RemoveAt(last);
  382. _specialPopups.Remove(obj);
  383. }
  384. }
  385. }
  386. _popupStack.Add(popup);
  387. if (closeUntilUpEvent)
  388. _specialPopups.Add(popup);
  389. if (target != null)
  390. {
  391. GObject p = target;
  392. while (p != null)
  393. {
  394. if (p.parent == this)
  395. {
  396. if (popup.sortingOrder < p.sortingOrder)
  397. {
  398. popup.sortingOrder = p.sortingOrder;
  399. }
  400. break;
  401. }
  402. p = p.parent;
  403. }
  404. }
  405. AddChild(popup);
  406. AdjustModalLayer();
  407. if ((popup is Window) && target == null && dir == PopupDirection.Auto)
  408. return;
  409. Vector2 pos = GetPoupPosition(popup, target, dir);
  410. popup.xy = pos;
  411. }
  412. [Obsolete]
  413. public Vector2 GetPoupPosition(GObject popup, GObject target, object downward)
  414. {
  415. return GetPoupPosition(popup, target,
  416. downward == null ? PopupDirection.Auto : ((bool)downward == true ? PopupDirection.Down : PopupDirection.Up));
  417. }
  418. /// <summary>
  419. ///
  420. /// </summary>
  421. /// <param name="popup"></param>
  422. /// <param name="target"></param>
  423. /// <param name="dir"></param>
  424. /// <returns></returns>
  425. public Vector2 GetPoupPosition(GObject popup, GObject target, PopupDirection dir)
  426. {
  427. Vector2 pos;
  428. Vector2 size = Vector2.zero;
  429. if (target != null)
  430. {
  431. pos = target.LocalToRoot(Vector2.zero, this);
  432. size = target.LocalToRoot(target.size, this) - pos;
  433. }
  434. else
  435. {
  436. pos = this.GlobalToLocal(Stage.inst.touchPosition);
  437. }
  438. float xx, yy;
  439. xx = pos.x;
  440. if (xx + popup.width > this.width)
  441. xx = xx + size.x - popup.width;
  442. yy = pos.y + size.y;
  443. if ((dir == PopupDirection.Auto && yy + popup.height > this.height)
  444. || dir == PopupDirection.Up)
  445. {
  446. yy = pos.y - popup.height - 1;
  447. if (yy < 0)
  448. {
  449. yy = 0;
  450. xx += size.x / 2;
  451. }
  452. }
  453. return new Vector2(Mathf.RoundToInt(xx), Mathf.RoundToInt(yy));
  454. }
  455. /// <summary>
  456. /// If a popup is showing, then close it; otherwise, open it.
  457. /// </summary>
  458. /// <param name="popup"></param>
  459. public void TogglePopup(GObject popup)
  460. {
  461. TogglePopup(popup, null, PopupDirection.Auto, false);
  462. }
  463. /// <summary>
  464. ///
  465. /// </summary>
  466. /// <param name="popup"></param>
  467. /// <param name="target"></param>
  468. public void TogglePopup(GObject popup, GObject target)
  469. {
  470. TogglePopup(popup, target, PopupDirection.Auto, false);
  471. }
  472. [Obsolete]
  473. public void TogglePopup(GObject popup, GObject target, object downward)
  474. {
  475. TogglePopup(popup, target,
  476. downward == null ? PopupDirection.Auto : ((bool)downward == true ? PopupDirection.Down : PopupDirection.Up),
  477. false);
  478. }
  479. /// <summary>
  480. ///
  481. /// </summary>
  482. /// <param name="popup"></param>
  483. /// <param name="target"></param>
  484. /// <param name="dir"></param>
  485. public void TogglePopup(GObject popup, GObject target, PopupDirection dir)
  486. {
  487. TogglePopup(popup, target, dir, false);
  488. }
  489. /// <summary>
  490. ///
  491. /// </summary>
  492. /// <param name="popup"></param>
  493. /// <param name="target"></param>
  494. /// <param name="dir"></param>
  495. /// <param name="closeUntilUpEvent"></param>
  496. public void TogglePopup(GObject popup, GObject target, PopupDirection dir, bool closeUntilUpEvent)
  497. {
  498. if (_justClosedPopups.IndexOf(popup) != -1)
  499. return;
  500. ShowPopup(popup, target, dir, closeUntilUpEvent);
  501. }
  502. /// <summary>
  503. /// Close all popups.
  504. /// </summary>
  505. public void HidePopup()
  506. {
  507. HidePopup(null);
  508. }
  509. /// <summary>
  510. /// Close a popup.
  511. /// </summary>
  512. /// <param name="popup"></param>
  513. public void HidePopup(GObject popup)
  514. {
  515. if (popup != null)
  516. {
  517. int k = _popupStack.IndexOf(popup);
  518. if (k != -1)
  519. {
  520. for (int i = _popupStack.Count - 1; i >= k; i--)
  521. {
  522. int last = _popupStack.Count - 1;
  523. GObject obj = _popupStack[last];
  524. ClosePopup(obj);
  525. _popupStack.RemoveAt(last);
  526. _specialPopups.Remove(obj);
  527. }
  528. }
  529. }
  530. else
  531. {
  532. foreach (GObject obj in _popupStack)
  533. ClosePopup(obj);
  534. _popupStack.Clear();
  535. _specialPopups.Clear();
  536. }
  537. }
  538. /// <summary>
  539. ///
  540. /// </summary>
  541. public bool hasAnyPopup
  542. {
  543. get { return _popupStack.Count > 0; }
  544. }
  545. void ClosePopup(GObject target)
  546. {
  547. if (target.parent != null)
  548. {
  549. if (target is Window)
  550. ((Window)target).Hide();
  551. else
  552. RemoveChild(target);
  553. }
  554. }
  555. /// <summary>
  556. ///
  557. /// </summary>
  558. /// <param name="msg"></param>
  559. public void ShowTooltips(string msg)
  560. {
  561. ShowTooltips(msg, 0.1f);
  562. }
  563. /// <summary>
  564. ///
  565. /// </summary>
  566. /// <param name="msg"></param>
  567. /// <param name="delay"></param>
  568. public void ShowTooltips(string msg, float delay)
  569. {
  570. if (_defaultTooltipWin == null || _defaultTooltipWin.isDisposed)
  571. {
  572. string resourceURL = UIConfig.tooltipsWin;
  573. if (string.IsNullOrEmpty(resourceURL))
  574. {
  575. Debug.LogWarning("FairyGUI: UIConfig.tooltipsWin not defined");
  576. return;
  577. }
  578. _defaultTooltipWin = UIPackage.CreateObjectFromURL(resourceURL);
  579. _defaultTooltipWin.SetHome(this);
  580. _defaultTooltipWin.touchable = false;
  581. }
  582. _defaultTooltipWin.text = msg;
  583. ShowTooltipsWin(_defaultTooltipWin, delay);
  584. }
  585. /// <summary>
  586. ///
  587. /// </summary>
  588. /// <param name="tooltipWin"></param>
  589. public void ShowTooltipsWin(GObject tooltipWin)
  590. {
  591. ShowTooltipsWin(tooltipWin, 0.1f);
  592. }
  593. /// <summary>
  594. ///
  595. /// </summary>
  596. /// <param name="tooltipWin"></param>
  597. /// <param name="delay"></param>
  598. public void ShowTooltipsWin(GObject tooltipWin, float delay)
  599. {
  600. HideTooltips();
  601. _tooltipWin = tooltipWin;
  602. Timers.inst.Add(delay, 1, __showTooltipsWin);
  603. }
  604. void __showTooltipsWin(object param)
  605. {
  606. if (_tooltipWin == null)
  607. return;
  608. float xx = Stage.inst.touchPosition.x + 10;
  609. float yy = Stage.inst.touchPosition.y + 20;
  610. Vector2 pt = this.GlobalToLocal(new Vector2(xx, yy));
  611. xx = pt.x;
  612. yy = pt.y;
  613. if (xx + _tooltipWin.width > this.width)
  614. xx = xx - _tooltipWin.width;
  615. if (yy + _tooltipWin.height > this.height)
  616. {
  617. yy = yy - _tooltipWin.height - 1;
  618. if (yy < 0)
  619. yy = 0;
  620. }
  621. _tooltipWin.x = Mathf.RoundToInt(xx);
  622. _tooltipWin.y = Mathf.RoundToInt(yy);
  623. AddChild(_tooltipWin);
  624. }
  625. /// <summary>
  626. ///
  627. /// </summary>
  628. public void HideTooltips()
  629. {
  630. if (_tooltipWin != null)
  631. {
  632. if (_tooltipWin.parent != null)
  633. RemoveChild(_tooltipWin);
  634. _tooltipWin = null;
  635. }
  636. }
  637. /// <summary>
  638. ///
  639. /// </summary>
  640. public GObject focus
  641. {
  642. get
  643. {
  644. GObject obj = DisplayObjectToGObject(Stage.inst.focus);
  645. if (obj != null && !IsAncestorOf(obj))
  646. return null;
  647. else
  648. return obj;
  649. }
  650. set
  651. {
  652. if (value == null)
  653. Stage.inst.focus = null;
  654. else
  655. Stage.inst.focus = value.displayObject;
  656. }
  657. }
  658. void __stageTouchBegin(EventContext context)
  659. {
  660. if (_tooltipWin != null)
  661. HideTooltips();
  662. CheckPopups(true);
  663. }
  664. void __stageTouchEnd(EventContext context)
  665. {
  666. CheckPopups(false);
  667. }
  668. void CheckPopups(bool touchBegin)
  669. {
  670. if (touchBegin)
  671. _justClosedPopups.Clear();
  672. if (_popupStack.Count > 0)
  673. {
  674. DisplayObject mc = Stage.inst.touchTarget as DisplayObject;
  675. bool handled = false;
  676. while (mc != Stage.inst && mc != null)
  677. {
  678. if (mc.gOwner != null)
  679. {
  680. int k = _popupStack.IndexOf(mc.gOwner);
  681. if (k != -1)
  682. {
  683. for (int i = _popupStack.Count - 1; i > k; i--)
  684. {
  685. int last = _popupStack.Count - 1;
  686. GObject popup = _popupStack[last];
  687. if (touchBegin == _specialPopups.Contains(popup))
  688. continue;
  689. ClosePopup(popup);
  690. _justClosedPopups.Add(popup);
  691. _popupStack.RemoveAt(last);
  692. _specialPopups.Remove(popup);
  693. }
  694. handled = true;
  695. break;
  696. }
  697. }
  698. mc = mc.parent;
  699. }
  700. if (!handled)
  701. {
  702. for (int i = _popupStack.Count - 1; i >= 0; i--)
  703. {
  704. GObject popup = _popupStack[i];
  705. if (touchBegin == _specialPopups.Contains(popup))
  706. continue;
  707. ClosePopup(popup);
  708. _justClosedPopups.Add(popup);
  709. _popupStack.RemoveAt(i);
  710. _specialPopups.Remove(popup);
  711. }
  712. }
  713. }
  714. }
  715. /// <summary>
  716. ///
  717. /// </summary>
  718. public void EnableSound()
  719. {
  720. Stage.inst.EnableSound();
  721. }
  722. /// <summary>
  723. ///
  724. /// </summary>
  725. public void DisableSound()
  726. {
  727. Stage.inst.DisableSound();
  728. }
  729. /// <summary>
  730. ///
  731. /// </summary>
  732. /// <param name="clip"></param>
  733. /// <param name="volumeScale"></param>
  734. public void PlayOneShotSound(AudioClip clip, float volumeScale)
  735. {
  736. Stage.inst.PlayOneShotSound(clip, volumeScale);
  737. }
  738. /// <summary>
  739. ///
  740. /// </summary>
  741. /// <param name="clip"></param>
  742. public void PlayOneShotSound(AudioClip clip)
  743. {
  744. Stage.inst.PlayOneShotSound(clip);
  745. }
  746. /// <summary>
  747. ///
  748. /// </summary>
  749. public float soundVolume
  750. {
  751. get { return Stage.inst.soundVolume; }
  752. set { Stage.inst.soundVolume = value; }
  753. }
  754. }
  755. }