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 can not be paused while working at realTime mode #16672

Merged
merged 4 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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 @@ -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 @@ -1191,6 +1191,7 @@
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 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 @@

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 @@
}
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 @@
// 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 @@ -176,13 +176,13 @@
const vPtr = model.vPtr;
const vLength = vc * Float32Array.BYTES_PER_ELEMENT * floatStride;
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
vUint8Buf.set(HEAPU8.subarray(vPtr, vPtr + vLength));

Check failure on line 179 in cocos/spine/skeleton-cache.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `ArrayLike<number>`

const iPtr = model.iPtr;
const iLength = Uint16Array.BYTES_PER_ELEMENT * ic;
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
const iUint8Buf = new Uint8Array(iUint16Buf.buffer);
iUint8Buf.set(HEAPU8.subarray(iPtr, iPtr + iLength));

Check failure on line 185 in cocos/spine/skeleton-cache.ts

View workflow job for this annotation

GitHub Actions / Run ESLint

Unsafe argument of type `any` assigned to a parameter of type `ArrayLike<number>`

const modelData = new SpineModel();
modelData.vCount = vc;
Expand Down Expand Up @@ -234,7 +234,7 @@
}
}
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 @@
}

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why delete SpineSkeletonSystem ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Its no need an additional helper class to release the spineSkeletonInstance object.

}

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
Loading