<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="initDetector()" layout="absolute"  xmlns:local="*"
    xmlns:controls="com.dougmccune.controls.*" 
    viewSourceURL="srcview/index.html"
    
    width="600" height="600" frameRate="80" >
    
    <mx:Script>
        <![CDATA[
            import com.theflashblog.fp10.SimpleZSorter;
            
            import com.dougmccune.Target;
            import com.dougmccune.face.MarilenaDetector;
            
            [Bindable]
            private var detector:MarilenaDetector;
            
            private function initDetector():void{
                detector = new MarilenaDetector();
                detector.foundCallback = faceFound;
                detector.notFoundCallback = faceNotFound;
                
                detector.startDetecting();    
            }
            
            private function faceNotFound():void {
                error.visible = true;    
            }
            
            private function faceFound():void {
                error.visible =  false;    
                drawHUDOverlay();
                targetComponent.faceFound();
            }
            
            /**
             * Draws the area that was detected by the face tracking component, including a green box
             * around the face. The image that was used for the face detection might not be the full webcam image,
             * since we might have limited the area of the image we're scanning, based on the rpevius face that 
             * was found, or we might be rotating the source image to check for rotated faces.
             */
            private function drawHUDOverlay():void {
                hudOverlay.graphics.clear();
                hudOverlay.graphics.lineStyle(0, 0, 0);
                
                var m:Matrix = new Matrix();
                m.scale(-1, 1);
                m.translate(detector.lastBitmapChecked.width, 0);
                
                hudOverlay.graphics.beginBitmapFill(detector.lastBitmapChecked, m);
                hudOverlay.graphics.drawRect(0,0,detector.lastBitmapChecked.width, detector.lastBitmapChecked.height);
                hudOverlay.graphics.endFill();
                
                hudOverlay.graphics.lineStyle(1, 0x00ff00);
                hudOverlay.graphics.drawRect(detector.lastBitmapChecked.width - (detector.lastSourceFaceRectangle.x + detector.lastSourceFaceRectangle.width), detector.lastSourceFaceRectangle.y, detector.lastSourceFaceRectangle.width, detector.lastSourceFaceRectangle.height);
            }
            
        ]]>
    </mx:Script>
    
    <mx:Style source="/assets/style.css" />
    
    <local:TargetComponent id="targetComponent" detector="{detector}" backgroundColor="#000000"
        width="500" height="500" horizontalCenter="0" verticalCenter="0" />
    
    <mx:UIComponent id="hudOverlay" />
    
    <controls:WebcamDisplay height="80" mirror="true" left="100" />
    
    <mx:VBox styleName="errorBox" visible="false" id="error" right="0">
        <mx:Label text="Can't find a face" />
    </mx:VBox>
            
</mx:Application>