Skip to content

Commit

Permalink
Fix spine can not be paused while working at realTime mode (#16672)
Browse files Browse the repository at this point in the history
* Fix spine can not be paused while running in realTime mode
  • Loading branch information
bofeng-song authored Jan 10, 2024
1 parent 288a149 commit ddd45f9
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 104 deletions.
1 change: 1 addition & 0 deletions cocos/spine/lib/spine-core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1191,6 +1191,7 @@ declare namespace spine {
isDelete: boolean;
enable: boolean;
setTrackEntryListener: any;
destroy();
initSkeleton(data: SkeletonData);
getAnimationState();
setAnimation(trackIndex: number, name: string, loop: boolean): spine.TrackEntry | null;
Expand Down
17 changes: 10 additions & 7 deletions cocos/spine/skeleton-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export interface AnimationFrame {
}

export class AnimationCache {
protected _instance: spine.SkeletonInstance = null!;
protected _instance: spine.SkeletonInstance | null = null;
protected _state: spine.AnimationState = null!;
protected _skeletonData: spine.SkeletonData = null!;
protected _skeleton: spine.Skeleton = null!;
Expand Down Expand Up @@ -117,7 +117,7 @@ export class AnimationCache {

public setSkin (skinName: string): void {
if (this._skeleton) this._skeleton.setSkinByName(skinName);
this._instance.setSkin(skinName);
this._instance!.setSkin(skinName);
}

public setAnimation (animationName: string): void {
Expand All @@ -134,7 +134,7 @@ export class AnimationCache {
}
this._maxFrameIdex = Math.floor((animation as any).duration / FrameTime);
if (this._maxFrameIdex <= 0) this._maxFrameIdex = 1;
this._instance.setAnimation(0, animationName, false);
this._instance!.setAnimation(0, animationName, false);
}

public updateToFrame (frameIdx: number): void {
Expand All @@ -145,8 +145,8 @@ export class AnimationCache {
// Solid update frame rate 1/60.
this._frameIdx++;
this.totalTime += FrameTime;
this._instance.updateAnimation(FrameTime);
const model = this._instance.updateRenderData();
this._instance!.updateAnimation(FrameTime);
const model = this._instance!.updateRenderData();
this.updateRenderData(this._frameIdx, model);
if (this._frameIdx >= this._maxFrameIdex) {
this.isCompleted = true;
Expand Down Expand Up @@ -234,7 +234,7 @@ export class AnimationCache {
}
}
const listener = skeletonInfo?.listener;
this._instance.setAnimation(0, this._animationName!, false);
this._instance!.setAnimation(0, this._animationName!, false);
this.bind(listener!);

// record cur animation cache
Expand Down Expand Up @@ -302,7 +302,10 @@ export class AnimationCache {
}

public destroy (): void {
spine.SkeletonSystem.destroySpineInstance(this._instance);
if (this._instance) {
this._instance.destroy();
this._instance = null;
}
}
}

Expand Down
1 change: 0 additions & 1 deletion cocos/spine/skeleton-system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export class SkeletonSystem extends System {
if (!this._skeletons) {
return;
}
if (!EDITOR_NOT_IN_PREVIEW && !JSB) spine.SkeletonSystem.updateAnimation(dt);
this._skeletons.forEach((skeleton) => {
skeleton.updateAnimation(dt);
});
Expand Down
64 changes: 35 additions & 29 deletions cocos/spine/skeleton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ export class Skeleton extends UIRenderer {

protected _runtimeData: spine.SkeletonData | null = null;
public _skeleton: spine.Skeleton = null!;
protected _instance: spine.SkeletonInstance = null!;
protected _instance: spine.SkeletonInstance | null = null;
protected _state: spine.AnimationState = null!;
protected _textures: Texture2D[] = [];
private _skeletonInfo: SkeletonCacheItemInfo | null = null;
Expand Down Expand Up @@ -491,7 +491,7 @@ export class Skeleton extends UIRenderer {
set premultipliedAlpha (v: boolean) {
if (v !== this._premultipliedAlpha) {
this._premultipliedAlpha = v;
this._instance.setPremultipliedAlpha(v);
this._instance!.setPremultipliedAlpha(v);
this.markForUpdateRenderData();
}
}
Expand Down Expand Up @@ -710,8 +710,9 @@ export class Skeleton extends UIRenderer {
//if (this._cacheMode == AnimationCacheMode.PRIVATE_CACHE) this._animCache?.destroy();
this._animCache = null;
SkeletonSystem.getInstance().remove(this);
if (!JSB) {
spine.SkeletonSystem.destroySpineInstance(this._instance);
if (!JSB && this._instance) {
this._instance.destroy();
this._instance = null;
}
this._destroySkeletonInfo(this._skeletonCache);
this._skeletonCache = null;
Expand Down Expand Up @@ -812,9 +813,9 @@ export class Skeleton extends UIRenderer {
this._skeleton = this._skeletonInfo.skeleton!;
}
} else {
this._skeleton = this._instance.initSkeleton(skeletonData);
this._state = this._instance.getAnimationState();
this._instance.setPremultipliedAlpha(this._premultipliedAlpha);
this._skeleton = this._instance!.initSkeleton(skeletonData);
this._state = this._instance!.getAnimationState();
this._instance!.setPremultipliedAlpha(this._premultipliedAlpha);
}
// Recreate render data and mark dirty
this._flushAssembler();
Expand Down Expand Up @@ -929,7 +930,7 @@ export class Skeleton extends UIRenderer {
}
} else {
this._animationName = name;
trackEntry = this._instance.setAnimation(trackIndex, name, loop);
trackEntry = this._instance!.setAnimation(trackIndex, name, loop);
}
this.markForUpdateRenderData();
return trackEntry;
Expand Down Expand Up @@ -1007,7 +1008,7 @@ export class Skeleton extends UIRenderer {
*/
public setSkin (name: string): void {
if (this._skeleton) this._skeleton.setSkinByName(name);
this._instance.setSkin(name);
this._instance!.setSkin(name);
if (this.isAnimationCached()) {
if (this._animCache) {
this._animCache.setSkin(name);
Expand Down Expand Up @@ -1050,6 +1051,8 @@ export class Skeleton extends UIRenderer {
return;
}
this._updateCache(dt);
} else {
this._instance!.updateAnimation(dt);
}
}

Expand Down Expand Up @@ -1112,7 +1115,7 @@ export class Skeleton extends UIRenderer {
const model = this._curFrame.model;
return model;
} else {
const model = this._instance.updateRenderData();
const model = this._instance!.updateRenderData();
return model;
}
}
Expand Down Expand Up @@ -1444,7 +1447,7 @@ export class Skeleton extends UIRenderer {
return;
}
if (this._state) {
this._instance.setMix(fromAnimation, toAnimation, duration);
this._instance!.setMix(fromAnimation, toAnimation, duration);
//this._state.data.setMix(fromAnimation, toAnimation, duration);
}
}
Expand Down Expand Up @@ -1577,7 +1580,7 @@ export class Skeleton extends UIRenderer {
this.destroyRenderData();
if (!JSB) {
if (!this.isAnimationCached()) {
this._instance.setUseTint(this._useTint);
this._instance!.setUseTint(this._useTint);
}
}
if (this._assembler && this._skeleton) {
Expand Down Expand Up @@ -1607,7 +1610,7 @@ export class Skeleton extends UIRenderer {
if (this.isAnimationCached()) {
warn('Debug bones or slots is invalid in cached mode');
} else if (!JSB) {
this._instance.setDebugMode(true);
this._instance!.setDebugMode(true);
}
} else if (this._debugRenderer) {
this._debugRenderer.node.destroy();
Expand Down Expand Up @@ -1655,7 +1658,7 @@ export class Skeleton extends UIRenderer {
const r = this._color.r / 255.0;
const g = this._color.g / 255.0;
const b = this._color.b / 255.0;
this._instance.setColor(r, g, b, a);
this._instance!.setColor(r, g, b, a);
}

/**
Expand All @@ -1664,6 +1667,9 @@ export class Skeleton extends UIRenderer {
* @param effectDelegate @en Vertex effect delegate. @zh 顶点特效代理。
*/
public setVertexEffectDelegate (effectDelegate: VertexEffectDelegate | null | undefined): void {
if (!this._instance) {
return;
}
if (!effectDelegate) {
this._instance.clearEffect();
return;
Expand Down Expand Up @@ -1692,7 +1698,7 @@ 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._instance!.setListener(listenerID, spine.EventType.start);
this._listener!.start = listener;
}

Expand All @@ -1704,7 +1710,7 @@ 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._instance!.setListener(listenerID, spine.EventType.interrupt);
this._listener!.interrupt = listener;
}

Expand All @@ -1716,7 +1722,7 @@ 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._instance!.setListener(listenerID, spine.EventType.end);
this._listener!.end = listener;
}

Expand All @@ -1728,7 +1734,7 @@ 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._instance!.setListener(listenerID, spine.EventType.dispose);
this._listener!.dispose = listener;
}

Expand All @@ -1740,7 +1746,7 @@ 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._instance!.setListener(listenerID, spine.EventType.complete);
this._listener!.complete = listener;
}

Expand All @@ -1752,7 +1758,7 @@ 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._instance!.setListener(listenerID, spine.EventType.event);
this._listener!.event = listener;
}

Expand All @@ -1763,7 +1769,7 @@ export class Skeleton extends UIRenderer {
* @param listener @en Listener for registering callback functions. @zh 监听器对象,可注册回调方法。
*/
public setTrackStartListener (entry: spine.TrackEntry, listener: TrackListener): void {
TrackEntryListeners.getListeners(entry, this._instance).start = listener;
TrackEntryListeners.getListeners(entry, this._instance!).start = listener;
}

/**
Expand All @@ -1773,7 +1779,7 @@ export class Skeleton extends UIRenderer {
* @param listener @en Listener for registering callback functions. @zh 监听器对象,可注册回调方法。
*/
public setTrackInterruptListener (entry: spine.TrackEntry, listener: TrackListener): void {
TrackEntryListeners.getListeners(entry, this._instance).interrupt = listener;
TrackEntryListeners.getListeners(entry, this._instance!).interrupt = listener;
}

/**
Expand All @@ -1783,7 +1789,7 @@ export class Skeleton extends UIRenderer {
* @param listener @en Listener for registering callback functions. @zh 监听器对象,可注册回调方法。
*/
public setTrackEndListener (entry: spine.TrackEntry, listener: TrackListener): void {
TrackEntryListeners.getListeners(entry, this._instance).end = listener;
TrackEntryListeners.getListeners(entry, this._instance!).end = listener;
}

/**
Expand All @@ -1793,7 +1799,7 @@ export class Skeleton extends UIRenderer {
* @param listener @en Listener for registering callback functions. @zh 监听器对象,可注册回调方法。
*/
public setTrackDisposeListener (entry: spine.TrackEntry, listener: TrackListener): void {
TrackEntryListeners.getListeners(entry, this._instance).dispose = listener;
TrackEntryListeners.getListeners(entry, this._instance!).dispose = listener;
}

/**
Expand All @@ -1810,7 +1816,7 @@ export class Skeleton extends UIRenderer {
// this._instance.setListener(listenerID, spine.EventType.event);
// this._listener!.event = listener;
};
TrackEntryListeners.getListeners(entry, this._instance).complete = onComplete;
TrackEntryListeners.getListeners(entry, this._instance!).complete = onComplete;
}

/**
Expand All @@ -1820,14 +1826,14 @@ export class Skeleton extends UIRenderer {
* @param listener @en Listener for registering callback functions. @zh 监听器对象,可注册回调方法。
*/
public setTrackEventListener (entry: spine.TrackEntry, listener: TrackListener|TrackListener2): void {
TrackEntryListeners.getListeners(entry, this._instance).event = listener;
TrackEntryListeners.getListeners(entry, this._instance!).event = listener;
}

/**
* @engineInternal
*/
public getDebugShapes (): any {
return this._instance.getDebugShapes();
return this._instance!.getDebugShapes();
}

/**
Expand All @@ -1852,7 +1858,7 @@ export class Skeleton extends UIRenderer {
const width = tex2d.width;
const height = tex2d.height;
const createNewAttachment = createNew || false;
this._instance.resizeSlotRegion(slotName, width, height, createNewAttachment);
this._instance!.resizeSlotRegion(slotName, width, height, createNewAttachment);
if (!this._slotTextures) this._slotTextures = new Map<number, Texture2D>();
let textureID = 0;
this._slotTextures.forEach((value, key) => {
Expand All @@ -1862,7 +1868,7 @@ export class Skeleton extends UIRenderer {
textureID = ++_slotTextureID;
this._slotTextures.set(textureID, tex2d);
}
this._instance.setSlotTexture(slotName, textureID);
this._instance!.setSlotTexture(slotName, textureID);
}

private _destroySkeletonInfo (skeletonCache: SkeletonCache | null): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "spine-mesh-data.h"
#include "spine-wasm.h"
#include "util-function.h"
#include "spine-skeleton-system.h"

SlotMesh globalMesh(nullptr, nullptr, 0, 0);

Expand Down Expand Up @@ -34,7 +33,6 @@ static void trackEntryCallback(AnimationState *state, EventType type, TrackEntry

SpineSkeletonInstance::SpineSkeletonInstance() {
_model = new SpineModel();
SpineSkeletonSystem::addSpineInstance(this);
}

SpineSkeletonInstance::~SpineSkeletonInstance() {
Expand All @@ -46,6 +44,10 @@ SpineSkeletonInstance::~SpineSkeletonInstance() {
if (_model) delete _model;
}

void SpineSkeletonInstance::destroy() {
delete this;
}

Skeleton *SpineSkeletonInstance::initSkeleton(SkeletonData *data) {
if (_clipper) delete _clipper;
if (_animState) delete _animState;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class SpineSkeletonInstance {
std::vector<SpineDebugShape> &getDebugShapes();
void resizeSlotRegion(const std::string &slotName, uint32_t width, uint32_t height, bool createNew = false);
void setSlotTexture(const std::string &slotName, uint32_t index);
void destroy();
bool isCache{false};
bool enable{true};
float dtRate{1.0F};
Expand Down
Loading

0 comments on commit ddd45f9

Please sign in to comment.