Advertisement
CassataGames

Controller Abstraction

Jan 15th, 2017
1,600
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.72 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Controller_Abstraction : MonoBehaviour {
  6.  
  7.     // Allows access to this script from other scripts
  8.     public static Controller_Abstraction Access;
  9.  
  10.     //public MainGame_DigitalInputs MainGame_Digital;
  11.     public MenuControls_DigitalInputs MenuControls_Digital;
  12.  
  13.     // This is used to repeat the same input after the button is held for long enough.
  14.     public float HoldTime;
  15.  
  16.     // Use this for initialization
  17.     void Start ()
  18.     {
  19.         // Assign this instance of the script.
  20.         Access = this;
  21.     }
  22.    
  23.     // Update is called once per frame
  24.     void Update ()
  25.     {
  26.         // Begin the HoldTime countdown if any menu key is pressed.
  27.         if (Diamond_SteamController.Access.MenuControlsSet.PressedAny)
  28.             HoldTime += Time.deltaTime;
  29.         else
  30.             HoldTime = 0;
  31.     }
  32.  
  33.     /// <summary>
  34.     /// Check against the Steam API whether an input is pressed.
  35.     /// </summary>
  36.     /// <param name="prev_Action">Access to the previously changed action.</param>
  37.     /// <param name="SteamBool">Check to see what Steam is reporting for the Action.</param>
  38.     /// <returns>Returns whether the action is pressed.</returns>
  39.     public bool CheckSteam(bool SteamBool, ref bool prev_Action, ref bool Action_Pressed)
  40.     {
  41.         // Start off the button press as false
  42.         Action_Pressed = false;
  43.         if (HoldTime > 0.75)
  44.         {
  45.             // If the hold time exceeds the time limit and the action is currently being held in
  46.             // automatically return a new action press and reset the HoldTime to a slightly lower number.
  47.             if (prev_Action)
  48.             {
  49.                 Action_Pressed = true;
  50.                 HoldTime = 0.5f;
  51.             }
  52.         }
  53.         // If the previous action isn't equal to the one reported by Steam,
  54.         // we know the state of the action changed.
  55.         // Update the previous action state to reflect the new state.
  56.         if (prev_Action != SteamBool)
  57.         {
  58.             prev_Action = SteamBool;
  59.             // If the updated action change is true, return an Action Press.
  60.             if (prev_Action)
  61.             {
  62.                 Action_Pressed = true;
  63.             }
  64.         }
  65.         return Action_Pressed;
  66.     }
  67. }
  68.  
  69. [System.Serializable]
  70. public class MenuControls_DigitalInputs
  71. {
  72.     // These are the ActionTypes we can look for.
  73.     // These action types should match what the Steam Controller API report as available.
  74.     public enum ActionType
  75.     {
  76.         MenuUp,
  77.         MenuDown,
  78.         MenuLeft,
  79.         MenuRight,
  80.         MenuSelect,
  81.         MenuCancel,
  82.         MenuReturn
  83.     }
  84.  
  85.     public List<bool> prev_Action;
  86.     public List<bool> Action_Pressed;
  87.  
  88.     // This is automatically called from Diamond_SteamController so we know how many actions to store for.
  89.     public void PopulateBooleanLists(int n_Actions)
  90.     {
  91.         // For each action discovered, add a new boolean to each array.
  92.         for (int i = 0; i < n_Actions; ++i)
  93.         {
  94.             prev_Action.Add(false);
  95.             Action_Pressed.Add(false);
  96.         }
  97.     }
  98.  
  99.     public bool CheckAction(ActionType Action)
  100.     {
  101.         // We know we'll be directly accessing this set, so provide direct access to it via this Access variable.
  102.         Diamond_SteamController.MenuControlsActionSet Access = Diamond_SteamController.Access.MenuControlsSet;
  103.  
  104.         // Reference variables
  105.         bool prev_Ref = false;
  106.         bool Ref_Pressed = false;
  107.         // Final Press State
  108.         bool ActionPressed;
  109.         // Switch Index to discover which item in the boolean lists to influence.
  110.         int caseIndex = (int)Action;
  111.         switch (Action)
  112.         {
  113.             default:
  114.                 Debug.Log("Unknown Button: " + Action);
  115.                 return false;
  116.                 // First set the pre_Ref to the pre_Action at the current index.
  117.                 // Then check to see if an action is pressed.
  118.                 // When checking if an action is pressed, the prev_Ref and Ref_Pressed are updated.
  119.                 // Assign the prev_Ref and Ref_Pressed to the right item in the array at the current index.
  120.                 // Return ActionPressed.
  121.             case ActionType.MenuUp:
  122.                 prev_Ref = prev_Action[caseIndex];
  123.                 ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.MenuUp, ref prev_Ref, ref Ref_Pressed);
  124.                 prev_Action[caseIndex] = prev_Ref;
  125.                 Action_Pressed[caseIndex] = Ref_Pressed;
  126.                 return ActionPressed;
  127.             case ActionType.MenuDown:
  128.                 prev_Ref = prev_Action[caseIndex];
  129.                 ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.MenuDown, ref prev_Ref, ref Ref_Pressed);
  130.                 prev_Action[caseIndex] = prev_Ref;
  131.                 Action_Pressed[caseIndex] = Ref_Pressed;
  132.                 return ActionPressed;
  133.             case ActionType.MenuLeft:
  134.                 prev_Ref = prev_Action[caseIndex];
  135.                 ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.MenuLeft, ref prev_Ref, ref Ref_Pressed);
  136.                 prev_Action[caseIndex] = prev_Ref;
  137.                 Action_Pressed[caseIndex] = Ref_Pressed;
  138.                 return ActionPressed;
  139.             case ActionType.MenuRight:
  140.                 prev_Ref = prev_Action[caseIndex];
  141.                 ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.MenuRight, ref prev_Ref, ref Ref_Pressed);
  142.                 prev_Action[caseIndex] = prev_Ref;
  143.                 Action_Pressed[caseIndex] = Ref_Pressed;
  144.                 return ActionPressed;
  145.             case ActionType.MenuSelect:
  146.                 prev_Ref = prev_Action[caseIndex];
  147.                 ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.Select, ref prev_Ref, ref Ref_Pressed);
  148.                 prev_Action[caseIndex] = prev_Ref;
  149.                 Action_Pressed[caseIndex] = Ref_Pressed;
  150.                 return ActionPressed;
  151.             case ActionType.MenuCancel:
  152.                 prev_Ref = prev_Action[caseIndex];
  153.                 ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.Cancel, ref prev_Ref, ref Ref_Pressed);
  154.                 prev_Action[caseIndex] = prev_Ref;
  155.                 Action_Pressed[caseIndex] = Ref_Pressed;
  156.                 return ActionPressed;
  157.             case ActionType.MenuReturn:
  158.                 prev_Ref = prev_Action[caseIndex];
  159.                 ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.Return, ref prev_Ref, ref Ref_Pressed);
  160.                 prev_Action[caseIndex] = prev_Ref;
  161.                 Action_Pressed[caseIndex] = Ref_Pressed;
  162.                 return ActionPressed;
  163.         }
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement