/**
 * Hilo 1.4.1 for commonjs
 * Copyright 2016 alibaba.com
 * Licensed under the MIT License
 */
var Hilo=require("../core/Hilo"),Class=require("../core/Class"),EventMixin=require("../event/EventMixin"),Matrix=require("../geom/Matrix"),util=require("../util/util"),View=function(){function t(t,e,i){for(var r,n,a,o,s=0,h=!1,u=0,l=i.length;u<l;u++){var y=i[u],c=i[(u+1)%l];if(y.y==c.y&&e==y.y&&(y.x>c.x?(r=c.x,n=y.x):(r=y.x,n=c.x),t>=r&&t<=n))h=!0;else if(y.y>c.y?(a=c.y,o=y.y):(a=y.y,o=c.y),!(e<a||e>o)){var x=(e-y.y)*(c.x-y.x)/(c.y-y.y)+y.x;if(x>t?s++:x==t&&(h=!0),y.x>t&&y.y==e){var d=i[(l+u-1)%l];(d.y<e&&c.y>e||d.y>e&&c.y<e)&&s++}}}return h||s%2==1}function e(t,e){var r=i(t,e,{overlap:-(1/0),normal:{x:0,y:0}});return!!r&&i(e,t,r)}function i(t,e,i){for(var r,n,a,o,s,h,u,l,y,c=t.length,x=e.length,d={x:0,y:0},f=0;f<c;f++){r=t[f],n=t[f<c-1?f+1:0],d.x=r.y-n.y,d.y=n.x-r.x,a=Math.sqrt(d.x*d.x+d.y*d.y),d.x/=a,d.y/=a,o=s=t[0].x*d.x+t[0].y*d.y;for(var v=1;v<c;v++)l=t[v].x*d.x+t[v].y*d.y,l>s?s=l:l<o&&(o=l);for(h=u=e[0].x*d.x+e[0].y*d.y,v=1;v<x;v++)l=e[v].x*d.x+e[v].y*d.y,l>u?u=l:l<h&&(h=l);if(o<h?(y=h-s,d.x=-d.x,d.y=-d.y):y=o-u,y>=0)return!1;y>i.overlap&&(i.overlap=y,i.normal.x=d.x,i.normal.y=d.y)}return i}return Class.create({Mixes:EventMixin,constructor:function(t){t=t||{},this.id=this.id||t.id||Hilo.getUid("View"),util.copy(this,t,!0)},tint:16777215,id:null,x:0,y:0,width:0,height:0,alpha:1,rotation:0,visible:!0,pivotX:0,pivotY:0,scaleX:1,scaleY:1,pointerEnabled:!0,background:null,mask:null,align:null,drawable:null,boundsArea:null,parent:null,depth:-1,transform:null,blendMode:"source-over",getStage:function(){for(var t,e=this;t=e.parent;)e=t;return e.canvas?e:null},getScaledWidth:function(){return this.width*this.scaleX},getScaledHeight:function(){return this.height*this.scaleY},addTo:function(t,e){return"number"==typeof e?t.addChildAt(this,e):t.addChild(this),this},removeFromParent:function(){var t=this.parent;return t&&t.removeChild(this),this},getBounds:function(){for(var t,e,i,r,n,a,o,s=this.width,h=this.height,u=this.getConcatenatedMatrix(),l=this.boundsArea||[{x:0,y:0},{x:s,y:0},{x:s,y:h},{x:0,y:h}],y=[],c=0,x=l.length;c<x;c++)t=u.transformPoint(l[c],!0,!0),e=t.x,i=t.y,0==c?(r=n=e,a=o=i):(r>e?r=e:n<e&&(n=e),a>i?a=i:o<i&&(o=i)),y[c]=t;return y.x=r,y.y=a,y.width=n-r,y.height=o-a,y},getConcatenatedMatrix:function(t){for(var e=new Matrix(1,0,0,1,0,0),i=this;i!=t&&i.parent;i=i.parent){var r=1,n=0,a=i.rotation%360,o=i.pivotX,s=i.pivotY,h=i.scaleX,u=i.scaleY,l=i.transform;if(l)e.concat(l);else{if(a){var y=a*Math.PI/180;r=Math.cos(y),n=Math.sin(y)}0!=o&&(e.tx-=o),0!=s&&(e.ty-=s);var c=i.getAlignPosition();e.concat(r*h,n*h,-n*u,r*u,c.x,c.y)}}return e},getAlignPosition:function(){var t=this.parent,e=this.align,i=this.x,r=this.y;if(t&&this.align){if("function"==typeof e)return this.align();var n=this.width,a=this.height,o=t.width,s=t.height;switch(e){case"TL":i=0,r=0;break;case"T":i=o-n>>1,r=0;break;case"TR":i=o-n,r=0;break;case"L":i=0,r=s-a>>1;break;case"C":i=o-n>>1,r=s-a>>1;break;case"R":i=o-n,r=s-a>>1;break;case"BL":i=0,r=s-a;break;case"B":i=o-n>>1,r=s-a;break;case"BR":i=o-n,r=s-a}}return{x:i,y:r}},hitTestPoint:function(e,i,r){var n=this.getBounds(),a=e>=n.x&&e<=n.x+n.width&&i>=n.y&&i<=n.y+n.height;return a&&r&&(a=t(e,i,n)),a},hitTestObject:function(t,i){var r=this.getBounds(),n=t.getBounds(),a=r.x<=n.x+n.width&&n.x<=r.x+r.width&&r.y<=n.y+n.height&&n.y<=r.y+r.height;return a&&i&&(a=e(r,n)),!!a},_render:function(t,e){this.onUpdate&&this.onUpdate(e)===!1||!t.startDraw(this)||(t.transform(this),this.render(t,e),t.endDraw(this))},_fireMouseEvent:function(t){if(t.eventCurrentTarget=this,this.fire(t),"mousemove"==t.type){if(!this.__mouseOver){this.__mouseOver=!0;var e=util.copy({},t);e.type="mouseover",this.fire(e)}}else"mouseout"==t.type&&(this.__mouseOver=!1);var i=this.parent;t._stopped||t._stopPropagationed||!i||("mouseout"==t.type||"touchout"==t.type?i.hitTestPoint(t.stageX,t.stageY,!0)||i._fireMouseEvent(t):i._fireMouseEvent(t))},onUpdate:null,render:function(t,e){t.draw(this)},toString:function(){return Hilo.viewToString(this)}})}();module.exports=View;