Skip to content
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

Fix spine's listener can not be removed #17342

Merged
merged 3 commits into from
Jul 9, 2024
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
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 @@ -45,11 +45,11 @@
duration: number;
name: string;
timelines: Array<Timeline>;
apply(skeleton: Skeleton, lastTime: number, time: number, loop: boolean, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 48 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 166. Maximum allowed is 150
hasTimeline(id: number): boolean;
}
interface Timeline {
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 52 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
getPropertyId(): number;
}
enum MixBlend {
Expand Down Expand Up @@ -92,7 +92,7 @@
getCurveType(frameIndex: number): number;
setCurve(frameIndex: number, cx1: number, cy1: number, cx2: number, cy2: number): void;
getCurvePercent(frameIndex: number, percent: number): number;
abstract apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 95 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 160. Maximum allowed is 150
}
class RotateTimeline extends CurveTimeline {
static ENTRIES: number;
Expand All @@ -104,7 +104,7 @@
constructor(frameCount: number);
getPropertyId(): number;
setFrame(frameIndex: number, time: number, degrees: number): void;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 107 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class TranslateTimeline extends CurveTimeline {
static readonly ENTRIES: number;
Expand All @@ -112,17 +112,17 @@
constructor(frameCount: number);
getPropertyId(): number;
setFrame(frameIndex: number, time: number, x: number, y: number): void;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 115 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class ScaleTimeline extends TranslateTimeline {
constructor(frameCount: number);
getPropertyId(): number;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 120 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class ShearTimeline extends TranslateTimeline {
constructor(frameCount: number);
getPropertyId(): number;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 125 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class ColorTimeline extends CurveTimeline {
static ENTRIES: number;
Expand All @@ -133,7 +133,7 @@
getSlotIndex(): number;
setSlotIndex(inValue: number): void;
setFrame(frameIndex: number, time: number, r: number, g: number, b: number, a: number): void;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 136 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class TwoColorTimeline extends CurveTimeline {
static readonly ENTRIES: number;
Expand All @@ -144,7 +144,7 @@
getSlotIndex(): number;
setSlotIndex(inValue: number): void;
setFrame(frameIndex: number, time: number, r: number, g: number, b: number, a: number, r2: number, g2: number, b2: number): void;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 147 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class AttachmentTimeline implements Timeline {
slotIndex: number;
Expand All @@ -157,7 +157,7 @@
setSlotIndex(inValue: number): void;
getAttachmentNames(): Array<string>;
setFrame(frameIndex: number, time: number, attachmentName: string): void;
apply(skeleton: Skeleton, lastTime: number, time: number, events: Array<Event>, alpha: number, blend: MixBlend, direction: MixDirection): void;

Check warning on line 160 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

This line has a length of 151. Maximum allowed is 150
}
class DeformTimeline extends CurveTimeline {
slotIndex: number;
Expand Down Expand Up @@ -779,7 +779,7 @@
MipMapNearestNearest = 9984,
MipMapLinearNearest = 9985,
MipMapNearestLinear = 9986,
MipMapLinearLinear = 9987

Check failure on line 782 in cocos/spine/lib/spine-core.d.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Duplicate enum member value 9987
}
enum TextureWrap {
MirroredRepeat = 33648,
Expand Down Expand Up @@ -1202,7 +1202,7 @@
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"
}
}
Loading