package de.halfreal.spezi.gdx.framework;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;

import com.badlogic.gdx.Game;
import com.badlogic.gdx.Gdx;

public abstract class SpeziGame extends Game {

	public static interface DensityAdapter {
		float calculateDensity(float screenWdth, float screenHeight,
				float systemDensity);
	}

	private static DensityAdapter densityAdapter = new DensityAdapter() {

		@Override
		public float calculateDensity(float screenWdth, float screenHeight,
				float systemDensity) {
			return systemDensity;
		}
	};

	public static float getDeviceDensity() {
		if (Gdx.graphics != null) {
			return densityAdapter.calculateDensity(Gdx.graphics.getWidth(),
					Gdx.graphics.getHeight(), Gdx.graphics.getDensity());
		} else {
			return 2.0f;
		}
	}

	public static void setDensityAdapter(DensityAdapter densityAdapter) {
		SpeziGame.densityAdapter = densityAdapter;
	}

	@Override
	public AbstractScreen<?, ?> getScreen() {
		return (de.halfreal.spezi.gdx.framework.AbstractScreen<?, ?>) super
				.getScreen();
	}

	/**
	 * load an AbstractScreen from the defined Package and from the System
	 * Property -Dscreen=<YourScreenName>
	 * 
	 * @return C extends AbstractScreen<?,?> or null if no class found or if no
	 *         one argument Constructor exists
	 */
	public AbstractScreen<?, ?> loadScreenFromProperty(String basePackageName) {
		try {
			String property = System.getProperty("screen");
			if (property != null) {
				String className = basePackageName + "." + property;
				Class<?> clazz = Class.forName(className);
				Constructor<?> constructor = clazz
						.getConstructor(SpeziGame.class);

				return (AbstractScreen<?, ?>) constructor.newInstance(this);
			}

		} catch (ClassNotFoundException e) {
		} catch (SecurityException e) {
		} catch (NoSuchMethodException e) {
		} catch (IllegalArgumentException e) {
		} catch (InstantiationException e) {
		} catch (IllegalAccessException e) {
		} catch (InvocationTargetException e) {
		}
		return null;
	}

	public void startScreen(final AbstractScreen<?, ?> screen, boolean restart) {

		if (!restart && getScreen() != null && screen != null
				&& screen.getClass().equals(getScreen().getClass())) {
			return;
		}

		if (getScreen() != null) {
			final AbstractScreen<?, ?> oldScreen = getScreen();

			oldScreen.onPauseScreen(new TransitionListener() {

				@Override
				public void complete() {
					setScreen(screen);
				}

			});
		} else {
			setScreen(screen);
		}
	}

}

@@ -1780,32 +1780,56 @@
 dException e) %7B%0A
+%09%09%09e.printStackTrace();%0A
 %09%09%7D catch (Secur
@@ -1838,32 +1838,56 @@
 yException e) %7B%0A
+%09%09%09e.printStackTrace();%0A
 %09%09%7D catch (NoSuc
@@ -1900,32 +1900,56 @@
 dException e) %7B%0A
+%09%09%09e.printStackTrace();%0A
 %09%09%7D catch (Illeg
@@ -1965,32 +1965,56 @@
 tException e) %7B%0A
+%09%09%09e.printStackTrace();%0A
 %09%09%7D catch (Insta
@@ -2028,32 +2028,56 @@
 nException e) %7B%0A
+%09%09%09e.printStackTrace();%0A
 %09%09%7D catch (Illeg
@@ -2099,16 +2099,40 @@
 on e) %7B%0A
+%09%09%09e.printStackTrace();%0A
 %09%09%7D catc
@@ -2165,16 +2165,40 @@
 on e) %7B%0A
+%09%09%09e.printStackTrace();%0A
 %09%09%7D%0A%09%09re

Print exceptions in loadScreenFromProperty