using System.Numerics;
using GamePort.Assets;
using GamePort.Game;
namespace GamePort.Application;
/// Source stationary crosshair strip and prior-capture pitch label.
/// Original TIME set5 glyphs preserve their green pixels and null-space advance. UiViewport supplies the port's uniform centered widescreen policy.
public sealed class BaseballAimOverlay
{
private static readonly Dictionary Expected = new()
{
[253]=(15,210,"2e16e11b151c8474ba68fd5017d1a3a6304a594a7f34c3b423f3417c7edfb453"),
[118]=(16,16,"edc60e65dcf7320813a80d4312e3c45951ccfc566ea79dfabf7a22ebc4b96049"),
[127]=(16,16,"1fd058ad5bdc0f8a064ef7d2d52b2b036df3f35515691a366799d7e8aab5297f"),
[120]=(16,16,"62aaeaaf107300a6f4f7ff52e98106c1520a9edbe2137626ee41091cf243e5c5"),
[121]=(16,16,"d78bf1895f5838b9eb8c6a22565a9588dae4fc8f7106697df373f4408aae634c"),
[122]=(16,16,"f6d628639de4e40005f62f63a49308ff9ee0656a36bb6eb7787230b4830a5ed9"),
[123]=(16,16,"9f58cad5621d89b7a7012036daa8532b2397b0d9d267ab7d1aa5a7cd10d7ae87"),
[124]=(16,16,"49cdc9ec812f4ef3826011d542c30afbb10229c4b4ce549e7820233bbaa53dd9"),
[125]=(16,16,"aaef2d59c2636c059b446bed643985ca55d15b2d070b0582b2d8994b3bf74f1e"),
[126]=(16,16,"bade574a4624a3f763ddd9c87fa2f102562b00d8d604e9faf32a43b372de64e0"),
[119]=(16,16,"0a663ddf2df9f1065f5e4ffe27b4d1fa96edd5f19b6323217bad4148f8d2f3d7")
};
private static readonly int[] DigitIds = [118,127,120,121,122,123,124,125,126,119];
private readonly int[] offsets = new int[21];
private readonly uint[] firstIndices = new uint[21];
public Mesh Mesh { get; }
public BaseballAimOverlay(Mesh combined, string preparedRoot)
{
var images = BaseballResourcePixels.Load(preparedRoot,"aimTextures",Expected);
var builder = new BaseballQuadBuilder(combined);
(offsets[0],firstIndices[0]) = builder.AddQuad(builder.AddTexture(images[253]),true);
//TextureId is immutable. Two banks of ten digit quads select exact glyphs
//without modifying shared draws or allocating per-frame geometry.
for(var digit=0;digit<10;digit++)
{
var texture=builder.AddTexture(images[DigitIds[digit]]);
for(var place=0;place<2;place++)
{
var index=1+place*10+digit;
(offsets[index],firstIndices[index])=builder.AddQuad(texture,true);
}
}
Mesh=builder.Build();
}
public void Update(Mesh destination, BaseballGadget device, uint width, uint height)
{
if(destination.Vertices.Length45)throw new ArgumentOutOfRangeException(nameof(device),"Source pitch label must remain0..45");
var number=(int)pitch;
var y=(int)((45-(double)pitch)*4.3+100);
Quad(0,new(315,100),new(15,210),device.AimVisible);
for(var digit=0;digit<10;digit++)
{
//TIME set5 has a null space pointer; source468939 skips both
//drawing and advance for the positive sign blank.
Quad(1+digit,new(286,y),new(16,16),device.AimVisible&&digit==number/10);
Quad(11+digit,new(302,y),new(16,16),device.AimVisible&&digit==number%10);
}
void Quad(int index,Vector2 position,Vector2 size,bool visible)
{
var draw=destination.Draws.First(d=>d.ScreenSpace&&d.FirstIndex==firstIndices[index]&&d.IndexCount==6);
draw.Visible=visible;
if(!visible)return;
var p=position+viewport.CenterOffset;
var corners=new[]{p,p+new Vector2(size.X,0),p+size,p+new Vector2(0,size.Y)};
for(var corner=0;corner<4;corner++)
destination.Vertices[offsets[index]+corner].Position=viewport.ToNdc(corners[corner]);
}
}
}