Permalink
Browse files

adding flatten to scrollRect

  • Loading branch information...
Vagif Kuliyev
Vagif Kuliyev committed Mar 22, 2012
1 parent 234b688 commit f853f2e3fe461d5685ae15cc79aefa91fb43de40
@@ -18,6 +18,7 @@ package starling.core
import flash.display3D.IndexBuffer3D;
import flash.display3D.VertexBuffer3D;
import flash.geom.Matrix3D;
+ import flash.geom.Vector3D;
import flash.utils.getQualifiedClassName;
import starling.display.DisplayObject;
@@ -26,9 +27,11 @@ package starling.core
import starling.display.Quad;
import starling.errors.MissingContextError;
import starling.textures.Texture;
+ import starling.textures.RenderTexture;
import starling.textures.TextureSmoothing;
import starling.utils.VertexData;
+
/** Optimizes rendering of a number of quads with an identical state.
*
* <p>The majority of all rendered objects in Starling are quads. In fact, all the default
@@ -236,7 +239,8 @@ package starling.core
{
return quadBatchID; // ignore transparent objects, except root
}
-
+ var image:Image;
+ var quadBatch:QuadBatch;
if (object is DisplayObjectContainer)
{
var container:DisplayObjectContainer = object as DisplayObjectContainer;
@@ -248,17 +252,41 @@ package starling.core
var child:DisplayObject = container.getChildAt(i);
childMatrix.copyFrom(transformationMatrix);
RenderSupport.transformMatrixForObject(childMatrix, child);
- quadBatchID = compileObject(child, quadBatches, quadBatchID,
- childMatrix, alpha * child.alpha);
+ if(child.scrollRect!=null) {
+ var xTransform:Vector3D=childMatrix.deltaTransformVector(new Vector3D(0,1,0));
+ var yTransform:Vector3D=childMatrix.deltaTransformVector(new Vector3D(1,0,0));
+ var rootScaleX:Number=xTransform.length;
+ var rootScaleY:Number=yTransform.length;
+ var renderTexture:RenderTexture=new RenderTexture(child.width*rootScaleX,child.height*rootScaleY);
+ renderTexture.support.rootDisplayObject=child;
+ function drawingBlock():void {
+ this.support.scaleMatrix(rootScaleX,rootScaleY);
+ child.render(this.support,1.0);
+ }
+ renderTexture.drawBundled(drawingBlock);
+ image=new Image(renderTexture);
+ quadBatch = quadBatches[quadBatchID];
+ quadBatch.syncBuffers();
+ quadBatchID++;
+ if (quadBatches.length <= quadBatchID)
+ quadBatches.push(new QuadBatch());
+ quadBatch = quadBatches[quadBatchID];
+ quadBatch.reset();
+ childMatrix.appendScale(1/rootScaleX,1/rootScaleY,1);
+ quadBatch.addQuad(image, child.alpha, renderTexture, image.smoothing, childMatrix);
+ } else {
+ quadBatchID = compileObject(child, quadBatches, quadBatchID,
+ childMatrix, alpha * child.alpha);
+ }
}
}
else if (object is Quad)
{
var quad:Quad = object as Quad;
- var image:Image = quad as Image;
+ image = quad as Image;
var texture:Texture = image ? image.texture : null;
var smoothing:String = image ? image.smoothing : null;
- var quadBatch:QuadBatch = quadBatches[quadBatchID];
+ quadBatch = quadBatches[quadBatchID];
if (quadBatch.isStateChange(quad, texture, smoothing))
{
@@ -281,7 +309,6 @@ package starling.core
{
quadBatches[quadBatchID].syncBuffers();
- trace("disposed");
for (i=quadBatches.length-1; i>quadBatchID; --i)
{
quadBatches[i].dispose();
@@ -24,6 +24,7 @@ package starling.display
import starling.display.Image;
import starling.utils.getSmallestRect;
import starling.utils.getRectBounds;
+ import starling.core.QuadBatch;
/**
* A DisplayObjectContainer represents a collection of display objects.
@@ -67,6 +68,7 @@ package starling.display
// members
private var mChildren:Vector.<DisplayObject>;
+ private var mFlattenedContents:Vector.<QuadBatch>;
/** Helper objects. */
private static var sHelperMatrix:Matrix = new Matrix();
@@ -86,6 +88,7 @@ package starling.display
/** Disposes the resources of all children. */
public override function dispose():void
{
+ unflatten();
var numChildren:int = mChildren.length;
for (var i:int=0; i<numChildren; ++i)
@@ -278,15 +281,13 @@ package starling.display
{
var scrollRectBounds:Rectangle;
if(targetSpace==this) {
- scrollRectBounds=scrollRect;
+ scrollRectBounds=scrollRect.clone();
+ scrollRectBounds.x=0;
+ scrollRectBounds.y=0;
} else {
getTransformationMatrix(targetSpace, sHelperMatrix);
scrollRectBounds=getRectBounds(scrollRect,sHelperMatrix);
}
- if(debug) {
- trace("scrollRectBounds"+scrollRectBounds.toString());
- trace("resultRect"+resultRect.toString());
- }
getSmallestRect(resultRect,scrollRectBounds,resultRect);
}
return resultRect;
@@ -319,6 +320,50 @@ package starling.display
return null;
}
+ /** Indicates if the sprite was flattened. */
+ public function get isFlattened():Boolean { return mFlattenedContents != null; }
+
+ /** Optimizes the sprite for optimal rendering performance. Changes in the
+ * children of a flattened sprite will not be displayed any longer. For this to happen,
+ * either call <code>flatten</code> again, or <code>unflatten</code> the sprite. */
+ public function flatten():void
+ {
+ dispatchEventOnChildren(new Event(Event.FLATTEN));
+
+ if (mFlattenedContents == null)
+ {
+ mFlattenedContents = new <QuadBatch>[];
+ Starling.current.addEventListener(Event.CONTEXT3D_CREATE, onContextCreated);
+ }
+
+ QuadBatch.compile(this, mFlattenedContents);
+ }
+
+ /** Removes the rendering optimizations that were created when flattening the sprite.
+ * Changes to the sprite's children will become immediately visible again. */
+ public function unflatten():void
+ {
+ if (mFlattenedContents)
+ {
+ Starling.current.removeEventListener(Event.CONTEXT3D_CREATE, onContextCreated);
+ var numBatches:int = mFlattenedContents.length;
+
+ for (var i:int=0; i<numBatches; ++i)
+ mFlattenedContents[i].dispose();
+
+ mFlattenedContents = null;
+ }
+ }
+
+ private function onContextCreated(event:Event):void
+ {
+ if (mFlattenedContents)
+ {
+ mFlattenedContents = new <QuadBatch>[];
+ flatten();
+ }
+ }
+
/** @inheritDoc */
public override function render(support:RenderSupport, alpha:Number):void
{
@@ -359,19 +404,33 @@ package starling.display
return;
}
}
- alpha *= this.alpha;
- var numChildren:int = mChildren.length;
-
- for (var i:int=0; i<numChildren; ++i)
+ var i:int;
+ if (mFlattenedContents)
{
- var child:DisplayObject = mChildren[i];
- if (child.alpha != 0.0 && child.visible && child.scaleX != 0.0 && child.scaleY != 0.0)
- {
- support.pushMatrix();
- support.transformMatrix(child);
- child.render(support, alpha);
- support.popMatrix();
+ if(mScrollRect==null) {
+ support.finishQuadBatch();
}
+
+ alpha *= this.alpha;
+ var numBatches:int = mFlattenedContents.length;
+
+ for (i=0; i<numBatches; ++i)
+ mFlattenedContents[i].render(support.mvpMatrix, alpha);
+ } else {
+ alpha *= this.alpha;
+ var numChildren:int = mChildren.length;
+
+ for (i=0; i<numChildren; ++i)
+ {
+ var child:DisplayObject = mChildren[i];
+ if (child.alpha != 0.0 && child.visible && child.scaleX != 0.0 && child.scaleY != 0.0)
+ {
+ support.pushMatrix();
+ support.transformMatrix(child);
+ child.render(support, alpha);
+ support.popMatrix();
+ }
+ }
}
if(mScrollRect && !mRenderingScrollRectTexture) {
support.finishQuadBatch();
@@ -42,7 +42,6 @@ package starling.display
*/
public class Sprite extends DisplayObjectContainer
{
- private var mFlattenedContents:Vector.<QuadBatch>;
private var mUseHandCursor:Boolean;
/** Creates an empty sprite. */
@@ -51,13 +50,6 @@ package starling.display
super();
}
- /** @inheritDoc */
- public override function dispose():void
- {
- unflatten();
- super.dispose();
- }
-
/** Indicates if the mouse cursor should transform into a hand while it's over the sprite.
* @default false */
public function get useHandCursor():Boolean { return mUseHandCursor; }
@@ -76,65 +68,5 @@ package starling.display
{
Mouse.cursor = event.interactsWith(this) ? MouseCursor.BUTTON : MouseCursor.AUTO;
}
-
- /** Optimizes the sprite for optimal rendering performance. Changes in the
- * children of a flattened sprite will not be displayed any longer. For this to happen,
- * either call <code>flatten</code> again, or <code>unflatten</code> the sprite. */
- public function flatten():void
- {
- dispatchEventOnChildren(new Event(Event.FLATTEN));
-
- if (mFlattenedContents == null)
- {
- mFlattenedContents = new <QuadBatch>[];
- Starling.current.addEventListener(Event.CONTEXT3D_CREATE, onContextCreated);
- }
-
- QuadBatch.compile(this, mFlattenedContents);
- }
-
- /** Removes the rendering optimizations that were created when flattening the sprite.
- * Changes to the sprite's children will become immediately visible again. */
- public function unflatten():void
- {
- if (mFlattenedContents)
- {
- Starling.current.removeEventListener(Event.CONTEXT3D_CREATE, onContextCreated);
- var numBatches:int = mFlattenedContents.length;
-
- for (var i:int=0; i<numBatches; ++i)
- mFlattenedContents[i].dispose();
-
- mFlattenedContents = null;
- }
- }
-
- private function onContextCreated(event:Event):void
- {
- if (mFlattenedContents)
- {
- mFlattenedContents = new <QuadBatch>[];
- flatten();
- }
- }
-
- /** Indicates if the sprite was flattened. */
- public function get isFlattened():Boolean { return mFlattenedContents != null; }
-
- /** @inheritDoc */
- public override function render(support:RenderSupport, alpha:Number):void
- {
- if (mFlattenedContents)
- {
- support.finishQuadBatch();
-
- alpha *= this.alpha;
- var numBatches:int = mFlattenedContents.length;
-
- for (var i:int=0; i<numBatches; ++i)
- mFlattenedContents[i].render(support.mvpMatrix, alpha);
- }
- else super.render(support, alpha);
- }
}
}

0 comments on commit f853f2e

Please sign in to comment.