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
122 lines (102 loc) · 3.52 KB
/
Copy pathMouseInput.cs
File metadata and controls
122 lines (102 loc) · 3.52 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
/*
* @author Valentin Simonov / http://va.lent.in/
*/
using TouchScript.Utils.Editor.Attributes;
using UnityEngine;
namespace TouchScript.InputSources
{
/// <summary>
/// Input source to grab mouse clicks as touch points.
/// </summary>
[AddComponentMenu("TouchScript/Input Sources/Mouse Input")]
public class MouseInput : InputSource
{
#region Public properties
[ToggleLeft]
public bool DisableOnMobilePlatforms = true;
#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:
// 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));
} else
{
if (mousePointId == -1) mousePointId = beginTouch(new Vector2(pos.x, pos.y));
}
} 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
}
}