UNPKG

@nativescript/rive

Version:
295 lines 11.7 kB
import { Http, knownFolders, Utils } from '@nativescript/core'; import { RiveViewBase, TypeRiveAlignment, TypeRiveDirection, TypeRiveFit, TypeRiveLoop, autoPlayProperty, srcProperty, fitProperty, alignmentProperty, RiveEvents, inputValueProperty } from './common'; export { TypeRiveAlignment, TypeRiveDirection, TypeRiveFit, TypeRiveLoop } from './common'; function lazy(action) { let _value; return () => _value || (_value = action()); } var Listener = /** @class */ (function (_super) { __extends(Listener, _super); function Listener(owner) { var _this = _super.call(this) || this; _this.owner = owner; return global.__native(_this); } Listener.prototype.notifyPlay = function (name) { var owner = this.owner.get(); if (owner) { owner.events.notifyEvent(RiveEvents.onPlayEvent, { name: name }); } }; Listener.prototype.notifyStop = function (name) { var owner = this.owner.get(); if (owner) { owner.events.notifyEvent(RiveEvents.onStopEvent, { name: name }); } }; Listener.prototype.notifyPause = function (name) { var owner = this.owner.get(); if (owner) { owner.events.notifyEvent(RiveEvents.onPauseEvent, { name: name }); } }; Listener.prototype.notifyLoop = function (name) { var owner = this.owner.get(); if (owner) { owner.events.notifyEvent(RiveEvents.onLoopEndEvent, { name: name, loop: owner.loop }); } }; Listener.prototype.notifyStateChanged = function (stateMachine, stateName) { var owner = this.owner.get(); if (owner) { owner.events.notifyEvent(RiveEvents.stateChangedEvent, { stateMachine: stateMachine, stateName: stateName }); } }; var _a; Listener = __decorate([ Interfaces([org.nativescript.plugins.rive.Rive.Events]), __metadata("design:paramtypes", [typeof (_a = typeof WeakRef !== "undefined" && WeakRef) === "function" ? _a : Object]) ], Listener); return Listener; }(java.lang.Object)); export class RiveView extends RiveViewBase { constructor() { super(); this.events = new RiveEvents(this); } createNativeView() { return new app.rive.runtime.kotlin.RiveAnimationView(this._context, null); } initNativeView() { this.addListener(); } disposeNativeView() { super.disposeNativeView(); } async [srcProperty.setNative](src) { if (!src) { console.log('No rive file specified'); } else if (src[0] === '~' || src[0] === '@') { if (!/.(riv)$/.test(src)) { src += '.riv'; } const filename = src.replace(/^.*[\\/]/, ''); const folder = knownFolders.currentApp().getFolder(src.substring(2).replace(filename, '')); const file = folder.getFile(filename); this.bytes = await file.read(); } else if (src.startsWith(Utils.RESOURCE_PREFIX)) { const resName = src .replace(Utils.RESOURCE_PREFIX + 'raw/', '') .replace(Utils.RESOURCE_PREFIX, '') .replace('.riv', ''); const context = Utils.android.getApplicationContext(); const resId = context.getResources().getIdentifier(resName, 'raw', context.getPackageName()); const inStream = context.getResources().openRawResource(resId); const buffer = Array.create('byte', inStream.available()); inStream.read(buffer); this.bytes = buffer; } else if (src.startsWith('http')) { const file = await Http.getFile(src); this.bytes = await file.read(); } else { console.log('[ui-rive] File not supported'); } if (this.bytes) { this._init(); } } [autoPlayProperty.setNative](autoPlay) { this.nativeViewProtected.setAutoplay(autoPlay); } [fitProperty.getDefault]() { return TypeRiveFit.CONTAIN; } [fitProperty.setNative](value) { this.nativeViewProtected.setFit(this.getFit(value)); } [alignmentProperty.getDefault]() { return TypeRiveAlignment.CENTER; } [alignmentProperty.setNative](value) { this.nativeViewProtected.setAlignment(value); } [inputValueProperty.setNative](value) { this.setInputValue(value); } triggerInput(name) { if (this.nativeViewProtected) { this._setInputValue(name, true); } } triggerInputValue(name, value) { this._setInputValue(name, value); } setInputValue(value) { this._setInputValue(this.input, value); } _setInputValue(name, value) { this.input = name; if (this.input && this.stateMachine && value !== undefined && value !== null && this.nativeViewProtected) { if (Utils.isBoolean(value) || ['true', 'false'].includes(value)) { this.nativeViewProtected.setBooleanState(this.stateMachine, this.input, value === true || value === 'true'); } else { const number = Number(value); if (!isNaN(number)) { this.nativeViewProtected.setNumberState(this.stateMachine, this.input, number); } } } } _init() { if (this.nativeViewProtected) { this.nativeViewProtected.reset(); this.nativeViewProtected.setRiveBytes(this.bytes, this.artboard, this.animation, this.stateMachine, this.autoPlay, false, this.getFit(this.fit), this.getAlignment(this.alignment), this.getLoop(this.loop)); if (this.input) { this.setInputValue(this.inputValue); } } } isPlaying() { return this.nativeViewProtected ? this.nativeViewProtected.isPlaying() : false; } play(loop = TypeRiveLoop.AUTO, direction = TypeRiveDirection.AUTO, settleInitialState = true) { this.nativeViewProtected.play(this.getLoop(loop), this.getDirection(direction), settleInitialState); } playWithAnimations(animationNames = [], loop = TypeRiveLoop.AUTO, direction = TypeRiveDirection.AUTO, areStateMachines = false, settleInitialState = true) { if (Array.isArray(animationNames)) { this.nativeViewProtected.play(this.buildList(animationNames), this.getLoop(loop), this.getDirection(direction), areStateMachines, settleInitialState); } else if (typeof animationNames === 'string') { this.nativeViewProtected.play(animationNames, this.getLoop(loop), this.getDirection(direction), areStateMachines, settleInitialState); } } stop() { if (this.nativeViewProtected) { this.nativeViewProtected.stop(); } } stopWithAnimations(animations = [], areStateMachines = false) { if (Array.isArray(animations)) { this.nativeViewProtected.stop(this.buildList(animations), areStateMachines); } else if (typeof animations === 'string') { this.nativeViewProtected.stop(animations, areStateMachines); } } pause() { if (this.nativeViewProtected) { this.nativeViewProtected.pause(); } } pauseWithAnimations(animations = [], areStateMachines = false) { if (Array.isArray(animations)) { this.nativeViewProtected.pause(this.buildList(animations), areStateMachines); } else if (typeof animations === 'string') { this.nativeViewProtected.pause(animations, areStateMachines); } } reset() { if (this.nativeViewProtected) { this.nativeViewProtected.reset(); } } fireState(stateMachineName, inputName) { if (this.nativeViewProtected) { this.nativeViewProtected.fireState(stateMachineName, inputName); } } getStateMachines() { if (this.nativeViewProtected) { this.nativeViewProtected.getStateMachines(); } } getPlayingStateMachines() { if (this.nativeViewProtected) { this.nativeViewProtected.getPlayingStateMachines(); } } getAnimations() { if (this.nativeViewProtected) { this.nativeViewProtected.getAnimations(); } } getPlayingAnimations() { if (this.nativeViewProtected) { this.nativeViewProtected.getPlayingAnimations(); } } addListener() { if (!this.listener) { this.listener = new org.nativescript.plugins.rive.Rive(); this.listener.setEvents(new Listener(new WeakRef(this))); this.nativeViewProtected.getController().registerListener(this.listener); } } buildList(array) { return java.util.Arrays.asList(array); } getLoop(riveLoop) { switch (riveLoop) { case TypeRiveLoop.ONESHOT: return app.rive.runtime.kotlin.core.Loop.ONESHOT; case TypeRiveLoop.LOOP: return app.rive.runtime.kotlin.core.Loop.LOOP; case TypeRiveLoop.PINGPONG: return app.rive.runtime.kotlin.core.Loop.PINGPONG; default: return app.rive.runtime.kotlin.core.Loop.AUTO; } } getDirection(riveDirection) { switch (riveDirection) { case TypeRiveDirection.BACKWARDS: return app.rive.runtime.kotlin.core.Direction.BACKWARDS; case TypeRiveDirection.FORWARDS: return app.rive.runtime.kotlin.core.Direction.FORWARDS; default: return app.rive.runtime.kotlin.core.Direction.AUTO; } } getFit(riveFit) { switch (riveFit) { case TypeRiveFit.FILL: return app.rive.runtime.kotlin.core.Fit.FILL; case TypeRiveFit.CONTAIN: return app.rive.runtime.kotlin.core.Fit.CONTAIN; case TypeRiveFit.COVER: return app.rive.runtime.kotlin.core.Fit.COVER; case TypeRiveFit.FIT_WIDTH: return app.rive.runtime.kotlin.core.Fit.FIT_WIDTH; case TypeRiveFit.FIT_HEIGHT: return app.rive.runtime.kotlin.core.Fit.FIT_HEIGHT; case TypeRiveFit.SCALE_DOWN: return app.rive.runtime.kotlin.core.Fit.SCALE_DOWN; default: return app.rive.runtime.kotlin.core.Fit.NONE; } } getAlignment(riveAlignment) { switch (riveAlignment) { case TypeRiveAlignment.TOP_LEFT: return app.rive.runtime.kotlin.core.Alignment.TOP_LEFT; case TypeRiveAlignment.TOP_CENTER: return app.rive.runtime.kotlin.core.Alignment.TOP_CENTER; case TypeRiveAlignment.TOP_RIGHT: return app.rive.runtime.kotlin.core.Alignment.TOP_RIGHT; case TypeRiveAlignment.CENTER_LEFT: return app.rive.runtime.kotlin.core.Alignment.CENTER_LEFT; case TypeRiveAlignment.CENTER_RIGHT: return app.rive.runtime.kotlin.core.Alignment.CENTER_RIGHT; case TypeRiveAlignment.BOTTOM_LEFT: return app.rive.runtime.kotlin.core.Alignment.BOTTOM_LEFT; case TypeRiveAlignment.BOTTOM_RIGHT: return app.rive.runtime.kotlin.core.Alignment.BOTTOM_RIGHT; default: return app.rive.runtime.kotlin.core.Alignment.CENTER; } } } //# sourceMappingURL=index.android.js.map