UIBG.ts 1.8 KB

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