UIBG.ts 1.7 KB

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