SanAndreasUnity/Assets/Scripts/UI/TouchInput.cs

268 lines
8.7 KiB
C#
Raw Normal View History

2019-07-23 23:15:49 +00:00
using System.Collections.Generic;
using UnityEngine;
using SanAndreasUnity.Behaviours;
using SanAndreasUnity.Utilities;
using UnityEngine.UI;
namespace SanAndreasUnity.UI
{
public class TouchInput : MonoBehaviour
{
public static TouchInput Instance { get; private set; }
Canvas canvas;
2019-07-25 00:16:59 +00:00
GameObject pedMovementInputGo, vehicleInputGo;
Button walkButton, sprintButton, jumpButton, crouchButton, enterButton, aimButton, fireButton, flyButton,
handbrakeButton, backwardVehicleButton, forwardVehicleButton, exitVehicleButton;
UIEventsPickup jumpButtonEventsPickup, fireButtonEventsPickup, handbrakePickup, backwardVehiclePickup, forwardVehiclePickup;
2019-07-24 17:43:21 +00:00
Text walkButtonText, sprintButtonText, aimButtonText, jumpButtonText, fireButtonText;
2019-07-25 00:16:59 +00:00
ArrowsMovementButton movementButton, turnVehicleButton;
2019-07-23 23:15:49 +00:00
2019-07-25 00:16:59 +00:00
bool m_walkPressed, m_sprintPressed, m_aimPressed, m_crouchPressed, m_enterPressed, m_flyPressed, m_exitVehiclePressed;
2019-07-24 17:43:21 +00:00
public Color activeButtonColor = Color.blue;
public Color inactiveButtonColor = Color.black;
2019-07-23 23:15:49 +00:00
void Awake ()
{
Instance = this;
// setup references
canvas = this.transform.GetChild(0).GetComponent<Canvas>();
pedMovementInputGo = canvas.transform.GetChild(0).gameObject;
2019-07-25 00:16:59 +00:00
vehicleInputGo = canvas.transform.GetChild(1).gameObject;
2019-07-23 23:15:49 +00:00
Transform parent = pedMovementInputGo.transform;
walkButton = parent.Find("WalkButton").GetComponent<Button>();
sprintButton = parent.Find("SprintButton").GetComponent<Button>();
jumpButton = parent.Find("JumpButton").GetComponent<Button>();
crouchButton = parent.Find("CrouchButton").GetComponent<Button>();
enterButton = parent.Find("EnterButton").GetComponent<Button>();
aimButton = parent.Find("AimButton").GetComponent<Button>();
fireButton = parent.Find("FireButton").GetComponent<Button>();
flyButton = parent.Find("FlyButton").GetComponent<Button>();
movementButton = parent.Find("MovementButton").GetComponent<ArrowsMovementButton>();
2019-07-25 00:16:59 +00:00
parent = vehicleInputGo.transform;
exitVehicleButton = parent.Find("ExitButton").GetComponent<Button>();
turnVehicleButton = parent.Find("TurnButton").GetComponent<ArrowsMovementButton>();
// repeat buttons: handbrake, backward, forward
handbrakePickup = parent.Find("HandbrakeButton").gameObject.GetOrAddComponent<UIEventsPickup>();
backwardVehiclePickup = parent.Find("BackwardButton").gameObject.GetOrAddComponent<UIEventsPickup>();
forwardVehiclePickup = parent.Find("ForwardButton").gameObject.GetOrAddComponent<UIEventsPickup>();
2019-07-24 17:43:21 +00:00
// repeat buttons: jump, fire
jumpButtonEventsPickup = jumpButton.gameObject.GetOrAddComponent<UIEventsPickup>();
fireButtonEventsPickup = fireButton.gameObject.GetOrAddComponent<UIEventsPickup>();
// text components
walkButtonText = walkButton.GetComponentInChildren<Text>();
sprintButtonText = sprintButton.GetComponentInChildren<Text>();
aimButtonText = aimButton.GetComponentInChildren<Text>();
jumpButtonText = jumpButton.GetComponentInChildren<Text>();
fireButtonText = fireButton.GetComponentInChildren<Text>();
// setup button event handlers
// note: for this to work properly, EventSystem.Update() must run before our Update()
// toggle buttons
walkButton.onClick.AddListener( () => m_walkPressed = true );
sprintButton.onClick.AddListener( () => m_sprintPressed = true );
aimButton.onClick.AddListener( () => m_aimPressed = true );
2019-07-25 00:16:59 +00:00
// click buttons: crouch, enter, fly, exit vehicle
2019-07-24 17:43:21 +00:00
crouchButton.onClick.AddListener( () => m_crouchPressed = true );
enterButton.onClick.AddListener( () => m_enterPressed = true );
flyButton.onClick.AddListener( () => m_flyPressed = true );
2019-07-25 00:16:59 +00:00
exitVehicleButton.onClick.AddListener( () => m_exitVehiclePressed = true );
2019-07-24 17:43:21 +00:00
2019-07-23 23:15:49 +00:00
}
void OnLoaderFinished()
{
2019-07-25 01:34:27 +00:00
// assign textures to movement buttons' arrows
2019-07-23 23:15:49 +00:00
movementButton.leftArrow.texture = HUD.LeftArrowTexture;
movementButton.rightArrow.texture = HUD.RightArrowTexture;
movementButton.upArrow.texture = HUD.DownArrowTexture;
movementButton.downArrow.texture = HUD.UpArrowTexture;
2019-07-25 01:34:27 +00:00
turnVehicleButton.leftArrow.texture = HUD.LeftArrowTexture;
turnVehicleButton.rightArrow.texture = HUD.RightArrowTexture;
2019-07-23 23:15:49 +00:00
}
void Update()
{
this.ResetCustomInput();
if (!UIManager.Instance.UseTouchInput || !GameManager.CanPlayerReadInput())
{
// we are not using touch input, or we should not read input right now
this.canvas.gameObject.SetActive(false);
return;
}
this.canvas.gameObject.SetActive(true);
// ignore mouse buttons when touch is enabled
CustomInput.Instance.SetButton("LeftClick", false);
if (!CustomInput.Instance.HasButton("RightClick"))
CustomInput.Instance.SetButton("RightClick", false);
CustomInput.Instance.SetButtonDown("LeftClick", false);
CustomInput.Instance.SetButtonDown("RightClick", false);
Ped ped = Ped.Instance;
2019-07-23 23:15:49 +00:00
if (ped != null)
{
if (ped.IsDrivingVehicle)
{
pedMovementInputGo.SetActive(false);
vehicleInputGo.SetActive(true);
this.UpdateVehicleTurningInput();
}
else
{
pedMovementInputGo.SetActive(true);
vehicleInputGo.SetActive(false);
this.UpdatePedMovementInput();
}
this.UpdateActionsInput();
}
else
{
pedMovementInputGo.SetActive(false);
vehicleInputGo.SetActive(false);
}
2019-07-23 23:15:49 +00:00
}
void ResetCustomInput()
{
var customInput = CustomInput.Instance;
if (!UIManager.Instance.UseTouchInput)
{
// touch input is not used
customInput.ResetAllInput();
return;
}
2019-07-24 17:43:21 +00:00
// preserve input for toggle buttons: walk, sprint, aim
2019-07-23 23:15:49 +00:00
bool isWalkOn = customInput.GetButtonNoDefaultInput("Walk");
bool isSprintOn = customInput.GetButtonNoDefaultInput("Sprint");
bool isAimOn = customInput.GetButtonNoDefaultInput("RightClick");
customInput.ResetAllInput();
customInput.SetButton("Walk", isWalkOn);
customInput.SetButton("Sprint", isSprintOn);
customInput.SetButton("RightClick", isAimOn);
}
2019-07-25 01:34:27 +00:00
void UpdatePedMovementInput()
2019-07-23 23:15:49 +00:00
{
2019-07-25 01:34:27 +00:00
// obtain input from arrow button
this.UpdateMovementInput(movementButton, movementButton.GetMovement());
}
2019-07-23 23:15:49 +00:00
2019-07-25 01:34:27 +00:00
void UpdateVehicleTurningInput()
{
// obtain input from arrow button
Vector2 input = turnVehicleButton.GetMovementPercentage();
// ignore y axis
input.y = 0;
// now input has only x value between -1 and 1
2019-07-23 23:15:49 +00:00
2019-07-25 01:34:27 +00:00
this.UpdateMovementInput(turnVehicleButton, input);
}
2019-07-23 23:15:49 +00:00
2019-07-25 01:34:27 +00:00
void UpdateMovementInput(ArrowsMovementButton arrowButton, Vector2 input)
{
if (arrowButton.IsPointerDown && arrowButton.IsPointerInside)
2019-07-23 23:15:49 +00:00
{
2019-07-25 01:34:27 +00:00
// ignore mouse move input while arrow button is pressed
this.ResetMouseMoveInput();
2019-07-23 23:15:49 +00:00
}
2019-07-25 01:34:27 +00:00
this.SetMovementAxesInput(input);
}
void ResetMouseMoveInput()
{
var customInput = CustomInput.Instance;
customInput.SetAxis("Mouse X", 0);
customInput.SetAxis("Mouse Y", 0);
customInput.SetAxis("Joystick X", 0);
customInput.SetAxis("Joystick Y", 0);
}
void SetMovementAxesInput(Vector2 input)
{
var customInput = CustomInput.Instance;
2019-07-23 23:15:49 +00:00
customInput.SetAxis("Vertical", input.y);
customInput.SetAxis("Horizontal", input.x);
}
2019-07-24 17:43:21 +00:00
void UpdateActionsInput()
{
var customInput = CustomInput.Instance;
// get status of jump & fire repeat butons
bool isJumpOn = jumpButtonEventsPickup.IsPointerInside && jumpButtonEventsPickup.IsPointerDown;
bool isFireOn = fireButtonEventsPickup.IsPointerInside && fireButtonEventsPickup.IsPointerDown;
customInput.SetButton("Jump", isJumpOn);
customInput.SetButton("LeftClick", isFireOn);
// process click events
if (m_walkPressed)
customInput.SetButton("Walk", ! customInput.GetButton("Walk"));
if (m_sprintPressed)
customInput.SetButton("Sprint", ! customInput.GetButton("Sprint"));
if (m_aimPressed)
customInput.SetButton("RightClick", ! customInput.GetButton("RightClick"));
if (m_crouchPressed)
customInput.SetKeyDown(KeyCode.C, true);
if (m_enterPressed)
customInput.SetButtonDown("Use", true);
if (m_flyPressed)
customInput.SetKeyDown(KeyCode.T, true);
2019-07-25 00:16:59 +00:00
if (m_exitVehiclePressed)
customInput.SetButtonDown("Use", true);
2019-07-24 17:43:21 +00:00
2019-07-25 00:16:59 +00:00
m_walkPressed = m_sprintPressed = m_aimPressed = m_crouchPressed = m_enterPressed = m_flyPressed = m_exitVehiclePressed = false;
2019-07-24 17:43:21 +00:00
// set color of toggle & repeat buttons
jumpButtonText.color = isJumpOn ? this.activeButtonColor : this.inactiveButtonColor;
fireButtonText.color = isFireOn ? this.activeButtonColor : this.inactiveButtonColor;
walkButtonText.color = customInput.GetButton("Walk") ? this.activeButtonColor : this.inactiveButtonColor;
sprintButtonText.color = customInput.GetButton("Sprint") ? this.activeButtonColor : this.inactiveButtonColor;
aimButtonText.color = customInput.GetButton("RightClick") ? this.activeButtonColor : this.inactiveButtonColor;
}
2019-07-23 23:15:49 +00:00
}
}