comparison Widgets/FlyingFader.py @ 25:f0e1dde35aec

better brakes and numerics (oscillations are still possible if the updates get better brakes and numerics (oscillations are still possible if the updates get slow, like when you're moving a lot of faders)
author drewp
date Sun, 07 Jul 2002 08:30:56 +0000
parents 548d3aa2660f
children bee0862f4436
comparison
equal deleted inserted replaced
24:548d3aa2660f 25:f0e1dde35aec
7 self.x=0 # position 7 self.x=0 # position
8 self.xgoal=0 # position goal 8 self.xgoal=0 # position goal
9 9
10 self.v=0 # velocity 10 self.v=0 # velocity
11 self.maxspeed = .8 # maximum speed, in position/second 11 self.maxspeed = .8 # maximum speed, in position/second
12
13 self.maxaccel = 3 # maximum acceleration, in position/second^2 12 self.maxaccel = 3 # maximum acceleration, in position/second^2
14 13 self.eps = .03 # epsilon - numbers within this much are considered the same
15 self.eps = .01 # epsilon - numbers within this much are considered the same
16 14
17 self._lastupdate=time() 15 self._lastupdate=time()
18 self._stopped=1 16 self._stopped=1
19 17
20 def equal(self,a,b): 18 def equal(self,a,b):
43 return 41 return
44 42
45 self._stopped=0 43 self._stopped=0
46 dir = (-1.0,1,0)[self.xgoal>self.x] 44 dir = (-1.0,1,0)[self.xgoal>self.x]
47 45
46 if abs(self.xgoal-self.x) < abs(self.v*5*dt):
47 # apply the brakes on the last 5 steps
48 dir *= -.5
49
48 self.v += dir*self.maxaccel*dt # velocity changes with acceleration in the right direction 50 self.v += dir*self.maxaccel*dt # velocity changes with acceleration in the right direction
49 self.v = min(max(self.v,-self.maxspeed),self.maxspeed) # clamp velocity 51 self.v = min(max(self.v,-self.maxspeed),self.maxspeed) # clamp velocity
50 52
51 print "x=%+.03f v=%+.03f a=%+.03f %f" % (self.x,self.v,self.maxaccel,self.xgoal) 53 #print "x=%+.03f v=%+.03f a=%+.03f %f" % (self.x,self.v,self.maxaccel,self.xgoal)
52 54
53 def goto(self,newx): 55 def goto(self,newx):
54 self.xgoal=newx 56 self.xgoal=newx
55 57
56 def ismoving(self): 58 def ismoving(self):
106 def ismoving(self): 108 def ismoving(self):
107 return self.fadevel!=0 or self.fadeacc!=0 109 return self.fadevel!=0 or self.fadeacc!=0
108 110
109 def newfade(self, newlevel, evt=None, length=None): 111 def newfade(self, newlevel, evt=None, length=None):
110 112
113 # these are currently unused-- Mass needs to accept a speed input
111 mult = 1 114 mult = 1
112
113 if evt.state & 8 and evt.state & 4: mult = 0.25 # both 115 if evt.state & 8 and evt.state & 4: mult = 0.25 # both
114 elif evt.state & 8: mult = 0.5 # alt 116 elif evt.state & 8: mult = 0.5 # alt
115 elif evt.state & 4: mult = 2 # control 117 elif evt.state & 4: mult = 2 # control
116 118
119
117 self.mass.x = self.variable.get() 120 self.mass.x = self.variable.get()
118 self.mass.goto(newlevel) 121 self.mass.goto(newlevel)
119
120 self.scale['troughcolor'] = 'red'
121 122
122 self.gofade() 123 self.gofade()
123 124
124 def gofade(self): 125 def gofade(self):
125 self.mass.update() 126 self.mass.update()
126 self.variable.set(self.mass.x) 127 self.variable.set(self.mass.x)
127 128
128
129 if not self.mass.ismoving(): 129 if not self.mass.ismoving():
130 self.scale['troughcolor'] = self.oldtrough 130 self.scale['troughcolor'] = self.oldtrough
131 return 131 return
132
133 # blink the trough while the thing's moving
134 if time()%.4>.2:
135 self.scale.config(troughcolor=self.oldtrough)
136 else:
137 self.scale.config(troughcolor='white')
132 138
133 # colorfade(self.scale, percent) 139 # colorfade(self.scale, percent)
134 self.after(30, self.gofade) 140 self.after(30, self.gofade)
135 141
136 def updatelabel(self, *args): 142 def updatelabel(self, *args):
154 high = (0, 0, 0) 160 high = (0, 0, 0)
155 out = [int(l+lev*(h-l)) for h, l in zip(high,low)] 161 out = [int(l+lev*(h-l)) for h, l in zip(high,low)]
156 col="#%02X%02X%02X" % tuple(out) 162 col="#%02X%02X%02X" % tuple(out)
157 scale.config(troughcolor=col) 163 scale.config(troughcolor=col)
158 164
165
159 if __name__ == '__main__': 166 if __name__ == '__main__':
160
161
162 # m=Mass()
163 # m.goto(3)
164 # while 1:
165 # m.update()
166 # print "%.03f %.03f" % (m.x, m.v)
167 # sleep(.02)
168
169 root = Tk() 167 root = Tk()
170 root.tk_focusFollowsMouse() 168 root.tk_focusFollowsMouse()
171 169
172 FlyingFader(root, variable=DoubleVar(), label="suck").pack(side=LEFT, 170 FlyingFader(root, variable=DoubleVar(), label="suck").pack(side=LEFT,
173 expand=1, fill=BOTH) 171 expand=1, fill=BOTH)