package ca.sumost.kinetic;

import ca.sumost.kinetic.editor.WorldEditorListener;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.input.GestureDetector;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.utils.Array;

public class GameScreen implements Screen 
{
	private final KineticTheoryGame game;
	private final OrthographicCamera camera = new OrthographicCamera(10, 10);
	
	@SuppressWarnings("unused")
	private final Box2DDebugRenderer debugRenderer = new Box2DDebugRenderer();
	private final ShapeRenderer mShapeRenderer = new ShapeRenderer();
	
	
	public GameScreen(final KineticTheoryGame g)
	{
		game = g;
		
		ScreenConverter sc = new ScreenConverter()
		{
			@Override
			public Vector2 ToWorld(float xScreen, float yScreen) 
			{
				Vector3 point = new Vector3();
				point.set(xScreen, yScreen, 0);
	        	camera.unproject(point);
	        	return new Vector2(point.x, point.y);
			}
		};
		
		Gdx.input.setInputProcessor(new GestureDetector(new WorldEditorListener(g.world, sc)));
	}
	
	@Override
	public void render(float delta) 
	{
		Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        camera.update();
        //debugRenderer.render(game.world, camera.combined);
        mShapeRenderer.setProjectionMatrix(camera.combined);
        
        renderWorld();
        
        game.world.step(1/60f, 6, 2);
	}

	private void renderWorld() 
	{
		mShapeRenderer.begin(ShapeType.Filled);
		
		Array<Body> bodies = new Array<Body>();
        game.world.getBodies(bodies);
        for (Body body : bodies)
        {
        	Object ud = body.getUserData();
        	if (ud instanceof Renderable)
        	{
        		((Renderable)ud).render(mShapeRenderer, body);
        	}
        }
        
        mShapeRenderer.end();
	}

	@Override
	public void resize(int width, int height) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void show() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void hide() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub
		
	}

}

@@ -655,17 +655,51 @@
  Screen 
+// TODO: use ScreenAdapter instead
 %0A
-
 %7B%0A%09priva
@@ -801,14 +801,8 @@
 era(
-10, 10
 );%0A%09
@@ -1333,16 +1333,39 @@
 %09%09%7D;%0A%09%09%0A
+%09%09setCameraViewport();%0A
 %09%09Gdx.in
@@ -1451,16 +1451,383 @@
 );%0A%09%7D%0A%09%0A
+%09private void setCameraViewport()%0A%09%7B%0A%09%09final float smallEdgeLength = 10;%0A%09%09float aspectRatio = (float) Gdx.graphics.getWidth() / (float) Gdx.graphics.getHeight();%0A%09%09if (aspectRatio %3C 1)%0A%09%09%7B%0A%09%09%09camera.setToOrtho(false, smallEdgeLength, smallEdgeLength / aspectRatio);%0A%09%09%7D%0A%09%09else%0A%09%09%7B%0A%09%09%09camera.setToOrtho(false, smallEdgeLength * aspectRatio, smallEdgeLength);%0A%09%09%7D%0A%09%7D%0A%0A
 %09@Overri
@@ -2655,49 +2655,34 @@
 ht) 
+%0A%09
 %7B%0A%09%09
-// TODO Auto-generated method stub%0A%09%09
+setCameraViewport();
 %0A%09%7D%0A

Fix issue of viewport aspect ratio: circles are now circles.