using Augumenta;
using UnityEngine;

/**
 * PoseTransformer stores and provides a class for moving the objects based on
 * PoseEvents using various restrictions, like screen, rotation or translation.
 */

public class PoseTransformer: PoseHandler
{
	// enable object rotation
	[Tooltip("Update object rotation from detection event")]
	public bool enableRotation=true;
	// enable object translation
	[Tooltip("Update object position from detection event")]
	public bool enableTranslation=true;
	// use screen coordinates for translation
	[Tooltip("Use screen space to position the object from event")]
	public bool relativeToScreen=false;

	protected void TransformPose(GameObject obj, PoseEvent e, Vector3 p, Quaternion q)
	{
		if(enableTranslation) {
			if(relativeToScreen) {
				// set object position to event center relative to the screen coordinates
				float x=e.relativeImage.centerX * Camera.main.pixelWidth;
				float y=(1 - e.relativeImage.centerY) * Camera.main.pixelHeight;
				float z=e.absoluteCamera.centerZ / 1000f;
				obj.transform.position=Camera.main.ScreenToWorldPoint(new Vector3(x, y, z));
			} else {
				// set the object position in absolute coordinate of the head (where webcamera is mounted)
				obj.transform.position=p;
			}
		}
		if(enableRotation) {
			obj.transform.rotation=q;
		}
		// Debug.DrawRay(Camera.main.transform.position, obj.transform.position-Camera.main.transform.position, Color.red);
	}
}
