IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
Core.cs
1 using UnityEngine;
2 using UnityEngine.EventSystems;
3 using System.Collections;
4 using System.Collections.Generic;
5 using UnityEngine.UI;
6 
7 namespace UI
8 {
9  public class Core : MonoBehaviour
10  {
12  public bool pointerIsOverUIObject{ private set; get; }
15  public bool pointerIsOverPlatformUIObject{ private set; get; }
16 
17  public static Core instance { private set; get; }
18 
19  public Camera UICamera;
20 
21  public LayoutSystem layoutSystem { private set; get; }
22 
24  public float UIScale = 0.0025f;
25  public float pixelsPerMeter = 500f;
26  public float aspectRatio = 1f;
27 
28  public Color TabHighlightColor;
29  public Color ButtonBaseColor;
30  public Color ButtonHighlightColor;
31 
32  public Sprite normalTabImage;
33  public Sprite selectedTabImage;
34 
35  public GameObject indicatorLeft;
36  public GameObject indicatorRight;
37 
39  public GameObject statusBar;
40  private GameObject closePatientButton;
41  private GameObject savePatientButton;
42 
43  private List<GameObject> activeIndicators = new List<GameObject> ();
44  private int indicationID = 0;
45 
46  public GameObject PatientSelector;
47 
48 
49  public Core()
50  {
51  if( instance != null )
52  throw(new System.Exception ("Error: Cannot create more than one instance of UI.Core!"));
53  instance = this;
54  }
55 
56  public void OnEnable()
57  {
58  layoutSystem = new LayoutSystem ();
59 
60  indicatorLeft.SetActive (false);
61  indicatorRight.SetActive (false);
62  statusBar.SetActive (true);
63 
64  Transform tf = statusBar.transform.Find ("ButtonClose");
65  if (tf != null) {
66  closePatientButton = tf.gameObject;
67  closePatientButton.GetComponent<Button> ().onClick.AddListener (() => closePatient ());
68  closePatientButton.SetActive (false);
69  } else {
70  Debug.LogWarning ("ButtonClose not found on Status Bar!");
71  }
72 
73  tf = statusBar.transform.Find ("ButtonSave");
74  if (tf != null) {
75  savePatientButton = tf.gameObject;
76  savePatientButton.GetComponent<Button> ().onClick.AddListener (() => savePatient ());
77  savePatientButton.SetActive (false);
78  } else {
79  Debug.LogWarning ("ButtonSave not found on Status Bar!");
80  }
81 
82  PatientEventSystem.startListening (PatientEventSystem.Event.PATIENT_FinishedLoading, patientLoaded );
83  //PatientEventSystem.startListening (PatientEventSystem.Event.PATIENT_Closed, hidePatientDefaultUI );
84  }
85  public void OnDisable()
86  {
87  //PatientEventSystem.stopListening (PatientEventSystem.Event.PATIENT_Loaded, showPatientDefaultUI);
88  //PatientEventSystem.stopListening (PatientEventSystem.Event.PATIENT_Closed, hidePatientDefaultUI);
89  }
90 
91  public void setPointerIsOnUI( bool onUI )
92  {
93  pointerIsOverUIObject = onUI;
94  }
95  public void setPointerIsOnPlatformUI( bool onUI )
96  {
98  }
99 
100  public void setCamera( Camera cam )
101  {
102  UICamera = cam;
103  aspectRatio = (float)UICamera.targetTexture.width / (float)UICamera.targetTexture.height;
104  layoutSystem.updateDimensions ();
105 
106  // Adjust position/size of the statusbar:
107  Rect newRect = layoutSystem.getStatusBarPosition ();
108  RectTransform widgetRect = statusBar.GetComponent<RectTransform> ();
109  widgetRect.localPosition = newRect.position;
110  widgetRect.sizeDelta = newRect.size;
111  }
112 
113  public Widget getWidgetByName( string name )
114  {
115  foreach (Transform child in transform) {
116  if (child.name == name) {
117  if (child.GetComponent<Widget> () != null) {
118  return child.GetComponent<Widget> ();
119  }
120  }
121  }
122  return null;
123  }
124 
125  public void selectTab( Button b )
126  {
127  ColorBlock colors = b.colors;
128  colors.normalColor = TabHighlightColor;
129  colors.highlightedColor = TabHighlightColor;
130  b.colors = colors;
131  b.image.sprite = selectedTabImage;
132  }
133 
134  public void unselectTab( Button b )
135  {
136  ColorBlock colors = b.colors;
137  colors.normalColor = ButtonBaseColor;
138  colors.highlightedColor = ButtonBaseColor;
139  b.colors = colors;
140  b.image.sprite = normalTabImage;
141  }
142 
143  public int addIndication( UI.Screen screen, string message )
144  {
145  if (activeIndicators.Count > 0) {
146  clearIndication (indicationID);
147  }
148 
149  if (screen == UI.Screen.left) {
150  GameObject indicator1 = Instantiate (indicatorLeft) as GameObject;
151  indicator1.GetComponent<Widget> ().layoutScreen = UI.Screen.right;
152  indicator1.GetComponent<Widget> ().layoutAlignHorizontal = AlignmentH.center;
153  indicator1.GetComponent<Widget> ().layoutAlignVertical = AlignmentV.center;
154  indicator1.SetActive (true);
155  Text t = indicator1.GetComponentInChildren<Text> ();
156  t.text = message;
157  indicator1.transform.SetParent (transform, false);
158  activeIndicators.Add (indicator1);
159 
160  GameObject indicator2 = Instantiate (indicatorLeft) as GameObject;
161  indicator2.GetComponent<Widget> ().layoutScreen = UI.Screen.center;
162  indicator2.GetComponent<Widget> ().layoutAlignHorizontal = AlignmentH.center;
163  indicator2.GetComponent<Widget> ().layoutAlignVertical = AlignmentV.center;
164  indicator2.SetActive (true);
165  t = indicator2.GetComponentInChildren<Text> ();
166  t.text = message;
167  indicator2.transform.SetParent (transform, false);
168  activeIndicators.Add (indicator2);
169  } else if (screen == UI.Screen.center) {
170  GameObject indicator1 = Instantiate (indicatorLeft) as GameObject;
171  indicator1.GetComponent<Widget> ().layoutScreen = UI.Screen.right;
172  indicator1.GetComponent<Widget> ().layoutAlignHorizontal = AlignmentH.center;
173  indicator1.GetComponent<Widget> ().layoutAlignVertical = AlignmentV.center;
174  indicator1.SetActive (true);
175  Text t = indicator1.GetComponentInChildren<Text> ();
176  t.text = message;
177  indicator1.transform.SetParent (transform, false);
178  activeIndicators.Add (indicator1);
179 
180  GameObject indicator2 = Instantiate (indicatorRight) as GameObject;
181  indicator2.GetComponent<Widget> ().layoutScreen = UI.Screen.left;
182  indicator2.GetComponent<Widget> ().layoutAlignHorizontal = AlignmentH.center;
183  indicator2.GetComponent<Widget> ().layoutAlignVertical = AlignmentV.center;
184  indicator2.SetActive (true);
185  t = indicator2.GetComponentInChildren<Text> ();
186  t.text = message;
187  indicator2.transform.SetParent (transform, false);
188  activeIndicators.Add (indicator2);
189  } else if (screen == UI.Screen.right) {
190  GameObject indicator1 = Instantiate (indicatorRight) as GameObject;
191  indicator1.GetComponent<Widget> ().layoutScreen = UI.Screen.left;
192  indicator1.GetComponent<Widget> ().layoutAlignHorizontal = AlignmentH.center;
193  indicator1.GetComponent<Widget> ().layoutAlignVertical = AlignmentV.center;
194  indicator1.SetActive (true);
195  Text t = indicator1.GetComponentInChildren<Text> ();
196  t.text = message;
197  indicator1.transform.SetParent (transform, false);
198  activeIndicators.Add (indicator1);
199 
200  GameObject indicator2 = Instantiate (indicatorRight) as GameObject;
201  indicator2.GetComponent<Widget> ().layoutScreen = UI.Screen.center;
202  indicator2.GetComponent<Widget> ().layoutAlignHorizontal = AlignmentH.center;
203  indicator2.GetComponent<Widget> ().layoutAlignVertical = AlignmentV.center;
204  indicator2.SetActive (true);
205  t = indicator2.GetComponentInChildren<Text> ();
206  t.text = message;
207  indicator2.transform.SetParent (transform, false);
208  activeIndicators.Add (indicator2);
209  }
210  return (++indicationID);
211  }
212 
213  public void clearIndication( int id )
214  {
215  if (indicationID != id)
216  return;
217 
218  foreach (GameObject go in activeIndicators) {
219  Destroy (go);
220  }
221  activeIndicators.Clear ();
222  }
223 
224  public void closePatient()
225  {
226  Patient.close ();
227  PatientEventSystem.triggerEvent (PatientEventSystem.Event.PATIENT_Closed);
228  layoutSystem.closeAllWidgets ();
229  closePatientButton.SetActive (false);
230  savePatientButton.SetActive (false);
231  PatientSelector.SetActive (true);
232  }
233  public void savePatient()
234  {
235  if (Patient.getLoadedPatient () != null) {
236  Patient.getLoadedPatient ().save ();
237  NotificationControl.instance.createNotification ("Patient saved.", new System.TimeSpan (0, 0, 5));
238  }
239  }
240 
241  public void patientLoaded( object obj = null )
242  {
243  closePatientButton.SetActive (true);
244  savePatientButton.SetActive (true);
245  }
246  }
247 }
Definition: Core.cs:9
float UIScale
The scale with which all the UI elements are scaled (i.e. pixels to meters)
Definition: Core.cs:24
GameObject statusBar
A bar on the lower end of the screen (for close button etc.)
Definition: Core.cs:39
bool pointerIsOverPlatformUIObject
Definition: Core.cs:15
bool pointerIsOverUIObject
Is set to true whenever the mouse is over a UI elemnt.
Definition: Core.cs:12