Advertisement
noradninja

VitaInputManager

Mar 6th, 2025
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.46 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.PSVita;
  5.  
  6.  
  7. public class VitaInputManager : MonoBehaviour {
  8.  
  9.     #region SingletonSetup
  10.    
  11.         private static VitaInputManager instance;
  12.  
  13.         public static VitaInputManager Instance
  14.         {
  15.             get
  16.             {
  17.                 if (!instance)
  18.                 {
  19.                     instance = GameObject.FindObjectOfType(typeof(VitaInputManager)) as VitaInputManager;
  20.                     if (!instance)
  21.                     {
  22.                         VitaDebug.Log("No active VitaInputManager script on any GameObject");
  23.                     }
  24.                 }
  25.  
  26.                 return instance;
  27.             }
  28.         }
  29.    
  30.     #endregion
  31.  
  32.     #region Delegates
  33.  
  34.         public delegate void FaceButtonEvent();
  35.  
  36.         public delegate void DpadButtonEvent();
  37.  
  38.         public delegate void TriggerButtonEvent();
  39.  
  40.         public delegate void StartSelectEvent();
  41.  
  42.         public delegate void LeftStickEvent(float horizontal, float vertical);
  43.  
  44.         public delegate void RightStickEvent(float horizontal, float vertical);
  45.  
  46.         public delegate void RearTouchPadEvent(float [,] touchData, bool tl1,
  47.                                                                     bool tl2,
  48.                                                                     bool tr1,
  49.                                                                     bool tr2);
  50.     #endregion
  51.    
  52.     //events
  53.  
  54.     #region FaceButtonEvents
  55.    
  56.         //cross events
  57.         public event FaceButtonEvent OnCross;
  58.         public event FaceButtonEvent OnCrossDown;
  59.         public event FaceButtonEvent OnCrossUp;
  60.         //square events
  61.         public event FaceButtonEvent OnSquare;
  62.         public event FaceButtonEvent OnSquareDown;
  63.         public event FaceButtonEvent OnSquareUp;
  64.         //triangle events
  65.         public event FaceButtonEvent OnTriangle;
  66.         public event FaceButtonEvent OnTriangleDown;
  67.         public event FaceButtonEvent OnTriangleUp;
  68.         //circle events
  69.         public event FaceButtonEvent OnCircle;
  70.         public event FaceButtonEvent OnCircleDown;
  71.         public event FaceButtonEvent OnCircleUp;
  72.    
  73.     #endregion
  74.  
  75.     #region DPadEvents
  76.  
  77.         public event DpadButtonEvent OnDpadUp;
  78.         public event DpadButtonEvent OnDpadDown;
  79.         public event DpadButtonEvent OnDpadLeft;
  80.         public event DpadButtonEvent OnDpadRight;
  81.  
  82.     #endregion
  83.  
  84.     #region TriggerEvents
  85.  
  86.         //L Trigger events
  87.         public event TriggerButtonEvent OnLTrig;
  88.         public event TriggerButtonEvent OnLTrigDown;
  89.         public event TriggerButtonEvent OnLTrigUp;
  90.         //R Trigger events
  91.         public event TriggerButtonEvent OnRTrig;
  92.         public event TriggerButtonEvent OnRTrigDown;
  93.         public event TriggerButtonEvent OnRTrigUp;
  94.    
  95.     #endregion
  96.  
  97.     #region StartSelectEvents
  98.  
  99.         public event StartSelectEvent OnStart;
  100.         public event StartSelectEvent OnSelect;
  101.  
  102.     #endregion
  103.  
  104.     #region JoystickEvents
  105.  
  106.         public event LeftStickEvent OnLeftStick;
  107.         public event RightStickEvent OnRightStick;
  108.  
  109.     #endregion
  110.    
  111.     #region RearTouchpadEvents
  112.         public event RearTouchPadEvent GetSecondaryTouch;
  113.  
  114.     #endregion
  115.    
  116.     private int previousBackTouchCount = 0; // Tracks the previous frame's touch count
  117.     private int currentBackTouchCount = 0; // Current frame's touch count
  118.     //bool flags for back pad triggers
  119.     private bool TR1;
  120.     private bool TR2;
  121.     private bool TL1;
  122.     private bool TL2;
  123.    
  124.     // Here, we are just checking conditions and invoking events to pass state along
  125.     void Update () {
  126.        
  127.         #region Cross
  128.        
  129.             if (Input.GetButton("Cross"))
  130.             {
  131.                 if (OnCross != null)
  132.                 {
  133.                     OnCross();
  134.                 }
  135.             }
  136.             if (Input.GetButtonDown("Cross"))
  137.             {
  138.                 if (OnCrossDown != null)
  139.                 {
  140.                     OnCrossDown();
  141.                 }
  142.             }
  143.             if (Input.GetButtonUp("Cross"))
  144.             {
  145.                 if (OnCrossUp != null)
  146.                 {
  147.                     OnCrossUp();
  148.                 }
  149.             }
  150.            
  151.         #endregion
  152.        
  153.         #region Square
  154.            
  155.             if (Input.GetButton("Square"))
  156.             {
  157.                 if (OnSquare != null)
  158.                 {
  159.                     OnSquare();
  160.                 }
  161.             }
  162.             if (Input.GetButtonDown("Square"))
  163.             {
  164.                 if (OnSquareDown != null)
  165.                 {
  166.                     OnSquareDown();
  167.                 }
  168.             }
  169.             if (Input.GetButtonUp("Square"))
  170.             {
  171.                 if (OnCrossUp != null)
  172.                 {
  173.                     OnCrossUp();
  174.                 }
  175.             }
  176.            
  177.         #endregion
  178.        
  179.         #region Triangle
  180.            
  181.             if (Input.GetButton("Triangle"))
  182.             {
  183.                 if (OnTriangle != null)
  184.                 {
  185.                     OnTriangle();
  186.                 }
  187.             }
  188.             if (Input.GetButtonDown("Triangle"))
  189.             {
  190.                 if (OnTriangleDown != null)
  191.                 {
  192.                     OnTriangleDown();
  193.                 }
  194.             }
  195.             if (Input.GetButtonUp("Triangle"))
  196.             {
  197.                 if (OnTriangleUp != null)
  198.                 {
  199.                     OnTriangleUp();
  200.                 }
  201.             }
  202.            
  203.         #endregion
  204.        
  205.         #region Circle
  206.            
  207.             if (Input.GetButton("Circle"))
  208.             {
  209.                 if (OnCircle != null)
  210.                 {
  211.                     OnCircle();
  212.                 }
  213.             }
  214.             if (Input.GetButtonDown("Circle"))
  215.             {
  216.                 if (OnCircleDown != null)
  217.                 {
  218.                     OnCircleDown();
  219.                 }
  220.             }
  221.             if (Input.GetButtonUp("Circle"))
  222.             {
  223.                 if (OnCircleUp != null)
  224.                 {
  225.                     OnCircleUp();
  226.                 }
  227.             }
  228.            
  229.         #endregion
  230.        
  231.         #region Dpad
  232.             //add code here
  233.             if (Input.GetButtonDown("Up"))
  234.             {
  235.                 if (OnDpadUp != null)
  236.                 {
  237.                     OnDpadUp();
  238.                 }
  239.             }
  240.             if (Input.GetButtonDown("Down"))
  241.             {
  242.                 if (OnDpadDown != null)
  243.                 {
  244.                     OnDpadDown();
  245.                 }
  246.             }
  247.             if (Input.GetButtonDown("Left"))
  248.             {
  249.                 if (OnDpadLeft != null)
  250.                 {
  251.                     OnDpadLeft();
  252.                 }
  253.             }
  254.             if (Input.GetButtonDown("Right"))
  255.             {
  256.                 if (OnDpadRight != null)
  257.                 {
  258.                     OnDpadRight();
  259.                 }
  260.             }
  261.         #endregion
  262.  
  263.         #region Triggers
  264.  
  265.             //L Trigger
  266.             if (Input.GetButton("LTRIG"))
  267.             {
  268.                 if (OnLTrig != null)
  269.                 {
  270.                     OnLTrig();
  271.                 }
  272.             }
  273.             if (Input.GetButtonDown("LTRIG"))
  274.             {
  275.                 if (OnLTrigDown != null)
  276.                 {
  277.                     OnLTrigDown();
  278.                 }
  279.             }
  280.             if (Input.GetButtonUp("LTRIG"))
  281.             {
  282.                 if (OnLTrigUp != null)
  283.                 {
  284.                     OnLTrigUp();
  285.                 }
  286.             }
  287.             //R Trigger
  288.             if (Input.GetButton("RTRIG"))
  289.             {
  290.                 if (OnRTrig != null)
  291.                 {
  292.                     OnRTrig();
  293.                 }
  294.             }
  295.             if (Input.GetButtonDown("RTRIG"))
  296.             {
  297.                 if (OnRTrigDown != null)
  298.                 {
  299.                     OnRTrigDown();
  300.                 }
  301.             }
  302.             if (Input.GetButtonUp("RTRIG"))
  303.             {
  304.                 if (OnRTrigUp != null)
  305.                 {
  306.                     OnRTrigUp();
  307.                 }
  308.             }
  309.  
  310.         #endregion
  311.  
  312.         #region StartSelect
  313.  
  314.             if (Input.GetButtonDown("Start"))
  315.             {
  316.                 if (OnStart != null)
  317.                 {
  318.                     OnStart();
  319.                 }
  320.             }
  321.             if (Input.GetButtonDown("Select"))
  322.             {
  323.                 if (OnSelect != null)
  324.                 {
  325.                     OnSelect();
  326.                 }  
  327.             }
  328.  
  329.         #endregion
  330.  
  331.         #region Joysticks
  332.  
  333.             if (OnLeftStick != null)
  334.             {
  335.                 OnLeftStick(Input.GetAxis("Left Stick Horizontal"),
  336.                                 Input.GetAxis("Left Stick Vertical"));
  337.             }
  338.             if (OnRightStick != null)
  339.             {
  340.                 OnRightStick(Input.GetAxis("Right Stick Horizontal"),
  341.                                 Input.GetAxis("Right Stick Vertical"));
  342.             }
  343.  
  344.         #endregion
  345.  
  346.         #region RearTouches
  347.  
  348.         if (Application.platform == RuntimePlatform.PSP2) // PSV/PSTV only
  349.         {
  350.             currentBackTouchCount = PSVitaInput.touchesSecondary.Length;
  351.  
  352.             // Trigger only if touch count increases or decreases, but not when it's zero or unchanged
  353.             if (currentBackTouchCount != previousBackTouchCount)
  354.             {
  355.                 float[,] dataMatrix = new float[3, 4]; // 3 rows: Finger ID, X, Y
  356.                 bool foundTL1 = false, foundTL2 = false, foundTR1 = false, foundTR2 = false; // Track active touch presence
  357.                 int TL1ID = 255, TL2ID = 255, TR1ID = 255, TR2ID = 255; //initialize with OOB values
  358.                 for (int t = 0; t < currentBackTouchCount && t < 4; t++) // Limit to 4 touches
  359.                 {
  360.                     Touch currentTouch = PSVitaInput.GetSecondaryTouch(t);
  361.  
  362.                     // Store touch data in the matrix
  363.                     dataMatrix[0, t] = currentTouch.fingerId; // Row 0: Touch ID
  364.                     dataMatrix[1, t] = currentTouch.position.x; // Row 1: X positions
  365.                     dataMatrix[2, t] = currentTouch.position.y; // Row 2: Y positions
  366.  
  367.                     float x = currentTouch.position.x;
  368.                     float y = currentTouch.position.y;
  369.  
  370.                     // Check touch positions and update tracking flags
  371.                     if (x >= 0 && x <= 240 && y >= 136 && y <= 272) // TL1, upper left
  372.                     {
  373.                         foundTL1 = true;
  374.                         TL1ID = currentTouch.fingerId;
  375.                     }
  376.  
  377.                     if (x >= 0 && x <= 240 && y >= 0 && y <= 136) // TL2, lower left
  378.                     {
  379.                         foundTL2 = true;
  380.                         TL2ID = currentTouch.fingerId;
  381.                     }
  382.  
  383.                     if (x >= 240 && x <= 480 && y >= 136 && y <= 272) // TR1, upper right
  384.                     {
  385.                         foundTR1 = true;
  386.                         TR1ID = currentTouch.fingerId;
  387.                     }
  388.  
  389.                     if (x >= 240 && x <= 480 && y >= 0 && y <= 136) // TR2, lower right
  390.                     {
  391.                         foundTR2 = true;
  392.                         TR2ID = currentTouch.fingerId;
  393.                     }
  394.                 }
  395.  
  396.                 // Update boolean states only if there is a change, reset ID to OOB value
  397.                 if (!foundTL1)
  398.                 {
  399.                     TL1 = false;
  400.                     TL1ID = 255;
  401.                 }
  402.  
  403.                 if (!foundTL2)
  404.                 {
  405.                     TL2 = false;
  406.                     TL2ID = 255;
  407.                 }
  408.  
  409.                 if (!foundTR1)
  410.                 {
  411.                     TR1 = false;
  412.                     TR1ID = 255;
  413.                 }
  414.  
  415.                 if (!foundTR2)
  416.                 {
  417.                     TR2 = false;
  418.                     TR2ID = 255;
  419.                 }
  420.  
  421.                 // If at least one touch is in the region, set it to true
  422.                 if (foundTL1) TL1 = true;
  423.                 if (foundTL2) TL2 = true;
  424.                 if (foundTR1) TR1 = true;
  425.                 if (foundTR2) TR2 = true;
  426.  
  427.                 // Invoke only when needed
  428.                 if (GetSecondaryTouch != null)
  429.                 {
  430.                     GetSecondaryTouch(dataMatrix, TL1,  
  431.                                                   TL2,  
  432.                                                   TR1,
  433.                                                   TR2); // Push data to subscribers
  434.                 }
  435.             }
  436.  
  437.             // Update previous touch count for the next frame
  438.             previousBackTouchCount = currentBackTouchCount;
  439.         }
  440.         #endregion
  441.     }
  442. }
  443.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement