Files
cruiseSimulator/Assets/Soundings/Runtime/GameGraphics.cs

128 lines
5.5 KiB
C#

using UnityEngine;
using UnityEngine.UIElements;
namespace Soundings
{
public sealed partial class SoundingsApp
{
private static void Shade(VisualElement parent, bool vertical, params (float Stop, Color32 Tint)[] stops)
{
var element = Add(parent, "screen-shade");
element.pickingMode = PickingMode.Ignore;
element.generateVisualContent += context =>
{
Rect rect = context.visualElement.contentRect;
if (rect.width <= 0 || rect.height <= 0)
return;
int strips = stops.Length - 1;
MeshWriteData mesh = context.Allocate(strips * 4, strips * 6);
for (int i = 0; i < strips; i++)
{
var (from, startColor) = stops[i];
var (to, endColor) = stops[i + 1];
float left = vertical ? 0 : from * rect.width;
float right = vertical ? rect.width : to * rect.width;
float top = vertical ? from * rect.height : 0;
float bottom = vertical ? to * rect.height : rect.height;
mesh.SetNextVertex(ShadeVertex(left, top, startColor));
mesh.SetNextVertex(ShadeVertex(right, top, vertical ? startColor : endColor));
mesh.SetNextVertex(ShadeVertex(right, bottom, endColor));
mesh.SetNextVertex(ShadeVertex(left, bottom, vertical ? endColor : startColor));
int v = i * 4;
mesh.SetNextIndex((ushort)v);
mesh.SetNextIndex((ushort)(v + 1));
mesh.SetNextIndex((ushort)(v + 2));
mesh.SetNextIndex((ushort)v);
mesh.SetNextIndex((ushort)(v + 2));
mesh.SetNextIndex((ushort)(v + 3));
}
};
}
private static Vertex ShadeVertex(float x, float y, Color32 color)
{
return new Vertex { position = new Vector3(x, y, Vertex.nearZ), tint = color, uv = Vector2.zero };
}
private static void GameIcon(VisualElement parent, string shape)
{
var icon = Add(parent, "game-icon " + shape + "-icon");
icon.pickingMode = PickingMode.Ignore;
icon.generateVisualContent += context =>
{
Rect area = context.visualElement.contentRect;
if (area.width <= 0 || area.height <= 0)
return;
float scale = Mathf.Min(area.width, area.height) / 24;
Vector2 center = area.center;
Vector2 Point(float x, float y) => center + new Vector2(x - 12, y - 12) * scale;
Painter2D pen = context.painter2D;
pen.lineWidth = 1.32f * scale;
pen.lineCap = LineCap.Round;
pen.lineJoin = LineJoin.Round;
pen.strokeColor = shape == "chart" && parent.ClassListContains("helm-command")
? new Color32(29, 32, 31, 255) : new Color32(218, 192, 144, 255);
pen.BeginPath();
void Path(params float[] points)
{
pen.MoveTo(Point(points[0], points[1]));
for (int i = 2; i < points.Length; i += 2)
pen.LineTo(Point(points[i], points[i + 1]));
}
switch (shape)
{
case "compass":
Path(12, 1, 20, 7, 20, 17, 12, 23, 4, 17, 4, 7, 12, 1);
Path(8, 16, 11, 8, 16, 7, 13, 15, 8, 16);
Path(11, 8, 13, 15);
break;
case "anchor":
pen.Arc(Point(12, 5), 2.2f * scale, 0, 360);
Path(12, 7.5f, 12, 21, 7, 19, 4, 15);
Path(12, 21, 17, 19, 20, 15);
Path(3, 18, 4, 15, 7, 15);
Path(17, 15, 20, 15, 21, 18);
Path(8, 11, 16, 11);
break;
case "decks":
Path(3, 9, 12, 4, 21, 9, 12, 14, 3, 9);
Path(3, 13, 12, 18, 21, 13);
Path(3, 17, 12, 22, 21, 17);
break;
case "chart":
Path(3, 7, 8, 4, 16, 7, 21, 4, 21, 18, 16, 21, 8, 18, 3, 21, 3, 7);
Path(8, 4, 8, 18);
Path(16, 7, 16, 21);
Path(5, 15, 10, 11, 14, 12, 19, 8);
break;
case "yard":
Path(4, 21, 4, 6, 21, 6, 17, 3, 8, 3, 4, 6);
Path(7, 21, 7, 6);
Path(16, 6, 16, 12, 14, 13, 15, 15, 17, 15, 18, 13);
Path(2, 21, 21, 21);
break;
case "log":
Path(5, 5, 11, 5, 12, 7, 13, 5, 20, 5, 20, 19, 13, 19, 12, 21, 11, 19, 5, 19, 5, 5);
Path(12, 7, 12, 21);
Path(7, 10, 10, 10);
Path(14, 10, 18, 10);
Path(14, 13, 17, 13);
break;
default:
Path(5, 7, 19, 7);
Path(5, 12, 19, 12);
Path(5, 17, 19, 17);
break;
}
pen.Stroke();
};
}
}
}