0
|
1 #!/usr/bin/python
|
|
2
|
152
|
3 from __future__ import division,nested_scopes
|
0
|
4 from skim.zooming import Zooming,Pair
|
|
5 from math import sqrt,sin,cos
|
|
6 from pygame.rect import Rect
|
152
|
7 from xmlnodebase import xmlnodeclass,collectiveelement,xmldocfile
|
|
8 import dispatcher
|
0
|
9
|
|
10 import Tkinter as tk
|
152
|
11
|
|
12 def pairdist(pair1,pair2):
|
|
13 return sqrt((pair1[0]-pair2[0])**2+(pair1[1]-pair2[1])**2)
|
|
14
|
|
15 def canvashighlighter(canvas,obj,attribute,normalval,highlightval):
|
|
16 """creates bindings on a canvas obj that make attribute go
|
|
17 from normal to highlight when the mouse is over the obj"""
|
|
18 canvas.tag_bind(obj,"<Enter>",
|
|
19 lambda ev: canvas.itemconfig(obj,**{attribute:highlightval}))
|
|
20 canvas.tag_bind(obj,"<Leave>",
|
|
21 lambda ev: canvas.itemconfig(obj,**{attribute:normalval}))
|
|
22
|
|
23 class Field(xmlnodeclass):
|
0
|
24
|
|
25 """one light has a field of influence. for any point on the
|
|
26 canvas, you can ask this field how strong it is. """
|
|
27
|
152
|
28 def name(self,newval=None):
|
|
29 """light/sub name"""
|
|
30 return self._getorsetattr("name",newval)
|
|
31 def center(self,x=None,y=None):
|
|
32 """x,y float coords for the center of this light in the field. returns
|
|
33 a Pair, although it accepts x,y"""
|
|
34 return Pair(self._getorsettypedattr("x",float,x),
|
|
35 self._getorsettypedattr("y",float,y))
|
0
|
36
|
152
|
37 def falloff(self,dist=None):
|
0
|
38
|
|
39 """linear falloff from 1 at center, to 0 at dist pixels away
|
|
40 from center"""
|
152
|
41 return self._getorsettypedattr("falloff",float,dist)
|
0
|
42
|
|
43 def getdistforintensity(self,intens):
|
|
44 """returns the distance you'd have to be for the given intensity (0..1)"""
|
152
|
45 return (1-intens)*self.falloff()
|
0
|
46
|
|
47 def calc(self,x,y):
|
|
48 """returns field strength at point x,y"""
|
152
|
49 dist=pairdist(Pair(x,y),self.center())
|
|
50 return max(0,(self.falloff()-dist)/self.falloff())
|
0
|
51
|
152
|
52 class Fieldset(collectiveelement):
|
0
|
53 """group of fields. persistent."""
|
152
|
54 def childtype(self): return Field
|
|
55
|
|
56 def version(self):
|
|
57 """read-only version attribute on fieldset tag"""
|
|
58 return self._getorsetattr("version",None)
|
|
59
|
0
|
60 def report(self,x,y):
|
|
61 """reports active fields and their intensities"""
|
152
|
62 active=0
|
|
63 for f in self.getall():
|
|
64 name=f.name()
|
0
|
65 intens=f.calc(x,y)
|
|
66 if intens>0:
|
|
67 print name,intens,
|
152
|
68 active+=1
|
|
69 if active>0:
|
|
70 print
|
0
|
71 def getbounds(self):
|
|
72 """returns xmin,xmax,ymin,ymax for the non-zero areas of this field"""
|
|
73 r=None
|
152
|
74 for f in self.getall():
|
0
|
75 rad=f.getdistforintensity(0)
|
152
|
76 fx,fy=f.center()
|
0
|
77 fieldrect=Rect(fx-rad,fy-rad,rad*2,rad*2)
|
|
78 if r is None:
|
|
79 r=fieldrect
|
|
80 else:
|
|
81 r=r.union(fieldrect)
|
|
82 return r.left,r.right,r.top,r.bottom
|
|
83
|
152
|
84 class Fieldsetfile(xmldocfile):
|
|
85 def __init__(self,filename):
|
|
86 self._openornew(filename,topleveltype=Fieldset)
|
|
87 def fieldset(self):
|
|
88 return self._gettoplevel()
|
|
89
|
|
90 ########################################################################
|
|
91 ########################################################################
|
|
92
|
0
|
93 class FieldDisplay:
|
|
94 """the view for a Field."""
|
|
95 def __init__(self,canvas,field):
|
|
96 self.canvas=canvas
|
|
97 self.field=field
|
|
98 self.tags=[str(id(self))] # canvas tag to id our objects
|
152
|
99
|
|
100 def setcoords(self):
|
|
101 """adjust canvas obj coords to match the field"""
|
|
102 # this uses the canvas object ids saved by makeobjs
|
|
103 f=self.field
|
|
104 c=self.canvas
|
|
105 w2c=self.canvas.world2canvas
|
|
106
|
|
107 # rings
|
|
108 for intens,ring in self.rings.items():
|
|
109 rad=f.getdistforintensity(intens)
|
|
110 p1=w2c(*(f.center()-Pair(rad,rad)))
|
|
111 p2=w2c(*(f.center()+Pair(rad,rad)))
|
|
112 c.coords(ring,p1[0],p1[1],p2[0],p2[1])
|
|
113
|
|
114 # text
|
|
115 p1=w2c(*f.center())
|
|
116 c.coords(self.txt,*p1)
|
|
117
|
|
118 def makeobjs(self):
|
|
119 """(re)create the canvas objs (null coords) and make their bindings"""
|
0
|
120 c=self.canvas
|
|
121 f=self.field
|
|
122 c.delete(self.tags)
|
|
123
|
|
124 w2c=self.canvas.world2canvas
|
|
125
|
152
|
126 # make rings
|
|
127 self.rings={} # rad,canvasobj
|
|
128 for intens,color in (#(1,'white'),
|
0
|
129 (.8,'gray90'),(.6,'gray80'),(.4,'gray60'),(.2,'gray50'),
|
|
130 (0,'#000080')):
|
152
|
131 self.rings[intens]=c.create_oval(0,0,0,0,
|
|
132 outline=color,width=2,tags=self.tags,
|
|
133 outlinestipple='gray50')
|
|
134
|
|
135 # make text
|
|
136 self.txt=c.create_text(0,0,text=f.name(),font="arial 10 bold",
|
|
137 fill='white',anchor='c',
|
|
138 tags=self.tags)
|
|
139
|
|
140 # highlight text bindings
|
|
141 canvashighlighter(c,self.txt,'fill',normalval='white',highlightval='red')
|
|
142
|
|
143 # position drag bindings
|
|
144 def press(ev):
|
|
145 self._lastmouse=ev.x,ev.y
|
|
146 def motion(ev):
|
|
147 dcan=Pair(*[a-b for a,b in zip((ev.x,ev.y),self._lastmouse)])
|
|
148 dworld=c.canvas2world_vector(*dcan)
|
|
149 self.field.center(*(self.field.center()+dworld))
|
|
150 self._lastmouse=ev.x,ev.y
|
|
151 dispatcher.send("field coord changed")
|
|
152 self.setcoords() # redraw
|
|
153 def release(ev):
|
|
154 del self._lastmouse
|
|
155 c.tag_bind(self.txt,"<ButtonPress-1>",press)
|
|
156 c.tag_bind(self.txt,"<B1-Motion>",motion)
|
|
157 c.tag_bind(self.txt,"<B1-ButtonRelease>",release)
|
|
158
|
|
159 # radius drag bindings
|
|
160 outerring=self.rings[0]
|
|
161 canvashighlighter(c,outerring,
|
|
162 'outline',normalval='#000080',highlightval='#4040ff')
|
|
163 def motion(ev):
|
|
164 worldmouse=self.canvas.canvas2world(ev.x,ev.y)
|
|
165 currentdist=pairdist(worldmouse,self.field.center())
|
|
166 self.field.falloff(currentdist)
|
|
167 dispatcher.send("field coord changed")
|
|
168 self.setcoords()
|
|
169 c.tag_bind(outerring,"<B1-Motion>",motion)
|
|
170
|
|
171 self.setcoords()
|
|
172
|
0
|
173 class Tracker(tk.Frame):
|
|
174
|
|
175 """whole tracker widget, which is mostly a view for a
|
152
|
176 Fieldset. tracker makes its own fieldset"""
|
0
|
177
|
|
178 # world coords of the visible canvas (preserved even in window resizes)
|
|
179 xmin=0
|
|
180 xmax=100
|
|
181 ymin=0
|
|
182 ymax=100
|
152
|
183
|
|
184 fieldsetfile=None
|
|
185 displays=None # Field : FieldDisplay. we keep these in sync with the fieldset
|
0
|
186
|
|
187 def __init__(self,master):
|
|
188 tk.Frame.__init__(self,master)
|
|
189
|
152
|
190 self.displays={}
|
0
|
191
|
152
|
192 c=self.canvas=Zooming(self,bg='black',closeenough=5)
|
0
|
193 c.pack(fill='both',exp=1)
|
|
194
|
152
|
195 # preserve edge coords over window resize
|
0
|
196 c.bind("<Configure>",self.configcoords)
|
|
197
|
152
|
198 c.bind("<Motion>",
|
|
199 lambda ev: self._fieldset().report(*c.canvas2world(ev.x,ev.y)))
|
|
200 def save(ev):
|
|
201 print "saving"
|
|
202 self.fieldsetfile.save()
|
|
203 master.bind("<Key-s>",save)
|
|
204 dispatcher.connect(self.autobounds,"field coord changed")
|
0
|
205
|
152
|
206 def _fieldset(self):
|
|
207 return self.fieldsetfile.fieldset()
|
0
|
208
|
152
|
209 def load(self,filename):
|
|
210 self.fieldsetfile=Fieldsetfile(filename)
|
|
211 self.displays.clear()
|
|
212 for f in self.fieldsetfile.fieldset().getall():
|
|
213 self.displays[f]=FieldDisplay(self.canvas,f)
|
|
214 self.displays[f].makeobjs()
|
|
215 self.autobounds()
|
0
|
216
|
|
217 def configcoords(self,*args):
|
152
|
218 # force our canvas coords to stay at the edges of the window
|
0
|
219 c=self.canvas
|
|
220 cornerx,cornery=c.canvas2world(0,0)
|
|
221 c.move(cornerx-self.xmin,
|
|
222 cornery-self.ymin)
|
|
223 c.setscale(0,0,
|
|
224 c.winfo_width()/(self.xmax-self.xmin),
|
|
225 c.winfo_height()/(self.ymax-self.ymin))
|
|
226
|
|
227 def autobounds(self):
|
152
|
228 """figure out our bounds from the fieldset, and adjust the display zooms.
|
|
229 writes the corner coords onto the canvas."""
|
|
230 self.xmin,self.xmax,self.ymin,self.ymax=self._fieldset().getbounds()
|
0
|
231
|
152
|
232 self.configcoords()
|
|
233
|
|
234 c=self.canvas
|
|
235 c.delete('cornercoords')
|
|
236 for x,anc2 in ((self.xmin,'w'),(self.xmax,'e')):
|
|
237 for y,anc1 in ((self.ymin,'n'),(self.ymax,'s')):
|
|
238 pos=c.world2canvas(x,y)
|
|
239 c.create_text(pos[0],pos[1],text="%s,%s"%(x,y),
|
|
240 fill='white',anchor=anc1+anc2,
|
|
241 tags='cornercoords')
|
|
242 [d.setcoords() for d in self.displays.values()]
|
0
|
243
|
152
|
244 ########################################################################
|
|
245 ########################################################################
|
|
246
|
0
|
247 root=tk.Tk()
|
150
|
248
|
0
|
249 tra=Tracker(root)
|
|
250 tra.pack(fill='both',exp=1)
|
|
251
|
152
|
252 tra.load("fieldsets/demo")
|
0
|
253
|
|
254 root.mainloop()
|