IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
ViewControl.cs
1 using UnityEngine;
2 using System;
3 using System.Collections;
4 using System.Collections.Generic;
5 using UnityEngine.UI;
6 
7 public class ViewControl : MonoBehaviour {
8 
9  public GameObject mainPane;
10  public GameObject editPane;
11  public GameObject viewNameInputField;
12  public Color colorInactive;
13  public Color colorActive;
14 
15  public MeshLoader mMeshLoader;
16  public Button newButton, deleteButton;
17  public Text viewNameText;
18  public Button buttoPrev, buttonNext;
19  public GameObject viewCountElement;
20 
21  public GameObject meshViewerScaleNode, meshViewerRotationNode;
22 
23  private int currentViewIndex = 0;
24 
25  // Use this for initialization
26  void Start () {
27  viewCountElement.SetActive (false);
28 
29  //meshViewerScaleNode = GameObject.Find ("MeshViewerBase/MeshViewerScale");
30  //meshViewerRotationNode = GameObject.Find ("MeshViewerBase/MeshViewerScale/MeshRotationNode");
31 
32  patientClosed ();
33  showMainPane ();
34 
35  Patient p = Patient.getLoadedPatient ();
36  if (p != null) {
37  meshLoaded ();
38  }
39  }
40 
41  void OnEnable()
42  {
43  // Register event callbacks for MESH events:
44  PatientEventSystem.startListening(PatientEventSystem.Event.MESH_LoadedSingle, meshLoaded);
45  PatientEventSystem.startListening(PatientEventSystem.Event.PATIENT_Closed, patientClosed);
46 
47  mMeshLoader = GameObject.Find("GlobalScript").GetComponent<MeshLoader>();
48 
49  activate ();
50  }
51 
52  void OnDisable()
53  {
54  // Unregister myself - no longer receives events (until the next OnEnable() call):
55  PatientEventSystem.stopListening(PatientEventSystem.Event.MESH_LoadedSingle, meshLoaded);
56  PatientEventSystem.stopListening(PatientEventSystem.Event.PATIENT_Closed, patientClosed);
57  }
58 
59  public void meshLoaded( object obj = null )
60  {
61  activate ();
62  }
63  public void activate()
64  {
65  Debug.Log ("Activate View Control");
66  newButton.interactable = true;
67  currentViewIndex = 0;
68  setView (currentViewIndex);
69  }
70  public void patientClosed( object obj = null )
71  {
72  deleteButton.interactable = false;
73  newButton.interactable = false;
74  viewNameText.text = "No patient loaded.";
75  }
76 
77  public void showMainPane()
78  {
79  editPane.SetActive (false);
80  mainPane.SetActive (true);
81  }
82  public void showEditPane()
83  {
84  mainPane.SetActive (false);
85  editPane.SetActive (true);
86  }
87  public void nextView()
88  {
89  setView (currentViewIndex + 1);
90  }
91  public void prevView()
92  {
93  setView (currentViewIndex - 1);
94  }
95 
96  public void insertNewView()
97  {
98  Patient p = Patient.getLoadedPatient ();
99  if (p != null) {
100  string t = viewNameInputField.GetComponent<InputField> ().text;
101  if (t.Length > 0) {
102  if (mMeshLoader.MeshGameObjectContainers.Count != 0) {
103  //createContent();
104  }
105  View newView = new View ();
106  newView.name = t;
107  newView.orientation = meshViewerRotationNode.transform.localRotation;
108  newView.scale = meshViewerScaleNode.transform.localScale;
109  //newView.opacities = new Dictionary<string,double> ();
110 
111  foreach (GameObject g in mMeshLoader.MeshGameObjectContainers) {
112  MeshRenderer mr = g.GetComponentInChildren<MeshRenderer> ();
113  if (g.activeSelf) {
114  newView.opacities [g.name] = mr.material.color.a;
115  } else {
116  newView.opacities [g.name] = 0.0f;
117  }
118  }
119 
120  currentViewIndex = p.insertView ( newView, currentViewIndex + 1 );
121  setView (currentViewIndex);
122 
123  //p.saveViews ();
124 
125  showMainPane ();
126  }
127  }
128  }
129 
130  public void deleteView()
131  {
132  Patient p = Patient.getLoadedPatient ();
133  if (p != null) {
134  p.deleteView (currentViewIndex);
135  //p.saveViews ();
136 
137  if (p.getViewCount() <= currentViewIndex) {
138  currentViewIndex = p.getViewCount() - 1;
139  }
140  }
141  setView (currentViewIndex, false);
142  }
143 
144  void setView( int index, bool animate = true )
145  {
146  Patient p = Patient.getLoadedPatient ();
147  if (p != null) {
148 
149  if (p.getViewCount () == 0) {
150  viewNameText.text = "No views configured.";
151  } else {
152 
153  View view = p.getView (index);
154  if (view != null) {
155  viewNameText.text = (index + 1).ToString ();
156  viewNameText.text += ": ";
157  viewNameText.text += view.name;
158 
159  if ( animate ) {
160  // Slowly zoom and rotate towards the target:
161  meshViewerScaleNode.GetComponent<ModelZoomer> ().setTargetZoom (view.scale, 0.8f);
162  meshViewerRotationNode.GetComponent<ModelRotator> ().setTargetOrientation (view.orientation, 0.8f);
163  } else {
164  meshViewerScaleNode.GetComponent<ModelZoomer> ().setTargetZoom (view.scale);
165  meshViewerRotationNode.GetComponent<ModelRotator> ().setTargetOrientation (view.orientation);
166  }
167 
168  foreach (KeyValuePair<string, double> entry in view.opacities) {
169  setMeshOpacity (entry.Key, (float)entry.Value);
170  }
171 
172  currentViewIndex = index;
173  }
174  }
175 
176  PatientEventSystem.triggerEvent (PatientEventSystem.Event.MESH_Opacity_Changed);
177 
178  updateViewCount ();
179 
180  if( p.getViewCount () > 0 )
181  deleteButton.interactable = true;
182  else
183  deleteButton.interactable = false;
184  }
185  }
186 
187  void setMeshOpacity( string name, float opacity )
188  {
189  // First, find the GameObject which holds the mesh given by "name"
190  GameObject gameObjectToChangeOpacity = null;
191  foreach (GameObject g in mMeshLoader.MeshGameObjectContainers) {
192  if (g.name == name) {
193  gameObjectToChangeOpacity = g;
194  break;
195  }
196  }
197 
198  // If we found such a GameObject, then set the opacity for all it's children (the meshes):
199  if (gameObjectToChangeOpacity != null) {
200  MeshMaterialControl moc = gameObjectToChangeOpacity.GetComponent<MeshMaterialControl> ();
201  if (moc != null) {
202  moc.changeOpactiyOfChildren (opacity);
203  }
204  }
205  }
206 
207  void updateViewCount()
208  {
209  Patient p = Patient.getLoadedPatient ();
210  if (p != null) {
211  int elementsToShow = Math.Max( p.getViewCount (), 1 );
212 
213  if (viewCountElement.transform.parent.childCount < elementsToShow + 1 ) {
214  int toCreate = elementsToShow - (viewCountElement.transform.parent.childCount - 1);
215  for (int i = 0; i < toCreate; i++) {
216  GameObject newElem = Instantiate (viewCountElement);
217  newElem.transform.SetParent (viewCountElement.transform.parent, false);
218  newElem.SetActive (true);
219  }
220  } else if( viewCountElement.transform.parent.childCount > elementsToShow + 1 ) {
221  // Remove the last element until we have the correct amount of elements:
222  int toRemove = (viewCountElement.transform.parent.childCount - 1) - elementsToShow;
223  for (int i = 0; i < toRemove; i++) {
224  Transform tf = viewCountElement.transform.parent.GetChild (viewCountElement.transform.parent.childCount - 1 - i);
225  if( tf != null )
226  Destroy( tf.gameObject );
227  }
228  }
229 
230  foreach (Transform tf in viewCountElement.transform.parent) {
231  tf.GetComponent<Image> ().color = colorInactive;
232  }
233 
234  // Highlight the currently active view:
235  Transform t = viewCountElement.transform.parent.GetChild( currentViewIndex + 1 );
236  t.GetComponent<Image>().color = colorActive;
237  }
238  }
239 }
Definition: View.cs:9