-
Notifications
You must be signed in to change notification settings - Fork 0
/
TouchHandler.cs
68 lines (61 loc) · 2.05 KB
/
TouchHandler.cs
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
using System.Collections.Generic;
using System.Linq;
using Android.Views;
namespace Segmentus
{
static class TouchHandler
{
public static HashSet<TouchablePart> listeners = new HashSet<TouchablePart>();
public static void RemoveAllListeners() => listeners.Clear();
public static bool Handle(MotionEvent e)
{
if (e.PointerCount > 1)
return false;
switch (e.Action)
{
case MotionEventActions.Down:
PerformTouchDown((int)e.GetX(), (int)e.GetY());
break;
case MotionEventActions.Up:
PerformTouchUp((int)e.GetX(), (int)e.GetY());
break;
case MotionEventActions.Cancel:
PerformTouchCancel((int)e.GetX(), (int)e.GetY());
break;
case MotionEventActions.Move:
PerformTouchMove((int)e.GetX(), (int)e.GetY());
break;
}
return true;
}
static void PerformTouchDown(int x, int y)
{
foreach (TouchablePart t in listeners.ToList())
if (t.Bounds.Contains(x, y))
t.OnTouchDown(x, y);
else
t.OnTouchOutside(x, y);
}
static void PerformTouchUp(int x, int y)
{
foreach (TouchablePart t in listeners.ToList())
if (t.Bounds.Contains(x, y))
t.OnTouchUp(x, y);
else
t.OnTouchCancel(x, y);
}
static void PerformTouchCancel(int x, int y)
{
foreach (TouchablePart t in listeners.ToList())
t.OnTouchCancel(x, y);
}
static void PerformTouchMove(int x, int y)
{
foreach (TouchablePart t in listeners.ToList())
if (t.Bounds.Contains(x, y))
t.OnTouchMove(x, y);
else
t.OnTouchOutside(x, y);
}
}
}