-
Notifications
You must be signed in to change notification settings - Fork 3
/
manymouse_pygame.py
127 lines (100 loc) · 4.54 KB
/
manymouse_pygame.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
import manymouse
import pygame
pygame.init() #should this be called here?
MOUSERELMOTION = 'mouserelmotion'
MOUSEABSMOTION = 'mouseabsmotion'
MOUSEBUTTONUP = 'mousebuttonup'
MOUSEBUTTONDOWN = 'mousebuttondown'
MOUSESCROLL = 'mousescroll'
MOUSEDISCONNECT = 'mousedisconnect'
UP = 0
DOWN = 1
LEFT = 2
RIGHT = 3
MAX_BUTTONS = 32
class Mouse(object):
def __init__(self, id, name, pos=(0,0), connected=True, xscale=1.0, yscale=1.0):
self.id = id
self.name = name
self.x = pos[0]
self.y = pos[1]
self.connected = connected
self.xscale = xscale
self.yscale = yscale
self.buttons = [0]*MAX_BUTTONS
self.scrollticks = [-1,-1,-1,-1]
class Bridge(object):
def __init__(self):
self.mice = []
def init_mice(self, init_pos=(0,0)):
n = manymouse.init()
self.mice = []
if n == 0:
print 'No mice detected!'
else:
for i in range(n):
name = manymouse.device_name(i)
mouse = Mouse(i, name, init_pos)
self.mice.append(mouse)
print '#%s: %s' % (i, name)
def process_events(self):
event = manymouse.Event()
while manymouse.poll_event(event) != 0:
if event.device >= len(self.mice):
continue
mouse = self.mice[event.device]
pgevent = None
if event.type == manymouse.EVENT_RELMOTION:
delta_x = delta_y = 0
if event.item == 0:
mouse.x += event.value
delta_x = event.value
elif event.item == 1:
mouse.y += event.value
delta_y = event.value
pgevent = pygame.event.Event(pygame.locals.USEREVENT, subtype=MOUSERELMOTION, delta=(delta_x, delta_y), mouse=mouse)
elif event.type == manymouse.EVENT_ABSMOTION:
val = event.value - event.minval
maxval = event.maxval - event.minval
if event.item == 0:
mouse.x = (val/maxval)*mouse.xscale
elif event.item == 1:
mouse.y = (val/maxval)*mouse.yscale
pgevent = pygame.event.Event(pygame.locals.USEREVENT, subtype=MOUSEABSMOTION, pos=(mouse.x, mouse.y), mouse=mouse)
elif event.type == manymouse.EVENT_BUTTON:
if event.item < MAX_BUTTONS:
if event.value:
if not mouse.buttons[event.item]:
pgevent = pygame.event.Event(pygame.locals.USEREVENT, subtype=MOUSEBUTTONDOWN, button=event.item, mouse=mouse)
mouse.buttons[event.item] = True
else:
if mouse.buttons[event.item]:
pgevent = pygame.event.Event(pygame.locals.USEREVENT, subtype=MOUSEBUTTONUP, button=event.item, mouse=mouse)
mouse.buttons[event.item] = False
elif event.type == manymouse.EVENT_SCROLL:
if event.item == 0:
if event.value < 0:
d = DOWN
else:
d = UP
mouse.scrollticks[d] = pygame.time.get_ticks()
elif event.item == 1:
if event.value < 0:
d = LEFT
mouse.scrollticks[d] = pygame.time.get_ticks()
else:
d = RIGHT
mouse.scrollticks[d] = pygame.time.get_ticks()
pgevent = pygame.event.Event(pygame.locals.USEREVENT, subtype=MOUSESCROLL, dir=d, mouse=mouse)
elif event.type == manymouse.EVENT_DISCONNECT:
mice[event.device].connected = False
pgevent = pygame.event.Event(pygame.locals.USEREVENT, subtype=MOUSEDISCONNECT, num=event.device, mouse=mouse)
if pgevent == None:
print 'No pygame event generated by manymouse event'
else:
pygame.event.post(pgevent)
def quit(self):
manymouse.quit()
def reinit_mice(self):
manymouse.quit()
self.init()