-
Notifications
You must be signed in to change notification settings - Fork 1
/
touchpad.py
241 lines (177 loc) · 7.3 KB
/
touchpad.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
from kivy.uix.widget import Widget
from kivy.graphics import Color, Ellipse
from controller import Controller
class TouchpadWidget(Widget):
def clear_canvas(self):
if not self.visuals_for_touchpad:
return
self.canvas.clear()
def draw_touch(self, touch):
if not self.visuals_for_touchpad:
return
self.canvas.clear()
with self.canvas:
Color(*self.color)
Ellipse(pos=(touch.x - self.radius, touch.y - self.radius), size=self.ellipse_size)
def init_prev_and_count(self, touch_event):
self.touch_down_count += 1
if self.touch_down_count > self.MAX_FINGERS_SUPPORTED:
# self.touch_down_count = 0
raise ValueError(f'Down or move: {self.touch_down_count}')
self.prev_x = round(touch_event.x)
self.prev_y = round(touch_event.y)
def on_touch_down(self, touch_event):
if self.is_in_zone(touch_event):
self.init_prev_and_count(touch_event)
if touch_event.is_double_tap:
if self.controller.is_mouse_mode:
self.double_tap_func()
else:
# self.two_fingers_func()
self.controller.is_mouse_mode = True
return True
else:
return super().on_touch_down(touch_event)
def get_convert_to_send(self, offset, bytes_msg):
def actual_func(x):
if bytes_msg[offset] > 128:
bytes_msg[offset] -= 256
x = bytes_msg[offset] + x
if x > 127:
x = 127
# print(f"value is too much: {x}")
elif x < -127:
x = -127
# print(f"value is too much: {x}")
if x < 0:
x += 256
bytes_msg[offset] = x
return actual_func
def on_touch_move(self, touch_event):
if self.is_in_zone(touch_event) and self.touch_down_count <= 1:
if self.prev_x == self.value_not_set:
self.init_prev_and_count(touch_event)
else:
self.cur_x = round(touch_event.x)
self.cur_y = round(touch_event.y)
self.move_x = self.cur_x - self.prev_x
self.move_y = self.cur_y - self.prev_y
self.prev_x = self.cur_x
self.prev_y = self.cur_y
if self.controller.is_mouse_mode:
self.write_mouse_x_byte(self.move_x)
self.write_mouse_y_byte(-self.move_y)
else:
self.write_scroll_byte(self.move_y * self.scroll_by)
# self.draw_touch(touch_event)
return True
else:
# don't reset here it will prevent releasing pressed button on touch_up
# if finger goes out of touchpad zone and then touch_up happens
return super().on_touch_move(touch_event)
def on_touch_up(self, touch_event):
in_zone = self.is_in_zone(touch_event)
originated_within_element = self.prev_x != self.value_not_set # originated within this element
if self.touch_down_count <= 1:
if originated_within_element:
self.reset()
self.release_func()
else:
if in_zone or originated_within_element:
if not self.mult_fingers_handled:
if self.touch_down_count == 2:
self.mult_fingers_handled = True
self.two_fingers_func()
elif self.touch_down_count == 3:
self.mult_fingers_handled = True
self.three_fingers_func()
if in_zone or originated_within_element:
if self.touch_down_count > 0:
self.touch_down_count -= 1
if in_zone:
# self.clear_canvas()
return True
else:
return super().on_touch_up(touch_event)
def reset(self):
self.prev_x = self.value_not_set
self.cur_x = self.value_not_set
self.mult_fingers_handled = False
def full_reset(self):
self.touch_down_count = 0
self.reset()
self.cur_game_button = self.LeftMouse
def is_in_zone(self, touch_event):
return self.x <= touch_event.x <= self.max_x and self.y <= touch_event.y <= self.max_y
def recalc_size(self):
self.max_x = self.x + self.width
self.max_y = self.y + self.height
def on_size(self, obj, values):
self.full_reset()
self.recalc_size()
def send_buffer(self, dt):
self.mouse_sock.send(self.mouse_msg)
self.mouse_msg[0] = 0
self.mouse_msg[1] = 0
self.scroll_sock.send(self.scroll_msg)
self.scroll_msg[0] = 0
def game_aim(self):
self.controller.send_pressed_mouse(self.RightMouse)
def game_release_aim(self):
self.controller.send_released_mouse(self.RightMouse)
def game_toggle_aim(self):
pass
def game_fire(self):
self.game_aim()
self.controller.send_pressed_mouse(self.LeftMouse)
def game_release_fire(self):
self.controller.send_released_mouse(self.LeftMouse)
self.game_release_aim()
def left_press(self):
self.controller.send_pressed_mouse(self.LeftMouse)
def release_left(self):
self.controller.send_released_mouse(self.LeftMouse)
def right_click(self):
self.controller.is_mouse_mode = True
self.controller.send_type(self.RightMouse)
def toggle_scroll(self):
self.controller.is_mouse_mode = not self.controller.is_mouse_mode
def switch_to_scroll(self):
self.controller.is_mouse_mode = False
def init(self):
self.always_release = True # kivy behavior
self.MAX_FINGERS_SUPPORTED = 3
self.value_not_set = 1000
self.controller = Controller()
self.scroll_by = self.controller.scroll_by
self.LeftMouse = self.controller.LeftMouse
self.RightMouse = self.controller.RightMouse
if self.controller.is_game_mode:
self.double_tap_func = self.game_fire
self.two_fingers_func = self.game_aim
self.three_fingers_func = self.empty_func
self.release_func = self.game_release_fire
else:
self.double_tap_func = self.left_press
self.two_fingers_func = self.toggle_scroll
self.three_fingers_func = self.right_click
self.release_func = self.release_left
self.mouse_sock = self.controller.mouse_sock
self.scroll_sock = self.controller.scroll_sock
self.mouse_msg = bytearray(2)
self.scroll_msg = bytearray(1)
self.write_mouse_x_byte = self.get_convert_to_send(0, self.mouse_msg)
self.write_mouse_y_byte = self.get_convert_to_send(1, self.mouse_msg)
self.write_scroll_byte = self.get_convert_to_send(0, self.scroll_msg)
self.visuals_for_touchpad = self.controller.visuals_for_touchpad
self.touch_down_count = 0
self.prev_x = 0
self.prev_y = 0
self.move_x = 0
self.move_y = 0
self.full_reset()
max_color = 255
self.color = (80 / max_color, 200 / max_color, 1 / max_color)
diameter = 30.
self.radius = diameter / 2
self.ellipse_size = (diameter, diameter)