forked from TouchScript/TouchScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouseInput.cs
More file actions
148 lines (125 loc) · 4.38 KB
/
MouseInput.cs
File metadata and controls
148 lines (125 loc) · 4.38 KB
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
/*
* @author Valentin Simonov / http://va.lent.in/
*/
using TouchScript.Utils.Attributes;
using UnityEngine;
namespace TouchScript.InputSources
{
/// <summary>
/// Input source which transfers mouse clicks to touches.
/// </summary>
[AddComponentMenu("TouchScript/Input Sources/Mouse Input")]
public sealed class MouseInput : InputSource
{
#region Public properties
/// <summary>
/// Indicates if this input source should be disabled on mobile platforms.
/// </summary>
/// <remarks>
/// Operation Systems which support touch input send first touches as mouse clicks which may result in duplicated touch points in exactly the same coordinates. This affects clusters and multitouch gestures.
/// </remarks>
[ToggleLeft]
public bool DisableOnMobilePlatforms = true;
/// <summary>
/// Tags added to touches coming from this input.
/// </summary>
public Tags Tags = new Tags(Tags.INPUT_MOUSE);
#endregion
#region Private variables
private int mousePointId = -1;
private int fakeMousePointId = -1;
private Vector3 mousePointPos = Vector3.zero;
#endregion
#region Unity methods
/// <inheritdoc />
protected override void OnEnable()
{
if (DisableOnMobilePlatforms)
{
switch (Application.platform)
{
case RuntimePlatform.Android:
case RuntimePlatform.IPhonePlayer:
case RuntimePlatform.WP8Player:
case RuntimePlatform.MetroPlayerARM:
case RuntimePlatform.MetroPlayerX64:
case RuntimePlatform.MetroPlayerX86:
// don't need mouse here
enabled = false;
return;
}
}
base.OnEnable();
mousePointId = -1;
fakeMousePointId = -1;
}
/// <inheritdoc />
protected override void OnDisable()
{
if (mousePointId != -1) cancelTouch(mousePointId);
if (fakeMousePointId != -1) cancelTouch(fakeMousePointId);
base.OnDisable();
}
/// <inheritdoc />
protected override void Update()
{
base.Update();
var upHandled = false;
if (Input.GetMouseButtonUp(0))
{
if (mousePointId != -1)
{
endTouch(mousePointId);
mousePointId = -1;
upHandled = true;
}
}
if (fakeMousePointId > -1 && !(Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt)))
{
endTouch(fakeMousePointId);
fakeMousePointId = -1;
}
if (Input.GetMouseButtonDown(0))
{
var pos = Input.mousePosition;
if ((Input.GetKey(KeyCode.LeftAlt) || Input.GetKey(KeyCode.RightAlt)) && fakeMousePointId == -1)
{
if (fakeMousePointId == -1) fakeMousePointId = beginTouch(new Vector2(pos.x, pos.y)).Id;
}
else
{
if (mousePointId == -1) mousePointId = beginTouch(new Vector2(pos.x, pos.y)).Id;
}
}
else if (Input.GetMouseButton(0))
{
var pos = Input.mousePosition;
if (mousePointPos != pos)
{
mousePointPos = pos;
if (fakeMousePointId > -1 && mousePointId == -1)
{
moveTouch(fakeMousePointId, new Vector2(pos.x, pos.y));
}
else
{
moveTouch(mousePointId, new Vector2(pos.x, pos.y));
}
}
}
if (Input.GetMouseButtonUp(0) && !upHandled)
{
endTouch(mousePointId);
mousePointId = -1;
}
}
#endregion
#region Protected methods
/// <inheritdoc />
protected override ITouch beginTouch(Vector2 position)
{
return beginTouch(position, new Tags(Tags));
}
#endregion
}
}