UNPKG

@awayjs/graphics

Version:
326 lines (325 loc) 11.1 kB
import { __extends } from "tslib"; import { AssetBase, AbstractMethodError, RequestAnimationFrame, getTimer } from '@awayjs/core'; import { AnimatorEvent } from '../events/AnimatorEvent'; /** * Dispatched when playback of an animation inside the animator object starts. * * @eventType away3d.events.AnimatorEvent */ //[Event(name="start", type="away3d.events.AnimatorEvent")] /** * Dispatched when playback of an animation inside the animator object stops. * * @eventType away3d.events.AnimatorEvent */ //[Event(name="stop", type="away3d.events.AnimatorEvent")] /** * Dispatched when playback of an animation reaches the end of an animation. * * @eventType away3d.events.AnimatorEvent */ //[Event(name="cycle_complete", type="away3d.events.AnimatorEvent")] /** * Provides an abstract base class for animator classes that control animation output from a data set subtype of <code>AnimationSetBase</code>. * * @see away.animators.AnimationSetBase */ var AnimatorBase = /** @class */ (function (_super) { __extends(AnimatorBase, _super); /** * Creates a new <code>AnimatorBase</code> object. * * @param animationSet The animation data set to be used by the animator object. */ function AnimatorBase(animationSet) { var _this = _super.call(this) || this; _this._autoUpdate = true; _this._time = 0; _this._playbackSpeed = 1; _this._pOwners = new Array(); _this._pAbsoluteTime = 0; _this._animationStates = new Object(); /** * Enables translation of the animated graphics from data returned per frame via the positionDelta property of the active animation node. Defaults to true. * * @see away.animators.IAnimationState#positionDelta */ _this.updatePosition = true; _this._pAnimationSet = animationSet; _this._broadcaster = new RequestAnimationFrame(_this.onEnterFrame, _this); return _this; } AnimatorBase.prototype.getAnimationState = function (node) { var className = node.stateClass; var uID = node.id; if (this._animationStates[uID] == null) this._animationStates[uID] = new className(this, node); return this._animationStates[uID]; }; AnimatorBase.prototype.getAnimationStateByName = function (name) { return this.getAnimationState(this._pAnimationSet.getAnimation(name)); }; Object.defineProperty(AnimatorBase.prototype, "absoluteTime", { /** * Returns the internal absolute time of the animator, calculated by the current time and the playback speed. * * @see #time * @see #playbackSpeed */ get: function () { return this._pAbsoluteTime; }, enumerable: false, configurable: true }); Object.defineProperty(AnimatorBase.prototype, "animationSet", { /** * Returns the animation data set in use by the animator. */ get: function () { return this._pAnimationSet; }, enumerable: false, configurable: true }); Object.defineProperty(AnimatorBase.prototype, "activeState", { /** * Returns the current active animation state. */ get: function () { return this._pActiveState; }, enumerable: false, configurable: true }); Object.defineProperty(AnimatorBase.prototype, "activeAnimation", { /** * Returns the current active animation node. */ get: function () { return this._pAnimationSet.getAnimation(this._pActiveAnimationName); }, enumerable: false, configurable: true }); Object.defineProperty(AnimatorBase.prototype, "activeAnimationName", { /** * Returns the current active animation node. */ get: function () { return this._pActiveAnimationName; }, enumerable: false, configurable: true }); Object.defineProperty(AnimatorBase.prototype, "autoUpdate", { /** * Determines whether the animators internal update mechanisms are active. Used in cases * where manual updates are required either via the <code>time</code> property or <code>update()</code> method. * Defaults to true. * * @see #time * @see #update() */ get: function () { return this._autoUpdate; }, set: function (value) { if (this._autoUpdate == value) return; this._autoUpdate = value; if (this._autoUpdate) this.start(); else this.stop(); }, enumerable: false, configurable: true }); Object.defineProperty(AnimatorBase.prototype, "time", { /** * Gets and sets the internal time clock of the animator. */ get: function () { return this._time; }, set: function (value) { if (this._time == value) return; this.update(value); }, enumerable: false, configurable: true }); /** * Sets the animation phase of the current active state's animation clip(s). * * @param value The phase value to use. 0 represents the beginning of an animation clip, 1 represents the end. */ AnimatorBase.prototype.phase = function (value) { this._pActiveState.phase(value); }; Object.defineProperty(AnimatorBase.prototype, "playbackSpeed", { /** * The amount by which passed time should be scaled. Used to slow down or speed up animations. Defaults to 1. */ get: function () { return this._playbackSpeed; }, set: function (value) { this._playbackSpeed = value; }, enumerable: false, configurable: true }); AnimatorBase.prototype.setRenderState = function (shader, renderable) { throw new AbstractMethodError(); }; /** * Resumes the automatic playback clock controling the active state of the animator. */ AnimatorBase.prototype.start = function () { if (this._isPlaying || !this._autoUpdate) return; this._time = this._pAbsoluteTime = getTimer(); this._isPlaying = true; this._broadcaster.start(); if (!this.hasEventListener(AnimatorEvent.START)) return; if (this._startEvent == null) this._startEvent = new AnimatorEvent(AnimatorEvent.START, this); this.dispatchEvent(this._startEvent); }; /** * Pauses the automatic playback clock of the animator, in case manual updates are required via the * <code>time</code> property or <code>update()</code> method. * * @see #time * @see #update() */ AnimatorBase.prototype.stop = function () { if (!this._isPlaying) return; this._isPlaying = false; this._broadcaster.stop(); if (!this.hasEventListener(AnimatorEvent.STOP)) return; if (this._stopEvent == null) this._stopEvent = new AnimatorEvent(AnimatorEvent.STOP, this); this.dispatchEvent(this._stopEvent); }; /** * Provides a way to manually update the active state of the animator when automatic * updates are disabled. * * @see #stop() * @see #autoUpdate */ AnimatorBase.prototype.update = function (time) { var dt = (time - this._time) * this.playbackSpeed; this._pUpdateDeltaTime(dt); this._time = time; }; AnimatorBase.prototype.reset = function (name, offset) { if (offset === void 0) { offset = 0; } this.getAnimationState(this._pAnimationSet.getAnimation(name)).offset(offset + this._pAbsoluteTime); }; /** * Used by the graphics object to which the animator is applied, registers the owner for internal use. * * @private */ AnimatorBase.prototype.addOwner = function (entity) { this._pOwners.push(entity); }; /** * Used by the graphics object from which the animator is removed, unregisters the owner for internal use. * * @private */ AnimatorBase.prototype.removeOwner = function (entity) { this._pOwners.splice(this._pOwners.indexOf(entity), 1); }; /** * Internal abstract method called when the time delta property of the animator's contents requires updating. * * @private */ AnimatorBase.prototype._pUpdateDeltaTime = function (dt) { this._pAbsoluteTime += dt; this._pActiveState.update(this._pAbsoluteTime); if (this.updatePosition) this.applyPositionDelta(); }; /** * Enter frame event handler for automatically updating the active state of the animator. */ AnimatorBase.prototype.onEnterFrame = function (event) { if (event === void 0) { event = null; } this.update(getTimer()); }; AnimatorBase.prototype.applyPositionDelta = function () { var delta = this._pActiveState.positionDelta; var dist = delta.length; var len; if (dist > 0) { len = this._pOwners.length; for (var i = 0; i < len; ++i) this._pOwners[i].transform.translateLocal(delta, dist); } }; /** * for internal use. * * @private */ AnimatorBase.prototype.dispatchCycleEvent = function () { if (this.hasEventListener(AnimatorEvent.CYCLE_COMPLETE)) { if (this._cycleEvent == null) this._cycleEvent = new AnimatorEvent(AnimatorEvent.CYCLE_COMPLETE, this); this.dispatchEvent(this._cycleEvent); } }; /** * @inheritDoc */ AnimatorBase.prototype.clone = function () { throw new AbstractMethodError(); }; /** * @inheritDoc */ AnimatorBase.prototype.dispose = function () { }; AnimatorBase.prototype.invalidateElements = function () { var entity; var len = this._pOwners.length; for (var i = 0; i < len; i++) { entity = this._pOwners[i]; entity.invalidateElements(); } }; /** * @inheritDoc */ AnimatorBase.prototype.testGPUCompatibility = function (shader) { throw new AbstractMethodError(); }; Object.defineProperty(AnimatorBase.prototype, "assetType", { /** * @inheritDoc */ get: function () { return AnimatorBase.assetType; }, enumerable: false, configurable: true }); AnimatorBase.prototype.getRenderableElements = function (renderable, sourceElements) { //nothing to do here return sourceElements; }; AnimatorBase.assetType = '[asset Animator]'; return AnimatorBase; }(AssetBase)); export { AnimatorBase };