FuncBg.ts 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import { _decorator, Color, Component, isValid, Layers, Node, NodeEventType, Sprite, SpriteFrame, Texture2D, tween, Tween, UIOpacity, UITransform, view } from 'cc';
  2. const { ccclass, property } = _decorator;
  3. export enum FuncBgType {
  4. None,
  5. Single,//打开一个 会关闭前一个
  6. Series,//可同时打开多个,会同时关闭
  7. }
  8. @ccclass('FuncBg')
  9. export class FuncBg extends Component {
  10. singleTar: Node = null
  11. SeriesTarList: Array<Node> = []
  12. /** 添加功能背景,如全屏关闭的遮罩背景
  13. * @param node 目标节点(背景在它之下)
  14. * @param func 具体功能
  15. * @param mode 模式,参考 FuncBgType
  16. * @param opacity 0 为透明背景
  17. * @param useCapture 有其他上层的点击遮挡时 是否穿透
  18. */
  19. public add(node: Node, func: Function, mode: FuncBgType = 0, opacity = 100, useCapture = false) {
  20. let event = () => {
  21. if (mode == FuncBgType.Single) this.singleTar = null
  22. if (mode == FuncBgType.Series) this.clsoeSeries(node)
  23. if (!isValid(bg)) {
  24. console.warn("FuncBG is not find");
  25. return;
  26. }
  27. Tween.stopAllByTarget(bg)
  28. func && func()
  29. tween(bg.getComponent(UIOpacity)).to(time, { opacity: 0 }).call(_ => {
  30. bg.destroy()
  31. }).start()
  32. console.log("关闭功能背景");
  33. node.off("CLOSEBG")
  34. }
  35. const time = 0.05
  36. let bg = this.newBg(node)
  37. bg.once(NodeEventType.TOUCH_END, event, null, useCapture)
  38. tween(bg.getComponent(UIOpacity)).to(time, { opacity: opacity / 255 }).start()
  39. node.once("CLOSEBG", () => { event(); })
  40. node.once(NodeEventType.NODE_DESTROYED, () => {
  41. // console.warn("BG 销毁");
  42. isValid(bg) && bg.destroy()
  43. })
  44. node.once(NodeEventType.ACTIVE_IN_HIERARCHY_CHANGED, () => {
  45. // console.warn("BG 隐藏");
  46. isValid(bg) && bg.destroy()
  47. })
  48. if (mode == FuncBgType.Single) {
  49. this.clsoeLastSingle(node)
  50. } else if (mode == FuncBgType.Series) {
  51. this.SeriesTarList.push(node)
  52. }
  53. }
  54. newBg(tar: Node): Node {
  55. let bg = new Node("FuncBG");
  56. bg.layer = Layers.Enum.DEFAULT;
  57. let ui = bg.addComponent(UITransform);
  58. let sp = bg.addComponent(Sprite);
  59. let op = bg.addComponent(UIOpacity);
  60. sp.sizeMode = Sprite.SizeMode.CUSTOM;
  61. // 1. 创建纯黑纹理
  62. const texture = new Texture2D();
  63. texture.create(1, 1, Texture2D.PixelFormat.RGBA8888);
  64. texture.uploadData(new Uint8Array([0, 0, 0, 255])); // R,G,B,A 全0是纯黑
  65. // 2. 创建SpriteFrame并绑定纹理
  66. const spriteFrame = new SpriteFrame();
  67. spriteFrame.texture = texture;
  68. sp.spriteFrame = spriteFrame;
  69. sp.color = new Color(0, 0, 0, 255);
  70. ui.width = view.getVisibleSize().width;
  71. ui.height = 1600//view.getVisibleSize().height;
  72. bg.setPosition(ui.width / 2, ui.height / 2)
  73. bg.setParent(tar.parent, true)
  74. bg.setSiblingIndex(tar.getSiblingIndex())
  75. bg.toOpacity(0)
  76. op.opacity = 0
  77. return bg
  78. }
  79. /**关闭上一个 */
  80. clsoeLastSingle(node) {
  81. if (isValid(this.singleTar)) {
  82. this.singleTar.emit("CLOSEBG")
  83. }
  84. this.singleTar = node
  85. }
  86. /**全部关闭 */
  87. clsoeSeries(node) {
  88. let index = this.SeriesTarList.indexOf(node)
  89. if (index < 0) return
  90. this.SeriesTarList.splice(index, 1)
  91. this.SeriesTarList.forEach((tar) => {
  92. tar.emit("CLOSEBG")
  93. })
  94. }
  95. /** 手动关闭功能背景
  96. * @param node 目标节点
  97. */
  98. public close(node: Node) {
  99. node.emit("CLOSEBG")
  100. }
  101. }
  102. export const funcBg = new FuncBg()