Skip to content

Commit

Permalink
Fix spine's listener can not be removed (#17342)
Browse files Browse the repository at this point in the history
* Fix spine's listener can not be removed
  • Loading branch information
bofeng-song authored Jul 9, 2024
1 parent ea0018b commit 141ebfb
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 90 deletions.
2 changes: 1 addition & 1 deletion cocos/spine/lib/spine-core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1202,7 +1202,7 @@ declare namespace spine {
setJitterEffect(jitter: spine.VertexEffect);
setSwirlEffect(swirl: spine.VertexEffect);
updateRenderData();
setListener(id: number, type: number);
setListener(id: number);
setDebugMode(debug: boolean);
getDebugShapes();
resizeSlotRegion(slotName: string, width: number, height: number, createNew: boolean);
Expand Down
15 changes: 2 additions & 13 deletions cocos/spine/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1689,6 +1689,8 @@ export class Skeleton extends UIRenderer {
protected _ensureListener (): void {
if (!this._listener) {
this._listener = new TrackEntryListeners();
const listenerID = TrackEntryListeners.addListener(this._listener);
this._instance!.setListener(listenerID);
}
}

Expand All @@ -1699,8 +1701,6 @@ export class Skeleton extends UIRenderer {
*/
public setStartListener (listener: TrackListener): void {
this._ensureListener();
const listenerID = TrackEntryListeners.addListener(listener);
this._instance!.setListener(listenerID, spine.EventType.start);
this._listener!.start = listener;
}

Expand All @@ -1711,8 +1711,6 @@ export class Skeleton extends UIRenderer {
*/
public setInterruptListener (listener: TrackListener): void {
this._ensureListener();
const listenerID = TrackEntryListeners.addListener(listener);
this._instance!.setListener(listenerID, spine.EventType.interrupt);
this._listener!.interrupt = listener;
}

Expand All @@ -1723,8 +1721,6 @@ export class Skeleton extends UIRenderer {
*/
public setEndListener (listener: TrackListener): void {
this._ensureListener();
const listenerID = TrackEntryListeners.addListener(listener);
this._instance!.setListener(listenerID, spine.EventType.end);
this._listener!.end = listener;
}

Expand All @@ -1735,8 +1731,6 @@ export class Skeleton extends UIRenderer {
*/
public setDisposeListener (listener: TrackListener): void {
this._ensureListener();
const listenerID = TrackEntryListeners.addListener(listener);
this._instance!.setListener(listenerID, spine.EventType.dispose);
this._listener!.dispose = listener;
}

Expand All @@ -1747,8 +1741,6 @@ export class Skeleton extends UIRenderer {
*/
public setCompleteListener (listener: TrackListener): void {
this._ensureListener();
const listenerID = TrackEntryListeners.addListener(listener);
this._instance!.setListener(listenerID, spine.EventType.complete);
this._listener!.complete = listener;
}

Expand All @@ -1759,8 +1751,6 @@ export class Skeleton extends UIRenderer {
*/
public setEventListener (listener: TrackListener2): void {
this._ensureListener();
const listenerID = TrackEntryListeners.addListener(listener);
this._instance!.setListener(listenerID, spine.EventType.event);
this._listener!.event = listener;
}

Expand Down Expand Up @@ -1813,7 +1803,6 @@ export class Skeleton extends UIRenderer {
public setTrackCompleteListener (entry: spine.TrackEntry, listener: TrackListener2): void {
const onComplete = (trackEntry: spine.TrackEntry): void => {
const loopCount = Math.floor(trackEntry.trackTime / trackEntry.animationEnd);
const listenerID = TrackEntryListeners.addListener(listener);
listener(trackEntry, loopCount);
// this._instance.setListener(listenerID, spine.EventType.event);
// this._listener!.event = listener;
Expand Down
44 changes: 38 additions & 6 deletions cocos/spine/track-entry-listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,44 @@ export class TrackEntryListeners {
return entry.listener;
}

static emitListener (id: number, entry: spine.TrackEntry, event: spine.Event): void {
static emitListener (id: number, entry: spine.TrackEntry, event: spine.Event, eventType: spine.EventType): void {
const listener = TrackEntryListeners._listenerSet.get(id);
if (!listener) return;
const listener2 = listener as TrackListener2;
if (listener2) {
listener2(entry, event);
switch (eventType) {
case spine.EventType.event:
if (listener.event) {
listener.event(entry, event);
}
break;
case spine.EventType.start:
if (listener.start) {
listener.start(entry);
}
break;
case spine.EventType.interrupt:
if (listener.interrupt) {
listener.interrupt(entry);
}
break;
case spine.EventType.end:
if (listener.end) {
listener.end(entry);
}
break;
case spine.EventType.dispose:
if (listener.dispose) {
listener.dispose(entry);
}
this._listenerSet.delete(id);
break;
case spine.EventType.complete:
if (listener.complete) {
listener.complete(entry);
}
break;
default:
warn('emitListener doesn\'t handled', eventType);
break;
}
}

Expand Down Expand Up @@ -101,13 +133,13 @@ export class TrackEntryListeners {
}
}

static addListener (listener: CommonTrackEntryListener): number {
static addListener (listener: TrackEntryListeners): number {
const id = ++_listener_ID;
TrackEntryListeners._listenerSet.set(id, listener);
return id;
}

private static _listenerSet = new Map<number, CommonTrackEntryListener>();
private static _listenerSet = new Map<number, TrackEntryListeners>();
private static _trackSet = new Map<number, spine.TrackEntry>();
}

Expand Down
3 changes: 2 additions & 1 deletion native/cocos/editor-support/spine-wasm/library_spine.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ mergeInto(LibraryManager.library, {
var listenerID = wasmUtil.getCurrentListenerID();
var trackEntry = wasmUtil.getCurrentTrackEntry();
var event = wasmUtil.getCurrentEvent();
globalThis.TrackEntryListeners.emitListener(listenerID, trackEntry, event);
var eventType = wasmUtil.getCurrentEventType();
globalThis.TrackEntryListeners.emitListener(listenerID, trackEntry, event, eventType.value);
},

spineTrackListenerCallback: function() {
Expand Down
65 changes: 3 additions & 62 deletions native/cocos/editor-support/spine-wasm/spine-skeleton-instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,6 @@ void SpineSkeletonInstance::setSkin(const std::string &name) {
if (!_skeleton) return;
_skeleton->setSkin(name.c_str());
_skeleton->setSlotsToSetupPose();
_animState->apply(*_skeleton);
_skeleton->updateWorldTransform();
}

void SpineSkeletonInstance::updateAnimation(float dltTime) {
Expand Down Expand Up @@ -412,29 +410,6 @@ void SpineSkeletonInstance::setMix(const std::string &from, const std::string &t
_animStateData->setMix(from.c_str(), to.c_str(), duration);
}

void SpineSkeletonInstance::setListener(uint32_t listenerID, uint32_t type) {
switch (type) {
case EventType_Start:
_startListenerID = listenerID;
break;
case EventType_Interrupt:
_interruptListenerID = listenerID;
break;
case EventType_End:
_endListenerID = listenerID;
break;
case EventType_Dispose:
_disposeListenerID = listenerID;
break;
case EventType_Complete:
_completeListenerID = listenerID;
break;
case EventType_Event:
_eventListenerID = listenerID;
break;
}
}

void SpineSkeletonInstance::setTrackEntryListener(uint32_t trackId, TrackEntry *entry) {
if (!entry->getRendererObject()) {
_trackEntryListenerID = trackId;
Expand Down Expand Up @@ -464,43 +439,9 @@ void SpineSkeletonInstance::onAnimationStateEvent(TrackEntry *entry, EventType t
SpineWasmUtil::s_currentType = type;
SpineWasmUtil::s_currentEntry = entry;
SpineWasmUtil::s_currentEvent = event;
switch (type) {
case EventType_Start:
if (_startListenerID != 0) {
SpineWasmUtil::s_listenerID = _startListenerID;
spineListenerCallBackFromJS();
}
break;
case EventType_Interrupt:
if (_interruptListenerID != 0) {
SpineWasmUtil::s_listenerID = _interruptListenerID;
spineListenerCallBackFromJS();
}
break;
case EventType_End:
if (_endListenerID != 0) {
SpineWasmUtil::s_listenerID = _endListenerID;
spineListenerCallBackFromJS();
}
break;
case EventType_Dispose:
if (_disposeListenerID != 0) {
SpineWasmUtil::s_listenerID = _disposeListenerID;
spineListenerCallBackFromJS();
}
break;
case EventType_Complete:
if (_completeListenerID != 0) {
SpineWasmUtil::s_listenerID = _completeListenerID;
spineListenerCallBackFromJS();
}
break;
case EventType_Event:
if (_eventListenerID != 0) {
SpineWasmUtil::s_listenerID = _eventListenerID;
spineListenerCallBackFromJS();
}
break;
if (_eventListenerID != 0) {
SpineWasmUtil::s_listenerID = _eventListenerID;
spineListenerCallBackFromJS();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class SpineSkeletonInstance {
void clearEffect();
AnimationState *getAnimationState();
void setMix(const std::string &from, const std::string &to, float duration);
void setListener(uint32_t listenerID, uint32_t type);
inline void setListener(uint32_t listenerID) { _eventListenerID = listenerID;}
void setTrackEntryListener(uint32_t trackId, TrackEntry *entry);
void onAnimationStateEvent(TrackEntry *entry, EventType type, Event *event);
void onTrackEntryEvent(TrackEntry *entry, EventType type, Event *event);
Expand All @@ -73,11 +73,6 @@ class SpineSkeletonInstance {
SkeletonClipping *_clipper = nullptr;
VertexEffect *_effect = nullptr;
SpineModel *_model = nullptr;
uint32_t _startListenerID = 0;
uint32_t _interruptListenerID = 0;
uint32_t _endListenerID = 0;
uint32_t _disposeListenerID = 0;
uint32_t _completeListenerID = 0;
uint32_t _eventListenerID = 0;
uint32_t _trackEntryListenerID = 0;
UserData _userData;
Expand Down
2 changes: 1 addition & 1 deletion native/external-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
"type": "github",
"owner": "cocos-creator",
"name": "engine-native-external",
"checkout": "v3.8.2-23"
"checkout": "v3.8.4-1"
}
}

0 comments on commit 141ebfb

Please sign in to comment.