MapComplete/src/Models/ThemeConfig/PointRenderingConfig.ts

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

336 lines
12 KiB
TypeScript
Raw Normal View History

import PointRenderingConfigJson from "./Json/PointRenderingConfigJson"
import TagRenderingConfig from "./TagRenderingConfig"
import { TagsFilter } from "../../Logic/Tags/TagsFilter"
import { TagUtils } from "../../Logic/Tags/TagUtils"
import { Utils } from "../../Utils"
import Svg from "../../Svg"
import WithContextLoader from "./WithContextLoader"
2023-10-07 03:07:32 +02:00
import { ImmutableStore, Store } from "../../Logic/UIEventSource"
import BaseUIElement from "../../UI/BaseUIElement"
import { FixedUiElement } from "../../UI/Base/FixedUiElement"
import Img from "../../UI/Base/Img"
import Combine from "../../UI/Base/Combine"
import { VariableUiElement } from "../../UI/Base/VariableUIElement"
import { TagRenderingConfigJson } from "./Json/TagRenderingConfigJson"
import SvelteUIElement from "../../UI/Base/SvelteUIElement"
import DynamicMarker from "../../UI/Map/DynamicMarker.svelte"
import { html } from "svelte/types/compiler/utils/namespaces"
export class IconConfig extends WithContextLoader {
2023-11-14 17:35:12 +01:00
public static readonly defaultIcon = new IconConfig({ icon: "pin", color: "#ff9939" })
public readonly icon: TagRenderingConfig
public readonly color: TagRenderingConfig
constructor(
config: {
icon: string | TagRenderingConfigJson
color?: string | TagRenderingConfigJson
},
context?: string
) {
super(config, context)
this.icon = this.tr("icon")
this.color = this.tr("color")
}
}
2022-06-04 18:10:09 +02:00
export default class PointRenderingConfig extends WithContextLoader {
static readonly allowed_location_codes: ReadonlySet<string> = new Set<string>([
"point",
"centroid",
"start",
"end",
"projected_centerpoint",
])
public readonly location: Set<
"point" | "centroid" | "start" | "end" | "projected_centerpoint" | string
>
2022-09-08 21:40:48 +02:00
public readonly marker: IconConfig[]
2021-10-21 21:41:45 +02:00
public readonly iconBadges: { if: TagsFilter; then: TagRenderingConfig }[]
public readonly iconSize: TagRenderingConfig
public readonly anchor: TagRenderingConfig
public readonly label: TagRenderingConfig
public readonly labelCss: TagRenderingConfig
public readonly labelCssClasses: TagRenderingConfig
public readonly rotation: TagRenderingConfig
public readonly cssDef: TagRenderingConfig
public readonly cssClasses?: TagRenderingConfig
2023-03-25 02:48:24 +01:00
public readonly pitchAlignment?: TagRenderingConfig
public readonly rotationAlignment?: TagRenderingConfig
constructor(json: PointRenderingConfigJson, context: string) {
super(json, context)
2021-11-07 16:34:51 +01:00
if (json === undefined || json === null) {
throw `At ${context}: Invalid PointRenderingConfig: undefined or null`
}
2021-11-07 16:34:51 +01:00
if (typeof json.location === "string") {
json.location = [json.location]
}
2021-11-07 16:34:51 +01:00
this.location = new Set(json.location)
2021-11-07 16:34:51 +01:00
this.location.forEach((l) => {
const allowed = PointRenderingConfig.allowed_location_codes
2021-11-07 16:34:51 +01:00
if (!allowed.has(l)) {
throw `A point rendering has an invalid location: '${l}' is not one of ${Array.from(
allowed
).join(", ")} (at ${context}.location)`
}
})
2021-11-07 16:34:51 +01:00
if (json.marker === undefined && json.label === undefined) {
2023-10-11 04:34:57 +02:00
throw `At ${context}: A point rendering should define at least an marker or a label`
}
2023-11-13 04:33:25 +01:00
if (json["markers"]) {
throw `At ${context}.markers: detected a field 'markerS' in pointRendering. It is written as a singular case`
}
if (json.marker && !Array.isArray(json.marker)) {
throw `At ${context}.marker: the marker in a pointRendering should be an array`
}
2021-11-07 16:34:51 +01:00
if (this.location.size == 0) {
throw (
"A pointRendering should have at least one 'location' to defined where it should be rendered. (At " +
context +
".location)"
2022-09-08 21:40:48 +02:00
)
}
2023-10-07 03:07:32 +02:00
this.marker = (json.marker ?? []).map((m) => new IconConfig(<any>m))
2022-12-16 13:45:07 +01:00
if (json.css !== undefined) {
this.cssDef = this.tr("css", undefined)
}
this.cssClasses = this.tr("cssClasses", undefined)
this.labelCss = this.tr("labelCss", undefined)
this.labelCssClasses = this.tr("labelCssClasses", undefined)
2021-10-21 21:41:45 +02:00
this.iconBadges = (json.iconBadges ?? []).map((overlay, i) => {
return {
if: TagUtils.Tag(overlay.if),
then: new TagRenderingConfig(overlay.then, `iconBadges.${i}`),
}
})
if (typeof json.iconSize === "string") {
const s = json.iconSize
if (["bottom", "top", "center"].some((e) => s.endsWith(e))) {
throw (
"At " +
context +
" in : iconSize uses legacy ,bottom, center or top postfix. Use the field `anchor` instead."
)
}
}
2023-10-02 01:23:43 +02:00
this.iconSize = this.tr("iconSize", "40,40", context + ".iconsize")
this.anchor = this.tr("anchor", "center", context + ".anchor")
this.label = this.tr("label", undefined, context + ".label")
this.rotation = this.tr("rotation", "0", context + ".rotation")
this.pitchAlignment = this.tr("pitchAlignment", "canvas", context + ".pitchAlignment")
2023-03-25 02:48:24 +01:00
this.rotationAlignment = this.tr(
"rotationAlignment",
2023-10-02 01:23:43 +02:00
json.pitchAlignment === "map" ? "map" : "canvas",
context + ".rotationAlignment"
2023-03-25 02:48:24 +01:00
)
}
private static FromHtmlMulti(multiSpec: string, tags: Store<Record<string, string>>) {
const icons: IconConfig[] = []
for (const subspec of multiSpec.split(";")) {
const [icon, color] = subspec.split(":")
icons.push(new IconConfig({ icon, color }))
2021-10-21 21:41:45 +02:00
}
return new SvelteUIElement(DynamicMarker, { marker: icons, tags }).SetClass(
"w-full h-full block absolute top-0 left-0"
2022-09-08 21:40:48 +02:00
)
2021-10-21 21:41:45 +02:00
}
public GetBaseIcon(tags?: Record<string, string>): BaseUIElement {
return new SvelteUIElement(DynamicMarker, {
marker: this.marker,
rotation: this.rotation,
tags: new ImmutableStore(tags),
})
2021-10-21 21:41:45 +02:00
}
2023-11-14 17:35:12 +01:00
2023-03-24 19:21:15 +01:00
public RenderIcon(
2023-03-28 05:13:48 +02:00
tags: Store<Record<string, string>>,
2021-10-30 02:34:16 +02:00
options?: {
noSize?: false | boolean
includeBadges?: true | boolean
2023-11-14 17:35:12 +01:00
metatags?: Store<Record<string, string>>
2021-10-30 02:34:16 +02:00
}
2021-10-21 21:41:45 +02:00
): {
html: BaseUIElement
iconAnchor: [number, number]
2021-10-21 21:41:45 +02:00
} {
function num(str, deflt = 40) {
const n = Number(str)
if (isNaN(n)) {
return deflt
}
return n
}
function render(tr: TagRenderingConfig, deflt?: string): string {
if (tags === undefined) {
return deflt
}
const str = tr?.GetRenderValue(tags.data)?.txt ?? deflt
return Utils.SubstituteKeys(str, tags.data).replace(/{.*}/g, "")
}
// in MapLibre, the offset is relative to the _center_ of the object, with left = [-x, 0] and up = [0,-y]
let anchorW = 0
2023-03-24 19:21:15 +01:00
let anchorH = 0
2023-11-14 17:35:12 +01:00
const anchor = render(this.anchor, "center")
const mode = anchor?.trim()?.toLowerCase() ?? "center"
const size = this.iconSize.GetRenderValue(tags.data).Subs(tags).txt ?? "[40,40]"
const [iconW, iconH] = size.split(",").map((x) => num(x))
if (mode === "left") {
anchorW = -iconW / 2
}
if (mode === "right") {
anchorW = iconW / 2
}
if (mode === "top") {
anchorH = iconH / 2
}
if (mode === "bottom") {
anchorH = -iconH / 2
}
const icon = new SvelteUIElement(DynamicMarker, {
marker: this.marker,
rotation: this.rotation,
tags,
}).SetClass("w-full h-full")
let badges = undefined
if (options?.includeBadges ?? true) {
2023-11-17 18:26:59 +01:00
badges = this.GetBadges(tags, options?.metatags)
}
const iconAndBadges = new Combine([icon, badges]).SetClass("block relative")
2023-11-14 17:35:12 +01:00
if (options?.noSize) {
2021-10-30 02:34:16 +02:00
iconAndBadges.SetClass("w-full h-full")
}
2023-11-14 17:35:12 +01:00
tags.map((tags) => this.iconSize.GetRenderValue(tags).Subs(tags).txt ?? "[40,40]").map(
(size) => {
const [iconW, iconH] = size.split(",").map((x) => num(x))
iconAndBadges.SetStyle(`width: ${iconW}px; height: ${iconH}px`)
}
)
2022-01-26 21:40:38 +01:00
const css = this.cssDef?.GetRenderValue(tags.data)?.txt
const cssClasses = this.cssClasses?.GetRenderValue(tags.data)?.txt
let label = this.GetLabel(tags)
2022-01-26 21:40:38 +01:00
let htmlEl: BaseUIElement
if (icon === undefined && label === undefined) {
htmlEl = undefined
2022-01-26 21:40:38 +01:00
} else if (icon === undefined) {
2021-12-05 05:17:29 +01:00
htmlEl = new Combine([label])
2022-01-26 21:40:38 +01:00
} else if (label === undefined) {
htmlEl = new Combine([iconAndBadges])
} else {
htmlEl = new Combine([iconAndBadges, label]).SetStyle("flex flex-col")
}
2022-12-16 13:45:07 +01:00
if (css !== undefined) {
htmlEl?.SetStyle(css)
}
2022-12-16 13:45:07 +01:00
if (cssClasses !== undefined) {
htmlEl?.SetClass(cssClasses)
}
return {
html: htmlEl,
2021-10-21 21:41:45 +02:00
iconAnchor: [anchorW, anchorH],
}
}
2021-10-21 21:41:45 +02:00
2023-11-14 17:35:12 +01:00
private GetBadges(
tags: Store<Record<string, string>>,
metaTags?: Store<Record<string, string>>
): BaseUIElement {
2021-11-07 16:34:51 +01:00
if (this.iconBadges.length === 0) {
return undefined
}
return new VariableUiElement(
2023-11-14 17:35:12 +01:00
tags.map(
(tagsData) => {
2023-11-14 17:35:12 +01:00
const badgeElements = this.iconBadges.map((badge) => {
if (!badge.if.matchesProperties(tagsData)) {
2023-11-14 17:35:12 +01:00
// Doesn't match...
return undefined
}
const metaCondition = badge.then.metacondition
if (
metaCondition &&
metaTags &&
!metaCondition.matchesProperties(metaTags.data)
) {
// Doesn't match
return undefined
}
const htmlDefs = Utils.SubstituteKeys(
badge.then.GetRenderValue(tagsData)?.txt,
tagsData
2023-11-14 17:35:12 +01:00
)
if (htmlDefs.startsWith("<") && htmlDefs.endsWith(">")) {
// This is probably an HTML-element
return new FixedUiElement(Utils.SubstituteKeys(htmlDefs, tagsData))
2023-11-14 17:35:12 +01:00
.SetStyle("width: 1.5rem")
.SetClass("block")
}
if (!htmlDefs) {
return undefined
}
2023-11-14 17:35:12 +01:00
const badgeElement = PointRenderingConfig.FromHtmlMulti(
htmlDefs,
tags
2023-11-14 17:35:12 +01:00
)?.SetClass("block relative")
if (badgeElement === undefined) {
return undefined
}
return new Combine([badgeElement])
.SetStyle("width: 1.5rem")
.SetClass("block")
2023-11-14 17:35:12 +01:00
})
return new Combine(badgeElements).SetClass("inline-flex h-full")
},
[metaTags]
)
2021-11-07 16:34:51 +01:00
).SetClass("absolute bottom-0 right-1/3 h-1/2 w-0")
}
2023-03-28 05:13:48 +02:00
private GetLabel(tags: Store<Record<string, string>>): BaseUIElement {
2021-11-07 16:34:51 +01:00
if (this.label === undefined) {
return undefined
}
const cssLabel = this.labelCss?.GetRenderValue(tags.data)?.txt
const cssClassesLabel = this.labelCssClasses?.GetRenderValue(tags.data)?.txt
2021-11-07 16:34:51 +01:00
const self = this
return new VariableUiElement(
tags.map((tags) => {
const label = self.label
?.GetRenderValue(tags)
?.Subs(tags)
?.SetClass("block center absolute text-center ")
?.SetClass(cssClassesLabel)
if (cssLabel) {
label.SetStyle(cssLabel)
}
return new Combine([label]).SetClass("flex flex-col items-center")
2021-11-07 16:34:51 +01:00
})
2022-09-08 21:40:48 +02:00
)
2021-11-07 16:34:51 +01:00
}
}