-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandle_mouse.py
More file actions
85 lines (69 loc) · 2.54 KB
/
handle_mouse.py
File metadata and controls
85 lines (69 loc) · 2.54 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
import cv2
class MouseHandler():
'''mouse handler'''
def __init__(self, max_height, max_width):
self.max_height = max_height
self.max_width = max_width
self.center_y = max_height//2
self.center_x = max_width//2
self.cursor_y = max_height//2
self.cursor_x = max_width//2
self.move_center = False # move center, using mouse (press and hold scroll)
self.move_center_outside = False # flag for allowing to move center outside window; not in use for now
self.last_event = None
self.shoot = False
def limit_number(self, number, low_limit, high_limit):
'''limit number, to specified bounds'''
if number < low_limit:
return low_limit
if number > high_limit:
return high_limit
return number
def handle_event(self, event, x, y, flags, param):
'''
opencv events list:
EVENT_FLAG_ALTKEY
EVENT_FLAG_CTRLKEY
EVENT_FLAG_LBUTTON
EVENT_FLAG_MBUTTON
EVENT_FLAG_RBUTTON
EVENT_FLAG_SHIFTKEY
EVENT_LBUTTONDBLCLK
EVENT_LBUTTONDOWN
EVENT_LBUTTONUP
EVENT_MBUTTONDBLCLK
EVENT_MBUTTONDOWN
EVENT_MBUTTONUP
EVENT_MOUSEHWHEEL
EVENT_MOUSEMOVE
EVENT_MOUSEWHEEL
EVENT_RBUTTONDBLCLK
EVENT_RBUTTONDOWN
EVENT_RBUTTONUP
'''
if event != 0:
# print('event: {}'.format(event))
pass
if event == cv2.EVENT_LBUTTONDOWN: # 1
# shoot her; press left mouse button
self.shoot = True
if event == cv2.EVENT_LBUTTONUP:
# left button release
self.shoot = False
if event == cv2.EVENT_MBUTTONDOWN: # 3
# press and hold scroll button; move center active
self.move_center = True
if event == cv2.EVENT_MBUTTONUP: # 6
# release scroll button; move center is inactive
self.move_center = False
if self.move_center:
# print(x, y)
if (0 <= x <= self.max_width) and not self.move_center_outside:
self.center_x = x
if (0 <= y <= self.max_height) and not self.move_center_outside:
self.center_y = y
# store current cursor position
self.cursor_x = self.limit_number(x, 0, self.max_width)
self.cursor_y = self.limit_number(y, 0, self.max_height)
if __name__ == "__main__":
pass