API Reference 0.7.1rikulo_effectMotion

Motion class

The control object of an AnimatorTask, with a built-in life cycle and control APIs.

class Motion {
 
 static const _MOTION_STATE_INIT = 0;
 static const _MOTION_STATE_RUNNING = 1;
 static const _MOTION_STATE_PAUSED = 2;
 
 final MotionStart _start;
 final MotionMove _move;
 final MotionEnd _end;
 AnimatorTask _task;
 
 MotionState _state;
 int _stateFlag = _MOTION_STATE_INIT;
 
 Motion({MotionStart start, MotionMove move, MotionEnd end}) : 
   _start = start, _move = move, _end = end {
   
   _task = (int time) {
     if (_stateFlag == _MOTION_STATE_INIT) {
       _state = new MotionState._(time);
       onStart(_state);
       _stateFlag = _MOTION_STATE_RUNNING;
     }
     _state._snapshot(time);
     switch (_stateFlag) {
       case _MOTION_STATE_RUNNING:
         if (_state.isPaused) { // resume from pause
           _state._resume(time);
           onResume(_state);
         }
         bool cont = onMove(_state);
         if (cont == null)
           cont = true;
         if (!cont) {
           onEnd(_state);
           _state = null;
           _stateFlag = _MOTION_STATE_INIT;
         }
         return cont;
       case _MOTION_STATE_PAUSED:
         if (!_state.isPaused) { // pause from running
           _state._pause(time);
           onPause(_state);
         }
         return true; // do nothing but keep in the loop
     }
   };
 }
 
 /**
  * The Animator associated with the motion.
  */
 Animator get animator => _animator;
 
 /** Retrieve the current [MotionState]. [null] if not running.
  */
 MotionState get state => _state;
 
 /**
  * Called in the first animator iteration after the motion is added into animator.
  */
 void onStart(MotionState state) {
   if (_start != null)
     _start(state);
 }
 
 /**
  * Called in each animator iteration, when the motion is at running state.
  */
 bool onMove(MotionState state) => 
     _move == null || _move(state);
 
 /**
  * Called after the runner returns false. Calling stop() will not invoke this function.
  */
 void onEnd(MotionState state) {
   if (_end != null)
     _end(state);
 }
 
 /**
  * Called in the first animator iteration after the motion is paused.
  */
 void onPause(MotionState state) {}
 
 /**
  * Called in the first animator iteration after the motion is resumed from pause.
  */
 void onResume(MotionState state) {}
 
 /**
  * Start the motion, or resume it from a pause.
  */
 void run() {
   switch (_stateFlag) {
     case _MOTION_STATE_RUNNING:
       return;
     case _MOTION_STATE_PAUSED:
       _stateFlag = _MOTION_STATE_RUNNING; // resume
       break;
     case _MOTION_STATE_INIT:
       animator.add(this._task);
       break;
   }
 }
 
 /**
  * Pause the motion. 
  */
 void pause() {
   if (_stateFlag != _MOTION_STATE_RUNNING)
     return;
   _stateFlag = _MOTION_STATE_PAUSED;
 }
 
 /**
  * Stop the motion and reset the internal states.
  */
 void stop() {
   animator.remove(this._task);
   _state = null;
   _stateFlag = _MOTION_STATE_INIT;
 }
 
 /**
  * Return true if the motion is at running state.
  */
 bool get isRunning => _stateFlag == _MOTION_STATE_RUNNING;
 
 /**
  * Return true if the motion is at paused state.
  */
 bool get isPaused => _stateFlag == _MOTION_STATE_PAUSED;
}

Subclasses

EasingMotion

Constructors

new Motion({MotionStart start, MotionMove move, MotionEnd end}) #

Creates a new Object instance.

Object instances have no meaningful state, and are only useful through their identity. An Object instance is equal to itself only.

docs inherited from Object
Motion({MotionStart start, MotionMove move, MotionEnd end}) : 
 _start = start, _move = move, _end = end {
 
 _task = (int time) {
   if (_stateFlag == _MOTION_STATE_INIT) {
     _state = new MotionState._(time);
     onStart(_state);
     _stateFlag = _MOTION_STATE_RUNNING;
   }
   _state._snapshot(time);
   switch (_stateFlag) {
     case _MOTION_STATE_RUNNING:
       if (_state.isPaused) { // resume from pause
         _state._resume(time);
         onResume(_state);
       }
       bool cont = onMove(_state);
       if (cont == null)
         cont = true;
       if (!cont) {
         onEnd(_state);
         _state = null;
         _stateFlag = _MOTION_STATE_INIT;
       }
       return cont;
     case _MOTION_STATE_PAUSED:
       if (!_state.isPaused) { // pause from running
         _state._pause(time);
         onPause(_state);
       }
       return true; // do nothing but keep in the loop
   }
 };
}

Properties

final Animator animator #

The Animator associated with the motion.

Animator get animator => _animator;

final bool isPaused #

Return true if the motion is at paused state.

bool get isPaused => _stateFlag == _MOTION_STATE_PAUSED;

final bool isRunning #

Return true if the motion is at running state.

bool get isRunning => _stateFlag == _MOTION_STATE_RUNNING;

final MotionState state #

Retrieve the current MotionState. null if not running.

MotionState get state => _state;

Methods

void onEnd(MotionState state) #

Called after the runner returns false. Calling stop() will not invoke this function.

void onEnd(MotionState state) {
 if (_end != null)
   _end(state);
}

bool onMove(MotionState state) #

Called in each animator iteration, when the motion is at running state.

bool onMove(MotionState state) => 
   _move == null || _move(state);

void onPause(MotionState state) #

Called in the first animator iteration after the motion is paused.

void onPause(MotionState state) {}

void onResume(MotionState state) #

Called in the first animator iteration after the motion is resumed from pause.

void onResume(MotionState state) {}

void onStart(MotionState state) #

Called in the first animator iteration after the motion is added into animator.

void onStart(MotionState state) {
 if (_start != null)
   _start(state);
}

void pause() #

Pause the motion.

void pause() {
 if (_stateFlag != _MOTION_STATE_RUNNING)
   return;
 _stateFlag = _MOTION_STATE_PAUSED;
}

void run() #

Start the motion, or resume it from a pause.

void run() {
 switch (_stateFlag) {
   case _MOTION_STATE_RUNNING:
     return;
   case _MOTION_STATE_PAUSED:
     _stateFlag = _MOTION_STATE_RUNNING; // resume
     break;
   case _MOTION_STATE_INIT:
     animator.add(this._task);
     break;
 }
}

void stop() #

Stop the motion and reset the internal states.

void stop() {
 animator.remove(this._task);
 _state = null;
 _stateFlag = _MOTION_STATE_INIT;
}