rodrigolopezpeker

Genome2D > testing invalidations in v0.9.3.1193

Oct 26th, 2013
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package {
  2.  
  3.     import com.genome2d.core.GConfig;
  4.     import com.genome2d.core.GNode;
  5.     import com.genome2d.core.GNodeFactory;
  6.     import com.genome2d.core.Genome2D;
  7.     import com.genome2d.g2d;
  8.     import com.genome2d.textures.GTextureAlignType;
  9.     import com.genome2d.textures.factories.GTextureFactory;
  10.  
  11.     import flash.display.Graphics;
  12.     import flash.display.Sprite;
  13.     import flash.display.StageAlign;
  14.     import flash.display.StageQuality;
  15.     import flash.display.StageScaleMode;
  16.     import flash.geom.Rectangle;
  17.  
  18.     [SWF(width="800", height="600", backgroundColor="#232323", frameRate="60")]
  19.  
  20.     use namespace g2d ;
  21.     public class TestProject extends Sprite {
  22.  
  23.         private var world:GNode;
  24.  
  25.         public function TestProject() {
  26.             init() ;
  27.             stage.scaleMode = StageScaleMode.NO_SCALE;
  28.             stage.align = StageAlign.TOP_LEFT;
  29.             stage.quality = StageQuality.LOW;
  30.             stage.showDefaultContextMenu = false;
  31.         }
  32.  
  33.         private function init(): void {
  34.             var config:GConfig = new GConfig(new Rectangle(0,0,800,600));
  35.             config.backgroundColor = stage.color ;
  36.             Genome2D.getInstance().onInitialized.addOnce(onGInitialized);
  37.             Genome2D.getInstance().init(stage, config);
  38.         }
  39.  
  40.  
  41.         private function onGInitialized(): void {
  42.             GTextureFactory.createFromColor('red', 0xFFFFFF, 32, 32).alignTexture(GTextureAlignType.TOP_LEFT);
  43.             world = GNodeFactory.createNode();
  44.             Genome2D.getInstance().root.addChild(world);
  45.             world.transform.setPosition(50,80);
  46.             for (var i: int = 0; i < 12; i++) {
  47.                 var btn:Button = GNodeFactory.createNodeWithComponent(Button) as Button ;
  48.                 btn.setLabel('Button N' + (Math.pow(10,i)));
  49.                 btn.node.transform.y = i * 30 ;
  50.                 btn.node.transform.x = i * 30 ;
  51.                 world.addChild(btn.node);
  52. //              -- insane approach :D
  53. //              world.core.updateAndRender();
  54. //              trace(btn.node.getWorldBounds());
  55.             }
  56.  
  57.             // OPTION 1:
  58. //          world.core.updateAndRender();
  59. //          drawWorldBoundries();
  60.  
  61.             // OPTION 2:
  62.             // otherwise, wait 1 frame til the render process calculates the positions and bounding boxes.
  63.             // order: preUpdate,postUpdate,preRender, postRender... everything happens in 1 enterFrame tick.
  64.             Genome2D.getInstance().onPostRender.addOnce(function():void{drawWorldBoundries()});
  65.         }
  66.  
  67.         private function drawWorldBoundries(): void {
  68.             var rect:Rectangle = world.getWorldBounds() ;
  69.             var g: Graphics = this.graphics ;
  70.             g.beginFill(0x00FF00, 0.2 );
  71.             g.drawRect(rect.x,rect.y,rect.width,rect.height);
  72.         }
  73.     }
  74. }
  75.  
  76.  
  77. import com.genome2d.components.GComponent;
  78. import com.genome2d.components.renderables.GSprite;
  79. import com.genome2d.components.renderables.flash.GFlashText;
  80. import com.genome2d.context.GBlendMode;
  81. import com.genome2d.core.GNode;
  82. import com.genome2d.core.GNodeFactory;
  83. import com.genome2d.textures.GTexture;
  84. import com.genome2d.textures.GTextureAlignType;
  85.  
  86. import flash.geom.Rectangle;
  87. import flash.text.TextFieldAutoSize;
  88. import flash.text.TextFormat;
  89.  
  90. class Button extends GComponent {
  91.  
  92.     private var bg: GSprite;
  93.     private var tf: GFlashText;
  94.     private var w: int = 100;
  95.     private var h: int = 25 ;
  96.     private static var textFormat: TextFormat;
  97.     private static var bgTx: GTexture;
  98.  
  99.     public function Button(pNode:GNode) {
  100.         super(pNode);
  101.         if(!textFormat){
  102.             textFormat = new TextFormat('Arial', 22, 0xFF0000 );
  103.             bgTx = GTexture.getTextureById('red');
  104.         }
  105.         init() ;
  106.     }
  107.  
  108.     private function init(): void {
  109.         bg = GNodeFactory.createNodeWithComponent(GSprite) as GSprite ;
  110.         bg.setTexture(bgTx);
  111.         tf = GNodeFactory.createNodeWithComponent(GFlashText) as GFlashText ;
  112.         tf.textFormat = textFormat ;
  113.         tf.transparent = true ;
  114.         tf.autoSize = TextFieldAutoSize.LEFT ;
  115.         tf.multiLine = false ;
  116.         tf.blendMode = GBlendMode.NORMAL;
  117.         tf.align = GTextureAlignType.TOP_LEFT ;
  118.         bg.node.transform.setScale( w/bgTx.width, h/bgTx.height );
  119.         node.addChild(bg.node);
  120.         node.addChild(tf.node);
  121.     }
  122.  
  123.     public function setLabel(pLabel:String):void {
  124.         tf.text = pLabel ;
  125.  
  126.         // you can update the bg width here, or wait for the postRender to avoid a forced invalidatation.
  127. //      tf.invalidate(true);
  128. //      updateButtonWidth();
  129.  
  130.         // Is preferable to do this on the parent node, or anywhere where u store this instances.
  131.         // and toggle node's visibility when u finally render the stuffs, otherwise, it'll look
  132.         // weird as u'll see 1 frame the bg size not-synced.
  133. //      node.transform.visible = false ;
  134.         node.core.onPostRender.addOnce(updateButtonWidth);
  135.     }
  136.  
  137.     private function updateButtonWidth(): void {
  138.         var r:Rectangle = tf.node.getWorldBounds();
  139.         w = r.width ;
  140.         bg.node.transform.scaleX = w/bgTx.width ;
  141. //      node.transform.visible = true ;
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment