Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package {
- import com.genome2d.core.GConfig;
- import com.genome2d.core.GNode;
- import com.genome2d.core.GNodeFactory;
- import com.genome2d.core.Genome2D;
- import com.genome2d.g2d;
- import com.genome2d.textures.GTextureAlignType;
- import com.genome2d.textures.factories.GTextureFactory;
- import flash.display.Graphics;
- import flash.display.Sprite;
- import flash.display.StageAlign;
- import flash.display.StageQuality;
- import flash.display.StageScaleMode;
- import flash.geom.Rectangle;
- [SWF(width="800", height="600", backgroundColor="#232323", frameRate="60")]
- use namespace g2d ;
- public class TestProject extends Sprite {
- private var world:GNode;
- public function TestProject() {
- init() ;
- stage.scaleMode = StageScaleMode.NO_SCALE;
- stage.align = StageAlign.TOP_LEFT;
- stage.quality = StageQuality.LOW;
- stage.showDefaultContextMenu = false;
- }
- private function init(): void {
- var config:GConfig = new GConfig(new Rectangle(0,0,800,600));
- config.backgroundColor = stage.color ;
- Genome2D.getInstance().onInitialized.addOnce(onGInitialized);
- Genome2D.getInstance().init(stage, config);
- }
- private function onGInitialized(): void {
- GTextureFactory.createFromColor('red', 0xFFFFFF, 32, 32).alignTexture(GTextureAlignType.TOP_LEFT);
- world = GNodeFactory.createNode();
- Genome2D.getInstance().root.addChild(world);
- world.transform.setPosition(50,80);
- for (var i: int = 0; i < 12; i++) {
- var btn:Button = GNodeFactory.createNodeWithComponent(Button) as Button ;
- btn.setLabel('Button N' + (Math.pow(10,i)));
- btn.node.transform.y = i * 30 ;
- btn.node.transform.x = i * 30 ;
- world.addChild(btn.node);
- // -- insane approach :D
- // world.core.updateAndRender();
- // trace(btn.node.getWorldBounds());
- }
- // OPTION 1:
- // world.core.updateAndRender();
- // drawWorldBoundries();
- // OPTION 2:
- // otherwise, wait 1 frame til the render process calculates the positions and bounding boxes.
- // order: preUpdate,postUpdate,preRender, postRender... everything happens in 1 enterFrame tick.
- Genome2D.getInstance().onPostRender.addOnce(function():void{drawWorldBoundries()});
- }
- private function drawWorldBoundries(): void {
- var rect:Rectangle = world.getWorldBounds() ;
- var g: Graphics = this.graphics ;
- g.beginFill(0x00FF00, 0.2 );
- g.drawRect(rect.x,rect.y,rect.width,rect.height);
- }
- }
- }
- import com.genome2d.components.GComponent;
- import com.genome2d.components.renderables.GSprite;
- import com.genome2d.components.renderables.flash.GFlashText;
- import com.genome2d.context.GBlendMode;
- import com.genome2d.core.GNode;
- import com.genome2d.core.GNodeFactory;
- import com.genome2d.textures.GTexture;
- import com.genome2d.textures.GTextureAlignType;
- import flash.geom.Rectangle;
- import flash.text.TextFieldAutoSize;
- import flash.text.TextFormat;
- class Button extends GComponent {
- private var bg: GSprite;
- private var tf: GFlashText;
- private var w: int = 100;
- private var h: int = 25 ;
- private static var textFormat: TextFormat;
- private static var bgTx: GTexture;
- public function Button(pNode:GNode) {
- super(pNode);
- if(!textFormat){
- textFormat = new TextFormat('Arial', 22, 0xFF0000 );
- bgTx = GTexture.getTextureById('red');
- }
- init() ;
- }
- private function init(): void {
- bg = GNodeFactory.createNodeWithComponent(GSprite) as GSprite ;
- bg.setTexture(bgTx);
- tf = GNodeFactory.createNodeWithComponent(GFlashText) as GFlashText ;
- tf.textFormat = textFormat ;
- tf.transparent = true ;
- tf.autoSize = TextFieldAutoSize.LEFT ;
- tf.multiLine = false ;
- tf.blendMode = GBlendMode.NORMAL;
- tf.align = GTextureAlignType.TOP_LEFT ;
- bg.node.transform.setScale( w/bgTx.width, h/bgTx.height );
- node.addChild(bg.node);
- node.addChild(tf.node);
- }
- public function setLabel(pLabel:String):void {
- tf.text = pLabel ;
- // you can update the bg width here, or wait for the postRender to avoid a forced invalidatation.
- // tf.invalidate(true);
- // updateButtonWidth();
- // Is preferable to do this on the parent node, or anywhere where u store this instances.
- // and toggle node's visibility when u finally render the stuffs, otherwise, it'll look
- // weird as u'll see 1 frame the bg size not-synced.
- // node.transform.visible = false ;
- node.core.onPostRender.addOnce(updateButtonWidth);
- }
- private function updateButtonWidth(): void {
- var r:Rectangle = tf.node.getWorldBounds();
- w = r.width ;
- bg.node.transform.scaleX = w/bgTx.width ;
- // node.transform.visible = true ;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment