Show live Horizon terrain and radar views in the web panel
This commit is contained in:
@@ -1,3 +1,6 @@
|
||||
import {drawSymbol} from "./symbols.js";
|
||||
import {createTerrain} from "./terrain.js";
|
||||
|
||||
const styles = [
|
||||
["units", "unit", "#6adcd2"],
|
||||
["objects", "object", "#a9baf6"],
|
||||
@@ -6,11 +9,13 @@ const styles = [
|
||||
];
|
||||
const clamp = (x, low, high) => Math.min(high, Math.max(low, x));
|
||||
|
||||
export function createMap(canvas, {onSelect, onView}) {
|
||||
export function createMap(canvas, {onSelect, onView, onTerrain}) {
|
||||
const ctx = canvas.getContext("2d");
|
||||
const view = {x: 0, z: 0, scale: .22};
|
||||
const fingers = new Map();
|
||||
let points = [], visible = [], selected, width = 1, height = 1, drawnFrame;
|
||||
const terrain = createTerrain(schedule);
|
||||
let mode = "map", scope, online, radarId;
|
||||
let moved = false, autoFit = true, queued = false;
|
||||
|
||||
function paint() {
|
||||
@@ -24,28 +29,67 @@ export function createMap(canvas, {onSelect, onView}) {
|
||||
canvas.height = h;
|
||||
}
|
||||
ctx.setTransform(ratio, 0, 0, ratio, 0, 0);
|
||||
ctx.fillStyle = "#101c24";
|
||||
ctx.fillRect(0, 0, width, height);
|
||||
const {x, z, scale} = view;
|
||||
const px = v => (v - x) * scale + width / 2;
|
||||
const py = v => (v - z) * scale + height / 2;
|
||||
const left = x - width / (2 * scale), top = z - height / (2 * scale);
|
||||
const steps = [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000];
|
||||
const step = steps.find(s => s * scale >= 48) || steps.at(-1);
|
||||
ctx.strokeStyle = "#233947";
|
||||
const steps = [5, 10, 25, 50, 100, 250, 500, 1000, 2500, 5000, 10000, 25000];
|
||||
const step = steps.find(s => s * scale >= 54) || steps.at(-1);
|
||||
ctx.fillStyle = mode === "radar" ? "#071718" : "#101c27";
|
||||
ctx.fillRect(0, 0, width, height);
|
||||
ctx.lineWidth = 1;
|
||||
ctx.beginPath();
|
||||
for (let a = Math.ceil(left / step) * step; px(a) < width; a += step) {
|
||||
const line = Math.round(px(a)) + .5;
|
||||
ctx.moveTo(line, 0);
|
||||
ctx.lineTo(line, height);
|
||||
if (mode === "radar") {
|
||||
const station = drawnFrame?.units?.find(unit => String(unit.id) === radarId && unit.radarRange > 0);
|
||||
const cx = station ? px(station.x) : width / 2;
|
||||
const cy = station ? py(station.z) : height / 2;
|
||||
const spacing = step * scale;
|
||||
const min = Math.max(1, Math.floor(Math.hypot(Math.max(-cx, cx - width, 0), Math.max(-cy, cy - height, 0)) / spacing));
|
||||
const max = Math.ceil(Math.hypot(Math.max(Math.abs(cx), Math.abs(cx - width)), Math.max(Math.abs(cy), Math.abs(cy - height))) / spacing);
|
||||
ctx.strokeStyle = "#345c54";
|
||||
ctx.fillStyle = "#8ab5aa";
|
||||
ctx.font = "11px system-ui, sans-serif";
|
||||
for (let ring = min; ring <= max; ring++) {
|
||||
const radius = ring * spacing;
|
||||
ctx.beginPath();
|
||||
ctx.arc(cx, cy, radius, 0, Math.PI * 2);
|
||||
ctx.stroke();
|
||||
ctx.fillText(`${ring * step}`, cx + 5, cy - radius + 14);
|
||||
}
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(cx, 0);
|
||||
ctx.lineTo(cx, height);
|
||||
ctx.moveTo(0, cy);
|
||||
ctx.lineTo(width, cy);
|
||||
ctx.stroke();
|
||||
if (station) {
|
||||
ctx.beginPath();
|
||||
ctx.arc(cx, cy, station.radarRange * scale, 0, Math.PI * 2);
|
||||
ctx.strokeStyle = "#73dbc5";
|
||||
ctx.setLineDash([6, 5]);
|
||||
ctx.stroke();
|
||||
ctx.setLineDash([]);
|
||||
}
|
||||
onTerrain(station ? `${station.label} · номинальная дальность ${Math.round(station.radarRange)} бл.` : "Общая картина сети");
|
||||
} else {
|
||||
ctx.imageSmoothingEnabled = false;
|
||||
const loaded = terrain.paint(ctx, view, width, height, scope, online);
|
||||
ctx.imageSmoothingEnabled = true;
|
||||
const left = x - width / (2 * scale), top = z - height / (2 * scale);
|
||||
ctx.strokeStyle = loaded ? "#d9eff429" : "#253745";
|
||||
ctx.beginPath();
|
||||
for (let a = Math.ceil(left / step) * step; px(a) < width; a += step) {
|
||||
const line = Math.round(px(a)) + .5;
|
||||
ctx.moveTo(line, 0);
|
||||
ctx.lineTo(line, height);
|
||||
}
|
||||
for (let a = Math.ceil(top / step) * step; py(a) < height; a += step) {
|
||||
const line = Math.round(py(a)) + .5;
|
||||
ctx.moveTo(0, line);
|
||||
ctx.lineTo(width, line);
|
||||
}
|
||||
ctx.stroke();
|
||||
onTerrain(online ? loaded ? "Рельеф загруженных чанков" : "Ожидание данных местности" :
|
||||
loaded ? "Последний загруженный рельеф" : "Рельеф доступен в открытом мире");
|
||||
}
|
||||
for (let a = Math.ceil(top / step) * step; py(a) < height; a += step) {
|
||||
const line = Math.round(py(a)) + .5;
|
||||
ctx.moveTo(0, line);
|
||||
ctx.lineTo(width, line);
|
||||
}
|
||||
ctx.stroke();
|
||||
ctx.font = "12px system-ui, sans-serif";
|
||||
ctx.fillStyle = "#b6c8d0";
|
||||
ctx.fillText("↑ N", width - 36, 20);
|
||||
@@ -59,25 +103,11 @@ export function createMap(canvas, {onSelect, onView}) {
|
||||
const x = px(row.x), y = py(row.z);
|
||||
if (x < -40 || y < -40 || x > width + 40 || y > height + 40) continue;
|
||||
visible.push({...point, x, y});
|
||||
const r = point.key === selected ? 8 : 5;
|
||||
ctx.beginPath();
|
||||
if (kind === "unit") {
|
||||
ctx.rect(x - r, y - r, 2 * r, 2 * r);
|
||||
} else if (kind === "contact") {
|
||||
ctx.arc(x, y, r, 0, Math.PI * 2);
|
||||
} else {
|
||||
ctx.moveTo(x, y - r);
|
||||
ctx.lineTo(x + r, kind === "object" ? y : y + r);
|
||||
if (kind === "object") ctx.lineTo(x, y + r);
|
||||
ctx.lineTo(x - r, kind === "object" ? y : y + r);
|
||||
ctx.closePath();
|
||||
}
|
||||
ctx.strokeStyle = ctx.fillStyle = color;
|
||||
ctx.lineWidth = 2;
|
||||
if (kind !== "contact") ctx.fill();
|
||||
if (row.status?.startsWith("Оценка")) ctx.setLineDash([3, 2]);
|
||||
ctx.stroke();
|
||||
ctx.setLineDash([]);
|
||||
const heading = Math.hypot(row.vx, row.vz) > 0 ? Math.atan2(row.vx, -row.vz) : 0;
|
||||
const symbol = row.symbol || (kind === "contact" ? "unknown" : kind);
|
||||
drawSymbol(ctx, x, y, {
|
||||
kind, symbol, color, selected: point.key === selected, uncertain: symbol === "unknown", heading,
|
||||
});
|
||||
if (point.key === selected && row.radius > 0) {
|
||||
ctx.beginPath();
|
||||
ctx.arc(x, y, Math.min(380, row.radius * scale), 0, Math.PI * 2);
|
||||
@@ -86,15 +116,6 @@ export function createMap(canvas, {onSelect, onView}) {
|
||||
ctx.stroke();
|
||||
ctx.setLineDash([]);
|
||||
}
|
||||
if (Number.isFinite(row.vx) && Number.isFinite(row.vz)) {
|
||||
const n = Math.hypot(row.vx, row.vz) || 1;
|
||||
const length = clamp(n * scale / 3, 5, 28);
|
||||
ctx.strokeStyle = color;
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(x, y);
|
||||
ctx.lineTo(x + row.vx / n * length, y + row.vz / n * length);
|
||||
ctx.stroke();
|
||||
}
|
||||
}
|
||||
const focus = visible.find(item => item.key === selected);
|
||||
if (focus) {
|
||||
@@ -123,10 +144,10 @@ export function createMap(canvas, {onSelect, onView}) {
|
||||
ctx.fillStyle = "#eaf7fa";
|
||||
ctx.fillText(label, left + 6, top + 16, length - 12);
|
||||
}
|
||||
if (!points.length) {
|
||||
if (!drawnFrame) {
|
||||
ctx.fillStyle = "#9ab0bb";
|
||||
ctx.textAlign = "center";
|
||||
ctx.fillText(drawnFrame ? "На выбранных слоях нет объектов" : "Нет снимка карты", width / 2, height / 2);
|
||||
ctx.fillText("Нет снимка карты", width / 2, height / 2);
|
||||
ctx.textAlign = "left";
|
||||
}
|
||||
}
|
||||
@@ -210,7 +231,7 @@ export function createMap(canvas, {onSelect, onView}) {
|
||||
let hit, distance = 21;
|
||||
for (const p of visible) {
|
||||
const d = Math.hypot(x - p.x, y - p.y);
|
||||
if (d < distance) [hit, distance] = [p, d];
|
||||
if (d <= distance) [hit, distance] = [p, d];
|
||||
}
|
||||
if (hit) onSelect(hit.key);
|
||||
});
|
||||
@@ -221,14 +242,39 @@ export function createMap(canvas, {onSelect, onView}) {
|
||||
draw(frame, options) {
|
||||
drawnFrame = frame;
|
||||
selected = options.selected;
|
||||
mode = options.mode;
|
||||
scope = options.scope;
|
||||
online = options.online;
|
||||
radarId = options.radarId;
|
||||
points = styles.flatMap(([group, kind, color]) => options.layers[group] === false ? [] : (frame?.[group] || [])
|
||||
.filter(row => Number.isFinite(row.x) && Number.isFinite(row.z))
|
||||
.map(row => ({row, color, kind, key: `${kind}:${row.id}`})));
|
||||
if (autoFit && canvas.clientWidth > 100 && points.length) fit();
|
||||
const index = points.findIndex(point => point.key === selected);
|
||||
if (index >= 0) points.push(points.splice(index, 1)[0]);
|
||||
if (autoFit && canvas.clientWidth > 100) {
|
||||
if (points.length) {
|
||||
fit();
|
||||
} else if (options.center?.every(Number.isFinite)) {
|
||||
view.x = options.center[0];
|
||||
view.z = options.center[1];
|
||||
view.scale = 1.5;
|
||||
autoFit = false;
|
||||
onView({...view});
|
||||
}
|
||||
}
|
||||
schedule();
|
||||
},
|
||||
fit,
|
||||
zoom,
|
||||
clearTerrain: terrain.clear,
|
||||
centerAt(x, z, radius) {
|
||||
view.x = x;
|
||||
view.z = z;
|
||||
if (radius > 0) view.scale = clamp((Math.min(canvas.clientWidth, canvas.clientHeight) - 48) / (radius * 2), .006, 12);
|
||||
autoFit = false;
|
||||
onView({...view});
|
||||
schedule();
|
||||
},
|
||||
restore(saved) {
|
||||
if (saved && [saved.x, saved.z, saved.scale].every(Number.isFinite)) {
|
||||
Object.assign(view, saved, {scale: clamp(saved.scale, .006, 12)});
|
||||
|
||||
Reference in New Issue
Block a user