Skip to content

feat(rive): stateChange event for state machine transitions #18

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 packages/rive/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class RiveEvents {
static onPauseEvent = 'onPauseEvent';
static onLoopEndEvent = 'onLoopEndEvent';
static onStopEvent = 'onStopEvent';
static stateChangedEvent = 'stageChangedEvent';
static stateChangedEvent = 'stateChangedEvent';
static receivedInputEvent = 'receivedInputEvent';
static touchBeganEvent = 'touchBeganEvent';
static touchCancelledEvent = 'touchCancelledEvent';
Expand Down
11 changes: 11 additions & 0 deletions packages/rive/index.android.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ class Listener extends java.lang.Object implements app.rive.runtime.kotlin.contr

notifyStateChanged(stateMachine: string, stateName: string): void {
// this.owner.get()?.events.notifyEvent(RiveEvents.stateChangedEvent, { stateMachine, stateName });
const owner = this.owner.get();
if (owner) {
owner.events.notifyEvent(RiveEvents.stateChangedEvent, { stateMachine, stateName });
owner.notify({
eventName: 'stateChange',
object: owner,
detail: { stateMachine, stateName },
});
}
}
}

Expand Down Expand Up @@ -250,6 +259,8 @@ export class RiveView extends RiveViewBase {
// todo investigate.
// this.listener = new Listener(new WeakRef(this));
//this.nativeViewProtected.getController().registerListener(this.listener);
this.listener = new Listener(new WeakRef(this));
this.nativeViewProtected.getController().registerListener(this.listener);
}
}

Expand Down
1 change: 1 addition & 0 deletions packages/rive/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,5 @@ export declare class RiveView extends View {

triggerInput(name: string): void;
triggerInputValue(name: string, value: string | boolean | number | null): void;
on(event: 'stateChange', callback: (ev: { object: RiveView; detail: { stateMachine: any; stateName: string } }) => void, thisArg?: any): void;
}
15 changes: 10 additions & 5 deletions packages/rive/index.ios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,6 @@ export class RiveView extends RiveViewBase {
if (this.ctrl) {
this.ctrl.stop();
}
// if (Array.isArray(animationNames)) {
// this.nativeViewProtected.stop(this.buildList(animationNames), areStateMachines);
// } else if (typeof animationNames === 'string') {
// this.nativeViewProtected.stop(animationNames, areStateMachines);
// }
}

public pause(): void {
Expand Down Expand Up @@ -365,6 +360,11 @@ class RiveStateMachineDelegateImpl extends NSObject implements RiveStateMachineD
const owner = this._owner.deref();
if (owner) {
owner.events.notifyEvent(RiveEvents.stateChangedEvent, { stateMachine, stateName });
owner.notify({
eventName: 'stateChange',
object: owner,
detail: { stateMachine, stateName },
});
}
}

Expand All @@ -373,6 +373,11 @@ class RiveStateMachineDelegateImpl extends NSObject implements RiveStateMachineD
const owner = this._owner.deref();
if (owner) {
owner.events.notifyEvent(RiveEvents.receivedInputEvent, { stateMachine, input });
// owner.notify({
// eventName: 'receivedInput',
// object: owner,
// detail: { stateMachine, input }
// });
}
}

Expand Down