331 lines
14 KiB
Plaintext
331 lines
14 KiB
Plaintext
@rendermode @(new InteractiveWebAssemblyRenderMode(prerender: false))
|
||
@implements IAsyncDisposable
|
||
@using Lab505.Shared
|
||
@using Lab505.Client.Services
|
||
@inject StateService State
|
||
@inject IJSRuntime JS
|
||
|
||
<div class="mp-wrap" @ref="_el"
|
||
@onmousedown="OnMouseDown"
|
||
@onmousemove="OnMouseMove"
|
||
@onmouseup="OnMouseUp"
|
||
@onmouseleave="OnMouseLeave"
|
||
@ontouchstart="OnTouchStart"
|
||
@ontouchmove="OnTouchMove"
|
||
@ontouchend="OnTouchEnd"
|
||
style="cursor:@(_dragging?"grabbing":"grab")">
|
||
|
||
<svg width="100%" height="100%" xmlns="http://www.w3.org/2000/svg" style="display:block">
|
||
<defs>
|
||
<!-- 1 m minor grid -->
|
||
<pattern id="mp-g1"
|
||
width="@F(_s)" height="@F(_s)"
|
||
x="@F(Mod(_panX, _s))" y="@F(Mod(_panY, _s))"
|
||
patternUnits="userSpaceOnUse">
|
||
<path d="M @F(_s) 0 L 0 0 0 @F(_s)" fill="none" stroke="#232323" stroke-width="1"/>
|
||
</pattern>
|
||
<!-- 5 m major grid -->
|
||
<pattern id="mp-g5"
|
||
width="@F(5*_s)" height="@F(5*_s)"
|
||
x="@F(Mod(_panX, 5*_s))" y="@F(Mod(_panY, 5*_s))"
|
||
patternUnits="userSpaceOnUse">
|
||
<path d="M @F(5*_s) 0 L 0 0 0 @F(5*_s)" fill="none" stroke="#2c2c2c" stroke-width="1.5"/>
|
||
</pattern>
|
||
</defs>
|
||
|
||
<!-- Background + grid layers -->
|
||
<rect width="100%" height="100%" fill="#1a1a1a"/>
|
||
<rect width="100%" height="100%" fill="url(#mp-g1)"/>
|
||
<rect width="100%" height="100%" fill="url(#mp-g5)"/>
|
||
|
||
<!-- Axes -->
|
||
<line x1="@F(ToX(-100))" y1="@F(ToY(0))" x2="@F(ToX(100))" y2="@F(ToY(0))"
|
||
stroke="#2e2e2e" stroke-width="1.5"/>
|
||
<line x1="@F(ToX(0))" y1="@F(ToY(-100))" x2="@F(ToX(0))" y2="@F(ToY(100))"
|
||
stroke="#2e2e2e" stroke-width="1.5"/>
|
||
|
||
<!-- Grid coordinate labels (X axis, every 5 m) -->
|
||
@for (int gx = -15; gx <= 15; gx += 5)
|
||
{
|
||
double lx = ToX(gx), ly = ToY(0) + 14;
|
||
if (lx > 10 && lx < 3000)
|
||
{
|
||
@SvgTxt(lx, ly, "#383838", 10, "middle", "", gx.ToString())
|
||
}
|
||
}
|
||
<!-- Grid coordinate labels (Y axis, every 5 m) -->
|
||
@for (int gy = -15; gy <= 15; gy += 5)
|
||
{
|
||
if (gy == 0) continue;
|
||
double lx = ToX(0) + 5, ly = ToY(gy);
|
||
if (ly > 10 && ly < 3000)
|
||
{
|
||
@SvgTxt(lx, ly, "#383838", 10, "start", "middle", gy.ToString())
|
||
}
|
||
}
|
||
|
||
<!-- IMR trail -->
|
||
@if (_imrTrail.Count > 1)
|
||
{
|
||
<polyline points="@TrailPts(_imrTrail)"
|
||
fill="none" stroke="#4a9eff" stroke-width="2.5"
|
||
stroke-opacity="0.35" stroke-linejoin="round" stroke-linecap="round"/>
|
||
}
|
||
|
||
<!-- AMR trail -->
|
||
@if (_amrTrail.Count > 1)
|
||
{
|
||
<polyline points="@TrailPts(_amrTrail)"
|
||
fill="none" stroke="#4caf50" stroke-width="2.5"
|
||
stroke-opacity="0.35" stroke-linejoin="round" stroke-linecap="round"/>
|
||
}
|
||
|
||
<!-- Waypoints -->
|
||
@foreach (var wp in _waypoints)
|
||
{
|
||
double wx = ToX(wp.X), wy = ToY(wp.Y);
|
||
<g style="pointer-events:none">
|
||
<line x1="@F(wx-6)" y1="@F(wy)" x2="@F(wx+6)" y2="@F(wy)"
|
||
stroke="@wp.Color" stroke-width="1.5" opacity="0.4"/>
|
||
<line x1="@F(wx)" y1="@F(wy-6)" x2="@F(wx)" y2="@F(wy+6)"
|
||
stroke="@wp.Color" stroke-width="1.5" opacity="0.4"/>
|
||
<circle cx="@F(wx)" cy="@F(wy)" r="4"
|
||
fill="none" stroke="@wp.Color" stroke-width="1.5" opacity="0.5"/>
|
||
@SvgTxtColor(wx, wy - 11, wp.Color, 11, "middle", "", wp.Name, "opacity:0.6;user-select:none")
|
||
</g>
|
||
}
|
||
|
||
<!-- IMR robot -->
|
||
@{
|
||
double ix = ToX(State.Imr.X), iy = ToY(State.Imr.Y);
|
||
bool io = State.Imr.IsOnline;
|
||
string ic = io ? "#4a9eff" : "#333";
|
||
}
|
||
<g style="pointer-events:none">
|
||
@if (io) { <circle cx="@F(ix)" cy="@F(iy)" r="20" fill="#4a9eff" fill-opacity="0.07"/> }
|
||
<circle cx="@F(ix)" cy="@F(iy)" r="9" fill="@ic" stroke="#111" stroke-width="2"/>
|
||
<polygon points="@F(ix),@F(iy-15) @F(ix-4),@F(iy-9) @F(ix+4),@F(iy-9)"
|
||
fill="@ic" opacity="0.7"/>
|
||
<text x="@F(ix)" y="@F(iy - 20)" fill="@ic"
|
||
font-size="11" font-weight="600" text-anchor="middle"
|
||
style="user-select:none">IMR</text>
|
||
<text x="@F(ix)" y="@F(iy + 26)" fill="#505050"
|
||
font-size="9" text-anchor="middle"
|
||
style="user-select:none">(@State.Imr.X.ToString("F2", CI),@State.Imr.Y.ToString("F2", CI))</text>
|
||
</g>
|
||
|
||
<!-- AMR robot -->
|
||
@{
|
||
double ax = ToX(State.Amr.X), ay = ToY(State.Amr.Y);
|
||
bool ao = State.Amr.IsOnline;
|
||
string ac = ao ? "#4caf50" : "#333";
|
||
}
|
||
<g style="pointer-events:none">
|
||
@if (ao) { <circle cx="@F(ax)" cy="@F(ay)" r="20" fill="#4caf50" fill-opacity="0.07"/> }
|
||
<circle cx="@F(ax)" cy="@F(ay)" r="9" fill="@ac" stroke="#111" stroke-width="2"/>
|
||
<polygon points="@F(ax),@F(ay-15) @F(ax-4),@F(ay-9) @F(ax+4),@F(ay-9)"
|
||
fill="@ac" opacity="0.7"/>
|
||
<text x="@F(ax)" y="@F(ay - 20)" fill="@ac"
|
||
font-size="11" font-weight="600" text-anchor="middle"
|
||
style="user-select:none">AMR</text>
|
||
<text x="@F(ax)" y="@F(ay + 26)" fill="#505050"
|
||
font-size="9" text-anchor="middle"
|
||
style="user-select:none">(@State.Amr.X.ToString("F2", CI),@State.Amr.Y.ToString("F2", CI))</text>
|
||
</g>
|
||
|
||
<!-- Legend (top-left) -->
|
||
<g transform="translate(10,10)" style="pointer-events:none">
|
||
<rect width="116" height="68" rx="5"
|
||
fill="rgba(0,0,0,0.72)" stroke="#2a2a2a" stroke-width="1"/>
|
||
<circle cx="14" cy="19" r="5" fill="@(io ? "#4a9eff" : "#333")"/>
|
||
<text x="24" y="23" fill="#aaa" font-size="11">IMR @(State.Imr.IsOnline ? "●" : "○")</text>
|
||
<circle cx="14" cy="39" r="5" fill="@(ao ? "#4caf50" : "#333")"/>
|
||
<text x="24" y="43" fill="#aaa" font-size="11">AMR @(State.Amr.IsOnline ? "●" : "○")</text>
|
||
<text x="8" y="59" fill="#3a3a3a" font-size="9">1 ô = 1 m²</text>
|
||
</g>
|
||
</svg>
|
||
|
||
<div class="mp-zoom-badge">×@_zoom.ToString("F1", CI)</div>
|
||
<button class="mp-reset" @onclick="ResetView" title="Reset view">⊙</button>
|
||
</div>
|
||
|
||
@code {
|
||
ElementReference _el;
|
||
DotNetObjectReference<MapPanel>? _self;
|
||
|
||
// ── Pan / Zoom ────────────────────────────────────────────────
|
||
double _panX = 150, _panY = 180;
|
||
double _zoom = 1.0;
|
||
const double CELL = 40.0;
|
||
double _s => _zoom * CELL;
|
||
|
||
// ── Drag ─────────────────────────────────────────────────────
|
||
bool _dragging;
|
||
double _dragCX, _dragCY, _dragPX, _dragPY;
|
||
|
||
// ── Trail ─────────────────────────────────────────────────────
|
||
const int MAX_TRAIL = 200;
|
||
readonly Queue<(double X, double Y)> _imrTrail = new();
|
||
readonly Queue<(double X, double Y)> _amrTrail = new();
|
||
|
||
// ── Waypoints ─────────────────────────────────────────────────
|
||
record Wp(string Name, double X, double Y, string Color);
|
||
static readonly Wp[] _waypoints =
|
||
[
|
||
new("C1", RobotPositions.IMR_C1_X, RobotPositions.IMR_C1_Y, "#5b9bd5"),
|
||
new("ABB", RobotPositions.IMR_ABB_X, RobotPositions.IMR_ABB_Y, "#5b9bd5"),
|
||
new("Cap1", RobotPositions.AMR_CAP1_X, RobotPositions.AMR_CAP1_Y, "#5cb85c"),
|
||
new("Cap2", RobotPositions.AMR_CAP2_X, RobotPositions.AMR_CAP2_Y, "#5cb85c"),
|
||
new("Home", RobotPositions.AMR_HOME_X, RobotPositions.AMR_HOME_Y, "#888"),
|
||
];
|
||
|
||
static readonly System.Globalization.CultureInfo CI =
|
||
System.Globalization.CultureInfo.InvariantCulture;
|
||
|
||
// ── Lifecycle ─────────────────────────────────────────────────
|
||
protected override void OnInitialized()
|
||
{
|
||
State.ImrChanged += OnImrChanged;
|
||
State.AmrChanged += OnAmrChanged;
|
||
}
|
||
|
||
protected override async Task OnAfterRenderAsync(bool firstRender)
|
||
{
|
||
if (!firstRender) return;
|
||
_self = DotNetObjectReference.Create(this);
|
||
await JS.InvokeVoidAsync("eval",
|
||
"if(!window._mpWheelInit)window._mpWheelInit=function(el,dn){" +
|
||
"el.addEventListener('wheel',function(e){e.preventDefault();" +
|
||
"var r=el.getBoundingClientRect();" +
|
||
"dn.invokeMethodAsync('OnWheelJs',e.deltaY,e.clientX-r.left,e.clientY-r.top);" +
|
||
"},{passive:false});};");
|
||
await JS.InvokeVoidAsync("_mpWheelInit", _el, _self);
|
||
}
|
||
|
||
[JSInvokable]
|
||
public void OnWheelJs(double deltaY, double offsetX, double offsetY)
|
||
{
|
||
double factor = deltaY < 0 ? 1.15 : 1.0 / 1.15;
|
||
double newZoom = Math.Clamp(_zoom * factor, 0.12, 12.0);
|
||
double f = newZoom / _zoom;
|
||
_panX = offsetX + (_panX - offsetX) * f;
|
||
_panY = offsetY + (_panY - offsetY) * f;
|
||
_zoom = newZoom;
|
||
InvokeAsync(StateHasChanged);
|
||
}
|
||
|
||
void ResetView() { _panX = 150; _panY = 180; _zoom = 1.0; }
|
||
|
||
// ── Mouse pan ─────────────────────────────────────────────────
|
||
void OnMouseDown(MouseEventArgs e)
|
||
{
|
||
if (e.Button != 0) return;
|
||
_dragging = true;
|
||
_dragCX = e.ClientX; _dragCY = e.ClientY;
|
||
_dragPX = _panX; _dragPY = _panY;
|
||
}
|
||
void OnMouseMove(MouseEventArgs e)
|
||
{
|
||
if (!_dragging) return;
|
||
_panX = _dragPX + (e.ClientX - _dragCX);
|
||
_panY = _dragPY + (e.ClientY - _dragCY);
|
||
}
|
||
void OnMouseUp(MouseEventArgs e) { if (e.Button == 0) _dragging = false; }
|
||
void OnMouseLeave(MouseEventArgs e) { _dragging = false; }
|
||
|
||
// ── Touch pan ─────────────────────────────────────────────────
|
||
void OnTouchStart(TouchEventArgs e)
|
||
{
|
||
if (e.Touches.Length != 1) return;
|
||
var t = e.Touches[0]; _dragging = true;
|
||
_dragCX = t.ClientX; _dragCY = t.ClientY;
|
||
_dragPX = _panX; _dragPY = _panY;
|
||
}
|
||
void OnTouchMove(TouchEventArgs e)
|
||
{
|
||
if (!_dragging || e.Touches.Length != 1) return;
|
||
var t = e.Touches[0];
|
||
_panX = _dragPX + (t.ClientX - _dragCX);
|
||
_panY = _dragPY + (t.ClientY - _dragCY);
|
||
}
|
||
void OnTouchEnd(TouchEventArgs e) { _dragging = false; }
|
||
|
||
// ── State updates ─────────────────────────────────────────────
|
||
void OnImrChanged() { AddTrail(_imrTrail, State.Imr.X, State.Imr.Y); InvokeAsync(StateHasChanged); }
|
||
void OnAmrChanged() { AddTrail(_amrTrail, State.Amr.X, State.Amr.Y); InvokeAsync(StateHasChanged); }
|
||
|
||
static void AddTrail(Queue<(double, double)> q, double x, double y)
|
||
{
|
||
if (q.TryPeek(out var last)
|
||
&& Math.Abs(last.Item1 - x) < 0.005
|
||
&& Math.Abs(last.Item2 - y) < 0.005) return;
|
||
q.Enqueue((x, y));
|
||
while (q.Count > MAX_TRAIL) q.Dequeue();
|
||
}
|
||
|
||
// ── Coordinate helpers ────────────────────────────────────────
|
||
double ToX(double wx) => _panX + wx * _s;
|
||
double ToY(double wy) => _panY - wy * _s; // flip Y: VDA5050 Y-up → SVG Y-down
|
||
static double Mod(double a, double b) => ((a % b) + b) % b;
|
||
static string F(double v) => v.ToString("F2", CI);
|
||
|
||
string TrailPts(Queue<(double X, double Y)> q)
|
||
=> string.Join(" ", q.Select(p => $"{F(ToX(p.X))},{F(ToY(p.Y))}"));
|
||
|
||
// ── SVG <text> helpers (avoid Razor parser conflict inside code blocks) ──
|
||
// Blazor treats <text> as a Razor directive inside @for/@foreach — use MarkupString instead.
|
||
static MarkupString SvgTxt(double x, double y, string fill, int size,
|
||
string anchor, string baseline, string content)
|
||
{
|
||
var bl = baseline.Length > 0 ? $" dominant-baseline=\"{baseline}\"" : "";
|
||
return (MarkupString)(
|
||
$"<text x=\"{x:F2}\" y=\"{y:F2}\" fill=\"{fill}\" font-size=\"{size}\"" +
|
||
$" text-anchor=\"{anchor}\"{bl}" +
|
||
$" style=\"pointer-events:none;user-select:none\">{content}</text>");
|
||
}
|
||
|
||
static MarkupString SvgTxtColor(double x, double y, string fill, int size,
|
||
string anchor, string baseline, string content, string style)
|
||
{
|
||
var bl = baseline.Length > 0 ? $" dominant-baseline=\"{baseline}\"" : "";
|
||
return (MarkupString)(
|
||
$"<text x=\"{x:F2}\" y=\"{y:F2}\" fill=\"{fill}\" font-size=\"{size}\"" +
|
||
$" text-anchor=\"{anchor}\"{bl}" +
|
||
$" style=\"{style}\">{content}</text>");
|
||
}
|
||
|
||
public ValueTask DisposeAsync()
|
||
{
|
||
State.ImrChanged -= OnImrChanged;
|
||
State.AmrChanged -= OnAmrChanged;
|
||
_self?.Dispose();
|
||
return ValueTask.CompletedTask;
|
||
}
|
||
}
|
||
|
||
<style>
|
||
.mp-wrap {
|
||
width: 100%; height: 100%;
|
||
position: relative;
|
||
overflow: hidden;
|
||
background: #1a1a1a;
|
||
touch-action: none;
|
||
user-select: none;
|
||
-webkit-user-select: none;
|
||
}
|
||
.mp-zoom-badge {
|
||
position: absolute; bottom: 12px; left: 12px;
|
||
color: #383838; font-size: 10px;
|
||
font-family: monospace; pointer-events: none;
|
||
}
|
||
.mp-reset {
|
||
position: absolute; bottom: 10px; right: 10px;
|
||
width: 28px; height: 28px;
|
||
background: rgba(0,0,0,0.65); border: 1px solid #2e2e2e;
|
||
border-radius: 5px; color: #555; font-size: 15px;
|
||
cursor: pointer; padding: 0; line-height: 1;
|
||
transition: color .15s, border-color .15s;
|
||
}
|
||
.mp-reset:hover { color: #aaa; border-color: #555; }
|
||
</style>
|