IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
AnnotationTranslater.cs
1 using UnityEngine;
2 using System.Collections;
3 using UnityEngine.EventSystems;
4 
5 public class AnnotationTranslater : MonoBehaviour {
6 
7  //Mouse Moving attributs
8  private bool translate;
9  private Vector3 moveDirection;
10  private Vector3 cursorAnnoVector;
11  public float maxDistanceToMeshNode = 12f;
12 
13 
14  public void dragXDirection (BaseEventData eventData) {
15  PointerEventData data = eventData as PointerEventData;
16  if (data.button != PointerEventData.InputButton.Left) {
17  return;
18  }
19  moveDirection = new Vector3 (1f, 0f, 0f);
20  setupDirection ();
21  }
22 
23  public void dragYDirection (BaseEventData eventData) {
24  PointerEventData data = eventData as PointerEventData;
25  if (data.button != PointerEventData.InputButton.Left) {
26  return;
27  }
28  moveDirection = new Vector3 (0f, 1f, 0f);
29  setupDirection ();
30  }
31 
32  public void dragZDirection (BaseEventData eventData) {
33  PointerEventData data = eventData as PointerEventData;
34  if (data.button != PointerEventData.InputButton.Left) {
35  return;
36  }
37  moveDirection = new Vector3 (0f, 0f, 1f);
38  setupDirection ();
39  }
40 
41  public void setupDirection() {
42  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
43  Plane intersectPlane = new Plane ();
44  //get distance cursor to center
45  Vector3 camAnnoVec = (inputDevice.getEventCamera ().transform.position - this.transform.position);
46  Vector3 globalDir = this.transform.TransformDirection (moveDirection);
47  Vector3 planeVec = Vector3.Cross (globalDir, camAnnoVec);
48  Vector3 normal = Vector3.Cross (planeVec, globalDir);
49  //Debug.DrawLine (transform.position, transform.position + normal);
50  normal.Normalize ();
51  intersectPlane.SetNormalAndPosition (normal, transform.position);
52 
53  float dist = 0f;
54  Ray intersectRay = inputDevice.createRay ();
55  //Move Ray to CameraCenter
56  intersectRay.origin = inputDevice.getEventCamera ().transform.position;
57  if (intersectPlane.Raycast (intersectRay, out dist)) {
58  Vector3 intersectPoint = intersectRay.GetPoint (dist);
59  intersectPoint = this.transform.InverseTransformPoint (intersectPoint);
60  intersectPoint = Vector3.Scale (intersectPoint, moveDirection);
61  cursorAnnoVector = intersectPoint - this.transform.localPosition;
62  }
63  translate = true;
64  }
65 
66  public void stopTranslation(BaseEventData eventData){
67  PointerEventData data = eventData as PointerEventData;
68  if (data.button != PointerEventData.InputButton.Left) {
69  return;
70  }
71  translate = false;
72  }
73 
74  private void translateAnnotation () {
75  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
76  Plane intersectPlane = new Plane ();
77 
78  //get distance cursor to center
79  Vector3 camAnnoVec = (inputDevice.getEventCamera ().transform.position - this.transform.position);
80  Vector3 globalDir = this.transform.TransformDirection (moveDirection);
81  Vector3 planeVec = Vector3.Cross (globalDir, camAnnoVec);
82  Vector3 normal = Vector3.Cross (planeVec, globalDir);
83  //Debug.DrawLine (transform.position, transform.position + normal);
84  normal.Normalize ();
85  intersectPlane.SetNormalAndPosition (normal, transform.position);
86 
87  float dist = 0f;
88  Ray intersectRay = inputDevice.createRay ();
89  //Move Ray to CameraCenter
90  intersectRay.origin = inputDevice.getEventCamera ().transform.position;
91  if (intersectPlane.Raycast (intersectRay, out dist)) {
92  Vector3 intersectPoint = intersectRay.GetPoint (dist);
93  //intersectPoint is Local
94  intersectPoint = this.transform.InverseTransformPoint (intersectPoint);
95  intersectPoint = Vector3.Scale (intersectPoint, moveDirection);
96  intersectPoint -= cursorAnnoVector;
97  intersectPoint = this.transform.TransformPoint (intersectPoint);
98 
99  Vector3 vectorToMesh = AnnotationControl.instance.meshPositionNode.transform.position - intersectPoint;
100 
101  //Dont go outside of the dome !!!
102  if (vectorToMesh.magnitude < maxDistanceToMeshNode) {
103 
104  //Debug.DrawLine (transform.position, intersectPoint);
105  this.GetComponentInParent<Annotation>().translateMesh(intersectPoint);
106  }
107  }
108  }
109 
110  // Update is called once per frame
111  void Update () {
112  if(AnnotationControl.instance.meshPositionNode != null) {
113  this.transform.rotation = AnnotationControl.instance.meshPositionNode.transform.rotation;
114  }
115 
116  if(translate) {
117  translateAnnotation ();
118  }
119  }
120 }