BITFALL/Assets/BITKit/Unity/Scripts/Entity/Components/InputSystem/EntityInputSystem.cs

81 lines
1.7 KiB
C#

using System.Collections;
using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.UIElements;
#endif
using UnityEngine;
using UnityEngine.UIElements;
namespace BITKit.Entities.InputSystem
{
public class EntityInputSystem : EntityBehavior
{
protected readonly InputActionGroup inputActionGroup = new()
{
allowGlobalActivation = true
};
[Inject(true)]
private IHealth _health;
[Inject(true)]
private IEntityOverride _override;
[SerializeField,HideInInspector] internal bool Allow;
public override void Initialize(IEntity _entity)
{
base.Initialize(_entity);
UnityEntity.AddService(inputActionGroup);
}
public override void OnAwake()
{
base.OnAwake();
inputActionGroup.allowInput.AddListener(x=>Allow=x);
if (_health is not null)
{
_health.OnSetAlive += x =>
{
inputActionGroup.allowInput.SetElements(_health,x);
};
}
if (_override is not null)
{
_override.OnOverride += x =>
{
inputActionGroup.allowInput.SetDisableElements(_override,x);
};
}
}
public override void OnStart()
{
base.OnStart();
inputActionGroup.allowInput.Invoke();
}
public override void OnDestroyComponent()
{
base.OnDestroyComponent();
inputActionGroup.Dispose();
}
}
#if UNITY_EDITOR
[CustomEditor(typeof(EntityInputSystem))]
public sealed class EntityInputSystemInspector : BITInspector<EntityInputSystem>
{
public override VisualElement CreateInspectorGUI()
{
FillDefaultInspector();
var checkBox = root.Create<Toggle>();
checkBox.label = "Allow Input";
checkBox.SetEnabled(false);
checkBox.BindProperty(serializedObject.FindProperty(nameof(EntityInputSystem.Allow)));
return root;
}
}
#endif
}