rodrigolopezpeker

Genome2D > GScale9Image (w/mouseEvents)

Dec 29th, 2013
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Code by rodrigolopezpeker (aka 7interactive™) on 12/29/13 3:35 PM.
  3.  */
  4. package rgl {
  5. import com.genome2d.components.GCamera;
  6. import com.genome2d.components.GComponent;
  7. import com.genome2d.components.GTransform;
  8. import com.genome2d.context.GContext;
  9. import com.genome2d.core.GNode;
  10. import com.genome2d.g2d;
  11. import com.genome2d.textures.GTexture;
  12.  
  13. import flash.events.MouseEvent;
  14. import flash.geom.Matrix3D;
  15. import flash.geom.Rectangle;
  16. import flash.geom.Vector3D;
  17.  
  18. use namespace g2d ;
  19.  
  20. public class GScale9Image extends GComponent {
  21.  
  22.     private var _width:Number;
  23.     private var _height:Number;
  24.     private var texture:GScale9Texture;
  25.     private var _invalidate:Boolean;
  26.     private var _slices:Vector.<GSlice> ;
  27.     private var _rotation:Number=0;
  28.  
  29.     public var mousePixelEnabled:Boolean=true;
  30.  
  31.     // pivot to scale/rotate percentage 0-1.
  32.     public var pivotX:Number = 0 ;
  33.     public var pivotY:Number = 0 ;
  34.  
  35.     private var rotationCos:Number =0 ;
  36.     private var rotationSin:Number =0;
  37.  
  38.     public function GScale9Image(pNode:GNode) {
  39.         super(pNode);
  40.         initLayout();
  41.     }
  42.  
  43.     private function initLayout():void {
  44.         _slices = new Vector.<GSlice>();
  45.         for (var i:int = 0; i < 9; i++) _slices.push(new GSlice());
  46.     }
  47.  
  48.     override public function render(p_context:GContext, p_camera:GCamera, p_maskRect:Rectangle):void {
  49.         super.render(p_context, p_camera, p_maskRect);
  50.         // render top left.
  51.         if( !texture || _width<=0 || _height<=0 ) return ;
  52.         var t:GTransform = cNode.cTransform ;
  53.         if( _rotation != t.nWorldRotation ) {
  54.             _rotation = t.nWorldRotation ;
  55.             _invalidate = true ;
  56.         }
  57.         if( _invalidate ){
  58.             // TODO: force mouse invalidation ?
  59.             _invalidate= false ;
  60.             rotationCos = Math.cos(_rotation);
  61.             rotationSin = Math.sin(_rotation);
  62.             drawLayout() ;
  63.         }
  64.         for (var i:int = 0; i < 9; i++) {
  65.             var slice:GSlice = _slices[i];
  66.             p_context.draw( slice.texture, slice.x + t.nWorldX, slice.y+ t.nWorldY, slice.scaleX* t.nWorldScaleX, slice.scaleY* t.nWorldScaleY, _rotation, t.nWorldRed, t.nWorldGreen, t.nWorldBlue, t.nWorldAlpha, 1, p_maskRect ) ;
  67.         }
  68.     }
  69.  
  70.     override public function processMouseEvent(p_captured:Boolean, p_event:MouseEvent, p_position:Vector3D):Boolean {
  71.         if (p_captured && p_event.type == MouseEvent.MOUSE_UP) cNode.cMouseDown = null;
  72.         if (p_captured || !texture ) {
  73.             if (cNode.cMouseOver == cNode)
  74.                 cNode.handleMouseEvent(cNode, MouseEvent.MOUSE_OUT, Number.NaN, Number.NaN, p_event.buttonDown, p_event.ctrlKey);
  75.             return false;
  76.         }
  77.         var transformMatrix:Matrix3D = cNode.cTransform.getTransformedWorldTransformMatrix(_width, _height, _rotation, true);
  78.         var localMousePos:Vector3D = transformMatrix.transformVector(p_position);
  79.         var minX:Number = -pivotX;
  80.         var minY:Number = -pivotY;
  81.         var maxX:Number = 1-pivotX;
  82.         var maxY:Number = 1-pivotY;
  83.         if( localMousePos.x >= minX && localMousePos.x <= maxX && localMousePos.y >= minY && localMousePos.y <= maxY ){
  84.             if( mousePixelEnabled ){
  85.                 var mouseX: Number = localMousePos.x * _width ;
  86.                 var mouseY: Number = localMousePos.y * _height ;
  87.                 var mouseSlice:GSlice = getScale9Slice(mouseX, mouseY);
  88.                 if( !mouseSlice ) return true ;
  89.                 var px:Number = (mouseX-mouseSlice.ox+pivotX*_width)/mouseSlice.w ;
  90.                 var py:Number = (mouseY-mouseSlice.oy+pivotY*_height)/mouseSlice.h ;
  91.                 if( mouseSlice.texture.getAlphaAtUV(px,py)==0){
  92.                     if (cNode.cMouseOver == cNode) {
  93.                         cNode.handleMouseEvent(cNode, MouseEvent.MOUSE_OUT, localMousePos.x, localMousePos.y, p_event.buttonDown, p_event.ctrlKey);
  94.                     }
  95.                     return false;
  96.                 }
  97.             }
  98.             cNode.handleMouseEvent(cNode, p_event.type, localMousePos.x, localMousePos.y, p_event.buttonDown, p_event.ctrlKey );
  99.             if( cNode.cMouseOver != cNode ){
  100.                 cNode.handleMouseEvent(cNode, MouseEvent.MOUSE_OVER, localMousePos.x, localMousePos.y, p_event.buttonDown, p_event.ctrlKey );
  101.             }
  102.             return true ;
  103.         } else {
  104.             if( cNode.cMouseOver == cNode )
  105.                 cNode.handleMouseEvent(cNode, MouseEvent.MOUSE_OUT, localMousePos.x, localMousePos.y, p_event.buttonDown, p_event.ctrlKey );
  106.         }
  107.         return false ;
  108.     }
  109.  
  110.     private function getScale9Slice(x:Number, y:Number):GSlice {
  111.         x += pivotX * _width ;
  112.         y += pivotY * _height ;
  113.         for (var i:int = 0; i < 9; i++) {
  114.             if( x >= _slices[i].ox && y >= _slices[i].oy &&
  115.                 x <= _slices[i].ox +_slices[i].w && y <= _slices[i].oy +_slices[i].h )
  116.                 return _slices[i];
  117.         }
  118.         return null ;
  119.     }
  120.  
  121.     private function drawLayout():void {
  122. //      if(_width<0||_height<0)return ;
  123.  
  124.         var centerX:Number = _width * pivotX;
  125.         var centerY:Number = _height * pivotY;
  126.  
  127.         var leftW:Number = _slices[0].ow ;
  128.         var centerW:Number = _width - _slices[0].ow - _slices[2].ow ;
  129.         var centerH:Number = _height - _slices[0].oh - _slices[2].oh ;
  130.         var bottomY:Number = _height - _slices[6].h ;
  131.  
  132.         _slices[3].h = _slices[4].h = _slices[5].h = centerH ;
  133.         _slices[3].scaleY = _slices[4].scaleY = _slices[5].scaleY = _slices[3].h / _slices[3].oh ;
  134.  
  135.         _slices[1].w = _slices[4].w = _slices[7].w = centerW ;
  136.         _slices[1].scaleX = _slices[4].scaleX = _slices[7].scaleX = _slices[1].w / _slices[1].ow ;
  137.  
  138.         _slices[0].ox = _slices[3].ox = _slices[6].ox = 0 ;
  139.         _slices[1].ox = _slices[4].ox = _slices[7].ox = leftW ;
  140.         _slices[2].ox = _slices[5].ox = _slices[8].ox = leftW + centerW ;
  141.  
  142.         _slices[0].oy = _slices[1].oy = _slices[2].oy = 0 ;
  143.         _slices[3].oy = _slices[4].oy = _slices[5].oy = _slices[0].h ;
  144.         _slices[6].oy = _slices[7].oy = _slices[8].oy = bottomY;
  145.         var i:int = 0 ;
  146.         if(_rotation==0){
  147.             for (i=0; i < 9; i++) {
  148.                 _slices[i].x = _slices[i].ox - centerX ;
  149.                 _slices[i].y = _slices[i].oy - centerY ;
  150.             }
  151.         } else {
  152.             for (i=0; i < 9; i++) {
  153.                 var dx:Number = _slices[i].ox - centerX ;
  154.                 var dy:Number = _slices[i].oy - centerY ;
  155.                 _slices[i].x = dx * rotationCos - dy * rotationSin ;
  156.                 _slices[i].y = dy * rotationCos + dx * rotationSin;
  157.             }
  158.         }
  159.     }
  160.  
  161.     public function setTexture(pTexture:GScale9Texture):void {
  162.         texture = pTexture;
  163.         _width = texture.texture.width;
  164.         _height = texture.texture.height;
  165.         setSlice(0, texture.topLeft);
  166.         setSlice(1, texture.topCenter);
  167.         setSlice(2, texture.topRight);
  168.         setSlice(3, texture.middleLeft);
  169.         setSlice(4, texture.middleCenter);
  170.         setSlice(5, texture.middleRight);
  171.         setSlice(6, texture.bottomLeft);
  172.         setSlice(7, texture.bottomCenter);
  173.         setSlice(8, texture.bottomRight);
  174.         _invalidate = true;
  175.     }
  176.  
  177.     private function setSlice(pIndex:int, pTexture:GTexture):void {
  178.         _slices[pIndex].texture = pTexture ;
  179.         _slices[pIndex].w = _slices[pIndex].ow = pTexture.width;
  180.         _slices[pIndex].h = _slices[pIndex].oh = pTexture.height;
  181.     }
  182.  
  183.     public function get height():Number {
  184.         return _height;
  185.     }
  186.  
  187.     public function set height(value:Number):void {
  188.         if(_height==value)return;
  189.         _height = value;
  190.         _invalidate = true ;
  191.     }
  192.  
  193.     public function get width():Number {
  194.         return _width;
  195.     }
  196.  
  197.     public function set width(value:Number):void {
  198.         if(_width==value)return;
  199.         _width = value;
  200.         _invalidate = true ;
  201.     }
  202.  
  203. }
  204.  
  205.  
  206. }
  207.  
  208. import com.genome2d.textures.GTexture;
  209.  
  210. class GSlice {
  211.  
  212.     // without rotation
  213.     public var ox:Number=0;
  214.     public var oy:Number=0;
  215.  
  216.     // w/rotation
  217.     public var x:Number=0;
  218.     public var y:Number=0;
  219.     public var scaleX:Number=1;
  220.     public var scaleY:Number=1;
  221.     public var rotation:Number=0;
  222.     public var texture:GTexture;
  223.     public var w:Number=0;
  224.     public var h:Number=0;
  225.     public var ow:Number;
  226.     public var oh:Number;
  227.  
  228.     public function GSlice():void {
  229.     }
  230.  
  231.     public function toString():String{
  232.         return 'GSlice [ x:' + x + ',y:' + y + ',texture:' + texture +
  233.                 ',scaleX:'+scaleX+',scaleY:' + scaleY + ', rotation:' + rotation ;
  234.     }
  235. }
Advertisement
Add Comment
Please, Sign In to add comment