-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__init__.py
executable file
·284 lines (247 loc) · 9.1 KB
/
__init__.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import sys, pygame, shutil, os
import time
try:
import looky_config as lcfg
except ImportError:
answer = raw_input('looky_config.py does not exist. Should it be automatically generated? [y/n] ').lower()
if answer=='y':
shutil.copyfile('./looky_config_template.py','./looky_config.py')
import looky_config as lcfg
else:
sys.exit('Please create looky_config.py from looky_config_template.py.')
if not os.path.exists('./dpi.txt'):
answer = raw_input('dpi.txt does not exist. Should calibrate.py be run to determine monitor DPI? [y/n] ').lower()
if answer=='y':
import calibrate
pygame.quit()
else:
sys.exit('Please run calibrate.py first to establish the monitor DPI, or create a text file dpi.txt containing just this number.')
from constants import *
from components import Target,Modstate
import datetime
# load parameters from config file
line_color = lcfg.LINE_COLOR
background_color = lcfg.BACKGROUND_COLOR
font = lcfg.FONT
font_size = lcfg.FONT_SIZE
fps = lcfg.MAX_FPS
try:
display_mode_index = lcfg.DEFAULT_DISPLAY_MODE
except Exception as e:
display_mode_index = 0
# open a log file and define a logging function:
try:
logfile = open('log.txt','a')
except Exception as e:
logfile = open('log.txt','wb')
def log(text):
now = datetime.datetime.now()
logfile.write('%s\t%s\n'%(now.strftime('%Y-%m-%d\t%H:%M:%S'),text))
# initialize pygame, set some initial parameters:
pygame.init()
myfont = pygame.font.SysFont(font, font_size)
pygame.key.set_repeat(100,50)
clock = pygame.time.Clock()
# set up the screen using the desired display mode:
display_modes = pygame.display.list_modes()
# check if the requested index is too high
display_mode_index = min(display_mode_index,len(display_modes)-1)
size = width, height = display_modes[display_mode_index]
n_display_modes = len(display_modes)
screen = pygame.display.set_mode(size)
hwidth = width//2
hheight = height//2
# initialize the Target object:
tar = Target()
# this function is used to cycle through possible
# display modes; time_of_last_mode_change is used
# to display the mode for a second as the user
# is cycling:
time_of_last_mode_change = 0
def cycle_modes():
"""Cycle through display modes."""
global screen,size,width,height,hwidth,hheight,display_modes,display_mode_index,n_display_modes,time_of_last_mode_change
time_of_last_mode_change = time.time()
display_mode_index = (display_mode_index+1)%n_display_modes
size = width, height = display_modes[display_mode_index]
screen = pygame.display.set_mode(size)
hwidth = width//2
hheight = height//2
def exit():
"""Exit via sys.exit()."""
sys.exit()
def fullscreen():
"""Toggle fullscreen mode."""
pygame.display.toggle_fullscreen()
def toggle_help():
"""Toggle help hints."""
global help_on
help_on = not help_on
key_triples = [
(pygame.K_ESCAPE,Modstate('any'),exit),
(pygame.K_q,Modstate('any'),exit),
(pygame.K_F5,Modstate(''),fullscreen),
(pygame.K_LEFT,Modstate(''),tar.left),
(pygame.K_RIGHT,Modstate(''),tar.right),
(pygame.K_UP,Modstate(''),tar.up),
(pygame.K_DOWN,Modstate(''),tar.down),
(pygame.K_LEFT,Modstate('ctrl'),tar.small_left),
(pygame.K_RIGHT,Modstate('ctrl'),tar.small_right),
(pygame.K_UP,Modstate('ctrl'),tar.small_up),
(pygame.K_DOWN,Modstate('ctrl'),tar.small_down),
(pygame.K_LEFT,Modstate('shift-ctrl'),tar.very_small_left),
(pygame.K_RIGHT,Modstate('shift-ctrl'),tar.very_small_right),
(pygame.K_UP,Modstate('shift-ctrl'),tar.very_small_up),
(pygame.K_DOWN,Modstate('shift-ctrl'),tar.very_small_down),
(pygame.K_LEFT,Modstate('alt'),tar.offset_left),
(pygame.K_RIGHT,Modstate('alt'),tar.offset_right),
(pygame.K_UP,Modstate('alt'),tar.offset_up),
(pygame.K_DOWN,Modstate('alt'),tar.offset_down),
(pygame.K_EQUALS,Modstate('ctrl'),tar.increment_line_width),
(pygame.K_MINUS,Modstate('ctrl'),tar.decrement_line_width),
(pygame.K_EQUALS,Modstate(''),tar.increase_radius),
(pygame.K_MINUS,Modstate(''),tar.decrease_radius),
(pygame.K_SPACE,Modstate(''),tar.switch_eye),
(pygame.K_m,Modstate(''),cycle_modes),
(pygame.K_c,Modstate(''),tar.center),
(pygame.K_c,Modstate('ctrl'),tar.center_offsets),
(pygame.K_SLASH,Modstate(''),toggle_help)
]
# Use the keys and function docstrings to make a help menu.
help_strings = []
for kt in key_triples:
if kt[0] in [pygame.K_RIGHT,pygame.K_UP,pygame.K_DOWN]:
continue
elif kt[0]==pygame.K_LEFT:
key_name = 'arrow'
else:
key_name = pygame.key.name(kt[0])
modifier = kt[1].__str__()
if modifier in ['any','none']:
modifier = ''
else:
modifier = modifier+'-'
doc = kt[2].__doc__
lead = '%s%s:'%(modifier,key_name)
while len(lead)<18:
lead = lead+' '
help_strings.append('%s %s'%(lead,doc))
# convert to a dictionary for efficient lookup:
key_dict = {}
for key,key_ms,func in key_triples:
if key in key_dict.keys():
key_dict[key].append((key_ms,func))
else:
key_dict[key] = [(key_ms,func)]
# note the time the loop starts:
t0 = time.time()
log_t0 = time.time()
# a couple of booleans to keep track of state:
printed = False
help_on = False
# current_ms keeps track of the currently pressed
# keyboard modifiers; its state is updated in the
# event loop below, using its own call to
# pygame.key.get_mods
current_ms = Modstate()
n_frames = 0
while 1:
n_frames = n_frames + 1
# throttle the frame rate to the lcfg value:
clock.tick(fps)
# check the current fps:
fps = clock.get_fps()
# get the system time and calculate the
# age of the process and the time since
# the last display mode change:
t = time.time()
mode_age = t-time_of_last_mode_change
age = t-t0
log_age = t-log_t0
if (age):
other_fps = float(n_frames)/float(age)
else:
other_fps = -1
# set mode_changed to true if the mode
# was changed in the last second:
mode_changed = mode_age<1.0
state_changed = False
# after the target has been at one location
# for more than 5 seconds, if the location
# hasn't been printed to the log, print
# it now.
if not printed and age>5.0:
log(tar.msg_log_entry())
printed = True
log_t0 = time.time()
for event in pygame.event.get():
t0 = time.time()
printed = False
state_changed = True
mouse_state_changed = False
current_ms.update()
alt_on = current_ms.alt
if event.type == pygame.QUIT: exit()
elif event.type == pygame.KEYDOWN:
try:
tups = key_dict[event.key]
for key_ms,func in tups:
if key_ms==current_ms:
func()
break
except Exception as e:
pass
elif event.type == pygame.MOUSEMOTION:
mouse_state_changed = True
mousex,mousey = event.pos
mousex = mousex-hwidth
mousey = mousey-hheight
x_deg,y_deg = tar.px2deg(mousex,mousey)
elif event.type == pygame.MOUSEBUTTONUP:
mousex,mousey = event.pos
mousex = mousex-hwidth
mousey = mousey-hheight
x_deg,y_deg = tar.px2deg(mousex,mousey)
tar.set_position(x_deg,y_deg)
if not state_changed and not mode_changed and not mouse_state_changed:
continue
screen.fill(background_color)
# draw here
lines = tar.get_lines()
for pt1,pt2 in lines:
dpt1 = (pt1[0]+hwidth,pt1[1]+hheight)
dpt2 = (pt2[0]+hwidth,pt2[1]+hheight)
pygame.draw.line(screen,line_color,dpt1,dpt2,tar.line_width_px)
if lcfg.EMPTY_CENTER:
cx,cy,crad = tar.get_circle()
cx = cx+hwidth
cy = cy+hheight
pygame.draw.circle(screen,lcfg.BACKGROUND_COLOR,(cx,cy),crad,0)
if alt_on:
offset_lines = tar.get_offset_lines()
for pt1,pt2 in offset_lines:
dpt1 = (pt1[0]+hwidth,pt1[1]+hheight)
dpt2 = (pt2[0]+hwidth,pt2[1]+hheight)
pygame.draw.line(screen,RED,dpt1,dpt2,tar.line_width_px)
msg_list = [tar.msg_ret_location()]
msg_colors = [lcfg.WHITE]
#msg_list = [tar.msg_ret_location(),tar.msg_abs_location()]
#msg_colors = [lcfg.WHITE,lcfg.GRAY]
#msg_list.append('%0.1f fps'%other_fps)
#msg_colors.append(lcfg.GRAY)
if mouse_state_changed:
msg_list.append('%0.3f, %0.3f'%(x_deg,y_deg))
msg_colors.append(lcfg.GREEN)
if alt_on:
msg_list.append(tar.msg_offset_location())
msg_colors.append(lcfg.OFFSET_COLOR)
if mode_changed:
msg_list.append('%d x %d (mode %d)'%(width,height,display_mode_index))
msg_colors.append(lcfg.WHITE)
if help_on:
msg_list = msg_list + help_strings
msg_colors = msg_colors + [lcfg.HELP_COLOR]*len(help_strings)
for idx,(msg,color) in enumerate(zip(msg_list,msg_colors)):
textsurface = myfont.render(msg, False, color)
screen.blit(textsurface,(0,0+idx*font_size))
pygame.display.flip()