marineparkclient/assets/common/ui/UIMaskScript.ts
2023-08-15 11:09:12 +08:00

87 lines
2.7 KiB
TypeScript

import UiHelper from "./base/UIHelper";
import { MaskOpacity } from "./config/SysDefine";
import UIManager from "./UIManager";
const {ccclass, property} = cc._decorator;
@ccclass
export class UIMaskScript extends cc.Component {
public uid: string;
/** 代码创建一个单色texture */
private _texture: cc.Texture2D = null;
private getSingleTexture() {
if(this._texture) return this._texture;
let data: any = new Uint8Array(2 * 2 * 4);
for(let i=0; i<2; i++) {
for(let j=0; j<2; j++) {
data[i*2*4 + j*4+0] = 255;
data[i*2*4 + j*4+1] = 255;
data[i*2*4 + j*4+2] = 255;
data[i*2*4 + j*4+3] = 255;
}
}
let texture = new cc.Texture2D();
texture.initWithData(data, cc.Texture2D.PixelFormat.RGBA8888, 2, 2);
this._texture = texture;
return this._texture;
}
/**
* 初始化
*/
public init() {
let maskTexture = this.getSingleTexture();
let size = cc.view.getVisibleSize();
this.node.height = size.height;
this.node.width = size.width;
this.node.x = size.width/2;
this.node.y = size.height/2;
this.node.addComponent(cc.Button);
this.node.on('click', this.clickMaskWindow, this);
let sprite = this.node.addComponent(cc.Sprite)
sprite.sizeMode = cc.Sprite.SizeMode.CUSTOM;
sprite.spriteFrame = new cc.SpriteFrame(maskTexture);
this.node.color = new cc.Color(0, 0, 0);
this.node.opacity = 0;
this.node.active = true;
}
//
public async showMask(lucenyType: number, time: number = 0.6, isEasing: boolean = true) {
let o = 0;
switch (lucenyType) {
case MaskOpacity.None:
this.node.active = false;
break;
case MaskOpacity.OpacityZero:
o = 0;
break;
case MaskOpacity.OpacityLow:
o = 63;
break;
case MaskOpacity.OpacityHalf:
o = 126;
break;
case MaskOpacity.OpacityHigh:
o = 189;
break;
case MaskOpacity.OpacityFull:
o = 255;
break;
}
if(!this.node.active) return ;
if(isEasing) {
await UiHelper.runTweenAsync(this.node, cc.tween().to(time, {opacity: o}));
}else {
this.node.opacity = o;
}
}
public async clickMaskWindow() {
let com = UIManager.getInstance().getComponentByUid(this.uid);
if(com && com.maskType.clickMaskClose) {
await UIManager.getInstance().closeUIPanel(this.uid);
}
}
}