"use strict"; cc._RF.push(module, '48d42LW3YtMg7iRbd9yzcMP', 'bridge.binder.item.icon.count'); // Scripts/zbridge/binder/bridge.binder.item.icon.count.js "use strict"; /****************************************************************** * Copyright(C) 2019 - 2020 Nx Studio * * 数据绑定 --- 道具图标+数量模板 * * ******************************************************************/ var BridgeComponent = require("bridge.component"); var RoleController = require("role_controller"); var BackPackController = require("backpack_controller"); var BridgeJumper = require("bridge.jumper"); cc.Class({ "extends": BridgeComponent, properties: { itemId: { "default": 0, displayName: "道具编号" }, ndIcon: { "default": null, type: cc.Node, displayName: "图标" }, ndCount: { "default": null, type: cc.Node, displayName: "数量" }, useSmall: { "default": true, displayName: "使用小图标" }, shortMoney: { "default": false, displayName: "货币简化支持" }, autoJump: { "default": true, displayName: "自动跳转" } }, // 编辑器特性 editor: { // 允许当前组件在编辑器模式下运行 executeInEditMode: false, // 当本组件添加到节点上后,禁止同类型(含子类)的组件再添加到同一个节点,防止逻辑发生冲突 disallowMultiple: true, // menu 用来将当前组件添加到组件菜单中,方便用户查找 menu: "老版桥接/资产道具信息" }, // 载入 onLoad: function onLoad() { this._super(); // 初始刷新 if (nx.dt.numPositive(this.itemId, false) && !nx.dt.fnGood(this.cbChangeNotify)) { this.setID(this.itemId); } // 开关点击 this.openTouch(this.autoJump); }, // 销毁 onDestroy: function onDestroy() { // 开关点击 this.openTouch(false); this.setID(0); this._super(); }, // 设置新ID setID: function setID(_id, _cbChange) { if (_cbChange === void 0) { _cbChange = null; } // 改变通告 this.cbChangeNotify = _cbChange; // 释放 if (this.itemId > 0) { this.itemId = 0; this.unbindGEvents(); if (this.binder && this.handler) { this.binder.unbind(this.handler); this.handler = null; this.binder = null; } } if (!window.nx) { return; } // 新绑定 this.itemId = parseInt(_id); this.config = gitemdata(this.itemId); // 无效绑定 if (nx.dt.objEmpty(this.config)) { this.itemId = 0; this.config = null; // nx.gui.setSpriteFrame( this.ndIcon, "", null ); // nx.gui.setString( this.ndCount, "", "" ); return; } // 图标 if (this.ndIcon) { if (this.useSmall) { nx.bridge.setIconS(this.ndIcon, "", this.config.icon); } else { nx.bridge.setIcon(this.ndIcon, "", this.config.icon); } } // 辨别绑定方式 this.bindPK = ""; var key = game.configs.item_data.data_assets_id2label[this.itemId]; if (nx.dt.strNEmpty(key)) { this.bindPK = key; } // 分别绑定 if (nx.dt.strEmpty(this.bindPK)) { this.bindBagItem(); } else { this.bindRoleProp(); } }, // 绑定角色属性 bindRoleProp: function bindRoleProp() { this.binder = RoleController.getInstance().getRoleVo(); if (!this.binder) { nx.error("[Binder]当前无主角,属性绑定失败!"); return; } var eid = EventId.UPDATE_ROLE_ATTRIBUTE; this.handler = this.binder.bind(eid, this.onPropUpdate, this); if (!this.handler) { nx.error("[Binder]属性监听失败!"); return; } // 手动更新一次 this.onPropUpdate(this.bindPK, this.binder[this.bindPK]); }, // 绑定背包道具 bindBagItem: function bindBagItem() { this.unbindGEvents(); if (!nx.dt.numPositive(this.itemId, false)) { return; } // 背包监听 this.bindGEvent(EventId.ADD_GOODS, this.onBagItemUpdate.bind(this)); this.bindGEvent(EventId.DELETE_GOODS, this.onBagItemUpdate.bind(this)); this.bindGEvent(EventId.MODIFY_GOODS_NUM, this.onBagItemUpdate.bind(this)); // 手动更新一次 this.updateItemCount(); }, // 属性更新 onPropUpdate: function onPropUpdate(_key, _value) { if (_key != this.bindPK) { return; } var val = '' + _value; if (this.shortMoney) { val = nx.dt.shortCount(_value); } nx.gui.setString(this.ndCount, "", val); // 改变通告 nx.dt.fnInvoke(this.cbChangeNotify, _key, _value); }, // 道具数量更新 updateItemCount: function updateItemCount() { var BC = BackPackController.getInstance(); var count = BC.getModel().getBackPackItemNumByBid(this.itemId); var val = '' + count; if (this.shortMoney) { val = nx.dt.shortCount(count); } nx.gui.setString(this.ndCount, "", val); // 改变通告 nx.dt.fnInvoke(this.cbChangeNotify, this.itemId, count); }, // 道具更新 onBagItemUpdate: function onBagItemUpdate(_bagCode, _itemList) { for (var k in _itemList) { var tm = _itemList[k]; if (tm && tm.base_id == this.itemId) { this.updateItemCount(); break; } } }, // 获取跳转 onTouchJumpSource: function onTouchJumpSource(event) { if (event.type == 'touchend') BridgeJumper.jumpGetItem(this.itemId); }, // 开关点击 openTouch: function openTouch(_open) { if (_open === void 0) { _open = false; } this.node.off("touchend", this.onTouchJumpSource, this); if (_open) { this.node.on("touchend", this.onTouchJumpSource, this); } } }); cc._RF.pop();