IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
OpacityControl.cs
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 using UnityEngine.UI;
5 
6 public class OpacityControl : MonoBehaviour
7 {
8 
9  public GameObject defaultLine;
10 
11  private MeshLoader mMeshLoader;
12 
13  void OnEnable()
14  {
15  // Register event callbacks for MESH events:
16  PatientEventSystem.startListening(PatientEventSystem.Event.MESH_LoadedAll, createContent);
17  PatientEventSystem.startListening(PatientEventSystem.Event.PATIENT_Closed, clearContent);
18 
19  mMeshLoader = GameObject.Find("GlobalScript").GetComponent<MeshLoader>();
20  defaultLine.SetActive(false);
21  if (mMeshLoader.MeshGameObjectContainers.Count != 0)
22  {
23  createContent();
24  }
25  }
26 
27  void OnDisable()
28  {
29  // Unregister myself - no longer receives events (until the next OnEnable() call):
30  PatientEventSystem.stopListening(PatientEventSystem.Event.MESH_LoadedAll, createContent);
31  PatientEventSystem.stopListening(PatientEventSystem.Event.PATIENT_Closed, clearContent);
32  }
33 
34 
35 
36  private void createContent(object obj = null)
37  {
38  clearContent();
39 
40  foreach (GameObject g in mMeshLoader.MeshGameObjectContainers)
41  {
42  // Create a new instance of the list button:
43  GameObject newLine = Instantiate(defaultLine).gameObject;
44  newLine.SetActive(true);
45 
46  // Attach the new button to the list:
47  newLine.transform.SetParent(defaultLine.transform.parent, false);
48 
49  //Save game object in slider
50  GameObject slider = newLine.transform.Find("Slider").gameObject;
51  slider.GetComponent<OpacitySlider> ().gameObjectToChangeOpacity = g;
52 
53  // Change button text to name of tool:
54  GameObject textObject = newLine.transform.Find("Text").gameObject;
55  Text buttonText = textObject.GetComponent<Text>();
56  if (g.name.Substring (0, 2) == "ME") {
57  buttonText.text = g.name.Substring (2, g.name.Length - 2);
58  } else {
59  buttonText.text = g.name;
60  }
61  }
62  }
63 
64  private void clearContent(object obj = null)
65  {
66  //Destroy all object except for default line
67  for (int i = 0; i < defaultLine.transform.parent.childCount; i++)
68  {
69  if (i != 0) //TODO !=0
70  {
71  Destroy(defaultLine.transform.parent.GetChild(i).gameObject);
72  }
73  }
74  }
75 
76 }