Skip to content
Browse files

Component classes source codes.

First actual Genome2D source codes commit.
  • Loading branch information...
1 parent 96e8888 commit 44f28ad7f07ecde0a4312a14433a8e096ebaa2d3 @pshtif committed Dec 10, 2012
View
0 debug.log
No changes.
View
230 source/com/genome2d/components/GCamera.as
@@ -0,0 +1,230 @@
+/*
+* Genome2D - GPU 2D framework utilizing Molehill API
+*
+* Copyright 2011 Peter Stefcek. All rights reserved.
+*
+* License:: ./doc/LICENSE.md (https://github.com/pshtif/Genome2D/blob/master/LICENSE.md)
+*/
+package com.genome2d.components
+{
+ import avmplus.getQualifiedClassName;
+
+ import com.genome2d.context.GContext;
+ import com.genome2d.core.GNode;
+ import com.genome2d.g2d;
+
+ import flash.events.MouseEvent;
+ import flash.geom.Rectangle;
+ import flash.geom.Vector3D;
+
+ use namespace g2d;
+
+ public class GCamera extends GComponent
+ {
+ override public function getPrototype():XML {
+ _xPrototype = super.getPrototype();
+
+ return _xPrototype;
+ }
+
+ /**
+ * Camera mask used against node camera group a node is rendered through this camera if camera.mask and nodecameraGroup != 0
+ */
+ public var mask:int = 0xFFFFFF;
+ /**
+ * Viewport x offset, this value should be always within 0 and 1 its based on context main viewport
+ */
+ public var normalizedViewX:Number = 0;
+ /**
+ * Viewport y offset, this value should be always within 0 and 1 it based on context main viewport
+ */
+ public var normalizedViewY:Number = 0;
+ /**
+ * Viewport width, this value should be always within 0 and 1 its based on context main viewport
+ */
+ public var normalizedViewWidth:Number = 1;
+ /**
+ * Viewport height, this value should be always within 0 and 1 its based on context main viewport
+ */
+ public var normalizedViewHeight:Number = 1;
+
+ /**
+ * Red component of viewport background color
+ */
+ public var backgroundRed:Number = 0;
+ /**
+ * Green component of viewport background color
+ */
+ public var backgroundGreen:Number = 0;
+ /**
+ * Blue component of viewport background color
+ */
+ public var backgroundBlue:Number = 0;
+ /**
+ * @private
+ */
+ public var backgroundAlpha:Number = 0;
+
+ /**
+ * Get a viewport color
+ */
+ public function get backgroundColor():uint {
+ var alpha:uint = uint(backgroundAlpha*255)<<24;
+ var red:uint = uint(backgroundRed*255)<<16;
+ var green:uint = uint(backgroundGreen*255)<<8;
+ var blue:uint = uint(backgroundBlue*255);
+
+ return alpha+red+green+blue;
+ }
+ /**
+ * @private
+ */
+ g2d var rViewRectangle:Rectangle;
+ /**
+ * @private
+ */
+ g2d var rendererData:Object;
+
+ g2d var bCapturedThisFrame:Boolean = false;
+
+ /**
+ * @private
+ */
+ //g2d var nX:Number = 0;
+ /**
+ * @private
+ */
+ //g2d var nY:Number = 0;
+ /**
+ * @private
+ */
+ g2d var nViewX:Number = 0;
+ /**
+ * @private
+ */
+ g2d var nViewY:Number = 0;
+ /**
+ * @private
+ */
+ g2d var nScaleX:Number = 1;
+ /**
+ * @private
+ */
+ g2d var nScaleY:Number = 1;
+
+ g2d var aCameraVector:Vector.<Number> = new <Number>[0,0,0,0, 0,0,0,0];
+
+ g2d var iRenderedNodesCount:int;
+
+ public function get zoom():Number {
+ return nScaleX;
+ }
+ public function set zoom(p_value:Number):void {
+ nScaleX = nScaleY = p_value;
+ }
+
+ /**
+ * @private
+ */
+ //g2d var nRotation:Number = 0;
+
+ /**
+ * @private
+ */
+ public function GCamera(p_node:GNode) {
+ super(p_node);
+
+ rViewRectangle = new Rectangle();
+
+ if (cNode != cNode.cCore.root && cNode.isOnStage()) cNode.cCore.addCamera(this);
+
+ cNode.onAddedToStage.add(onAddedToStage);
+ cNode.onRemovedFromStage.add(onRemovedFromStage);
+ }
+
+ /**
+ * @private
+ */
+ override public function update(p_deltaTime:Number, p_parentTransformUpdate:Boolean, p_parentColorUpdate:Boolean):void {
+ }
+
+ g2d function invalidate():void {
+ rViewRectangle.x = normalizedViewX*cNode.cCore.cConfig.viewRect.width;
+ rViewRectangle.y = normalizedViewY*cNode.cCore.cConfig.viewRect.height;
+ var nw:Number = (normalizedViewWidth+normalizedViewX > 1) ? 1-normalizedViewX : normalizedViewWidth;
+ var nh:Number = (normalizedViewHeight+normalizedViewY > 1) ? 1-normalizedViewY : normalizedViewHeight;
+ rViewRectangle.width = nw*cNode.cCore.cConfig.viewRect.width;
+ rViewRectangle.height = nh*cNode.cCore.cConfig.viewRect.height;
+
+ aCameraVector[0] = cNode.cTransform.nWorldRotation;
+ aCameraVector[1] = rViewRectangle.x + rViewRectangle.width/2;
+ aCameraVector[2] = rViewRectangle.y + rViewRectangle.height/2;
+
+ aCameraVector[4] = cNode.cTransform.nWorldX;
+ aCameraVector[5] = cNode.cTransform.nWorldY;
+
+ aCameraVector[6] = nScaleX;
+ aCameraVector[7] = nScaleY;
+ }
+
+ /**
+ * @private
+ */
+ override public function render(p_context:GContext, p_camera:GCamera, p_maskRect:Rectangle):void {
+ if (p_camera != null || !cNode.active) return;
+ iRenderedNodesCount = 0;
+
+ if (backgroundAlpha != 0) p_context.blitColor(rViewRectangle.x + rViewRectangle.width/2, rViewRectangle.y + rViewRectangle.height/2, rViewRectangle.width, rViewRectangle.height, backgroundRed, backgroundGreen, backgroundBlue, backgroundAlpha, 1, rViewRectangle);
+
+ p_context.setCamera(this);
+ cNode.cCore.root.render(p_context, this, rViewRectangle, false);
+ }
+
+ /**
+ * @private
+ */
+ g2d function captureMouseEvent(p_captured:Boolean, p_event:MouseEvent, p_position:Vector3D):Boolean {
+ if (bCapturedThisFrame || !cNode.active) return false;
+ bCapturedThisFrame = true;
+
+ if (!rViewRectangle.contains(p_position.x, p_position.y)) return false;
+
+ p_position.x -= rViewRectangle.x + rViewRectangle.width/2;
+ p_position.y -= rViewRectangle.y + rViewRectangle.height/2;
+
+ var cos:Number = Math.cos(-cNode.cTransform.nWorldRotation);
+ var sin:Number = Math.sin(-cNode.cTransform.nWorldRotation);
+
+ var tx:Number = (p_position.x*cos - p_position.y*sin);
+ var ty:Number = (p_position.y*cos + p_position.x*sin);
+
+ tx /= nScaleY;
+ ty /= nScaleX;
+
+ p_position.x = tx + cNode.cTransform.nWorldX;
+ p_position.y = ty + cNode.cTransform.nWorldY;
+
+ return cNode.cCore.root.processMouseEvent(p_captured, p_event, p_position, this);
+ }
+
+ /**
+ * @private
+ */
+ override public function dispose():void {
+ cNode.cCore.removeCamera(this);
+
+ cNode.onAddedToStage.remove(onAddedToStage);
+ cNode.onRemovedFromStage.remove(onRemovedFromStage);
+
+ super.dispose();
+ }
+
+ private function onAddedToStage():void {
+ cNode.cCore.addCamera(this);
+ }
+
+ private function onRemovedFromStage():void {
+ cNode.cCore.removeCamera(this);
+ }
+ }
+}
View
197 source/com/genome2d/components/GComponent.as
@@ -0,0 +1,197 @@
+/*
+* Genome2D - GPU 2D framework utilizing Molehill API
+*
+* Copyright 2011 Peter Stefcek. All rights reserved.
+*
+* License:: ./doc/LICENSE.md (https://github.com/pshtif/Genome2D/blob/master/LICENSE.md)
+*/
+package com.genome2d.components
+{
+ import com.genome2d.context.GContext;
+ import com.genome2d.core.GNode;
+ import com.genome2d.g2d;
+
+ import flash.events.MouseEvent;
+ import flash.geom.Rectangle;
+ import flash.geom.Vector3D;
+ import flash.utils.describeType;
+ import flash.utils.getQualifiedClassName;
+
+ use namespace g2d;
+
+ public class GComponent
+ {
+ protected var _xPrototype:XML;
+ public function getPrototype():XML {
+ _xPrototype = <component/>;
+ _xPrototype.@id = _sId;
+ _xPrototype.@componentClass = getQualifiedClassName(this).split("::").join("-");
+ _xPrototype.@componentLookupClass = getQualifiedClassName(this.cLookupClass).split("::").join("-");
+
+ _xPrototype.properties = <properties/>;
+
+ var describe:XML = describeType(this);
+ var variables:XMLList = describe.variable;
+ var i:int;
+ for (i=0; i<variables.length(); ++i) {
+ var variable:XML = variables[i];
+ addPrototypeProperty(variable.@name, this[variable.@name], variable.@type);
+ }
+
+ var accessors:XMLList = describe.accessor;
+ for (i=0; i<accessors.length(); ++i) {
+ var accessor:XML = accessors[i];
+ if (accessor.@access != "readwrite") continue;
+ addPrototypeProperty(accessor.@name, this[accessor.@name], accessor.@type);
+ }
+
+ return _xPrototype;
+ }
+
+ protected function addPrototypeProperty(p_name:String, p_value:*, p_type:String, p_prototype:XML = null):void {
+ var node:XML;
+ p_type = p_type.toLowerCase();
+ var valueType:String = typeof(p_value);
+ // Discard complex types
+ if (valueType == "object" && (p_type!="array" && p_type!="object")) return;
+ if (valueType != "object") {
+ node = <{p_name} value={String(p_value)} type={p_type}/>;
+ }
+ /* Creation of simple arrays and objects not implemented yet */
+ else {
+ node = <{p_name} type={p_type}/>;
+ for (var it in p_value) {
+ addPrototypeProperty(it, p_value[it], typeof(p_value[it]), node);
+ }
+ }
+ /**/
+
+ if (p_prototype == null) _xPrototype.properties.appendChild(node);
+ else p_prototype.appendChild(node);
+ }
+
+ public function bindFromPrototype(p_prototype:XML):void {
+ _sId = p_prototype.@id;
+
+ var properties:XMLList = p_prototype.properties;
+ var count:int = properties.children().length();
+ for (var i:int = 0; i<count; ++i) {
+ bindPrototypeProperty(properties.children()[i], this);
+ }
+ }
+
+ public function bindPrototypeProperty(p_property:XML, p_object:Object):void {
+ var value:* = null;
+
+ if (p_property.@type == "object") {
+ // Not implemented yet
+ }
+
+ if (p_property.@type == "array") {
+ value = new Array();
+ var count:int = p_property.children().length();
+ for (var i:int = 0; i<count; ++i) bindPrototypeProperty(p_property.children()[i], value);
+ }
+
+ if (p_property.@type == "boolean") {
+ value = (p_property.@value == "false") ? false : true;
+ }
+
+ try {
+ p_object[p_property.name()] = (value == null) ? p_property.@value : value;
+ } catch (e:Error) {
+ trace("bindPrototypeProperty", e, p_object, p_property.name(), value);
+ }
+ }
+
+ protected var _bActive:Boolean = true;
+ /**
+ * @private
+ */
+ public function set active(p_value:Boolean):void {
+ _bActive = p_value;
+ }
+ /**
+ * @private
+ */
+ public function get active():Boolean {
+ return _bActive;
+ }
+
+ protected var _sId:String = "";
+ /**
+ * Component id, this property is read only
+ */
+ public function get id():String {
+ return _sId;
+ }
+
+ g2d var cLookupClass:Class;
+
+ /**
+ * @private
+ */
+ g2d var cPrevious:GComponent;
+ /**
+ * @private
+ */
+ g2d var cNext:GComponent;
+
+ /**
+ * @private
+ */
+ g2d var cNode:GNode;
+
+ /**
+ * @private
+ * This is used internally by a renderer to avoid direct referencing of renderer specific data, crucial for FP10 compatibility
+ */
+ g2d var cRenderData:Object
+
+ /**
+ * Get a node instance that is using this component
+ */
+ public function get node():GNode {
+ return cNode;
+ }
+
+ /**
+ * @private
+ */
+ public function GComponent(p_node:GNode) {
+ cNode = p_node;
+ }
+
+ /**
+ * Abstract method that should be overriden and implemented if you are creating your own components, its called each time a node that uses this component is being updated
+ */
+ public function update(p_deltaTime:Number, p_parentTransformUpdate:Boolean, p_parentColorUpdate:Boolean):void {
+
+ }
+ /**
+ * Abstract method that should be overriden and implemented if you are creating your own components, its called each time a node that uses this component is being rendered
+ */
+ public function render(p_context:GContext, p_camera:GCamera, p_maskRect:Rectangle):void {
+
+ }
+
+ /**
+ * Abstract method that should be overriden and implemented if you are creating your own components, its called each time a node that uses this component is processing mouse events
+ */
+ public function processMouseEvent(p_captured:Boolean, p_event:MouseEvent, p_position:Vector3D):Boolean {
+ return false;
+ }
+
+ /**
+ * Base dispose method, if there is a disposing you need to do in your extending component you should override it and always call super.dispose() its used when a node using this component is being disposed
+ */
+ public function dispose():void {
+ _bActive = false;
+
+ cNode = null;
+
+ cNext = null;
+ cPrevious = null;
+ }
+ }
+}
View
29 source/com/genome2d/components/GPrototypePropertyType.as
@@ -0,0 +1,29 @@
+package com.genome2d.components
+{
+ public class GPrototypePropertyType
+ {
+ static public const UNKNOWN:String = "unknown";
+ static public const NUMBER:String = "number";
+ static public const INT:String = "int";
+ static public const BOOLEAN:String = "boolean";
+ static public const OBJECT:String = "object";
+ static public const STRING:String = "string";
+
+ static public function getPrototypeType(p_value:*):String {
+ var type:String = typeof(p_value);
+
+ switch (type) {
+ case "number":
+ return NUMBER;
+ case "boolean":
+ return BOOLEAN;
+ case "string":
+ return STRING;
+ case "object":
+ return OBJECT;
+ }
+
+ return "unknown";
+ }
+ }
+}
View
318 source/com/genome2d/components/GTransform.as
@@ -0,0 +1,318 @@
+/*
+* Genome2D - GPU 2D framework utilizing Molehill API
+*
+* Copyright 2011 Peter Stefcek. All rights reserved.
+*
+* License:: ./doc/LICENSE.md (https://github.com/pshtif/Genome2D/blob/master/LICENSE.md)
+*/
+package com.genome2d.components
+{
+ import com.genome2d.core.GNode;
+ import com.genome2d.g2d;
+
+ import flash.geom.Matrix3D;
+ import flash.geom.Rectangle;
+ import flash.geom.Vector3D;
+
+ use namespace g2d;
+
+ public class GTransform extends GComponent
+ {
+ public var visible:Boolean = true;
+
+ private var __bWorldTransformMatrixDirty:Boolean = true;
+ private var __mWorldTransformMatrix:Matrix3D = new Matrix3D();
+ public function get worldTransformMatrix():Matrix3D {
+ if (bTransformDirty) invalidate(true, false);
+ if (__bWorldTransformMatrixDirty) {
+ __mWorldTransformMatrix.identity();
+ __mWorldTransformMatrix.prependScale(nWorldScaleX, nWorldScaleY, 1);
+ __mWorldTransformMatrix.prependRotation(nWorldRotation*180/Math.PI, Vector3D.Z_AXIS);
+ __mWorldTransformMatrix.appendTranslation(nWorldX, nWorldY, 0);
+ __bWorldTransformMatrixDirty = false;
+ }
+
+ return __mWorldTransformMatrix;
+ }
+
+ private var __mLocalTransformMatrix:Matrix3D = new Matrix3D();
+ public function get localTransformMatrix():Matrix3D {
+ __mLocalTransformMatrix.identity();
+ __mLocalTransformMatrix.prependScale(__nLocalScaleX, __nLocalScaleY, 1);
+ __mLocalTransformMatrix.prependRotation(__nLocalRotation*180/Math.PI, Vector3D.Z_AXIS);
+ __mLocalTransformMatrix.appendTranslation(nLocalX, nLocalY, 0);
+
+ return __mLocalTransformMatrix;
+ }
+
+ override public function set active(p_active:Boolean):void {
+ super.active = p_active;
+ bTransformDirty = _bActive;
+ }
+
+ public function getTransformedWorldTransformMatrix(p_scaleX:Number, p_scaleY:Number, p_rotation:Number, p_invert:Boolean):Matrix3D {
+ var matrix:Matrix3D = worldTransformMatrix.clone();
+
+ if (p_scaleX != 1 && p_scaleY != 1) matrix.prependScale(p_scaleX, p_scaleY, 1);
+ if (p_rotation != 0) matrix.prependRotation(p_rotation, Vector3D.Z_AXIS);
+ if (p_invert) matrix.invert();
+
+ return matrix;
+ }
+
+ /**
+ * @private
+ */
+ g2d var bTransformDirty:Boolean = true;
+ /**
+ * @private
+ */
+ g2d var nWorldX:Number = 0;
+ g2d var nLocalX:Number = 0;
+ public function get x():Number { return nLocalX };
+ public function set x(p_x:Number):void {
+ nWorldX = nLocalX = p_x;
+ bTransformDirty = true;
+ if (cNode.cBody) cNode.cBody.x = p_x;
+ if (rMaskRect) rAbsoluteMaskRect.x = rMaskRect.x + nWorldX;
+ }
+
+ /**
+ * @private
+ */
+ g2d var nWorldY:Number = 0;
+ g2d var nLocalY:Number = 0;
+ public function get y():Number { return nLocalY };
+ public function set y(p_y:Number):void {
+ nWorldY = nLocalY = p_y;
+ bTransformDirty = true;
+ if (cNode.cBody) cNode.cBody.y = p_y;
+ if (rMaskRect) rAbsoluteMaskRect.y = rMaskRect.y + nWorldY;
+ }
+
+ public function setPosition(p_x:Number, p_y:Number):void {
+ nWorldX = nLocalX = p_x;
+ nWorldY = nLocalY = p_y;
+ bTransformDirty = true;
+ if (cNode.cBody) {
+ cNode.cBody.x = p_x;
+ cNode.cBody.y = p_y;
+ }
+ if (rMaskRect) {
+ rAbsoluteMaskRect.x = rMaskRect.x + nWorldX;
+ rAbsoluteMaskRect.y = rMaskRect.y + nWorldY;
+ }
+ }
+
+ public function setScale(p_scaleX:Number, p_scaleY:Number):void {
+ nWorldScaleX = __nLocalScaleX = p_scaleX;
+ nWorldScaleY = __nLocalScaleY = p_scaleY;
+ bTransformDirty = true;
+ if (cNode.cBody) {
+ cNode.cBody.scaleX = p_scaleX;
+ cNode.cBody.scaleY = p_scaleY;
+ }
+ }
+
+ /**
+ * @private
+ */
+ g2d var nWorldScaleX:Number = 1;
+ private var __nLocalScaleX:Number = 1;
+ public function get scaleX():Number { return __nLocalScaleX };
+ public function set scaleX(p_scaleX:Number):void {
+ nWorldScaleX = __nLocalScaleX = p_scaleX;
+ bTransformDirty = true;
+ if (cNode.cBody) cNode.cBody.scaleX = p_scaleX;
+ }
+
+ /**
+ * @private
+ */
+ g2d var nWorldScaleY:Number = 1;
+ private var __nLocalScaleY:Number = 1;
+ public function get scaleY():Number { return __nLocalScaleY };
+ public function set scaleY(p_scaleY:Number):void {
+ nWorldScaleY = __nLocalScaleY = p_scaleY;
+ bTransformDirty = true;
+ if (cNode.cBody) cNode.cBody.scaleY = p_scaleY;
+ }
+
+ /**
+ * @private
+ */
+ g2d var nWorldRotation:Number = 0;
+ private var __nLocalRotation:Number = 0;
+ public function get rotation():Number { return __nLocalRotation };
+ public function set rotation(p_rotation:Number):void {
+ nWorldRotation = __nLocalRotation = p_rotation;
+ bTransformDirty = true;
+ if (cNode.cBody) cNode.cBody.rotation = p_rotation;
+ }
+
+ /**
+ * @private
+ */
+ g2d var bColorDirty:Boolean = true;
+
+ public function set color(p_value:int):void {
+ red = Number(p_value>>16&0xFF)/0xFF;
+ green = Number(p_value>>8&0xFF)/0xFF;
+ blue = Number(p_value&0xFF)/0xFF;
+ }
+
+ /**
+ * @private
+ */
+ g2d var nWorldRed:Number = 1;
+ private var _red:Number = 1;
+ public function get red():Number { return _red };
+ public function set red(p_red:Number):void {
+ nWorldRed = _red = p_red;
+ bColorDirty = true;
+ }
+
+ /**
+ * @private
+ */
+ g2d var nWorldGreen:Number = 1;
+ private var _green:Number = 1;
+ public function get green():Number { return _green };
+ public function set green(p_green:Number):void {
+ nWorldGreen = _green = p_green;
+ bColorDirty = true;
+ }
+
+ /**
+ * @private
+ */
+ g2d var nWorldBlue:Number = 1;
+ private var _blue:Number = 1;
+ public function get blue():Number { return _blue };
+ public function set blue(p_blue:Number):void {
+ nWorldBlue = _blue = p_blue;
+ bColorDirty = true;
+ }
+
+ /**
+ * @private
+ */
+ g2d var nWorldAlpha:Number = 1;
+ private var _alpha:Number = 1;
+ public function get alpha():Number { return _alpha };
+ public function set alpha(p_alpha:Number):void {
+ nWorldAlpha = _alpha = p_alpha;
+ bColorDirty = true;
+ }
+
+ public var useWorldSpace:Boolean = false;
+ public var useWorldColor:Boolean = false;
+
+ g2d var cMask:GNode;
+ public function get mask():GNode {
+ return cMask;
+ }
+ public function set mask(p_mask:GNode):void {
+ if (cMask) cMask.iUsedAsMask--;
+ cMask = p_mask;
+ cMask.iUsedAsMask++;
+ }
+
+ g2d var rMaskRect:Rectangle;
+ public function get maskRect():Rectangle {
+ return rMaskRect;
+ }
+ public function set maskRect(p_rect:Rectangle):void {
+ rMaskRect = p_rect;
+ rAbsoluteMaskRect = p_rect.clone();
+ rAbsoluteMaskRect.x += nWorldX;
+ rAbsoluteMaskRect.y += nWorldY;
+ }
+ g2d var rAbsoluteMaskRect:Rectangle;
+
+ /**
+ * @private
+ */
+ public function GTransform(p_node:GNode) {
+ super(p_node);
+ }
+
+ /**
+ * @private
+ */
+ g2d function invalidate(p_invalidateTransform:Boolean, p_invalidateColor:Boolean):void {
+ if (cNode.cParent == null) return;
+
+ var parentTransform:GTransform = cNode.cParent.cTransform;
+ if (cNode.cBody != null && cNode.cBody.isDynamic()) {
+ nLocalX = nWorldX = cNode.cBody.x;
+ nLocalY = nWorldY = cNode.cBody.y;
+ __nLocalRotation = nWorldRotation = cNode.cBody.rotation;
+
+ __bWorldTransformMatrixDirty = true;
+ } else {
+ if (p_invalidateTransform) {
+ if (!useWorldSpace) {
+ if (parentTransform.nWorldRotation != 0) {
+ var cos:Number = Math.cos(parentTransform.nWorldRotation);
+ var sin:Number = Math.sin(parentTransform.nWorldRotation);
+
+ nWorldX = (nLocalX*cos - nLocalY*sin)*parentTransform.nWorldScaleX + parentTransform.nWorldX;
+ nWorldY = (nLocalY*cos + nLocalX*sin)*parentTransform.nWorldScaleY + parentTransform.nWorldY;
+ } else {
+ nWorldX = nLocalX*parentTransform.nWorldScaleX + parentTransform.nWorldX;
+ nWorldY = nLocalY*parentTransform.nWorldScaleY + parentTransform.nWorldY;
+ }
+ nWorldScaleX = __nLocalScaleX * parentTransform.nWorldScaleX;
+ nWorldScaleY = __nLocalScaleY * parentTransform.nWorldScaleY;
+ nWorldRotation = __nLocalRotation + parentTransform.nWorldRotation;
+
+ if (rMaskRect) {
+ rAbsoluteMaskRect.x = rMaskRect.x + nWorldX;
+ rAbsoluteMaskRect.y = rMaskRect.y + nWorldY;
+ }
+
+ bTransformDirty = false;
+ __bWorldTransformMatrixDirty = true;
+ }
+ }
+ }
+
+ if (p_invalidateColor && !useWorldColor) {
+ nWorldRed = _red * parentTransform.nWorldRed;
+ nWorldGreen = _green * parentTransform.nWorldGreen;
+ nWorldBlue = _blue * parentTransform.nWorldBlue;
+ nWorldAlpha = _alpha * parentTransform.nWorldAlpha;
+
+ bColorDirty = false;
+ }
+ }
+
+ public function setColor(p_red:Number=1, p_green:Number=1, p_blue:Number=1, p_alpha:Number=1):void {
+ red = p_red;
+ green = p_green;
+ blue = p_blue;
+ alpha = p_alpha;
+ }
+
+ public function worldToLocal(p_position:Vector3D):Vector3D {
+ if (cNode.cParent == null) return p_position;
+
+ var matrix:Matrix3D = getTransformedWorldTransformMatrix(1,1,0,true);
+
+ return matrix.transformVector(p_position);
+ }
+
+ public function localToWorld(p_position:Vector3D):Vector3D {
+ if (cNode.cParent == null) return p_position;
+
+ p_position = localTransformMatrix.transformVector(p_position);
+
+ return cNode.cParent.cTransform.localToWorld(p_position);
+ }
+
+ public function toString():String {
+ return "["+x+","+y+","+scaleX+","+scaleY+"]\n["+nWorldX+","+nWorldY+"]";
+ }
+ }
+}
View
81 source/com/genome2d/components/physics/GBody.as
@@ -0,0 +1,81 @@
+/*
+* Genome2D - GPU 2D framework utilizing Molehill API
+*
+* Copyright 2011 Peter Stefcek. All rights reserved.
+*
+* License:: ./doc/LICENSE.md (https://github.com/pshtif/Genome2D/blob/master/LICENSE.md)
+*/
+package com.genome2d.components.physics
+{
+ import com.genome2d.core.GNode;
+ import com.genome2d.g2d;
+ import com.genome2d.components.GComponent;
+ import com.genome2d.components.GTransform;
+
+ use namespace g2d;
+
+ public class GBody extends GComponent
+ {
+ public function get x():Number {
+ return 0;
+ }
+ public function set x(p_x:Number):void {
+ }
+
+ public function get y():Number {
+ return 0;
+ }
+ public function set y(p_y:Number):void {
+ }
+
+ public function get scaleX():Number {
+ return 1;
+ }
+ public function set scaleX(p_scaleX:Number):void {
+ }
+
+ public function get scaleY():Number {
+ return 1;
+ }
+ public function set scaleY(p_scaleY:Number):void {
+ }
+
+ public function get rotation():Number {
+ return 0;
+ }
+ public function set rotation(p_rotation:Number):void {
+ }
+
+ public function isDynamic():Boolean {
+ return false;
+ }
+
+ public function isKinematic():Boolean {
+ return false;
+ }
+ /**
+ * @private
+ */
+ public function GBody(p_node:GNode):void {
+ super(p_node);
+ }
+
+ /**
+ * @private
+ */
+ g2d function addToSpace():void {
+ }
+
+ /**
+ * @private
+ */
+ g2d function removeFromSpace():void {
+ }
+
+ /**
+ * @private
+ */
+ g2d function invalidateKinematic(p_transform:GTransform):void {
+ }
+ }
+}
View
59 source/com/genome2d/components/renderables/GColorQuad.as
@@ -0,0 +1,59 @@
+/*
+* Genome2D - GPU 2D framework utilizing Molehill API
+*
+* Copyright 2011 Peter Stefcek. All rights reserved.
+*
+* License:: ./doc/LICENSE.md (https://github.com/pshtif/Genome2D/blob/master/LICENSE.md)
+*/
+package com.genome2d.components.renderables
+{
+ import com.genome2d.components.GCamera;
+ import com.genome2d.components.GTransform;
+ import com.genome2d.context.GContext;
+ import com.genome2d.context.materials.GCameraColorQuadVertexShaderBatchMaterial;
+ import com.genome2d.core.GNode;
+ import com.genome2d.g2d;
+
+ import flash.geom.Rectangle;
+
+ use namespace g2d;
+
+ public class GColorQuad extends GRenderable
+ {
+ /**
+ * @private
+ */
+ public function GColorQuad(p_node:GNode) {
+ super(p_node);
+
+ if (cMaterial == null) cMaterial = new GCameraColorQuadVertexShaderBatchMaterial();
+ }
+
+ static private var cMaterial:GCameraColorQuadVertexShaderBatchMaterial;
+ static private var cTransformVector:Vector.<Number> = new <Number>[0,0,0,0, 0,1,1,1, 1,1,1,1];
+
+ /**
+ * @private
+ */
+ override public function render(p_context:GContext, p_camera:GCamera, p_maskRect:Rectangle):void {
+ if (p_context.checkAndSetupRender(cMaterial, iBlendMode, true, p_maskRect)) cMaterial.bind(p_context.cContext, p_context.bReinitialize, p_camera);
+
+ var transform:GTransform = cNode.cTransform;
+
+ cTransformVector[0] = transform.nWorldX;
+ cTransformVector[1] = transform.nWorldY;
+ cTransformVector[2] = transform.nWorldScaleX;
+ cTransformVector[3] = transform.nWorldScaleY;
+
+ cTransformVector[4] = transform.nWorldRotation;
+
+ cTransformVector[8] = transform.nWorldRed;
+ cTransformVector[9] = transform.nWorldGreen;
+ cTransformVector[10] = transform.nWorldBlue;
+ cTransformVector[11] = transform.nWorldAlpha;
+
+ /**/
+ cMaterial.draw(cTransformVector);
+ }
+ }
+}
View
139 source/com/genome2d/components/renderables/GMovieClip.as
@@ -0,0 +1,139 @@
+/*
+* Genome2D - GPU 2D framework utilizing Molehill API
+*
+* Copyright 2011 Peter Stefcek. All rights reserved.
+*
+* License:: ./doc/LICENSE.md (https://github.com/pshtif/Genome2D/blob/master/LICENSE.md)
+*/
+package com.genome2d.components.renderables
+{
+ import avmplus.getQualifiedClassName;
+
+ import com.genome2d.core.GNode;
+ import com.genome2d.g2d;
+ import com.genome2d.textures.GTextureAtlas;
+ import com.genome2d.textures.GTextureBase;
+
+ use namespace g2d;
+
+ public class GMovieClip extends GTexturedQuad
+ {
+ private var _nSpeed:Number = 1000/30;
+ private var _nAccumulatedTime:Number = 0;
+
+ private var _iCurrentFrame:int = -1;
+ public function get currentFrame():int {
+ return _iCurrentFrame;
+ }
+
+ private var _iStartIndex:int = -1;
+ private var _iEndIndex:int = -1;
+ private var _bPlaying:Boolean = true;
+
+ private var __cTextureAtlas:GTextureAtlas;
+ public function get textureAtlasId():String {
+ return (__cTextureAtlas) ? __cTextureAtlas.id : "";
+ }
+ public function set textureAtlasId(p_value:String):void {
+ __cTextureAtlas = (p_value != "") ? GTextureAtlas.getTextureAtlasById(p_value) : null;
+ if (__aFrameIds) cTexture = __cTextureAtlas.getTexture(__aFrameIds[0]);
+ }
+
+ private var __aFrameIds:Array;
+ private var __iFrameIdsLength:int = 0;
+ public function get frames():Array {
+ return __aFrameIds;
+ }
+ public function set frames(p_value:Array):void {
+ __aFrameIds = p_value;
+ __iFrameIdsLength = __aFrameIds.length;
+ _iCurrentFrame = 0;
+ if (__cTextureAtlas) cTexture = __cTextureAtlas.getTexture(__aFrameIds[0]);
+ }
+
+ public var repeatable:Boolean = true;
+
+ static private var __iCount:int = 0;
+
+ /**
+ * @private
+ */
+ public function GMovieClip(p_node:GNode) {
+ super(p_node);
+ }
+
+ /**
+ * Set texture atlas that should be used by this movie clip
+ */
+ public function setTextureAtlas(p_textureAtlas:GTextureAtlas):void {
+ __cTextureAtlas = p_textureAtlas;
+ if (__aFrameIds) cTexture = __cTextureAtlas.getTexture(__aFrameIds[0]);
+ }
+
+ public function get frameRate():int {
+ return 1000/_nSpeed;
+ }
+ /**
+ * Set framerate at which this clip should play
+ */
+ public function set frameRate(p_frameRate:int):void {
+ _nSpeed = 1000/p_frameRate;
+ }
+
+ public function get numFrames():int {
+ return __iFrameIdsLength;
+ }
+
+ /**
+ * Go to a specified frame of this movie clip
+ */
+ public function gotoFrame(p_frame:int):void {
+ if (__aFrameIds == null) return;
+ _iCurrentFrame = p_frame;
+ _iCurrentFrame %= __aFrameIds.length;
+ cTexture = __cTextureAtlas.getTexture(__aFrameIds[_iCurrentFrame]);
+ }
+
+ public function gotoAndPlay(p_frame:int):void {
+ gotoFrame(p_frame);
+ play();
+ }
+
+ /**
+ * Stop playback of this movie clip
+ */
+ public function stop():void {
+ _bPlaying = false;
+ }
+
+ /**
+ * Start the playback of this movie clip
+ */
+ public function play():void {
+ _bPlaying = true;
+ }
+
+ /**
+ * @private
+ */
+ override public function update(p_deltaTime:Number, p_parentTransformUpdate:Boolean, p_parentColorUpdate:Boolean):void {
+ if (cTexture == null) return;
+
+ if (_bPlaying) {
+ _nAccumulatedTime += p_deltaTime;
+
+ if (_nAccumulatedTime >= _nSpeed) {
+ _iCurrentFrame += _nAccumulatedTime/_nSpeed;
+ if (_iCurrentFrame<__iFrameIdsLength || repeatable) {
+ _iCurrentFrame %= __aFrameIds.length;
+ } else {
+ _iCurrentFrame = __iFrameIdsLength-1;
+ }
+
+ cTexture = __cTextureAtlas.getTexture(__aFrameIds[_iCurrentFrame]);
+ }
+ _nAccumulatedTime %= _nSpeed;
+ }
+ }
+ }
+}
View
42 source/com/genome2d/components/renderables/GRenderable.as
@@ -0,0 +1,42 @@
+/*
+* Genome2D - GPU 2D framework utilizing Molehill API
+*
+* Copyright 2011 Peter Stefcek. All rights reserved.
+*
+* License:: ./doc/LICENSE.md (https://github.com/pshtif/Genome2D/blob/master/LICENSE.md)
+*/
+package com.genome2d.components.renderables
+{
+ import com.genome2d.components.GComponent;
+ import com.genome2d.context.GBlendMode;
+ import com.genome2d.core.GNode;
+ import com.genome2d.g2d;
+
+ import flash.geom.Rectangle;
+
+ use namespace g2d;
+
+ public class GRenderable extends GComponent {
+
+ g2d var iBlendMode:int = GBlendMode.NORMAL;
+ public function set blendMode(p_blendMode:int):void {
+ iBlendMode = p_blendMode;
+ }
+ public function get blendMode():int {
+ return iBlendMode;
+ }
+ /**
+ * @private
+ */
+ public function GRenderable(p_node:GNode) {
+ super(p_node);
+ }
+
+ public function getWorldBounds(p_target:Rectangle = null):Rectangle {
+ if (p_target) p_target.setTo(cNode.cTransform.nWorldX, cNode.cTransform.nWorldY, 0, 0);
+ else p_target = new Rectangle(cNode.cTransform.nWorldX, cNode.cTransform.nWorldY, 0, 0);
+
+ return p_target;
+ }
+ }
+}
View
87 source/com/genome2d/components/renderables/GShape.as
@@ -0,0 +1,87 @@
+package com.genome2d.components.renderables
+{
+ import com.genome2d.g2d;
+ import com.genome2d.components.GCamera;
+ import com.genome2d.components.GTransform;
+ import com.genome2d.context.GContext;
+ import com.genome2d.context.materials.GCameraTexturedPolygonMaterial;
+ import com.genome2d.core.GNode;
+ import com.genome2d.textures.GTexture;
+
+ import flash.geom.Rectangle;
+
+ use namespace g2d;
+
+ public class GShape extends GRenderable
+ {
+ static private var cTransformVector:Vector.<Number> = new <Number>[0,0,0,0, 0,0,0,0, 0,1,1,1, 1,1,1,1];
+
+ protected var _cMaterial:GCameraTexturedPolygonMaterial;
+ g2d var cTexture:GTexture;
+
+ protected var _iMaxVertices:int = 0;
+ protected var _iCurrentVertices:int = 0;
+ protected var _aVertices:Vector.<Number>;
+ protected var _aUVs:Vector.<Number>;
+
+ protected var __bDirty:Boolean = false;
+
+ public function setTexture(p_texture:GTexture):void {
+ cTexture = p_texture;
+ }
+
+ public function GShape(p_node:GNode) {
+ super(p_node);
+
+ _cMaterial = new GCameraTexturedPolygonMaterial();
+ }
+
+ override public function render(p_context:GContext, p_camera:GCamera, p_maskRect:Rectangle):void {
+ if (cTexture == null || _iMaxVertices == 0) return;
+
+ p_context.checkAndSetupRender(_cMaterial, iBlendMode, cTexture.premultiplied, p_maskRect);
+ _cMaterial.bind(p_context.cContext, p_context.bReinitialize, p_camera, _iMaxVertices);
+ var transform:GTransform = cNode.cTransform;
+
+ cTransformVector[0] = transform.nWorldX;
+ cTransformVector[1] = transform.nWorldY;
+ cTransformVector[2] = transform.nWorldScaleX;
+ cTransformVector[3] = transform.nWorldScaleY;
+
+ cTransformVector[4] = cTexture.nUvX;
+ cTransformVector[5] = cTexture.nUvY;
+ cTransformVector[6] = cTexture.nUvScaleX;
+ cTransformVector[7] = cTexture.nUvScaleY;
+
+ cTransformVector[8] = transform.nWorldRotation;
+ cTransformVector[10] = cTexture.nPivotX * transform.nWorldScaleX;
+ cTransformVector[11] = cTexture.nPivotY * transform.nWorldScaleY;
+
+ cTransformVector[12] = transform.nWorldRed*transform.nWorldAlpha;
+ cTransformVector[13] = transform.nWorldGreen*transform.nWorldAlpha;
+ cTransformVector[14] = transform.nWorldBlue*transform.nWorldAlpha;
+ cTransformVector[15] = transform.nWorldAlpha;
+ //trace(cTransformVector);
+ /**/
+ _cMaterial.draw(cTransformVector, cTexture.cContextTexture.tTexture, cTexture.iFilteringType, _aVertices, _aUVs, _iCurrentVertices, __bDirty);
+
+ __bDirty = false;
+ }
+
+ public function init(p_vertices:Vector.<Number>, p_uvs:Vector.<Number>):void {
+ __bDirty = true;
+ _iCurrentVertices = p_vertices.length/2;
+
+ if (p_vertices.length/2 > _iMaxVertices) {
+ _iMaxVertices = p_vertices.length/2;
+ _aVertices = p_vertices;
+ _aUVs = p_uvs;
+ } else {
+ for (var i:int = 0; i<_iCurrentVertices*2; ++i) {
+ _aVertices[i] = p_vertices[i];
+ _aUVs[i] = p_uvs[i];
+ }
+ }
+ }
+ }
+}
View
43 source/com/genome2d/components/renderables/GSprite.as
@@ -0,0 +1,43 @@
+/*
+* Genome2D - GPU 2D framework utilizing Molehill API
+*
+* Copyright 2011 Peter Stefcek. All rights reserved.
+*
+* License:: ./doc/LICENSE.md (https://github.com/pshtif/Genome2D/blob/master/LICENSE.md)
+*/
+package com.genome2d.components.renderables
+{
+ import com.genome2d.core.GNode;
+ import com.genome2d.error.GError;
+ import com.genome2d.g2d;
+ import com.genome2d.textures.GTexture;
+ import com.genome2d.textures.GTextureBase;
+
+ use namespace g2d;
+
+ public class GSprite extends GTexturedQuad
+ {
+ public function get textureId():String {
+ if (cTexture) return cTexture.id;
+ return "";
+ }
+ public function set textureId(p_value:String):void {
+ cTexture = GTextureBase.getTextureBaseById(p_value) as GTexture;
+ //if (cTexture == null) throw new GError(GError.TEXTURE_ID_DOESNT_EXIST, p_value);
+ }
+
+ /**
+ * Set a texture that should be used by this sprite
+ */
+ public function setTexture(p_texture:GTexture):void {
+ cTexture = p_texture;
+ }
+ /**
+ * @private
+ */
+ public function GSprite(p_node:GNode)
+ {
+ super(p_node);
+ }
+ }
+}
View
197 source/com/genome2d/components/renderables/GTextureText.as
@@ -0,0 +1,197 @@
+/*
+* Genome2D - GPU 2D framework utilizing Molehill API
+*
+* Copyright 2011 Peter Stefcek. All rights reserved.
+*
+* License:: ./doc/LICENSE.md (https://github.com/pshtif/Genome2D/blob/master/LICENSE.md)
+*/
+package com.genome2d.components.renderables
+{
+ import com.genome2d.g2d;
+ import com.genome2d.core.GNode;
+ import com.genome2d.core.GNodeFactory;
+ import com.genome2d.error.GError;
+ import com.genome2d.textures.GTexture;
+ import com.genome2d.textures.GTextureAtlas;
+
+ import flash.events.MouseEvent;
+ import flash.geom.Matrix3D;
+ import flash.geom.Vector3D;
+
+ use namespace g2d;
+
+ public class GTextureText extends GRenderable
+ {
+ private var __cTextureAtlas:GTextureAtlas;
+
+ private var __bInvalidate:Boolean = false;
+
+ private var __nTracking:Number = 0;
+ public function get tracking():Number {
+ return __nTracking;
+ }
+ public function set tracking(p_tracking:Number):void {
+ __nTracking = p_tracking;
+ __bInvalidate = true;
+ }
+
+ private var __iAlign:int = GTextureTextAlignType.TOP_LEFT;
+ public function get align():int {
+ return __iAlign;
+ }
+ public function set align(p_align:int):void {
+ __iAlign = p_align;
+ __bInvalidate = true;
+ }
+
+ /**
+ * @private
+ */
+ public function GTextureText(p_node:GNode) {
+ super(p_node);
+ }
+
+ public function get textureAtlasId():String {
+ if (__cTextureAtlas) return __cTextureAtlas.id;
+ return "";
+ }
+
+ public function set textureAtlasId(p_value:String):void {
+ setTextureAtlas(GTextureAtlas.getTextureAtlasById(p_value));
+ }
+
+ public function setTextureAtlas(p_textureAtlas:GTextureAtlas):void {
+ __cTextureAtlas = p_textureAtlas;
+ __bInvalidate = true;
+ }
+
+ private var __sText:String = "";
+ public function get text():String {
+ return __sText;
+ }
+ public function set text(p_text:String):void {
+ __sText = p_text;
+ __bInvalidate = true;
+ }
+
+ private var __nWidth:Number = 0;
+ public function get width():Number {
+ if (__bInvalidate) invalidateText();
+
+ return __nWidth*cNode.cTransform.nWorldScaleX;
+ }
+
+ private var __nHeight:Number = 0;
+ public function get height():Number {
+ if (__bInvalidate) invalidateText();
+
+ return __nHeight*cNode.cTransform.nWorldScaleY;
+ }
+
+ override public function update(p_deltaTime:Number, p_parentTransformUpdate:Boolean, p_parentColorUpdate:Boolean):void {
+ if (!__bInvalidate) return;
+
+ invalidateText();
+ }
+
+ private function invalidateText():void {
+ if (__cTextureAtlas == null) return;
+
+ var offset:int = 0;
+ var charNode:GNode = cNode.firstChild;
+ var charSprite:GSprite;
+ var texture:GTexture;
+
+ for (var i:int = 0; i<__sText.length; ++i) {
+ texture = __cTextureAtlas.getTexture(String(__sText.charCodeAt(i)));
+ if (texture == null) throw new GError(GError.NO_TEXTURE_FOR_CHARACTER_FOUND+__sText.charCodeAt(i)+" "+__sText.charAt(i));
+ __nHeight = texture.height;
+ if (charNode == null) {
+ charSprite = GNodeFactory.createNodeWithComponent(GSprite) as GSprite;
+ charNode = charSprite.cNode;
+ cNode.addChild(charNode);
+ } else {
+ charSprite = charNode.getComponent(GSprite) as GSprite;
+ }
+ charSprite.node.cameraGroup = node.cameraGroup;
+ charSprite.setTexture(texture);
+ offset += texture.width/2;
+ charSprite.cNode.cTransform.x = offset;
+ charSprite.cNode.cTransform.y = texture.height/2;
+ offset += texture.width/2 + __nTracking;
+ charNode = charNode.next;
+ }
+
+ __nWidth = offset;
+
+ while (charNode) {
+ var next:GNode = charNode.next;
+ cNode.removeChild(charNode);
+ charNode = next;
+ }
+
+ invalidateAlign();
+
+ __bInvalidate = false;
+ }
+
+ private function invalidateAlign():void {
+ var node:GNode;
+ switch (__iAlign) {
+ case GTextureTextAlignType.MIDDLE:
+ for (node = cNode.firstChild; node; node = node.next) {
+ node.transform.x -= __nWidth/2;
+ node.transform.y -= __nHeight/2;
+ }
+ break;
+ case GTextureTextAlignType.TOP_RIGHT:
+ for (node = cNode.firstChild; node; node = node.next) {
+ node.transform.x -= __nWidth;
+ }
+ break;
+ case GTextureTextAlignType.TOP_LEFT:
+ break;
+ }
+ }
+
+ /**
+ * @private
+ */
+ override public function processMouseEvent(p_captured:Boolean, p_event:MouseEvent, p_position:Vector3D):Boolean {
+ if (p_captured) {
+ if (cNode.cMouseOver == cNode) cNode.handleMouseEvent(cNode, MouseEvent.MOUSE_OUT, Number.NaN, Number.NaN, p_event.buttonDown, p_event.ctrlKey);
+ return false;
+ }
+
+ var transformMatrix:Matrix3D = cNode.cTransform.getTransformedWorldTransformMatrix(__nWidth, __nHeight, 0, true);
+
+ var localMousePosition:Vector3D = transformMatrix.transformVector(p_position);
+
+ transformMatrix.prependScale(1/__nWidth, 1/__nHeight, 1);
+
+ var tx:Number = 0;
+ var ty:Number = 0;
+ switch (__iAlign) {
+ case GTextureTextAlignType.MIDDLE:
+ tx = -.5;
+ ty = -.5;
+ break;
+ }
+
+ if (localMousePosition.x >= tx && localMousePosition.x <= 1+tx && localMousePosition.y >= ty && localMousePosition.y <= 1+ty) {
+ cNode.handleMouseEvent(cNode, p_event.type, localMousePosition.x*__nWidth, localMousePosition.y*__nHeight, p_event.buttonDown, p_event.ctrlKey);
+ if (cNode.cMouseOver != cNode) {
+ cNode.handleMouseEvent(cNode, MouseEvent.MOUSE_OVER, localMousePosition.x*__nWidth, localMousePosition.y*__nHeight, p_event.buttonDown, p_event.ctrlKey);
+ }
+
+ return true;
+ } else {
+ if (cNode.cMouseOver == cNode) {
+ cNode.handleMouseEvent(cNode, MouseEvent.MOUSE_OUT, localMousePosition.x*__nWidth, localMousePosition.y*__nHeight, p_event.buttonDown, p_event.ctrlKey);
+ }
+ }
+
+ return false;
+ }
+ }
+}
View
9 source/com/genome2d/components/renderables/GTextureTextAlignType.as
@@ -0,0 +1,9 @@
+package com.genome2d.components.renderables
+{
+ public class GTextureTextAlignType
+ {
+ static public var TOP_LEFT:int = 0;
+ static public var TOP_RIGHT:int = 1;
+ static public var MIDDLE:int = 2;
+ }
+}
View
223 source/com/genome2d/components/renderables/GTexturedQuad.as
@@ -0,0 +1,223 @@
+/*
+* Genome2D - GPU 2D framework utilizing Molehill API
+*
+* Copyright 2011 Peter Stefcek. All rights reserved.
+*
+* License:: ./doc/LICENSE.md (https://github.com/pshtif/Genome2D/blob/master/LICENSE.md)
+*/
+package com.genome2d.components.renderables
+{
+ import com.genome2d.g2d;
+ import com.genome2d.components.GCamera;
+ import com.genome2d.components.GTransform;
+ import com.genome2d.context.GContext;
+ import com.genome2d.context.filters.GFilter;
+ import com.genome2d.core.GNode;
+ import com.genome2d.textures.GTexture;
+
+ import flash.events.MouseEvent;
+ import flash.geom.Matrix3D;
+ import flash.geom.Rectangle;
+ import flash.geom.Vector3D;
+
+ use namespace g2d;
+
+ public class GTexturedQuad extends GRenderable
+ {
+ static private const NORMALIZED_VERTICES_3D:Vector.<Number> = Vector.<Number>([
+ -.5, .5, 0,
+ -.5, -.5, 0,
+ .5, -.5, 0,
+ .5, .5, 0
+ ]);
+
+ public var filter:GFilter;
+
+ /**
+ * @private
+ */
+ g2d var cTexture:GTexture;
+ public function getTexture():GTexture {
+ return cTexture;
+ }
+
+ private var __aTransformedVertices:Vector.<Number> = new Vector.<Number>();
+
+ /**
+ * If this flag is true any node using this component will use pixel perfect mouse detection based on a data from this texture
+ */
+ public var mousePixelEnabled:Boolean = false;
+
+ /**
+ * @private
+ */
+ public function GTexturedQuad(p_node:GNode) {
+ super(p_node);
+ }
+
+ /**
+ * @private
+ */
+ override public function render(p_context:GContext, p_camera:GCamera, p_maskRect:Rectangle):void {
+ if (cTexture == null) return;
+
+ var transform:GTransform = cNode.cTransform;
+ p_context.draw(cTexture, transform.nWorldX, transform.nWorldY, transform.nWorldScaleX, transform.nWorldScaleY, transform.nWorldRotation, transform.nWorldRed, transform.nWorldGreen, transform.nWorldBlue, transform.nWorldAlpha, iBlendMode, p_maskRect, filter);
+ }
+
+ override public function getWorldBounds(p_target:Rectangle = null):Rectangle {
+ var vertices:Vector.<Number> = getTransformedVertices3D();
+ if (vertices == null) return p_target;
+ if (p_target) p_target.setTo(vertices[0], vertices[1],0,0);
+ else p_target = new Rectangle(vertices[0], vertices[1],0,0);
+ var length:int = vertices.length;
+ for (var i:int=3; i<length; i+=3) {
+ if (p_target.left>vertices[i]) p_target.left = vertices[i];
+ if (p_target.right<vertices[i]) p_target.right = vertices[i];
+ if (p_target.top>vertices[i+1]) p_target.top = vertices[i+1];
+ if (p_target.bottom<vertices[i+1]) p_target.bottom = vertices[i+1];
+ }
+
+ return p_target;
+ }
+
+ /**
+ * @private
+ */
+ g2d function getTransformedVertices3D():Vector.<Number> {
+ if (cTexture == null) return null;
+ var region:Rectangle = cTexture.region;
+
+ var transformMatrix:Matrix3D = cNode.cTransform.worldTransformMatrix;
+ transformMatrix.prependScale(region.width, region.height, 1);
+
+ transformMatrix.transformVectors(NORMALIZED_VERTICES_3D, __aTransformedVertices);
+
+ transformMatrix.prependScale(1/region.width, 1/region.height, 1);
+ return __aTransformedVertices;
+ }
+
+ /**
+ * Hit test detection of this and remote G2DSprite component
+ */
+ public function hitTestObject(p_sprite:GTexturedQuad):Boolean {
+ var tvs1:Vector.<Number> = p_sprite.getTransformedVertices3D();
+ var tvs2:Vector.<Number> = getTransformedVertices3D();
+
+ var cx:Number = (tvs1[0]+tvs1[3]+tvs1[6]+tvs1[9])/4;
+ var cy:Number = (tvs1[1]+tvs1[4]+tvs1[7]+tvs1[10])/4;
+
+ if (isSeparating(tvs1[3], tvs1[4], tvs1[0]-tvs1[3], tvs1[1]-tvs1[4], cx, cy, tvs2)) return false;
+
+ if (isSeparating(tvs1[6], tvs1[7], tvs1[3]-tvs1[6], tvs1[4]-tvs1[7], cx, cy, tvs2)) return false;
+
+ if (isSeparating(tvs1[9], tvs1[10], tvs1[6]-tvs1[9], tvs1[7]-tvs1[10], cx, cy, tvs2)) return false;
+
+ if (isSeparating(tvs1[0], tvs1[1], tvs1[9]-tvs1[0], tvs1[10]-tvs1[1], cx, cy, tvs2)) return false;
+
+ cx = (tvs2[0]+tvs2[3]+tvs2[6]+tvs2[9])/4;
+ cy = (tvs2[1]+tvs2[4]+tvs2[7]+tvs2[10])/4;
+
+ if (isSeparating(tvs2[3], tvs2[4], tvs2[0]-tvs2[3], tvs2[1]-tvs2[4], cx, cy, tvs1)) return false;
+
+ if (isSeparating(tvs2[6], tvs2[7], tvs2[3]-tvs2[6], tvs2[4]-tvs2[7], cx, cy, tvs1)) return false;
+
+ if (isSeparating(tvs2[9], tvs2[10], tvs2[6]-tvs2[9], tvs2[7]-tvs2[10], cx, cy, tvs1)) return false;
+
+ if (isSeparating(tvs2[0], tvs2[1], tvs2[9]-tvs2[0], tvs2[10]-tvs2[1], cx, cy, tvs1)) return false;
+
+ return true;
+ }
+
+ private function isSeparating(p_sx:Number, p_sy:Number, p_ex:Number, p_ey:Number, p_cx:Number, p_cy:Number, p_vertices:Vector.<Number>):Boolean {
+ var rx:Number = -p_ey;
+ var ry:Number = p_ex;
+
+ var sideCenter:Number = rx * (p_cx - p_sx) + ry * (p_cy - p_sy);
+
+ var sideV1:Number = rx * (p_vertices[0] - p_sx) + ry * (p_vertices[1] - p_sy);
+ var sideV2:Number = rx * (p_vertices[3] - p_sx) + ry * (p_vertices[4] - p_sy);
+ var sideV3:Number = rx * (p_vertices[6] - p_sx) + ry * (p_vertices[7] - p_sy);
+ var sideV4:Number = rx * (p_vertices[9] - p_sx) + ry * (p_vertices[10] - p_sy);
+
+ if (sideCenter < 0 && sideV1 >= 0 && sideV2 >= 0 && sideV3 >= 0 && sideV4 >= 0) return true;
+ if (sideCenter > 0 && sideV1 <= 0 && sideV2 <= 0 && sideV3 <= 0 && sideV4 <= 0) return true;
+
+ return false;
+ }
+
+ /**
+ * Hit test point if its within this quad
+ */
+ public function hitTestPoint(p_point:Vector3D, p_pixelEnabled:Boolean = false):Boolean {
+ var tWidth:Number = cTexture.width;// * cTexture.resampleScale;
+ var tHeight:Number = cTexture.height;// * cTexture.resampleScale;
+
+ var transformMatrix:Matrix3D = cNode.cTransform.getTransformedWorldTransformMatrix(tWidth, tHeight, 0, true);
+
+ var localPoint:Vector3D = transformMatrix.transformVector(p_point);
+ localPoint.x = (localPoint.x+.5);
+ localPoint.y = (localPoint.y+.5);
+
+ if (localPoint.x >= -cTexture.nPivotX/tWidth && localPoint.x <= 1-cTexture.nPivotX/tWidth && localPoint.y >= -cTexture.nPivotY/tHeight && localPoint.y <= 1-cTexture.nPivotY/tHeight) {
+ if (mousePixelEnabled && cTexture.getAlphaAtUV(localPoint.x+cTexture.pivotX/tWidth, localPoint.y+cTexture.nPivotY/tHeight) == 0) {
+ return false;
+ }
+ return true;
+ }
+
+ return false;
+ }
+
+ /**
+ * @private
+ */
+ override public function processMouseEvent(p_captured:Boolean, p_event:MouseEvent, p_position:Vector3D):Boolean {
+ if (p_captured && p_event.type == MouseEvent.MOUSE_UP) cNode.cMouseDown = null;
+ if (p_captured || cTexture == null) {
+ if (cNode.cMouseOver == cNode) cNode.handleMouseEvent(cNode, MouseEvent.MOUSE_OUT, Number.NaN, Number.NaN, p_event.buttonDown, p_event.ctrlKey);
+ return false;
+ }
+
+ var tWidth:Number = cTexture.width;// * cTexture.resampleScale;
+ var tHeight:Number = cTexture.height;// * cTexture.resampleScale;
+
+ var transformMatrix:Matrix3D = cNode.cTransform.getTransformedWorldTransformMatrix(tWidth, tHeight, 0, true);
+
+ var localMousePosition:Vector3D = transformMatrix.transformVector(p_position);
+
+ localMousePosition.x = (localMousePosition.x+.5);
+ localMousePosition.y = (localMousePosition.y+.5);
+
+ if (localMousePosition.x >= -cTexture.nPivotX/tWidth && localMousePosition.x <= 1-cTexture.nPivotX/tWidth && localMousePosition.y >= -cTexture.nPivotY/tHeight && localMousePosition.y <= 1-cTexture.nPivotY/tHeight) {
+ if (mousePixelEnabled && cTexture.getAlphaAtUV(localMousePosition.x+cTexture.pivotX/tWidth, localMousePosition.y+cTexture.nPivotY/tHeight) == 0) {
+ if (cNode.cMouseOver == cNode) {
+ cNode.handleMouseEvent(cNode, MouseEvent.MOUSE_OUT, localMousePosition.x*tWidth+cTexture.nPivotX, localMousePosition.y*tHeight+cTexture.nPivotY, p_event.buttonDown, p_event.ctrlKey);
+ }
+ return false;
+ }
+ cNode.handleMouseEvent(cNode, p_event.type, localMousePosition.x*tWidth+cTexture.nPivotX, localMousePosition.y*tHeight+cTexture.nPivotY, p_event.buttonDown, p_event.ctrlKey);
+ if (cNode.cMouseOver != cNode) {
+ cNode.handleMouseEvent(cNode, MouseEvent.MOUSE_OVER, localMousePosition.x*tWidth+cTexture.nPivotX, localMousePosition.y*tHeight+cTexture.nPivotY, p_event.buttonDown, p_event.ctrlKey);
+ }
+
+ return true;
+ } else {
+ if (cNode.cMouseOver == cNode) {
+ cNode.handleMouseEvent(cNode, MouseEvent.MOUSE_OUT, localMousePosition.x*tWidth+cTexture.nPivotX, localMousePosition.y*tHeight+cTexture.nPivotY, p_event.buttonDown, p_event.ctrlKey);
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * @private
+ */
+ override public function dispose():void {
+ super.dispose();
+
+ cTexture = null;
+ }
+ }
+}
View
11 source/com/genome2d/components/renderables/GTile.as
@@ -0,0 +1,11 @@
+package com.genome2d.components.renderables
+{
+ public class GTile
+ {
+ public var textureId:String;
+
+ public function GTile() {
+
+ }
+ }
+}
View
113 source/com/genome2d/components/renderables/GTileMap.as
@@ -0,0 +1,113 @@
+package com.genome2d.components.renderables
+{
+ import com.genome2d.g2d;
+ import com.genome2d.components.GCamera;
+ import com.genome2d.components.renderables.GRenderable;
+ import com.genome2d.context.GContext;
+ import com.genome2d.core.GNode;
+ import com.genome2d.textures.GTexture;
+ import com.genome2d.textures.GTextureAtlas;
+
+ import flash.geom.Rectangle;
+ import flash.utils.clearTimeout;
+
+ use namespace g2d;
+
+ public class GTileMap extends GRenderable
+ {
+ private var __iWidth:int;
+ private var __iHeight:int;
+ private var __aTiles:Vector.<GTile>;
+
+ private var __iTileWidth:int = 0;
+ private var __iTileHeight:int = 0;
+ private var __bIso:Boolean = false;
+
+ public function GTileMap(p_node:GNode) {
+ super(p_node);
+ }
+
+ public function setTiles(p_tiles:Vector.<GTile>, p_mapWidth:int, p_mapHeight:int, p_tileWidth:int, p_tileHeight:int, p_iso:Boolean = false):void {
+ if (p_mapWidth*p_mapHeight != p_tiles.length) throw new Error("Invalid tile map.");
+
+ __aTiles = p_tiles;
+ __iWidth = p_mapWidth;
+ __iHeight = p_mapHeight;
+ __bIso = p_iso;
+
+ setTileSize(p_tileWidth, p_tileHeight);
+ }
+
+ public function setTile(p_tileIndex:int, p_tile:int):void {
+ if (p_tileIndex<0 || p_tileIndex>= __aTiles.length) return;
+ __aTiles[p_tileIndex] = p_tile;
+ }
+
+ public function setTileSize(p_width:int, p_height:int):void {
+ __iTileWidth = p_width;
+ __iTileHeight = p_height;
+ }
+
+ override public function render(p_context:GContext, p_camera:GCamera, p_maskRect:Rectangle):void {
+ if (__aTiles == null) return;
+
+ var mapHalfWidth:Number = __iTileWidth * __iWidth * .5;
+ var mapHalfHeight:Number = __iTileHeight * __iHeight * (__bIso ? .25 : .5);
+
+ // Position of top left visible tile from 0,0
+ var startX:Number = p_camera.cNode.cTransform.nWorldX - cNode.cTransform.nWorldX - p_camera.rViewRectangle.width *.5;
+ var startY:Number = p_camera.cNode.cTransform.nWorldY - cNode.cTransform.nWorldY - p_camera.rViewRectangle.height *.5;
+ // Position of top left tile from map center
+ var firstX:Number = -mapHalfWidth + (__bIso ? __iTileWidth/2 : 0);
+ var firstY:Number = -mapHalfHeight + (__bIso ? __iTileHeight/2 : 0);
+
+ // Index of top left visible tile
+ var indexX:int = (startX - firstX) / __iTileWidth;
+ if (indexX<0) indexX = 0;
+ var indexY:int = (startY - firstY) / (__bIso ? __iTileHeight/2 : __iTileHeight);
+ if (indexY<0) indexY = 0;
+
+ // Position of bottom right tile from map center
+ var endX:Number = p_camera.cNode.cTransform.nWorldX - cNode.cTransform.nWorldX + p_camera.rViewRectangle.width * .5 - (__bIso ? __iTileWidth/2 : __iTileWidth);
+ var endY:Number = p_camera.cNode.cTransform.nWorldY - cNode.cTransform.nWorldY + p_camera.rViewRectangle.height * .5 - (__bIso ? 0 : __iTileHeight);
+
+ var indexWidth:int = (endX - firstX) / __iTileWidth - indexX+2;
+ if (indexWidth>__iWidth-indexX) indexWidth = __iWidth - indexX;
+
+ var indexHeight:int = (endY - firstY) / (__bIso ? __iTileHeight/2 : __iTileHeight) - indexY+2;
+ if (indexHeight>__iHeight-indexY) indexHeight = __iHeight - indexY;
+ //trace(indexX, indexY, indexWidth, indexHeight);
+ var tileCount:int = indexWidth*indexHeight;
+ for (var i:int=0; i<tileCount; ++i) {
+ var row:int = int(i / indexWidth);
+ var x:Number = cNode.cTransform.nWorldX + (indexX + (i % indexWidth)) * __iTileWidth - mapHalfWidth + (__bIso && (indexY+row)%2 == 1 ? __iTileWidth : __iTileWidth/2);
+ var y:Number = cNode.cTransform.nWorldY + (indexY + row) * (__bIso ? __iTileHeight/2 : __iTileHeight) - mapHalfHeight + __iTileHeight/2;
+
+ var index:int = indexY * __iWidth + indexX + int(i / indexWidth) * __iWidth + i % indexWidth;
+ var tile:GTile = __aTiles[index];
+ // TODO: All transforms
+ if (tile != null && tile.textureId != null) p_context.draw(GTexture.getTextureById(tile.textureId), x, y, 1, 1, 0, 1, 1, 1, 1, 1, p_maskRect);
+ }
+ }
+
+ public function getTileAt(p_x:Number, p_y:Number, p_camera:GCamera = null):void {
+ if (p_camera == null) p_camera = node.core.defaultCamera;
+
+ p_x -= p_camera.rViewRectangle.x + p_camera.rViewRectangle.width/2;
+ p_y -= p_camera.rViewRectangle.y + p_camera.rViewRectangle.height/2;
+
+ var mapHalfWidth:Number = __iTileWidth * __iWidth * .5;
+ var mapHalfHeight:Number = __iTileHeight * __iHeight * (__bIso ? .25 : .5);
+
+ var firstX:Number = -mapHalfWidth + (__bIso ? __iTileWidth/2 : 0);
+ var firstY:Number = -mapHalfHeight + (__bIso ? __iTileHeight/2 : 0);
+ trace(firstX, firstY);
+ var tx:Number = p_camera.cNode.cTransform.nWorldX - cNode.cTransform.nWorldX + p_x;
+ var ty:Number = p_camera.cNode.cTransform.nWorldY - cNode.cTransform.nWorldY + p_y;
+ trace(tx, ty);
+ var indexX:int = (tx - firstX) / __iTileWidth;
+ var indexY:int = (ty - firstY) / __iTileHeight;
+ trace(indexX, indexY);
+ }
+ }
+}
View
189 source/com/genome2d/components/renderables/flash/GFlashObject.as
@@ -0,0 +1,189 @@
+/*
+* Genome2D - GPU 2D framework utilizing Molehill API
+*
+* Copyright 2011 Peter Stefcek. All rights reserved.
+*
+* License:: ./doc/LICENSE.md (https://github.com/pshtif/Genome2D/blob/master/LICENSE.md)
+*/
+package com.genome2d.components.renderables.flash
+{
+ import com.genome2d.components.GCamera;
+ import com.genome2d.components.renderables.GTexturedQuad;
+ import com.genome2d.context.GBlendMode;
+ import com.genome2d.context.GContext;
+ import com.genome2d.core.GNode;
+ import com.genome2d.error.GError;
+ import com.genome2d.g2d;
+ import com.genome2d.textures.GTexture;
+ import com.genome2d.textures.GTextureAlignType;
+ import com.genome2d.textures.GTextureFilteringType;
+ import com.genome2d.textures.GTextureResampleType;
+ import com.genome2d.textures.factories.GTextureFactory;
+
+ import flash.display.BitmapData;
+ import flash.display.DisplayObject;
+ import flash.events.MouseEvent;
+ import flash.geom.Matrix;
+ import flash.geom.Rectangle;
+ import flash.geom.Vector3D;
+
+ use namespace g2d;
+
+ public class GFlashObject extends GTexturedQuad
+ {
+ static public var defaultSampleScale:int = 1;
+ static public var defaultUpdateFrameRate:int = 20;
+ static private var __iDefaultResampleType:int = GTextureResampleType.UP_CROP;
+ static public function get defaultResampleType():int {
+ return __iDefaultResampleType;
+ }
+ static public function set defaultResampleType(p_type:int):void {
+ if (!GTextureResampleType.isValid(p_type)) throw new GError(GError.INVALID_RESAMPLE_TYPE);
+ __iDefaultResampleType = p_type;
+ }
+
+ protected var _iAlign:int = GTextureAlignType.CENTER;
+ public function get align():int {
+ return _iAlign;
+ }
+ public function set align(p_align:int):void {
+ _iAlign = p_align;
+ invalidateTexture(true);
+ }
+
+ protected var _doNative:DisplayObject;
+ public function get native():DisplayObject {
+ return _doNative;
+ }
+ public function set native(p_native:DisplayObject):void {
+ _doNative = p_native;
+ }
+
+ private var __mNativeMatrix:Matrix;
+
+ private var __sTextureId:String;
+
+ protected var _bInvalidate:Boolean = false;
+ public function invalidate(p_force:Boolean = false):void {
+ if (p_force) invalidateTexture(true);
+ else _bInvalidate = true;
+ }
+
+ protected var _iResampleScale:int = defaultSampleScale;
+ public function get resampleScale():int {
+ return _iResampleScale;
+ }
+ public function set resampleScale(p_scale:int):void {
+ if (p_scale<=0) return;
+ _iResampleScale = p_scale;
+ if (_doNative != null) invalidateTexture(true);
+ }
+
+ protected var _iFilteringType:int = GTextureFilteringType.NEAREST;
+ public function get filteringType():int {
+ return _iFilteringType;
+ }
+ public function set filteringType(p_filteringType:int):void {
+ _iFilteringType = p_filteringType;
+ if (cTexture) cTexture.filteringType = _iFilteringType;
+ }
+
+ protected var _iResampleType:int = __iDefaultResampleType;
+ public function get resampleType():int {
+ return _iResampleType;
+ }
+ public function set resampleType(p_type:int):void {
+ if (!GTextureResampleType.isValid(p_type)) throw new GError(GError.INVALID_RESAMPLE_TYPE);
+ _iResampleType = p_type;
+ if (_doNative != null) invalidateTexture(true);
+ }
+
+ private var __nLastNativeWidth:Number = 0;
+ private var __nLastNativeHeight:Number = 0;
+ private var __nAccumulatedTime:Number = 0;
+
+ public var updateFrameRate:int = defaultUpdateFrameRate;
+ protected var _bTransparent:Boolean = false;
+ public function set transparent(p_transparent:Boolean):void {
+ _bTransparent = p_transparent;
+ if (_doNative != null) invalidateTexture(true);
+ }
+ public function get transparent():Boolean {
+ return _bTransparent;
+ }
+
+ static private var __iCount:int = 0;
+ /**
+ * @private
+ */
+ public function GFlashObject(p_node:GNode) {
+ super(p_node);
+
+ iBlendMode = GBlendMode.NONE;
+ __sTextureId = "G2DNativeObject#"+__iCount++;
+ __mNativeMatrix = new Matrix();
+ }
+
+ override public function update(p_deltaTime:Number, p_parentTransformUpdate:Boolean, p_parentColorUpdate:Boolean):void {
+ if (_doNative == null) return;
+
+ invalidateTexture(false);
+
+ __nAccumulatedTime += p_deltaTime;
+ var updateTime:Number = 1000/updateFrameRate;
+ if (_bInvalidate || __nAccumulatedTime > updateTime) {
+ cTexture.bitmapData.fillRect(cTexture.bitmapData.rect, 0x0);
+ cTexture.bitmapData.draw(_doNative, __mNativeMatrix);
+ cTexture.invalidate();
+
+ __nAccumulatedTime %= updateTime;
+ }
+
+ _bInvalidate = false;
+ }
+ /**/
+ override public function render(p_context:GContext, p_camera:GCamera, p_maskRect:Rectangle):void {
+ cNode.cTransform.nWorldScaleX *= _iResampleScale;
+ cNode.cTransform.nWorldScaleY *= _iResampleScale;
+
+ super.render(p_context, p_camera, p_maskRect);
+
+ cNode.cTransform.nWorldScaleX /= _iResampleScale;
+ cNode.cTransform.nWorldScaleY /= _iResampleScale;
+ }
+ /**/
+ protected function invalidateTexture(p_force:Boolean):void {
+ if (_doNative == null) return;
+ if (!p_force && __nLastNativeWidth == _doNative.width && __nLastNativeHeight == _doNative.height) return;
+
+ __nLastNativeWidth = _doNative.width;
+ __nLastNativeHeight = _doNative.height;
+
+ __mNativeMatrix.identity();
+ __mNativeMatrix.scale(_doNative.scaleX/_iResampleScale, _doNative.scaleY/_iResampleScale);
+ var bitmapData:BitmapData = new BitmapData(__nLastNativeWidth/_iResampleScale, __nLastNativeHeight/_iResampleScale, _bTransparent, 0x0);
+
+ //__mNativeMatrix.scale(_doNative.scaleX, _doNative.scaleY);
+ //var bitmapData:BitmapData = new BitmapData(__nLastNativeWidth, __nLastNativeHeight, _bTransparent, 0x000000);
+
+ if (cTexture == null) {
+ cTexture = GTextureFactory.createFromBitmapData(__sTextureId, bitmapData);
+ //cTexture.resampleScale = _iResampleScale;
+ cTexture.resampleType = _iResampleType;
+ cTexture.filteringType = _iFilteringType;
+ } else {
+ cTexture.bitmapData = bitmapData;
+ }
+
+ cTexture.alignTexture(_iAlign);
+
+ _bInvalidate = true;
+ }
+
+ override public function dispose():void {
+ cTexture.dispose();
+
+ super.dispose();
+ }
+ }
+}
View
113 source/com/genome2d/components/renderables/flash/GFlashText.as
@@ -0,0 +1,113 @@
+/*
+* Genome2D - GPU 2D framework utilizing Molehill API
+*
+* Copyright 2011 Peter Stefcek. All rights reserved.
+*
+* License:: ./doc/LICENSE.md (https://github.com/pshtif/Genome2D/blob/master/LICENSE.md)
+*/
+package com.genome2d.components.renderables.flash
+{
+ import com.genome2d.core.GNode;
+ import com.genome2d.g2d;
+
+ import flash.text.TextField;
+ import flash.text.TextFormat;
+
+ use namespace g2d;
+
+ public class GFlashText extends GFlashObject
+ {
+ private var __tfoTextFormat:TextFormat;
+ private var __tfTextField:TextField;
+
+ public function set textFormat(p_textFormat:TextFormat):void {
+ __tfTextField.defaultTextFormat = p_textFormat;
+ if (__tfTextField.text.length > 0) __tfTextField.setTextFormat(p_textFormat, 0, __tfTextField.text.length-1);
+
+ _bInvalidate = true;
+ }
+
+ public function set embedFonts(p_value:Boolean):void {
+ __tfTextField.embedFonts = p_value;
+ }
+
+ public function set background(p_background:Boolean):void {
+ __tfTextField.background = p_background;
+
+ _bInvalidate = true;
+ }
+
+ public function set wordWrap(p_wordWrap:Boolean):void {
+ __tfTextField.wordWrap = p_wordWrap;
+
+ _bInvalidate = true;
+ }
+
+ public function set backgroundColor(p_backgroundColor:int):void {
+ __tfTextField.backgroundColor = p_backgroundColor;
+
+ _bInvalidate = true;
+ }
+
+ public function set htmlText(p_htmlText:String):void {
+ __tfTextField.htmlText = p_htmlText;
+
+ _bInvalidate = true;
+ }
+
+ public function set text(p_text:String):void {
+ __tfTextField.text = p_text;
+
+ _bInvalidate = true;
+ }
+
+ public function set multiLine(p_multiline:Boolean):void {
+ __tfTextField.multiline = p_multiline;
+
+ _bInvalidate = true;
+ }
+
+ public function set textColor(p_textColor:int):void {
+ __tfTextField.textColor = p_textColor;
+
+ _bInvalidate = true;
+ }
+
+ public function set autoSize(p_autoSize:String):void {
+ __tfTextField.autoSize = p_autoSize;
+
+ _bInvalidate = true;
+ }
+
+ public function get width():Number {
+ return __tfTextField.width;
+ }
+ public function set width(p_width:Number):void {
+ __tfTextField.width = p_width;
+
+ _bInvalidate = true;
+ }
+
+ public function get height():Number {
+ return __tfTextField.height;
+ }
+ public function set height(p_height:Number):void {
+ __tfTextField.height = p_height;
+
+ _bInvalidate = true;
+ }
+
+ static private var __iCount:int = 0;
+ /**
+ * @private
+ */
+ public function GFlashText(p_node:GNode) {
+ super(p_node);
+
+ updateFrameRate = 0;
+
+ __tfTextField = new TextField();
+ _doNative = __tfTextField;
+ }
+ }
+}
View
106 source/com/genome2d/components/renderables/flash/GFlashVideo.as
@@ -0,0 +1,106 @@
+/*
+* Genome2D - GPU 2D framework utilizing Molehill API
+*
+* Copyright 2011 Peter Stefcek. All rights reserved.
+*
+* License:: ./doc/LICENSE.md (https://github.com/pshtif/Genome2D/blob/master/LICENSE.md)
+*/
+package com.genome2d.components.renderables.flash
+{
+ import com.genome2d.core.GNode;
+ import com.genome2d.g2d;
+ import com.genome2d.textures.GTextureResampleType;
+
+ import flash.display.Shape;
+ import flash.events.IOErrorEvent;
+ import flash.events.NetStatusEvent;
+ import flash.media.Video;
+ import flash.net.NetConnection;
+ import flash.net.NetStream;
+
+ use namespace g2d;
+
+ public class GFlashVideo extends GFlashObject
+ {
+ private var __ncConnection:NetConnection;
+
+ private var __nsStream:NetStream;
+ public function get netStream():NetStream {
+ return __nsStream;
+ }
+
+ private var __vNativeVideo:Video;
+ public function get nativeVideo():Video {
+ return __vNativeVideo;
+ }
+
+ private var __nAccumulatedTime:int;
+
+ private var __bPlaying:Boolean = false;
+ private var __sTextureId:String;
+
+ static private var __iCount:int = 0;
+ /**
+ * @private
+ */
+ public function GFlashVideo(p_node:GNode) {
+ super(p_node);
+
+ _iResampleType = GTextureResampleType.NEAREST_DOWN_RESAMPLE_UP_CROP;
+ __sTextureId = "G2DVideo#"+__iCount++;
+
+ __ncConnection = new NetConnection();
+ __ncConnection.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
+ __ncConnection.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
+ __ncConnection.connect(null);
+
+ __nsStream = new NetStream(__ncConnection);
+ __nsStream.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
+ __nsStream.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
+ __nsStream.client = this;
+
+ __vNativeVideo = new Video();
+ __vNativeVideo.attachNetStream(__nsStream);
+ _doNative = __vNativeVideo;
+ }
+
+ public function onMetaData(p_data:Object, ... args):void {
+ __vNativeVideo.width = (p_data.width!=undefined) ? p_data.width : 320;
+ __vNativeVideo.height = (p_data.height!=undefined) ? p_data.height : 240;
+
+ if (updateFrameRate != 0 && p_data.framerate != undefined) updateFrameRate = p_data.framerate;