Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Controller_Abstraction : MonoBehaviour {
- // Allows access to this script from other scripts
- public static Controller_Abstraction Access;
- //public MainGame_DigitalInputs MainGame_Digital;
- public MenuControls_DigitalInputs MenuControls_Digital;
- // This is used to repeat the same input after the button is held for long enough.
- public float HoldTime;
- // Use this for initialization
- void Start ()
- {
- // Assign this instance of the script.
- Access = this;
- }
- // Update is called once per frame
- void Update ()
- {
- // Begin the HoldTime countdown if any menu key is pressed.
- if (Diamond_SteamController.Access.MenuControlsSet.PressedAny)
- HoldTime += Time.deltaTime;
- else
- HoldTime = 0;
- }
- /// <summary>
- /// Check against the Steam API whether an input is pressed.
- /// </summary>
- /// <param name="prev_Action">Access to the previously changed action.</param>
- /// <param name="SteamBool">Check to see what Steam is reporting for the Action.</param>
- /// <returns>Returns whether the action is pressed.</returns>
- public bool CheckSteam(bool SteamBool, ref bool prev_Action, ref bool Action_Pressed)
- {
- // Start off the button press as false
- Action_Pressed = false;
- if (HoldTime > 0.75)
- {
- // If the hold time exceeds the time limit and the action is currently being held in
- // automatically return a new action press and reset the HoldTime to a slightly lower number.
- if (prev_Action)
- {
- Action_Pressed = true;
- HoldTime = 0.5f;
- }
- }
- // If the previous action isn't equal to the one reported by Steam,
- // we know the state of the action changed.
- // Update the previous action state to reflect the new state.
- if (prev_Action != SteamBool)
- {
- prev_Action = SteamBool;
- // If the updated action change is true, return an Action Press.
- if (prev_Action)
- {
- Action_Pressed = true;
- }
- }
- return Action_Pressed;
- }
- }
- [System.Serializable]
- public class MenuControls_DigitalInputs
- {
- // These are the ActionTypes we can look for.
- // These action types should match what the Steam Controller API report as available.
- public enum ActionType
- {
- MenuUp,
- MenuDown,
- MenuLeft,
- MenuRight,
- MenuSelect,
- MenuCancel,
- MenuReturn
- }
- public List<bool> prev_Action;
- public List<bool> Action_Pressed;
- // This is automatically called from Diamond_SteamController so we know how many actions to store for.
- public void PopulateBooleanLists(int n_Actions)
- {
- // For each action discovered, add a new boolean to each array.
- for (int i = 0; i < n_Actions; ++i)
- {
- prev_Action.Add(false);
- Action_Pressed.Add(false);
- }
- }
- public bool CheckAction(ActionType Action)
- {
- // We know we'll be directly accessing this set, so provide direct access to it via this Access variable.
- Diamond_SteamController.MenuControlsActionSet Access = Diamond_SteamController.Access.MenuControlsSet;
- // Reference variables
- bool prev_Ref = false;
- bool Ref_Pressed = false;
- // Final Press State
- bool ActionPressed;
- // Switch Index to discover which item in the boolean lists to influence.
- int caseIndex = (int)Action;
- switch (Action)
- {
- default:
- Debug.Log("Unknown Button: " + Action);
- return false;
- // First set the pre_Ref to the pre_Action at the current index.
- // Then check to see if an action is pressed.
- // When checking if an action is pressed, the prev_Ref and Ref_Pressed are updated.
- // Assign the prev_Ref and Ref_Pressed to the right item in the array at the current index.
- // Return ActionPressed.
- case ActionType.MenuUp:
- prev_Ref = prev_Action[caseIndex];
- ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.MenuUp, ref prev_Ref, ref Ref_Pressed);
- prev_Action[caseIndex] = prev_Ref;
- Action_Pressed[caseIndex] = Ref_Pressed;
- return ActionPressed;
- case ActionType.MenuDown:
- prev_Ref = prev_Action[caseIndex];
- ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.MenuDown, ref prev_Ref, ref Ref_Pressed);
- prev_Action[caseIndex] = prev_Ref;
- Action_Pressed[caseIndex] = Ref_Pressed;
- return ActionPressed;
- case ActionType.MenuLeft:
- prev_Ref = prev_Action[caseIndex];
- ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.MenuLeft, ref prev_Ref, ref Ref_Pressed);
- prev_Action[caseIndex] = prev_Ref;
- Action_Pressed[caseIndex] = Ref_Pressed;
- return ActionPressed;
- case ActionType.MenuRight:
- prev_Ref = prev_Action[caseIndex];
- ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.MenuRight, ref prev_Ref, ref Ref_Pressed);
- prev_Action[caseIndex] = prev_Ref;
- Action_Pressed[caseIndex] = Ref_Pressed;
- return ActionPressed;
- case ActionType.MenuSelect:
- prev_Ref = prev_Action[caseIndex];
- ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.Select, ref prev_Ref, ref Ref_Pressed);
- prev_Action[caseIndex] = prev_Ref;
- Action_Pressed[caseIndex] = Ref_Pressed;
- return ActionPressed;
- case ActionType.MenuCancel:
- prev_Ref = prev_Action[caseIndex];
- ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.Cancel, ref prev_Ref, ref Ref_Pressed);
- prev_Action[caseIndex] = prev_Ref;
- Action_Pressed[caseIndex] = Ref_Pressed;
- return ActionPressed;
- case ActionType.MenuReturn:
- prev_Ref = prev_Action[caseIndex];
- ActionPressed = Controller_Abstraction.Access.CheckSteam(Access.Return, ref prev_Ref, ref Ref_Pressed);
- prev_Action[caseIndex] = prev_Ref;
- Action_Pressed[caseIndex] = Ref_Pressed;
- return ActionPressed;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement