Attached is a zip file of which the following is the final, test portion.
To run the complete program you need the latest version of VPython (which
implements full mouse control).
The test routine is shown below. Establish a normal VPython graphics window
with a red cube in it. Then create a "controls" window named "c" and create
two buttons and two sliders in it. The "actions" defined for the buttons
and sliders are functions (def's) to be executed when a button comes up or
when a slider is dragged. In the while loop, c.interact() checks for mouse
interactions on buttons or sliders, updates their appearances, and performs
the actions specified. The slider "value" attribute gives the
floating-point location along the slider, whose default min and max are
0-100. The effect is that you can control the speed and direction of the
cube's rotation. Just for fun, the sliders are coupled together: changing
one changes the other. The coordinate system for setting up the buttons and
sliders has the origin in the lower left corner, and the upper right corner
is (100,100).
The point of the exercise is to seek your comments on this proposed module
for a VPython-based set of controls. The actual appearances of the buttons
and sliders can obviously be improved. The key issues are whether this
"interact" scheme looks good, useful, and easy to teach and use.
Bruce Sherwood
--------------------------------------------------
from visual import *
display(x=300, y=0, range=3, forward=-vector(0,1,1), newzoom=1)
cube = box(color=color.red)
c = controls()
bl = button(y=60, text='Left', action='setdir(1.)')
br = button(x=50, y=60, text='Right', action='setdir(-1.)')
hs = hslider(x=40, y=20, width=10, length=60, action='hsetrate()')
vs = vslider(x=20, y=0, width=10, length=50, action='vsetrate()')
def setdir(direction): # called on button up events
cube.dir = direction
def setrate(value):
cube.dtheta = 2.*value*pi/1e4
def hsetrate(): # called on hslider drag events
setrate(hs.value) # hs.value is 0-100 slider position
vs.value = hs.value # demonstrate making both sliders read alike
def vsetrate(): # called on vslider drag events
setrate(vs.value) # vs.value is 0-100 slider position
hs.value = vs.value # demonstrate making both sliders read alike
hs.value = 70. # mimic dragging the slider
hsetrate()
setdir(-1.)
while 1:
rate(100)
c.interact() # check for events, drive actions
cube.rotate(axis=(0,1,0), angle=cube.dir*cube.dtheta)
|