UIBG.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. const { ccclass, property } = cc._decorator;
  2. /**
  3. * 窗口遮罩
  4. * - 挂载到节点上
  5. * @author 薛鸿潇
  6. */
  7. @ccclass//('UIBG')
  8. export default class UIBG extends cc.Component {
  9. @property({ displayName: '背景颜色' })
  10. private color = new cc.Color(0, 0, 0, 180);
  11. @property({ displayName: '自定义屏蔽节点', type: cc.Node })
  12. private design_block_node: cc.Node = null!;
  13. @property({ displayName: '点击关闭' })
  14. private bClickClose: boolean = false;
  15. @property({ displayName: '完成回调', tooltip: "面板打开完成后触发", type: cc.Component.EventHandler })
  16. public onComplete: cc.Component.EventHandler[] = [];
  17. /** 背景节点 */
  18. private node_bg: cc.Node = null;
  19. onLoad() {
  20. this.initBG();
  21. }
  22. private initBG() {
  23. const width = mk.game.getWinSize().width;
  24. const height = mk.game.getWinSize().height;
  25. this.node_bg = this.drawColorRect(new cc.Rect(-width / 2, -height / 2, width, height));
  26. this.node_bg.parent = this.node;
  27. this.node_bg.zIndex = -10000;
  28. this.node_bg.addComponent(cc.Button);
  29. this.node_bg.on('click', this.initClick, this);
  30. // this.btn_close && this.btn_close.node.on('click', this.onClickClose, this);
  31. if (this.design_block_node) this.design_block_node.addComponent(cc.BlockInputEvents);
  32. }
  33. /**
  34. * 按钮回调
  35. */
  36. private initClick() {
  37. if (this.bClickClose) this.onClickClose();
  38. }
  39. private onClickClose() {
  40. mk.ui.closePanel(this.node.name);
  41. const c_count = this.onComplete.length;
  42. for (let i = 0; i < c_count; i++) {
  43. if (this.onComplete[i].handler) this.onComplete[i].emit([])
  44. }
  45. }
  46. /**
  47. * 创建背景蒙版 by 绘图组件
  48. * @param size 坐标及宽高
  49. * @returns 创建的节点
  50. */
  51. public drawColorRect(size: cc.Rect): cc.Node {
  52. let node = new cc.Node('colorRect');
  53. node.setContentSize(size);
  54. let graph = node.addComponent(cc.Graphics);
  55. graph.fillColor = this.color;
  56. graph.fillRect(size.x, size.y, size.width, size.height);
  57. return node;
  58. }
  59. }