implement movement button with arrows

This commit is contained in:
in0finite 2019-07-24 01:15:49 +02:00
parent 15ddef4cfd
commit 1ad26f11ed
8 changed files with 289 additions and 1 deletions

View file

@ -2469,6 +2469,7 @@ GameObject:
- component: {fileID: 5276166029133205497}
- component: {fileID: 2601409855260129285}
- component: {fileID: 1001696752572998561}
- component: {fileID: 4551117954529856185}
m_Layer: 0
m_Name: MovementButton
m_TagString: Untagged
@ -2579,6 +2580,22 @@ MonoBehaviour:
m_Calls: []
m_TypeName: UnityEngine.UI.Button+ButtonClickedEvent, UnityEngine.UI, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null
--- !u!114 &4551117954529856185
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4269826247867591473}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 8c4791292f9958a9fbed806e4ded7cfc, type: 3}
m_Name:
m_EditorClassIdentifier:
leftArrow: {fileID: 1126114949254154696}
rightArrow: {fileID: 8882690897287738119}
upArrow: {fileID: 2109694461557635166}
downArrow: {fileID: 833226323754409474}
--- !u!1 &4539774699302292208
GameObject:
m_ObjectHideFlags: 0
@ -2746,6 +2763,7 @@ GameObject:
serializedVersion: 6
m_Component:
- component: {fileID: 6831965866518684118}
- component: {fileID: 1778824086211100969}
m_Layer: 0
m_Name: TouchInput
m_TagString: Untagged
@ -2768,6 +2786,18 @@ Transform:
m_Father: {fileID: 4450559263798022}
m_RootOrder: 6
m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0}
--- !u!114 &1778824086211100969
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 4957097452703587359}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 45aa345221bfc4f68b568dfb0da703aa, type: 3}
m_Name:
m_EditorClassIdentifier:
--- !u!1 &5220313559694819154
GameObject:
m_ObjectHideFlags: 0

View file

@ -273,6 +273,8 @@ namespace SanAndreasUnity.Behaviours.Peds.States
public virtual void OnDrawHUD()
{
return;
if (!UIManager.Instance.UseTouchInput || !GameManager.CanPlayerReadInput())
{
// we are not using touch input, or we should not read input right now

View file

@ -0,0 +1,126 @@
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;
GameObject pedMovementInputGo;
Button walkButton, sprintButton, jumpButton, crouchButton, enterButton, aimButton, fireButton, flyButton;
ArrowsMovementButton movementButton;
void Awake ()
{
Instance = this;
// setup references
canvas = this.transform.GetChild(0).GetComponent<Canvas>();
pedMovementInputGo = canvas.transform.GetChild(0).gameObject;
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>();
}
void OnLoaderFinished()
{
// assign textures to movement button's arrows
movementButton.leftArrow.texture = HUD.LeftArrowTexture;
movementButton.rightArrow.texture = HUD.RightArrowTexture;
movementButton.upArrow.texture = HUD.DownArrowTexture;
movementButton.downArrow.texture = HUD.UpArrowTexture;
}
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);
this.UpdateMovementInput();
}
void ResetCustomInput()
{
var customInput = CustomInput.Instance;
if (!UIManager.Instance.UseTouchInput)
{
// touch input is not used
customInput.ResetAllInput();
return;
}
// preserve input for: walk, sprint, aim
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);
}
void UpdateMovementInput()
{
var customInput = CustomInput.Instance;
// obtain input from movement button
Vector2 input = Vector2.zero;
if (movementButton.IsPointerDown && movementButton.IsPointerInside)
{
input = movementButton.GetMovement();
Debug.LogFormat("pointer is down, input: {0}", input);
}
// set input for vertical and horizontal axis
customInput.SetAxis("Vertical", input.y);
customInput.SetAxis("Horizontal", input.x);
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 45aa345221bfc4f68b568dfb0da703aa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -14,6 +14,11 @@ namespace SanAndreasUnity.Utilities
//Static class with extra functions
public static class F
{
private static Vector3[] m_fourCornersArray = new Vector3[4];
//Returns the number with the greatest absolute value
public static float MaxAbs(params float[] nums)
{
@ -734,6 +739,32 @@ namespace SanAndreasUnity.Utilities
return new Rect (center - size / 2.0f, size);
}
public static Rect GetRect (this RectTransform rectTransform)
{
Vector3[] localCorners = m_fourCornersArray;
rectTransform.GetLocalCorners (localCorners);
float xMin = float.PositiveInfinity, yMin = float.PositiveInfinity;
float xMax = float.NegativeInfinity, yMax = float.NegativeInfinity;
for (int i = 0; i < localCorners.Length; i++) {
Vector3 corner = localCorners [i];
if (corner.x < xMin)
xMin = corner.x;
else if (corner.x > xMax)
xMax = corner.x;
if (corner.y < yMin)
yMin = corner.y;
else if (corner.y > yMax)
yMax = corner.y;
}
return new Rect (xMin, yMin, xMax - xMin, yMax - yMin);
}
public static Texture2D CreateTexture (int width, int height, Color color) {
Color[] pixels = new Color[width * height];

View file

@ -0,0 +1,77 @@
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
namespace SanAndreasUnity.Utilities
{
public class ArrowsMovementButton : MonoBehaviour, IPointerDownHandler, IPointerUpHandler, IPointerEnterHandler, IPointerExitHandler
{
public RawImage leftArrow, rightArrow, upArrow, downArrow;
bool m_isPointerDown = false;
public bool IsPointerDown => m_isPointerDown;
public bool IsPointerInside { get; private set; } = false;
void Awake()
{
// setup references
// leftArrow = this.transform.FindChild("LeftArrow").GetComponent<RawImage>();
// rightArrow = this.transform.FindChild("RightArrow").GetComponent<RawImage>();
// upArrow = this.transform.FindChild("UpArrow").GetComponent<RawImage>();
// downArrow = this.transform.FindChild("DownArrow").GetComponent<RawImage>();
}
void OnDisable()
{
m_isPointerDown = false;
this.IsPointerInside = false;
}
public void OnPointerDown(PointerEventData pointerEventData)
{
Debug.Log(name + " Click in Progress");
m_isPointerDown = true;
}
public void OnPointerUp(PointerEventData pointerEventData)
{
Debug.Log(name + "No longer being clicked");
m_isPointerDown = false;
}
public void OnPointerEnter(PointerEventData pointerEventData)
{
this.IsPointerInside = true;
}
public void OnPointerExit(PointerEventData pointerEventData)
{
this.IsPointerInside = false;
}
public Vector2 GetMovement()
{
if (!m_isPointerDown || !this.IsPointerInside)
return Vector2.zero;
Vector2 mousePos = Input.mousePosition;
Vector2 localPoint = Vector2.zero;
if (!RectTransformUtility.ScreenPointToLocalPointInRectangle(this.transform as RectTransform, mousePos, null, out localPoint))
return Vector2.zero;
Vector2 diff = localPoint;
Debug.LogFormat("mousePos: {0}, diff: {1}", mousePos, diff);
if (diff.sqrMagnitude < float.Epsilon)
return Vector2.zero;
return diff.normalized;
}
}
}

View file

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8c4791292f9958a9fbed806e4ded7cfc
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View file

@ -27,7 +27,7 @@
- Chat
- Android: HUD must run before other UI scripts ; add perms for read/write access to storage ; forbid screen rotation ; disable left and right mouse click ; vehicle touch input: forward, backward, handbrake, left & right ; weapon switching buttons ; circular button for movement ; lock cursor when testing finishes ; must use new UI system ;
- Android: HUD must run before other UI scripts ; add perms for read/write access to storage ; forbid screen rotation ; disable left and right mouse click ; vehicle touch input: forward, backward, handbrake, left & right ; weapon switching buttons ; circular button for movement ; lock cursor when testing finishes ; must use new UI system ; don't report mouse move input while movement button is being pressed ; remove HUD code from state class ;
- Play sounds: horn ; empty weapon slot ; ped damage ; footsteps in run and sprint states ;