1c6623815813aae756c57cca6f89a3182c222719.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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. var btn = evt.target.getComponent(Button);
  41. var clickEvents = btn.clickEvents;
  42. for (var i = 0; i < clickEvents.length; ++i) {
  43. var h = clickEvents[i];
  44. if (h.customEventData == customEventData) {
  45. var cb = h['$cb$'];
  46. var target = h['$target$'];
  47. var 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. var checkEvents = toggle.checkEvents; //if (toggle['_toggleContainer']) {
  58. // checkEvents = toggle['_toggleContainer'].checkEvents;
  59. //}
  60. for (var i = 0; i < checkEvents.length; ++i) {
  61. var h = checkEvents[i];
  62. if (h.customEventData == customEventData) {
  63. var cb = h['$cb$'];
  64. var target = h['$target$'];
  65. var 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 (var i = 0; i < this._uis.length; ++i) {
  91. var 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) {
  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. for (var _len = arguments.length, data = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
  167. data[_key - 2] = arguments[_key];
  168. }
  169. this.onCreated(...data); //check whether it has been destroyed, if has, hide it.
  170. //检查是否为已销毁,如果已销毁,则走销毁流程
  171. if (this._destroyed) this.close();
  172. }
  173. /**
  174. * @en hide and destroy this ui panel.
  175. * @zh 隐藏并销毁此UI面板
  176. * */
  177. close() {
  178. var _this$_resolve_close;
  179. (_this$_resolve_close = this._resolve_close) == null || _this$_resolve_close.call(this);
  180. this._resolve_close = null;
  181. this._destroyed = true;
  182. if (!this.node) return;
  183. this.node.removeFromParent();
  184. for (var i = 0; i < ui_base._uis.length; ++i) {
  185. if (ui_base._uis[i] == this) {
  186. ui_base._uis.splice(i, 1);
  187. break;
  188. }
  189. }
  190. this.onDispose();
  191. this.node.destroy();
  192. this.node = null;
  193. ui_base._removeCls(this._cls);
  194. this._cls = null;
  195. }
  196. closeAndRelease() {
  197. var _assetManager$getBund;
  198. this.close();
  199. (_assetManager$getBund = assetManager.getBundle(this._bundle)) == null || _assetManager$getBund.release(this._prefab);
  200. }
  201. /**等待此ui关闭*/
  202. wait_close() {
  203. if (this._resolve_close) return;
  204. return new Promise(resolve => {
  205. this._resolve_close = resolve;
  206. });
  207. }
  208. /**
  209. * @en add button event handler
  210. * @zh 添加按钮事件
  211. * @param relativeNodePath to indicate a button node, can pass `string`|`Node`|`Button` here.
  212. * @param cb will be called when event emits. method format:(btn:Button,args:any)=>void
  213. * @param target the `this` argument of `cb`
  214. * */
  215. onButtonEvent(relativeNodePath, cb, target, args) {
  216. var buttonNode = null;
  217. if (relativeNodePath instanceof Node) {
  218. buttonNode = relativeNodePath;
  219. } else if (relativeNodePath instanceof Button) {
  220. buttonNode = relativeNodePath.node;
  221. } else {
  222. buttonNode = find(relativeNodePath, this.node);
  223. }
  224. if (!buttonNode) {
  225. return null;
  226. } //添加转发器
  227. var agent = this.node.getComponent(__NodeEventAgent__);
  228. if (!agent) {
  229. agent = this.node.addComponent(__NodeEventAgent__);
  230. }
  231. var btn = buttonNode.getComponent(Button);
  232. var clickEvents = btn.clickEvents;
  233. var handler = new EventHandler();
  234. handler.target = this.node;
  235. handler.component = 'tgxNodeEventAgent';
  236. handler.handler = 'onButtonClicked';
  237. handler.customEventData = '' + _id++; //附加额外信息 供事件转发使用
  238. handler['$cb$'] = cb;
  239. handler['$target$'] = target;
  240. handler['$args$'] = args;
  241. clickEvents.push(handler);
  242. btn.clickEvents = clickEvents;
  243. }
  244. /**
  245. * @en remove button event handler
  246. * @zh 移除按钮事件
  247. * @param relativeNodePath to indicate a button node, can pass `string`|`Node`|`Button` here.
  248. * @param cb will be called when event emits.
  249. * @param target the `this` argument of `cb`
  250. * */
  251. offButtonEvent(relativeNodePath, cb, target) {
  252. var buttonNode = null;
  253. if (relativeNodePath instanceof Node) {
  254. buttonNode = relativeNodePath;
  255. } else if (relativeNodePath instanceof Button) {
  256. buttonNode = relativeNodePath.node;
  257. } else {
  258. buttonNode = find(relativeNodePath, this.node);
  259. }
  260. if (!buttonNode) {
  261. return;
  262. "";
  263. }
  264. var agent = this.node.getComponent(__NodeEventAgent__);
  265. if (!agent) {
  266. return;
  267. }
  268. var btn = buttonNode.getComponent(Button);
  269. if (!btn) {
  270. return;
  271. }
  272. var clickEvents = btn.clickEvents;
  273. for (var i = 0; i < clickEvents.length; ++i) {
  274. var h = clickEvents[i];
  275. if (h['$cb$'] == cb && h['$target$'] == target) {
  276. clickEvents.splice(i, 1);
  277. btn.clickEvents = clickEvents;
  278. break;
  279. }
  280. }
  281. }
  282. /**
  283. * @en add toggle event handler
  284. * @zh 添加Toggle事件
  285. * @param relativeNodePath to indicate a button node, can pass `string`|`Node`|`Button` here.
  286. * @param cb will be called when event emits. method format:(btn:Toggle,args:any)=>void
  287. * @param target the `this` argument of `cb`
  288. */
  289. onToggleEvent(relativeNodePath, cb, target, args) {
  290. var buttonNode = null;
  291. if (relativeNodePath instanceof Node) {
  292. buttonNode = relativeNodePath;
  293. } else if (relativeNodePath instanceof Toggle) {
  294. buttonNode = relativeNodePath.node;
  295. } else if (relativeNodePath instanceof ToggleContainer) {
  296. buttonNode = relativeNodePath.node;
  297. } else {
  298. buttonNode = find(relativeNodePath, this.node);
  299. }
  300. if (!buttonNode) {
  301. return null;
  302. } //添加转发器
  303. var agent = this.node.getComponent(__NodeEventAgent__);
  304. if (!agent) agent = this.node.addComponent(__NodeEventAgent__);
  305. var btn = buttonNode.getComponent(Toggle);
  306. if (!btn) btn = buttonNode.getComponent(ToggleContainer);
  307. var checkEvents = btn.checkEvents;
  308. var handler = new EventHandler();
  309. handler.target = this.node;
  310. handler.component = 'tgxNodeEventAgent';
  311. handler.handler = 'onToggleEvent';
  312. handler.customEventData = '' + _id++; //附加额外信息 供事件转发使用
  313. handler['$cb$'] = cb;
  314. handler['$target$'] = target;
  315. handler['$args$'] = args;
  316. checkEvents.push(handler);
  317. btn.checkEvents = checkEvents;
  318. }
  319. /**
  320. * @en remove toggle event handler
  321. * @zh 移除Toggle事件
  322. * @param relativeNodePath to indicate a button node, can pass `string`|`Node`|`Button` here.
  323. * @param cb will be called when event emits. method format:(btn:Toggle,args:any)=>void
  324. * @param target the `this` argument of `cb`
  325. * */
  326. offToggleEvent(relativeNodePath, cb, target) {
  327. var buttonNode = null;
  328. if (relativeNodePath instanceof Node) {
  329. buttonNode = relativeNodePath;
  330. } else if (relativeNodePath instanceof Toggle) {
  331. buttonNode = relativeNodePath.node;
  332. } else if (relativeNodePath instanceof ToggleContainer) {
  333. buttonNode = relativeNodePath.node;
  334. } else {
  335. buttonNode = find(relativeNodePath, this.node);
  336. }
  337. if (!buttonNode) {
  338. return null;
  339. } //添加转发器
  340. var agent = this.node.getComponent(__NodeEventAgent__);
  341. if (!agent) {
  342. return;
  343. }
  344. var btn = buttonNode.getComponent(Toggle);
  345. if (!btn) {
  346. btn = buttonNode.getComponent(ToggleContainer);
  347. }
  348. var checkEvents = btn.checkEvents;
  349. for (var i = 0; i < checkEvents.length; ++i) {
  350. var h = checkEvents[i];
  351. if (h['$cb$'] == cb && h['$target$'] == target) {
  352. checkEvents.splice(i, 1);
  353. btn.checkEvents = checkEvents;
  354. break;
  355. }
  356. }
  357. } //子类的所有操作,需要在这个函数之后。
  358. onCreated() {} //当界面销毁时调用
  359. onDispose() {} //
  360. onUpdate(dt) {}
  361. });
  362. ui_base._clss = new Set();
  363. ui_base._uis = [];
  364. _cclegacy._RF.pop();
  365. _crd = false;
  366. }
  367. };
  368. });
  369. //# sourceMappingURL=1c6623815813aae756c57cca6f89a3182c222719.js.map