using UnityEngine;
using UnityEngine.UI;
using UnityEngine.InputSystem;  // Input System を使うために必要な名前空間

public class InputSystemSample1 : MonoBehaviour
{
    [SerializeField] Text _console1;
    [SerializeField] Text _console2;
    [SerializeField] Image _joystickBase;
    [SerializeField] Image _joystickHandle;

    void Start()
    {
        _console1.text = "";
        _console2.text = "";
    }
    
    void Update()
    {
        string console2Text = "";

        // Input System で、Input Manager と同じようにキー入力を検出する

        if (Keyboard.current.qKey.wasPressedThisFrame)
        {
            AddLogToConsole1("<color=red>Q キーが押された</color>");
        }   // Qキーが押されたことを検出する

        if (Keyboard.current.qKey.wasReleasedThisFrame)
        {
            AddLogToConsole1("Q キーが離された");
        }   // Qキーが離されたことを検出する

        if (Keyboard.current.qKey.isPressed)
        {
            console2Text = "<color=red>Q キーが押されている</color>\n";
        }
        else
        {
            console2Text = "Q キーが押されていない\n";
        }   // Qキーが押されているかどうかを判定する

        _console2.text = console2Text;

        // このやり方では、Input Manager のように Axis は取得できないので、自分で処理する
        // この場合は、矢印キー/WASD キー/ゲームパッド用のコードを個別に自分で書かなければならない

        Vector2 input = Vector2.zero;

        if (Keyboard.current.leftArrowKey.isPressed)
        {
            input.x -= 1;
        }

        if (Keyboard.current.rightArrowKey.isPressed)
        {
            input.x += 1;
        }

        if (Keyboard.current.upArrowKey.isPressed)
        {
            input.y += 1;
        }

        if (Keyboard.current.downArrowKey.isPressed)
        {
            input.y -= 1;
        }

        _joystickHandle.rectTransform.anchoredPosition = input * (_joystickBase.rectTransform.sizeDelta.x / 2);
    }

    void AddLogToConsole1(string log)
    {
        _console1.text = log + "\n" + _console1.text;
    }
}
