forked from TouchScript/TouchScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMouseHandler.cs
More file actions
96 lines (84 loc) · 2.85 KB
/
Copy pathMouseHandler.cs
File metadata and controls
96 lines (84 loc) · 2.85 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
/*
* @author Valentin Simonov / http://va.lent.in/
*/
using System;
using UnityEngine;
namespace TouchScript.InputSources
{
internal class MouseHandler
{
private Func<Vector2, int> beginTouch;
private Action<int, Vector2> moveTouch;
private Action<int> endTouch;
private Action<int> cancelTouch;
private int mousePointId = -1;
private int fakeMousePointId = -1;
private Vector3 mousePointPos = Vector3.zero;
public MouseHandler(Func<Vector2, int> beginTouch, Action<int, Vector2> moveTouch, Action<int> endTouch,
Action<int> cancelTouch)
{
this.beginTouch = beginTouch;
this.moveTouch = moveTouch;
this.endTouch = endTouch;
this.cancelTouch = cancelTouch;
mousePointId = -1;
fakeMousePointId = -1;
}
public void 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;
}
}
public void Destroy()
{
if (mousePointId != -1) cancelTouch(mousePointId);
if (fakeMousePointId != -1) cancelTouch(fakeMousePointId);
}
}
}