e2c07fd606d93eb79afeed6ef4de521f2be03414.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529
  1. System.register(["__unresolved_0", "cc", "__unresolved_1", "__unresolved_2"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, _decorator, Component, director, error, instantiate, Node, UITransform, Widget, assetManager, tween, v3, UIOpacity, Vec3, Tween, ui_base, ResolutionAutoFit, ui, _dec, _class, _crd, ccclass, property, ui_updater, GameUILayers, gui;
  4. function _reportPossibleCrUseOfui_base(extras) {
  5. _reporterNs.report("ui_base", "./ui_base", _context.meta, extras);
  6. }
  7. function _reportPossibleCrUseOfResolutionAutoFit(extras) {
  8. _reporterNs.report("ResolutionAutoFit", "./ui_ResolutionAutoFit", _context.meta, extras);
  9. }
  10. return {
  11. setters: [function (_unresolved_) {
  12. _reporterNs = _unresolved_;
  13. }, function (_cc) {
  14. _cclegacy = _cc.cclegacy;
  15. __checkObsolete__ = _cc.__checkObsolete__;
  16. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  17. _decorator = _cc._decorator;
  18. Component = _cc.Component;
  19. director = _cc.director;
  20. error = _cc.error;
  21. instantiate = _cc.instantiate;
  22. Node = _cc.Node;
  23. UITransform = _cc.UITransform;
  24. Widget = _cc.Widget;
  25. assetManager = _cc.assetManager;
  26. tween = _cc.tween;
  27. v3 = _cc.v3;
  28. UIOpacity = _cc.UIOpacity;
  29. Vec3 = _cc.Vec3;
  30. Tween = _cc.Tween;
  31. }, function (_unresolved_2) {
  32. ui_base = _unresolved_2.default;
  33. }, function (_unresolved_3) {
  34. ResolutionAutoFit = _unresolved_3.ResolutionAutoFit;
  35. }],
  36. execute: function () {
  37. _crd = true;
  38. _cclegacy._RF.push({}, "81e48wxjGBMPp2rQOJDljTG", "ui", undefined);
  39. __checkObsolete__(['_decorator', 'Component', 'director', 'error', 'instantiate', 'Node', 'Prefab', 'UITransform', 'Widget', 'assetManager', 'AssetManager', 'tween', 'v3', 'UIOpacity', 'Vec3', 'ProgressBar', 'Sprite', 'Tween']);
  40. ({
  41. ccclass,
  42. property
  43. } = _decorator);
  44. ui_updater = (_dec = ccclass('ui_updater'), _dec(_class = class ui_updater extends Component {
  45. update(dt) {
  46. (_crd && ui_base === void 0 ? (_reportPossibleCrUseOfui_base({
  47. error: Error()
  48. }), ui_base) : ui_base).updateAll(dt);
  49. }
  50. }) || _class);
  51. _export("GameUILayers", GameUILayers = /*#__PURE__*/function (GameUILayers) {
  52. GameUILayers[GameUILayers["GAME"] = 0] = "GAME";
  53. GameUILayers[GameUILayers["JOY_STICK"] = 1] = "JOY_STICK";
  54. GameUILayers[GameUILayers["HUD"] = 2] = "HUD";
  55. GameUILayers[GameUILayers["POPUP"] = 3] = "POPUP";
  56. GameUILayers[GameUILayers["ALERT"] = 4] = "ALERT";
  57. GameUILayers[GameUILayers["NOTICE"] = 5] = "NOTICE";
  58. GameUILayers[GameUILayers["LOADING"] = 6] = "LOADING";
  59. GameUILayers[GameUILayers["OVERLAY"] = 7] = "OVERLAY";
  60. return GameUILayers;
  61. }({}));
  62. /**ui管理模块*/
  63. ui = class ui {
  64. constructor() {
  65. this._uiCanvas = void 0;
  66. this._uiRoot = void 0;
  67. this._clss_loading = new Set();
  68. }
  69. static getInstance() {
  70. if (!this._instance) this._instance = new ui();
  71. return this._instance;
  72. }
  73. delay(ms) {
  74. return new Promise(resolve => setTimeout(resolve, ms));
  75. }
  76. delay_second(s) {
  77. return new Promise(resolve => setTimeout(resolve, s * 1000));
  78. }
  79. progressBar_anim(bar, progress, duration = 0.2) {
  80. if (bar.progress >= progress) {
  81. bar.progress = progress;
  82. return;
  83. } else {
  84. return new Promise(resolve => tween(bar).to(duration, {
  85. progress: progress
  86. }).call(() => {
  87. resolve();
  88. }).start());
  89. }
  90. }
  91. fillRange_anim(sp, progress, duration = 0.2, limt = 0) {
  92. if (limt > 0 && sp.fillRange >= progress && limt < 0 && sp.fillRange <= progress) {
  93. sp.fillRange = progress;
  94. return;
  95. } else {
  96. return new Promise(resolve => tween(sp).to(duration, {
  97. fillRange: progress
  98. }).call(() => {
  99. resolve();
  100. }).start());
  101. }
  102. }
  103. scale_anim(node, duration = 0.2, start = 0.5, end = 1) {
  104. node.setScale(start, start, 1);
  105. return new Promise(resolve => tween(node).to(duration, {
  106. scale: v3(end, end, 1)
  107. }).call(() => {
  108. resolve();
  109. }).start());
  110. }
  111. scale_elasticOut_anim(node, duration = 0.2, start = 0.5, end = 1) {
  112. node.setScale(start, start, 1);
  113. return new Promise(resolve => tween(node).to(duration, {
  114. scale: v3(end, end, 1)
  115. }, {
  116. easing: 'elasticOut'
  117. }).call(() => {
  118. resolve();
  119. }).start());
  120. }
  121. fade_anim(node, duration = 0.2, startOpacity = 0, targetOpacity = 255) {
  122. var _node$getComponent;
  123. const opacity = (_node$getComponent = node.getComponent(UIOpacity)) != null ? _node$getComponent : node.addComponent(UIOpacity);
  124. opacity.opacity = startOpacity;
  125. return new Promise(resolve => {
  126. tween(opacity).to(duration, {
  127. opacity: targetOpacity
  128. }).call(() => {
  129. resolve();
  130. }).start();
  131. });
  132. }
  133. shake_anim(node, duration = 0.2, intensity = 5, shakeCount = 7) {
  134. return new Promise(resolve => {
  135. const originalPosition = node.position.clone(); // 保存原始位置
  136. const shakeDuration = duration / (shakeCount * 2); // 每次抖动的持续时间
  137. const shakeTween = tween(node);
  138. for (let i = 0; i < shakeCount; i++) {
  139. const offsetX = i % 2 === 0 ? intensity : -intensity; // 交替偏移
  140. shakeTween.to(shakeDuration, {
  141. position: v3(originalPosition.x + offsetX, originalPosition.y, originalPosition.z)
  142. }).to(shakeDuration, {
  143. position: originalPosition
  144. }); // 回到原位
  145. }
  146. shakeTween.call(() => {
  147. resolve();
  148. }).start(); // 开始抖动动画
  149. });
  150. }
  151. rotate_shake_anim(node, duration = 0.3, intensity = 5, shakeCount = 5) {
  152. return new Promise(resolve => {
  153. const originalAngle = node.angle; // 保存原始角度
  154. const shakeDuration = duration / (shakeCount * 2); // 每次晃动的持续时间
  155. const shakeTween = tween(node);
  156. for (let i = 0; i < shakeCount; i++) {
  157. const offsetAngle = i % 2 === 0 ? intensity : -intensity; // 交替旋转
  158. shakeTween.to(shakeDuration, {
  159. angle: originalAngle + offsetAngle
  160. }).to(shakeDuration, {
  161. angle: originalAngle
  162. }); // 回到原位
  163. }
  164. shakeTween.call(() => {
  165. resolve();
  166. }).start(); // 开始角度晃动动画
  167. });
  168. }
  169. scale_shake_anim(node, duration = 0.3, intensity = 0.1, shakeCount = 10, reset = null) {
  170. if (reset) {
  171. Tween.stopAllByTarget(node);
  172. node.setScale(reset, reset, reset);
  173. }
  174. return new Promise(resolve => {
  175. const originalScale = node.scale.clone(); // 保存原始缩放
  176. const shakeDuration = duration / (shakeCount * 2); // 每次震动的持续时间
  177. const shakeTween = tween(node);
  178. for (let i = 0; i < shakeCount; i++) {
  179. const offsetScale = i % 2 === 0 ? intensity : -intensity; // 交替缩放
  180. shakeTween.to(shakeDuration, {
  181. scale: v3(originalScale.x + offsetScale, originalScale.y + offsetScale, originalScale.z)
  182. }).to(shakeDuration, {
  183. scale: originalScale
  184. }); // 回到原位
  185. }
  186. shakeTween.call(() => {
  187. resolve();
  188. }).start(); // 开始缩放震动动画
  189. });
  190. }
  191. opacity_shake_anim(node, duration = 0.8, intensity = 80, shakeCount = 5) {
  192. var _node$getComponent2;
  193. const opacity = (_node$getComponent2 = node.getComponent(UIOpacity)) != null ? _node$getComponent2 : node.addComponent(UIOpacity);
  194. const originalOpacity = opacity.opacity;
  195. const shakeDuration = duration / (shakeCount * 2);
  196. return new Promise(resolve => {
  197. const shakeTween = tween(opacity);
  198. for (let i = 0; i < shakeCount; i++) {
  199. const offsetOpacity = i % 2 === 0 ? intensity : -intensity;
  200. shakeTween.to(shakeDuration, {
  201. opacity: Math.min(Math.max(originalOpacity + offsetOpacity, 0), 255)
  202. }).to(shakeDuration, {
  203. opacity: originalOpacity
  204. });
  205. }
  206. shakeTween.call(() => {
  207. resolve();
  208. }).start(); // 开始透明度震动动画
  209. });
  210. }
  211. /**处理弹跳动画*/
  212. bounce_anim(node, height = 100, duration = 0.5) {
  213. return new Promise(resolve => {
  214. tween(node).to(duration, {
  215. position: node.position.clone().add(new Vec3(0, height, 0))
  216. }, {
  217. easing: 'bounceOut'
  218. }) // 向上弹跳
  219. .to(duration, {
  220. position: node.position
  221. }, {
  222. easing: 'bounceIn'
  223. }) // 回到原位
  224. .call(() => {
  225. resolve();
  226. }) //完成动画
  227. .start(); //开始动画
  228. });
  229. }
  230. /**
  231. * 奖励物飞行动画
  232. * @param node 奖励物节点
  233. * @param startPos 起始爆开点
  234. * @param targetPos 最终目标点
  235. * @param explosionDistance 爆开距离
  236. * @param explosionDuration 爆开时间
  237. * @param stayDuration 停留时间
  238. * @param moveDuration 移动到目标的时间
  239. * @returns
  240. */
  241. reward_fly_anim(node, startPos, targetPos, explosionDistance = 100, explosionDuration = 0.3, stayDuration = 0.5, moveDuration = 0.3) {
  242. node.setPosition(startPos);
  243. return new Promise(resolve => {
  244. //随机方向
  245. const randomDirection = new Vec3(Math.random() * 2 - 1, Math.random() * 2 - 1, 0).normalize();
  246. const explosionEndPos = startPos.add(randomDirection.multiplyScalar(explosionDistance)); // 爆炸效果的位置
  247. tween(node).to(explosionDuration, {
  248. worldPosition: explosionEndPos
  249. }) // 爆炸动画
  250. .delay(stayDuration) //停留时间
  251. .to(moveDuration, {
  252. worldPosition: targetPos
  253. }, {
  254. easing: 'cubicIn'
  255. }).call(() => {
  256. resolve();
  257. }).start();
  258. });
  259. }
  260. createFullScreenNode() {
  261. let canvas = this._uiCanvas.getComponent(UITransform);
  262. let node = new Node();
  263. node.layer = this._uiCanvas.layer;
  264. let uiTransform = node.addComponent(UITransform);
  265. uiTransform.width = canvas.width;
  266. uiTransform.height = canvas.height;
  267. let widget = node.addComponent(Widget);
  268. widget.isAlignBottom = true;
  269. widget.isAlignTop = true;
  270. widget.isAlignLeft = true;
  271. widget.isAlignRight = true;
  272. widget.left = 0;
  273. widget.right = 0;
  274. widget.top = 0;
  275. widget.bottom = 0;
  276. return node;
  277. }
  278. getLayers() {
  279. return Object.values(GameUILayers).filter(value => typeof value === 'number');
  280. }
  281. /**
  282. * @en init,`don't call more than once`.
  283. * @zh 初始化UIMgr,`不要多次调用`
  284. * */
  285. init(uiCanvas) {
  286. if (this._uiCanvas) return;
  287. if (!uiCanvas) throw error('uiCanvas must be a Node or Prefab');
  288. if (uiCanvas instanceof Node) {
  289. this._uiCanvas = uiCanvas;
  290. } else {
  291. this._uiCanvas = instantiate(uiCanvas);
  292. director.getScene().addChild(this._uiCanvas);
  293. }
  294. this._uiCanvas.name = '__ui_canvas__';
  295. director.addPersistRootNode(this._uiCanvas);
  296. if (!this._uiCanvas.getComponent(ui_updater)) {
  297. this._uiCanvas.addComponent(ui_updater);
  298. }
  299. let canvas = this._uiCanvas.getComponent(UITransform);
  300. this._uiCanvas.addComponent(_crd && ResolutionAutoFit === void 0 ? (_reportPossibleCrUseOfResolutionAutoFit({
  301. error: Error()
  302. }), ResolutionAutoFit) : ResolutionAutoFit);
  303. this._uiRoot = this.createFullScreenNode();
  304. this._uiRoot.name = 'root';
  305. canvas.node.addChild(this._uiRoot);
  306. const layers = this.getLayers(); //create layers
  307. for (let i = 0; i < layers.length; ++i) {
  308. let layerNode = this.createFullScreenNode();
  309. layerNode.name = 'layer_' + GameUILayers[layers[i]];
  310. this._uiRoot.addChild(layerNode);
  311. }
  312. }
  313. /**获取层级节点*/
  314. getLayerNode(layerIndex) {
  315. return this._uiRoot.children[layerIndex] || this._uiRoot;
  316. }
  317. /**关闭所有界面*/
  318. closeAll() {
  319. (_crd && ui_base === void 0 ? (_reportPossibleCrUseOfui_base({
  320. error: Error()
  321. }), ui_base) : ui_base).closeAll();
  322. }
  323. /**关闭和释放所有界面 */
  324. closeAndReleaseAll() {
  325. (_crd && ui_base === void 0 ? (_reportPossibleCrUseOfui_base({
  326. error: Error()
  327. }), ui_base) : ui_base).closeAndReleaseAll();
  328. }
  329. /**关闭某个界面*/
  330. close(uiCls) {
  331. var _this$get;
  332. (_this$get = this.get(uiCls)) == null || _this$get.close();
  333. }
  334. /**获取界面*/
  335. get(uiCls) {
  336. let all = ui_base._uis;
  337. for (let i = 0; i < all.length; ++i) {
  338. let c = all[i];
  339. if (c instanceof uiCls) {
  340. return c;
  341. }
  342. }
  343. return null;
  344. }
  345. /**某个界面是否显示中*/
  346. isShowing(uiCls) {
  347. return ui_base._hasCls(uiCls);
  348. }
  349. /**是否有正在加载中的界面*/
  350. isLoading(uiCls) {
  351. if (!uiCls) return this._clss_loading.size > 0;
  352. return this._clss_loading.has(uiCls);
  353. }
  354. /***
  355. * @en show ui by the given parameters.
  356. * @zh 显示UI
  357. * @param uiCls the class, must inherits from the class `UIController`.
  358. * @returns the instance of `uiCls`
  359. * */
  360. async show(uiCls, ...data) {
  361. if (this.isLoading(uiCls)) return null;
  362. if (this.isShowing(uiCls)) return null;
  363. let ui = new uiCls();
  364. this._clss_loading.add(uiCls);
  365. let bundleName = ui.bundle;
  366. if (bundleName) {
  367. let bundle = assetManager.getBundle(bundleName);
  368. if (!bundle) {
  369. try {
  370. const loadedBundle = await this.loadBundleAsync(bundleName);
  371. return await this._create(loadedBundle, ui, uiCls, ...data);
  372. } catch (err) {
  373. console.error(err);
  374. this._clss_loading.delete(uiCls);
  375. }
  376. } else {
  377. return await this._create(bundle, ui, uiCls, ...data);
  378. }
  379. } else {
  380. this._clss_loading.delete(uiCls);
  381. console.error("ui no bundle name");
  382. return null;
  383. }
  384. }
  385. async _create(bundle, ui, uiCls, ...p) {
  386. try {
  387. const data = await this.loadPrefabAsync(bundle, ui.prefab);
  388. let node = instantiate(data);
  389. let parent = this.getLayerNode(ui.layer);
  390. parent.addChild(node);
  391. ui._setup(uiCls, node, ...p);
  392. this._clss_loading.delete(uiCls);
  393. return ui;
  394. } catch (err) {
  395. console.error(err);
  396. this._clss_loading.delete(uiCls);
  397. return;
  398. }
  399. }
  400. loadBundleAsync(bundleName) {
  401. return new Promise((resolve, reject) => {
  402. assetManager.loadBundle(bundleName, null, (err, loadedBundle) => {
  403. if (err) {
  404. reject(err);
  405. } else {
  406. resolve(loadedBundle);
  407. }
  408. });
  409. });
  410. }
  411. loadPrefabAsync(bundle, prefabName) {
  412. return new Promise((resolve, reject) => {
  413. bundle.load(prefabName, (err, data) => {
  414. if (err) {
  415. reject(err);
  416. } else {
  417. resolve(data);
  418. }
  419. });
  420. });
  421. }
  422. };
  423. ui._instance = void 0;
  424. _export("gui", gui = ui.getInstance());
  425. _export("ui_base", ui_base);
  426. _cclegacy._RF.pop();
  427. _crd = false;
  428. }
  429. };
  430. });
  431. //# sourceMappingURL=e2c07fd606d93eb79afeed6ef4de521f2be03414.js.map