f5e17874f550400748c63f17e937b5753c07e3b7.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. System.register(["__unresolved_0", "cc", "__unresolved_1"], function (_export, _context) {
  2. "use strict";
  3. var _reporterNs, _cclegacy, __checkObsolete__, __checkObsoleteInNamespace__, Animation, AnimationClip, instantiate, Prefab, Size, UITransform, v3, resLoader, ViewUtil, _crd;
  4. function _reportPossibleCrUseOfresLoader(extras) {
  5. _reporterNs.report("resLoader", "../common/loader/ResLoader", _context.meta, extras);
  6. }
  7. _export("ViewUtil", void 0);
  8. return {
  9. setters: [function (_unresolved_) {
  10. _reporterNs = _unresolved_;
  11. }, function (_cc) {
  12. _cclegacy = _cc.cclegacy;
  13. __checkObsolete__ = _cc.__checkObsolete__;
  14. __checkObsoleteInNamespace__ = _cc.__checkObsoleteInNamespace__;
  15. Animation = _cc.Animation;
  16. AnimationClip = _cc.AnimationClip;
  17. instantiate = _cc.instantiate;
  18. Prefab = _cc.Prefab;
  19. Size = _cc.Size;
  20. UITransform = _cc.UITransform;
  21. v3 = _cc.v3;
  22. }, function (_unresolved_2) {
  23. resLoader = _unresolved_2.resLoader;
  24. }],
  25. execute: function () {
  26. _crd = true;
  27. _cclegacy._RF.push({}, "f607cCjAEZHVKVZ/FyRs5bA", "ViewUtil", undefined);
  28. /*
  29. * @Author: dgflash
  30. * @Date: 2021-08-16 09:34:56
  31. * @LastEditors: dgflash
  32. * @LastEditTime: 2023-01-19 14:52:12
  33. */
  34. __checkObsolete__(['Animation', 'AnimationClip', 'EventTouch', 'instantiate', 'Node', 'Prefab', 'Size', 'UITransform', 'v3', 'Vec3']);
  35. /** 显示对象工具 */
  36. _export("ViewUtil", ViewUtil = class ViewUtil {
  37. /**
  38. * 把Node当前的节点树结构根据Node命名转成一个js对象,重名的组件会覆盖,
  39. * Node的name不应该包含空格键,否则将跳过
  40. * @param parent 被遍历的Node组件
  41. * @param obj 绑定的js对象 (可选)
  42. */
  43. static nodeTreeInfoLite(parent, obj) {
  44. let map = obj || new Map();
  45. let items = parent.children;
  46. for (let i = 0; i < items.length; i++) {
  47. let _node = items[i];
  48. if (_node.name.indexOf(" ") < 0) {
  49. map.set(_node.name, _node);
  50. }
  51. ViewUtil.nodeTreeInfoLite(_node, map);
  52. }
  53. return map;
  54. }
  55. /**
  56. * 正则搜索节点名字,符合条件的节点将会返回
  57. * @param reg 正则表达式
  58. * @param parent 要搜索的父节点
  59. * @param nodes 返回的数组(可选)
  60. */
  61. static findNodes(reg, parent, nodes) {
  62. let ns = nodes || [];
  63. let items = parent.children;
  64. for (let i = 0; i < items.length; i++) {
  65. let _name = items[i].name;
  66. if (reg.test(_name)) {
  67. ns.push(items[i]);
  68. }
  69. ViewUtil.findNodes(reg, items[i], ns);
  70. }
  71. return ns;
  72. }
  73. /**
  74. * 节点之间坐标互转
  75. * @param a A节点
  76. * @param b B节点
  77. * @param aPos A节点空间中的相对位置
  78. */
  79. static calculateASpaceToBSpacePos(a, b, aPos) {
  80. const world = a.getComponent(UITransform).convertToWorldSpaceAR(aPos);
  81. return b.getComponent(UITransform).convertToNodeSpaceAR(world);
  82. }
  83. /**
  84. * 屏幕转空间坐标
  85. * @param event 触摸事件
  86. * @param space 转到此节点的坐标空间
  87. */
  88. static calculateScreenPosToSpacePos(event, space) {
  89. const uil = event.getUILocation();
  90. const worldPos = v3(uil.x, uil.y);
  91. return space.getComponent(UITransform).convertToNodeSpaceAR(worldPos);
  92. }
  93. /**
  94. * 显示对象等比缩放
  95. * @param targetWidth 目标宽
  96. * @param targetHeight 目标高
  97. * @param defaultWidth 默认宽
  98. * @param defaultHeight 默认高
  99. */
  100. static uniformScale(targetWidth, targetHeight, defaultWidth, defaultHeight) {
  101. const widthRatio = defaultWidth / targetWidth;
  102. const heightRatio = defaultHeight / targetHeight;
  103. let ratio;
  104. widthRatio < heightRatio ? ratio = widthRatio : ratio = heightRatio;
  105. return new Size(Math.floor(targetWidth * ratio), Math.floor(targetHeight * ratio));
  106. }
  107. /**
  108. * 从资源缓存中找到预制资源名并创建一个显示对象(建议使用GameComponent里的同名方法,能自动管理内存施放)
  109. * @param path 资源路径
  110. * @param bundleName 资源包名
  111. */
  112. static createPrefabNode(path, bundleName = (_crd && resLoader === void 0 ? (_reportPossibleCrUseOfresLoader({
  113. error: Error()
  114. }), resLoader) : resLoader).defaultBundleName) {
  115. const p = (_crd && resLoader === void 0 ? (_reportPossibleCrUseOfresLoader({
  116. error: Error()
  117. }), resLoader) : resLoader).get(path, Prefab, bundleName);
  118. if (p) {
  119. return instantiate(p);
  120. }
  121. return null;
  122. }
  123. /**
  124. * 加载预制并创建预制节点(建议使用GameComponent里的同名方法,能自动管理内存施放)
  125. * @param path 资源路径
  126. * @param bundleName 资源包名
  127. */
  128. static createPrefabNodeAsync(path, bundleName = (_crd && resLoader === void 0 ? (_reportPossibleCrUseOfresLoader({
  129. error: Error()
  130. }), resLoader) : resLoader).defaultBundleName) {
  131. return new Promise(async (resolve, reject) => {
  132. const p = await (_crd && resLoader === void 0 ? (_reportPossibleCrUseOfresLoader({
  133. error: Error()
  134. }), resLoader) : resLoader).loadAsync(bundleName, path, Prefab);
  135. if (p) {
  136. resolve(instantiate(p));
  137. } else {
  138. console.error(`名为【${path}】的资源加载失败`);
  139. resolve(null);
  140. }
  141. });
  142. }
  143. /**
  144. * 添加节点动画
  145. * @param path 资源路径
  146. * @param node 目标节点
  147. * @param onlyOne 是否唯一
  148. * @param isDefaultClip 是否播放默认动画剪辑
  149. */
  150. static addNodeAnimation(path, node, onlyOne = true, isDefaultClip = false) {
  151. if (!node || !node.isValid) {
  152. return;
  153. }
  154. let anim = node.getComponent(Animation);
  155. if (anim == null) {
  156. anim = node.addComponent(Animation);
  157. }
  158. const clip = (_crd && resLoader === void 0 ? (_reportPossibleCrUseOfresLoader({
  159. error: Error()
  160. }), resLoader) : resLoader).get(path, AnimationClip);
  161. if (!clip) {
  162. return;
  163. }
  164. if (onlyOne && anim.getState(clip.name) && anim.getState(clip.name).isPlaying) {
  165. return;
  166. }
  167. if (isDefaultClip) {
  168. anim.defaultClip = clip;
  169. anim.play();
  170. return;
  171. } // 播放完成后恢复播放默认动画
  172. anim.once(Animation.EventType.FINISHED, () => {
  173. if (anim.defaultClip) {
  174. anim.play();
  175. }
  176. }, this);
  177. if (anim.getState(clip.name)) {
  178. anim.play(clip.name);
  179. return;
  180. }
  181. anim.createState(clip, clip.name);
  182. anim.play(clip.name);
  183. }
  184. });
  185. _cclegacy._RF.pop();
  186. _crd = false;
  187. }
  188. };
  189. });
  190. //# sourceMappingURL=f5e17874f550400748c63f17e937b5753c07e3b7.js.map