rodrigolopezpeker

Genome2D > GSlice (Scale3/9)

Dec 7th, 2013
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Created with IntelliJ IDEA.
  3.  * User: rodrigo
  4.  * Date: 12/6/13
  5.  * Time: 8:23 PM
  6.  */
  7. package {
  8.     import com.genome2d.components.GComponent;
  9.     import com.genome2d.components.renderables.GSimpleShape;
  10.     import com.genome2d.core.GNode;
  11.     import com.genome2d.core.GNodeFactory;
  12.     import com.genome2d.g2d;
  13.     import com.genome2d.textures.GTexture;
  14.  
  15.     import flash.geom.Rectangle;
  16.  
  17.     use namespace g2d ;
  18.  
  19.     public class GSlice extends GComponent {
  20.  
  21.         private var _vertices:Vector.<Number>;
  22.         private var _uvs:Vector.<Number>;
  23.         private var _shape:GSimpleShape;
  24.  
  25.         private var _texture:GTexture ;
  26.         private var _textureW:int ;
  27.         private var _textureH:int ;
  28.  
  29.         private var _width:int ;
  30.         private var _height:int ;
  31.  
  32.         public static const SLICE_TYPE_3:int = 0 ;
  33.         public static const SLICE_TYPE_9:int = 1 ;
  34.         private var _sliceType:int ;
  35.  
  36.         public static const SLICE_3_HORIZONTAL: int = 0 ;
  37.         public static const SLICE_3_VERTICAL: int = 1 ;
  38.         private var _slice3Direction: int = 1 ;
  39.  
  40.         private var _sliceRect: Rectangle;
  41.         private var _isInvalidated: Boolean;
  42.  
  43.         /**
  44.          * Constructor.
  45.          * @param pNode
  46.          */
  47.         public function GSlice(pNode:GNode) {
  48.             super(pNode);
  49.             _sliceRect = new Rectangle();
  50.             _vertices = new Vector.<Number>() ;
  51.             _uvs = new Vector.<Number>() ;
  52.             _shape = GNodeFactory.createNodeWithComponent(GSimpleShape) as GSimpleShape ;
  53.             cNode.addChild(_shape.node);
  54.         }
  55.  
  56.         public function setTexture(pTexture:GTexture):void {
  57.             _texture = pTexture;
  58.             if(!_texture) return ;
  59.             _textureW = _texture.width ;
  60.             _textureH = _texture.height ;
  61.             _height = _textureH ;
  62.             _width = _textureW ;
  63.             _shape.cTexture = _texture ;
  64.         }
  65.  
  66.         override public function update(p_deltaTime: Number): void {
  67.             if( _isInvalidated ){
  68.                 _isInvalidated = false ;
  69.                 draw() ;
  70. //              setSize( _width, _height );
  71.             }
  72.         }
  73.  
  74.         public function get textureId():String{ return _texture ? _texture.id : null ;}
  75.         public function set textureId(pId:String):void{
  76.             setTexture(GTexture.getTextureById(pId)) ;
  77.         }
  78.  
  79.  
  80.         public function setSlice3(pDirection: int, pPaddingA: int, pPaddingB:int = 0): void {
  81.             _sliceType = SLICE_TYPE_3 ;
  82.             _sliceRect.setEmpty();
  83.             _sliceRect.x = pPaddingA ;
  84.             _slice3Direction = pDirection ;
  85.             _sliceRect.y = pPaddingB==0 ? pPaddingA : pPaddingB<0 ? -pPaddingB : pPaddingB ;
  86.             buildMesh3() ;
  87.         }
  88.  
  89.         private function buildMesh3(): void {
  90.             _vertices.length = 0 ;
  91.             _uvs.length = 0 ;
  92.             var isHorizontal:Boolean = _slice3Direction==SLICE_3_HORIZONTAL ;
  93.             var pad1:int = _sliceRect.x ;
  94.             var pad2:int = isHorizontal ? _width - _sliceRect.y : _height - _sliceRect.y ;
  95.             if(isHorizontal){
  96.                 defPoly( 0,0, pad1,_height );
  97.                 defPoly( pad1, 0, pad2, _height);
  98.                 defPoly( pad2, 0, _width, _height );
  99.             } else {
  100.                 defPoly( 0, 0, _width, pad1 );
  101.                 defPoly( 0, pad1, _width, pad2 );
  102.                 defPoly( 0, pad2, _width, _height );
  103.             }
  104.             _shape.init(_vertices, _uvs);
  105.         }
  106.  
  107.         private function buildMesh9(): void {
  108.             _vertices.length = 0 ;
  109.             _uvs.length = 0 ;
  110.  
  111.             // define the areas.
  112.             var pad:Rectangle = _sliceRect ;
  113.             var py:int = 0 ;
  114.             defPoly( 0, py, pad.x, pad.y );
  115.             defPoly( pad.x, py, _width - pad.width, pad.y );
  116.             defPoly( _width - pad.width, py, _width, pad.y );
  117.  
  118.             py = pad.y ;
  119.             defPoly( 0, py, pad.x, _height - pad.height );
  120.             defPoly( pad.x, py, _width - pad.width, _height - pad.height );
  121.             defPoly( _width - pad.width, py, _width,_height - pad.height );
  122.  
  123.             py = _height-pad.height ;
  124.             defPoly( 0, py, pad.x, _height );
  125.             defPoly( pad.x, py, _width - pad.width, _height );
  126.             defPoly( _width - pad.width, py, _width,_height );
  127.  
  128.             _shape.init(_vertices, _uvs);
  129.         }
  130.  
  131.         [Inline]
  132.         private final function draw(): void {
  133.             if(_sliceType==SLICE_TYPE_9){
  134.                 var pad:Rectangle = _sliceRect ;
  135.                 var pw:int = _width - pad.width ;
  136.                 var ph:int = _height - pad.height ;
  137.                 resizePoly( 0, 0, 0, pad.x, pad.y );
  138.                 resizePoly( 1, pad.x, 0, pw, pad.y );
  139.                 resizePoly( 2, pw, 0, _width, pad.y );
  140.  
  141.                 resizePoly( 3, 0, pad.y, pad.x, ph );
  142.                 resizePoly( 4, pad.x, pad.y, pw, ph );
  143.                 resizePoly( 5, pw, pad.y, _width, ph );
  144.  
  145.                 resizePoly( 6, 0, ph, pad.x, _height );
  146.                 resizePoly( 7, pad.x, ph, pw, _height );
  147.                 resizePoly( 8, pw, ph, _width, _height );
  148.             } else {
  149.                 var isHorizontal:Boolean = _slice3Direction==SLICE_3_HORIZONTAL ;
  150.                 var pad1:int = _sliceRect.x ;
  151.                 var pad2:int = isHorizontal ? _width - _sliceRect.y : _height - _sliceRect.y ;
  152.                 if(isHorizontal){
  153.                     resizePoly(0, 0,0, pad1, _height);
  154.                     resizePoly(1, pad1, 0, pad2, _height);
  155.                     resizePoly(2, pad2, 0, _width, _height);
  156.                 } else {
  157.                     resizePoly(0, 0, 0, _width, pad1 );
  158.                     resizePoly(1, 0, pad1, _width, pad2 );
  159.                     resizePoly(2, 0, pad2, _width, _height );
  160.                 }
  161.             }
  162.         }
  163.  
  164.         public function setSize(pW:int, pH:int):void {
  165.             _width = pW ;
  166.             _height = pH ;
  167.             _isInvalidated = true ;
  168.         }
  169.  
  170.         [Inline]
  171.         private final function resizePoly(pIndex: int, x: int, y: int, w: int, h: int): void {
  172.             var ci:uint = pIndex * 12 ;
  173.             var values:Array= [ x,y, w,y, x,h, w,y, w,h, x,h ] ;
  174.             for (var i:uint = 0; i < 12; i++, ci++) _vertices[ci] = values[i];
  175.         }
  176.  
  177.         private function defPoly(x: int, y: int, w: int, h: int): void {
  178.             var ux:Number = x / _textureW ;
  179.             var uy:Number = y / _textureH ;
  180.             var uw:Number = w / _textureW ;
  181.             var uh:Number = h / _textureH ;
  182.  
  183.             _vertices.push( x,y, w,y, x,h );
  184.             _vertices.push( w,y, w,h, x,h );
  185.             _uvs.push(ux,uy, uw,uy, ux,uh );
  186.             _uvs.push(uw,uy, uw,uh, ux,uh );
  187.         }
  188.  
  189.         public function get sliceRect(): Rectangle {return _sliceRect;}
  190.         public function set sliceRect(value: Rectangle): void {
  191.             _sliceRect = value;
  192.         }
  193.  
  194.         public function get width(): int {return _width;}
  195.         public function set width(value: int): void {
  196.             if(value==_width)return ;
  197.             _width = value;
  198.             _isInvalidated = true ;
  199.         }
  200.  
  201.         public function get height(): int {return _height;}
  202.         public function set height(value: int): void {
  203.             if(value==_height)return ;
  204.             _height = value;
  205.             _isInvalidated = true ;
  206.         }
  207.  
  208.         public function setSlice9(pRect: Rectangle, pMargin: int = 0): void {
  209.             if(pRect==null) _sliceRect.setTo(pMargin,pMargin,pMargin,pMargin);
  210.             else _sliceRect.copyFrom(pRect);
  211.             _sliceType = SLICE_TYPE_9 ;
  212.             buildMesh9() ;
  213.         }
  214.  
  215.     }
  216. }
Advertisement
Add Comment
Please, Sign In to add comment