Skip to content

Commit

Permalink
refine
Browse files Browse the repository at this point in the history
  • Loading branch information
bofeng-song committed Nov 16, 2023
1 parent 1aea419 commit e188105
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 12 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 @@ -1189,7 +1189,7 @@ declare namespace spine {
isCache: boolean;
isDelete: boolean;
enable: boolean;
setTrackListener: any;
setTrackEntryListener: any;
initSkeleton(data: SkeletonData);
getAnimationState();
setAnimation(trackIndex: number, name: string, loop: boolean): spine.TrackEntry | null;
Expand Down
12 changes: 8 additions & 4 deletions cocos/spine/track-entry-listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/

import spine from './lib/spine-core.js';
import { warn } from '../core';

let _listener_ID = 0;
let _track_ID = 0;
Expand All @@ -43,7 +44,7 @@ export class TrackEntryListeners {
if (!entry.listener) {
entry.listener = new TrackEntryListeners() as any;
const id = ++_track_ID;
instance.setTrackListener(id, entry);
instance.setTrackEntryListener(id, entry);
TrackEntryListeners._trackSet.set(id, entry);
}
return entry.listener;
Expand All @@ -58,11 +59,10 @@ export class TrackEntryListeners {
}
}

static emitTrackListener (id: number, entry: spine.TrackEntry, event: spine.Event, eventType: spine.EventType): void {
static emitTrackEntryListener (id: number, entry: spine.TrackEntry, event: spine.Event, eventType: spine.EventType): void {
const curTrack = this._trackSet.get(id);
if (!curTrack) return;
// eslint-disable-next-line default-case
switch (eventType as number) {
switch (eventType) {
case spine.EventType.start:
if (curTrack.listener.start) {
curTrack.listener.start(entry);
Expand All @@ -83,6 +83,7 @@ export class TrackEntryListeners {
curTrack.listener.dispose(entry);
}
this._trackSet.delete(id);
curTrack.listener = null as any;
break;
case spine.EventType.complete:
if (curTrack.listener.complete) {
Expand All @@ -94,6 +95,9 @@ export class TrackEntryListeners {
curTrack.listener.event(entry, event);
}
break;
default:
warn('TrackEntry doesn\'t handled', eventType);
break;
}
}

Expand Down
2 changes: 1 addition & 1 deletion native/cocos/editor-support/spine-wasm/library_spine.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ mergeInto(LibraryManager.library, {
var eventType = wasmUtil.getCurrentEventType();
var trackEntry = wasmUtil.getCurrentTrackEntry();
var event = wasmUtil.getCurrentEvent();
globalThis.TrackEntryListeners.emitTrackListener(listenerID, trackEntry, event, eventType.value);
globalThis.TrackEntryListeners.emitTrackEntryListener(listenerID, trackEntry, event, eventType.value);
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,9 @@ void SpineSkeletonInstance::setListener(uint32_t listenerID, uint32_t type) {
}
}

void SpineSkeletonInstance::setTrackListener(uint32_t trackId, TrackEntry *entry) {
void SpineSkeletonInstance::setTrackEntryListener(uint32_t trackId, TrackEntry *entry) {
if (!entry->getRendererObject()) {
_trackListenerID = trackId;
_trackEntryListenerID = trackId;
entry->setRendererObject(this);
entry->setListener(trackEntryCallback);
}
Expand All @@ -451,7 +451,7 @@ void SpineSkeletonInstance::setDebugMode(bool debug) {

void SpineSkeletonInstance::onTrackEntryEvent(TrackEntry *entry, EventType type, Event *event) {
if (!entry->getRendererObject()) return;
SpineWasmUtil::s_listenerID = _trackListenerID;
SpineWasmUtil::s_listenerID = _trackEntryListenerID;
SpineWasmUtil::s_currentType = type;
SpineWasmUtil::s_currentEntry = entry;
SpineWasmUtil::s_currentEvent = event;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SpineSkeletonInstance {
AnimationState *getAnimationState();
void setMix(const std::string &from, const std::string &to, float duration);
void setListener(uint32_t listenerID, uint32_t type);
void setTrackListener(uint32_t trackId, TrackEntry *entry);
void setTrackEntryListener(uint32_t trackId, TrackEntry *entry);
void onAnimationStateEvent(TrackEntry *entry, EventType type, Event *event);
void onTrackEntryEvent(TrackEntry *entry, EventType type, Event *event);
std::vector<SpineDebugShape> &getDebugShapes();
Expand All @@ -79,7 +79,7 @@ class SpineSkeletonInstance {
uint32_t _disposeListenerID = 0;
uint32_t _completeListenerID = 0;
uint32_t _eventListenerID = 0;
uint32_t _trackListenerID = 0;
uint32_t _trackEntryListenerID = 0;
UserData _userData;
std::vector<SpineDebugShape> _debugShapes{};
std::map<Slot *, uint32_t> slotTextureSet{};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1604,7 +1604,7 @@ EMSCRIPTEN_BINDINGS(spine) {
.function("getAnimationState", &SpineSkeletonInstance::getAnimationState, allow_raw_pointer<AnimationState>())
.function("setMix", &SpineSkeletonInstance::setMix)
.function("setListener", &SpineSkeletonInstance::setListener)
.function("setTrackListener", &SpineSkeletonInstance::setTrackListener, allow_raw_pointer<TrackEntry *>())
.function("setTrackEntryListener", &SpineSkeletonInstance::setTrackEntryListener, allow_raw_pointer<TrackEntry *>())
.function("setDebugMode", &SpineSkeletonInstance::setDebugMode)
.function("getDebugShapes", &SpineSkeletonInstance::getDebugShapes)
.function("resizeSlotRegion", &SpineSkeletonInstance::resizeSlotRegion)
Expand Down

0 comments on commit e188105

Please sign in to comment.