summaryrefslogtreecommitdiff
path: root/Thronefall_v1.0/Rewired/Rewired.Demos/TouchJoystickExample.cs
blob: f05af897f3c1a6da89a699763854a454d779a2ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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;
	}
}