mapcomplete/UI/Input/DirectionInput.ts

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

119 lines
4 KiB
TypeScript
Raw Normal View History

2022-09-08 21:40:48 +02:00
import { InputElement } from "./InputElement"
import { UIEventSource } from "../../Logic/UIEventSource"
import Combine from "../Base/Combine"
import Svg from "../../Svg"
import BaseUIElement from "../BaseUIElement"
import { FixedUiElement } from "../Base/FixedUiElement"
import { Utils } from "../../Utils"
import Loc from "../../Models/Loc"
import Minimap from "../Base/Minimap"
2020-11-15 03:10:44 +01:00
/**
* Selects a direction in degrees
*/
export default class DirectionInput extends InputElement<string> {
2022-09-08 21:40:48 +02:00
public readonly IsSelected: UIEventSource<boolean> = new UIEventSource<boolean>(false)
private readonly _location: UIEventSource<Loc>
private readonly value: UIEventSource<string>
private background
constructor(
mapBackground: UIEventSource<any>,
location: UIEventSource<Loc>,
value?: UIEventSource<string>
) {
super()
this._location = location
this.value = value ?? new UIEventSource<string>(undefined)
this.background = mapBackground
2021-06-16 17:09:32 +02:00
}
2021-06-18 14:30:49 +02:00
GetValue(): UIEventSource<string> {
2022-09-08 21:40:48 +02:00
return this.value
2021-06-18 14:30:49 +02:00
}
IsValid(str: string): boolean {
2022-09-08 21:40:48 +02:00
const t = Number(str)
return !isNaN(t) && t >= 0 && t <= 360
2021-06-18 14:30:49 +02:00
}
2021-06-16 17:09:32 +02:00
protected InnerConstructElement(): HTMLElement {
let map: BaseUIElement = new FixedUiElement("")
if (!Utils.runningFromConsole) {
map = Minimap.createMiniMap({
background: this.background,
allowMoving: false,
2022-09-08 21:40:48 +02:00
location: this._location,
})
}
2021-06-16 17:09:32 +02:00
2021-06-18 14:30:49 +02:00
const element = new Combine([
2022-09-08 21:40:48 +02:00
Svg.direction_stroke_svg()
.SetStyle(
`position: absolute;top: 0;left: 0;width: 100%;height: 100%;transform:rotate(${
this.value.data ?? 0
}deg);`
)
.SetClass("direction-svg relative")
.SetStyle("z-index: 1000"),
2022-09-08 21:40:48 +02:00
map.SetStyle(`position: absolute;top: 0;left: 0;width: 100%;height: 100%;`),
2021-06-10 14:05:26 +02:00
])
.SetStyle("width: min(100%, 25em); height: 0; padding-bottom: 100%") // A bit a weird CSS , see https://stackoverflow.com/questions/13851940/pure-css-solution-square-elements#19448481
2021-08-26 16:24:18 +02:00
.SetClass("relative block bg-white border border-black overflow-hidden rounded-full")
2021-06-10 14:05:26 +02:00
.ConstructElement()
2022-09-08 21:40:48 +02:00
this.value.addCallbackAndRunD((rotation) => {
2021-06-16 17:09:32 +02:00
const cone = element.getElementsByClassName("direction-svg")[0] as HTMLElement
2022-09-08 21:40:48 +02:00
cone.style.transform = `rotate(${rotation}deg)`
2020-11-15 03:10:44 +01:00
})
2021-06-18 14:30:49 +02:00
this.RegisterTriggers(element)
element.style.overflow = "hidden"
2021-08-26 16:24:18 +02:00
element.style.display = "block"
2020-11-15 03:10:44 +01:00
2022-09-08 21:40:48 +02:00
return element
2020-11-15 03:10:44 +01:00
}
2021-06-16 17:09:32 +02:00
private RegisterTriggers(htmlElement: HTMLElement) {
2022-09-08 21:40:48 +02:00
const self = this
2020-11-15 03:10:44 +01:00
function onPosChange(x: number, y: number) {
2022-09-08 21:40:48 +02:00
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) => {
2022-09-08 21:40:48 +02:00
onPosChange(ev.touches[0].clientX, ev.touches[0].clientY)
ev.preventDefault()
}
htmlElement.ontouchstart = (ev: TouchEvent) => {
2022-09-08 21:40:48 +02:00
onPosChange(ev.touches[0].clientX, ev.touches[0].clientY)
2020-11-15 03:10:44 +01:00
}
2022-09-08 21:40:48 +02:00
let isDown = false
2020-11-15 03:10:44 +01:00
htmlElement.onmousedown = (ev: MouseEvent) => {
2022-09-08 21:40:48 +02:00
isDown = true
onPosChange(ev.clientX, ev.clientY)
ev.preventDefault()
2020-11-15 03:10:44 +01:00
}
htmlElement.onmouseup = (ev) => {
2022-09-08 21:40:48 +02:00
isDown = false
ev.preventDefault()
2020-11-15 03:10:44 +01:00
}
htmlElement.onmousemove = (ev: MouseEvent) => {
if (isDown) {
2022-09-08 21:40:48 +02:00
onPosChange(ev.clientX, ev.clientY)
2021-06-18 14:30:49 +02:00
}
2022-09-08 21:40:48 +02:00
ev.preventDefault()
2020-11-15 03:10:44 +01:00
}
}
2022-09-08 21:40:48 +02:00
}