<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    backgroundColor="0xFFFFFF"
    creationComplete="drawTiles()"
    horizontalAlign="center" 
    layout="vertical" viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import com.renaun.truchet.TruchetRandomArcs;
            import com.renaun.truchet.TruchetCircularArcs;
            import com.renaun.truchet.TruchetTriangle;
            public function drawTiles():void
            {
                pattern.graphics.clear();
                pattern.width = stepper.value;
                pattern.height = stepper.value;
                var color:uint = uint(Math.random() * 0xeeeeee);
                
                if (type.selectedItem == "Triangle")
                {
                    var truchetTriangle:TruchetTriangle = new TruchetTriangle();
                    truchetTriangle.tileColor = color;
                    truchetTriangle.drawPattern(pattern.graphics, pattern.width, pattern.height, columns.value);
                }
                if (type.selectedItem == "Circular Arc")
                {
                    var truchetArcs:TruchetCircularArcs = new TruchetCircularArcs();
                    truchetArcs.tileColor = color;
                    truchetArcs.drawPattern(pattern.graphics, pattern.width, pattern.height, columns.value);
                }
                if (type.selectedItem == "Random Arc")
                {
                    var truchetRArcs:TruchetRandomArcs = new TruchetRandomArcs();
                    truchetRArcs.tileColor = color;
                    truchetRArcs.drawPattern(pattern.graphics, pattern.width, pattern.height, columns.value);
                }
            }
            
            private function surpriseMe():void
            {
                stepper.value = int(Math.random() * (stepper.maximum - stepper.minimum)) + stepper.minimum;
                columns.value = int(Math.random() * (columns.maximum - columns.minimum)) + columns.minimum;
                type.selectedItem = type.dataProvider[int(Math.random() * 3)];
                drawTiles();
            }
        ]]>
    </mx:Script>
    <mx:HBox>
        <mx:Button label="New Pattern" click="drawTiles()" />
        <mx:Label text="Type" />
        <mx:ComboBox id="type">
            <mx:dataProvider>
                <mx:Array>
                    <mx:String>Random Arc</mx:String>
                    <mx:String>Circular Arc</mx:String>
                    <mx:String>Triangle</mx:String>
                </mx:Array>
            </mx:dataProvider>
        </mx:ComboBox>
        <mx:Label text="Size" />
        <mx:NumericStepper id="stepper"
            value="200" stepSize="50" minimum="300" maximum="{width}" />    
        <mx:Label text="Columns" />
        <mx:NumericStepper id="columns"
            value="5" stepSize="5" minimum="5" maximum="60" />
        <mx:Button label="Surprise Me!" click="surpriseMe()" />
    </mx:HBox>
    
    <mx:Canvas id="pattern" width="200" height="200" top="30"
        clipContent="false" />
</mx:Application>