IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
AnnotationSphereScaler.cs
1 using UnityEngine;
2 using System.Collections;
3 using UnityEngine.EventSystems;
4 using UI;
5 
6 public class AnnotationSphereScaler : MonoBehaviour{
7 
8  public GameObject bobble;
9 
10  public float maxScale = 15f;
11  public float minScale = 0.5f;
12 
13  private bool reScale = false;
14  //private bool onMesh = false;
15  private Vector3 rescaleDirection;
16 
17  public void rescaleAnnotation(BaseEventData eventData) {
18  PointerEventData data = eventData as PointerEventData;
19  if (data.button == PointerEventData.InputButton.Left) {
20  rescaleDirection = bobble.transform.localPosition;
21  reScale = true;
22  }
23  }
24 
25  public void stopRescale(BaseEventData eventData) {
26  PointerEventData data = eventData as PointerEventData;
27  if (data.button == PointerEventData.InputButton.Left) {
28  reScale = false;
29  }
30 
31  }
32 
33  private void rescaleMesh() {
34  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
35  Plane intersectPlane = new Plane ();
36  float dist = 0f;
37  Ray intersectRay = inputDevice.createRay ();
38  //Move Ray to CameraCenter
39  intersectRay.origin = inputDevice.getEventCamera ().transform.position;
40 
41  //update plane
42 
43  Vector3 globalDir = this.transform.parent.TransformDirection (rescaleDirection);
44  Vector3 camAnnoVec = (intersectRay.origin - this.transform.parent.position);
45 
46  Vector3 planeVec = Vector3.Cross (globalDir, camAnnoVec);
47  Vector3 normal = Vector3.Cross (globalDir, planeVec);
48  normal.Normalize ();
49 
50  intersectPlane.SetNormalAndPosition (normal, this.transform.parent.position);
51 
52  if (intersectPlane.Raycast (intersectRay, out dist)) {
53  Vector3 intersectPoint = intersectRay.GetPoint (dist);
54  Vector3 intersectDirection = intersectPoint - this.transform.parent.position;
55  if (intersectDirection.magnitude < maxScale && intersectDirection.magnitude > minScale) {
56  bobble.transform.position = intersectPoint;
57  //Add scale only in one Direction
58  float newScale = bobble.transform.localPosition.magnitude * 2;
59  this.GetComponentInParent<Annotation> ().rescaleMesh (new Vector3 (newScale, newScale, newScale));
60  }
61 
62 
63 
64 
65  }
66 
67  }
68 
69  public void Start() {
70  Vector3 scale = this.GetComponentInParent<Annotation> ().getMeshScale ();
71  bobble.transform.localPosition = new Vector3 (scale.x / 2f, 0f, 0f);
72  }
73 
74  public void Update() {
75  if(reScale) {
76  rescaleMesh ();
77  }
78  }
79 }