299 lines
14 KiB
C#
299 lines
14 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.UIElements;
|
|
|
|
namespace Soundings
|
|
{
|
|
public sealed partial class SoundingsApp
|
|
{
|
|
private const int MapSamples = 40;
|
|
private static readonly Color MapCompleted = new(.49f, .66f, .65f);
|
|
private static readonly Color MapCurrent = new(.98f, .79f, .44f);
|
|
private static readonly Color MapPlanned = new(.31f, .79f, .68f);
|
|
private bool voyageMapOpen;
|
|
private VisualElement voyageMapPlot;
|
|
private Label voyageMapReadout;
|
|
private float nextMapUpdate;
|
|
|
|
private void ToggleVoyageMap()
|
|
{
|
|
voyageMapOpen = !voyageMapOpen;
|
|
Render();
|
|
}
|
|
|
|
private void DrawVoyageMap(VisualElement overlay)
|
|
{
|
|
voyageMapPlot = null;
|
|
if (!voyageMapOpen)
|
|
return;
|
|
|
|
CruiseItinerary itinerary = Ship.Itinerary;
|
|
bool scheduled = itinerary is { HasNext: true } && itinerary.NextPort == underway.Destination;
|
|
int currentLeg = scheduled ? itinerary.CompletedStops : 0;
|
|
var ports = new int[scheduled ? itinerary.TotalStops + 1 : 2];
|
|
ports[0] = scheduled ? itinerary.Origin : underway.Origin;
|
|
if (scheduled)
|
|
itinerary.Stops.CopyTo(ports, 1);
|
|
else
|
|
ports[1] = underway.Destination;
|
|
|
|
Vector2[][] paths = MapPaths(ports);
|
|
var chart = Add(overlay, "voyage-map");
|
|
var header = Add(chart, "voyage-map-header");
|
|
var heading = Add(header, "voyage-map-heading");
|
|
AddDisplay(heading, "Voyage map", "voyage-map-title");
|
|
AddText(heading, scheduled ? $"Leg {currentLeg + 1} of {ports.Length - 1} · {underway.ShipName}" : underway.ShipName,
|
|
"voyage-map-readout");
|
|
header.Add(Action("Close M", ToggleVoyageMap, "voyage-view-button", "voyage-map-close"));
|
|
|
|
var plot = Add(chart, "voyage-map-plot");
|
|
voyageMapPlot = plot;
|
|
AddText(plot, "N ↑", "voyage-map-north").pickingMode = PickingMode.Ignore;
|
|
var labels = new Dictionary<int, Label>();
|
|
foreach (int port in ports)
|
|
{
|
|
if (labels.ContainsKey(port))
|
|
continue;
|
|
Label label = AddText(plot, Market.Ports[port].Name, "voyage-map-port");
|
|
label.pickingMode = PickingMode.Ignore;
|
|
if (port == underway.Destination)
|
|
label.AddToClassList("voyage-map-port-next");
|
|
labels.Add(port, label);
|
|
}
|
|
plot.generateVisualContent += context => PaintVoyageMap(context, paths, currentLeg);
|
|
plot.RegisterCallback<GeometryChangedEvent>(change => PlaceMapLabels(change.newRect.size, paths, ports, labels));
|
|
|
|
var footer = Add(chart, "voyage-map-footer");
|
|
voyageMapReadout = AddText(footer, "", "voyage-map-readout");
|
|
var legend = Add(footer, "voyage-map-legend");
|
|
if (currentLeg > 0)
|
|
MapLegend(legend, "Sailed", MapCompleted);
|
|
MapLegend(legend, "Current leg", MapCurrent);
|
|
if (currentLeg < paths.Length - 1)
|
|
MapLegend(legend, "Later calls", MapPlanned);
|
|
nextMapUpdate = 0;
|
|
UpdateVoyageMap();
|
|
}
|
|
|
|
private static void MapLegend(VisualElement legend, string text, Color color)
|
|
{
|
|
var key = Add(legend, "voyage-map-key");
|
|
Add(key, "voyage-map-swatch").style.backgroundColor = color;
|
|
AddText(key, text, "voyage-map-readout");
|
|
}
|
|
|
|
private void UpdateVoyageMap()
|
|
{
|
|
if (voyageMapPlot == null || Time.unscaledTime < nextMapUpdate)
|
|
return;
|
|
nextMapUpdate = Time.unscaledTime + .08f;
|
|
float duration = PassageDuration(underway);
|
|
int distance = Mathf.RoundToInt(underway.Miles * passageHelm.RemainingSeconds / duration);
|
|
int heading = Mathf.RoundToInt(Mathf.Repeat(CourseBearing(Market.Ports[underway.Origin], Market.Ports[underway.Destination])
|
|
+ passageHelm.Heading, 360)) % 360;
|
|
SetPassageText(voyageMapReadout, $"{Market.Ports[underway.Destination].Name} · {distance:N0} nm · {heading:000}° · {passageHelm.Speed:F1} kn");
|
|
voyageMapPlot.MarkDirtyRepaint();
|
|
}
|
|
|
|
private static Vector2[][] MapPaths(int[] ports)
|
|
{
|
|
var paths = new Vector2[ports.Length - 1][];
|
|
float longitude = Market.Ports[ports[0]].Longitude;
|
|
for (int leg = 0; leg < paths.Length; leg++)
|
|
{
|
|
PortSpec origin = Market.Ports[ports[leg]], dest = Market.Ports[ports[leg + 1]];
|
|
float start = longitude;
|
|
longitude = start + Mathf.DeltaAngle(start, dest.Longitude);
|
|
Vector3 a = GlobePoint(origin), b = GlobePoint(dest);
|
|
Vector2[] path = paths[leg] = new Vector2[MapSamples + 1];
|
|
for (int s = 0; s <= MapSamples; s++)
|
|
{
|
|
float t = (float)s / MapSamples;
|
|
Vector3 point = Vector3.Slerp(a, b, t);
|
|
float expected = Mathf.Lerp(start, longitude, t);
|
|
float lon = Mathf.Atan2(point.z, point.x) * Mathf.Rad2Deg;
|
|
path[s] = new Vector2(expected + Mathf.DeltaAngle(expected, lon), -Mathf.Asin(point.y) * Mathf.Rad2Deg);
|
|
}
|
|
}
|
|
return paths;
|
|
}
|
|
|
|
private static Vector3 GlobePoint(PortSpec port)
|
|
{
|
|
float lat = port.Latitude * Mathf.Deg2Rad, lon = port.Longitude * Mathf.Deg2Rad;
|
|
return new Vector3(Mathf.Cos(lat) * Mathf.Cos(lon), Mathf.Sin(lat), Mathf.Cos(lat) * Mathf.Sin(lon));
|
|
}
|
|
|
|
private readonly struct MapProjection
|
|
{
|
|
public readonly Vector2 Center, Dimensions;
|
|
public readonly float Scale;
|
|
|
|
public MapProjection(Vector2 size, Vector2[][] paths)
|
|
{
|
|
var minimum = paths[0][0];
|
|
var maximum = minimum;
|
|
foreach (Vector2[] path in paths)
|
|
foreach (Vector2 point in path)
|
|
{
|
|
minimum = Vector2.Min(minimum, point);
|
|
maximum = Vector2.Max(maximum, point);
|
|
}
|
|
Center = (minimum + maximum) * .5f;
|
|
Vector2 span = Vector2.Max(maximum - minimum, new Vector2(5.5f, 3.1f));
|
|
Scale = Mathf.Max(.01f, Mathf.Min((size.x - 146) / span.x, (size.y - 86) / span.y));
|
|
Dimensions = size;
|
|
}
|
|
|
|
public Vector2 Pixel(Vector2 point) => Dimensions * .5f + (point - Center) * Scale;
|
|
}
|
|
|
|
private static void PlaceMapLabels(Vector2 size, Vector2[][] paths, int[] ports, Dictionary<int, Label> labels)
|
|
{
|
|
var projection = new MapProjection(size, paths);
|
|
var used = new HashSet<int>();
|
|
var rows = new List<Rect>(ports.Length);
|
|
for (int i = 0; i < ports.Length; i++)
|
|
{
|
|
int port = ports[i];
|
|
if (!used.Add(port))
|
|
continue;
|
|
Vector2 pixel = projection.Pixel(i == 0 ? paths[0][0] : paths[i - 1][MapSamples]);
|
|
Label label = labels[port];
|
|
float width = Mathf.Min(150, label.text.Length * 7 + 12);
|
|
float x = Mathf.Clamp(pixel.x + 10, 7, Mathf.Max(7, size.x - width - 7));
|
|
float y = Mathf.Clamp(pixel.y - 21, 6, Mathf.Max(6, size.y - 26));
|
|
var area = new Rect(x, y, width, 20);
|
|
for (int j = 0; j < rows.Count; j++)
|
|
{
|
|
if (area.Overlaps(rows[j]))
|
|
area.y = Mathf.Min(size.y - 26, rows[j].yMax + 2);
|
|
}
|
|
rows.Add(area);
|
|
label.style.left = area.x;
|
|
label.style.top = area.y;
|
|
}
|
|
}
|
|
|
|
private void PaintVoyageMap(MeshGenerationContext context, Vector2[][] paths, int currentLeg)
|
|
{
|
|
Vector2 size = context.visualElement.contentRect.size;
|
|
if (size.x < 50 || size.y < 50)
|
|
return;
|
|
var projection = new MapProjection(size, paths);
|
|
Painter2D pen = context.painter2D;
|
|
PaintMapGrid(pen, projection);
|
|
PaintMapCoast(pen, projection);
|
|
|
|
for (int i = 0; i < paths.Length; i++)
|
|
{
|
|
if (i == currentLeg)
|
|
continue;
|
|
Color color = i < currentLeg ? MapCompleted : MapPlanned;
|
|
PaintMapPath(pen, projection, paths[i], color, 1.65f);
|
|
PaintMapPort(pen, projection.Pixel(paths[i][0]), color);
|
|
PaintMapPort(pen, projection.Pixel(paths[i][MapSamples]), color);
|
|
}
|
|
|
|
Vector2[] current = paths[currentLeg];
|
|
PaintMapPath(pen, projection, current, new Color(.98f, .79f, .44f, .21f), 8);
|
|
PaintMapPath(pen, projection, current, MapCurrent, 2.6f);
|
|
PaintMapPort(pen, projection.Pixel(current[0]), MapCurrent);
|
|
PaintMapPort(pen, projection.Pixel(current[MapSamples]), MapCurrent);
|
|
Vector2 travel = passageHelm.Position / PassageDuration(underway);
|
|
float step = travel.x * MapSamples;
|
|
int segment = Mathf.Clamp(Mathf.FloorToInt(step), 0, MapSamples - 1);
|
|
Vector2 tangent = (current[segment + 1] - current[segment]).normalized;
|
|
Vector2 right = new(-tangent.y, tangent.x);
|
|
float span = Vector2.Distance(current[0], current[MapSamples]);
|
|
Vector2 course = Vector2.LerpUnclamped(current[segment], current[segment + 1], step - segment);
|
|
Vector2 location = projection.Pixel(course + right * (travel.y * span));
|
|
location.x = Mathf.Clamp(location.x, 14, size.x - 14);
|
|
location.y = Mathf.Clamp(location.y, 14, size.y - 14);
|
|
float angle = passageHelm.Heading * Mathf.Deg2Rad;
|
|
Vector2 forward = tangent * Mathf.Cos(angle) + right * Mathf.Sin(angle);
|
|
Vector2 wing = new(-forward.y, forward.x);
|
|
pen.fillColor = MapCurrent;
|
|
pen.strokeColor = new Color(.09f, .18f, .22f);
|
|
pen.lineWidth = 1.5f;
|
|
pen.BeginPath();
|
|
pen.MoveTo(location + forward * 11);
|
|
pen.LineTo(location - forward * 8 + wing * 6);
|
|
pen.LineTo(location - forward * 4);
|
|
pen.LineTo(location - forward * 8 - wing * 6);
|
|
pen.ClosePath();
|
|
pen.Fill();
|
|
pen.Stroke();
|
|
}
|
|
|
|
private static void PaintMapGrid(Painter2D pen, MapProjection map)
|
|
{
|
|
Vector2 reach = map.Dimensions / (2 * map.Scale);
|
|
float wanted = reach.x / 4;
|
|
float factor = Mathf.Pow(10, Mathf.Floor(Mathf.Log10(wanted)));
|
|
float interval = factor * (wanted / factor >= 5 ? 5 : wanted / factor >= 2 ? 2 : 1);
|
|
pen.strokeColor = new Color(.37f, .59f, .62f, .26f);
|
|
pen.lineWidth = 1;
|
|
pen.BeginPath();
|
|
for (float lon = Mathf.Ceil((map.Center.x - reach.x) / interval) * interval; lon <= map.Center.x + reach.x; lon += interval)
|
|
{
|
|
float x = map.Pixel(new Vector2(lon, 0)).x;
|
|
pen.MoveTo(new Vector2(x, 0));
|
|
pen.LineTo(new Vector2(x, map.Dimensions.y));
|
|
}
|
|
for (float lat = Mathf.Ceil((map.Center.y - reach.y) / interval) * interval; lat <= map.Center.y + reach.y; lat += interval)
|
|
{
|
|
float y = map.Pixel(new Vector2(0, lat)).y;
|
|
pen.MoveTo(new Vector2(0, y));
|
|
pen.LineTo(new Vector2(map.Dimensions.x, y));
|
|
}
|
|
pen.Stroke();
|
|
}
|
|
|
|
private static void PaintMapCoast(Painter2D pen, MapProjection projection)
|
|
{
|
|
float reach = projection.Dimensions.x / (2 * projection.Scale);
|
|
int first = Mathf.FloorToInt((projection.Center.x - reach + 180) / 360);
|
|
int last = Mathf.FloorToInt((projection.Center.x + reach + 180) / 360);
|
|
pen.fillColor = new Color(.14f, .27f, .28f);
|
|
pen.strokeColor = new Color(.48f, .61f, .51f, .73f);
|
|
pen.lineWidth = 1.1f;
|
|
for (int zone = first; zone <= last; zone++)
|
|
foreach (Vector2[] coast in WorldCoastlines.Shapes)
|
|
{
|
|
pen.BeginPath();
|
|
for (int i = 0; i < coast.Length; i++)
|
|
{
|
|
Vector2 visual = projection.Pixel(new Vector2(coast[i].x * 360 - 180 + zone * 360, coast[i].y * 137 - 76));
|
|
if (i == 0)
|
|
pen.MoveTo(visual);
|
|
else
|
|
pen.LineTo(visual);
|
|
}
|
|
pen.ClosePath();
|
|
pen.Fill();
|
|
pen.Stroke();
|
|
}
|
|
}
|
|
|
|
private static void PaintMapPath(Painter2D pen, MapProjection projection, Vector2[] path, Color color, float width)
|
|
{
|
|
pen.strokeColor = color;
|
|
pen.lineWidth = width;
|
|
pen.BeginPath();
|
|
pen.MoveTo(projection.Pixel(path[0]));
|
|
for (int i = 1; i < path.Length; i++)
|
|
pen.LineTo(projection.Pixel(path[i]));
|
|
pen.Stroke();
|
|
}
|
|
|
|
private static void PaintMapPort(Painter2D pen, Vector2 port, Color color)
|
|
{
|
|
pen.fillColor = color;
|
|
pen.BeginPath();
|
|
pen.Arc(port, 4.1f, 0, 360);
|
|
pen.Fill();
|
|
}
|
|
}
|
|
}
|