summaryrefslogtreecommitdiff
path: root/Rewired/Rewired.Demos/TouchJoystickExample.cs
diff options
context:
space:
mode:
authorchai <215380520@qq.com>2024-05-19 16:05:01 +0800
committerchai <215380520@qq.com>2024-05-19 16:05:01 +0800
commitc5f145786f4c6d2fe4bea831dfc16e52228920a5 (patch)
treea6ead7ea8266c767d58ed0f816dcd7a1dd75bd65 /Rewired/Rewired.Demos/TouchJoystickExample.cs
parent48b64e573a1709dc923cb9162b55be0246b3ff63 (diff)
* move
Diffstat (limited to 'Rewired/Rewired.Demos/TouchJoystickExample.cs')
-rw-r--r--Rewired/Rewired.Demos/TouchJoystickExample.cs110
1 files changed, 0 insertions, 110 deletions
diff --git a/Rewired/Rewired.Demos/TouchJoystickExample.cs b/Rewired/Rewired.Demos/TouchJoystickExample.cs
deleted file mode 100644
index f05af89..0000000
--- a/Rewired/Rewired.Demos/TouchJoystickExample.cs
+++ /dev/null
@@ -1,110 +0,0 @@
-using System;
-using UnityEngine;
-using UnityEngine.EventSystems;
-using UnityEngine.UI;
-
-namespace Rewired.Demos;
-
-[AddComponentMenu("")]
-[RequireComponent(typeof(Image))]
-public class TouchJoystickExample : MonoBehaviour, IPointerDownHandler, IEventSystemHandler, IPointerUpHandler, IDragHandler
-{
- public bool allowMouseControl = true;
-
- public int radius = 50;
-
- private Vector2 origAnchoredPosition;
-
- private Vector3 origWorldPosition;
-
- private Vector2 origScreenResolution;
-
- private ScreenOrientation origScreenOrientation;
-
- [NonSerialized]
- private bool hasFinger;
-
- [NonSerialized]
- private int lastFingerId;
-
- public Vector2 position { get; private set; }
-
- private void Start()
- {
- if (SystemInfo.deviceType == DeviceType.Handheld)
- {
- allowMouseControl = false;
- }
- StoreOrigValues();
- }
-
- private void Update()
- {
- if ((float)Screen.width != origScreenResolution.x || (float)Screen.height != origScreenResolution.y || Screen.orientation != origScreenOrientation)
- {
- Restart();
- StoreOrigValues();
- }
- }
-
- private void Restart()
- {
- hasFinger = false;
- (base.transform as RectTransform).anchoredPosition = origAnchoredPosition;
- position = Vector2.zero;
- }
-
- private void StoreOrigValues()
- {
- origAnchoredPosition = (base.transform as RectTransform).anchoredPosition;
- origWorldPosition = base.transform.position;
- origScreenResolution = new Vector2(Screen.width, Screen.height);
- origScreenOrientation = Screen.orientation;
- }
-
- private void UpdateValue(Vector3 value)
- {
- Vector3 vector = origWorldPosition - value;
- vector.y = 0f - vector.y;
- vector /= (float)radius;
- position = new Vector2(0f - vector.x, vector.y);
- }
-
- void IPointerDownHandler.OnPointerDown(PointerEventData eventData)
- {
- if (!hasFinger && (allowMouseControl || !IsMousePointerId(eventData.pointerId)))
- {
- hasFinger = true;
- lastFingerId = eventData.pointerId;
- }
- }
-
- void IPointerUpHandler.OnPointerUp(PointerEventData eventData)
- {
- if (eventData.pointerId == lastFingerId && (allowMouseControl || !IsMousePointerId(eventData.pointerId)))
- {
- Restart();
- }
- }
-
- void IDragHandler.OnDrag(PointerEventData eventData)
- {
- if (hasFinger && eventData.pointerId == lastFingerId)
- {
- Vector3 vector = new Vector3(eventData.position.x - origWorldPosition.x, eventData.position.y - origWorldPosition.y);
- vector = Vector3.ClampMagnitude(vector, radius);
- Vector3 value = origWorldPosition + vector;
- base.transform.position = value;
- UpdateValue(value);
- }
- }
-
- private static bool IsMousePointerId(int id)
- {
- if (id != -1 && id != -2)
- {
- return id == -3;
- }
- return true;
- }
-}