AI: Add in-game galaxy map as MapEditor canvas background
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
using Microsoft.Maui.Graphics;
|
using Microsoft.Maui.Graphics;
|
||||||
using ZeroSpace.MapEditor.Models;
|
using ZeroSpace.MapEditor.Models;
|
||||||
|
using ZeroSpace.MapEditor.Services;
|
||||||
|
|
||||||
namespace ZeroSpace.MapEditor.Views;
|
namespace ZeroSpace.MapEditor.Views;
|
||||||
|
|
||||||
@@ -25,11 +26,19 @@ public sealed class MapDrawable : IDrawable
|
|||||||
|
|
||||||
public RectF? SelectionBox { get; set; }
|
public RectF? SelectionBox { get; set; }
|
||||||
|
|
||||||
|
/// <summary>In-game galaxy map screenshot, drawn under the nodes.</summary>
|
||||||
|
public IImage? BackgroundImage { get; set; }
|
||||||
|
|
||||||
|
/// <summary>Whether the galaxy map background is shown.</summary>
|
||||||
|
public bool ShowBackground { get; set; } = true;
|
||||||
|
|
||||||
public void Draw(ICanvas canvas, RectF dirtyRect)
|
public void Draw(ICanvas canvas, RectF dirtyRect)
|
||||||
{
|
{
|
||||||
canvas.FillColor = Color.FromArgb("#1e2430");
|
canvas.FillColor = Color.FromArgb("#1e2430");
|
||||||
canvas.FillRectangle(dirtyRect);
|
canvas.FillRectangle(dirtyRect);
|
||||||
|
|
||||||
|
DrawBackground(canvas);
|
||||||
|
|
||||||
PointF ToScreen(double worldX, double worldY) =>
|
PointF ToScreen(double worldX, double worldY) =>
|
||||||
new((float)(worldX * Scale + OffsetX), (float)(worldY * Scale + OffsetY));
|
new((float)(worldX * Scale + OffsetX), (float)(worldY * Scale + OffsetY));
|
||||||
|
|
||||||
@@ -85,6 +94,39 @@ public sealed class MapDrawable : IDrawable
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Draws the galaxy map screenshot fitted inside the world square,
|
||||||
|
/// preserving aspect ratio so node world coordinates line up exactly
|
||||||
|
/// with the map underneath.
|
||||||
|
/// </summary>
|
||||||
|
private void DrawBackground(ICanvas canvas)
|
||||||
|
{
|
||||||
|
if (!ShowBackground || BackgroundImage is null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var imageWidth = BackgroundImage.Width;
|
||||||
|
var imageHeight = BackgroundImage.Height;
|
||||||
|
if (imageWidth <= 0 || imageHeight <= 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const double world = StarSystemDocStore.WorldSize;
|
||||||
|
var fit = Math.Min(world / imageWidth, world / imageHeight);
|
||||||
|
var drawWidth = imageWidth * fit;
|
||||||
|
var drawHeight = imageHeight * fit;
|
||||||
|
var originX = (world - drawWidth) / 2;
|
||||||
|
var originY = (world - drawHeight) / 2;
|
||||||
|
|
||||||
|
canvas.DrawImage(BackgroundImage,
|
||||||
|
(float)(originX * Scale + OffsetX),
|
||||||
|
(float)(originY * Scale + OffsetY),
|
||||||
|
(float)(drawWidth * Scale),
|
||||||
|
(float)(drawHeight * Scale));
|
||||||
|
}
|
||||||
|
|
||||||
public static Color ColorForOwner(string owner) => owner switch
|
public static Color ColorForOwner(string owner) => owner switch
|
||||||
{
|
{
|
||||||
"The Eden Accord" => Color.FromArgb("#10b981"),
|
"The Eden Accord" => Color.FromArgb("#10b981"),
|
||||||
|
|||||||
Reference in New Issue
Block a user