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; }
|
|
|
|
|
2019-07-26 19:50:27 +00:00
|
|
|
public Canvas canvas;
|
|
|
|
public GameObject panel;
|
|
|
|
public GameObject pedMovementInputGo, vehicleInputGo;
|
2019-07-25 00:16:59 +00:00
|
|
|
Button walkButton, sprintButton, jumpButton, crouchButton, enterButton, aimButton, fireButton, flyButton,
|
2019-07-25 16:17:58 +00:00
|
|
|
exitVehicleButton, nextWeaponButton, previousWeaponButton;
|
2019-07-26 19:50:27 +00:00
|
|
|
UIEventsPickup jumpButtonEventsPickup, fireButtonEventsPickup, handbrakePickup, backwardVehiclePickup, forwardVehiclePickup, panelPickup;
|
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 16:17:58 +00:00
|
|
|
bool m_walkPressed, m_sprintPressed, m_aimPressed, m_crouchPressed, m_enterPressed, m_flyPressed, m_exitVehiclePressed,
|
|
|
|
m_nextWeaponPressed, m_previousWeaponPressed;
|
2019-07-24 17:43:21 +00:00
|
|
|
|
|
|
|
public Color activeButtonColor = Color.blue;
|
|
|
|
public Color inactiveButtonColor = Color.black;
|
|
|
|
|
2019-07-25 16:59:19 +00:00
|
|
|
public float vehicleTurnMultiplier = 1.5f;
|
|
|
|
|
2019-07-26 19:50:27 +00:00
|
|
|
//List<Vector2> m_panelDeltas = new List<Vector2>();
|
|
|
|
Vector2 m_panelDeltasSum = Vector2.zero;
|
|
|
|
public float touchPointerSensitivity = 1f;
|
|
|
|
|
2019-07-23 23:15:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
void Awake ()
|
|
|
|
{
|
|
|
|
Instance = this;
|
|
|
|
|
|
|
|
// setup references
|
|
|
|
|
|
|
|
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>();
|
2019-07-25 16:17:58 +00:00
|
|
|
nextWeaponButton = parent.Find("NextWeaponButton").GetComponent<Button>();
|
|
|
|
previousWeaponButton = parent.Find("PreviousWeaponButton").GetComponent<Button>();
|
2019-07-23 23:15:49 +00:00
|
|
|
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>();
|
|
|
|
|
2019-07-26 19:50:27 +00:00
|
|
|
// panel
|
|
|
|
panelPickup = this.panel.GetOrAddComponent<UIEventsPickup>();
|
|
|
|
|
2019-07-24 17:43:21 +00:00
|
|
|
// text components
|
|
|
|
walkButtonText = walkButton.GetComponentInChildren<Text>();
|
|
|
|
sprintButtonText = sprintButton.GetComponentInChildren<Text>();
|
|
|
|
aimButtonText = aimButton.GetComponentInChildren<Text>();
|
|
|
|
jumpButtonText = jumpButton.GetComponentInChildren<Text>();
|
|
|
|
fireButtonText = fireButton.GetComponentInChildren<Text>();
|
|
|
|
|
2019-07-26 19:50:27 +00:00
|
|
|
// setup event handlers
|
2019-07-24 17:43:21 +00:00
|
|
|
// 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 16:17:58 +00:00
|
|
|
// click buttons: crouch, enter, fly, exit vehicle, next weapon, previous weapon
|
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-25 16:17:58 +00:00
|
|
|
nextWeaponButton.onClick.AddListener( () => m_nextWeaponPressed = true );
|
|
|
|
previousWeaponButton.onClick.AddListener( () => m_previousWeaponPressed = true );
|
2019-07-24 17:43:21 +00:00
|
|
|
|
2019-07-26 19:50:27 +00:00
|
|
|
// panel
|
|
|
|
panelPickup.onDrag += (eventData) => this.OnPanelDrag(eventData);
|
|
|
|
|
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);
|
|
|
|
|
2019-07-26 19:50:27 +00:00
|
|
|
var customInput = CustomInput.Instance;
|
|
|
|
|
2019-07-23 23:15:49 +00:00
|
|
|
// ignore mouse buttons when touch is enabled
|
2019-07-26 19:50:27 +00:00
|
|
|
customInput.SetButton("LeftClick", false);
|
|
|
|
if (!customInput.HasButton("RightClick"))
|
|
|
|
customInput.SetButton("RightClick", false);
|
|
|
|
customInput.SetButtonDown("LeftClick", false);
|
|
|
|
customInput.SetButtonDown("RightClick", false);
|
2019-07-23 23:15:49 +00:00
|
|
|
|
2019-07-25 01:46:30 +00:00
|
|
|
Ped ped = Ped.Instance;
|
2019-07-23 23:15:49 +00:00
|
|
|
|
2019-07-25 01:46:30 +00:00
|
|
|
if (ped != null)
|
|
|
|
{
|
|
|
|
if (ped.IsDrivingVehicle)
|
|
|
|
{
|
|
|
|
pedMovementInputGo.SetActive(false);
|
|
|
|
vehicleInputGo.SetActive(true);
|
|
|
|
|
2019-07-25 02:30:48 +00:00
|
|
|
this.UpdateVehicleMovementInput();
|
2019-07-25 01:46:30 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
2019-07-26 19:50:27 +00:00
|
|
|
// set mouse move input based on panel drag events
|
|
|
|
|
|
|
|
this.ResetMouseMoveInput();
|
2019-07-27 17:40:32 +00:00
|
|
|
customInput.SetAxis("Mouse X", m_panelDeltasSum.x / Screen.width * this.touchPointerSensitivity * GameManager.Instance.cursorSensitivity.x);
|
|
|
|
customInput.SetAxis("Mouse Y", m_panelDeltasSum.y / Screen.height * this.touchPointerSensitivity * GameManager.Instance.cursorSensitivity.y);
|
2019-07-26 19:50:27 +00:00
|
|
|
// reset deltas sum
|
|
|
|
m_panelDeltasSum = Vector2.zero;
|
|
|
|
|
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 02:30:48 +00:00
|
|
|
void UpdateVehicleMovementInput()
|
2019-07-25 01:34:27 +00:00
|
|
|
{
|
2019-07-25 02:30:48 +00:00
|
|
|
var customInput = CustomInput.Instance;
|
|
|
|
|
2019-07-25 01:34:27 +00:00
|
|
|
// 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-25 16:59:19 +00:00
|
|
|
// apply multiplier
|
|
|
|
input.x *= this.vehicleTurnMultiplier;
|
|
|
|
// clamp between -1 and 1
|
|
|
|
input.x = Mathf.Clamp(input.x, -1f, 1f);
|
2019-07-23 23:15:49 +00:00
|
|
|
|
2019-07-25 01:34:27 +00:00
|
|
|
this.UpdateMovementInput(turnVehicleButton, input);
|
2019-07-25 02:30:48 +00:00
|
|
|
|
|
|
|
// get status of backward and forward buttons
|
|
|
|
bool isBackwardOn = backwardVehiclePickup.IsPointerInside && backwardVehiclePickup.IsPointerDown;
|
|
|
|
bool isForwardOn = forwardVehiclePickup.IsPointerInside && forwardVehiclePickup.IsPointerDown;
|
|
|
|
if (isBackwardOn || isForwardOn)
|
|
|
|
customInput.SetAxis("Vertical", isForwardOn ? 1.0f : -1.0f);
|
|
|
|
|
2019-07-25 01:34:27 +00:00
|
|
|
}
|
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);
|
|
|
|
|
2019-07-25 02:30:48 +00:00
|
|
|
// get status of handbrake
|
|
|
|
|
|
|
|
bool isHandbrakeOn = handbrakePickup.IsPointerInside && handbrakePickup.IsPointerDown;
|
|
|
|
customInput.SetButton("Brake", isHandbrakeOn);
|
|
|
|
|
2019-07-24 17:43:21 +00:00
|
|
|
// 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-25 16:17:58 +00:00
|
|
|
if (m_nextWeaponPressed)
|
|
|
|
customInput.SetKeyDown (KeyCode.E, true);
|
|
|
|
if (m_previousWeaponPressed)
|
|
|
|
customInput.SetKeyDown (KeyCode.Q, true);
|
2019-07-24 17:43:21 +00:00
|
|
|
|
2019-07-25 16:17:58 +00:00
|
|
|
m_walkPressed = m_sprintPressed = m_aimPressed = m_crouchPressed = m_enterPressed = m_flyPressed =
|
|
|
|
m_exitVehiclePressed = m_nextWeaponPressed = m_previousWeaponPressed = 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-26 19:50:27 +00:00
|
|
|
void OnPanelDrag(UnityEngine.EventSystems.PointerEventData eventData)
|
|
|
|
{
|
|
|
|
m_panelDeltasSum += eventData.delta;
|
|
|
|
}
|
|
|
|
|
2019-07-23 23:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|