DirectGUI button - make it work while held down

I’m adding a DirectGUI button that will allow me to rotate a model in a certain direction.

It works fine, but as it’s just calling a method when clicked , it requires the user to click over and over to keep the model rotating.

How can I make the method continue to be called while the button is held down?

You could put the model rotation logic in a task, define a command that adds the task to the taskMgr and define another command that removes it. Then you can bind the former command to the DGG.B1PRESS event of the DirectButton and the latter to its DGG.B1RELEASE event, something like this:

class MyApp(ShowBase):

    def __init__(self):

        ShowBase.__init__(self)

        def startTask(*args):
          self.taskMgr.add(self.rotateModel, "rotate_model")
        def stopTask(*args):
          self.taskMgr.remove("rotate_model")

        button = DirectButton(frameSize=(-2.5,2.5,-0.5,1), pos=(-0.8,0,-0.65),
                              text="Rotate", scale=.1, rolloverSound=None,
                              clickSound=None, pressEffect=0)
        button.bind(DGG.B1PRESS, command=startTask)
        button.bind(DGG.B1RELEASE, command=stopTask)

    def rotateModel(self, task):
        """ This task rotates a model """
        # insert code here
        return task.cont

Now when you press the button the model should start rotating until you release the button.

Hope this helps :slight_smile: .

Thx! I had figured out it was probably a task I needed, but wasn’t clear on the bind steps .

I’ll give it a try!