package net.mgsx.game.plugins.core.systems;

import com.badlogic.ashley.core.Engine;
import com.badlogic.ashley.core.Entity;
import com.badlogic.ashley.core.Family;
import com.badlogic.ashley.systems.IteratingSystem;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.math.Vector3;

import net.mgsx.game.core.EditorScreen;
import net.mgsx.game.core.GamePipeline;
import net.mgsx.game.core.annotations.EditableSystem;
import net.mgsx.game.core.annotations.Inject;
import net.mgsx.game.core.components.Movable;
import net.mgsx.game.core.tools.Tool;
import net.mgsx.game.plugins.core.components.Transform2DComponent;
import net.mgsx.game.plugins.editor.systems.DebugRenderSystem;
import net.mgsx.game.plugins.editor.systems.SelectionSystem;

@EditableSystem(isDebug=true)
public class SelectionRenderSystem extends IteratingSystem {
	final private EditorScreen editor;
	@Inject protected DebugRenderSystem render;
	final private Vector3 pos = new Vector3();
	
	private SelectionSystem selection;
	
	public SelectionRenderSystem(EditorScreen editor) {
		super(Family.one(Movable.class, Transform2DComponent.class).get(), GamePipeline.RENDER_OVER);
		this.editor = editor;
	}
	
	@Override
	public void addedToEngine(Engine engine) {
		super.addedToEngine(engine);
		selection = engine.getSystem(SelectionSystem.class);
	}

	@Override
	public void update(float deltaTime) {
		
		
		render.shapeRenderer.setProjectionMatrix(editor.getGameCamera().combined);
		render.shapeRenderer.begin(ShapeType.Line);
		
		super.update(deltaTime);
		
		render.shapeRenderer.end();
	}

	@Override
	protected void processEntity(Entity entity, float deltaTime) 
	{
		// TODO optimize with a "selected" component ! or editorComponent with selection flag ?
		Movable movable = Movable.components.get(entity);
		Transform2DComponent transform = Transform2DComponent.components.get(entity);
		if(movable != null) movable.getPosition(entity, pos);
		else pos.set(transform.position.x, transform.position.y, 0);
		
		Vector2 s = Tool.pixelSize(editor.getGameCamera()).scl(5);
		boolean inSelection = selection.contains(entity);
		if(inSelection) render.shapeRenderer.setColor(1, 1, 0, 1);
		render.shapeRenderer.rect(pos.x-s.x, pos.y-s.y, 2*s.x, 2*s.y);
		if(inSelection) render.shapeRenderer.setColor(1, 1, 1, 1);
	}
}
@@ -206,24 +206,64 @@
 tingSystem;%0A
+import com.badlogic.gdx.graphics.Color;%0A
 import com.b
@@ -316,16 +316,16 @@
 peType;%0A
-
 import c
@@ -1251,12 +1251,13 @@
 DER_
-OVER
+TOOLS
 );%0A%09
@@ -2205,24 +2205,24 @@
 ns(entity);%0A
-
 %09%09if(inSelec
@@ -2270,16 +2270,67 @@
  0, 1);%0A
+%09%09else render.shapeRenderer.setColor(Color.WHITE);%0A
 %09%09render

fix selection rendering