IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
CanvasMovement.cs
1 using UnityEngine;
2 using System.Collections;
3 
4 public class CanvasMovement : MonoBehaviour {
5 
6  // Use this for initialization
7 
8  public Vector3 newPos { get; set; }
9  private Vector3 velocity = Vector3.one;
10 
11  private GameObject meshNode;
12  private GameObject meshPositionNode;
13  private GameObject meshViewerBase;
14  private Vector3[] pointsOnLabel = new Vector3[4];
15  private float[] distances = new float[4];
16  public GameObject annotationPoint { get; set; }
17 
18  public bool move = true;
19 
20  public float labelScale = 0.04f; // größe der labels..
21 
22 
23  void Start ()
24  {
25  meshViewerBase = GameObject.Find("MeshViewerBase");
26  meshNode = GameObject.Find("MeshViewerBase/MeshViewerScale");
27  meshPositionNode = GameObject.Find("MeshViewerBase/MeshViewerScale/MeshRotationNode/MeshPositionNode");
28  labelScale = 0.04f;
29  }
30 
31  // Update is called once per frame
32  void Update () {
33 
34  adaptLabelToZooming();
35 
36  if( move )
37  this.transform.position = Vector3.SmoothDamp(this.transform.position, newPos, ref velocity, 2f);
38 
39  updateConnectionLine ();
40  }
41 
42  public void updateConnectionLine()
43  {
44  //this.transform.position = newPos;
45  if(annotationPoint != null) {
46  this.GetComponent<LineRenderer>().SetPosition(0, annotationPoint.transform.position);
47  }
48 
49 
50  float width = this.GetComponent<RectTransform>().rect.width * this.transform.parent.localScale.x * this.GetComponent<RectTransform>().localScale.x * meshNode.transform.localScale.x * meshPositionNode.transform.localScale.x * meshViewerBase.transform.localScale.x;
51  float height = this.GetComponent<RectTransform>().rect.height * this.transform.parent.localScale.x * this.GetComponent<RectTransform>().localScale.x * meshNode.transform.localScale.x * meshPositionNode.transform.localScale.x * meshViewerBase.transform.localScale.x;
52 
53  pointsOnLabel[0] = this.transform.position + this.transform.up * (height / 2) + this.transform.right * (width / 2);
54  pointsOnLabel[1] = this.transform.position + this.transform.up * (height / 2) - this.transform.right * (width / 2);
55  pointsOnLabel[2] = this.transform.position - this.transform.up * (height / 2) + this.transform.right * (width / 2);
56  pointsOnLabel[3] = this.transform.position - this.transform.up * (height / 2) - this.transform.right * (width / 2);
57 
58 
59 
60  for (int i = 0; i < distances.Length; i++)
61  {
62 
63  if(annotationPoint != null)
64  {
65  distances[i] = (pointsOnLabel[i] - annotationPoint.transform.position).magnitude;
66 
67  }
68  }
69 
70 
71  for (int i = 0; i < distances.Length; i++)
72  {
73  bool smallest = false;
74 
75  for (int j = i; j < distances.Length; j++)
76  {
77 
78  if (distances[i] <= distances[j])
79  {
80  smallest = true;
81  }
82  else
83  {
84  smallest = false;
85  }
86 
87  }
88 
89  if (smallest == true)
90  {
91 
92  this.GetComponent<LineRenderer>().SetPosition(1, pointsOnLabel[i]);
93  break;
94  }
95 
96  }
97 
98  this.GetComponent<LineRenderer>().enabled = true;
99  }
100 
101  public void adaptLabelToZooming() // die größe der Labels sollten unabhängig vom zoomen sein..
102  { // war davor am label angeheftet
103 
104  this.GetComponent<RectTransform>().localScale = new Vector3(labelScale / meshNode.transform.localScale.x, labelScale / meshNode.transform.localScale.x, labelScale / meshNode.transform.localScale.x);
105 
106 
107  }
108 }