UIUtils.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. import {
  2. _decorator,
  3. Animation,
  4. Button,
  5. Component,
  6. EditBox,
  7. editorExtrasTag,
  8. find,
  9. isValid,
  10. js,
  11. Label,
  12. Layout,
  13. Node,
  14. ProgressBar,
  15. Quat,
  16. Slider,
  17. sp,
  18. Sprite,
  19. SpriteFrame,
  20. tween,
  21. UIOpacity,
  22. UITransform,
  23. Vec3,
  24. } from "cc";
  25. import { ScrollView } from "cc";
  26. import { Prefab } from "cc";
  27. import { instantiate } from "cc";
  28. import BaseUI from "./BaseUI";
  29. export default class UIUtils {
  30. static destroyNodeDelay(new_prop: Node, arg1: number) {
  31. setTimeout(() => {
  32. if (isValid(new_prop)) {
  33. new_prop.destroy();
  34. }
  35. }, arg1);
  36. }
  37. static removeEmptyNode(content: Node) {
  38. let emptyNode = content.getChildByName("EmptyNode");
  39. if (emptyNode) {
  40. emptyNode.destroy();
  41. }
  42. }
  43. static moveToNewParentAndAnim(parent: Node, animNode: Node, cb: Function) {
  44. UIUtils.moveToNewParent(parent, animNode);
  45. UIUtils.AnimBezierMoveAndScale(
  46. animNode,
  47. new Vec3(0, 0, 0),
  48. 0.2,
  49. 0.8,
  50. () => {
  51. cb();
  52. }
  53. );
  54. }
  55. static hideAnim(node: Node, arg1: () => void) {
  56. tween(node)
  57. .to(0.2, { scale: new Vec3(0, 0, 0) })
  58. .call(() => {
  59. node.active = false;
  60. node.setScale(new Vec3(1, 1, 1));
  61. arg1();
  62. })
  63. .start();
  64. }
  65. static closeUI(node: Node) {
  66. if (!isValid(node)) {
  67. return;
  68. }
  69. node.active = false;
  70. node.parent = null;
  71. node.destroy();
  72. }
  73. public static DeepFindChildByName(parentNode: Node, name: string): Node {
  74. if (!isValid(parentNode)) {
  75. return null;
  76. }
  77. return UIUtils._findInChildren(parentNode, name);
  78. }
  79. // 将一个node节点移动到新的父节点
  80. static moveToNewParent(parent: Node, child: Node) {
  81. // 先转换child的世界坐标位置
  82. let cp = child.getWorldPosition(new Vec3());
  83. // 更换父节点
  84. child.setParent(parent, false);
  85. // 转换世界坐标到新父节点的本地坐标系
  86. const parentUITrans = parent.getComponent(UITransform);
  87. // 设置新的本地坐标,使其屏幕位置不变
  88. const newP = parentUITrans.convertToNodeSpaceAR(cp);
  89. child.setPosition(newP);
  90. }
  91. /**
  92. * Layout子项目依次播放滑入动画
  93. * @param layout 布局节点
  94. * @param options 动画选项
  95. * @param callback 所有动画完成后的回调
  96. */
  97. static AnimateLayoutItems(
  98. layout: Node,
  99. options: {
  100. offsetX?: number;
  101. offsetY?: number;
  102. delayInterval?: number;
  103. duration?: number;
  104. scale?: number;
  105. easing?: string;
  106. staggerDirection?: "forward" | "reverse" | "random";
  107. onItemComplete?: (index: number, node: Node) => void;
  108. } = {},
  109. callback?: () => void
  110. ): void {
  111. if (!isValid(layout) || !layout.children || layout.children.length === 0) {
  112. if (callback) callback();
  113. return;
  114. }
  115. const { onItemComplete } = options;
  116. const children = [...layout.children];
  117. let completedCount = 0;
  118. const totalItems = children.length;
  119. // 先让所有子元素激活,让布局系统计算最终位置
  120. children.forEach((child) => {
  121. if (isValid(child)) {
  122. child.active = true;
  123. }
  124. });
  125. // 强制更新布局
  126. const layoutComp = layout.getComponent(Layout);
  127. if (layoutComp) {
  128. layoutComp.updateLayout();
  129. }
  130. // 等待布局更新完成后再开始动画
  131. setTimeout(() => {
  132. this.startSlideInAnimations(
  133. children,
  134. options,
  135. callback,
  136. completedCount,
  137. totalItems,
  138. onItemComplete
  139. );
  140. }, 0);
  141. }
  142. /**
  143. * 开始滑入动画
  144. */
  145. private static startSlideInAnimations(
  146. children: Node[],
  147. options: any,
  148. callback?: () => void,
  149. completedCount: number = 0,
  150. totalItems: number = 0,
  151. onItemComplete?: (index: number, node: Node) => void
  152. ): void {
  153. const {
  154. offsetX,
  155. offsetY,
  156. delayInterval,
  157. duration = 0.3,
  158. scale = 1,
  159. } = options;
  160. // 初始化所有子节点
  161. children.forEach((child, index) => {
  162. if (!isValid(child)) return;
  163. // 获取布局计算后的位置作为目标位置
  164. const targetPos = child.getPosition().clone();
  165. if (index < 8) {
  166. // 设置初始偏移位置,增加偏移量使动画更明显
  167. const startPos = new Vec3(
  168. targetPos.x + offsetX,
  169. targetPos.y +800,
  170. targetPos.z
  171. );
  172. child.setPosition(startPos);
  173. child.setScale(0.8, 0.8, 0.8);
  174. // 使用原始的DelayBezierMoveAndScale方法确保位置正确
  175. UIUtils.DelayBezierMoveAndScale(
  176. child,
  177. targetPos,
  178. scale,
  179. duration,
  180. delayInterval * index
  181. ).then(() => {
  182. completedCount++;
  183. if (onItemComplete) onItemComplete(index, child);
  184. if (completedCount >= totalItems && callback) {
  185. callback();
  186. }
  187. });
  188. } else {
  189. child.setPosition(targetPos);
  190. child.setScale(1, 1, 1);
  191. child.active = true;
  192. if (onItemComplete) onItemComplete(index, child);
  193. if (completedCount >= totalItems && callback) {
  194. callback();
  195. }
  196. }
  197. });
  198. }
  199. static AnimScaleShake(
  200. node: Node,
  201. scale: number = 1.2,
  202. duration: number = 0.6,
  203. repeatForever: boolean = true,
  204. callback?: () => void
  205. ) {
  206. const originalScale = node.getScale().clone();
  207. const scaleUp = Vec3.multiplyScalar(new Vec3(), originalScale, scale);
  208. const animation = tween(node)
  209. .to(
  210. duration / 2,
  211. { scale: originalScale.clone().multiplyScalar(scale) },
  212. { easing: "quadOut" }
  213. )
  214. .to(duration / 2, { scale: originalScale }, { easing: "quadIn" });
  215. if (repeatForever) {
  216. animation.repeatForever().start();
  217. } else {
  218. animation
  219. .call(() => {
  220. if (callback) callback();
  221. })
  222. .start();
  223. }
  224. }
  225. static async AnimShakeUpAndDownByTimes(node: Node, duration: number) {
  226. // 根据动画的次数,计算出动画的间隔时间
  227. return new Promise((resolve, reject) => {
  228. tween(node)
  229. .to(
  230. duration / 2,
  231. { position: new Vec3(0, 30, 0) },
  232. { easing: "quadOut" }
  233. )
  234. .to(
  235. duration / 2,
  236. { position: new Vec3(0, 150, 0) },
  237. { easing: "quadIn" }
  238. )
  239. .call(() => {
  240. resolve(node);
  241. })
  242. .start();
  243. });
  244. }
  245. static MoveXY(
  246. node: Node,
  247. moveX: number,
  248. moveY: number,
  249. duration: number,
  250. delay: number
  251. ) {
  252. let toPosition = node.getPosition();
  253. node.setPosition(toPosition.x + moveX, toPosition.y + moveY, toPosition.z);
  254. this.DelayBezierMoveAndScale(node, toPosition, 1, duration, delay);
  255. }
  256. static async DelayBezierMoveAndScale(
  257. node: Node,
  258. targetPos: Vec3,
  259. scale: number,
  260. duration: number,
  261. delay: number
  262. ) {
  263. await new Promise((resolve) => setTimeout(resolve, delay));
  264. return new Promise((resolve, reject) => {
  265. this.AnimBezierMoveAndScale(node, targetPos, scale, duration, () => {
  266. resolve(node);
  267. });
  268. });
  269. }
  270. static fadeIn(child: Node, duration: number) {
  271. if (!isValid(child)) {
  272. return;
  273. }
  274. let opacityComp = child.getComponent(UIOpacity);
  275. if (!opacityComp) {
  276. opacityComp = child.addComponent(UIOpacity);
  277. }
  278. child.setScale(0, 0);
  279. tween({ t: 0 })
  280. .to(
  281. duration,
  282. { t: 1 },
  283. {
  284. easing: "quadInOut",
  285. onUpdate: (obj) => {
  286. if (!isValid(child)) {
  287. return;
  288. }
  289. child.setScale(1 * obj.t, 1 * obj.t);
  290. opacityComp.opacity = 255 * obj.t;
  291. },
  292. }
  293. )
  294. .start();
  295. }
  296. static AnimBezierMoveAndTranslate(
  297. node: Node,
  298. targetPos: Vec3,
  299. scale: number,
  300. duration: number,
  301. callback?: Function
  302. ) {
  303. if (!isValid(node)) {
  304. return;
  305. }
  306. let opacityComp = node.getComponent(UIOpacity);
  307. if (!opacityComp) {
  308. opacityComp = node.addComponent(UIOpacity);
  309. }
  310. const startPos = node.getPosition();
  311. const startScale = node.scale.clone();
  312. const ctrl = new Vec3(
  313. (startPos.x + targetPos.x) / 2,
  314. Math.max(startPos.y, targetPos.y) + 150,
  315. (startPos.z + targetPos.z) / 2
  316. );
  317. const tempPos = new Vec3();
  318. const tempScale = new Vec3();
  319. tween({ t: 0 })
  320. .to(
  321. duration,
  322. { t: 1 },
  323. {
  324. easing: "quadInOut",
  325. onUpdate: (obj) => {
  326. if (!isValid(node)) {
  327. return;
  328. }
  329. const t = obj.t;
  330. const oneMinusT = 1 - t;
  331. // 贝塞尔曲线计算
  332. tempPos.x =
  333. oneMinusT * oneMinusT * startPos.x +
  334. 2 * t * oneMinusT * ctrl.x +
  335. t * t * targetPos.x;
  336. tempPos.y =
  337. oneMinusT * oneMinusT * startPos.y +
  338. 2 * t * oneMinusT * ctrl.y +
  339. t * t * targetPos.y;
  340. tempPos.z =
  341. oneMinusT * oneMinusT * startPos.z +
  342. 2 * t * oneMinusT * ctrl.z +
  343. t * t * targetPos.z;
  344. node.setPosition(tempPos);
  345. // 缩放插值计算
  346. tempScale.set(
  347. startScale.x + (scale - startScale.x) * t,
  348. startScale.y + (scale - startScale.y) * t,
  349. startScale.z + (scale - startScale.z) * t
  350. );
  351. node.setScale(tempScale);
  352. //设置透明度,越来越透明
  353. opacityComp.opacity = 255 * (1 - t);
  354. },
  355. }
  356. )
  357. .call(() => {
  358. if (callback) callback();
  359. })
  360. .start();
  361. }
  362. // 贝塞尔曲线实现移动和缩放动画
  363. static AnimBezierMoveAndScale(
  364. node: Node,
  365. targetPos: Vec3,
  366. scale: number,
  367. duration: number,
  368. callback?: Function,
  369. rotate?: number
  370. ) {
  371. if (!isValid(node)) {
  372. return;
  373. }
  374. const startPos = node.getPosition();
  375. const startScale = node.scale.clone();
  376. const ctrl = new Vec3(
  377. (startPos.x + targetPos.x) / 2,
  378. Math.max(startPos.y, targetPos.y) + 150,
  379. (startPos.z + targetPos.z) / 2
  380. );
  381. const tempPos = new Vec3();
  382. const tempScale = new Vec3();
  383. tween({ t: 0 })
  384. .to(
  385. duration,
  386. { t: 1 },
  387. {
  388. easing: "quadInOut",
  389. onUpdate: (obj) => {
  390. if (!isValid(node)) {
  391. return;
  392. }
  393. const t = obj.t;
  394. const oneMinusT = 1 - t;
  395. // 贝塞尔曲线计算
  396. tempPos.x =
  397. oneMinusT * oneMinusT * startPos.x +
  398. 2 * t * oneMinusT * ctrl.x +
  399. t * t * targetPos.x;
  400. tempPos.y =
  401. oneMinusT * oneMinusT * startPos.y +
  402. 2 * t * oneMinusT * ctrl.y +
  403. t * t * targetPos.y;
  404. tempPos.z =
  405. oneMinusT * oneMinusT * startPos.z +
  406. 2 * t * oneMinusT * ctrl.z +
  407. t * t * targetPos.z;
  408. node.setPosition(tempPos);
  409. // 缩放插值计算
  410. tempScale.set(
  411. startScale.x + (scale - startScale.x) * t,
  412. startScale.y + (scale - startScale.y) * t,
  413. startScale.z + (scale - startScale.z) * t
  414. );
  415. node.setScale(tempScale);
  416. if (rotate) {
  417. node.setRotationFromEuler(new Vec3(0, 0, rotate * t));
  418. }
  419. },
  420. }
  421. )
  422. .call(() => {
  423. if (callback) callback();
  424. })
  425. .start();
  426. }
  427. public static _findInChildren(node: Node, name: string): Node {
  428. var x = node.getChildByName(name);
  429. if (x) return x;
  430. if (node.children.length == 0) return null;
  431. for (var i = 0; i < node.children.length; ++i) {
  432. var tmp = this._findInChildren(node.children[i], name);
  433. if (tmp) return tmp;
  434. }
  435. return null;
  436. }
  437. }
  438. /*
  439. 使用示例:
  440. // 基本用法 - 滑入动画
  441. UIUtils.AnimateLayoutItems(layoutNode);
  442. // 自定义选项
  443. UIUtils.AnimateLayoutItems(layoutNode, {
  444. duration: 0.5,
  445. delayInterval: 0.15,
  446. scale: 1.1,
  447. easing: 'backOut',
  448. staggerDirection: 'reverse'
  449. }, () => {
  450. console.log('所有动画完成');
  451. });
  452. // 带单个项目完成回调
  453. UIUtils.AnimateLayoutItems(layoutNode, {
  454. onItemComplete: (index, node) => {
  455. console.log(`第${index}个项目动画完成`);
  456. }
  457. }, () => {
  458. console.log('所有项目动画完成');
  459. });
  460. */