Mercurial > code > home > repos > light9
annotate Widgets/FlyingFader.py @ 27:bee0862f4436
Construction colors
author | dmcc |
---|---|
date | Sun, 07 Jul 2002 08:36:21 +0000 |
parents | f0e1dde35aec |
children | c79d4df9d982 |
rev | line source |
---|---|
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
1 from Tkinter import * |
24 | 2 from time import time,sleep |
3 from __future__ import division | |
4 | |
5 class Mass: | |
6 def __init__(self): | |
7 self.x=0 # position | |
8 self.xgoal=0 # position goal | |
9 | |
10 self.v=0 # velocity | |
11 self.maxspeed = .8 # maximum speed, in position/second | |
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 | 14 |
15 self._lastupdate=time() | |
16 self._stopped=1 | |
17 | |
18 def equal(self,a,b): | |
19 return abs(a-b)<self.eps | |
20 | |
21 def stop(self): | |
22 self.v=0 | |
23 self.xgoal=self.x | |
24 self._stopped=1 | |
25 | |
26 def update(self): | |
27 t0 = self._lastupdate | |
28 tnow = time() | |
29 self._lastupdate = tnow | |
30 | |
31 dt = tnow-t0 | |
32 | |
33 self.x += self.v*dt | |
34 # hitting the ends stops the slider | |
35 if self.x>1: self.v=max(self.v,0); self.x=1 | |
36 if self.x<0: self.v=min(self.v,0); self.x=0 | |
37 | |
38 if self.equal(self.x,self.xgoal): | |
39 self.x=self.xgoal # clean up value | |
40 self.stop() | |
41 return | |
42 | |
43 self._stopped=0 | |
44 dir = (-1.0,1,0)[self.xgoal>self.x] | |
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 | 50 self.v += dir*self.maxaccel*dt # velocity changes with acceleration in the right direction |
51 self.v = min(max(self.v,-self.maxspeed),self.maxspeed) # clamp velocity | |
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 | 54 |
55 def goto(self,newx): | |
56 self.xgoal=newx | |
57 | |
58 def ismoving(self): | |
59 return not self._stopped | |
0 | 60 |
61 class FlyingFader(Frame): | |
22 | 62 def __init__(self, master, variable, label, fadedur=1.5, font=('Arial', 8), |
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 | 64 Frame.__init__(self, master) |
65 self.name = label | |
66 self.variable = variable | |
67 | |
24 | 68 self.mass = Mass() |
69 | |
0 | 70 self.config({'bd':1, 'relief':'raised'}) |
22 | 71 scaleopts = {'variable' : variable, 'showvalue' : 0, 'from' : 1.0, |
72 'to' : 0, 'res' : 0.001, 'width' : 20, 'length' : 200} | |
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) |
c76b62eccdec
put **kw back in, they now go to the scale and can override the defaults.
dmcc
parents:
11
diff
changeset
|
74 |
c76b62eccdec
put **kw back in, they now go to the scale and can override the defaults.
dmcc
parents:
11
diff
changeset
|
75 self.scale = Scale(self, scaleopts) |
22 | 76 self.vlabel = Label(self, text="0.0", width=6, font=font) |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
77 self.label = Label(self, text=label, wraplength=40, font=font) |
0 | 78 |
79 self.oldtrough = self.scale['troughcolor'] | |
80 | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
81 self.scale.pack(side=TOP, expand=1, fill=BOTH, anchor='c') |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
82 self.vlabel.pack(side=BOTTOM, expand=0, fill=X) |
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
83 self.label.pack(side=BOTTOM, expand=0, fill=X) |
0 | 84 |
85 for k in range(1, 10): | |
86 self.scale.bind("<Key-%d>" % k, | |
20 | 87 lambda evt, k=k: self.newfade(k / 10.0, evt)) |
0 | 88 |
24 | 89 self.scale.bind("<Key-0>", lambda evt: self.newfade(1.0, evt)) |
0 | 90 self.scale.bind("<grave>", lambda evt: self.newfade(0, evt)) |
91 | |
92 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
|
93 self.scale.bind("<2>", self.cancelfade) |
0 | 94 self.scale.bind("<3>", self.mousefade) |
95 | |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
96 self.variable.trace('w', self.updatelabel) |
0 | 97 |
98 def cancelfade(self, evt): | |
24 | 99 self.fadegoal = self.variable.get() |
100 self.fadevel = self.fadeacc = 0 | |
101 | |
0 | 102 self.scale['troughcolor'] = self.oldtrough |
103 | |
104 def mousefade(self, evt): | |
105 target = float(self.tk.call(self.scale, 'get', evt.x, evt.y)) | |
106 self.newfade(target, evt) | |
107 | |
24 | 108 def ismoving(self): |
109 return self.fadevel!=0 or self.fadeacc!=0 | |
22 | 110 |
9
342f7d1c7561
The FlyingFader will accept keyboard values and fade to them over 1.5
dmcc
parents:
0
diff
changeset
|
111 def newfade(self, newlevel, evt=None, length=None): |
24 | 112 |
25
f0e1dde35aec
better brakes and numerics (oscillations are still possible if the updates get
drewp
parents:
24
diff
changeset
|
113 # 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
|
114 mult = 1 |
0 | 115 if evt.state & 8 and evt.state & 4: mult = 0.25 # both |
116 elif evt.state & 8: mult = 0.5 # alt | |
117 elif evt.state & 4: mult = 2 # control | |
118 | |
25
f0e1dde35aec
better brakes and numerics (oscillations are still possible if the updates get
drewp
parents:
24
diff
changeset
|
119 |
24 | 120 self.mass.x = self.variable.get() |
121 self.mass.goto(newlevel) | |
0 | 122 |
123 self.gofade() | |
124 | |
125 def gofade(self): | |
24 | 126 self.mass.update() |
127 self.variable.set(self.mass.x) | |
128 | |
129 if not self.mass.ismoving(): | |
0 | 130 self.scale['troughcolor'] = self.oldtrough |
131 return | |
25
f0e1dde35aec
better brakes and numerics (oscillations are still possible if the updates get
drewp
parents:
24
diff
changeset
|
132 |
f0e1dde35aec
better brakes and numerics (oscillations are still possible if the updates get
drewp
parents:
24
diff
changeset
|
133 # 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
|
134 if time()%.4>.2: |
27 | 135 # self.scale.config(troughcolor=self.oldtrough) |
136 self.scale.config(troughcolor='orange') | |
25
f0e1dde35aec
better brakes and numerics (oscillations are still possible if the updates get
drewp
parents:
24
diff
changeset
|
137 else: |
27 | 138 # self.scale.config(troughcolor='white') |
139 self.scale.config(troughcolor='yellow') | |
24 | 140 |
141 # colorfade(self.scale, percent) | |
22 | 142 self.after(30, self.gofade) |
0 | 143 |
144 def updatelabel(self, *args): | |
18
053889940418
some changes to allow integration into rsn. more needed
dmcc
parents:
15
diff
changeset
|
145 self.vlabel['text'] = "%.3f" % self.variable.get() |
24 | 146 # if self.fadetimes[1] == 0: # no fade |
147 # self.vlabel['fg'] = 'black' | |
148 # elif self.curfade[1] > self.curfade[0]: | |
149 # self.vlabel['fg'] = 'red' | |
150 # else: | |
151 # self.vlabel['fg'] = 'blue' | |
0 | 152 |
11
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
153 def get(self): |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
154 return self.scale.get() |
0 | 155 |
11
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
156 def set(self, val): |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
157 self.scale.set(val) |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
158 |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
159 |
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
160 def colorfade(scale, lev): |
0 | 161 low = (255, 255, 255) |
162 high = (0, 0, 0) | |
11
c65119b66b00
- no more kw args -- they didn't do anything. use configure to change
dmcc
parents:
9
diff
changeset
|
163 out = [int(l+lev*(h-l)) for h, l in zip(high,low)] |
0 | 164 col="#%02X%02X%02X" % tuple(out) |
165 scale.config(troughcolor=col) | |
166 | |
25
f0e1dde35aec
better brakes and numerics (oscillations are still possible if the updates get
drewp
parents:
24
diff
changeset
|
167 |
0 | 168 if __name__ == '__main__': |
169 root = Tk() | |
170 root.tk_focusFollowsMouse() | |
171 | |
172 FlyingFader(root, variable=DoubleVar(), label="suck").pack(side=LEFT, | |
173 expand=1, fill=BOTH) | |
174 FlyingFader(root, variable=DoubleVar(), label="moof").pack(side=LEFT, | |
175 expand=1, fill=BOTH) | |
176 FlyingFader(root, variable=DoubleVar(), label="zarf").pack(side=LEFT, | |
177 expand=1, fill=BOTH) | |
178 FlyingFader(root, variable=DoubleVar(), | |
179 label="long name goes here. got it?").pack(side=LEFT, expand=1, | |
180 fill=BOTH) | |
181 | |
182 root.mainloop() |