<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx             ="http://www.adobe.com/2006/mxml"
                layout                 ="absolute" 
                applicationComplete ="initImage()" 
                backgroundColor     ="0x444444" 
                name                 ="CircularDisks" 
                viewSourceURL         ="srcview/index.html" >
    
    <!-- by Petri Leskinen, Espoo, Finland, April 2011  --> 
    
    <mx:Script>
        <![CDATA[
            
            import filter.CircularDisksFilter;
            
            import flash.display.Bitmap;
            import flash.display.BitmapData;
            import flash.display.Sprite;
            import flash.events.Event;
            
            import mx.controls.HSlider;
            import mx.controls.Label;
            import mx.controls.Spacer;
            import mx.controls.TextArea;
            import mx.controls.sliderClasses.Slider;
            import mx.core.UIComponent;
            import mx.events.SliderEvent;
            
            import net.*;
            
            
            /**     the shader class  **/ 
            [Bindable]protected var tiling:CircularDisksFilter = new CircularDisksFilter(null,null); 
            
            /**    coordinates for tiling base point */
            [Bindable]private var baseX:Number=0; 
            
            [Bindable]private var baseY:Number=0; 
            
            
            /** Embedded images */
            [Embed( '../assets/chessboard.jpg')]
            protected var Picture1:Class;
            
            [Embed( '../assets/yellowrose.png')]
            protected var Picture3:Class;
            
            [Embed( '../assets/bouquet.jpg')]
            protected var Picture2:Class;
            
            [Embed( '../assets/mandelbrot.png')]
            protected var Picture4:Class;
            
            [Embed('../assets/sinebrychoff.jpg')]
            protected var Picture5:Class;
            
            [Embed('../assets/sinebrychoff2.jpg')]
            protected var Picture6:Class;
            
            [Embed('../assets/dice.png')]
            protected var Picture7:Class;
            
            
            /**    
             *      initializes the application and
             *     change to the image selected in ComboBox 
             * 
             **/
            protected function initImage():void {
                var c:Class;
                switch (imageCombo.selectedLabel) { 
                    case "Sinebrychoff":
                        c =Picture5;
                        break;
                    
                    case "Sinebrychoff 2":
                        c =Picture6;
                        break;
                    
                    case "Yellow Rose":
                        c =Picture3;
                        break;
                    
                    case "Chessboard": 
                        c =Picture1;
                        break;
                    
                    case "Bouquet":
                        c =Picture2;
                        break;
                    
                    case "Mandelbrot": 
                        c =Picture4;
                        break;
                
                    case "Dice":     
                        c =Picture7;
                        break;        
                    
                    case "Solid Grey": 
                        initSource(new BitmapData(500,350, false, 0xA0A0A0));
                        return;
                        
                    case "... Browse":
                        uploadImage();
                        return;
                }
                initSource(Bitmap(new c).bitmapData);
            }
            
            
            //    leave some empty area as image borders
            private var padding:int=25;
            
            
            /**    change the source image */
            protected function initSource(source:BitmapData):void { 
                var bmd:BitmapData = new BitmapData(source.width+2*padding,source.height+2*padding, true,0); 
                bmd.draw(source, new Matrix(1,0,0,1,padding,padding));
                
                //    set filter input and output 
                tiling.input    = bmd; 
                tiling.output    = new BitmapData(bmd.width, bmd.height, true, 0); 
                
                //    update image on stage
                for (var i:int=UIRef.numChildren-1; i>-1; i--) {
                    UIRef.removeChildAt(i); 
                }
                UIRef.addChild(new Bitmap(tiling.output));
                
                //    update preview thumbnail on controls
                sourcePreview.source = new Bitmap(source);
            }
            
            
            /**    change pattern's base point on mouse click */
            protected function clicked(e:MouseEvent):void {
                if (UIRef.buttonMode) return;
                baseX= e.localX;
                baseY= e.localY;
                tiling.center = {x:baseX, y:baseY };
            }
            
            
            /**    upload an image from local computer */
            private function uploadImage():void {
                //    initSource as parameter is the function to call when upload is ready
                new ImageFormatImporter(initSource);
                imageCombo.selectedIndex=-1;
            }
            
            
            /**    save .png to local computer */
            private function savePNG():void {
                new PngFormatExporter(tiling.output, this.name+".png");
            } 
            
            /**    save .jpg to local computer */ 
            private function saveJPG():void {
                new JpgFormatExporter(tiling.output, this.name+".jpg");
            } 
            
        ]]> 
        
    </mx:Script> 
    
    <mx:HBox x="30" y="30"> 
        <mx:Panel label="Disks"> 
            
            <mx:VBox id="controls" x="20" y="40" horizontalAlign="center" verticalGap="5" 
                     width="170" height="465" backgroundColor="0x808080">
                
                <mx:Spacer height="20" /> 
                
                <mx:ComboBox 
                    id="imageCombo" width="150" 
                    change="initImage()" rowCount="10" 
                    selectedIndex="0"
                    prompt="Select an Image"> 
                    <mx:ArrayCollection>
                        <mx:Object label="Sinebrychoff" />
                        <mx:Object label="Sinebrychoff 2" />
                        <mx:Object label="Solid Grey" />
                        <mx:Object label="Yellow Rose" />
                        <mx:Object label="Chessboard" />
                        <mx:Object label="Mandelbrot" />
                        <mx:Object label="Bouquet" />
                        <mx:Object label="Dice" />
                        <mx:Object label="... Browse" />
                    </mx:ArrayCollection>
                </mx:ComboBox> 
                
                <mx:Image id="sourcePreview" width="120" height="80"
                          click="tiling.output.fillRect(tiling.output.rect,0);tiling.output.draw(sourcePreview.source as Bitmap)" 
                          />
                
                <mx:Spacer height="20" /> 
                
                <!-- 
                <mx:Button label="Upload an Image" width="150" click="uploadImage();" 
                    />
                <mx:HSlider value="@{this.baseX}" minimum="-100" maximum="800"
                            change="tiling.baseX=event.value;" liveDragging="true" 
                            labels="['','Base Point','']" labelOffset="0" width="150"
                    />
                <mx:HSlider value="@{this.baseY}" minimum="-100" maximum="600" width="150"
                            change="tiling.baseY=event.value;" liveDragging="true" 
                    />
                --> 
                
                <mx:HSlider minimum="2" value="@{tiling.size}" maximum="100"
                            change="tiling.size=event.value;" 
                            liveDragging="true" width="150" snapInterval="1"
                            labels="['','Pattern Size','']" labelOffset="0"    
                            /> 
                
                <mx:HSlider minimum="0.1" value="@{tiling.radius}" maximum="0.64"
                            change="tiling.radius=event.value;" liveDragging="true"
                            labels="['','Radius','']" labelOffset="0" width="150"
                            /> 
                 
                <mx:Spacer height="20" /> 
                 
                <mx:HBox>
                    <mx:Button label="Save as .png" width="96" click="savePNG();" />
                    <mx:Button label=".jpg" width="45" click="saveJPG()" />
                </mx:HBox>
                 
            </mx:VBox>
        </mx:Panel>
        
        <mx:Label id="laabeli" />
        <mx:Spacer width="25" />
        <!--    UIRef: the container for image -->
        <mx:UIComponent id="UIRef" buttonMode="true" click="UIRef.buttonMode=!UIRef.buttonMode;this.clicked(event)" mouseMove="clicked(event)" />
    </mx:HBox>
    
</mx:Application>