Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.PSVita;
- public class VitaInputManager : MonoBehaviour {
- #region SingletonSetup
- private static VitaInputManager instance;
- public static VitaInputManager Instance
- {
- get
- {
- if (!instance)
- {
- instance = GameObject.FindObjectOfType(typeof(VitaInputManager)) as VitaInputManager;
- if (!instance)
- {
- VitaDebug.Log("No active VitaInputManager script on any GameObject");
- }
- }
- return instance;
- }
- }
- #endregion
- #region Delegates
- public delegate void FaceButtonEvent();
- public delegate void DpadButtonEvent();
- public delegate void TriggerButtonEvent();
- public delegate void StartSelectEvent();
- public delegate void LeftStickEvent(float horizontal, float vertical);
- public delegate void RightStickEvent(float horizontal, float vertical);
- public delegate void RearTouchPadEvent(float [,] touchData, bool tl1,
- bool tl2,
- bool tr1,
- bool tr2);
- #endregion
- //events
- #region FaceButtonEvents
- //cross events
- public event FaceButtonEvent OnCross;
- public event FaceButtonEvent OnCrossDown;
- public event FaceButtonEvent OnCrossUp;
- //square events
- public event FaceButtonEvent OnSquare;
- public event FaceButtonEvent OnSquareDown;
- public event FaceButtonEvent OnSquareUp;
- //triangle events
- public event FaceButtonEvent OnTriangle;
- public event FaceButtonEvent OnTriangleDown;
- public event FaceButtonEvent OnTriangleUp;
- //circle events
- public event FaceButtonEvent OnCircle;
- public event FaceButtonEvent OnCircleDown;
- public event FaceButtonEvent OnCircleUp;
- #endregion
- #region DPadEvents
- public event DpadButtonEvent OnDpadUp;
- public event DpadButtonEvent OnDpadDown;
- public event DpadButtonEvent OnDpadLeft;
- public event DpadButtonEvent OnDpadRight;
- #endregion
- #region TriggerEvents
- //L Trigger events
- public event TriggerButtonEvent OnLTrig;
- public event TriggerButtonEvent OnLTrigDown;
- public event TriggerButtonEvent OnLTrigUp;
- //R Trigger events
- public event TriggerButtonEvent OnRTrig;
- public event TriggerButtonEvent OnRTrigDown;
- public event TriggerButtonEvent OnRTrigUp;
- #endregion
- #region StartSelectEvents
- public event StartSelectEvent OnStart;
- public event StartSelectEvent OnSelect;
- #endregion
- #region JoystickEvents
- public event LeftStickEvent OnLeftStick;
- public event RightStickEvent OnRightStick;
- #endregion
- #region RearTouchpadEvents
- public event RearTouchPadEvent GetSecondaryTouch;
- #endregion
- private int previousBackTouchCount = 0; // Tracks the previous frame's touch count
- private int currentBackTouchCount = 0; // Current frame's touch count
- //bool flags for back pad triggers
- private bool TR1;
- private bool TR2;
- private bool TL1;
- private bool TL2;
- // Here, we are just checking conditions and invoking events to pass state along
- void Update () {
- #region Cross
- if (Input.GetButton("Cross"))
- {
- if (OnCross != null)
- {
- OnCross();
- }
- }
- if (Input.GetButtonDown("Cross"))
- {
- if (OnCrossDown != null)
- {
- OnCrossDown();
- }
- }
- if (Input.GetButtonUp("Cross"))
- {
- if (OnCrossUp != null)
- {
- OnCrossUp();
- }
- }
- #endregion
- #region Square
- if (Input.GetButton("Square"))
- {
- if (OnSquare != null)
- {
- OnSquare();
- }
- }
- if (Input.GetButtonDown("Square"))
- {
- if (OnSquareDown != null)
- {
- OnSquareDown();
- }
- }
- if (Input.GetButtonUp("Square"))
- {
- if (OnCrossUp != null)
- {
- OnCrossUp();
- }
- }
- #endregion
- #region Triangle
- if (Input.GetButton("Triangle"))
- {
- if (OnTriangle != null)
- {
- OnTriangle();
- }
- }
- if (Input.GetButtonDown("Triangle"))
- {
- if (OnTriangleDown != null)
- {
- OnTriangleDown();
- }
- }
- if (Input.GetButtonUp("Triangle"))
- {
- if (OnTriangleUp != null)
- {
- OnTriangleUp();
- }
- }
- #endregion
- #region Circle
- if (Input.GetButton("Circle"))
- {
- if (OnCircle != null)
- {
- OnCircle();
- }
- }
- if (Input.GetButtonDown("Circle"))
- {
- if (OnCircleDown != null)
- {
- OnCircleDown();
- }
- }
- if (Input.GetButtonUp("Circle"))
- {
- if (OnCircleUp != null)
- {
- OnCircleUp();
- }
- }
- #endregion
- #region Dpad
- //add code here
- if (Input.GetButtonDown("Up"))
- {
- if (OnDpadUp != null)
- {
- OnDpadUp();
- }
- }
- if (Input.GetButtonDown("Down"))
- {
- if (OnDpadDown != null)
- {
- OnDpadDown();
- }
- }
- if (Input.GetButtonDown("Left"))
- {
- if (OnDpadLeft != null)
- {
- OnDpadLeft();
- }
- }
- if (Input.GetButtonDown("Right"))
- {
- if (OnDpadRight != null)
- {
- OnDpadRight();
- }
- }
- #endregion
- #region Triggers
- //L Trigger
- if (Input.GetButton("LTRIG"))
- {
- if (OnLTrig != null)
- {
- OnLTrig();
- }
- }
- if (Input.GetButtonDown("LTRIG"))
- {
- if (OnLTrigDown != null)
- {
- OnLTrigDown();
- }
- }
- if (Input.GetButtonUp("LTRIG"))
- {
- if (OnLTrigUp != null)
- {
- OnLTrigUp();
- }
- }
- //R Trigger
- if (Input.GetButton("RTRIG"))
- {
- if (OnRTrig != null)
- {
- OnRTrig();
- }
- }
- if (Input.GetButtonDown("RTRIG"))
- {
- if (OnRTrigDown != null)
- {
- OnRTrigDown();
- }
- }
- if (Input.GetButtonUp("RTRIG"))
- {
- if (OnRTrigUp != null)
- {
- OnRTrigUp();
- }
- }
- #endregion
- #region StartSelect
- if (Input.GetButtonDown("Start"))
- {
- if (OnStart != null)
- {
- OnStart();
- }
- }
- if (Input.GetButtonDown("Select"))
- {
- if (OnSelect != null)
- {
- OnSelect();
- }
- }
- #endregion
- #region Joysticks
- if (OnLeftStick != null)
- {
- OnLeftStick(Input.GetAxis("Left Stick Horizontal"),
- Input.GetAxis("Left Stick Vertical"));
- }
- if (OnRightStick != null)
- {
- OnRightStick(Input.GetAxis("Right Stick Horizontal"),
- Input.GetAxis("Right Stick Vertical"));
- }
- #endregion
- #region RearTouches
- if (Application.platform == RuntimePlatform.PSP2) // PSV/PSTV only
- {
- currentBackTouchCount = PSVitaInput.touchesSecondary.Length;
- // Trigger only if touch count increases or decreases, but not when it's zero or unchanged
- if (currentBackTouchCount != previousBackTouchCount)
- {
- float[,] dataMatrix = new float[3, 4]; // 3 rows: Finger ID, X, Y
- bool foundTL1 = false, foundTL2 = false, foundTR1 = false, foundTR2 = false; // Track active touch presence
- int TL1ID = 255, TL2ID = 255, TR1ID = 255, TR2ID = 255; //initialize with OOB values
- for (int t = 0; t < currentBackTouchCount && t < 4; t++) // Limit to 4 touches
- {
- Touch currentTouch = PSVitaInput.GetSecondaryTouch(t);
- // Store touch data in the matrix
- dataMatrix[0, t] = currentTouch.fingerId; // Row 0: Touch ID
- dataMatrix[1, t] = currentTouch.position.x; // Row 1: X positions
- dataMatrix[2, t] = currentTouch.position.y; // Row 2: Y positions
- float x = currentTouch.position.x;
- float y = currentTouch.position.y;
- // Check touch positions and update tracking flags
- if (x >= 0 && x <= 240 && y >= 136 && y <= 272) // TL1, upper left
- {
- foundTL1 = true;
- TL1ID = currentTouch.fingerId;
- }
- if (x >= 0 && x <= 240 && y >= 0 && y <= 136) // TL2, lower left
- {
- foundTL2 = true;
- TL2ID = currentTouch.fingerId;
- }
- if (x >= 240 && x <= 480 && y >= 136 && y <= 272) // TR1, upper right
- {
- foundTR1 = true;
- TR1ID = currentTouch.fingerId;
- }
- if (x >= 240 && x <= 480 && y >= 0 && y <= 136) // TR2, lower right
- {
- foundTR2 = true;
- TR2ID = currentTouch.fingerId;
- }
- }
- // Update boolean states only if there is a change, reset ID to OOB value
- if (!foundTL1)
- {
- TL1 = false;
- TL1ID = 255;
- }
- if (!foundTL2)
- {
- TL2 = false;
- TL2ID = 255;
- }
- if (!foundTR1)
- {
- TR1 = false;
- TR1ID = 255;
- }
- if (!foundTR2)
- {
- TR2 = false;
- TR2ID = 255;
- }
- // If at least one touch is in the region, set it to true
- if (foundTL1) TL1 = true;
- if (foundTL2) TL2 = true;
- if (foundTR1) TR1 = true;
- if (foundTR2) TR2 = true;
- // Invoke only when needed
- if (GetSecondaryTouch != null)
- {
- GetSecondaryTouch(dataMatrix, TL1,
- TL2,
- TR1,
- TR2); // Push data to subscribers
- }
- }
- // Update previous touch count for the next frame
- previousBackTouchCount = currentBackTouchCount;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement