1c6623815813aae756c57cca6f89a3182c222719.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. System.register(["cc"], function (_export, _context) {
  2. "use strict";
  3. var _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, assetManager, Button, Component, EventHandler, find, isValid, Node, Toggle, ToggleContainer, ui_base, _dec, _class, _crd, ccclass, property, __NodeEventAgent__, _id;
  4. _export("default", void 0);
  5. return {
  6. setters: [function (_cc) {
  7. _cclegacy = _cc.cclegacy;
  8. __checkObsolete__ = _cc.__checkObsolete__;
  9. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  10. _decorator = _cc._decorator;
  11. assetManager = _cc.assetManager;
  12. Button = _cc.Button;
  13. Component = _cc.Component;
  14. EventHandler = _cc.EventHandler;
  15. find = _cc.find;
  16. isValid = _cc.isValid;
  17. Node = _cc.Node;
  18. Toggle = _cc.Toggle;
  19. ToggleContainer = _cc.ToggleContainer;
  20. }],
  21. execute: function () {
  22. _crd = true;
  23. _cclegacy._RF.push({}, "a34ebvwnv5E2LG2J8RHwXU2", "ui_base", undefined);
  24. __checkObsolete__(['_decorator', 'assetManager', 'Button', 'Component', 'EventHandler', 'EventTouch', 'find', 'isValid', 'Node', 'Prefab', 'Toggle', 'ToggleContainer']);
  25. ({
  26. ccclass,
  27. property
  28. } = _decorator);
  29. /***
  30. * @en internal class, used for handling node event.
  31. * @zh 内部类,用于节点事件监听
  32. *
  33. * */
  34. __NodeEventAgent__ = (_dec = ccclass('tgxNodeEventAgent'), _dec(_class = class __NodeEventAgent__ extends Component {
  35. /***
  36. * @en recieve button click event and deliver them to the real handlers.
  37. * @zh 接受按钮事件,并转发给真正的处理函数
  38. * */
  39. onButtonClicked(evt, customEventData) {
  40. let btn = evt.target.getComponent(Button);
  41. let clickEvents = btn.clickEvents;
  42. for (let i = 0; i < clickEvents.length; ++i) {
  43. let h = clickEvents[i];
  44. if (h.customEventData == customEventData) {
  45. let cb = h['$cb$'];
  46. let target = h['$target$'];
  47. let args = h['$args$'];
  48. cb.apply(target, [btn, args]);
  49. }
  50. }
  51. }
  52. /***
  53. * @en recieve toggle event and deliver them to the real handlers.
  54. * @zh 接受Toggle事件,并转发给真正的处理函数
  55. * */
  56. onToggleEvent(toggle, customEventData) {
  57. let checkEvents = toggle.checkEvents; //if (toggle['_toggleContainer']) {
  58. // checkEvents = toggle['_toggleContainer'].checkEvents;
  59. //}
  60. for (let i = 0; i < checkEvents.length; ++i) {
  61. let h = checkEvents[i];
  62. if (h.customEventData == customEventData) {
  63. let cb = h['$cb$'];
  64. let target = h['$target$'];
  65. let args = h['$args$'];
  66. cb.apply(target, [toggle, args]);
  67. }
  68. }
  69. }
  70. }) || _class);
  71. _id = 0;
  72. _export("default", ui_base = class ui_base {
  73. /***
  74. * @en hide and destroy all ui panel.
  75. * @zh 隐藏并销毁所有UI面板
  76. * */
  77. static closeAll() {
  78. while (this._uis.length) {
  79. this._uis[0].close();
  80. }
  81. this._clss.clear();
  82. } //
  83. static closeAndReleaseAll() {
  84. while (this._uis.length) {
  85. this._uis[0].closeAndRelease();
  86. }
  87. this._clss.clear();
  88. } //update all ui, called by UI.
  89. static updateAll(dt) {
  90. for (let i = 0; i < this._uis.length; ++i) {
  91. let ctrl = this._uis[i];
  92. if (ctrl.node && isValid(ctrl.node)) {
  93. this._uis[i].onUpdate(dt);
  94. }
  95. }
  96. }
  97. static _addCls(cls) {
  98. if (!cls) return;
  99. this._clss.add(cls);
  100. }
  101. static _removeCls(cls) {
  102. if (!cls) return;
  103. this._clss.delete(cls);
  104. }
  105. static _hasCls(cls) {
  106. return this._clss.has(cls);
  107. }
  108. /***
  109. * @en the instance id to indicate an unique ui panel.
  110. * @zh 实例ID,用于标记一个唯一面板实例
  111. * */
  112. get id() {
  113. return this._id;
  114. }
  115. /***
  116. * @en url of the prefab used by this ui panel.
  117. * @zh 本UI使用prefab路径
  118. * */
  119. get prefab() {
  120. return this._prefab;
  121. }
  122. get bundle() {
  123. return this._bundle;
  124. }
  125. /***
  126. * @en layer of this ui panel.
  127. * @zh 本UI所在的UI层级
  128. * */
  129. get layer() {
  130. return this._layer;
  131. }
  132. /***
  133. * @en layout of this ui panel.
  134. * @zh 本UI组件
  135. * */
  136. get layout() {
  137. return this._layout;
  138. }
  139. getLayout() {
  140. return this._layout;
  141. }
  142. constructor(bundle, prefab, layer, layoutCls) {
  143. this._id = 0;
  144. this._bundle = void 0;
  145. this._prefab = void 0;
  146. this._layer = void 0;
  147. this._layout = void 0;
  148. this._cls = void 0;
  149. this.node = void 0;
  150. this._destroyed = false;
  151. this._resolve_close = null;
  152. this._cls = null;
  153. this._bundle = bundle;
  154. this._prefab = prefab;
  155. this._layer = layer;
  156. this._layout = layoutCls;
  157. this._id = _id++;
  158. } //setup this ui,called by UIMgr.
  159. _setup(cls, node, ...data) {
  160. ui_base._uis.push(this);
  161. this._cls = cls;
  162. ui_base._addCls(this._cls);
  163. this.node = node;
  164. if (this._layout) this._layout = this.node.getComponent(this._layout); //notify sub class to handle something.
  165. //节点创建完毕,调用子类的处理函数。
  166. this.onCreated(...data); //check whether it has been destroyed, if has, hide it.
  167. //检查是否为已销毁,如果已销毁,则走销毁流程
  168. if (this._destroyed) this.close();
  169. }
  170. /**
  171. * @en hide and destroy this ui panel.
  172. * @zh 隐藏并销毁此UI面板
  173. * */
  174. close() {
  175. var _this$_resolve_close;
  176. (_this$_resolve_close = this._resolve_close) == null || _this$_resolve_close.call(this);
  177. this._resolve_close = null;
  178. this._destroyed = true;
  179. if (!this.node) return;
  180. this.node.removeFromParent();
  181. for (let i = 0; i < ui_base._uis.length; ++i) {
  182. if (ui_base._uis[i] == this) {
  183. ui_base._uis.splice(i, 1);
  184. break;
  185. }
  186. }
  187. this.onDispose();
  188. this.node.destroy();
  189. this.node = null;
  190. ui_base._removeCls(this._cls);
  191. this._cls = null;
  192. }
  193. closeAndRelease() {
  194. var _assetManager$getBund;
  195. this.close();
  196. (_assetManager$getBund = assetManager.getBundle(this._bundle)) == null || _assetManager$getBund.release(this._prefab);
  197. }
  198. /**等待此ui关闭*/
  199. wait_close() {
  200. if (this._resolve_close) return;
  201. return new Promise(resolve => {
  202. this._resolve_close = resolve;
  203. });
  204. }
  205. /**
  206. * @en add button event handler
  207. * @zh 添加按钮事件
  208. * @param relativeNodePath to indicate a button node, can pass `string`|`Node`|`Button` here.
  209. * @param cb will be called when event emits. method format:(btn:Button,args:any)=>void
  210. * @param target the `this` argument of `cb`
  211. * */
  212. onButtonEvent(relativeNodePath, cb, target, args) {
  213. let buttonNode = null;
  214. if (relativeNodePath instanceof Node) {
  215. buttonNode = relativeNodePath;
  216. } else if (relativeNodePath instanceof Button) {
  217. buttonNode = relativeNodePath.node;
  218. } else {
  219. buttonNode = find(relativeNodePath, this.node);
  220. }
  221. if (!buttonNode) {
  222. return null;
  223. } //添加转发器
  224. let agent = this.node.getComponent(__NodeEventAgent__);
  225. if (!agent) {
  226. agent = this.node.addComponent(__NodeEventAgent__);
  227. }
  228. let btn = buttonNode.getComponent(Button);
  229. let clickEvents = btn.clickEvents;
  230. let handler = new EventHandler();
  231. handler.target = this.node;
  232. handler.component = 'tgxNodeEventAgent';
  233. handler.handler = 'onButtonClicked';
  234. handler.customEventData = '' + _id++; //附加额外信息 供事件转发使用
  235. handler['$cb$'] = cb;
  236. handler['$target$'] = target;
  237. handler['$args$'] = args;
  238. clickEvents.push(handler);
  239. btn.clickEvents = clickEvents;
  240. }
  241. /**
  242. * @en remove button event handler
  243. * @zh 移除按钮事件
  244. * @param relativeNodePath to indicate a button node, can pass `string`|`Node`|`Button` here.
  245. * @param cb will be called when event emits.
  246. * @param target the `this` argument of `cb`
  247. * */
  248. offButtonEvent(relativeNodePath, cb, target) {
  249. let buttonNode = null;
  250. if (relativeNodePath instanceof Node) {
  251. buttonNode = relativeNodePath;
  252. } else if (relativeNodePath instanceof Button) {
  253. buttonNode = relativeNodePath.node;
  254. } else {
  255. buttonNode = find(relativeNodePath, this.node);
  256. }
  257. if (!buttonNode) {
  258. return;
  259. ``;
  260. }
  261. let agent = this.node.getComponent(__NodeEventAgent__);
  262. if (!agent) {
  263. return;
  264. }
  265. let btn = buttonNode.getComponent(Button);
  266. if (!btn) {
  267. return;
  268. }
  269. let clickEvents = btn.clickEvents;
  270. for (let i = 0; i < clickEvents.length; ++i) {
  271. let h = clickEvents[i];
  272. if (h['$cb$'] == cb && h['$target$'] == target) {
  273. clickEvents.splice(i, 1);
  274. btn.clickEvents = clickEvents;
  275. break;
  276. }
  277. }
  278. }
  279. /**
  280. * @en add toggle event handler
  281. * @zh 添加Toggle事件
  282. * @param relativeNodePath to indicate a button node, can pass `string`|`Node`|`Button` here.
  283. * @param cb will be called when event emits. method format:(btn:Toggle,args:any)=>void
  284. * @param target the `this` argument of `cb`
  285. */
  286. onToggleEvent(relativeNodePath, cb, target, args) {
  287. let buttonNode = null;
  288. if (relativeNodePath instanceof Node) {
  289. buttonNode = relativeNodePath;
  290. } else if (relativeNodePath instanceof Toggle) {
  291. buttonNode = relativeNodePath.node;
  292. } else if (relativeNodePath instanceof ToggleContainer) {
  293. buttonNode = relativeNodePath.node;
  294. } else {
  295. buttonNode = find(relativeNodePath, this.node);
  296. }
  297. if (!buttonNode) {
  298. return null;
  299. } //添加转发器
  300. let agent = this.node.getComponent(__NodeEventAgent__);
  301. if (!agent) agent = this.node.addComponent(__NodeEventAgent__);
  302. let btn = buttonNode.getComponent(Toggle);
  303. if (!btn) btn = buttonNode.getComponent(ToggleContainer);
  304. let checkEvents = btn.checkEvents;
  305. let handler = new EventHandler();
  306. handler.target = this.node;
  307. handler.component = 'tgxNodeEventAgent';
  308. handler.handler = 'onToggleEvent';
  309. handler.customEventData = '' + _id++; //附加额外信息 供事件转发使用
  310. handler['$cb$'] = cb;
  311. handler['$target$'] = target;
  312. handler['$args$'] = args;
  313. checkEvents.push(handler);
  314. btn.checkEvents = checkEvents;
  315. }
  316. /**
  317. * @en remove toggle event handler
  318. * @zh 移除Toggle事件
  319. * @param relativeNodePath to indicate a button node, can pass `string`|`Node`|`Button` here.
  320. * @param cb will be called when event emits. method format:(btn:Toggle,args:any)=>void
  321. * @param target the `this` argument of `cb`
  322. * */
  323. offToggleEvent(relativeNodePath, cb, target) {
  324. let buttonNode = null;
  325. if (relativeNodePath instanceof Node) {
  326. buttonNode = relativeNodePath;
  327. } else if (relativeNodePath instanceof Toggle) {
  328. buttonNode = relativeNodePath.node;
  329. } else if (relativeNodePath instanceof ToggleContainer) {
  330. buttonNode = relativeNodePath.node;
  331. } else {
  332. buttonNode = find(relativeNodePath, this.node);
  333. }
  334. if (!buttonNode) {
  335. return null;
  336. } //添加转发器
  337. let agent = this.node.getComponent(__NodeEventAgent__);
  338. if (!agent) {
  339. return;
  340. }
  341. let btn = buttonNode.getComponent(Toggle);
  342. if (!btn) {
  343. btn = buttonNode.getComponent(ToggleContainer);
  344. }
  345. let checkEvents = btn.checkEvents;
  346. for (let i = 0; i < checkEvents.length; ++i) {
  347. let h = checkEvents[i];
  348. if (h['$cb$'] == cb && h['$target$'] == target) {
  349. checkEvents.splice(i, 1);
  350. btn.checkEvents = checkEvents;
  351. break;
  352. }
  353. }
  354. } //子类的所有操作,需要在这个函数之后。
  355. onCreated(...data) {} //当界面销毁时调用
  356. onDispose() {} //
  357. onUpdate(dt) {}
  358. });
  359. ui_base._clss = new Set();
  360. ui_base._uis = [];
  361. _cclegacy._RF.pop();
  362. _crd = false;
  363. }
  364. };
  365. });
  366. //# sourceMappingURL=1c6623815813aae756c57cca6f89a3182c222719.js.map