MediaInteract.cs

The media player's interact component. Uses IPressable so the USE key opens MediaRemote, or presses an on-screen button on MediaScreen/MediaSpeaker when the player aims at one, and builds the press tooltip with the media title and an auto-hide timer.

namespace Bimp;

/// <summary>
/// Lets players use the media player with the USE key (works with the engine's <see cref="PlayerController"/>,
/// or anything else that calls <see cref="Component.IPressable"/>). Needs a collider on this GameObject or a child.
/// <para>
/// Aiming at one of the screen's on-screen buttons while the controls are showing presses that button.
/// Anywhere else (or while the controls are hidden) opens the full remote.
/// </para>
/// </summary>
[Title( "Media Player Interact" ), Category( "Media" ), Icon( "touch_app" )]
public sealed class MediaInteract : Component, Component.IPressable
{
	/// <summary>
	/// The player to control. Found automatically in this object or its parents if unset.
	/// </summary>
	[Property] public MediaPlayer Player { get; set; }

	/// <summary>
	/// The screen whose on-screen buttons USE can press. Found automatically in this object or its children if unset.
	/// </summary>
	[Property] public MediaScreen Screen { get; set; }

	/// <summary>
	/// The speaker whose on-screen buttons USE can press. Found automatically in this object or its children if unset.
	/// </summary>
	[Property] public MediaSpeaker Speaker { get; set; }

	/// <summary>
	/// Hide the title and "Open controls" hint this many seconds after you start aiming at this when no on-screen
	/// controls decide it (a speaker, or a screen with nothing playing). They come back when you look away and back.
	/// 0 = never hide.
	/// </summary>
	[Property, Range( 0, 30 )] public float HideTooltipAfter { get; set; } = 3.0f;

	TimeSince sinceTooltip = 1000;
	TimeSince sinceAimStart;

	MediaPlayer Target => Player.IsValid() ? Player : (Player = GetComponentInParent<MediaPlayer>());
	MediaScreen TargetScreen => Screen.IsValid() ? Screen : (Screen = GetComponentInChildren<MediaScreen>());
	MediaSpeaker TargetSpeaker => Speaker.IsValid() ? Speaker : (Speaker = GetComponentInChildren<MediaSpeaker>());

	public bool CanPress( IPressable.Event e ) => Target.IsValid();

	public bool Press( IPressable.Event e )
	{
		if ( !Target.IsValid() ) return false;

		// The screen or speaker may already have handled this USE press itself this frame
		if ( !UseHandled.TryConsume() ) return true;

		// Press runs on the client that pressed, so this only affects them (button presses go through the
		// player's permission-checked host requests)
		var screen = TargetScreen;
		if ( screen.IsValid() && screen.TryPressAimedControl() )
			return true;

		var speaker = TargetSpeaker;
		if ( speaker.IsValid() && speaker.TryPressAimedControl() )
			return true;

		MediaRemote.Toggle( Target );
		return true;
	}

	public IPressable.Tooltip? GetTooltip( IPressable.Event e )
	{
		if ( !Target.IsValid() ) return null;

		var screen = TargetScreen;
		var speaker = TargetSpeaker;
		var title = Target.HasMedia ? Target.Title : "Media Player";

		var action = (screen.IsValid() ? screen.AimedActionName : null)
			?? (speaker.IsValid() ? speaker.AimedActionName : null);
		if ( action is not null )
		{
			if ( screen.IsValid() && Target.HasMedia && !screen.ControlsVisible ) return null;
			return new IPressable.Tooltip( action, "touch_app", title );
		}

		// The title and "Open controls" hint go away like the on-screen controls do: with them while a screen plays
		// media, otherwise (a speaker, or nothing playing) after HideTooltipAfter seconds of aiming. They return when
		// you look away and back.
		if ( screen.IsValid() && Target.HasMedia )
		{
			if ( !screen.ControlsVisible ) return null;
		}
		else
		{
			if ( sinceTooltip > 0.3f ) sinceAimStart = 0;
			sinceTooltip = 0;
			if ( HideTooltipAfter > 0 && sinceAimStart > HideTooltipAfter ) return null;
		}

		var icon = Target.AudioOnlyPlayer ? "speaker" : "smart_display";
		return new IPressable.Tooltip( title, icon, "Open controls" );
	}
}