Skip to content

Commit

Permalink
rotation & size optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
blooddy committed Oct 8, 2011
1 parent 705e699 commit a6678f5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 32 deletions.
84 changes: 56 additions & 28 deletions src/com/casualflash/rush/display/DisplayObject2D.as
Expand Up @@ -80,6 +80,16 @@ package com.casualflash.rush.display {
_BROADCAST_EVENTS[ Event.EXIT_FRAME ] = true;
_BROADCAST_EVENTS[ Event.FRAME_CONSTRUCTED ] = true;

/**
* @private
*/
private static const _RAD2DEG:Number = 180 / Math.PI;

/**
* @private
*/
private static const _DEG2RAD:Number = Math.PI / 180;

//--------------------------------------------------------------------------
//
// Constructor
Expand Down Expand Up @@ -149,11 +159,6 @@ package com.casualflash.rush.display {
*/
$internal const $matrix:Matrix = new Matrix();

/**
* @private
*/
$internal const $size:Point = new Point();

//--------------------------------------------------------------------------
//
// Properties
Expand Down Expand Up @@ -361,61 +366,84 @@ package com.casualflash.rush.display {
//----------------------------------
// rotation
//----------------------------------


/**
* @private
*/
$internal var $radianRotation:Number = 0;

public function get radianRotation():Number {
return this.$radianRotation;
}

/**
* @private
*/
$internal var $rotation:Number = 0;
public function set radianRotation(value:Number):void {
value %= 360;
if ( this.$radianRotation == value ) return;
this.$radianRotation = value;
this.$changed |= 14;
}

//----------------------------------
// rotation
//----------------------------------

public function get rotation():Number {
return this.$rotation;
return this.$radianRotation * _RAD2DEG;
}

/**
* @private
*/
public function set rotation(value:Number):void {
if ( this.$rotation == value ) return;
this.$rotation = value % 360;
value = ( value * _DEG2RAD ) % 360;
if ( this.$radianRotation == value ) return;
this.$radianRotation = value;
this.$changed |= 14;
}

//----------------------------------
// width
//----------------------------------


$internal var $width:Number = 0;

public function get width():Number {
if ( this.$changed & 7 ) this.$updateSize();
return this.$size.x;
return this.$width;
}

/**
* @private
*/
public function set width(value:Number):void { // TODO: optimize
if ( this.$changed & 7 ) this.$updateSize();
if ( this.$size.x == value ) return;
this.$matrix.scale( value / this.$size.x, 1 ); // тут видимо накопится погрешность
this.$size.x = value;
if ( this.$width == value ) return;
this.$matrix.scale( value / this.$width, 1 ); // тут видимо накопится погрешность
this.$width = value;
}

//----------------------------------
// height
//----------------------------------


$internal var $height:Number = 0;

public function get height():Number {
if ( this.$changed & 7 ) this.$updateSize();
return this.$size.y;
return this.$height;
}

/**
* @private
*/
public function set height(value:Number):void { // TODO: optimize
if ( this.$changed & 7 ) this.$updateSize();
if ( this.$size.y == value ) return;
this.$matrix.scale( 1, value / this.$size.y ); // тут видимо накопится погрешность
this.$size.y = value;
if ( this.$height == value ) return;
this.$matrix.scale( 1, value / this.$height ); // тут видимо накопится погрешность
this.$height = value;
}

//----------------------------------
Expand Down Expand Up @@ -748,7 +776,7 @@ package com.casualflash.rush.display {
// );
this.$matrix.identity();
this.$matrix.scale( this.$scaleX, this.$scaleY );
this.$matrix.rotate( this.$rotation / 180 * Math.PI );
this.$matrix.rotate( this.$radianRotation );
this.$matrix.translate( this.$x, this.$y );
}

Expand All @@ -764,7 +792,7 @@ package com.casualflash.rush.display {
var d:Number = this.$matrix.d;
this.$scaleX = Math.sqrt( a*a + b*b );
this.$scaleY = Math.sqrt( c*c + d*d );
this.$rotation = Math.atan2( b, a ) / Math.PI * 180;
this.$radianRotation = Math.atan2( b, a );
}

/**
Expand Down Expand Up @@ -811,12 +839,12 @@ package com.casualflash.rush.display {
var bottomRight:Point = this.$matrix.transformPoint( this.$orign.bottomRight );
var bottomLeft:Point = this.$matrix.transformPoint( new Point( this.$orign.left, this.$orign.bottom ) );
var bounds:Rectangle = new Rectangle();
bounds.top = Math.min( topLeft.y, topRight.y, bottomRight.y, bottomLeft.y );
bounds.right = Math.max( topLeft.x, topRight.x, bottomRight.x, bottomLeft.x );
bounds.bottom = Math.max( topLeft.y, topRight.y, bottomRight.y, bottomLeft.y );
bounds.left = Math.min( topLeft.x, topRight.x, bottomRight.x, bottomLeft.x );
this.$size.x = bounds.width;
this.$size.y = bounds.height;
bounds.top = Math.min( topLeft.y, topRight.y, bottomRight.y, bottomLeft.y );
bounds.right = Math.max( topLeft.x, topRight.x, bottomRight.x, bottomLeft.x );
bounds.bottom = Math.max( topLeft.y, topRight.y, bottomRight.y, bottomLeft.y );
bounds.left = Math.min( topLeft.x, topRight.x, bottomRight.x, bottomLeft.x );
this.$width = bounds.width;
this.$height = bounds.height;
}

/**
Expand Down
Expand Up @@ -246,12 +246,12 @@ package com.casualflash.rush.display {
*/
$internal override function $hitTestPoint(point:Point, shapeFlag:Boolean=false):Boolean {
var p:Point;
var mi:Matrix;
var im:Matrix;
for each ( var child:DisplayObject2D in this.$list ) {
if ( child.$changed & 2 ) child.$updateMatrix();
mi = child.$matrix.clone();
mi.invert();
p = mi.transformPoint( point );
im = child.$matrix.clone();
im.invert();
p = im.transformPoint( point );
if ( child.$orign.containsPoint( p ) ) {
if ( child.$hitTestPoint( p, shapeFlag ) ) {
return true;
Expand Down

0 comments on commit a6678f5

Please sign in to comment.