-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTouchManager.py
118 lines (92 loc) · 3.43 KB
/
TouchManager.py
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
import math, pygame
class TouchGesturesManager():
'''
Classe qui assure la reconnaissance des gestes faits au niveau tactile.
Elle reconnait, filtre, transforme, et envoie les messages finis au InputManager, qui renvoie tout ca
au MainSC, qui lui meme envoie tout ca aux applications du systeme.
'''
def __init__(self):
'''
This variable stores the gestures found with the mouse (e.g. when a mousePress happened but not
the same button's mouseRelease event). This allows us to store the mouse position when it was clicked
'''
self.current_gesture = None
'''
Variables generales
'''
self.mouse_pressed = False
self.previous_mousepos = [0, 0]
self.previous_frame_events = []
def Update(self, pygame_events):
frame_events = []
mx, my = pygame.mouse.get_pos()
mousepos = [mx, my]
for event in pygame_events:
if event.type == pygame.MOUSEBUTTONDOWN:
self.mouse_pressed = True
self.current_gesture = MouseGesture(mousepos)
if event.type == pygame.MOUSEBUTTONUP:
self.mouse_pressed = False
self.current_gesture.mouseReleased(mousepos)
result = self.current_gesture.result_direction
if result is not None:
frame_events.append(result)
frame_events.append("ENDSCROLL")
self.current_gesture = None
if self.mouse_pressed == True:
frame_movement = [self.previous_mousepos[0] - mousepos[0], self.previous_mousepos[1] - mousepos[1]]
if not (frame_movement[0] == 0 and frame_movement[1] == 0) and (frame_movement[0] < 100 and frame_movement[1] < 100) and (frame_movement[0] > -100 and frame_movement[1] > -100):
frame_events.append("SCROLL " + str(frame_movement[0]) + " " + str(frame_movement[1]))
self.previous_mousepos = mousepos
self.previous_frame_events = frame_events
if len(frame_events) > 0: print frame_events
return frame_events
class MouseGesture():
def __init__(self, mousePos):
self.mouse_pos = mousePos
self.result_direction = ""
self.Done = False
def mouseReleased(self, mousePos):
end_mouse_pos = mousePos
direction = (end_mouse_pos[0] - self.mouse_pos[0], (end_mouse_pos[1] - self.mouse_pos[1]) * -1)
'''
Trigonometric side
'''
r = math.sqrt((direction[0] ** 2) + (direction[1] ** 2))
if r > 40:
'''
On calcule l'angle et on prend une approximation entre 0 et PI (le signe n'apparait pas encore)
'''
angle = math.acos(direction[0] / r)
'''
On prend le sinus pour voir si l'angle est negatif ou pas
'''
sin_angle = direction[1] / r # on prend
'''
On le convertit en degres pour questions de simplicite d'ecriture
'''
angle = angle * 180.0 / math.pi
'''
Si le sinuis est negatif, alors on en deduit le vrai angle entre 0 et 360 degres.
'''
if sin_angle < 0:
angle = 360 - angle
'''
Finalement, on en deduit la direction du swipe.
swiping in one direction causes the program to open the screen in the opposite direction (logique).
'''
if angle > 315 or angle < 45:
self.result_direction = "RIGHT"
elif angle > 45 and angle < 135:
self.result_direction = "UP"
elif angle > 135 and angle < 225:
self.result_direction = "LEFT"
elif angle > 225 and angle < 315:
self.result_direction = "DOWN"
elif r < 20:
'''
si on n'a pas ou peu deplace notre doigt sur l'ecran, on considere l'action comme un simple clic.
'''
x, y = pygame.mouse.get_pos()
#DEBUG print "mouse touch : " + str(x) + " " + str(y)
self.result_direction = "TOUCH " + str(x) + " " + str(y)