sasalka/Code/UI/InteractionButton.cs
2025-06-10 23:24:50 +03:00

92 lines
2.0 KiB
C#

using Sandbox.Diagnostics;
namespace Sandbox.UI;
public abstract class InteractionButton : Component, Component.IPressable
{
[Property] public virtual string Label { get; set; } = "Label";
[Property] public bool RequiresHold { get; set; } = false;
private HoverInfoPanel _uiPanel;
private GameObject _interactionPanelPrefab { get; set; }
protected override void OnStart()
{
base.OnStart();
_interactionPanelPrefab = GameObject.GetPrefab( "prefabs/InteractionPanel.prefab" );
Assert.True( _interactionPanelPrefab.IsValid(), $"No InteractionPanel prefab found for {GameObject.Name}!" );
}
public virtual void Hover( IPressable.Event e )
{
CreatePanel();
}
public virtual void Blur( IPressable.Event e )
{
DestroyPanel();
}
public virtual void Look( IPressable.Event e )
{
if ( _uiPanel != null )
{
_uiPanel.ProgressionHold = 0f;
}
}
public virtual bool CanPress( IPressable.Event e )
{
return true;
}
public virtual bool Press( IPressable.Event e )
{
// // Проверяем, что событие относится именно к этому объекту
// if ( e.Source.GameObject != this.GameObject )
// return false; // Игнорируем, событие для другого объекта
if ( _uiPanel != null )
{
_uiPanel.SetPressed( true );
}
return false;
}
public virtual void Release( IPressable.Event e )
{
if ( _uiPanel != null )
{
_uiPanel?.SetPressed( false );
}
}
protected void CreatePanel()
{
if ( _uiPanel != null ) return;
var panelGo = _interactionPanelPrefab.Clone();
panelGo.WorldPosition = WorldPosition;
panelGo.Parent = Scene;
_uiPanel = panelGo.Components.Get<HoverInfoPanel>();
if ( _uiPanel != null )
{
_uiPanel.InteractionString = Label;
_uiPanel.IsHoldInteraction = RequiresHold;
_uiPanel.ProgressionHold = 0f;
}
}
protected void DestroyPanel()
{
if ( _uiPanel == null ) return;
_uiPanel.GameObject.Destroy();
_uiPanel = null;
// await Task.DelaySeconds(0.05f);
}
}