Files

243 lines
5.9 KiB
JavaScript
Raw Permalink Normal View History

2026-05-24 10:21:26 +08:00
"use strict";
cc._RF.push(module, '179f35IE0JLR4M0zXgcbrdZ', 'nx.fx.spine');
// Scripts/nx/cmp/flex/nx.fx.spine.js
"use strict";
/******************************************************************
* Copyright(C) 2019 - 2020 Nx Studio
*
* Spine强化
*
* 2023.11.01
******************************************************************/
cc.Class({
"extends": cc.Component,
properties: {
spNode: {
"default": null,
type: sp.Skeleton
},
spAction: {
"default": "",
displayName: "动作名"
},
spRK: {
"default": "",
displayName: "资源键值"
},
spLoop: {
"default": true,
displayName: "循环"
},
isAntialias: {
"default": true,
displayName: "抗锯齿"
}
},
// 编辑器特性
editor: {
// 允许当前组件在编辑器模式下运行
executeInEditMode: false,
// 当本组件添加到节点上后,禁止同类型(含子类)的组件再添加到同一个节点,防止逻辑发生冲突
disallowMultiple: true,
// menu 用来将当前组件添加到组件菜单中,方便用户查找
menu: "Nx/本地化|骨骼动画"
},
// 加载
onLoad: function onLoad() {
// SP事件绑定
this.spEvent();
// 语言改变
this.onLanguageChanged();
},
// 销毁
onDestroy: function onDestroy() {
// 资源释放
if (this.spSkeleton && window.nx) {
nx.assets.uncacheAsset(this.spSkeleton);
this.spSkeleton = null;
}
},
// 播放
play: function play(_rkey, _action, _cb, _loop) {
// 直接播放
if (this.key == _rkey && this.spNode.skeletonData) {
this.action(_action, _loop, _cb);
return;
}
// 先停止
this.stop();
// 再加载
var self = this;
this.load(_rkey, function (_e, _p) {
// 失败处理
if (_e) {
nx.dt.fnInvoke(_cb, _e);
return;
}
// 最后播放
self.action(_action, _loop, _cb);
});
},
// 停止
stop: function stop(_release) {
if (_release === void 0) {
_release = true;
}
this.spCB = null;
if (!this.spNode) {
return;
}
// 动画停止
this.spNode.node.opacity = 0;
this.spNode.animation = "";
if (_release && this.spNode.skeletonData) {
this.spNode.skeletonData.reset();
this.spNode.skeletonData = null;
nx.assets.uncacheAsset(this.spSkeleton);
this.spSkeleton = null;
}
},
// 加载SPINE文件
load: function load(_rkey, _cb) {
// 完成处理
// 记录当前文件信息
var self = this;
var done = function done() {
self.key = _rkey;
self.spRK = _rkey;
};
// 統一返回
var notify = function notify(_p1, _p2) {
if (!self || !self.spNode) {
nx.warn("NxSpine:\u8282\u70B9\u5DF2\u7ECF\u4E0D\u5B58\u5728,\u56DE\u8C03\u5FFD\u7565!");
return;
}
nx.dt.fnInvoke(_cb, _p1, _p2);
};
// 读取
var path = nx.res.fmtPath(_rkey);
nx.res.loadSpine(path, function (_e, _p) {
if (_e) {
notify(_e);
return;
}
// 抗锯齿
if (nx.dt.arrNEmpty(_p.textures)) {
var minF = self.isAntialias ? cc.Texture2D.Filter.LINEAR : cc.Texture2D.Filter.NEAREST;
var magF = self.isAntialias ? cc.Texture2D.Filter.LINEAR : cc.Texture2D.Filter.NEAREST;
for (var i in _p.textures) {
var tex = _p.textures[i];
if (tex) {
tex.setFilters(minF, magF);
}
}
}
// 资源绑定
if (self.spNode) {
nx.assets.cacheAsset(_p, path);
self.spNode.skeletonData = _p;
nx.assets.uncacheAsset(self.spSkeleton);
self.spSkeleton = _p;
self.spNode.node.opacity = 255;
}
done();
notify();
});
},
// 直接播放动作
action: function action(_action, _loop, _cb, _scale) {
if (_scale === void 0) {
_scale = 1.0;
}
if (!this.spNode) {
nx.warn("节点丢失,播放动作失败!" + _action);
return;
}
// 如果已经丢失数据,则重载播放
if (!this.spNode.skeletonData) {
this.play(this.spRK, _action, _loop, _cb);
return;
}
this.spCB = _cb;
this.spAction = _action;
this.spLoop = !!_loop;
// 播放
this.spNode.loop = !!_loop;
this.spNode.animation = _action;
this.spNode.node.opacity = 255;
this.spNode.timeScale = _scale;
},
// 修改播放速度
setTimeScale: function setTimeScale(_scale) {
if (_scale === void 0) {
_scale = 1.0;
}
if (this.spNode) {
this.spNode.timeScale = _scale;
}
},
// SP事件绑定
spEvent: function spEvent() {
if (!this.spNode) {
return;
}
var self = this;
var tracker = function tracker(_key, _tracker, _custom) {
if (_custom === void 0) {
_custom = false;
}
var name = _tracker.animation ? _tracker.animation.name : "";
self._notify(_key, name, _custom);
};
// 监听
this.spNode.setStartListener(function (_tk) {
tracker("start", _tk);
});
this.spNode.setInterruptListener(function (_tk) {
tracker("interrupt", _tk);
});
this.spNode.setEndListener(function (_tk) {
tracker("end", _tk);
});
this.spNode.setDisposeListener(function (_tk) {
tracker("disposed", _tk);
});
this.spNode.setCompleteListener(function (_tk) {
tracker("complete", _tk);
});
this.spNode.setEventListener(function (_tk, _event) {
tracker(_event.data.name, _tk, true);
});
},
// 当前语种改变
onLanguageChanged: function onLanguageChanged(_lang) {
if (nx.dt.strEmpty(this.spRK) || nx.dt.strEmpty(this.spAction)) {
return;
}
// 播放
this.play(this.spRK, this.spAction, this.spCB, this.spLoop);
},
// 通告
_notify: function _notify(event, anim, custom) {
// nx.debug( "[SPINE]Event:%s %s", event, anim );
nx.dt.fnInvoke(this.spCB, event, anim, custom);
}
});
cc._RF.pop();