<?xml version="1.0" encoding="utf-8"?>
<mx:Application 
    layout="absolute"
    xmlns:mx="http://www.adobe.com/2006/mxml"
    creationComplete="init()"
    xmlns:graphics="org.flexWorld.physics.graphics.*"
    width="600" height="450"
     viewSourceURL="srcview/index.html">
    <mx:Script>
        <![CDATA[
            import org.flexWorld.physics.graphics.bodies.PhysicalBody;
            import org.flexWorld.physics.graphics.bodies.shapes.PhysicalCircle;
            import org.flexWorld.physics.graphics.bodies.shapes.PhysicalBox;
            import Box2D.Common.Math.b2Vec2;
            import Box2D.Collision.b2AABB;

            protected function init():void
            {
                //It seems like the vectors are off if we call reset immediately
                //on creationComplete, so call it one phase later
                callLater(reset);
            }

            protected function startWorld():void
            {
                startButton.enabled = false;
                stopButton.enabled = true;
                world.activate();
            }

            protected function stopWorld():void
            {
                startButton.enabled = true;
                stopButton.enabled = false;
                world.deactivate();
            }

            protected function reset():void
            {
                startButton.enabled = true;
                stopButton.enabled = false;
                world.destroyWorld();
                world.drawDebug = debugCheckbox.selected;
                createWorld();
                createBodies();
            }

            protected function createWorld():void
            {
                var bounds:b2AABB = new b2AABB();
                bounds.upperBound.Set(world.toMeters(world.width), world.toMeters(world.height));
                bounds.lowerBound.Set(-world.toMeters(world.width), -world.toMeters(world.height));
                world.createWorld(bounds, new b2Vec2(0, 9.81));
            }

            protected function createBodies():void
            {
                var i:int = 0, n:int;
                n = numRectanglesStepper.value;
                for(i = 0; i < n; i++)
                {
                    setupBody(new PhysicalBox());
                }
                n = numCirclesStepper.value;
                for(i = 0; i < n; i++)
                {
                    setupBody(new PhysicalCircle());
                }
            }

            protected function setupBody(body:PhysicalBody):void
            {
                body.width = Math.max(Math.random() * 100, 10);
                body.height = Math.max(Math.random() * 100, 10);
                body.x = Math.max(Math.random() * (world.width - body.width), 0);
                body.y = Math.max(Math.random() * (world.height - body.height), 0);
                body.rotates = rotationCheckbox.selected;
                world.registerBody(body);
            }
        ]]>
    </mx:Script>
    <mx:Label
        x="15" y="15"
        text="FlexWorld by Paul Taylor"
        />
    <graphics:PhysicalWorld 
        height="400"
        id="world"
        width="400"
        x="5"
        y="5"
        />
    <mx:VBox 
        x="450"
        y="100"
        >
        <mx:Button 
            icon="@Embed('assets/icons/accept.png')"
            id="startButton"
            label="Start World"
            click="startWorld()"
            />
        <mx:Button 
            icon="@Embed('assets/icons/stop.png')"
            id="stopButton"
            label="Stop World"
            click="stopWorld()"
            />
        <mx:Button 
            icon="@Embed('assets/icons/resultset_previous.png')"
            id="resetButton"
            label="Reset World"
            click="reset()"
            />
        <mx:VBox verticalGap="0"
            >
            <mx:CheckBox 
                id="rotationCheckbox"
                label="Allow Object Rotation"
                selected="true"
                />
            <mx:CheckBox 
                id="debugCheckbox"
                label="Draw Debug Data"
                selected="false"
                />
        </mx:VBox>
        <mx:HBox>
            <mx:VBox verticalGap="0"
                >
                <mx:Label 
                    text="Rectangles"
                    />
                <mx:NumericStepper 
                    id="numRectanglesStepper"
                    maximum="99"
                    value="5"
                    />
            </mx:VBox>
            <mx:VBox verticalGap="0"
                >
                <mx:Label 
                    text="Circles"
                    />
                <mx:NumericStepper 
                    id="numCirclesStepper"
                    maximum="99"
                    value="5"
                    />
            </mx:VBox>
        </mx:HBox>
    </mx:VBox>
</mx:Application>