blob: fd84caa3c98031ad89b8c7707fce6b246cf395fe (
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
using System;
using UnityEngine;
public class Controller
{
public bool AnyTouch
{
get
{
return this.Touches[0].IsDown || this.Touches[1].IsDown;
}
}
public bool AnyTouchDown
{
get
{
return this.Touches[0].TouchStart || this.Touches[1].TouchStart;
}
}
public bool AnyTouchUp
{
get
{
return this.Touches[0].TouchEnd || this.Touches[1].TouchEnd;
}
}
public bool FirstDown
{
get
{
return this.Touches[0].TouchStart;
}
}
public Vector2 DragPosition
{
get
{
return this.Touches[this.touchId].Position;
}
}
public Vector2 DragStartPosition
{
get
{
return this.Touches[this.touchId].DownAt;
}
}
public readonly Controller.TouchState[] Touches = new Controller.TouchState[2];
private Collider2D amTouching;
private int touchId = -1;
public class TouchState
{
public Vector2 DownAt;
public Vector2 Position;
public bool WasDown;
public bool IsDown;
public bool TouchStart;
public bool TouchEnd;
}
public Controller()
{
for (int i = 0; i < this.Touches.Length; i++)
{
this.Touches[i] = new Controller.TouchState();
}
}
public DragState CheckDrag(Collider2D coll, bool firstOnly = false)
{
if (!coll)
{
return DragState.NoTouch;
}
if (this.touchId <= -1)
{
if (firstOnly)
{
Controller.TouchState touchState = this.Touches[0];
if (touchState.TouchStart && coll.OverlapPoint(touchState.Position))
{
this.amTouching = coll;
this.touchId = 0;
return DragState.TouchStart;
}
}
else
{
for (int i = 0; i < this.Touches.Length; i++)
{
Controller.TouchState touchState2 = this.Touches[i];
if (touchState2.TouchStart && coll.OverlapPoint(touchState2.Position))
{
this.amTouching = coll;
this.touchId = i;
return DragState.TouchStart;
}
}
}
return DragState.NoTouch;
}
if (coll != this.amTouching)
{
return DragState.NoTouch;
}
if (this.Touches[this.touchId].IsDown)
{
return DragState.Dragging;
}
this.amTouching = null;
this.touchId = -1;
return DragState.Released;
}
public void Update()
{
Controller.TouchState touchState = this.Touches[0];
bool mouseButton = Input.GetMouseButton(0);
touchState.Position = Camera.main.ScreenToWorldPoint(Input.mousePosition);
touchState.TouchStart = (!touchState.IsDown && mouseButton);
if (touchState.TouchStart)
{
touchState.DownAt = touchState.Position;
}
touchState.TouchEnd = (touchState.IsDown && !mouseButton);
touchState.IsDown = mouseButton;
}
public void Reset()
{
for (int i = 0; i < this.Touches.Length; i++)
{
this.Touches[i] = new Controller.TouchState();
}
this.touchId = -1;
this.amTouching = null;
}
public Controller.TouchState GetTouch(int i)
{
return this.Touches[i];
}
}
|