annotate Widgets/FlyingFader.py @ 107:47bda76f5236

backed up backuper backed up backuper panels and Lightboard: "What's up?" feature: list subs that are on Lightboard: new incorporator algorithm that is 0-5x faster (usually 2x)
author dmcc
date Sun, 21 Jul 2002 07:20:33 +0000
parents 15ead14b4dd1
children 0c619695d6c6
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
52
065896b0913c emergency commit
dmcc
parents: 50
diff changeset
1 from Tix import *
24
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
2 from time import time,sleep
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
3 from __future__ import division
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
4
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
5 class Mass:
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
6 def __init__(self):
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
7 self.x=0 # position
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
8 self.xgoal=0 # position goal
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
9
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
10 self.v=0 # velocity
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
11 self.maxspeed = .8 # maximum speed, in position/second
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
12 self.maxaccel = 3 # maximum acceleration, in position/second^2
25
f0e1dde35aec better brakes and numerics (oscillations are still possible if the updates get
drewp
parents: 24
diff changeset
13 self.eps = .03 # epsilon - numbers within this much are considered the same
24
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
14
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
15 self._lastupdate=time()
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
16 self._stopped=1
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
17
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
18 def equal(self,a,b):
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
19 return abs(a-b)<self.eps
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
20
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
21 def stop(self):
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
22 self.v=0
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
23 self.xgoal=self.x
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
24 self._stopped=1
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
25
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
26 def update(self):
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
27 t0 = self._lastupdate
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
28 tnow = time()
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
29 self._lastupdate = tnow
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
30
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
31 dt = tnow-t0
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
32
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
33 self.x += self.v*dt
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
34 # hitting the ends stops the slider
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
35 if self.x>1: self.v=max(self.v,0); self.x=1
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
36 if self.x<0: self.v=min(self.v,0); self.x=0
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
37
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
38 if self.equal(self.x,self.xgoal):
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
39 self.x=self.xgoal # clean up value
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
40 self.stop()
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
41 return
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
42
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
43 self._stopped=0
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
44 dir = (-1.0,1,0)[self.xgoal>self.x]
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
45
25
f0e1dde35aec better brakes and numerics (oscillations are still possible if the updates get
drewp
parents: 24
diff changeset
46 if abs(self.xgoal-self.x) < abs(self.v*5*dt):
f0e1dde35aec better brakes and numerics (oscillations are still possible if the updates get
drewp
parents: 24
diff changeset
47 # apply the brakes on the last 5 steps
f0e1dde35aec better brakes and numerics (oscillations are still possible if the updates get
drewp
parents: 24
diff changeset
48 dir *= -.5
f0e1dde35aec better brakes and numerics (oscillations are still possible if the updates get
drewp
parents: 24
diff changeset
49
24
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
50 self.v += dir*self.maxaccel*dt # velocity changes with acceleration in the right direction
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
51 self.v = min(max(self.v,-self.maxspeed),self.maxspeed) # clamp velocity
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
52
25
f0e1dde35aec better brakes and numerics (oscillations are still possible if the updates get
drewp
parents: 24
diff changeset
53 #print "x=%+.03f v=%+.03f a=%+.03f %f" % (self.x,self.v,self.maxaccel,self.xgoal)
24
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
54
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
55 def goto(self,newx):
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
56 self.xgoal=newx
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
57
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
58 def ismoving(self):
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
59 return not self._stopped
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
60
45b12307c695 Initial revision
drewp
parents:
diff changeset
61 class FlyingFader(Frame):
31
c79d4df9d982 now they're orientable
drewp
parents: 27
diff changeset
62 def __init__(self, master, variable, label, fadedur=1.5, font=('Arial', 8), labelwidth=12,
15
c76b62eccdec put **kw back in, they now go to the scale and can override the defaults.
dmcc
parents: 11
diff changeset
63 **kw):
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
64 Frame.__init__(self, master)
45b12307c695 Initial revision
drewp
parents:
diff changeset
65 self.name = label
45b12307c695 Initial revision
drewp
parents:
diff changeset
66 self.variable = variable
45b12307c695 Initial revision
drewp
parents:
diff changeset
67
24
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
68 self.mass = Mass()
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
69
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
70 self.config({'bd':1, 'relief':'raised'})
22
626aa2179630 some naming and defaults tweaks
drewp
parents: 20
diff changeset
71 scaleopts = {'variable' : variable, 'showvalue' : 0, 'from' : 1.0,
31
c79d4df9d982 now they're orientable
drewp
parents: 27
diff changeset
72 'to' : 0, 'res' : 0.001, 'width' : 20, 'length' : 200, 'orient':'vert'}
15
c76b62eccdec put **kw back in, they now go to the scale and can override the defaults.
dmcc
parents: 11
diff changeset
73 scaleopts.update(kw)
31
c79d4df9d982 now they're orientable
drewp
parents: 27
diff changeset
74 if scaleopts['orient']=='vert':
c79d4df9d982 now they're orientable
drewp
parents: 27
diff changeset
75 side1=TOP
c79d4df9d982 now they're orientable
drewp
parents: 27
diff changeset
76 side2=BOTTOM
c79d4df9d982 now they're orientable
drewp
parents: 27
diff changeset
77 else:
c79d4df9d982 now they're orientable
drewp
parents: 27
diff changeset
78 side1=RIGHT
c79d4df9d982 now they're orientable
drewp
parents: 27
diff changeset
79 side2=LEFT
15
c76b62eccdec put **kw back in, they now go to the scale and can override the defaults.
dmcc
parents: 11
diff changeset
80
c76b62eccdec put **kw back in, they now go to the scale and can override the defaults.
dmcc
parents: 11
diff changeset
81 self.scale = Scale(self, scaleopts)
22
626aa2179630 some naming and defaults tweaks
drewp
parents: 20
diff changeset
82 self.vlabel = Label(self, text="0.0", width=6, font=font)
31
c79d4df9d982 now they're orientable
drewp
parents: 27
diff changeset
83 self.label = Label(self, text=label, font=font, anchor='w',width=labelwidth) #wraplength=40, )
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
84
45b12307c695 Initial revision
drewp
parents:
diff changeset
85 self.oldtrough = self.scale['troughcolor']
45b12307c695 Initial revision
drewp
parents:
diff changeset
86
31
c79d4df9d982 now they're orientable
drewp
parents: 27
diff changeset
87 self.scale.pack(side=side2, expand=1, fill=BOTH, anchor='c')
c79d4df9d982 now they're orientable
drewp
parents: 27
diff changeset
88 self.vlabel.pack(side=side2, expand=0, fill=X)
c79d4df9d982 now they're orientable
drewp
parents: 27
diff changeset
89 self.label.pack(side=side2, expand=0, fill=X)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
90
45b12307c695 Initial revision
drewp
parents:
diff changeset
91 for k in range(1, 10):
45b12307c695 Initial revision
drewp
parents:
diff changeset
92 self.scale.bind("<Key-%d>" % k,
20
6eafd86930b5 fix precision bug
dmcc
parents: 18
diff changeset
93 lambda evt, k=k: self.newfade(k / 10.0, evt))
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
94
24
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
95 self.scale.bind("<Key-0>", lambda evt: self.newfade(1.0, evt))
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
96 self.scale.bind("<grave>", lambda evt: self.newfade(0, evt))
45b12307c695 Initial revision
drewp
parents:
diff changeset
97
45b12307c695 Initial revision
drewp
parents:
diff changeset
98 self.scale.bind("<1>", self.cancelfade)
11
c65119b66b00 - no more kw args -- they didn't do anything. use configure to change
dmcc
parents: 9
diff changeset
99 self.scale.bind("<2>", self.cancelfade)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
100 self.scale.bind("<3>", self.mousefade)
45b12307c695 Initial revision
drewp
parents:
diff changeset
101
50
5ff08b489693 fixed lingering trace bug
drewp
parents: 31
diff changeset
102 self.trace_ret = self.variable.trace('w', self.updatelabel)
5ff08b489693 fixed lingering trace bug
drewp
parents: 31
diff changeset
103 self.bind("<Destroy>",self.ondestroy)
5ff08b489693 fixed lingering trace bug
drewp
parents: 31
diff changeset
104
5ff08b489693 fixed lingering trace bug
drewp
parents: 31
diff changeset
105 def ondestroy(self,*ev):
5ff08b489693 fixed lingering trace bug
drewp
parents: 31
diff changeset
106 self.variable.trace_vdelete('w',self.trace_ret)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
107
45b12307c695 Initial revision
drewp
parents:
diff changeset
108 def cancelfade(self, evt):
24
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
109 self.fadegoal = self.variable.get()
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
110 self.fadevel = self.fadeacc = 0
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
111
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
112 self.scale['troughcolor'] = self.oldtrough
45b12307c695 Initial revision
drewp
parents:
diff changeset
113
45b12307c695 Initial revision
drewp
parents:
diff changeset
114 def mousefade(self, evt):
45b12307c695 Initial revision
drewp
parents:
diff changeset
115 target = float(self.tk.call(self.scale, 'get', evt.x, evt.y))
45b12307c695 Initial revision
drewp
parents:
diff changeset
116 self.newfade(target, evt)
45b12307c695 Initial revision
drewp
parents:
diff changeset
117
24
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
118 def ismoving(self):
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
119 return self.fadevel!=0 or self.fadeacc!=0
22
626aa2179630 some naming and defaults tweaks
drewp
parents: 20
diff changeset
120
9
342f7d1c7561 The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents: 0
diff changeset
121 def newfade(self, newlevel, evt=None, length=None):
24
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
122
25
f0e1dde35aec better brakes and numerics (oscillations are still possible if the updates get
drewp
parents: 24
diff changeset
123 # these are currently unused-- Mass needs to accept a speed input
9
342f7d1c7561 The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents: 0
diff changeset
124 mult = 1
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
125 if evt.state & 8 and evt.state & 4: mult = 0.25 # both
45b12307c695 Initial revision
drewp
parents:
diff changeset
126 elif evt.state & 8: mult = 0.5 # alt
45b12307c695 Initial revision
drewp
parents:
diff changeset
127 elif evt.state & 4: mult = 2 # control
45b12307c695 Initial revision
drewp
parents:
diff changeset
128
25
f0e1dde35aec better brakes and numerics (oscillations are still possible if the updates get
drewp
parents: 24
diff changeset
129
24
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
130 self.mass.x = self.variable.get()
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
131 self.mass.goto(newlevel)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
132
45b12307c695 Initial revision
drewp
parents:
diff changeset
133 self.gofade()
45b12307c695 Initial revision
drewp
parents:
diff changeset
134
45b12307c695 Initial revision
drewp
parents:
diff changeset
135 def gofade(self):
24
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
136 self.mass.update()
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
137 self.variable.set(self.mass.x)
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
138
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
139 if not self.mass.ismoving():
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
140 self.scale['troughcolor'] = self.oldtrough
45b12307c695 Initial revision
drewp
parents:
diff changeset
141 return
25
f0e1dde35aec better brakes and numerics (oscillations are still possible if the updates get
drewp
parents: 24
diff changeset
142
f0e1dde35aec better brakes and numerics (oscillations are still possible if the updates get
drewp
parents: 24
diff changeset
143 # blink the trough while the thing's moving
f0e1dde35aec better brakes and numerics (oscillations are still possible if the updates get
drewp
parents: 24
diff changeset
144 if time()%.4>.2:
27
bee0862f4436 Construction colors
dmcc
parents: 25
diff changeset
145 # self.scale.config(troughcolor=self.oldtrough)
bee0862f4436 Construction colors
dmcc
parents: 25
diff changeset
146 self.scale.config(troughcolor='orange')
25
f0e1dde35aec better brakes and numerics (oscillations are still possible if the updates get
drewp
parents: 24
diff changeset
147 else:
27
bee0862f4436 Construction colors
dmcc
parents: 25
diff changeset
148 # self.scale.config(troughcolor='white')
bee0862f4436 Construction colors
dmcc
parents: 25
diff changeset
149 self.scale.config(troughcolor='yellow')
24
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
150
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
151 # colorfade(self.scale, percent)
22
626aa2179630 some naming and defaults tweaks
drewp
parents: 20
diff changeset
152 self.after(30, self.gofade)
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
153
45b12307c695 Initial revision
drewp
parents:
diff changeset
154 def updatelabel(self, *args):
52
065896b0913c emergency commit
dmcc
parents: 50
diff changeset
155 if self.variable:
065896b0913c emergency commit
dmcc
parents: 50
diff changeset
156 self.vlabel['text'] = "%.3f" % self.variable.get()
24
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
157 # if self.fadetimes[1] == 0: # no fade
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
158 # self.vlabel['fg'] = 'black'
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
159 # elif self.curfade[1] > self.curfade[0]:
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
160 # self.vlabel['fg'] = 'red'
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
161 # else:
548d3aa2660f physics model fader!
drewp
parents: 22
diff changeset
162 # self.vlabel['fg'] = 'blue'
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
163
11
c65119b66b00 - no more kw args -- they didn't do anything. use configure to change
dmcc
parents: 9
diff changeset
164 def get(self):
c65119b66b00 - no more kw args -- they didn't do anything. use configure to change
dmcc
parents: 9
diff changeset
165 return self.scale.get()
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
166
11
c65119b66b00 - no more kw args -- they didn't do anything. use configure to change
dmcc
parents: 9
diff changeset
167 def set(self, val):
c65119b66b00 - no more kw args -- they didn't do anything. use configure to change
dmcc
parents: 9
diff changeset
168 self.scale.set(val)
c65119b66b00 - no more kw args -- they didn't do anything. use configure to change
dmcc
parents: 9
diff changeset
169
c65119b66b00 - no more kw args -- they didn't do anything. use configure to change
dmcc
parents: 9
diff changeset
170 def colorfade(scale, lev):
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
171 low = (255, 255, 255)
45b12307c695 Initial revision
drewp
parents:
diff changeset
172 high = (0, 0, 0)
11
c65119b66b00 - no more kw args -- they didn't do anything. use configure to change
dmcc
parents: 9
diff changeset
173 out = [int(l+lev*(h-l)) for h, l in zip(high,low)]
0
45b12307c695 Initial revision
drewp
parents:
diff changeset
174 col="#%02X%02X%02X" % tuple(out)
45b12307c695 Initial revision
drewp
parents:
diff changeset
175 scale.config(troughcolor=col)
45b12307c695 Initial revision
drewp
parents:
diff changeset
176
45b12307c695 Initial revision
drewp
parents:
diff changeset
177 if __name__ == '__main__':
45b12307c695 Initial revision
drewp
parents:
diff changeset
178 root = Tk()
45b12307c695 Initial revision
drewp
parents:
diff changeset
179 root.tk_focusFollowsMouse()
45b12307c695 Initial revision
drewp
parents:
diff changeset
180
45b12307c695 Initial revision
drewp
parents:
diff changeset
181 FlyingFader(root, variable=DoubleVar(), label="suck").pack(side=LEFT,
45b12307c695 Initial revision
drewp
parents:
diff changeset
182 expand=1, fill=BOTH)
45b12307c695 Initial revision
drewp
parents:
diff changeset
183 FlyingFader(root, variable=DoubleVar(), label="moof").pack(side=LEFT,
45b12307c695 Initial revision
drewp
parents:
diff changeset
184 expand=1, fill=BOTH)
45b12307c695 Initial revision
drewp
parents:
diff changeset
185 FlyingFader(root, variable=DoubleVar(), label="zarf").pack(side=LEFT,
45b12307c695 Initial revision
drewp
parents:
diff changeset
186 expand=1, fill=BOTH)
45b12307c695 Initial revision
drewp
parents:
diff changeset
187 FlyingFader(root, variable=DoubleVar(),
45b12307c695 Initial revision
drewp
parents:
diff changeset
188 label="long name goes here. got it?").pack(side=LEFT, expand=1,
45b12307c695 Initial revision
drewp
parents:
diff changeset
189 fill=BOTH)
45b12307c695 Initial revision
drewp
parents:
diff changeset
190
45b12307c695 Initial revision
drewp
parents:
diff changeset
191 root.mainloop()