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