package net.mostlyoriginal.game.system.logic;

import com.artemis.Aspect;
import com.artemis.ComponentMapper;
import com.artemis.Entity;
import com.artemis.annotations.Wire;
import com.artemis.managers.TagManager;
import com.artemis.systems.EntityProcessingSystem;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import net.mostlyoriginal.api.component.basic.Pos;
import net.mostlyoriginal.api.component.graphics.Anim;
import net.mostlyoriginal.api.utils.EntityUtil;
import net.mostlyoriginal.game.component.Tool;
import net.mostlyoriginal.game.component.ui.Clickable;
import net.mostlyoriginal.game.manager.AssetSystem;
import net.mostlyoriginal.game.system.interaction.MouseClickSystem;

/**
 * @author Daan van Yperen
 */
@Wire
public class ToolSystem extends EntityProcessingSystem {

	protected ComponentMapper<Clickable> mClickable;
	protected ComponentMapper<Tool> mTool;
	protected ComponentMapper<Anim> mAnim;
	protected ComponentMapper<Pos> mPos;
	private TagManager tagManager;
	private boolean buttonWasDown=true;
	private AssetSystem assetSystem;
	private MouseClickSystem mouseClickSystem;

	@SuppressWarnings("unchecked")
	public ToolSystem() {
		super(Aspect.getAspectForAll(Tool.class,Pos.class, Anim.class));
	}

	@Override
	protected void process(Entity e) {

		if ( mouseClickSystem.rightMouseTapped )
		{
			reset();
			return;
		}

		final Entity cursor = tagManager.getEntity("cursor");
		if ( cursor != null ) {
			Pos cursorPos = mPos.get(cursor);
			Pos toolPos = mPos.get(e);

			Anim anim = mAnim.get(e);
			TextureRegion icon = assetSystem.get(anim.id).getKeyFrame(0);
			toolPos.x = cursorPos.x - (icon.getRegionWidth()/2 * anim.scale);
			toolPos.y = cursorPos.y - (icon.getRegionHeight()/2 * anim.scale);

			Clickable clickable = mClickable.get(e);
			if ( mouseClickSystem.leftMouseTapped  )
			{
				Tool tool = mTool.get(e);
				if ( tool.listener.enabled() && (tool.continuous || !buttonWasDown) ) {
					tool.listener.run();
					buttonWasDown = true;
				}
			} else buttonWasDown=false;
		}
	}

	public void reset() {
		EntityUtil.safeDeleteAll(getActives());
	}
}

@@ -254,24 +254,84 @@
 singSystem;%0A
+import com.badlogic.gdx.Gdx;%0Aimport com.badlogic.gdx.Input;%0A
 import com.b
@@ -370,16 +370,16 @@
 Region;%0A
-
 import n
@@ -1160,16 +1160,50 @@
 kSystem;
+%0A%09private InputSystem inputSystem;
 %0A%0A%09@Supp
@@ -1885,41 +1885,53 @@
 f ( 
-mouseClickSystem.leftMouseTapped 
+Gdx.input.isButtonPressed(Input.Buttons.LEFT)
  )%0A%09

Fix drawing on main map.