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