Files
fc/dev/project/assets/Scripts/zbridge/binder/bridge.binder.attr.role.label.js
T
2026-05-23 22:10:14 +08:00

127 lines
2.9 KiB
JavaScript

/******************************************************************
* Copyright(C) 2019 - 2020 Nx Studio
*
* 数据绑定 --- 主角属性(文本)
*
*
******************************************************************/
const RoleController = require( "role_controller" );
cc.Class( {
extends: cc.Component,
properties: {
attKey: {
default : "",
displayName : "属性名",
},
ndLabel: {
default : null,
type : cc.Label,
displayName : "属性文本",
},
shortMoney: {
default : false,
displayName : "货币简化支持",
},
},
// 编辑器特性
editor: {
// 允许当前组件在编辑器模式下运行
executeInEditMode: false,
// requireComponent 参数用来指定当前组件的依赖组件
requireComponent: cc.Label,
// 当本组件添加到节点上后,禁止同类型(含子类)的组件再添加到同一个节点,防止逻辑发生冲突
disallowMultiple: true,
// menu 用来将当前组件添加到组件菜单中,方便用户查找
menu: "老版桥接/主角属性",
},
// 加载
onLoad: function() {
if( !this.ndLabel ) {
this.ndLabel = nx.gui.getComponent( this, "", cc.Label );
}
},
// 显示
onEnable: function() {
if( nx.dt.strNEmpty( this.attKey ) ) {
this.bindAttr( this.attKey );
}
},
// 隐藏
onDisable: function() {
if( this.binder && this.handler ) {
this.binder.unbind( this.handler );
this.handler = null;
this.binder = null;
}
},
// 设置绑定值
bindAttr: function( _key ) {
if( nx.dt.strEmpty( _key )) {
return;
}
// if( nx.dt.strNEmpty( this.attKey ) ) {
// nx.warn( "[Binder]绑定失败,无法中途修改!" );
// return;
// }
this.attKey = _key;
this.binder = RoleController.getInstance().getRoleVo();
if( !this.binder ) {
nx.error( "[Binder]当前无主角,属性绑定失败!" );
return;
}
let eid = EventId.UPDATE_ROLE_ATTRIBUTE;
this.handler = this.binder.bind( eid, this.onUpdate, this );
if( !this.handler ) {
nx.error( "[Binder]属性监听失败!" );
return;
}
// 手动更新一次
this.onUpdate( this.attKey, this.binder[this.attKey] );
},
// 更新
onUpdate: function( _key, _value ) {
if( _key != this.attKey ) {
return;
}
let val = '' + _value;
if( this.shortMoney ) {
val = Utils.getMoneyString( parseInt( _value ), true, 2 );
}
this.ndLabel.string = val;
},
} );