IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
NotificationControl.cs
1 using UnityEngine;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System;
5 using UnityEngine.UI;
6 
15 public class NotificationControl : MonoBehaviour {
16 
17  public GameObject firstNotification;
18  public GameObject secondNotification;
19  public GameObject thirdNotification;
20  public GameObject statusBar;
21  public GameObject additionalInformation;
22  public GameObject customAdditionalInfoIn; //For debugging
23  public GameObject customAdditionalInfo = null;
24 
25  [Header("Variables for moving notification center to the viewport")]
26  public bool moveNotificationCenter = true;
27  public int minXPositionNotificationCenter = -650;
28  public int maxXPositionNotificationCenter = 1000;
29 
30  private List<Notification> notitficationList = new List<Notification>();
31 
32 
33  public static NotificationControl instance { get; private set; }
34 
35  public NotificationControl()
36  {
37  if (instance != null)
38  throw( new System.Exception("Cannot create NotificationControl: Only one NotificationControl may exist!" ));
39 
40  instance = this;
41  }
42 
43  // Use this for initialization
44  void Start () {
45  firstNotification.SetActive(false);
46  secondNotification.SetActive(false);
47  thirdNotification.SetActive(false);
48  this.gameObject.SetActive(false);
49 
50  //Place notification center in the center
51  this.GetComponent<RectTransform>().localPosition = new Vector2(0, this.GetComponent<RectTransform>().localPosition.y);
52 
53  hideAdditionalInformation();
54  }
55 
56  // Update is called once per frame
57  void Update () {
58  bool listChanged = deleteNotificationsIfExpired();
59  if (listChanged)
60  {
61  hideAdditionalInformation();
62  updateNotificationCenter();
63  }
64 
65  //moving notification center to center of view port
66  if (moveNotificationCenter)
67  {
68  RaycastHit hit;
69  Ray ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
70  LayerMask onlyUIMeshLayer = 100000000; // hits only the ui mesh layer
71 
72  if (Physics.Raycast(ray, out hit, Mathf.Infinity, onlyUIMeshLayer)) {
73  float statusbarWidth = statusBar.GetComponent<RectTransform>().rect.width;
74  int newXPos = (int)(statusbarWidth * hit.textureCoord.x);
75  newXPos = newXPos - (int)(statusbarWidth / 2);
76  newXPos = Math.Min(newXPos, maxXPositionNotificationCenter);
77  newXPos = Math.Max(newXPos, minXPositionNotificationCenter);
78  this.GetComponent<RectTransform>().localPosition = new Vector2(newXPos, this.GetComponent<RectTransform>().localPosition.y);
79  }
80  }
81  }
82 
83  public void debug()
84  {
85  System.Random rnd = new System.Random();
86  Notification i = new Notification("Notification " + rnd.Next(1, 99), TimeSpan.Zero);
87  Notification m = new Notification("Notification " + rnd.Next(1, 99), TimeSpan.Zero, null, new AdditionalInfo("test test \ntest " + rnd.Next(1, 99)));
88  Notification n = new Notification("Notification " + rnd.Next(1, 99), TimeSpan.Zero, null, new AdditionalInfo(customAdditionalInfoIn));
89  createNotification(m);
90  createNotification(n);
91  createNotification(i);
92  }
93 
94  public void createNotification(Notification n)
95  {
96  notitficationList.Add(n);
97  orderNotitficationList();
98  updateNotificationCenter();
99  }
100 
102  public void createNotification( string text, TimeSpan timeToLive, Sprite notificationSprite = null, AdditionalInfo additionalInfo = null )
103  {
104  Notification n = new Notification (text, timeToLive, notificationSprite, additionalInfo);
105  createNotification (n);
106  }
107 
108  private bool deleteNotificationsIfExpired()
109  {
110  bool result = false;
111  for(int i = notitficationList.Count - 1; i >= 0; i--) //Foreach dont work
112  {
113  if(notitficationList[i].ExpireDate < DateTime.Now)
114  {
115  notitficationList.RemoveAt(i);
116  result = true;
117  }
118  }
119  return result;
120  }
121 
123  private void updateNotificationCenter()
124  {
125  if (notitficationList.Count == 0)
126  {
127  this.gameObject.SetActive(false);
128  return;
129  }
130 
131  firstNotification.SetActive(false);
132  secondNotification.SetActive(false);
133  thirdNotification.SetActive(false);
134 
135  this.gameObject.SetActive(true);
136 
137  if(notitficationList.Count > 0)
138  {
139  firstNotification.SetActive(true);
140  setTextAndIconInNotifiaction(firstNotification, notitficationList[0], true);
141  }
142  if (notitficationList.Count > 1)
143  {
144  secondNotification.SetActive(true);
145  setTextAndIconInNotifiaction(secondNotification, notitficationList[1], false);
146  }
147  if (notitficationList.Count > 2)
148  {
149  thirdNotification.SetActive(true);
150  setTextAndIconInNotifiaction(thirdNotification, notitficationList[2], false);
151  }
152  }
153 
154  private void setTextAndIconInNotifiaction(GameObject notificationGameObject, Notification n, bool firstNotification)
155  {
156  //Set text
157  notificationGameObject.GetComponentInChildren<Text>().text = n.Text;
158 
159  //Set Icon
160  if (n.NotificationSprite != null)
161  {
162  notificationGameObject.transform.Find("Icon").GetComponent<Image>().sprite = n.NotificationSprite;
163  }
164 
165  //Set reference to notification object
166  notificationGameObject.GetComponent<NotificationReference>().notification = n;
167 
168  //Set additional info text if notification has an additional info text and is the first notification on screen
169  if (firstNotification)
170  {
171  if(n.AdditionalInfo != null)
172  {
173  if (n.AdditionalInfo.Type == AdditionalInfo.AdditionalInfoType.TEXT)
174  {
175  additionalInformation.GetComponentInChildren<Text>().text = n.AdditionalInfo.AdditionalInfoText;
176  customAdditionalInfo = null;
177  }
178  if (n.AdditionalInfo.Type == AdditionalInfo.AdditionalInfoType.CUSTOM)
179  {
180  customAdditionalInfo = n.AdditionalInfo.CustomUIElement;
181  }
182  }
183  else
184  {
185  hideAdditionalInformation();
186  }
187  }
188 
189 
190  }
191 
192  public void deleteNotoficationPressed(GameObject sender)
193  {
194  for (int i = notitficationList.Count - 1; i >= 0; i--)
195  {
196  if (sender.GetComponent<NotificationReference>().notification == notitficationList[i])
197  {
198  notitficationList.RemoveAt(i);
199  break;
200  }
201  }
202  hideAdditionalInformation();
203  updateNotificationCenter();
204  }
205 
207  private void orderNotitficationList(){
208  notitficationList.Sort((x, y) => {
209  int result = x.ExpireDate.CompareTo(y.ExpireDate);
210  if (result == 0) {
211  result = x.CreationDate.CompareTo(y.CreationDate);
212  }
213  return result;
214  });
215  }
216 
217  public void showAdditionalInformation()
218  {
219  if(notitficationList.Count > 0 && notitficationList[0].AdditionalInfo != null)
220  {
221  if (notitficationList[0].AdditionalInfo.Type == AdditionalInfo.AdditionalInfoType.TEXT)
222  {
223  additionalInformation.SetActive(true);
224  } else if (notitficationList[0].AdditionalInfo.Type == AdditionalInfo.AdditionalInfoType.CUSTOM)
225  {
226  customAdditionalInfo.SetActive(true);
227  }
228 
229  }
230  }
231 
232  public void hideAdditionalInformation()
233  {
234  additionalInformation.SetActive(false);
235  if(customAdditionalInfo != null)
236  {
237  customAdditionalInfo.SetActive(false);
238  }
239  }
240 }
Used for notification game object to reference the acutal notifiaction.
void createNotification(string text, TimeSpan timeToLive, Sprite notificationSprite=null, AdditionalInfo additionalInfo=null)
Convenience overload.