using Godot; using RailEnterprise.Client.App; using RailEnterprise.Client.Map; using RailEnterprise.Core.Map; using RailEnterprise.Protocol.Client; namespace RailEnterprise.Client.Hud; /// Minimappa: terreno ridotto, linee, città e rettangolo della vista; un clic centra la mappa. public partial class Minimap : Control { private ClientWorld? _world; private MapView? _map; private ImageTexture? _texture; private int _terrainVersion = -1; public void Bind(ClientWorld world, MapView map) { _world = world; _map = map; _terrainVersion = -1; CustomMinimumSize = new Vector2(0, 220); MouseFilter = MouseFilterEnum.Stop; } public override void _Process(double delta) { if (_world?.Terrain is { } t && _world.TerrainVersion != _terrainVersion && Engine.GetProcessFrames() % 30 == 0) { _terrainVersion = _world.TerrainVersion; int step = Math.Max(1, t.Width / 256); int w = t.Width / step, h = t.Height / step; var data = new byte[w * h * 4]; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { var type = (CellType)t.Types[y * step * t.Width + x * step]; var c = type switch { CellType.Sea => Palette.Sea, CellType.Water => Palette.Water, CellType.Forest => Palette.Undergrowth, CellType.Town => Palette.TileRoof.Lightened(0.3f), CellType.Mountain or CellType.Rock => Palette.Highland, CellType.Hill => Palette.Hill, _ => Palette.Plain, }; int i = (y * w + x) * 4; data[i] = (byte)(c.R * 255); data[i + 1] = (byte)(c.G * 255); data[i + 2] = (byte)(c.B * 255); data[i + 3] = 255; } } _texture = ImageTexture.CreateFromImage(Image.CreateFromData(w, h, false, Image.Format.Rgba8, data)); } QueueRedraw(); } private Rect2 MapRect() { var t = _world!.Terrain!; float aspect = (float)t.Width / t.Height; float w = Size.X, h = Size.X / aspect; if (h > Size.Y) { h = Size.Y; w = h * aspect; } return new Rect2((Size.X - w) / 2, (Size.Y - h) / 2, w, h); } public override void _Draw() { DrawStyleBox(UiTheme.Box(Palette.Inner, Palette.Border, 8, 0), new Rect2(Vector2.Zero, Size)); if (_world?.Terrain is not { } t || _texture is null || _map is null) return; var r = MapRect(); DrawTextureRect(_texture, r, false); float k = r.Size.X / (float)(t.Width * t.CellSizeM); Vector2 P(Vector2 world) => r.Position + world * k; foreach (var e in _world.Edges.Values) { if (e.S.Length < 2) continue; DrawLine(P(MapScale.V(e.X[0], e.Y[0])), P(MapScale.V(e.X[^1], e.Y[^1])), new Color("2a2f2c"), 2); } foreach (var town in _world.Towns.Values) DrawCircle(P(MapScale.V(town.Position)), 3.5f, new Color("8a6a55")); var view = _map.VisibleRect(); DrawRect(new Rect2(P(view.Position), view.Size * k), Palette.Amber, false, 2); } public override void _GuiInput(InputEvent e) { if (_world?.Terrain is not { } t || _map is null) return; if (e is InputEventMouseButton { ButtonIndex: MouseButton.Left, Pressed: true } b || e is InputEventMouseMotion { ButtonMask: MouseButtonMask.Left }) { var pos = e is InputEventMouse m ? m.Position : Vector2.Zero; var r = MapRect(); float k = r.Size.X / (float)(t.Width * t.CellSizeM); _map.CenterOn((pos - r.Position) / k); AcceptEvent(); } } }