Driving mode ("Drive": big buttons — follow, zoom, 2D/3D, night, route overview — the sidebar hidden); palettes instead of the inversion (the UI's greys onto --ui-*, MapLibre's controls too, night → Night, new ◑ Dusk); the navigated route magenta, the driven part dimmed; route progress only forward (locate near the last position first); rerouting on the server off its own GNSS (TripStore#follow), the browser's only without the shared trip; the overview map hidden while following in 3D; the topbar's items apart
This commit is contained in:
1 parent
12b4ac6725
commit
07a4d363ec
13 files changed
+520
-219
No files matched your search
+206
-172
File diff suppressed because it is too large.
Load diff
+33
-6
@@ -1,5 +1,5 @@
|
||||
import { isLoggedIn } from "/js/auth.js";
|
||||
import { onRouteChanged, requestReroute, requestGnssFollow } from "/js/panels.js";
|
||||
import { onRouteChanged, requestReroute, requestGnssFollow, notifyRouteProgress } from "/js/panels.js";
|
||||
import { elevationAt } from "/js/dem.js";
|
||||
|
||||
// The car's dashboard over the map, off the CAN signals (GET /api/can,
|
||||
@@ -102,13 +102,27 @@ function prepareRoute(route) {
|
||||
return { coords, km, limits: route.properties.speed_limits, signals: route.properties.signals || [], maneuvers, viaKm };
|
||||
}
|
||||
|
||||
// Nearest point on the route: [km along it, meters off it].
|
||||
// ponytail: scans every segment each fix — a window around the last hit if long routes get slow.
|
||||
function locate(route, position) {
|
||||
// Nearest point on the route: [km along it, meters off it]. Near the last
|
||||
// one first (fromKm: LOCATE_BACK_KM behind to LOCATE_AHEAD_KM ahead), so a
|
||||
// route that comes back along the same road or crosses itself doesn't jump
|
||||
// to the other pass — progress goes forward, as Ferrostar's does; the whole
|
||||
// route only when that window has nothing close (a reroute, a restart).
|
||||
const LOCATE_BACK_KM = 0.1;
|
||||
const LOCATE_AHEAD_KM = 3;
|
||||
function locate(route, position, fromKm = null) {
|
||||
if (fromKm != null) {
|
||||
const near = locateIn(route, position, fromKm - LOCATE_BACK_KM, fromKm + LOCATE_AHEAD_KM);
|
||||
if (near[1] <= ON_ROUTE_M) return near;
|
||||
}
|
||||
return locateIn(route, position, -Infinity, Infinity);
|
||||
}
|
||||
|
||||
function locateIn(route, position, fromKm, toKm) {
|
||||
let best = [0, Infinity];
|
||||
const { coords, km } = route;
|
||||
const cos = Math.cos(position[1] * RAD);
|
||||
for (let i = 1; i < coords.length; i++) {
|
||||
if (km[i] < fromKm || km[i - 1] > toKm) continue;
|
||||
const [ax, ay] = [coords[i - 1][0] * cos, coords[i - 1][1]];
|
||||
const [bx, by] = [coords[i][0] * cos, coords[i][1]];
|
||||
const [px, py] = [position[0] * cos, position[1]];
|
||||
@@ -161,6 +175,7 @@ export class DashboardControl {
|
||||
this._route = prepareRoute(route);
|
||||
this._lastAt = 0;
|
||||
this._offFixes = 0;
|
||||
this._sentKm = undefined; // the new route drawn by the next fix
|
||||
}));
|
||||
this._trace = []; // [lon, lat] fixes TRACE_STEP_M apart, for /api/road
|
||||
this._road = null; // its last answer
|
||||
@@ -195,6 +210,13 @@ export class DashboardControl {
|
||||
this._values.roll = deg(left - right);
|
||||
}
|
||||
|
||||
// To the route panel (the driven part dimmed), when it moved ≥ 10 m.
|
||||
_progress(km) {
|
||||
if (km === this._sentKm || (km != null && this._sentKm != null && Math.abs(km - this._sentKm) < 0.01)) return;
|
||||
this._sentKm = km;
|
||||
notifyRouteProgress(km);
|
||||
}
|
||||
|
||||
// The limit and what's ahead: from the route while on it, else the road.
|
||||
_navigate() {
|
||||
const { lat, lon, speed } = this._values;
|
||||
@@ -202,7 +224,7 @@ export class DashboardControl {
|
||||
const position = [lon, lat];
|
||||
if (!this._trace.length || meters(this._trace.at(-1), position) >= TRACE_STEP_M) this._trace = [...this._trace.slice(-7), position];
|
||||
|
||||
const [at, off] = this._route ? locate(this._route, position) : [0, Infinity];
|
||||
const [at, off] = this._route ? locate(this._route, position, this._lastAt || null) : [0, Infinity];
|
||||
const fresh = position.join() !== this._lastFix; // polled faster than the fixes come
|
||||
this._lastFix = position.join();
|
||||
if (fresh && this._route && off > ON_ROUTE_M && (this._values.hdop ?? 0) <= MAX_HDOP) this._offFixes++;
|
||||
@@ -216,7 +238,10 @@ export class DashboardControl {
|
||||
const maneuver = maneuvers[index];
|
||||
const then = maneuvers[index + 1];
|
||||
const onStreet = maneuvers[index - 1]?.street; // the one being driven: the last maneuver's
|
||||
if (off <= ON_ROUTE_M) this._lastAt = at;
|
||||
if (off <= ON_ROUTE_M) {
|
||||
this._lastAt = at;
|
||||
this._progress(at);
|
||||
}
|
||||
this._nav = {
|
||||
limit: current?.[1] ?? null,
|
||||
next: next && { limit: next[1], m: (next[0] - at) * 1000 },
|
||||
@@ -227,6 +252,7 @@ export class DashboardControl {
|
||||
};
|
||||
return;
|
||||
}
|
||||
this._progress(null);
|
||||
this._nav = { limit: this._road?.speed_limit ?? null, offRoute: !!this._route, street: this._road?.name };
|
||||
if (this._route && (speed ?? 0) >= MOVING_MS && Date.now() - this._rerouteAt >= REROUTE_EVERY_MS) {
|
||||
this._rerouteAt = Date.now();
|
||||
@@ -352,6 +378,7 @@ export class DashboardControl {
|
||||
}
|
||||
|
||||
_disable() {
|
||||
this._progress(null);
|
||||
clearTimeout(this._timer);
|
||||
this._timer = null;
|
||||
this._card?.remove();
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
import { LngLatBounds } from "/vendor/maplibre-gl/maplibre-gl.mjs";
|
||||
import { requestGnssFollow, onRouteChanged } from "/js/panels.js";
|
||||
import { pauseFollow, resumeFollow } from "/js/gnss.js";
|
||||
|
||||
// Driving mode: the few things needed on the road as big buttons down the
|
||||
// side of the map, the planning sidebar and panels out of the way (TODO:
|
||||
// tiny icons can't be hit while driving). Per device (localStorage).
|
||||
const KEY = "drivingMode";
|
||||
const PITCH_3D = 60; // as map.js's 3D toggle
|
||||
|
||||
export function setupDriving(map, nightButton) {
|
||||
let route = onRouteChanged((r) => (route = r));
|
||||
const toggle = document.createElement("button");
|
||||
toggle.type = "button";
|
||||
toggle.className = "topbar__drive";
|
||||
toggle.title = "Driving mode: big buttons, no planning panels";
|
||||
toggle.textContent = "Drive";
|
||||
nightButton.before(toggle);
|
||||
|
||||
const bar = document.createElement("div");
|
||||
bar.className = "drive-bar";
|
||||
bar.hidden = true;
|
||||
const buttons = {
|
||||
follow: ["Follow", "Follow the position, heading up in 3D", () => {
|
||||
requestGnssFollow();
|
||||
resumeFollow(map);
|
||||
if (map.getPitch() === 0) map.easeTo({ pitch: PITCH_3D, duration: 300 });
|
||||
}],
|
||||
zoomIn: ["+", "Zoom in", () => map.zoomIn()],
|
||||
zoomOut: ["−", "Zoom out", () => map.zoomOut()],
|
||||
pitch: ["2D", "2D / 3D", () => map.easeTo({ pitch: map.getPitch() > 0 ? 0 : PITCH_3D, ...(map.getPitch() > 0 && { bearing: 0 }), duration: 300 })],
|
||||
night: ["", "Night mode", () => nightButton.click()],
|
||||
overview: ["Route", "The whole route (the follow pauses)", () => {
|
||||
const coords = route?.geometry?.coordinates;
|
||||
if (!coords?.length) return;
|
||||
pauseFollow(map);
|
||||
map.fitBounds(coords.reduce((b, c) => b.extend(c), new LngLatBounds(coords[0], coords[0])), { padding: 80, pitch: 0, bearing: 0 });
|
||||
}],
|
||||
exit: ["✕", "Leave driving mode", () => set(false)],
|
||||
};
|
||||
for (const [name, [label, title, action]] of Object.entries(buttons)) {
|
||||
const button = document.createElement("button");
|
||||
button.type = "button";
|
||||
button.className = `drive-bar__${name}`;
|
||||
button.textContent = label;
|
||||
button.title = title;
|
||||
button.setAttribute("aria-label", title);
|
||||
button.addEventListener("click", action);
|
||||
bar.appendChild(button);
|
||||
}
|
||||
map.getContainer().appendChild(bar);
|
||||
|
||||
const render = () => {
|
||||
bar.querySelector(".drive-bar__pitch").textContent = map.getPitch() > 0 ? "2D" : "3D";
|
||||
bar.querySelector(".drive-bar__night").textContent = nightButton.textContent;
|
||||
bar.querySelector(".drive-bar__overview").disabled = !route;
|
||||
};
|
||||
map.on("pitchend", render);
|
||||
nightButton.addEventListener("click", render);
|
||||
onRouteChanged(render);
|
||||
|
||||
function set(on) {
|
||||
document.documentElement.classList.toggle("driving", on);
|
||||
bar.hidden = !on;
|
||||
toggle.classList.toggle("active", on);
|
||||
try { localStorage.setItem(KEY, on ? "on" : "off"); } catch { /* private window: not kept */ }
|
||||
render();
|
||||
map.resize(); // the sidebar came or went
|
||||
}
|
||||
toggle.addEventListener("click", () => set(bar.hidden));
|
||||
let saved = null;
|
||||
try { saved = localStorage.getItem(KEY); } catch { /* nothing kept */ }
|
||||
set(saved === "on");
|
||||
}
|
||||
+23
-10
@@ -38,27 +38,40 @@ function freeLookState(map) {
|
||||
button.hidden = true;
|
||||
map.getContainer().appendChild(button);
|
||||
state = { until: 0, button, followedAt: 0 };
|
||||
button.addEventListener("click", () => {
|
||||
state.until = 0;
|
||||
button.hidden = true;
|
||||
clearTimeout(state.timer);
|
||||
});
|
||||
button.addEventListener("click", () => resumeFollow(map));
|
||||
const touched = (e) => {
|
||||
// our own easeTo, or nothing being followed (GNSS off)
|
||||
if (!e.originalEvent || Date.now() - state.followedAt > FOLLOWING_MS) return;
|
||||
state.until = Date.now() + FREE_LOOK_MS;
|
||||
button.hidden = false;
|
||||
clearTimeout(state.timer);
|
||||
state.timer = setTimeout(() => (button.hidden = true), FREE_LOOK_MS);
|
||||
if (e.originalEvent && Date.now() - state.followedAt <= FOLLOWING_MS) pauseFollow(map);
|
||||
};
|
||||
for (const type of ["dragstart", "zoomstart", "rotatestart", "pitchstart", "move"]) map.on(type, touched);
|
||||
freeLook.set(map, state);
|
||||
return state;
|
||||
}
|
||||
|
||||
// Also for a view the app moves to itself (the driving bar's route overview).
|
||||
export function pauseFollow(map) {
|
||||
const state = freeLookState(map);
|
||||
state.until = Date.now() + FREE_LOOK_MS;
|
||||
state.button.hidden = false;
|
||||
clearTimeout(state.timer);
|
||||
state.timer = setTimeout(() => (state.button.hidden = true), FREE_LOOK_MS);
|
||||
}
|
||||
|
||||
export function resumeFollow(map) {
|
||||
const state = freeLookState(map);
|
||||
state.until = 0;
|
||||
state.button.hidden = true;
|
||||
clearTimeout(state.timer);
|
||||
}
|
||||
|
||||
export function followPosition(map, lngLat, heading, speed, duration) {
|
||||
const free = freeLookState(map);
|
||||
free.followedAt = Date.now();
|
||||
// following in 3D like a navigation: the overview map out of the way
|
||||
// (style.css), back once the follow stops or the view goes 2D
|
||||
document.documentElement.classList.toggle("follow-3d", map.getPitch() > 0);
|
||||
clearTimeout(free.followTimer);
|
||||
free.followTimer = setTimeout(() => document.documentElement.classList.remove("follow-3d"), FOLLOWING_MS);
|
||||
if (Date.now() < free.until) return;
|
||||
free.button.hidden = true;
|
||||
const pitched = map.getPitch() > 0;
|
||||
|
||||
+17
-4
@@ -29,6 +29,7 @@ import { offerSharedFiles } from "/js/share_import.js";
|
||||
import HomeButton from "/vendor/npmap-home-button/home-button.esm.js";
|
||||
import OverviewMap from "/vendor/npmap-overview/overview.esm.js";
|
||||
import * as maplibregl from "/vendor/maplibre-gl/maplibre-gl.mjs";
|
||||
import { setupDriving } from "/js/driving.js";
|
||||
|
||||
const CENTER = [17.1077, 48.1486];
|
||||
const ZOOM = 13;
|
||||
@@ -467,10 +468,18 @@ updateSun();
|
||||
map.on("moveend", updateSun);
|
||||
setInterval(updateSun, 60000);
|
||||
|
||||
// Night mode (html.night, style.css) so the screen doesn't glare in the
|
||||
// car: "auto" follows the sun at the map center, or forced on/off — the
|
||||
// topbar button cycles the three, remembered per device.
|
||||
const NIGHT_MODES = { auto: ["◐", "Night mode: automatic (after sunset)"], on: ["☾", "Night mode: on"], off: ["☀", "Night mode: off"] };
|
||||
// Night mode so the screen doesn't glare in the car: "auto" follows the sun
|
||||
// at the map center, or by hand — the topbar button cycles them,
|
||||
// remembered per device.
|
||||
// The UI's palette (style.css, after S-52: Day, Dusk, Night) and the map's
|
||||
// night style: automatic by the sun, or by hand — Dusk the dark UI over the
|
||||
// day map, "for optional use by day as well" (docs/design.adoc).
|
||||
const NIGHT_MODES = {
|
||||
auto: ["◐", "Night mode: automatic (after sunset)"],
|
||||
on: ["☾", "Night mode: on"],
|
||||
dusk: ["◑", "Dusk: the dark UI over the day map"],
|
||||
off: ["☀", "Night mode: off"],
|
||||
};
|
||||
const nightButton = document.querySelector(".topbar__night");
|
||||
const nightMap = new NightMap(map);
|
||||
// The base style's texture patterns (grass, scrub, military hatch) and the
|
||||
@@ -506,6 +515,9 @@ const updateNight = () => {
|
||||
const { lng, lat } = sunAt;
|
||||
const dark = nightMode === "on" || (nightMode === "auto" && getPosition(new Date(), lat, lng).altitude < 0);
|
||||
document.documentElement.classList.toggle("night", dark);
|
||||
const palette = dark ? "night" : nightMode === "dusk" ? "dusk" : null;
|
||||
if (palette) document.documentElement.dataset.palette = palette;
|
||||
else delete document.documentElement.dataset.palette;
|
||||
nightMap.set(dark);
|
||||
[nightButton.textContent, nightButton.title] = NIGHT_MODES[nightMode] || NIGHT_MODES.auto;
|
||||
};
|
||||
@@ -520,6 +532,7 @@ nightButton.addEventListener("click", () => {
|
||||
updateNight();
|
||||
map.on("moveend", updateNight);
|
||||
setInterval(updateNight, 60000);
|
||||
setupDriving(map, nightButton);
|
||||
|
||||
const sidebar = document.getElementById("sidebar");
|
||||
|
||||
|
||||
@@ -123,6 +123,19 @@ export function notifyRouteChanged(route) {
|
||||
document.dispatchEvent(new CustomEvent(ROUTE_CHANGED_EVENT, { detail: route }));
|
||||
}
|
||||
|
||||
// How far along the route the position is, while the dashboard follows it
|
||||
// (km, or null once it's off it / not navigating): the route panel draws
|
||||
// the driven part dimmed and the rest as the active route.
|
||||
const ROUTE_PROGRESS_EVENT = "route-progress";
|
||||
|
||||
export function notifyRouteProgress(km) {
|
||||
document.dispatchEvent(new CustomEvent(ROUTE_PROGRESS_EVENT, { detail: km }));
|
||||
}
|
||||
|
||||
export function onRouteProgress(callback) {
|
||||
document.addEventListener(ROUTE_PROGRESS_EVENT, (event) => callback(event.detail));
|
||||
}
|
||||
|
||||
// Off the route while navigating: the route panel routes again from `from`
|
||||
// ([lon, lat]) to the points not yet reached (`passed` via points skipped).
|
||||
const REROUTE_EVENT = "route-reroute";
|
||||
|
||||
+22
-3
@@ -1,6 +1,6 @@
|
||||
import { Marker } from "/vendor/maplibre-gl/maplibre-gl.mjs";
|
||||
import { ROUTE_ICON, POSITION_ICON, SAVED_POINT_ICON, REVERSE_ICON, CLEAR_ICON, SAVE_ICON } from "/js/icons.js";
|
||||
import { notifySidePanelOpen, onlyOneSidePanelOpen, notifyGpxStoreChanged, onRoutePoint, onRouteEdit, notifyRouteChanged, onReroute } from "/js/panels.js";
|
||||
import { notifySidePanelOpen, onlyOneSidePanelOpen, notifyGpxStoreChanged, onRoutePoint, onRouteEdit, notifyRouteChanged, onReroute, onRouteProgress } from "/js/panels.js";
|
||||
import { isLoggedIn } from "/js/auth.js";
|
||||
import { wirePreview } from "/js/preview.js";
|
||||
import { elevationAt } from "/js/dem.js";
|
||||
@@ -52,12 +52,14 @@ export class RoutePanelControl {
|
||||
// Rerouting (the dashboard, off the route): the start and the via points
|
||||
// already passed give way to the current position.
|
||||
onReroute(({ from, passed }) => {
|
||||
if (this._points.length < 2) return;
|
||||
// the shared trip is rerouted by the server, off its own GNSS (TripStore#follow)
|
||||
if (this._shared || this._points.length < 2) return;
|
||||
this._points.splice(0, 1 + passed).forEach((p) => p.marker.remove());
|
||||
this._addPoint(from, "My position", { quiet: true });
|
||||
this._points.unshift(this._points.pop());
|
||||
this._changed();
|
||||
});
|
||||
onRouteProgress((km) => this._showProgress(km));
|
||||
onRoutePoint(({ lngLat, name }) => {
|
||||
this._setOpen(true);
|
||||
this._addPoint(lngLat, name);
|
||||
@@ -455,7 +457,7 @@ export class RoutePanelControl {
|
||||
source.setData(data);
|
||||
return;
|
||||
}
|
||||
this._map.addSource(SOURCE, { type: "geojson", data });
|
||||
this._map.addSource(SOURCE, { type: "geojson", data, lineMetrics: true }); // line-progress for _showProgress
|
||||
this._map.addLayer({
|
||||
id: `${SOURCE}-casing`,
|
||||
type: "line",
|
||||
@@ -480,6 +482,23 @@ export class RoutePanelControl {
|
||||
});
|
||||
}
|
||||
|
||||
// Navigated (km along the route, docs/design.adoc): the driven part
|
||||
// dimmed, the rest magenta (--route-active); null: the planned blue.
|
||||
_showProgress(km) {
|
||||
if (!this._map.getLayer(`${SOURCE}-line`)) return;
|
||||
const total = this._route?.properties.distance_km;
|
||||
if (km == null || !total) {
|
||||
this._map.setPaintProperty(`${SOURCE}-line`, "line-gradient", undefined);
|
||||
return;
|
||||
}
|
||||
const css = getComputedStyle(document.documentElement);
|
||||
const active = css.getPropertyValue("--route-active").trim() || "#a21caf";
|
||||
const driven = css.getPropertyValue("--ui-muted").trim() || "#6b6b66";
|
||||
const fraction = Math.min(1, Math.max(0, km / total));
|
||||
this._map.setPaintProperty(`${SOURCE}-line`, "line-gradient",
|
||||
fraction > 0 ? ["step", ["line-progress"], driven, fraction, active] : ["step", ["line-progress"], active, 1, active]);
|
||||
}
|
||||
|
||||
// A route item in the library ("Routes" folder), keeping its points and
|
||||
// vehicle so it can be routed again later; an edited one is updated in
|
||||
// place.
|
||||
|
||||
Reference in new issue
Block a user