Changeset - f0e1dde35aec
[Not reviewed]
default
0 1 0
drewp - 22 years ago 2002-07-07 08:30:56

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)
1 file changed with 15 insertions and 17 deletions:
0 comments (0 inline, 0 general)
Widgets/FlyingFader.py
Show inline comments
 
@@ -6,16 +6,14 @@ class Mass:
 
    def __init__(self):
 
        self.x=0 # position
 
        self.xgoal=0 # position goal
 
        
 
        self.v=0 # velocity
 
        self.maxspeed = .8 # maximum speed, in position/second
 

	
 
        self.maxaccel = 3 # maximum acceleration, in position/second^2
 

	
 
        self.eps = .01 # epsilon - numbers within this much are considered the same
 
        self.eps = .03 # epsilon - numbers within this much are considered the same
 

	
 
        self._lastupdate=time()
 
        self._stopped=1
 

	
 
    def equal(self,a,b):
 
        return abs(a-b)<self.eps
 
@@ -42,16 +40,20 @@ class Mass:
 
            self.stop()
 
            return
 
        
 
        self._stopped=0
 
        dir = (-1.0,1,0)[self.xgoal>self.x]
 

	
 
        if abs(self.xgoal-self.x) < abs(self.v*5*dt):
 
            # apply the brakes on the last 5 steps
 
            dir *= -.5
 

	
 
        self.v += dir*self.maxaccel*dt # velocity changes with acceleration in the right direction
 
        self.v = min(max(self.v,-self.maxspeed),self.maxspeed) # clamp velocity
 

	
 
        print "x=%+.03f v=%+.03f a=%+.03f %f" % (self.x,self.v,self.maxaccel,self.xgoal)
 
        #print "x=%+.03f v=%+.03f a=%+.03f %f" % (self.x,self.v,self.maxaccel,self.xgoal)
 

	
 
    def goto(self,newx):
 
        self.xgoal=newx
 

	
 
    def ismoving(self):
 
        return not self._stopped
 
@@ -105,33 +107,37 @@ class FlyingFader(Frame):
 

	
 
    def ismoving(self):
 
        return self.fadevel!=0 or self.fadeacc!=0
 

	
 
    def newfade(self, newlevel, evt=None, length=None):
 

	
 
        # these are currently unused-- Mass needs to accept a speed input
 
        mult = 1
 

	
 
        if evt.state & 8 and evt.state & 4: mult = 0.25 # both
 
        elif evt.state & 8: mult = 0.5 # alt
 
        elif evt.state & 4: mult = 2   # control
 

	
 

	
 
        self.mass.x = self.variable.get()
 
        self.mass.goto(newlevel)
 

	
 
        self.scale['troughcolor'] = 'red'
 

	
 
        self.gofade()
 

	
 
    def gofade(self):
 
        self.mass.update()
 
        self.variable.set(self.mass.x)
 

	
 

	
 
        if not self.mass.ismoving():
 
            self.scale['troughcolor'] = self.oldtrough
 
            return
 
        
 
        # blink the trough while the thing's moving
 
        if time()%.4>.2:
 
            self.scale.config(troughcolor=self.oldtrough)
 
        else:
 
            self.scale.config(troughcolor='white')
 

	
 
#        colorfade(self.scale, percent)
 
        self.after(30, self.gofade)
 

	
 
    def updatelabel(self, *args):
 
        self.vlabel['text'] = "%.3f" % self.variable.get()
 
@@ -153,22 +159,14 @@ def colorfade(scale, lev):
 
    low = (255, 255, 255)
 
    high = (0, 0, 0)
 
    out = [int(l+lev*(h-l)) for h, l in zip(high,low)]
 
    col="#%02X%02X%02X" % tuple(out)
 
    scale.config(troughcolor=col)
 

	
 

	
 
if __name__ == '__main__':
 

	
 

	
 
#    m=Mass()
 
#    m.goto(3)
 
#    while 1:
 
#        m.update()
 
#        print "%.03f %.03f" % (m.x, m.v)
 
#        sleep(.02)
 
    
 
    root = Tk()
 
    root.tk_focusFollowsMouse()
 

	
 
    FlyingFader(root, variable=DoubleVar(), label="suck").pack(side=LEFT, 
 
        expand=1, fill=BOTH)
 
    FlyingFader(root, variable=DoubleVar(), label="moof").pack(side=LEFT,
0 comments (0 inline, 0 general)