180 lines
4.6 KiB
JavaScript
180 lines
4.6 KiB
JavaScript
"use strict";
|
|
cc._RF.push(module, '5f7fb4uBDZHMbLuP+qhKgNq', 'nx.fx.button');
|
|
// Scripts/nx/cmp/flex/nx.fx.button.js
|
|
|
|
"use strict";
|
|
|
|
/******************************************************************
|
|
* Copyright(C) 2019 - 2020 Nx Studio
|
|
*
|
|
* 加锁按钮
|
|
*
|
|
* 2018.05.18
|
|
******************************************************************/
|
|
|
|
var TDefine = require("trace.define");
|
|
var 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 onLoad() {
|
|
// 默认解锁
|
|
this.lock(this.locked);
|
|
//设置解锁时的初始态
|
|
this.setInitParam();
|
|
},
|
|
// 显示
|
|
onEnable: function onEnable() {
|
|
this.node.on("click", this.onCatchClicked, this);
|
|
},
|
|
// 隐藏
|
|
onDisable: function onDisable() {
|
|
this.node.off("click", this.onCatchClicked, this);
|
|
},
|
|
setInitParam: function setInitParam() {
|
|
var cdn = this.node.children;
|
|
var outline = null;
|
|
this.oldColor = []; //可能有多个文本
|
|
for (var 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 lock(_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 setBtnColor(isnormal) {
|
|
if (!this.node.isValid || nx.dt.arrEmpty(this.oldColor)) return;
|
|
var cdn = this.node.children;
|
|
if (isnormal) {
|
|
var index = 0;
|
|
for (var n in cdn) {
|
|
if (cdn[n]) {
|
|
var outline = nx.gui.getComponent(cdn[n], "", cc.LabelOutline);
|
|
if (outline) {
|
|
outline.color = this.oldColor[index];
|
|
index++;
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
for (var g in cdn) {
|
|
if (cdn[g]) {
|
|
var _outline = nx.gui.getComponent(cdn[g], "", cc.LabelOutline);
|
|
if (_outline) {
|
|
_outline.color = this.grayColor;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
},
|
|
// 点击
|
|
onCatchClicked: function onCatchClicked() {
|
|
// 音效
|
|
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);
|
|
}
|
|
}
|
|
});
|
|
|
|
cc._RF.pop(); |