IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
PatientSelector.cs
1 using UnityEngine;
2 using UnityEngine.UI;
3 using System.Collections;
4 using UI;
5 
6 public class PatientSelector : MonoBehaviour {
7 
8  public Sprite SpriteLiver = null;
9  public Sprite SpriteBone = null;
10  public Sprite SpriteBrain = null;
11  public Sprite SpritePancreas = null;
12  public Sprite SpriteIntestine = null;
13  public Sprite SpriteUnknown = null;
14 
15  private GameObject mScrollView;
16 
17  private int notificationID;
18 
19  // Use this for initialization
20  void Start () {
21 
22  // Get the scroll view defined for the widget:
23  mScrollView = transform.Find("Background/Scroll View").gameObject;
24  // Disable the default button:
25  defaultPatientButton = mScrollView.transform.Find("Viewport/Content/ButtonPatient").gameObject;
26  defaultPatientButton.SetActive(false);
27 
28  PatientEventSystem.startListening (
29  PatientEventSystem.Event.PATIENT_NewPatientDirectoryFound,
30  addPatientEntry
31  );
32 
33  PatientDirectoryLoader.setPath("../Patients/");
34 
35  }
36  public void OnEnable()
37  {
38  notificationID = UI.Core.instance.addIndication ( GetComponent<Widget>().layoutScreen, "Please choose patient");
39  }
40  public void OnDisable()
41  {
42  UI.Core.instance.clearIndication (notificationID);
43  }
44 
45  void ChoosePatient( int index )
46  {
47  // TODO make singleton? Unload any previous patient.
48  //mPatientLoader.loadPatient(index);
49  PatientDirectoryLoader.loadPatient(index);
50  //PatientCache.instance.openPatient(index);
51  }
52 
53  void addPatientEntry( object obj = null )
54  {
55 
56  // Remove all entries in the list:
57  foreach(Transform child in defaultPatientButton.transform.parent) {
58  //Debug.Log (child.name);
59  // Remove all buttons except for the default button:
60  if( child.gameObject.activeSelf ) {
61  Destroy (child.gameObject);
62  }
63  }
64 
65  // Add all new entries:
66  for( int index = 0; index < PatientDirectoryLoader.getCount(); index ++ )
67  {
68  PatientMeta patient = PatientDirectoryLoader.getEntry(index);
69 
70  // Create a new instance of the list button:
71  GameObject newButton = Instantiate(defaultPatientButton);
72  newButton.SetActive(true);
73 
74  // Attach the new button to the list:
75  newButton.transform.SetParent(defaultPatientButton.transform.parent, false );
76 
77  // Fill button's text object:
78  Text t = newButton.transform.Find("Text").GetComponent<Text>();
79  t.text = patient.name + "\n <color=#DDDDDD>" + patient.birthDate + "</color>";
80 
81  newButton.transform.Find ("ImageFemale").gameObject.SetActive (false);
82  newButton.transform.Find ("ImageMale").gameObject.SetActive (false);
83  if (patient.sex == "f") {
84  newButton.transform.Find ("ImageFemale").gameObject.SetActive (true);
85  } else if (patient.sex == "m") {
86  newButton.transform.Find ("ImageMale").gameObject.SetActive (true);
87  }
88 
89  Text ageText = newButton.transform.Find("AgeText").GetComponent<Text>();
90  if (patient.age >= 0) {
91  ageText.text = patient.age + " a";
92  } else {
93  ageText.text = "";
94  }
95 
96  Text detailsText = newButton.transform.Find("TextDetails").GetComponent<Text>();
97  detailsText.text = patient.diagnosis + "\n <color=#DDDDDD>" + patient.details + "</color>";
98 
99  Image operationTypeImage = newButton.transform.Find ("IconBackground/OperationTypeImage").GetComponent<Image> ();
100  if (operationTypeImage != null) {
101  operationTypeImage.sprite = spriteForOperatedBodyPart (patient.operationBodyPart);
102  } else {
103  operationTypeImage.gameObject.SetActive (false);
104  }
105 
106  if (patient.warnings.Count > 0) {
107  string warnings = "";
108  for (int i = 0; i < patient.warnings.Count; i++) {
109  warnings = warnings + patient.warnings [i] + "\n";
110  }
111  Text warningsText = newButton.transform.Find("TextWarnings").GetComponent<Text>();
112  warningsText.text = warnings;
113  }
114 
115  // Set up events:
116  int capturedIndex = index;
117  Button b = newButton.GetComponent<Button>();
118  b.onClick.AddListener(() => ChoosePatient(capturedIndex));
119  b.onClick.AddListener (() => gameObject.SetActive (false));
120  }
121 
122  /*RectTransform rectTf = defaultPatientButton.transform.parent.GetComponent<RectTransform>();
123  RectTransform buttonRectTF = defaultPatientButton.transform.GetComponent<RectTransform>();
124  float newWidth = PatientDirectoryLoader.getCount () * (buttonRectTF.rect.width + 2.0f);
125  rectTf.SetSizeWithCurrentAnchors (RectTransform.Axis.Horizontal, newWidth);*/
126 
127  // Set the scroll view position:
128  //Vector2 currentScrollPos = mScrollView.GetComponent<ScrollRect>().normalizedPosition;
129  //mScrollView.GetComponent<ScrollRect>().normalizedPosition = new Vector2(0,currentScrollPos.y);
130 
131  }
132 
133  Sprite spriteForOperatedBodyPart( PatientMeta.OperationBodyPart ot )
134  {
135  if (ot == PatientMeta.OperationBodyPart.Liver) {
136  return SpriteLiver;
137  } else if (ot == PatientMeta.OperationBodyPart.Bone) {
138  return SpriteBone;
139  } else if (ot == PatientMeta.OperationBodyPart.Brain) {
140  return SpriteBrain;
141  } else if (ot == PatientMeta.OperationBodyPart.Pancreas) {
142  return SpritePancreas;
143  } else if (ot == PatientMeta.OperationBodyPart.Intestine) {
144  return SpriteIntestine;
145  } else {
146  return SpriteUnknown;
147  }
148  }
149 
150  private GameObject defaultPatientButton = null;
151 }