using System.Collections;
using System.Collections.Generic;
using System.IO;
using Luneraria.Core;
using Luneraria.View;
using NUnit.Framework;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.TestTools;
namespace Luneraria.UI.Tests
{
///
/// U3B: the HUD on a real match in the G1 scene, rendered over the camera image at 1920×1080
/// (tools/out/hud-*.png). First look at layout, fonts, plates and portraits against the G3
/// screenshots.
///
public sealed class HudCaptureTests
{
public static string Root { get { return Path.GetDirectoryName(Application.dataPath); } }
public static LuneUiAssets Assets()
{
#if UNITY_EDITOR
return UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/Luneraria/Settings/LuneUi.asset");
#else
return null;
#endif
}
/// The G1 scene with its match running at once, without the start screen.
public static IEnumerator LoadScene()
{
LuneMatchDriver.DirectStart = true;
AsyncOperation loading = SceneManager.LoadSceneAsync("G1_Map", LoadSceneMode.Single);
while (!loading.isDone)
yield return null;
yield return null;
}
/// Composes the camera image and the HUD texture and writes a PNG.
public static void Capture(LuneHud hud, RenderTexture target, string file)
{
LunePortraits.Shared?.Flush();
Texture2D scene = LuneHudCapture.RenderScene(Camera.main, target.width, target.height);
Texture2D overlay = LuneHudCapture.Read(target);
Texture2D image = LuneHudCapture.Compose(scene, overlay);
LuneHudCapture.Save(image, file);
Object.Destroy(scene);
Object.Destroy(overlay);
Object.Destroy(image);
}
[UnityTest]
[Timeout(600000)]
public IEnumerator PlanningHudOverTheScene()
{
LunePreferences.ResetSession();
yield return LoadScene();
var driver = Object.FindAnyObjectByType();
var view = Object.FindAnyObjectByType();
var rig = Object.FindAnyObjectByType();
driver.HumanSlot = 1;
driver.Seed = 4;
driver.Restart();
LuneMatch match = driver.Runner.Match;
Player me = match.Players[1];
List ids = match.BaseIds(me.Bund);
foreach (int index in new[] { 1, 0, 2 })
Assert.IsTrue(match.Submit(1, Command.Buy(ids[index])).Ok);
var target = new RenderTexture(1920, 1080, 0, RenderTextureFormat.ARGB32);
var hud = new GameObject("Hud").AddComponent();
hud.Init(match, 1, view.Library, Assets(), target);
Vec2 point = match.Map.LanePoint(me.Team, me.Lane, match.Map.LanePaths[me.Lane].Length * .2);
rig.LookAt(point, LuneCameraMath.DefaultZoom, true);
for (int frame = 0; frame < 30; frame++)
{
if (frame == 10)
LunePortraits.Shared.Flush();
yield return null;
}
Capture(hud, target, Path.Combine(Root, "tools/out/hud-planung-1920.png"));
Debug.Log("LUNE_HUD syncs " + hud.Syncs + " sync ms " + hud.SyncMs.ToString("0.000") + " portraits " + LunePortraits.Shared.Rendered + " scale " + hud.UiScale + " size " + hud.LogicalSize);
foreach (KeyValuePair region in hud.Regions())
Debug.Log("LUNE_HUD region " + region.Key + " " + region.Value);
Assert.Greater(hud.Syncs, 0);
Object.Destroy(hud.gameObject);
target.Release();
}
}
}