package net.mostlyoriginal.plugin;

import com.artemis.BaseSystem;
import com.artemis.annotations.Wire;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import net.mostlyoriginal.api.SystemProfiler;
import net.mostlyoriginal.api.SystemProfilerGUI;

/**
 * Renders profiler.
 *
 * @author piotr-j
 * @author Daan van Yperen
 */
@Wire
public class ProfilerSystem extends BaseSystem {

	OrthographicCamera camera;
	ShapeRenderer renderer;
	Stage stage;
	Skin skin;

	SystemProfilerGUI gui;

	@Override
	protected void initialize() {

		camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		camera.setToOrtho(false);
		camera.update();
		renderer = new ShapeRenderer();
		stage = new Stage();
		stage.getBatch().setProjectionMatrix(camera.combined);
		skin = new Skin(Gdx.files.internal("profiler/uiskin.json"));

		// resume profiling
		SystemProfiler.resume();
		// setup some static config like colors etc
		SystemProfilerGUI.GRAPH_H_LINE.set(Color.ORANGE);
		gui = new SystemProfilerGUI(skin, "default");
		gui.setResizeBorder(8);
		gui.show(stage);
		gui.setWidth(Gdx.graphics.getWidth());
	}

	@Override
	protected void processSystem() {
		if (!isEnabled() || gui.getParent() == null) {
			return;
		}

		updateStageInput();
		stage.act(world.delta);
		stage.draw();
		renderer.setProjectionMatrix(camera.combined);
		renderer.begin(ShapeRenderer.ShapeType.Line);
		gui.updateAndRender(world.delta, renderer);
		renderer.end();
	}

	private boolean leftMouseDown;
	/** Emulate stage input to maintain pre-existing input processor. */
	private void updateStageInput() {
		if ( Gdx.input.isButtonPressed(Input.Buttons.LEFT)) {
			if ( !leftMouseDown) {
				leftMouseDown = true;
				stage.touchDown(Gdx.input.getX(), Gdx.input.getY(), 0, Input.Buttons.LEFT);
			} else {
				stage.touchDragged(Gdx.input.getX(), Gdx.input.getY(), 0);
			}
		} else if (leftMouseDown)  {
			leftMouseDown = false;
			stage.touchUp(Gdx.input.getX(), Gdx.input.getY(), 0, Input.Buttons.LEFT);
		}
	}

	@Override
	protected void dispose() {
		SystemProfiler.dispose();
	}
}

@@ -733,16 +733,47 @@
 GUI gui;
+%0A%09private boolean f3ButtonDown;
 %0A%0A%09@Over
@@ -1124,57 +1124,8 @@
 );%0A%0A
-%09%09// resume profiling%0A%09%09SystemProfiler.resume();%0A
 %09%09//
@@ -1427,274 +1427,900 @@
  %7C%7C 
-gui.getParent() == null) %7B%0A%09%09%09return;%0A%09%09%7D%0A%0A%09%09updateStageInput();%0A%09%09stage.act(world.delta);%0A%09%09stage.draw();%0A%09%09renderer.setProjectionMatrix(camera.combined);%0A%09%09renderer.begin(ShapeRenderer.ShapeType.Line);%0A%09%09gui.updateAndRender(world.delta, renderer);%0A%09%09renderer.end()
+!isConfigured()) %7B%0A%09%09%09return;%0A%09%09%7D%0A%0A%09%09checkActivationButton();%0A%0A%09%09if ( SystemProfiler.isRunning()) %7B%0A%09%09%09processInput();%0A%09%09%09render();%0A%09%09%7D%0A%09%7D%0A%0A%09private boolean isConfigured() %7B%0A%09%09return gui.getParent() != null;%0A%09%7D%0A%0A%09private void render() %7B%0A%09%09stage.act(world.delta);%0A%09%09stage.draw();%0A%09%09renderer.setProjectionMatrix(camera.combined);%0A%09%09renderer.begin(ShapeRenderer.ShapeType.Line);%0A%09%09gui.updateAndRender(world.delta, renderer);%0A%09%09renderer.end();%0A%09%7D%0A%0A%09private void checkActivationButton() %7B%0A%09%09if ( Gdx.input.isKeyPressed(Input.Keys.F3) ) %7B%0A%09%09%09if ( !f3ButtonDown ) %7B%0A%09%09%09%09if (!SystemProfiler.isRunning()) %7B%0A%09%09%09%09%09gui.setHeight(Gdx.graphics.getHeight()/2);%0A%09%09%09%09%09SystemProfiler.resume();%0A%09%09%09%09%7D else if ( gui.getHeight() != Gdx.graphics.getHeight() ) %7B%0A%09%09%09%09%09gui.setHeight(Gdx.graphics.getHeight());%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09SystemProfiler.pause();%0A%09%09%09%09%7D%0A%09%09%09%7D%0A%09%09%09f3ButtonDown = true;%0A%09%09%7D else f3ButtonDown = false
 ;%0A%09%7D
@@ -2441,19 +2441,15 @@
 oid 
-updateStage
+process
 Inpu

Make profiler toggle with F3.