/[james]/archive/snapcam/snapcamgui.py
ViewVC logotype

Annotation of /archive/snapcam/snapcamgui.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 18 - (hide annotations) (download) (as text)
Tue Feb 11 10:33:20 2003 UTC (21 years, 4 months ago) by james
File MIME type: text/x-python
File size: 7793 byte(s)
Initial import.

1 james 18 #!/usr/bin/python
2    
3     import Tkinter, random, Pmw, os, copy, re, pprint, tkFileDialog
4     Tk = Tkinter
5    
6     ################################################################################
7    
8     margin = 40
9    
10     tick = 1000 # ms
11    
12     zoom = 0.5
13     radius = 4
14     pause = 0
15     increment = 1
16     callback = None
17    
18     cluster = 0 # dis/en-able partioning code
19     if cluster:
20     from clason import calculate_partition
21    
22     ################################################################################
23    
24     def read_position_data(datafile):
25     global data, map_x0, map_y0, map_x1, map_y1, map_width, map_height, frames
26    
27     file = open( datafile )
28    
29     data = []
30    
31     currentTimeDictionary = {}
32    
33     map_name = file.readline();
34     map_type = file.readline();
35     map_display_name = file.readline();
36     map_display_type = file.readline();
37     map_x0, map_y0, map_width, map_height = map(float, file.readline().split())
38     map_x1, map_y1 = map_x0 + map_width, map_y0 + map_height
39     map_y0, map_y1 = -map_y1, -map_y0
40    
41     root.title("%s (%s)" % (map_display_name, map_display_type))
42    
43     l = file.readline()
44     while l != "\n" and l != "\r\n":
45     l = l.strip("\r\n")
46     team, type, block, name, x, y, z, desc = l.split(' ', 7)
47     team, x, y, z = int(team), float(x), float(y), float(z)
48     if type in ('SpawnSphere', 'InteriorInstance', 'AIObjective'):
49     pass
50     else:
51     if type == 'WayPoint':
52     map_item.append([x, -y, z, team, desc])
53     elif (type, block[:9]) == ('StaticShape', 'Generator'):
54     map_item.append([x, -y, z, team, 'G'])
55     elif (type, block) == ('StaticShape', 'StationInventory'):
56     map_item.append([x, -y, z, team, 'I'])
57     elif (type, block) == ('StaticShape', 'StationVehiclePad'):
58     map_item.append([x, -y, z, team, 'V'])
59     elif type == 'Turret':
60     map_item.append([x, -y, z, team, 'T'])
61     elif (type, block[:6]) == ('StaticShape', 'Sensor'):
62     map_item.append([x, -y, z, team, 'S'])
63    
64     l = file.readline()
65    
66     carrier = []
67     l = file.readline()
68     while l != '':
69     if l == "\n" or l == "\r\n":
70     data.append( currentTimeDictionary )
71     currentTimeDictionary = {}
72     carrier = []
73     else:
74     m = l.strip().split(' ', 5) # the file format is: "playerid team x y z name"
75     playerID = m.pop(0)
76     if playerID == 'flag':
77     if len(m) == 2:
78     # the flag is being carried
79     carrier.append(m[1])
80     else:
81     m.append('flag')
82     playerID += m[0]
83    
84     if len(m) == 5 and m[1] != '':
85     team = int(m[0])
86     x = float(m[1])
87     y = -float(m[2])
88     z = float(m[3])
89     name = m[4]
90    
91     currentTimeDictionary[playerID] = x, y, z, team, name, playerID in carrier
92    
93    
94     l = file.readline()
95    
96     frames = len(data)
97     print frames, "frames"
98    
99     ################################################################################
100    
101    
102     def move_to(time):
103     global actor, data
104    
105     actor1 = {}
106    
107     nowData = data[time]
108    
109     for clientID in nowData.keys():
110     x, y, z, team, name, flag = nowData[clientID]
111     x = (x - map_x0) * zoom
112     y = (y - map_y0) * zoom
113     colour = ('black', 'green', 'red')[team]
114    
115     if flag:
116     colour, bcolour = 'black', colour
117     rad = radius * 2
118     else:
119     bcolour = 'black'
120     rad = radius
121    
122     if actor.has_key( clientID ):
123     dot, label = actor[clientID]
124     del actor[clientID]
125     canvas.coords(dot, x - rad, y - rad, x + rad, y + rad)
126     canvas.coords(label, x, y)
127     canvas.itemconfigure(dot, fill=colour, outline=bcolour)
128    
129     else:
130     if team == 1:
131     dot = canvas.create_oval(x - rad, y - rad,
132     x + rad, y + rad, fill=colour, outline=bcolour)
133     else:
134     dot = canvas.create_rectangle(x - rad, y - rad,
135     x + rad, y + rad, fill=colour, outline=bcolour)
136     label = canvas.create_text(x, y, text=name)
137    
138     actor1[clientID] = (dot, label)
139    
140     # any dot left is dead or has left the game, so remove it
141     for dot, label in actor.values():
142     canvas.delete(dot)
143     canvas.delete(label)
144    
145     actor = actor1
146    
147     if cluster:
148     show_partition( calculate_partition( data, time, data[time].keys() ) )
149    
150     status_bar.configure(text='%i / %i, zoom %.2f' % (time+1, frames, zoom))
151    
152    
153     def move():
154     global time, pause, increment, callback, current_partition, current_partition_cost, SWITCH_MULTIPLIER
155    
156     callback = None
157    
158     if increment > 0 and time == frames - 1:
159     return
160     elif increment < 0 and time == 0:
161     return
162    
163     time += increment
164     move_to(time)
165    
166     callback = root.after(int(tick), move)
167    
168    
169    
170    
171    
172     def show_partition( partition ):
173     global rectangle
174    
175     for rect in rectangle:
176     canvas.delete(rect)
177    
178     rectangle = []
179    
180     for set in partition:
181     x0, y0, x1, y1 = 1e10, 1e10, -1e10, -1e10
182     for player in set:
183     x0 = min((x0, data[time][player][0]))
184     y0 = min((y0, data[time][player][1]))
185     x1 = max((x1, data[time][player][0]))
186     y1 = max((y1, data[time][player][1]))
187     rectangle.append(canvas.create_rectangle((x0 - map_x0) * zoom, (y0 - map_y0) * zoom,
188     (x1 - map_x0) * zoom, (y1 - map_y0) * zoom))
189    
190    
191    
192     ################################################################################
193    
194     def zoom_in(event):
195     global zoom
196     zoom = zoom * 1.2
197     update_zoom()
198    
199     def zoom_out(event):
200     global zoom
201     zoom = zoom / 1.2
202     update_zoom()
203    
204     def update_zoom():
205     canvas.coords(bg_rect, 0, 0, map_width * zoom, map_height * zoom)
206     for item in map_item:
207     x, y, z, team, name, label = item
208     x = (x - map_x0) * zoom
209     y = (y - map_y0) * zoom
210     canvas.coords(label, x, y)
211     canvas.resizescrollregion()
212     move_to(time)
213    
214     def unpause():
215     global pause
216     if pause == 0:
217     return
218     pause = 0
219     move()
220    
221     def setpause():
222     global pause, callback
223     if pause == 1:
224     return
225     pause = 1
226     if callback is not None:
227     root.after_cancel(callback)
228     callback = None
229    
230     def button_handler(event):
231     global pause, increment, time, tick, partition_index
232    
233     if event == '|<':
234     setpause()
235     time = 0
236     move_to(time)
237     increment = 1
238     tick = 1000
239     elif event == '<':
240     unpause()
241     if increment == 1:
242     increment = -1
243     tick = 1000
244     else:
245     tick = tick * 0.8
246     elif event == '||<':
247     setpause()
248     if time > 0:
249     time -= 1
250     move_to(time)
251     elif event == '||':
252     if pause:
253     unpause()
254     else:
255     setpause()
256     elif event == '>||':
257     setpause()
258     if time < frames - 1:
259     time += 1
260     move_to(time)
261     elif event == '>':
262     unpause()
263     if increment == -1:
264     increment = 1
265     tick = 1000
266     else:
267     tick = tick * 0.8
268     elif event == '>|':
269     setpause()
270     time = frames - 1
271     move_to(time)
272     increment = -1
273     tick = 1000
274     elif event == 'partition':
275     show_partition( calculate_partition(data, time, data[time].keys()) )
276    
277    
278    
279    
280     ###############################################################################
281    
282     root = Tk.Tk()
283     root.withdraw()
284     Pmw.initialise()
285    
286     actor = {}
287     map_item = []
288     rectangle = []
289     time = 0
290    
291     datafile = tkFileDialog.askopenfilename()
292    
293     read_position_data(datafile)
294    
295     toolbar = Tk.Frame(root)
296    
297     button = {}
298     event_list = ['|<', '<', '||<', '||', '>||', '>', '>|']
299     if cluster:
300     event_list += ['partition']
301     for event in event_list:
302     button[event] = Tk.Button(toolbar, text=event, command=lambda x=event: button_handler(x))
303     button[event].pack(side=Tk.LEFT, anchor=Tk.W)
304    
305     status_bar = Tk.Label(toolbar, text='')
306     status_bar.pack(side=Tk.LEFT, anchor=Tk.W)
307    
308     toolbar.pack(side=Tk.TOP, anchor=Tk.W)
309    
310     canvas = Pmw.ScrolledCanvas(root,
311     hull_width=5000,
312     hull_height=5000,
313     usehullsize=1,
314     canvasmargin=margin)
315     bg_rect = canvas.create_rectangle(0, 0, map_width * zoom, map_height * zoom,
316     outline='black', fill='white')
317    
318     for item in map_item:
319     x, y, z, team, name = item
320     x = (x - map_x0) * zoom
321     y = (y - map_y0) * zoom
322     colour = ('black', 'darkgreen', 'red')[team]
323     label = canvas.create_text(x, y, text=name, fill=colour)
324     item.append(label)
325    
326     canvas.pack(side=Tk.TOP)
327     canvas.resizescrollregion()
328    
329     Tk.Widget.bind(canvas.component('canvas'), '<Button-1>', zoom_in)
330     Tk.Widget.bind(canvas.component('canvas'), '<Button-3>', zoom_out)
331    
332     move()
333     w, h = root.winfo_screenwidth(), root.winfo_screenheight()
334     root.geometry('=%ix%i+%i+%i' % (w * 0.8, h * 0.8, w * 0.1, h * 0.1))
335     root.wm_deiconify()
336    
337     root.mainloop()
338    

  ViewVC Help
Powered by ViewVC 1.1.26