using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class AR_TouchController : MonoBehaviour {
public GameObject[] scaleGameobject = new GameObject[]{} ;
public GameObject objectToRotate;
public float rotateSpead;
public float scaleSpeed;
public bool smothMovement;
private bool interactive = true;
static AR_TouchController myInstance;
static int instances = 0;
public static AR_TouchController Instance
{
get
{
if (myInstance == null ) {
myInstance = FindObjectOfType( typeof(AR_TouchController )) as AR_TouchController;
}
return myInstance;
}
}
//Called at the biginning of the game
void Start()
{
//Calibrates the myInstance static variable
instances++;
if (instances > 1) {
// Debug.LogError ("Warning: There are more than one AR_TouchController at the level");
}else {
myInstance = this;
}
ResourceModel.myEvent += FindSon;
}
// Subscribe to events
void OnEnable(){
EasyTouch.On_SwipeStart += On_SwipeStart;
EasyTouch.On_Swipe += On_Swipe;
EasyTouch.On_SwipeEnd += On_SwipeEnd;
EasyTouch.On_PinchIn += On_PinchIn;
EasyTouch.On_PinchOut += On_PinchOut;
EasyTouch.On_PinchEnd += On_PinchEnd;
}
void OnDisable(){
UnsubscribeEvent();
}
void OnDestroy(){
UnsubscribeEvent();
}
void UnsubscribeEvent(){
EasyTouch.On_SwipeStart -= On_SwipeStart;
EasyTouch.On_Swipe -= On_Swipe;
EasyTouch.On_SwipeEnd -= On_SwipeEnd;
EasyTouch.On_PinchIn -= On_PinchIn;
EasyTouch.On_PinchOut -= On_PinchOut;
EasyTouch.On_PinchEnd -= On_PinchEnd;
}
// At the swipe beginning
private void On_SwipeStart( Gesture gesture){
//Pich = objectToRotate.transform.eulerAngles.y;
//Yaw = objectToRotate.transform.eulerAngles.x;
}
float Yaw = 0;
float Pich = 0;
float targetYaw = 0;
float targetPich = 0;
// During the swipe
private void On_Swipe(Gesture gesture){
if (interactive) {
targetYaw = gesture.deltaPosition.x;
targetPich = gesture.deltaPosition.y;
if (smothMovement) {
// Yaw = Mathf.Lerp (Yaw, targetYaw, Time.deltaTime * rotateSpead);
// Pich = Mathf.Lerp (Pich, targetPich, Time.deltaTime * rotateSpead);
} else {
Yaw = Mathf.Lerp (Yaw, targetYaw, 1);
Pich = Mathf.Lerp (Pich, targetPich, 1);
}
}
}
void Update(){
if (objectToRotate == null)
return;
Yaw = Mathf.Lerp (Yaw, targetYaw, Time.deltaTime * rotateSpead);
Pich = Mathf.Lerp (Pich, targetPich, Time.deltaTime * rotateSpead);
//Debug.Log ("Yaw: " + Yaw + " targetYaw:" + targetYaw);
if ((Yaw != targetYaw) || (Pich != targetPich)) {
objectToRotate.transform.Rotate (transform.up, -Mathf.Deg2Rad * Yaw * 20 * rotateSpead, Space.Self);
objectToRotate.transform.Rotate (transform.right, Mathf.Deg2Rad * Pich * 20 * rotateSpead, Space.World);
}
}
// At the swipe end
private void On_SwipeEnd(Gesture gesture){
//Pich = objectToRotate.transform.eulerAngles.y;
//Yaw = objectToRotate.transform.eulerAngles.x;
targetYaw = 0;
targetPich = 0;
}
// At the pinch in
private void On_PinchIn(Gesture gesture){
if (interactive) {
float zoom = Time.deltaTime * gesture.deltaPinch * scaleSpeed;
Vector3 scale = objectToRotate.transform.localScale;
float zoomX = Mathf.Clamp (scale.x - zoom, 0.1f,3);
float zoomY = Mathf.Clamp (scale.y - zoom, 0.1f,3);
float zoomZ = Mathf.Clamp (scale.z - zoom, 0.1f,3);
objectToRotate.transform.localScale = new Vector3 (zoomX, zoomY, zoomZ);
//Debug.Log (objectToRotate.transform.localScale);
}
}
// At the pinch out
private void On_PinchOut(Gesture gesture){
if (interactive) {
float zoom = Time.deltaTime * gesture.deltaPinch * scaleSpeed;
Vector3 scale = objectToRotate.transform.localScale;
float zoomX = Mathf.Clamp (scale.x + zoom, 0.1f,3);
float zoomY = Mathf.Clamp (scale.y + zoom, 0.1f,3);
float zoomZ = Mathf.Clamp (scale.z + zoom, 0.1f,3);
objectToRotate.transform.localScale = new Vector3 (zoomX, zoomY, zoomZ);
//Debug.Log (zoomX);
}
}
// At the pinch end
private void On_PinchEnd(Gesture gesture){
}
// public mathod
public void SetTouchable(bool canTouch){
interactive = canTouch;
}
void FindSon(){
for (int i=0;i<scaleGameobject.Length;i++)
{
if (scaleGameobject [i].transform.childCount >= 1) {
objectToRotate = scaleGameobject [i].transform.GetChild (0).transform.transform.gameObject;
}
}
}
}