MT4j/examples/fiducials/StartExample.java

package fiducials;
 
import org.mt4j.MTApplication;
 
public class StartExample extends MTApplication {
 
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		initialize();
	}
 
	@Override
	public void startUp() {
		this.addScene(new FiducialScene(this, "Fiducial Scene"));
	}
 
}

MT4j/examples/fiducials/FiducialScene.java

package fiducials;
 
import java.util.HashMap;
import java.util.Map;
 
import org.mt4j.MTApplication;
import org.mt4j.components.visibleComponents.font.FontManager;
import org.mt4j.components.visibleComponents.font.IFont;
import org.mt4j.components.visibleComponents.shapes.AbstractShape;
import org.mt4j.components.visibleComponents.shapes.MTEllipse;
import org.mt4j.components.visibleComponents.widgets.MTTextArea;
import org.mt4j.input.IMTInputEventListener;
import org.mt4j.input.inputData.MTInputEvent;
import org.mt4j.input.inputProcessors.componentProcessors.dragProcessor.DragProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.rotateProcessor.RotateProcessor;
import org.mt4j.input.inputProcessors.componentProcessors.scaleProcessor.ScaleProcessor;
import org.mt4j.input.inputProcessors.globalProcessors.rawFiducialProcessor.RawFiducialEvt;
import org.mt4j.input.inputProcessors.globalProcessors.rawFiducialProcessor.RawFiducialProcessor;
import org.mt4j.sceneManagement.AbstractScene;
import org.mt4j.util.math.Tools3D;
import org.mt4j.util.math.Vector3D;
 
 
public class FiducialScene extends AbstractScene implements IMTInputEventListener {
	private MTApplication app;
	private IFont font;
 
	private Map<Integer, AbstractShape> fiducialIDToComp;
 
	public FiducialScene(MTApplication mtApplication, String name) {
		super(mtApplication, name);
		this.app = mtApplication;
 
		RawFiducialProcessor fiducialProcessor = new RawFiducialProcessor();
		fiducialProcessor.addProcessorListener(this);
		registerGlobalInputProcessor(fiducialProcessor);
 
		fiducialIDToComp = new HashMap<Integer, AbstractShape>();
 
		font = FontManager.getInstance().createFont(app, "arial.ttf", 30, 
				255, 255, 255, 255, 
				255, 255, 255, 255);
 
		setClearColor(220, 220, 200, 255);
	}
 
 
 
	@Override
	public boolean processInputEvent(MTInputEvent inEvt) {
		if (inEvt instanceof RawFiducialEvt) {
			RawFiducialEvt fEvt = (RawFiducialEvt) inEvt;
			int fID = fEvt.getFiducialID();
			AbstractShape comp;
 
			switch (fEvt.getId()) {
			case RawFiducialEvt.GESTURE_DETECTED:
				//Create a new component for the fiducial
				AbstractShape newComp = createComponent(fID, fEvt.getInputLastPosition());
				fiducialIDToComp.put(fID, newComp);
				//Move component to fiducial position
				newComp.setPositionGlobal(fEvt.getInputLastPosition());
				//Save the absolute rotation angle in the component for late
				newComp.setUserData("angle", fEvt.getAngle()); 
				//Rotate the component
				newComp.rotateZ(newComp.getCenterPointRelativeToParent(), MTApplication.degrees(fEvt.getAngle())); //TODO
				//Add the component to the canvas to draw it
				getCanvas().addChild(newComp);	
				break;
			case RawFiducialEvt.GESTURE_UPDATED:
				//Retrieve the corresponding component for the fiducial ID from the map
				comp = fiducialIDToComp.get(fID);
				if (comp != null){
					//Set the new position 
					comp.setPositionGlobal(fEvt.getInputLastPosition());
					//Set the rotation (we have to do a little more here because
					//mt4j does incremental rotations instead of specifying the absolute angle)
					float oldAngle = (Float)comp.getUserData("angle"); //retrieve the "old" angle
					float newAngle = fEvt.getAngle();
					if (oldAngle != newAngle){
						float diff = newAngle-oldAngle;
						comp.setUserData("angle", newAngle);
						diff = MTApplication.degrees(diff); //our rotation expects degrees (not radians)
						comp.rotateZ(comp.getCenterPointRelativeToParent(), diff); 
					}
				}
				break;
			case RawFiducialEvt.GESTURE_ENDED:
				comp = fiducialIDToComp.get(fID);
				if (comp != null){
					getCanvas().removeChild(comp);
					fiducialIDToComp.remove(fID);
				}
				break;
			default:
				break;
			}
		}
		return true;
	}
 
	private AbstractShape createComponent(int id, Vector3D pos){
		MTEllipse comp = new MTEllipse(app, new Vector3D(pos), 50,50, 50);
		comp.setNoFill(false);
		float r = Tools3D.getRandom(20, 255);
		float g = Tools3D.getRandom(20, 255); 
		float b = Tools3D.getRandom(20, 255);
		comp.setFillColor(r, g, b, 200);
		comp.setNoStroke(false);
		comp.setStrokeWeight(1);
		comp.setStrokeColor(r, g, b, 200);
		comp.removeAllGestureEventListeners(DragProcessor.class);
		comp.removeAllGestureEventListeners(RotateProcessor.class);
		comp.removeAllGestureEventListeners(ScaleProcessor.class);
 
		MTTextArea text = new MTTextArea(app, font);
		text.appendText(new Integer(id).toString());
		text.setFillColor(0, 0, 0, 0);
		text.setStrokeColor(0, 0, 0, 0);
		text.removeAllGestureEventListeners(DragProcessor.class);
		text.removeAllGestureEventListeners(RotateProcessor.class);
		text.removeAllGestureEventListeners(ScaleProcessor.class);
		comp.addChild(text);
		text.setPositionRelativeToParent(comp.getCenterPointLocal());
		return comp;
	}
 
	@Override
	public void init() {
	}
 
	@Override
	public void shutDown() {
	}
}
Powered by MediaWiki contact