notificationui.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. class NotificationUI
  2. {
  3. protected ref Widget m_Root;
  4. protected ref Widget m_Spacer;
  5. protected ref Widget m_VoiceContent;
  6. protected ref Widget m_NotificationContent;
  7. protected ref map<NotificationRuntimeData, Widget> m_Notifications;
  8. protected ref map<string, Widget> m_VoiceNotifications;
  9. protected float m_Width;
  10. protected float m_CurrentHeight;
  11. protected float m_TargetHeight;
  12. protected float m_BackupPosX;
  13. protected float m_BackupPosY;
  14. protected ref map<string, Widget> m_WidgetTimers;
  15. protected bool m_OffsetEnabled;;
  16. void NotificationUI()
  17. {
  18. m_Root = GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/notifications/notifications.layout");
  19. m_Spacer = m_Root.FindAnyWidget( "NotificationSpacer" );
  20. m_VoiceContent = m_Root.FindAnyWidget( "VoiceContent" );
  21. m_NotificationContent = m_Root.FindAnyWidget( "NotificationContent" );
  22. m_Notifications = new map<NotificationRuntimeData, Widget>;
  23. m_VoiceNotifications = new map<string, Widget>;
  24. m_WidgetTimers = new map<string, Widget>;
  25. NotificationSystem ntfSys = NotificationSystem.GetInstance();
  26. if (ntfSys)
  27. {
  28. ntfSys.m_OnNotificationAdded.Insert( AddNotification );
  29. ntfSys.m_OnNotificationRemoved.Insert( RemoveNotification );
  30. }
  31. }
  32. void ~NotificationUI()
  33. {
  34. NotificationSystem ntfSys = NotificationSystem.GetInstance();
  35. if (ntfSys)
  36. {
  37. ntfSys.m_OnNotificationAdded.Remove( AddNotification );
  38. ntfSys.m_OnNotificationRemoved.Remove( RemoveNotification );
  39. }
  40. }
  41. void AddNotification( NotificationRuntimeData data )
  42. {
  43. Widget notification = GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/notifications/notification_element.layout", m_NotificationContent);
  44. ImageWidget icon = ImageWidget.Cast( notification.FindAnyWidget( "Image" ) );
  45. RichTextWidget title = RichTextWidget.Cast( notification.FindAnyWidget( "Title" ) );
  46. if ( data.GetIcon() != "" )
  47. icon.LoadImageFile( 0, data.GetIcon() );
  48. title.SetText( data.GetTitleText() );
  49. if ( data.GetDetailText() != "" )
  50. {
  51. Widget bottom_spacer = notification.FindAnyWidget( "BottomSpacer" );
  52. RichTextWidget detail = RichTextWidget.Cast( notification.FindAnyWidget( "Detail" ) );
  53. bottom_spacer.Show(true);
  54. detail.SetText( data.GetDetailText() );
  55. detail.Update();
  56. bottom_spacer.Update();
  57. notification.Update();
  58. }
  59. m_Notifications.Insert( data, notification );
  60. UpdateTargetHeight();
  61. }
  62. void RemoveNotification( NotificationRuntimeData data )
  63. {
  64. if ( m_Notifications.Contains( data ) )
  65. {
  66. Widget notification = m_Notifications.Get( data );
  67. m_WidgetTimers.Insert( m_WidgetTimers.Count().ToString() + data.GetTime().ToString(), notification );
  68. m_Notifications.Remove( data );
  69. UpdateTargetHeight();
  70. }
  71. }
  72. void AddVoiceNotification(string player, string name)
  73. {
  74. if (!m_VoiceNotifications.Contains(player))
  75. {
  76. Widget notification;
  77. if (!m_WidgetTimers.Contains(player))
  78. {
  79. notification = GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/notifications/notification_voice_element.layout", m_VoiceContent);
  80. }
  81. else
  82. {
  83. notification = m_WidgetTimers.Get(player);
  84. m_WidgetTimers.Remove(player);
  85. notification.SetAlpha( 120 / 255 );
  86. Widget w_c = notification.FindAnyWidget( "Name" );
  87. if ( w_c )
  88. {
  89. w_c.SetAlpha( 1 );
  90. }
  91. }
  92. RichTextWidget title = RichTextWidget.Cast(notification.FindAnyWidget("Name"));
  93. m_VoiceNotifications.Insert(player, notification);
  94. title.SetText(name);
  95. UpdateTargetHeight();
  96. }
  97. }
  98. void RemoveVoiceNotification( string player )
  99. {
  100. if ( m_VoiceNotifications.Contains( player ) )
  101. {
  102. Widget notification = m_VoiceNotifications.Get( player );
  103. m_WidgetTimers.Insert( player, notification );
  104. m_VoiceNotifications.Remove( player );
  105. UpdateTargetHeight();
  106. }
  107. }
  108. void ClearVoiceNotifications()
  109. {
  110. for ( int i = 0; i < m_VoiceNotifications.Count(); i++ )
  111. {
  112. Widget w = m_VoiceNotifications.GetElement( i );
  113. delete w;
  114. }
  115. m_VoiceNotifications.Clear();
  116. UpdateTargetHeight();
  117. }
  118. void UpdateTargetHeight()
  119. {
  120. m_VoiceContent.Update();
  121. m_NotificationContent.Update();
  122. m_Spacer.Update();
  123. float x;
  124. m_Spacer.GetScreenSize( x, m_TargetHeight );
  125. m_Root.GetScreenSize( m_Width, m_CurrentHeight );
  126. UpdateOffset();
  127. }
  128. void UpdateOffset()
  129. {
  130. UIScriptedMenu menu = UIScriptedMenu.Cast(GetGame().GetUIManager().GetMenu());
  131. if (menu)
  132. {
  133. Widget expNotification = menu.GetLayoutRoot().FindAnyWidget("notification_root");
  134. if (expNotification && expNotification.IsVisible())
  135. {
  136. if (!m_OffsetEnabled)
  137. {
  138. m_Root.GetPos(m_BackupPosX, m_BackupPosY);
  139. float x, y, w, h;
  140. m_Root.GetScreenPos(x, y);
  141. expNotification.GetScreenSize(w, h);
  142. m_Root.SetScreenPos(x, h);
  143. m_OffsetEnabled = true;
  144. }
  145. }
  146. else if (m_OffsetEnabled)
  147. {
  148. m_Root.SetPos(m_BackupPosX, m_BackupPosY);
  149. m_OffsetEnabled = false;
  150. }
  151. }
  152. }
  153. static float m_VelArr[1];
  154. void Update( float timeslice )
  155. {
  156. UpdateOffset();
  157. float x;
  158. m_Spacer.GetScreenSize( x, m_TargetHeight );
  159. bool is_near = ( m_CurrentHeight + 0.01 < m_TargetHeight || m_CurrentHeight - 0.01 > m_TargetHeight );
  160. if ( is_near )
  161. {
  162. m_CurrentHeight = Math.SmoothCD(m_CurrentHeight, m_TargetHeight, m_VelArr, 0.2, 10000, timeslice);
  163. m_Root.SetSize( m_Width, m_CurrentHeight );
  164. }
  165. else if ( m_TargetHeight != m_CurrentHeight )
  166. {
  167. m_CurrentHeight = m_TargetHeight;
  168. m_Root.SetSize( m_Width, m_CurrentHeight );
  169. m_VelArr[0] = 0;
  170. }
  171. for ( int i = 0; i < m_WidgetTimers.Count(); )
  172. {
  173. Widget w = m_WidgetTimers.GetElement( i );
  174. float new_alpha = Math.Clamp( w.GetAlpha() - timeslice / NotificationSystem.NOTIFICATION_FADE_TIME, 0, 1 );
  175. if ( new_alpha > 0 )
  176. {
  177. w.SetAlpha( new_alpha );
  178. Widget w_c = w.FindAnyWidget( "TopSpacer" );
  179. Widget w_c2 = w.FindAnyWidget( "BottomSpacer" );
  180. Widget w_c3 = w.FindAnyWidget( "Title" );
  181. Widget w_c4 = w.FindAnyWidget( "Detail" );
  182. Widget w_c5 = w.FindAnyWidget( "Name" );
  183. if ( w_c && w_c2 )
  184. {
  185. float new_alpha_cont = Math.Clamp( w_c.GetAlpha() - timeslice / NotificationSystem.NOTIFICATION_FADE_TIME, 0, 1 );
  186. w_c.SetAlpha( new_alpha_cont );
  187. w_c2.SetAlpha( new_alpha_cont );
  188. w_c3.SetAlpha( new_alpha_cont );
  189. w_c4.SetAlpha( new_alpha_cont );
  190. }
  191. if ( w_c5 )
  192. {
  193. float new_alpha_voice = Math.Clamp( w_c5.GetAlpha() - timeslice / NotificationSystem.NOTIFICATION_FADE_TIME, 0, 1 );
  194. w_c5.SetAlpha(new_alpha_voice);
  195. }
  196. i++;
  197. }
  198. else
  199. {
  200. delete w;
  201. m_WidgetTimers.RemoveElement( i );
  202. UpdateTargetHeight();
  203. }
  204. }
  205. }
  206. }