IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
Annotation.cs
1 using UnityEngine;
2 using System.Collections;
3 using System;
4 using System.ComponentModel;
5 using UnityEngine.UI;
6 using UnityEngine.EventSystems;
7 using UI;
8 using System.Collections.Generic;
9 
10 public class Annotation : MonoBehaviour, IPointerHoverHandler{
11 
12  [DefaultValue ("")]
13  public string creator;
14  public DateTime creationDate;
15  //needs to be public so clone dont "loose" knowledge of his label ... but still child
16  public GameObject myAnnotationLabel;
17  public GameObject myAnnotationMesh;
18  public GameObject rotationObjects;
19  public GameObject translationObjects;
20  public GameObject scalingObjects;
21  public GameObject viveMoveObject;
22  public GameObject myAnnotationListEntry;
23  public AnnotationControl.AnnotationType myType;
24  private Color myColor;
25  private Color defaultColor;
26 
27  public Material defaultMaterial, previewMaterial;
28  public Collider myCollider;
29  public float maxDistanceToMeshNode = 12f;
30 
31  public float MoveCircleRadiant = 0.5f;
32  private bool rotateWithVive;
33 
34  // Use this for initialization
35  void Start () {
36  creationDate = DateTime.Now;
37  }
38 
39  void OnEnable () {
40  defaultColor = new Color (defaultMaterial.color.r, defaultMaterial.color.g, defaultMaterial.color.b);
41  defaultMaterial = Instantiate (defaultMaterial);
42  previewMaterial = Instantiate (previewMaterial);
43  }
44 
45  //Used to create new Label
46  public void CreateLabel (GameObject annotationLabelMaster) {
47  //Create Label
48  myAnnotationLabel = (GameObject)Instantiate (annotationLabelMaster, new Vector3 (0f, 0f, 15f), this.transform.localRotation);
49  myAnnotationLabel.transform.localScale = new Vector3 (0.05f, 0.05f, 0.05f); //*= meshNode.transform.localScale.x; //x,y,z are the same
50  myAnnotationLabel.transform.SetParent (this.transform, false);
51  myAnnotationLabel.SetActive (true);
52  myAnnotationLabel.GetComponent<AnnotationLabel> ().setLabelText (annotationLabelMaster.GetComponent<AnnotationLabel> ().getLabelText ());
53 
54  }
55 
56  //Updates the Label Text
57  public void setLabeText (String newLabel) {
58 
59  if (myAnnotationLabel == null) {
60  Debug.LogError ("Annotation has no AnnotationLabel to edit");
61  return;
62  }
63  // Change label text:
64  myAnnotationLabel.GetComponent<AnnotationLabel> ().setLabelText (newLabel);
65  }
66 
67 
73  public void updatePosition (Quaternion rotation, Vector3 position) {
74  this.transform.localPosition = position;
75  this.transform.localRotation = rotation;
76  }
77 
78  //to get AnnotationLabel
79  public GameObject getLabel () {
80  return myAnnotationLabel;
81  }
82 
83  //to get Annotation Label text
84  public String getLabelText () {
85  return myAnnotationLabel.GetComponent<AnnotationLabel> ().getLabelText ();
86  }
87 
88  //to get Color
89  public Color getColor () {
90  return new Color (myColor.r, myColor.g, myColor.b, myAnnotationMesh.GetComponent<MeshRenderer> ().material.color.a);
91  }
92 
93  //Used to save Label Changes
94  public void saveLabelChanges () {
95  if (myAnnotationListEntry != null) {
96  myAnnotationListEntry.GetComponent<AnnotationListEntry> ().updateLabel (getLabelText ());
97  }
98  AnnotationControl.instance.updatePatientAnnotationList ();
99  }
100 
101  //used to change color of Annotation
102  public void changeColor (Color newColor) {
103  myColor = new Color (newColor.r, newColor.g, newColor.b, myAnnotationMesh.GetComponent<MeshRenderer> ().material.color.a);
104  Material[] mats = myAnnotationMesh.GetComponent<MeshRenderer> ().materials;
105  for (int i = 0; i < mats.GetLength (0); i++) {
106  mats [i].color = myColor;
107  }
108  myAnnotationMesh.GetComponent<MeshRenderer> ().materials = mats;
109  }
110 
111  //used to change color of Annotation
112  public void setDefaultColor () {
113  changeColor (new Color (defaultColor.r, defaultColor.g, defaultColor.b, myAnnotationMesh.GetComponent<Renderer> ().material.color.a));
114  }
115 
119  public void makeTransperent (float alpha) {
120  Material[] mats = myAnnotationMesh.GetComponent<MeshRenderer> ().materials;
121  if(alpha == 1f) {
122  //make Opaque
123  for (int i = 0; i < mats.GetLength (0); i++) {
124  Color pinColor = mats [i].color;
125  pinColor.a = 1f;
126  mats [i] = defaultMaterial;
127  mats [i].color = pinColor;
128  }
129  myAnnotationMesh.GetComponent<MeshRenderer> ().materials = mats;
130  return;
131  }
132  for (int i = 0; i < mats.GetLength (0); i++) {
133  Color pinColor = mats [i].color;
134  pinColor.a = alpha;
135  mats [i] = previewMaterial;
136  mats [i].color = pinColor;
137  }
138  myAnnotationMesh.GetComponent<MeshRenderer> ().materials = mats;
139  }
140 
144  public void setDefaultTransparency () {
145  if(myType == AnnotationControl.AnnotationType.sphere) {
146  makeTransperent (AnnotationControl.instance.defaultTransperency);
147  return;
148  }
149  if(myType == AnnotationControl.AnnotationType.plane) {
150  makeTransperent (AnnotationControl.instance.defaultTransperency);
151  return;
152  }
153  if(myType == AnnotationControl.AnnotationType.pin) {
154  //make Opaque
155  Material[] mats = myAnnotationMesh.GetComponent<MeshRenderer> ().materials;
156  for (int i = 0; i < mats.GetLength (0); i++) {
157  Color pinColor = mats [i].color;
158  pinColor.a = 1f;
159  mats [i] = defaultMaterial;
160  mats [i].color = pinColor;
161  }
162  myAnnotationMesh.GetComponent<MeshRenderer> ().materials = mats;
163  return;
164  }
165 
166  }
167 
168  //Disables MeshCollider
169  public void disableMeshCollider () {
170  myCollider.enabled = false;
171  }
172 
173  public void enableAllCollider (bool enable) {
174  myCollider.enabled = enable;
175  myAnnotationLabel.GetComponent<AnnotationLabel> ().myBoxCollider.enabled = enable;
176  }
177 
178  //Transfers all Settings from old Annotation in this one (Label color etc.)
179  public void transferAnnotationSettings (GameObject settingSource) {
180  changeColor (settingSource.GetComponent<Annotation> ().getColor ());
181  setLabeText (settingSource.GetComponent<Annotation> ().getLabelText ());
182  }
183 
184 
189  public void setMovementMeshsActive (bool active) {
190  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
191 
192  if(active) {
193  makeTransperent (0.4f);
194  changeAnnotationLabelLayer ("UIAnnotationEdit");
195  GameObject.Find("GlobalScript").GetComponent<HierarchicalInputModule>().disableLayer("UIOrgans");
196  } else {
198  GameObject.Find("GlobalScript").GetComponent<HierarchicalInputModule>().enableLayer("UIOrgans");
199  changeAnnotationMeshLayer ("UIOrgans");
200  changeAnnotationLabelLayer ("UIOrgans");
201  }
202 
203  if (inputDevice.getDeviceType () == InputDeviceManager.InputDeviceType.Mouse) {
204  if (rotationObjects != null) {
205  rotationObjects.SetActive (active);
206  }
207  if (translationObjects != null) {
208  translationObjects.SetActive (active);
209  }
210  if (scalingObjects != null) {
211  scalingObjects.SetActive (active);
212  }
213  } else if (inputDevice.getDeviceType () == InputDeviceManager.InputDeviceType.ViveController) {
214  changeAnnotationMeshLayer ("UIAnnotationEdit");
215  if(viveMoveObject != null) {
216  viveMoveObject.SetActive (active);
217  }
218  }
219  }
220 
221  public void OnPointerHover (UnityEngine.EventSystems.PointerEventData eventData) {
222  //Debug.Log ("Hover");
223  if(viveMoveObject == null) {
224  return;
225  }
226  if(eventData.pointerEnter != myAnnotationMesh) {
227  return;
228  }
229  InputDevice inputDevice = InputDeviceManager.instance.currentInputDevice;
230  if (inputDevice.getDeviceType () == InputDeviceManager.InputDeviceType.ViveController) {
231  Vector3 localEventPos = transform.InverseTransformPoint (eventData.pointerCurrentRaycast.worldPosition);
232  GameObject trans = viveMoveObject.transform.GetChild (0).gameObject;
233  GameObject rot = viveMoveObject.transform.GetChild (1).gameObject;
234  viveMoveObject.transform.position = eventData.pointerCurrentRaycast.worldPosition;
235  if(myType == AnnotationControl.AnnotationType.sphere) {
236  //move or rotate with sphere
237  Vector3 localDevicePos = transform.InverseTransformPoint (inputDevice.getEventCamera().transform.position);
238  float angle = Vector3.Angle (localEventPos, localDevicePos);
239  if(angle > 30) {
240  rotateWithVive = true;
241  trans.SetActive (false);
242  rot.SetActive (true);
243  } else {
244  rotateWithVive = false;
245  rot.SetActive (false);
246  trans.SetActive (true);
247  }
248  } else if(myType == AnnotationControl.AnnotationType.plane) {
249  //move or rotate with plane
250  float minScale = Mathf.Min (myAnnotationMesh.transform.localScale.x, myAnnotationMesh.transform.localScale.y) / 2f;
251  float distance = (localEventPos - myAnnotationMesh.transform.localPosition).magnitude * 1f/minScale;
252 
253  if(distance > MoveCircleRadiant) {
254  rotateWithVive = true;
255  trans.SetActive (false);
256  rot.SetActive (true);
257  } else {
258  rotateWithVive = false;
259  rot.SetActive (false);
260  trans.SetActive (true);
261  }
262  }
263  }
264 
265  }
266 
267  public void endHover() {
268  if(viveMoveObject == null) {
269  return;
270  }
271  viveMoveObject.transform.GetChild (0).gameObject.SetActive(false);
272  viveMoveObject.transform.GetChild (1).gameObject.SetActive(false);
273  }
274 
275  //rescaling AnnotationMesh
276  public void rescaleMesh (Vector3 newScale) {
277  if(scalingObjects != null) {
278  myAnnotationMesh.transform.localScale = newScale;
279  }
280  }
281 
282  public Vector3 getMeshScale() {
283  return myAnnotationMesh.transform.localScale;
284  }
285 
286  //rotate Annotation Mesh with given Rotation in world space
287  public void rotateMesh(Quaternion rotation) {
288  myAnnotationMesh.transform.rotation *= rotation;
289  }
290 
291  public Quaternion getMeshRotation() {
292  return myAnnotationMesh.transform.rotation;
293  }
294 
295  //Moves Annotation to given Position in WorldSpace
296  public void translateMesh(Vector3 newPos) {
297  //world Space
298  this.transform.position = newPos;
299 
300  }
301 
302  public void changeAnnotationMeshLayer(String layer) {
303  myAnnotationMesh.layer = LayerMask.NameToLayer (layer);
304  }
305 
306  private void changeAnnotationLabelLayer(String layer) {
307  myAnnotationLabel.layer = LayerMask.NameToLayer (layer);
308  }
309 
310  public bool LabelClicked(PointerEventData data) {
311  return AnnotationControl.instance.annotationClicked (data);
312  }
313 
314  public void AnnotationClicked (BaseEventData eventData) {
315  PointerEventData data = eventData as PointerEventData;
316  if (AnnotationControl.instance.annotationClicked (data)) {
317  myAnnotationMesh.GetComponent<AnnotationMoveVive> ().AnnotationClicked (data, rotateWithVive);
318  }
319  }
320 
321  public void saveAnnotationChanges() {
322  AnnotationControl.instance.updatePatientAnnotationList ();
323  }
324 
325  public void destroyAnnotation() {
326  //Destroy Label
327 
328  if (myAnnotationLabel != null)
329  {
330  Destroy(myAnnotationLabel);
331  }
332  }
333 
334  public void ViveMovement(bool active) {
335  viveMoveObject.SetActive (!active);
336  if(active) {
337  makeTransperent (1f);
338  } else {
339  makeTransperent (0.4f);
340  }
341  }
342 }
void setDefaultTransparency()
Reste Transparency of Annotation
Definition: Annotation.cs:144
bool annotationClicked(PointerEventData data)
Called by Annotation when Clicked on Annotation
void makeTransperent(float alpha)
Makes the Annotation transperent.
Definition: Annotation.cs:119
void updatePosition(Quaternion rotation, Vector3 position)
Updates the position and Rotation in local Coordinates.
Definition: Annotation.cs:73
void setMovementMeshsActive(bool active)
Sets the movement meshs active.
Definition: Annotation.cs:189