package Controler.Parsor;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import jpen.PLevel;
import jpen.PLevelEvent;
import jpen.event.PenAdapter;
import jpen.owner.multiAwt.AwtPenToolkit;

public class JPenParsor extends PenAdapter
{
	private Point2D.Double loc = new Point2D.Double();
	private List<ParsorListener> parsorListenerList;

	public JPenParsor(JPanel panel) {
		
		parsorListenerList = new ArrayList<ParsorListener>();
		
		AwtPenToolkit.addPenListener(panel, this);
		
		//PenManager pm=AwtPenToolkit.getPenManager();
		//pm.pen.levelEmulator.setPressureTriggerForLeftCursorButton(0.5f);
		
	}

	@Override
	public void penLevelEvent(PLevelEvent ev)
	{
		if(!ev.isMovement())
			return;
		
		float pressure = ev.pen.getLevelValue(PLevel.Type.PRESSURE) * 100;
		loc.x = ev.pen.getLevelValue(PLevel.Type.X);
		loc.y = ev.pen.getLevelValue(PLevel.Type.Y);
				
		for(ParsorListener pl : parsorListenerList) {
			pl.parsorCoordinates(loc, (int)pressure);
		}
		
	}
	
	public void subscribe(ParsorListener pl) {
		parsorListenerList.add(pl);
	}
	
	public void unsubscribe(ParsorListener pl) {
		parsorListenerList.remove(pl);
	}
}