88 lines
1.7 KiB
C#
88 lines
1.7 KiB
C#
using Sandbox.Diagnostics;
|
|
|
|
namespace Sandbox.UI;
|
|
|
|
public abstract class InteractionButton : Component, Component.IPressable
|
|
{
|
|
[Property] public 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");
|
|
Log.Info(_interactionPanelPrefab.Name);
|
|
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 (_uiPanel != null)
|
|
{
|
|
_uiPanel?.SetPressed(true);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
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 async void DestroyPanel()
|
|
{
|
|
if (_uiPanel == null) return;
|
|
|
|
_uiPanel.GameObject.Destroy();
|
|
_uiPanel = null;
|
|
|
|
await Task.DelaySeconds(0.05f);
|
|
}
|
|
}
|