Files
2026-05-23 22:10:14 +08:00

177 lines
4.0 KiB
JavaScript

/******************************************************************
* Copyright(C) 2019 - 2020 Nx Studio
*
* 加锁按钮
*
* 2018.05.18
******************************************************************/
const TDefine = require( "trace.define" );
const TTT = TDefine.TraceType;
cc.Class( {
extends: cc.Component,
properties: {
nodUnLock: { default: null, type: cc.Node, displayName: "解锁态" },
nodLock: { default: null, type: cc.Node, displayName: "锁定态" },
touchAudio: { default: null, type: cc.AudioClip, displayName: "触摸音" },
clickInterval: { default: 0.2, displayName: "点击间隔(秒)" },
touchEvents: { default: [], type: cc.Component.EventHandler, displayName: "触摸回调" },
// 埋点
idType: { default: TTT.btnClicked, type: TTT, displayName: "埋点类型" },
btnName: { default: "", displayName: "按钮名" },
},
// 编辑器特性
editor: {
// 允许当前组件在编辑器模式下运行
executeInEditMode: false,
// requireComponent 参数用来指定当前组件的依赖组件
requireComponent: cc.Button,
// 当本组件添加到节点上后,禁止同类型(含子类)的组件再添加到同一个节点,防止逻辑发生冲突
disallowMultiple: true,
// menu 用来将当前组件添加到组件菜单中,方便用户查找
menu: "Nx/组件|按钮",
},
// 载入
onLoad: function() {
// 默认解锁
this.lock( this.locked );
//设置解锁时的初始态
this.setInitParam();
},
// 显示
onEnable: function() {
this.node.on( "click", this.onCatchClicked, this );
},
// 隐藏
onDisable: function() {
this.node.off( "click", this.onCatchClicked, this );
},
setInitParam: function() {
let cdn = this.node.children;
let outline = null;
this.oldColor = [];//可能有多个文本
for( let i in cdn ) {
if( cdn[ i ] ) {
outline = nx.gui.getComponent( cdn[ i ], "", cc.LabelOutline );
if( outline ) {
this.oldColor.push( outline.color );
}
}
}
this.grayColor = new cc.Color( 69, 69, 69 );
},
// 无效态
lock: function( _locked ) {
this.locked = _locked;
if( this.nodUnLock ) {
this.nodUnLock.active = !_locked;
}
if( this.nodLock ) {
this.nodLock.active = _locked;
}
var btn = this.node.getComponent( cc.Button );
if( btn ) {
btn.interactable = !_locked;
if( btn.enableAutoGrayEffect ) {
this.setBtnColor( btn.interactable );
}
}
},
setBtnColor: function( isnormal ) {
if( !this.node.isValid || nx.dt.arrEmpty( this.oldColor ) ) return;
let cdn = this.node.children;
if( isnormal ) {
let index = 0;
for( let n in cdn ) {
if( cdn[ n ] ) {
let outline = nx.gui.getComponent( cdn[ n ], "", cc.LabelOutline );
if( outline ) {
outline.color = this.oldColor[ index ];
index++;
}
}
}
} else {
for( let g in cdn ) {
if( cdn[ g ] ) {
let outline = nx.gui.getComponent( cdn[ g ], "", cc.LabelOutline );
if( outline ) {
outline.color = this.grayColor;
}
}
}
}
},
// 点击
onCatchClicked: function() {
// 音效
if( this.touchAudio ) {
nx.audio.playSFX( this.touchAudio, false );
} else {
if( nx.audioHelper ) {
nx.audioHelper.sfxBtnClicked();
}
}
// 点击间隔
var now = new Date();
if( typeof this.lastClickTime == "number" ) {
var ms = this.clickInterval * 1000;
if( ( now.getTime() - this.lastClickTime ) < ms ) {
return;
}
}
this.lastClickTime = now.getTime();
// 通告
if( this.touchEvents ) {
// 在没有提供CustomData,则返回节点
for( var i = 0; i < this.touchEvents.length; ++i ) {
var te = this.touchEvents[ i ];
if( te && te.customEventData === "" ) {
te.customEventData = this.node;
}
}
// nx.dt.trycatch( () => {
cc.Component.EventHandler.emitEvents( this.touchEvents );
// } )
}
// 埋点
if( nx.mTrace && this.idType > 0 && nx.dt.strNEmpty( this.btnName ) ) {
nx.mTrace.trace( this.idType, this.btnName );
}
}
} );