2 using System.Collections;
4 using UnityEngine.EventSystems;
5 using System.Collections.Generic;
20 public AnnotationControl.AnnotationType type;
30 public double PositionX;
31 public double PositionY;
32 public double PositionZ;
34 public double RotationW;
35 public double RotationX;
36 public double RotationY;
37 public double RotationZ;
39 public double MeshRotationW;
40 public double MeshRotationX;
41 public double MeshRotationY;
42 public double MeshRotationZ;
45 public string Creator;
46 public DateTime CreationDate;
53 public GameObject annotationPinGroupObj;
54 public GameObject annotationPlaneGroupObj;
55 public GameObject annotationSphereGroupObj;
57 public GameObject annotationLabel;
58 public GameObject annotationListEntry;
59 public GameObject annotationToolBar;
66 public GameObject ListScreen;
67 public GameObject AddEditScreen;
70 public GameObject instructionText;
71 public GameObject annotationSettings;
73 public GameObject meshNode;
74 public GameObject meshPositionNode;
77 private ActiveScreen currentActiveScreen = ActiveScreen.none;
80 private AnnotationType currentAnnotationType;
81 private GameObject currentAnnotationListEntry = null;
83 private List<GameObject> annotationListEntryList =
new List<GameObject> ();
84 private GameObject previewAnnotation;
89 public float previewTransparency = 0.2f;
90 public float editAnnotationTransparency = 0.2f;
91 public float defaultTransperency = 0.4f;
92 public float organTransparency = 0.4f;
99 private enum ActiveScreen
106 public enum AnnotationType {
114 if( instance != null )
115 throw(
new System.Exception (
"Error: Cannot create more than one instance of ANNOTATIONCONTROL!"));
123 AddEditScreen.SetActive (
false);
124 ListScreen.SetActive (
false);
125 annotationToolBar.SetActive (
true);
126 currentActiveScreen = ActiveScreen.none;
128 setAllAnnotationsActive (
true);
130 annotationLabel.SetActive (
false);
131 annotationPinGroupObj.SetActive (
false);
132 annotationPlaneGroupObj.SetActive (
false);
133 annotationSphereGroupObj.SetActive (
false);
134 annotationListEntry.gameObject.SetActive (
false);
136 clickNotifier = meshPositionNode.AddComponent<
ClickNotifier> ();
137 clickNotifier.clickNotificationEvent = OnMeshClicked;
138 clickNotifier.hoverNotificationEvent = hoveredOverMesh;
139 clickNotifier.exitNotificationEvent = pointerExitMesh;
146 Destroy (clickNotifier);
154 if (annotationPinGroupObj == null) {
155 Debug.LogError (
"No Annotation Pin Object is set in AnnotationControl.cs");
157 if (annotationPlaneGroupObj == null) {
158 Debug.LogError (
"No Annotation Plane Object is set in AnnotationControl.cs");
160 if (annotationSphereGroupObj == null) {
161 Debug.LogError (
"No Annotation Sphere Object is set in AnnotationControl.cs");
170 public void AddAnnotationPressed ()
172 if (currentActiveScreen == ActiveScreen.add) {
173 closeAnnotationScreen ();
176 if (currentAnnotationListEntry == null) {
177 instructionText.SetActive (
true);
179 currentAnnotationType = AnnotationType.pin;
180 createPreviewAnnotation ();
181 openAnnotationScreen ();
188 public void ToggleAnnotationsList ()
190 if (currentActiveScreen == ActiveScreen.list) {
192 ListScreen.SetActive (
false);
193 currentActiveScreen = ActiveScreen.none;
196 closeAnnotationScreen ();
197 ListScreen.SetActive (
true);
198 currentActiveScreen = ActiveScreen.list;
202 public void ChangeColorPressed (GameObject newColorButton)
204 if (currentAnnotationListEntry != null) {
206 newColorButton.GetComponent<Button> ().colors.normalColor);
208 if (previewAnnotation != null) {
209 previewAnnotation.GetComponent<
Annotation> ().changeColor (
210 newColorButton.GetComponent<Button> ().colors.normalColor);
212 updatePatientAnnotationList ();
215 public void changeAnnoTypeToPin () {
216 changeCurrentAnnotationType(AnnotationType.pin);
219 public void changeAnnoTypeToPlane () {
220 changeCurrentAnnotationType(AnnotationType.plane);
223 public void changeAnnoTypeToSphere () {
224 changeCurrentAnnotationType(AnnotationType.sphere);
230 public void OnMeshClicked (PointerEventData eventData)
232 if (eventData.button != PointerEventData.InputButton.Left) {
236 if (currentActiveScreen == ActiveScreen.add) {
237 Vector3 localpos = meshPositionNode.transform.InverseTransformPoint (eventData.pointerPressRaycast.worldPosition);
238 Vector3 localNormal = meshPositionNode.transform.InverseTransformDirection (eventData.pointerPressRaycast.worldNormal);
239 if (currentAnnotationListEntry == null) {
240 GameObject newAnnotation = createAnnotationGroup (currentAnnotationType, Quaternion.LookRotation (localNormal), localpos);
242 EditAnnotation (createNewAnnotationListEntry (newAnnotation));
243 }
else if (currentAnnotationType == AnnotationType.pin){
244 changeAnnotationPosition (Quaternion.LookRotation (localNormal), localpos);
252 public void hoveredOverMesh (PointerEventData eventData)
254 if (previewAnnotation != null) {
255 if(!eventData.pointerEnter.gameObject.CompareTag(
"AnnotationLabel") && !eventData.pointerEnter.gameObject.CompareTag(
"Annotation")) {
256 previewAnnotation.SetActive (
true);
257 Vector3 localpos = meshPositionNode.transform.InverseTransformPoint (eventData.pointerCurrentRaycast.worldPosition);
258 Vector3 localNormal = meshPositionNode.transform.InverseTransformDirection (eventData.pointerCurrentRaycast.worldNormal);
259 previewAnnotation.GetComponent<
Annotation> ().updatePosition (Quaternion.LookRotation (localNormal), localpos);
261 previewAnnotation.SetActive (
false);
267 public void pointerExitMesh (PointerEventData eventData)
269 if (previewAnnotation != null) {
270 previewAnnotation.SetActive (
false);
277 private void setAllAnnotationsActive(
bool active) {
278 foreach(GameObject g
in annotationListEntryList) {
280 setAnnotationActive (g, active);
285 private void enableAllAnnotationCollider(
bool enable) {
287 if(annotationListEntryList == null || annotationListEntryList.Count == 0) {
290 foreach(GameObject g
in annotationListEntryList) {
302 private void setAnnotationActive(GameObject aListentry,
bool active) {
306 private void setCurrentAnnotationListEntry(GameObject anno) {
307 currentAnnotationListEntry = anno;
308 GetComponent<LabelPositioner> ().forceRecalculate ();
312 private void resetCurrentAnnotation() {
313 currentAnnotationListEntry = null;
318 private void disableOrgans() {
319 GameObject.Find (
"GlobalScript").GetComponent<HierarchicalInputModule> ().disableLayer (
"MeshViewer");
320 meshPositionNode.GetComponent<
OpacityOfAllChanger> ().changeOpacityofAll (organTransparency);
323 private void enableOrgans() {
324 GameObject.Find (
"GlobalScript").GetComponent<HierarchicalInputModule> ().enableLayer (
"MeshViewer");
328 private void makeAnnotationsTransparent() {
329 foreach(GameObject g
in annotationListEntryList) {
330 if(g != currentAnnotationListEntry) {
331 g.GetComponent<
AnnotationListEntry> ().makeAnnotationTransparent (editAnnotationTransparency);
336 private void resetAnnotationTransparency() {
337 foreach(GameObject g
in annotationListEntryList) {
338 if(g != currentAnnotationListEntry) {
344 private void changeCurrentAnnotationType(AnnotationType newType) {
345 if(currentAnnotationListEntry == null) {
346 currentAnnotationType = newType;
347 createPreviewAnnotation ();
349 changeAnnoTypeTo (currentAnnotationListEntry, newType);
350 updateListInLabelPositioner ();
351 EditAnnotation (currentAnnotationListEntry);
355 private void changeAnnoTypeTo(GameObject annotationGroup, AnnotationType newType) {
356 if(annotationGroup != null) {
357 currentAnnotationType = newType;
358 GameObject curAnnoGroup = annotationGroup.GetComponent<
AnnotationListEntry> ().getAnnotation ();
359 GameObject newAnnoGroup = createAnnotationGroup (newType, curAnnoGroup.GetComponent<Transform> ().localRotation, curAnnoGroup.GetComponent<Transform> ().localPosition);
360 annotationGroup.GetComponent<AnnotationListEntry> ().replaceMyAnnotationMesh (newAnnoGroup);
361 updatePatientAnnotationList ();
366 private void jumpToListEntry (GameObject annotation)
368 if (currentActiveScreen == ActiveScreen.list) {
369 GameObject listEntry = annotation.GetComponent<
Annotation> ().myAnnotationListEntry;
371 listEntry.transform.parent.GetComponent<RectTransform> ().anchoredPosition =
372 new Vector2 (0.0f, (-(pos.y) - (ListScreen.GetComponent<RectTransform> ().rect.height / 3)));
376 private void createPreviewAnnotation ()
379 GameObject annotationType = getAnnotationTypeObject (currentAnnotationType);
380 Color c =
new Color ();
381 bool newColor =
false;
383 if(previewAnnotation != null) {
384 c = previewAnnotation.GetComponent<
Annotation> ().getColor ();
385 Destroy (previewAnnotation);
388 previewAnnotation = (GameObject)Instantiate (annotationType);
390 previewAnnotation.transform.localScale =
new Vector3 (5, 5, 5);
391 previewAnnotation.transform.SetParent (meshPositionNode.transform,
false);
393 previewAnnotation.SetActive (
true);
394 previewAnnotation.GetComponent<
Annotation> ().makeTransperent (previewTransparency);
395 previewAnnotation.GetComponent<Annotation> ().disableMeshCollider ();
397 previewAnnotation.GetComponent<Annotation> ().changeColor (c);
399 previewAnnotation.GetComponent<Annotation> ().setDefaultColor ();
404 private GameObject getAnnotationTypeObject (AnnotationType type) {
405 GameObject annotationType;
407 case AnnotationType.pin:
408 annotationType = annotationPinGroupObj;
410 case AnnotationType.plane:
411 annotationType = annotationPlaneGroupObj;
413 case AnnotationType.sphere:
414 annotationType = annotationSphereGroupObj;
417 annotationType = annotationPinGroupObj;
420 return annotationType;
426 private GameObject createAnnotationGroup (AnnotationType annoType, Quaternion rotation, Vector3 position)
428 GameObject annotationType = getAnnotationTypeObject (annoType);
429 GameObject newAnnotation = (GameObject)Instantiate (annotationType, position, rotation);
431 newAnnotation.transform.localScale =
new Vector3 (5, 5, 5);
432 newAnnotation.transform.SetParent (meshPositionNode.transform,
false);
434 newAnnotation.SetActive (
true);
437 newAnnotation.GetComponent<
Annotation>().myType = annoType;
440 newAnnotation.GetComponent<
Annotation> ().CreateLabel (annotationLabel);
443 if(previewAnnotation != null) {
444 newAnnotation.GetComponent<Annotation> ().changeColor(previewAnnotation.GetComponent<Annotation>().getColor());
446 newAnnotation.GetComponent<Annotation> ().setDefaultColor ();
449 newAnnotation.GetComponent<Annotation> ().setDefaultTransparency ();
451 return newAnnotation;
455 public void closeAnnotationScreen ()
459 if(currentAnnotationListEntry != null) {
460 currentAnnotationListEntry.GetComponent<
AnnotationListEntry> ().setAnnotationMovementActive (
false);
462 resetCurrentAnnotation ();
463 Destroy (previewAnnotation);
464 previewAnnotation = null;
467 resetAnnotationTransparency();
468 instructionText.gameObject.SetActive (
true);
470 AddEditScreen.SetActive (
false);
471 currentActiveScreen = ActiveScreen.none;
474 updatePatientAnnotationList ();
479 private void openAnnotationScreen ()
482 makeAnnotationsTransparent();
483 ListScreen.SetActive (
false);
484 AddEditScreen.SetActive (
true);
485 currentActiveScreen = ActiveScreen.add;
489 private void changeAnnotationPosition (Quaternion rotation, Vector3 position)
491 currentAnnotationListEntry.GetComponent<
AnnotationListEntry> ().updateAnnotationposition (rotation, position);
492 updatePatientAnnotationList ();
496 private GameObject createNewAnnotationListEntry (GameObject annotation)
498 if (annotation != null) {
501 GameObject newEntry = Instantiate (annotationListEntry).gameObject;
502 newEntry.SetActive (
true);
505 newEntry.transform.SetParent (annotationListEntry.transform.parent,
false);
509 annotationListEntryList.Add (newEntry);
510 updateListInLabelPositioner ();
513 Debug.LogAssertion (
"Annotation is Null");
519 private void removeOneAnnotation (GameObject aListEntry)
522 if(currentAnnotationListEntry == aListEntry)
524 resetCurrentAnnotation ();
529 Destroy (aListEntry);
536 public bool isCurrentAnnotation(GameObject annotation) {
537 if(currentAnnotationListEntry == null) {
540 if(annotation == currentAnnotationListEntry.GetComponent<
AnnotationListEntry>().getAnnotation()) {
546 public void updateListInLabelPositioner() {
547 GetComponent<LabelPositioner> ().updateAnnotationList (getAnnotationList ());
550 public List<GameObject> getAnnotationList() {
551 List<GameObject> returnList =
new List<GameObject> ();
552 if(annotationListEntryList != null) {
553 foreach(GameObject g
in annotationListEntryList) {
560 public void resetLayers() {
561 GameObject inMod = GameObject.Find (
"GlobalScript");
573 if(currentAnnotationListEntry == data.pointerEnter.GetComponentInParent<
Annotation> ().myAnnotationListEntry) {
576 if (currentActiveScreen == ActiveScreen.list) {
577 jumpToListEntry (data.pointerEnter);
579 if (currentAnnotationListEntry != null || currentActiveScreen == ActiveScreen.add) {
580 closeAnnotationScreen ();
582 EditAnnotation (data.pointerEnter.GetComponentInParent<
Annotation> ().myAnnotationListEntry);
588 public void deleteAllAnnotations ()
590 foreach (GameObject g
in annotationListEntryList) {
592 removeOneAnnotation (g);
596 currentAnnotationListEntry = null;
598 Destroy (previewAnnotation);
599 previewAnnotation = null;
600 annotationListEntryList =
new List<GameObject> ();
601 updatePatientAnnotationList ();
606 public void clearAll ()
608 setAllAnnotationsActive (
false);
609 currentAnnotationListEntry = null;
611 Destroy (previewAnnotation);
612 previewAnnotation = null;
618 Quaternion rotation =
new Quaternion ((
float)annotation.RotationX, (
float)annotation.RotationY, (
float)annotation.RotationZ, (
float)annotation.RotationW);
619 Quaternion meshRotation =
new Quaternion ((
float)annotation.MeshRotationX, (
float)annotation.MeshRotationY, (
float)annotation.MeshRotationZ, (
float)annotation.MeshRotationW);
620 Vector3 position =
new Vector3 ((
float)annotation.PositionX, (
float)annotation.PositionY, (
float)annotation.PositionZ);
623 GameObject newAnnotation = createAnnotationGroup (annotation.type, rotation, position);
624 if(annotation.type == AnnotationType.plane || annotation.type == AnnotationType.sphere) {
625 newAnnotation.GetComponent<
Annotation> ().myAnnotationMesh.transform.localRotation = meshRotation;
628 newAnnotation.GetComponent<
Annotation> ().setLabeText (annotation.Text);
631 if (annotation.ColorR != 0.0f || annotation.ColorG != 0.0f || annotation.ColorB != 0.0f) {
632 newAnnotation.GetComponent<
Annotation> ().changeColor (
new Color (annotation.ColorR, annotation.ColorG, annotation.ColorB));
634 if(annotation.ScaleX == 0f) {
635 newAnnotation.GetComponent<
Annotation> ().rescaleMesh (
new Vector3 (1f, 1f, 1f));
637 newAnnotation.GetComponent<
Annotation> ().rescaleMesh (
new Vector3(annotation.ScaleX, annotation.ScaleY, annotation.ScaleZ));
640 createNewAnnotationListEntry (newAnnotation);
645 public void updatePatientAnnotationList()
647 Patient p = Patient.getLoadedPatient ();
649 p.updateAnnotationList (annotationListEntryList);
653 public void EditAnnotation (GameObject aListEntry)
655 if(currentAnnotationListEntry == null) {
656 setCurrentAnnotationListEntry (aListEntry);
658 if(currentAnnotationType == AnnotationType.plane) {
660 Destroy(previewAnnotation);
661 previewAnnotation = null;
663 }
else if(currentAnnotationType == AnnotationType.sphere) {
665 Destroy(previewAnnotation);
666 previewAnnotation = null;
669 createPreviewAnnotation ();
670 previewAnnotation.GetComponent<
Annotation> ().changeColor (
675 currentAnnotationListEntry.GetComponent<
AnnotationListEntry> ().setAnnotationMovementActive (
true);
676 instructionText.SetActive (
false);
677 openAnnotationScreen ();
681 public void DeleteAnnotation (GameObject aListEntry)
684 annotationListEntryList.Remove (aListEntry);
685 removeOneAnnotation (aListEntry);
686 updateListInLabelPositioner ();
688 updatePatientAnnotationList ();
bool annotationClicked(PointerEventData data)
Called by Annotation when Clicked on Annotation