Skip to content

Commit

Permalink
Added wrapper class for Starling TouchEvents to play nice with PB eve…
Browse files Browse the repository at this point in the history
…nt dispatcher

Added TextFieldRenderer.
Fixed color property in SpriteSheetRenderer (and subclasses).
  • Loading branch information
zodouglass committed Apr 30, 2012
1 parent ff4ca4d commit b320f5f
Show file tree
Hide file tree
Showing 5 changed files with 175 additions and 43 deletions.
12 changes: 11 additions & 1 deletion src/com/pblabs/engine/core/TemplateManager.as
Expand Up @@ -169,6 +169,14 @@ package com.pblabs.engine.core
var alias:String=xml.attribute("alias");
if (alias == "")
alias = null;

//And the includeIn type...
var includeIn:String = xml.attribute("includeIn");
if ( includeIn != "" && includeIn != Serializer.instance.includeInType )
{
Profiler.exit("instantiateEntityFromXML");
return null; //only create instances for entities that includeIn match the includeInType, if defined
}

// Make the IEntity instance.
var entity:IEntity;
Expand Down Expand Up @@ -526,7 +534,9 @@ package com.pblabs.engine.core
else if (objectXML.name() == "objectReference")
{
_inGroup = true;
group.push(instantiateEntity(childName));
var ent:IEntity = instantiateEntity(childName);
if( ent != null )
group.push(ent);
_inGroup=false;
}
else
Expand Down
23 changes: 23 additions & 0 deletions src/com/starling/events/StarlingTouchEvent.as
@@ -0,0 +1,23 @@
package com.starling.events
{
import flash.events.Event;

/**
* Intermediate class to convert Starling Events to flash events, in order to play nice with PushButton.
*
* TODO - get starling to use flash.events.
*/
public class StarlingTouchEvent extends Event
{

public static const TOUCH_DOWN:String = "touch_down";

public function StarlingTouchEvent(type:String, bubbles:Boolean = false, cancelable:Boolean = false)
{
super(type, bubbles, cancelable);

}

}

}
123 changes: 82 additions & 41 deletions src/com/starling/rendering2D/DisplayObjectRenderer.as
@@ -1,22 +1,26 @@
package com.starling.rendering2D
{
import com.pblabs.engine.components.AnimatedComponent;
import com.pblabs.engine.debug.Logger;
import com.pblabs.engine.entity.EntityComponent;
import com.pblabs.engine.entity.PropertyReference;
import com.pblabs.engine.PBUtil;
import com.starling.events.StarlingTouchEvent;
import flash.geom.Matrix;
import flash.geom.Point;
import starling.animation.IAnimatable;
import starling.core.Starling;
import starling.display.DisplayObject;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.events.TouchPhase;

public class DisplayObjectRenderer extends EntityComponent implements IAnimatable
{

/**
* The StarlingScene in which to add the DisplayObjectRenderer component to.
*/
public var scene:StarlingScene;
public var displayObject:DisplayObject;
private var _layerIndex:int = 0;
private var _zIndex:int = 0;

/**
* If set, the layer index is gotten from this property every frame.
Expand All @@ -28,27 +32,13 @@ package com.starling.rendering2D
*/
public var zIndexProperty:PropertyReference;

// 0.1 to 1 = forground, fast scrolling
// 1 = fixed position
// 1.1 to 1.5 = background, slow scrolling
/*
* scrollFactor property for paralax effect
* 0.1 to 1 = forground, fast scrolling
* 1 = fixed position
* 1.1 to 1.5 = background, slow scrolling
*/
public var scrollFactor:Point = new Point(1, 1);
protected var basePosition:Point;
protected var _layerIndexDirty:Boolean = true;
protected var _zIndexDirty:Boolean = true;
protected var _transformDirty:Boolean = true;

protected var _transformMatrix:Matrix = new Matrix();

private var _position:Point = new Point();
private var _scale:Point = new Point(1,1);
private var _rotation:Number = 0;
private var _rotationOffset:Number = 0;
private var _alpha:Number = 1;

protected var _positionOffset:Point = new Point();
protected var _registrationPoint:Point = new Point();



/**
* If set, position is gotten from this property every frame.
Expand All @@ -71,7 +61,44 @@ package com.starling.rendering2D
*/
public var snapToNearestPixels:Boolean = true;

//IAnimatable
//Protected variables
protected var basePosition:Point; //used for scroll factor
protected var _displayObject:DisplayObject;
protected var _layerIndex:int = 0;
protected var _layerIndexDirty:Boolean = true;
protected var _zIndex:int = 0;
protected var _zIndexDirty:Boolean = true;
protected var _transformDirty:Boolean = true;
protected var _transformMatrix:Matrix = new Matrix();
protected var _position:Point = new Point();
protected var _scale:Point = new Point(1,1);
protected var _rotation:Number = 0;
protected var _rotationOffset:Number = 0;
protected var _alpha:Number = 1;
protected var _positionOffset:Point = new Point();
protected var _registrationPoint:Point = new Point();
protected var _touchable:Boolean = false;

public function get displayObject():DisplayObject
{
return _displayObject;
}

public function set displayObject(value:DisplayObject):void
{
_displayObject = value;

if ( _displayObject != null )
{
if ( touchable )
displayObject.addEventListener(TouchEvent.TOUCH, onTouch );
}
}

/**
* Starlings implementation of the IAnimatable class. Called very frame.
* @param deltaTime
*/
public function advanceTime(deltaTime:Number):void
{
if (!displayObject)
Expand All @@ -93,7 +120,6 @@ package com.starling.rendering2D

if ( displayObject != null && scene != null && scene.sceneView != null )
scene.add( this );
//scene.sceneView.addChild( displayObject );
}

override protected function onRemove():void
Expand Down Expand Up @@ -154,6 +180,20 @@ package com.starling.rendering2D
_transformDirty = true;
}

/**
* Indicates if this object (and its children) will receive starling touch events.
* @default false
*/
public function get touchable():Boolean
{
return _touchable;
}

public function set touchable(value:Boolean):void
{
_touchable = value;
_transformDirty = true;
}

public function get layerIndex():int
{
Expand Down Expand Up @@ -364,26 +404,13 @@ package com.starling.rendering2D

if(updateProps)
updateProperties();

/*
_transformMatrix.identity();
_transformMatrix.scale(_scale.x, _scale.y);
_transformMatrix.translate(-_registrationPoint.x * _scale.x, -_registrationPoint.y * _scale.y);
_transformMatrix.rotate(PBUtil.getRadiansFromDegrees(_rotation) + _rotationOffset);
_transformMatrix.translate(_position.x + _positionOffset.x, _position.y + _positionOffset.y);
displayObject.transformationMatrix = _transformMatrix;
displayObject.alpha = _alpha;
//displayObject.blendMode = _blendMode;
displayObject.visible = (_alpha > 0);
*/


displayObject.pivotX = _positionOffset.x;
displayObject.pivotY = _positionOffset.y;


if( displayObject.touchable != _touchable )
displayObject.touchable = _touchable;

//update position with scrollFactor
if ( scrollFactor.x != 1 || scrollFactor.y != 1 )
Expand Down Expand Up @@ -417,6 +444,20 @@ package com.starling.rendering2D
_transformDirty = false;
}


protected function onTouch(e:TouchEvent):void
{
//TODO - dispatch events for hover, and up

var touch:Touch = e.getTouch(e.target as DisplayObject, TouchPhase.BEGAN );

if ( touch != null ) //touch down
{
var se:StarlingTouchEvent = new StarlingTouchEvent(StarlingTouchEvent.TOUCH_DOWN );

owner.eventDispatcher.dispatchEvent(se);
}
}
}

}
2 changes: 1 addition & 1 deletion src/com/starling/rendering2D/SpriteSheetRenderer.as
Expand Up @@ -45,7 +45,7 @@ package com.starling.rendering2D
if( textures != null && textures.length > 0)
{
starlingMovieClip = new MovieClip(textures, fps);
this.displayObject = starlingMovieClip;
this.image = starlingMovieClip;

if( scene != null )
scene.add( this );
Expand Down
58 changes: 58 additions & 0 deletions src/com/starling/rendering2D/TextFieldRenderer.as
@@ -0,0 +1,58 @@
package com.starling.rendering2D
{
import com.pblabs.engine.entity.PropertyReference;
import com.starling.rendering2D.DisplayObjectRenderer;
import starling.display.DisplayObject;
import starling.text.TextField;

/**
* Static text field, rendered to the GPU. Once the text is set, either through the text property or the textReference, it cannot be changed.
*/
public class TextFieldRenderer extends DisplayObjectRenderer
{
public var textReference:PropertyReference;
public var text:String;
public var fontName:String = "Verdana";
public var fontSize:uint = 12;
public var bold:Boolean = false;
public var color:uint = 0x000000;
public var textWidth:int = 200;
public var textHeight:int = 30;
public var autoScale:Boolean = false;
public var border:Boolean = false;

public var textField:TextField;

override protected function onAdd():void
{
if ( text != null )
addTextField(text);

super.onAdd();
}

override public function advanceTime(deltaTime:Number):void
{
if ( textReference != null && owner.getProperty(textReference) as String != text)
{
addTextField(owner.getProperty(textReference) as String);
}
super.advanceTime(deltaTime);
}

private function addTextField(_text:String):void
{
text = _text;
textField = new TextField( textWidth, textHeight, text, fontName, fontSize, color, bold );
textField.autoScale = autoScale;
textField.border = border;
if ( this.displayObject != null ) //remove old text
scene.remove(this);

this.displayObject = textField as DisplayObject;
scene.add( this );
}

}

}

0 comments on commit b320f5f

Please sign in to comment.