| 
									
										
										
										
											2020-11-15 03:10:44 +01:00
										 |  |  | import {InputElement} from "./InputElement"; | 
					
						
							|  |  |  | import {UIEventSource} from "../../Logic/UIEventSource"; | 
					
						
							|  |  |  | import Combine from "../Base/Combine"; | 
					
						
							|  |  |  | import Svg from "../../Svg"; | 
					
						
							| 
									
										
										
										
											2021-01-02 21:03:40 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-15 03:10:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Selects a direction in degrees | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | export default class DirectionInput extends InputElement<string> { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private readonly value: UIEventSource<string>; | 
					
						
							|  |  |  |     public readonly IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     constructor(value?: UIEventSource<string>) { | 
					
						
							|  |  |  |         super(); | 
					
						
							|  |  |  |         this.dumbMode = false; | 
					
						
							|  |  |  |         this.value = value ?? new UIEventSource<string>(undefined); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         this.value.addCallbackAndRun(rotation => { | 
					
						
							|  |  |  |             const selfElement = document.getElementById(this.id); | 
					
						
							|  |  |  |             if (selfElement === null) { | 
					
						
							|  |  |  |                 return; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |             const cone = selfElement.getElementsByClassName("direction-svg")[0] as HTMLElement | 
					
						
							| 
									
										
										
										
											2021-04-21 01:55:15 +02:00
										 |  |  |             cone.style.transform = `rotate(${rotation}deg)`; | 
					
						
							| 
									
										
										
										
											2020-11-15 03:10:44 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         }) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     GetValue(): UIEventSource<string> { | 
					
						
							|  |  |  |         return this.value; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     InnerRender(): string { | 
					
						
							|  |  |  |         return new Combine([ | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  |             `<div id="direction-leaflet-div-${this.id}" style="width:100%;height: 100%;position: absolute;top:0;left:0;border-radius:100%;"></div>`, | 
					
						
							| 
									
										
										
										
											2020-11-15 03:10:44 +01:00
										 |  |  |             Svg.direction_svg().SetStyle( | 
					
						
							| 
									
										
										
										
											2021-04-21 01:55:15 +02:00
										 |  |  |                 `position: absolute;top: 0;left: 0;width: 100%;height: 100%;transform:rotate(${this.value.data ?? 0}deg);`) | 
					
						
							| 
									
										
										
										
											2020-11-15 03:10:44 +01:00
										 |  |  |                 .SetClass("direction-svg"), | 
					
						
							|  |  |  |             Svg.compass_svg().SetStyle( | 
					
						
							|  |  |  |                 "position: absolute;top: 0;left: 0;width: 100%;height: 100%;") | 
					
						
							|  |  |  |         ]) | 
					
						
							|  |  |  |             .SetStyle("position:relative;display:block;width: min(100%, 25em); padding-top: min(100% , 25em); background:white; border: 1px solid black; border-radius: 999em") | 
					
						
							|  |  |  |             .Render(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     protected InnerUpdate(htmlElement: HTMLElement) { | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  |         super.InnerUpdate(htmlElement); | 
					
						
							| 
									
										
										
										
											2020-11-15 03:10:44 +01:00
										 |  |  |         const self = this; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         function onPosChange(x: number, y: number) { | 
					
						
							|  |  |  |             const rect = htmlElement.getBoundingClientRect(); | 
					
						
							|  |  |  |             const dx = -(rect.left + rect.right) / 2 + x; | 
					
						
							|  |  |  |             const dy = (rect.top + rect.bottom) / 2 - y; | 
					
						
							|  |  |  |             const angle = 180 * Math.atan2(dy, dx) / Math.PI; | 
					
						
							|  |  |  |             const angleGeo = Math.floor((450 - angle) % 360); | 
					
						
							| 
									
										
										
										
											2020-11-17 02:22:48 +01:00
										 |  |  |             self.value.setData("" + angleGeo) | 
					
						
							| 
									
										
										
										
											2020-11-15 03:10:44 +01:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         htmlElement.ontouchmove = (ev: TouchEvent) => { | 
					
						
							| 
									
										
										
										
											2020-11-15 19:17:36 +01:00
										 |  |  |             onPosChange(ev.touches[0].clientX, ev.touches[0].clientY); | 
					
						
							|  |  |  |             ev.preventDefault(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         htmlElement.ontouchstart = (ev: TouchEvent) => { | 
					
						
							| 
									
										
										
										
											2020-11-15 03:10:44 +01:00
										 |  |  |             onPosChange(ev.touches[0].clientX, ev.touches[0].clientY); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let isDown = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         htmlElement.onmousedown = (ev: MouseEvent) => { | 
					
						
							|  |  |  |             isDown = true; | 
					
						
							| 
									
										
										
										
											2021-04-21 01:55:15 +02:00
										 |  |  |             onPosChange(ev.clientX, ev.clientY); | 
					
						
							| 
									
										
										
										
											2020-11-15 03:10:44 +01:00
										 |  |  |             ev.preventDefault(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         htmlElement.onmouseup = (ev) => { | 
					
						
							|  |  |  |             isDown = false; ev.preventDefault(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         htmlElement.onmousemove = (ev: MouseEvent) => { | 
					
						
							|  |  |  |             if (isDown) { | 
					
						
							| 
									
										
										
										
											2021-04-21 01:55:15 +02:00
										 |  |  |                 onPosChange(ev.clientX, ev.clientY); | 
					
						
							| 
									
										
										
										
											2020-11-15 03:10:44 +01:00
										 |  |  |             } ev.preventDefault(); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     IsValid(str: string): boolean { | 
					
						
							|  |  |  |         const t = Number(str); | 
					
						
							|  |  |  |         return !isNaN(t) && t >= 0 && t <= 360; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |