using jeanf.EventSystem; using jeanf.validationTools; using UnityEngine; using UnityEngine.UIElements; namespace jeanf.universalplayer { /// /// The player's screen-space HUD, as ONE UI Toolkit document (ScreenspaceUI.uxml) /// instead of a pile of screen-space canvases: the cursor/reticle, the loading status /// line and the loading bar. /// /// It is deliberately NOT interactable (pickingMode = Ignore on everything): it is /// pure HUD, so it can never eat a click meant for the world or a world-space canvas. /// /// Loading arrives on SO channels, so this package needs no reference to the scene /// loading package — SceneManagement's LoadingInformation re-broadcasts its status and /// its (real, Addressables-measured) progress onto them. /// /// The cursor element is exposed rather than driven here: CursorStateController stays /// the single authority for cursor state/colour and pushes into it. /// [RequireComponent(typeof(UIDocument))] public class ScreenspaceHud : MonoBehaviour, IDebugBehaviour { private const string LogPrefix = "[UniversalPlayer]"; [Header("Debug")] [Tooltip("Logs the panel/element layout once it resolves, plus every loading event received. " + "Turn on when the HUD renders in the wrong place or not at all — it tells you whether the " + "elements have a real size and whether the channels are actually firing.")] [SerializeField] private bool _isDebug = false; public bool isDebug { get => _isDebug; set => _isDebug = value; } private bool _loggedLayout; // Element names in ScreenspaceUI.uxml. private const string CursorName = "Cursor"; private const string LoadingName = "Loading"; private const string InformationName = "Information"; private const string ProgressName = "Progress"; [Header("Listening on:")] [Tooltip("Loading status text (SceneManagement's LoadingInformation broadcasts it). Empty string = not loading.")] [Validation("The loading status channel is required — without it the HUD never hears a scene load and the loading bar/status line silently never appear.")] [SerializeField] private StringEventChannelSO loadingStatusChannel; [Tooltip("Loading progress 0..1 (real progress, measured by the loaders).")] [Validation("The loading progress channel is required — without it the loading bar stays at 0% for the whole load (progress is never received).")] [SerializeField] private FloatEventChannelSO loadingProgressChannel; [Header("Loading bar")] [Tooltip("Bar background while loading. White by design — the progress fill uses the cursor's colour on top of it.")] [SerializeField] private Color loadingBackgroundColor = Color.white; [Tooltip("Loading status text colour.")] [SerializeField] private Color informationColor = Color.white; [Tooltip("Fallback progress-fill colour when no CursorStateController is present (it normally supplies the cursor's resting colour).")] [SerializeField] private Color progressFallbackColor = new Color(1f, 0.72f, 0f); private UIDocument _document; private VisualElement _cursor; private VisualElement _loading; private Label _information; private VisualElement _progress; private CursorStateController _cursorColors; private float _warnAt; private bool _warned; private string _lastStatus = string.Empty; private float _lastProgress; /// The cursor/reticle element — CursorStateController drives it. public VisualElement CursorElement => _cursor; public bool IsReady => _cursor != null; /// The live HUD (there is one per player). Null before it comes up. public static ScreenspaceHud Active { get; private set; } private void OnEnable() { Active = this; _document = GetComponent(); _warnAt = Time.unscaledTime + 2f; _warned = false; if (loadingStatusChannel != null) loadingStatusChannel.OnEventRaised += OnLoadingStatus; if (loadingProgressChannel != null) loadingProgressChannel.OnEventRaised += OnLoadingProgress; // May be too early: UIDocument builds its visual tree in ITS OnEnable, and the // order between components is not guaranteed. Update() retries until it is up. TryQueryElements(); } // Self-heals: the document's tree can appear a frame (or a scene load) after us. private void Update() { if (_cursor == null) TryQueryElements(); } private void OnDisable() { if (loadingStatusChannel != null) loadingStatusChannel.OnEventRaised -= OnLoadingStatus; if (loadingProgressChannel != null) loadingProgressChannel.OnEventRaised -= OnLoadingProgress; if (Active == this) Active = null; } private bool TryQueryElements() { var root = _document != null ? _document.rootVisualElement : null; if (root == null) { WarnIfLate("the UIDocument has no root — assign a Source Asset (ScreenspaceUI.uxml) and a PanelSettings."); return false; } // HUD only: never intercept pointer events meant for the world/world-space UI. root.pickingMode = PickingMode.Ignore; foreach (var child in root.Query().Build()) child.pickingMode = PickingMode.Ignore; // Stretch the document root over the panel, explicitly. // // Do NOT rely on the UXML's `height: 100%`: a percentage only resolves against // a parent with a DEFINITE size, and UIDocument's root is laid out as a column // whose cross axis (width) stretches while its main axis (height) is content- // driven. So width resolved and height did not — #Root collapsed to its content // (the 20px cursor) and flex-grow could not save it, because flex-grow needs a // definite main size to grow into. // // Everything anchored inside then landed wrong in the vertical axis ONLY, which // is the signature this fixes: the cursor's `top: 50%` collapsed toward 0 (it // sat centred horizontally but pinned to the top) and the loading bar's // `bottom: 0` anchored to the collapsed root instead of the bottom of the // screen, which is why the bar and its status line were nowhere to be seen. // Absolute + all four insets = definite height, independent of the layout the // document happens to hand us. root.style.position = Position.Absolute; root.style.left = 0f; root.style.top = 0f; root.style.right = 0f; root.style.bottom = 0f; _cursor = root.Q(CursorName); _loading = root.Q(LoadingName); _information = root.Q