let BaseNode=require("../Utils/BaseNode").BaseNode,APJS=require("../../../amazingpro");class CGScreenImageTap extends BaseNode{constructor(){super(),this._min=new APJS.Vector3f,this._max=new APJS.Vector3f,this._fixedTapPos=new APJS.Vector2f,this._tapPos=new APJS.Vector2f,this._clamp01=e=>Math.min(Math.max(e,0),1),this._imgTrans=null,this._img=null,this._isScreenTapped=!1,this.outputs[1]=!1,this._imgCenter=new APJS.Vector3f,this._xVec=new APJS.Vector3f,this._yVec=new APJS.Vector3f,this._planeNormal=new APJS.Vector3f,this._camRay=null}onInit(e){(this.sys=e)&&e.isSystem&&e.listenedCompTypeSet&&e.listenedCompTypeSet.add("Camera")}_getEffectNodeGUID(e){let t=e,i=t.guid;for(;null!=t;)i=t.guid,t=t.parent;return i}_checkIsValidImageCameraPair(e,t){var i=this._getEffectNodeGUID(e.getSceneObject()),s=this._getEffectNodeGUID(t.getSceneObject());return!!i.equals(s)&&!(!e.getSceneObject().getComponent("ScreenTransform").isValidScreenHierarchy()||t.cameraType!==APJS.CameraType.Ortho)}_runImageTapNode(e){var t=this.inputs[0]();if(null!=t&&t.getSceneObject().isEnabledInHierarchy()){null!=this._imgTrans&&this._img===t||(this._imgTrans=t.getSceneObject().getComponent("ScreenTransform"),this._img=t);var i,s=t.getSceneObject().layer;for(i of e.isSystem?e.listenedComponents:e.APJScene.getAllSceneObjects().flatMap(e=>e.getComponents()))if(i instanceof APJS.Camera&&i.isLayerVisible(s)&&i.getSceneObject().isEnabledInHierarchy()&&this._checkIsValidImageCameraPair(this._img,i)){var n=this._setupData(this._imgTrans,i);if(n)if(this._isImgTapped(i))return this.outputs[1]=!0,void(null!=this.nexts[0]&&this.nexts[0]())}}}_isImgTapped(e){var t=this._camRay.origin,i=this._planeNormal,s=this._imgCenter,n=this._camRay.direction,i=(i.x*(s.x-t.x)+i.y*(s.y-t.y)+i.z*(s.z-t.z))/(i.x*n.x+i.y*n.y+i.z*n.z),n=n.multiply(i).add(t),t=this._vectorFromToPoint(s,n),s=t.dot(this._xVec.clone().normalize())/this._xVec.magnitude(),n=t.dot(this._yVec.clone().normalize())/this._yVec.magnitude(),t=!(1<Math.abs(s)||1<Math.abs(n)),s=this._inFrustum(e,i);return t&&s}_setupData(e,t){var i,s,n=t.viewport,r=new APJS.Rect(this._clamp01(n.x,0,1),this._clamp01(n.y,0,1),n.x<0?n.x+n.width:1<n.x+n.width?1-n.x:n.width,n.y<0?n.y+n.height:1<n.y+n.height?1-n.y:n.height);return!!this._isInRect(this._tapPos,n)&&(n=this._img.getContentWorldCorners(),this._fixedTapPos.set(this._inverseLerp(this._tapPos.x,r.x,r.x+r.width),this._inverseLerp(this._tapPos.y,r.y,r.y+r.height)),r=n[0],i=n[1],s=n[2],n=n[3],this._camRay=t.viewportPointToRay(this._fixedTapPos),this._imgCenter=this._getCenter(r,n),t=this._getCenter(r,i),n=this._getCenter(r,s),this._xVec=this._vectorFromToPoint(t,this._imgCenter),this._yVec=this._vectorFromToPoint(n,this._imgCenter),this._planeNormal=this._xVec.clone().cross(this._yVec),0!==this._planeNormal.dot(this._camRay.direction))}_getCenter(e,t){return new APJS.Vector3f((e.x+t.x)/2,(e.y+t.y)/2,(e.z+t.z)/2)}_vectorFromToPoint(e,t){return new APJS.Vector3f(t.x-e.x,t.y-e.y,t.z-e.z)}_isInAABB(e,t,i){return e.x>t.x&&e.x<i.x&&e.y>t.y&&e.y<i.y}_mulv3(e,t){return new APJS.Vector3f(e.x*t.x,e.y*t.y,e.z*t.z)}_inFrustum(e,t){return t>=e.near&&t<e.far}_isInRect(e,t){return e.x>t.x&&e.x<t.x+t.width&&e.y>t.y&&e.y<t.y+t.height}_inverseLerp(e,t,i){return t===i?0:this._clamp01((e-t)/(i-t))}onUpdate(e,t){this.outputs[1]=!1,this._isScreenTapped&&(this._runImageTapNode(e),this._isScreenTapped=!1)}onEvent(e,t){null!=this.inputs[0]&&t.type===APJS.EventType.Touch&&(t=t.args[0]).phase===APJS.TouchPhase.Began&&(this._tapPos.set(t.position.x,1-t.position.y),this._isScreenTapped=!0)}}exports.CGScreenImageTap=CGScreenImageTap;