| 
									
										
										
										
											2020-05-16 10:43:11 +00:00
										 |  |  | import { BaseHUDPart } from "../base_hud_part"; | 
					
						
							|  |  |  | import { makeDiv } from "../../../core/utils"; | 
					
						
							| 
									
										
										
										
											2020-05-17 10:12:13 +00:00
										 |  |  | import { T } from "../../../translations"; | 
					
						
							| 
									
										
										
										
											2020-05-21 14:56:53 +00:00
										 |  |  | import { IS_DEMO } from "../../../core/config"; | 
					
						
							| 
									
										
										
										
											2020-05-16 10:43:11 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** @enum {string} */ | 
					
						
							|  |  |  | export const enumNotificationType = { | 
					
						
							|  |  |  |     saved: "saved", | 
					
						
							|  |  |  |     upgrade: "upgrade", | 
					
						
							|  |  |  |     success: "success", | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-16 15:57:25 +00:00
										 |  |  | const notificationDuration = 3; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-16 10:43:11 +00:00
										 |  |  | export class HUDNotifications extends BaseHUDPart { | 
					
						
							|  |  |  |     createElements(parent) { | 
					
						
							|  |  |  |         this.element = makeDiv(parent, "ingame_HUD_Notifications", [], ``); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     initialize() { | 
					
						
							|  |  |  |         this.root.hud.signals.notification.add(this.onNotification, this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         /** @type {Array<{ element: HTMLElement, expireAt: number}>} */ | 
					
						
							|  |  |  |         this.notificationElements = []; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // Automatic notifications
 | 
					
						
							| 
									
										
										
										
											2020-05-21 16:03:57 +00:00
										 |  |  |         this.root.signals.gameSaved.add(() => | 
					
						
							|  |  |  |             this.onNotification(T.ingame.notifications.gameSaved, enumNotificationType.saved) | 
					
						
							|  |  |  |         ); | 
					
						
							| 
									
										
										
										
											2020-05-16 10:43:11 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param {string} message | 
					
						
							|  |  |  |      * @param {enumNotificationType} type | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     onNotification(message, type) { | 
					
						
							|  |  |  |         const element = makeDiv(this.element, null, ["notification", "type-" + type], message); | 
					
						
							|  |  |  |         element.setAttribute("data-icon", "icons/notification_" + type + ".png"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.notificationElements.push({ | 
					
						
							|  |  |  |             element, | 
					
						
							| 
									
										
										
										
											2020-05-16 15:57:25 +00:00
										 |  |  |             expireAt: this.root.time.realtimeNow() + notificationDuration, | 
					
						
							| 
									
										
										
										
											2020-05-16 10:43:11 +00:00
										 |  |  |         }); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     update() { | 
					
						
							|  |  |  |         const now = this.root.time.realtimeNow(); | 
					
						
							|  |  |  |         for (let i = 0; i < this.notificationElements.length; ++i) { | 
					
						
							|  |  |  |             const handle = this.notificationElements[i]; | 
					
						
							|  |  |  |             if (handle.expireAt <= now) { | 
					
						
							|  |  |  |                 handle.element.remove(); | 
					
						
							|  |  |  |                 this.notificationElements.splice(i, 1); | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |