File size: 6,718 Bytes
24b81cb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
class NotificationUI
{
	protected ref Widget	m_Root;
	protected ref Widget	m_Spacer;
	protected ref Widget	m_VoiceContent;
	protected ref Widget	m_NotificationContent;
	
	protected ref map<NotificationRuntimeData, Widget>	m_Notifications;
	protected ref map<string, Widget>					m_VoiceNotifications;
	
	protected float										m_Width;
	protected float										m_CurrentHeight;
	protected float										m_TargetHeight;
	protected float										m_BackupPosX;
	protected float										m_BackupPosY;
	protected ref map<string, Widget>					m_WidgetTimers;
	
	protected bool										m_OffsetEnabled;;
	
	void NotificationUI()
	{
		m_Root					= GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/notifications/notifications.layout");
		m_Spacer				= m_Root.FindAnyWidget( "NotificationSpacer" );
		m_VoiceContent			= m_Root.FindAnyWidget( "VoiceContent" );
		m_NotificationContent	= m_Root.FindAnyWidget( "NotificationContent" );
		m_Notifications			= new map<NotificationRuntimeData, Widget>;
		m_VoiceNotifications	= new map<string, Widget>;
		m_WidgetTimers			= new map<string, Widget>;
		
		NotificationSystem ntfSys = NotificationSystem.GetInstance();
		if (ntfSys)
		{
			ntfSys.m_OnNotificationAdded.Insert( AddNotification );
			ntfSys.m_OnNotificationRemoved.Insert( RemoveNotification );
		}
	}
	
	void ~NotificationUI()
	{
		NotificationSystem ntfSys = NotificationSystem.GetInstance();
		if (ntfSys)
		{
			ntfSys.m_OnNotificationAdded.Remove( AddNotification );
			ntfSys.m_OnNotificationRemoved.Remove( RemoveNotification );
		}
	}
	
	void AddNotification( NotificationRuntimeData data )
	{
		Widget notification			= GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/notifications/notification_element.layout", m_NotificationContent);
		
		ImageWidget icon			= ImageWidget.Cast( notification.FindAnyWidget( "Image" ) );
		RichTextWidget title		= RichTextWidget.Cast( notification.FindAnyWidget( "Title" ) );
		
		if ( data.GetIcon() != "" )
			icon.LoadImageFile( 0, data.GetIcon() );
		title.SetText( data.GetTitleText() );
		
		if ( data.GetDetailText() != "" )
		{
			Widget bottom_spacer	= notification.FindAnyWidget( "BottomSpacer" );
			RichTextWidget detail	= RichTextWidget.Cast( notification.FindAnyWidget( "Detail" ) );
			bottom_spacer.Show(true);
			detail.SetText( data.GetDetailText() );
			detail.Update();
			bottom_spacer.Update();
			notification.Update();
		}
		
		m_Notifications.Insert( data, notification );
		UpdateTargetHeight();
	}
	
	void RemoveNotification( NotificationRuntimeData data )
	{
		if ( m_Notifications.Contains( data ) )
		{
			Widget notification = m_Notifications.Get( data );
			m_WidgetTimers.Insert( m_WidgetTimers.Count().ToString() + data.GetTime().ToString(), notification );
			m_Notifications.Remove( data );
			UpdateTargetHeight();
		}
	}
	
	void AddVoiceNotification(string player, string name)
	{
		if (!m_VoiceNotifications.Contains(player))
		{
			Widget notification;
			if (!m_WidgetTimers.Contains(player))
			{
				notification = GetGame().GetWorkspace().CreateWidgets("gui/layouts/new_ui/notifications/notification_voice_element.layout", m_VoiceContent);
			}
			else
			{
				notification = m_WidgetTimers.Get(player);
				m_WidgetTimers.Remove(player);
				notification.SetAlpha( 120 / 255 );
				Widget w_c = notification.FindAnyWidget( "Name" );
				if ( w_c )
				{
					w_c.SetAlpha( 1 );
				}
			}

			RichTextWidget title = RichTextWidget.Cast(notification.FindAnyWidget("Name"));
			m_VoiceNotifications.Insert(player, notification);
			title.SetText(name);
			UpdateTargetHeight();
		}
	}
	
	void RemoveVoiceNotification( string player )
	{
		if ( m_VoiceNotifications.Contains( player ) )
		{
			Widget notification = m_VoiceNotifications.Get( player );
			m_WidgetTimers.Insert( player, notification );
			m_VoiceNotifications.Remove( player );
			UpdateTargetHeight();
		}
	}
	
	void ClearVoiceNotifications()
	{
		for ( int i = 0; i < m_VoiceNotifications.Count(); i++ )
		{
			Widget w = m_VoiceNotifications.GetElement( i );
			delete w;
		}
		m_VoiceNotifications.Clear();
		UpdateTargetHeight();
	}
	
	void UpdateTargetHeight()
	{
		m_VoiceContent.Update();
		m_NotificationContent.Update();
		m_Spacer.Update();
		
		float x;
		m_Spacer.GetScreenSize( x, m_TargetHeight );
		m_Root.GetScreenSize( m_Width, m_CurrentHeight );
		
		UpdateOffset();
	}
	
	void UpdateOffset()
	{
		UIScriptedMenu menu = UIScriptedMenu.Cast(GetGame().GetUIManager().GetMenu());
		if (menu)
		{
			Widget expNotification = menu.GetLayoutRoot().FindAnyWidget("notification_root");
			if (expNotification && expNotification.IsVisible())
			{
				if (!m_OffsetEnabled)
				{
					m_Root.GetPos(m_BackupPosX, m_BackupPosY);
					
					float x, y, w, h;
					m_Root.GetScreenPos(x, y);
					expNotification.GetScreenSize(w, h);
					
					m_Root.SetScreenPos(x, h);
					m_OffsetEnabled = true;
				}
			}
			else if (m_OffsetEnabled)
			{
				m_Root.SetPos(m_BackupPosX, m_BackupPosY);
				m_OffsetEnabled = false;
			}
		}
	}
	
	static float m_VelArr[1];
	void Update( float timeslice )
	{
		UpdateOffset();
		
		float x;
		m_Spacer.GetScreenSize( x, m_TargetHeight );
		bool is_near = ( m_CurrentHeight + 0.01 < m_TargetHeight || m_CurrentHeight - 0.01 > m_TargetHeight );
		if ( is_near )
		{
			m_CurrentHeight = Math.SmoothCD(m_CurrentHeight, m_TargetHeight, m_VelArr, 0.2, 10000, timeslice);
			m_Root.SetSize( m_Width, m_CurrentHeight );
		}
		else if ( m_TargetHeight != m_CurrentHeight )
		{
			m_CurrentHeight = m_TargetHeight;
			m_Root.SetSize( m_Width, m_CurrentHeight );
			m_VelArr[0] = 0;
		}
		
		for ( int i = 0; i < m_WidgetTimers.Count(); )
		{
			Widget w		= m_WidgetTimers.GetElement( i );
			float new_alpha	= Math.Clamp( w.GetAlpha() - timeslice / NotificationSystem.NOTIFICATION_FADE_TIME, 0, 1 );
			if ( new_alpha > 0 )
			{
				w.SetAlpha( new_alpha );
				Widget w_c = w.FindAnyWidget( "TopSpacer" );
				Widget w_c2 = w.FindAnyWidget( "BottomSpacer" );
				Widget w_c3 = w.FindAnyWidget( "Title" );
				Widget w_c4 = w.FindAnyWidget( "Detail" );
				Widget w_c5 = w.FindAnyWidget( "Name" );
				if ( w_c && w_c2 )
				{
					float new_alpha_cont	= Math.Clamp( w_c.GetAlpha() - timeslice / NotificationSystem.NOTIFICATION_FADE_TIME, 0, 1 );
					w_c.SetAlpha( new_alpha_cont );
					w_c2.SetAlpha( new_alpha_cont );
					w_c3.SetAlpha( new_alpha_cont );
					w_c4.SetAlpha( new_alpha_cont );
				}
				if ( w_c5 )
				{
					float new_alpha_voice	= Math.Clamp( w_c5.GetAlpha() - timeslice / NotificationSystem.NOTIFICATION_FADE_TIME, 0, 1 );
					w_c5.SetAlpha(new_alpha_voice);
				}
				i++;
			}
			else
			{
				delete w;
				m_WidgetTimers.RemoveElement( i );
				UpdateTargetHeight();
			}
		}
	}
}