Skip to content

Issue #2760: updated some spine skeleton APIs, but it doesn't support Mesh. #2772

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions extensions/spine/CCSkeleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ sp.VERTEX_INDEX = {
};

/**
* The attachment type of spine. It contains three type: REGION(0), BOUNDING_BOX(1), REGION_SEQUENCE(2) and MESH(2).
* The attachment type of spine. It contains three type: REGION(0), BOUNDING_BOX(1), MESH(2) and SKINNED_MESH.
* @constant
* @type {{REGION: number, BOUNDING_BOX: number, REGION_SEQUENCE: number, MESH: number}}
*/
sp.ATTACHMENT_TYPE = {
REGION: 0,
BOUNDING_BOX: 1,
REGION_SEQUENCE: 2,
MESH: 2
MESH: 2,
SKINNED_MESH:3
};

/**
Expand Down Expand Up @@ -112,7 +112,7 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
},

/**
* Sets whether open debug solots.
* Sets whether open debug slots.
* @param {boolean} enable true to open, false to close.
*/
setDebugSolots:function(enable){
Expand All @@ -127,12 +127,48 @@ sp.Skeleton = cc.Node.extend(/** @lends sp.Skeleton# */{
this._debugBones = enable;
},

/**
* Sets whether open debug slots.
* @param {boolean} enabled true to open, false to close.
*/
setDebugSlotsEnabled: function(enabled) {
this._debugSlots = enabled;
},

/**
* Gets whether open debug slots.
* @returns {boolean} true to open, false to close.
*/
getDebugSlotsEnabled: function() {
return this._debugSlots;
},

/**
* Sets whether open debug bones.
* @param {boolean} enabled
*/
setDebugBonesEnabled: function(enabled) {
this._debugBones = enabled;
},

/**
* Gets whether open debug bones.
* @returns {boolean} true to open, false to close.
*/
getDebugBonesEnabled: function() {
return this._debugBones;
},

/**
* Sets the time scale of sp.Skeleton.
* @param {Number} v
* @param {Number} scale
*/
setTimeScale:function(v){
this._timeScale = v;
setTimeScale:function(scale){
this._timeScale = scale;
},

getTimeScale: function(){
return this._timeScale;
},

/**
Expand Down
124 changes: 121 additions & 3 deletions extensions/spine/CCSkeletonAnimation.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ sp._regionAttachment_updateQuad = function(self, slot, quad, premultipliedAlpha)

sp._meshAttachment_updateQuad = function(self, slot, quad, premultipliedAlpha) {
var vertices = {};
self.computeVertices(slot.bone.x, slot.bone.y, slot.bone, vertices);
self.computeWorldVertices(slot.bone.x, slot.bone.y, slot, vertices);
var r = slot.bone.skeleton.r * slot.r * 255;
var g = slot.bone.skeleton.g * slot.g * 255;
var b = slot.bone.skeleton.b * slot.b * 255;
Expand Down Expand Up @@ -182,6 +182,25 @@ sp.ANIMATION_EVENT_TYPE = {
EVENT: 3
};

sp.TrackEntryListeners = function(startListener, endListener, completeListener, eventListener){
this.startListener = startListener || null;
this.endListener = endListener || null;
this.completeListener = completeListener || null;
this.eventListener = eventListener || null;
};

sp.TrackEntryListeners.getListeners = function(entry){
if(!entry.rendererObject){
entry.rendererObject = new sp.TrackEntryListeners();
entry.listener = sp.trackEntryCallback;
}
return entry.rendererObject;
};

sp.trackEntryCallback = function(state, trackIndex, type, event, loopCount) {
state.rendererObject.onTrackEntryEvent(trackIndex, type, event, loopCount);
};

/**
* The skeleton animation of spine. It updates animation's state and skeleton's world transform.
* @class
Expand All @@ -195,12 +214,19 @@ sp.SkeletonAnimation = sp.Skeleton.extend(/** @lends sp.SkeletonAnimation# */{
_target: null,
_callback: null,

_ownsAnimationStateData: false,
_startListener: null,
_endListener: null,
_completeListener: null,
_eventListener: null,

/**
* Initializes a sp.SkeletonAnimation. please do not call this function by yourself, you should pass the parameters to constructor to initialize it.
* @override
*/
init: function () {
sp.Skeleton.prototype.init.call(this);
this._ownsAnimationStateData = true;
this.setAnimationStateData(new spine.AnimationStateData(this._skeleton.data));
},

Expand All @@ -210,6 +236,7 @@ sp.SkeletonAnimation = sp.Skeleton.extend(/** @lends sp.SkeletonAnimation# */{
*/
setAnimationStateData: function (stateData) {
var state = new spine.AnimationState(stateData);
state.rendererObject = this;
state.onStart = this._onAnimationStateStart.bind(this);
state.onComplete = this._onAnimationStateComplete.bind(this);
state.onEnd = this._onAnimationStateEnd.bind(this);
Expand Down Expand Up @@ -258,10 +285,11 @@ sp.SkeletonAnimation = sp.Skeleton.extend(/** @lends sp.SkeletonAnimation# */{
* @param {Number} trackIndex
* @param {String} name
* @param {Boolean} loop
* @param {Number} delay
* @param {Number} [delay=0]
* @returns {spine.TrackEntry|null}
*/
addAnimation: function (trackIndex, name, loop, delay) {
delay = delay == null ? 0 : delay;
var animation = this._skeleton.data.findAnimation(name);
if (!animation) {
cc.log("Spine: Animation not found:" + name);
Expand Down Expand Up @@ -302,13 +330,102 @@ sp.SkeletonAnimation = sp.Skeleton.extend(/** @lends sp.SkeletonAnimation# */{
*/
update: function (dt) {
this._super(dt);

dt *= this._timeScale;
this._state.update(dt);
this._state.apply(this._skeleton);
this._skeleton.updateWorldTransform();
},

/**
* Set the start event listener.
* @param {function} listener
*/
setStartListener: function(listener){
this._startListener = listener;
},

/**
* Set the end event listener.
* @param {function} listener
*/
setEndListener: function(listener) {
this._endListener = listener;
},

setCompleteListener: function(listener) {
this._completeListener = listener;
},

setEventListener: function(listener){
this._eventListener = listener;
},

setTrackStartListener: function(entry, listener){
sp.TrackEntryListeners.getListeners(entry).startListener = listener;
},

setTrackEndListener: function(entry, listener){
sp.TrackEntryListeners.getListeners(entry).endListener = listener;
},

setTrackCompleteListener: function(entry, listener){
sp.TrackEntryListeners.getListeners(entry).completeListener = listener;
},

setTrackEventListener: function(entry, listener){
sp.TrackEntryListeners.getListeners(entry).eventListener = listener;
},

onTrackEntryEvent: function(traceIndex, type, event, loopCount){
var entry = this._state.getCurrent(traceIndex);
if(!entry.rendererObject)
return;
var listeners = entry.rendererObject;
switch (type){
case sp.ANIMATION_EVENT_TYPE.START:
if(listeners.startListener)
listeners.startListener(traceIndex);
break;
case sp.ANIMATION_EVENT_TYPE.END:
if(listeners.endListener)
listeners.endListener(traceIndex);
break;
case sp.ANIMATION_EVENT_TYPE.COMPLETE:
if(listeners.completeListener)
listeners.completeListener(traceIndex, loopCount);
break;
case sp.ANIMATION_EVENT_TYPE.EVENT:
if(listeners.eventListener)
listeners.eventListener(traceIndex, event);
break;
}
},

onAnimationStateEvent: function(trackIndex, type, event, loopCount) {
switch(type){
case sp.ANIMATION_EVENT_TYPE.START:
if(this._startListener)
this._startListener(trackIndex);
break;
case sp.ANIMATION_EVENT_TYPE.END:
if(this._endListener)
this._endListener(trackIndex);
break;
case sp.ANIMATION_EVENT_TYPE.COMPLETE:
if(this._completeListener)
this._completeListener(trackIndex, loopCount);
break;
case sp.ANIMATION_EVENT_TYPE.EVENT:
if(this._eventListener)
this._eventListener(trackIndex, event);
break;
}
},

getState: function(){
return this._state;
},

_onAnimationStateStart: function (trackIndex) {
this._animationStateCallback(trackIndex, sp.ANIMATION_EVENT_TYPE.START, null, 0);
},
Expand All @@ -322,6 +439,7 @@ sp.SkeletonAnimation = sp.Skeleton.extend(/** @lends sp.SkeletonAnimation# */{
this._animationStateCallback(trackIndex, sp.ANIMATION_EVENT_TYPE.EVENT, event, 0);
},
_animationStateCallback: function (trackIndex, type, event, loopCount) {
this.onAnimationStateEvent(trackIndex, type, event, loopCount);
if (this._target && this._callback) {
this._callback.call(this._target, this, trackIndex, type, event, loopCount)
}
Expand Down
13 changes: 7 additions & 6 deletions extensions/spine/CCSkeletonWebGLRenderCmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,11 +82,13 @@

switch(slot.attachment.type) {
case sp.ATTACHMENT_TYPE.REGION:
sp._regionAttachment_updateQuad(attachment, slot, quad, node._premultipliedAlpha);
break;
sp._regionAttachment_updateQuad(attachment, slot, quad, node._premultipliedAlpha);
break;
case sp.ATTACHMENT_TYPE.MESH:
sp._meshAttachment_updateQuad(attachment, slot, quad, node._premultipliedAlpha);
break;
sp._meshAttachment_updateQuad(attachment, slot, quad, node._premultipliedAlpha);
break;
case sp.ATTACHMENT_TYPE.SKINNED_MESH:
break;
}

textureAtlas.updateQuad(quad, quadCount);
Expand All @@ -98,9 +100,8 @@
}

if (node._debugBones || node._debugSlots) {

cc.kmGLMatrixMode(cc.KM_GL_MODELVIEW);
//cc.kmGLPushMatrixWitMat4(node._stackMatrix);
//cc.kmGLPushMatrixWitMat4(this._stackMatrix);
cc.current_stack.stack.push(cc.current_stack.top);
cc.current_stack.top = this._stackMatrix;

Expand Down
Loading