// Adobe classes
import mx.transitions.easing.*;
//import flash.geom.*;
// Still support Flash 7:
import org.asapframework.util.types.*;
// ASAP classes
import org.asapframework.util.NumberUtils;
import org.asapframework.util.actionqueue.*;
import org.asapframework.util.MovieClipUtils;
import org.asapframework.util.RectangleUtils;
class ui.Image extends MovieClip {
private static var MOVE_IN_DURATION:Number = .25;
private static var MOVE_OUT_DURATION:Number = .9;
private static var SCALE_IN_FREQUENCY:Number = 1.3;
private static var EFFECT_IN:Function = Strong.easeInOut;
private static var EFFECT_OUT:Function = Strong.easeOut;
private static var SIDE_POSITION:Number = 150;
private var mId:String;
private var mFrame:Rectangle;
private var mImageFrame:Rectangle;
private var mMoveQueue:ActionQueue;
private var mScaleQueue:ActionQueue;
private var mRotateQueue:ActionQueue;
private var mScrambleLoc:Point;
private var mSelected:Boolean = false;
private var mRotation:Number;
private var mInited:Boolean = false;
private var mImageClip:MovieClip;
private var clip_mc:MovieClip;
private var border_mc:MovieClip;
//private var shadow_left_mc:MovieClip;
//private var shadow_right_mc:MovieClip;
/**
*/
public function Image () {
_visible = false;
// improve performance
cacheAsBitmap = true;
}
/**
Returns whether the ui.Image has been inited (in initWithScale).
*/
public function isInited () : Boolean {
return mInited;
}
/**
Sets the reference to the image clip after smoothing so ui.Image knows which clip to handle.
*/
public function setImageClip (inClip:MovieClip) : Void {
mImageClip = inClip;
}
/**
*/
public function initWithScale (inScale:Number) : Void {
mImageClip._xscale = inScale;
mImageClip._yscale = inScale;
mImageClip._x = -mImageClip._width / 2;
mImageClip._y = -mImageClip._height / 2;
border_mc._width = mImageClip._width;
border_mc._height = mImageClip._height;
border_mc._x = mImageClip._x;
border_mc._y = mImageClip._y;
border_mc.swapDepths(getNextHighestDepth());
/*
// shadows not used as they are too CPU intensive
shadow_left_mc._width = shadow_right_mc._width = mui.ImageClip._width;
shadow_left_mc._height = shadow_right_mc._height = mui.ImageClip._height;
shadow_left_mc._x = shadow_right_mc._x = mui.ImageClip._x;
shadow_left_mc._y = shadow_right_mc._y = mui.ImageClip._y;
*/
mImageFrame = RectangleUtils.rectOfMovieClip(this);
_visible = true;
mRotation = NumberUtils.randomInRanges([-20, -10], [10, 20]);
_rotation = mRotation;
scramble();
mInited = true;
}
/**
*/
public function getLoadImageContainer () : MovieClip {
return clip_mc;
}
/**
*/
public function setId (inId:String) : Void {
mId = inId;
}
/**
*/
public function getId () : String {
return mId;
}
/**
*/
public function setSelected (inState:Boolean) : Void {
mSelected = inState;
}
/**
*/
public function getSelected () : Boolean {
return mSelected;
}
/**
*/
public function getFrame () : Rectangle {
return mFrame;
}
/**
*/
public function setFrame (inFrame:Rectangle) : Void {
mFrame = inFrame;
mFrame.x += mImageClip._width / 2;
mFrame.y += mImageClip._height / 2;
}
/**
*/
public function presentFirstTime () : Void {
moveIn();
select();
}
/**
*/
public function show () : Void {
moveIn();
scaleIn();
rotateIn();
}
/**
*/
public function select () : Void {
scaleInSelect();
rotateInSelect();
}
/**
*/
public function hide () : Void {
moveOut();
rotateOut();
}
/**
*/
public function toString () : String {
return "ui.Image: " + mId;
}
// PRIVATE METHODS
/**
Places the image at a random position at the side of the stage.
*/
private function scramble () : Void {
var margin:Number = SIDE_POSITION;
var frame:Rectangle = new Rectangle(mFrame.right + margin, 0, Stage.width, Stage.height);
var xleft:Number = -_width + margin;
var xright:Number = mFrame.right + margin;
var y:Number = NumberUtils.randomInRange(frame.top, frame.bottom);
var x:Number = xright;
mScrambleLoc = new Point(x, y);
_x = mScrambleLoc.x;
_y = mScrambleLoc.y;
}
/**
*/
private function moveIn () : Void {
swapDepths(_parent.getNextHighestDepth());
mMoveQueue.quit();
mMoveQueue = new ActionQueue();
mMoveQueue.addAction(AQMove.move, this, MOVE_IN_DURATION, null, null, mFrame.left, mFrame.top, EFFECT_IN);
mMoveQueue.run();
}
/**
*/
private function scaleIn () : Void {
mScaleQueue.quit();
mScaleQueue = new ActionQueue();
var count:Number = 1;
var frequency:Number = SCALE_IN_FREQUENCY;
var maxScale:Number = 105;
var minScale:Number = 100;
var startScale:Number = _xscale;
var duration:Number = null;
mScaleQueue.addAction( AQPulse.change, this, ["_xscale", "_yscale"], count, frequency, maxScale, minScale, startScale, duration, Strong.easeOut );
mScaleQueue.run();
}
/**
*/
private function rotateIn () : Void {
var duration:Number = 1 / SCALE_IN_FREQUENCY;
var rotation = NumberUtils.randomInRanges([-7, -2], [2, 7]);
mRotateQueue.quit();
mRotateQueue = new ActionQueue();
mRotateQueue.addAction(AQRotate.rotate, this, duration, null, rotation, AQRotate.NEAR, Strong.easeOut);
mRotateQueue.run();
}
/**
*/
private function scaleInSelect () : Void {
swapDepths(_parent.getNextHighestDepth());
mScaleQueue.quit();
mScaleQueue = new ActionQueue();
var count:Number = 1;
var frequency:Number = SCALE_IN_FREQUENCY;
var maxScale:Number = 100;
var minScale:Number = 90;
var startScale:Number = 100;
mScaleQueue.addAction( AQPulse.change, this, ["_xscale", "_yscale"], count, frequency, maxScale, minScale, startScale, null, Strong.easeOut );
mScaleQueue.run();
}
/**
*/
private function rotateInSelect () : Void {
var duration:Number = 1 / SCALE_IN_FREQUENCY;
var rotation = 0;
mRotateQueue.quit();
mRotateQueue = new ActionQueue();
mRotateQueue.addAction(AQRotate.rotate, this, duration, null, rotation, AQRotate.NEAR, Strong.easeOut);
mRotateQueue.run();
}
/**
*/
private function moveOut () : Void {
mMoveQueue.quit();
mMoveQueue = new ActionQueue();
mMoveQueue.addAction(AQMove.move, this, MOVE_OUT_DURATION, null, null, mScrambleLoc.x, mScrambleLoc.y, EFFECT_OUT);
mMoveQueue.run();
}
/**
*/
private function rotateOut () : Void {
mRotateQueue.quit();
mRotateQueue = new ActionQueue();
mRotateQueue.addAction(AQRotate.rotate, this, MOVE_OUT_DURATION, null, mRotation, AQRotate.NEAR);
mRotateQueue.run();
}
}