Direct Slider

Ok I am having problems with my code. You see I want to add a DirectSlider into the program. I want the DirectSlider to change the scale of the model in the space. But the problem is whenever I cannot figure out how to get the value of the slider in to the function I have defined.

Here is the code.

import direct.directbase.DirectStart
from pandac.PandaModules import Filename
from direct.gui.OnscreenText import OnscreenText 
from direct.gui.DirectGui import *
import sys,os

pathname = os.path.abspath(sys.path[0])
pathname = Filename.fromOsSpecific(pathname).getFullpath()

size = 0.2
sonic = loader.loadModel(pathname + "/Sonic/sonic")
sonic.reparentTo(render);
sonic.setScale(size)
sonic.setPos(0, 20, -5)

base.camera.setPos(0,-10,0)

#add some text
bk_text = "DirectOptionMenu Demo"
textObject = OnscreenText(text = bk_text, pos = (0.85,0.85), 
scale = 0.07,fg=(1,0.5,0.5,1),align=TextNode.ACenter,mayChange=1)

#add some text
output = ""
textObject = OnscreenText(text = output, pos = (0.95,-0.95),
 scale = 0.07,fg=(1,0.5,0.5,1),align=TextNode.ACenter,mayChange=1)

#callback function to set  text 
def itemSel(arg):
	output = "Item Selected is: "+arg
	textObject.setText(output)
	
	size = float(arg)
	sonic.setScale(size)

#create a frame
slider = DirectSlider(scale = 0.2, pos = (0.95, 0, 0), range = (0.1,1), pageSize = 0.05, value = 0.1, command = itemSel)

#run the tutorial
run()

The error says I am not passing any arguments into the function. Yet a similar program using menus and this program has no problems.

Please help

The DirectSlider does not pass any parameters to its command function, so the function you pass in the “command” parameter should not take any parameters (other than self, if it is a method). If you want to get the value of the slider, use slider[‘value’].

David

Cool Thanks for explaining it to me.

Ok here is another nit-picky question. How com I cannot get DirectSlider to render using 2D graphics? If you look at the pos in the declaration above you will see I have to use 3D cordiants. Can anyone tell me how to get DirectSlider to use 2D cordinates?

The DirectGui objects in general all use 3-d coordinates, because Panda uses 3-d coordinates. It’s true that in the 2-d scene graph, the y coordinate doesn’t mean very much, but it must still be supplied. This is because the DirectGui objects directly inherit from NodePath, and they therefore inherit the 3-component setPos() method of NodePath.

One could argue that the real problem here is OnscreenText, which presents an inconsistent interface from the rest of Panda by accepting a 2-d screen position and then implicitly supplying the y coordinate. :slight_smile:

David