using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;
using UnityEngine.InputSystem.Interactions;

public class InputSystemSample2 : MonoBehaviour
{
    [SerializeField] Image _joystickBase;
    [SerializeField] Image _joystickHandle;
    [SerializeField] Image _joystickBase2;
    [SerializeField] Image _joystickHandle2;
    [SerializeField] Text _console;

    void Start()
    {
        if (_console)
            _console.text = "";
    }

    public void Move(InputAction.CallbackContext context)
    {
        Vector2 direction = context.ReadValue<Vector2>();
        _joystickHandle.rectTransform.anchoredPosition = direction * (_joystickBase.rectTransform.sizeDelta.x / 2);
    }   // "戻り値が void" で "引数が InputAction.CallbackContext" の "public メソッド" は、Unity Event として割り当てられる

    public void Look(InputAction.CallbackContext context)
    {
        Vector2 direction = context.ReadValue<Vector2>().normalized;
        _joystickHandle2.rectTransform.anchoredPosition = direction * (_joystickBase2.rectTransform.sizeDelta.x / 2);
    }

    public void Fire(InputAction.CallbackContext context)
    {
        if (context.performed)
        {
            AddLogToConsole1("<color=red>Fire performed</color>");

            switch (context.interaction)
            {
                case HoldInteraction:
                    AddLogToConsole1($"Fire interaction: HoldInteraction, duration={context.duration.ToString("0.000")}");
                    break;
                default:
                    break;
            }   // Hold を検出するには、Input Action の Interaction に HoldInteraction を設定する必要がある
        }

        if (context.started)
        {
            AddLogToConsole1("<color=green>Fire started</color>");
        }

        if (context.canceled)
        {
            AddLogToConsole1($"Fire canceled");
        }
    }

    void AddLogToConsole1(string log)
    {
        if (_console)
        {
            _console.text = log + "\n" + _console.text;
        }
        else
        {
            Debug.Log(log);
        }
    }
}
