﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/*************************************************************************
* 
* ARConnex CONFIDENTIAL
* __________________
* 
*  [2017] - [2021] ARConnex Incorporated 
*  All Rights Reserved.
* 
* NOTICE:  All information contained herein is, and remains
* the property of ARConnex Incorporated and its suppliers,
* if any.  The intellectual and technical concepts contained
* herein are proprietary to ARConnex Incorporated
* and its suppliers and may be covered by U.S. and Foreign Patents,
* patents in process, and are protected by trade secret or copyright law.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from ARConnex Incorporated.
*/

// Add a box collider component to the GameObject you want to allow swipe rotation.
// Add this component on that GameObject.  
// Choose the default starting axis or none if you will add a 3 axis user interface
// Change power as needed in the inspector to control the rotation speed.
// See the How To section of ARConnex.com to download the RotateViaSwipe.unitypackage
// which includes a prefab with a 3 axis user interface example.



public class RotateViaSwipe : MonoBehaviour
{  
       public enum Axis {none,X,Y,Z}
       [Header("Select Default Rotation Axis or NONE if using a 3 axis UI for control")]
       public Axis startingAxis;

       [Header("Rotation speed - 1 = fast, 100 = fine tune")]
       [Range(1, 100)]
       public int power=5;

       bool RotateAroundX;
       bool RotateAroundY;
       bool RotateAroundZ;
    
    Vector3 lastPosition;

    void Start()
    {
        if (startingAxis == Axis.X)
        {
            RotateAroundX = true;
        }
        if (startingAxis == Axis.Y)
        {
            RotateAroundY = true;
        }
        if (startingAxis == Axis.Z)
        {
            RotateAroundZ = true;
        }
        if (startingAxis == Axis.none)
        {
            RotateAroundX = false;
            RotateAroundY = false;
            RotateAroundZ = false;
        }

    }    

    void OnMouseDown()
    {
        lastPosition = Input.mousePosition;
    }   

    public void ToggleRotationX(){
       
        if (!RotateAroundX)
        {
            RotateAroundX = true;
            RotateAroundY = false;
            RotateAroundZ = false;           
        }       
    }

    public void ToggleRotationY()
    {
        if (!RotateAroundY)
        {
            RotateAroundX = false;
            RotateAroundY = true;
            RotateAroundZ = false;
        }
    }

    public void ToggleRotationZ(){
       
        if (!RotateAroundZ)
        {
            RotateAroundX = false;
            RotateAroundY = false;
            RotateAroundZ = true;
        }
    }  

    void PerformLinearRotationX()
    {
        Vector3 currPosition = Input.mousePosition;
        Vector3 difference = currPosition - lastPosition;
        lastPosition = currPosition;
        float change = difference.x + difference.y;
        transform.Rotate(new Vector3(change/power, 0, 0));
    }

    void PerformLinearRotationY()
    {
        Vector3 currPosition = Input.mousePosition;
        Vector3 difference = currPosition - lastPosition;
        lastPosition = currPosition;
        float change = difference.x + difference.y;        
        transform.Rotate(new Vector3(0, -change/power, 0));
    }

    
    void PerformLinearRotationZ()
    {
        Vector3 currPosition = Input.mousePosition;
        Vector3 difference = currPosition - lastPosition;
        lastPosition = currPosition;
        float change = difference.x + difference.y;
        transform.Rotate(new Vector3(0, 0, change/power));
    }

    void OnMouseDrag()
    {
        if (RotateAroundX || RotateAroundY || RotateAroundZ)
        {
            if (RotateAroundX)
            {
                PerformLinearRotationX();
            }
            if (RotateAroundY)
            {
                PerformLinearRotationY();
            }
            if (RotateAroundZ)
            {
                PerformLinearRotationZ();
            }

        }
       
    }
}
