WorldViz User Forum

WorldViz User Forum (https://forum.worldviz.com/index.php)
-   Vizard (https://forum.worldviz.com/forumdisplay.php?f=17)
-   -   Simple Joystick Question (https://forum.worldviz.com/showthread.php?t=2767)

4711 06-04-2010 05:32 AM

Simple Joystick Question
 
Hi all,

I have a question concerning the joystick buttons which is probably rather easy to be answered: How can I make my script to wait for the user to press one of the two buttons of a connected joystick.

My guess was:
Code:

import viz
import vizjoy
import viztask

viz.go()

joystick = vizjoy.add()

def WaitForButtonPress():
        yield viztask.waitButtonDown(None)
        print "Done."
        viz.quit()
       
viztask.schedule( WaitForButtonPress() )

It works fine if one replaces the "Button" by "Key" in the yield-expression, so I would have expected it to run for the joystick buttons as well. However, it does not, so what works?

Jeff 06-04-2010 12:50 PM

The viztask.waitButtonDown command waits for a GUI button object to be pressed. To wait for a joystick button press in your task function you can create a custom condition. The task function in the following example waits for either buttons 1 or 2 to be pressed before it continues:
Code:

import viz
import viztask
import vizjoy

viz.go()
joystick = vizjoy.add()

def buttonTask():
       
        d = viz.Data()
        waitButton1 = waitJoyButtonDown(joystick,1)
        waitButton2 = waitJoyButtonDown(joystick,2)
       
        yield viztask.waitAny([waitButton1,waitButton2],d)
       
        if d.condition is waitButton1:
                print 'button 1 was pressed'
        else:
                print 'button 2 was pressed'
               
        viz.quit()       

class waitJoyButtonDown( viztask.Condition ):
    def __init__( self, joy, button ):
        self._joy = joy
        self._button = button
 
    def update( self ):
        return self._joy.isButtonDown(self._button)

viztask.schedule( buttonTask() )


4711 06-07-2010 07:06 AM

Hi Jeff,

thanks for the clarification on the viztask.waitButtonDown command and also for your code which works fine for me! :)


All times are GMT -7. The time now is 02:00 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC