DirectSlider bug?

I want to have some directslider controls which go from -5 to 5.

I’ve noticed when the starting value is negative (for example, -1) - the slider value is set to 0. For positive numbers it works fine.

Try this quick program and set v to -1 and you’ll get 0, set it positive and you’ll see the number you set. Is this a bug?

from direct.gui.DirectGui import *
from direct.showbase.ShowBase import ShowBase

class MyApp(ShowBase):
	def __init__(self):
		ShowBase.__init__(self) 
		v=-1
		self.x=DirectSlider(range=(-5,5), value=v, pageSize=1, command=self.showx,
			thumb_frameSize=(0,.1,.03,-.03), thumb_frameColor=(1,1,0,1), 
			relief=DGG.RAISED, frameSize=(0,.8,.06,-.06), frameColor= (1,0,0,1))

	def showx(self):
		print int(self.x["value"])

app = MyApp()
app.run()

Hmm, could be an ordering issue when the range and value are both set as command-line arguments. Try setting the value in the line following, with this:

self.x['value'] = -1

David

Are you saying it’s better to not set both range and value in the call to DirectSlider?

I tried adding the line you suggested and it did seem to resolve, but it still doesn’t work everytime. I actually have 5 sliders at once , reading values from a file. I double-checked the file and the values are right there, it’s just on display they seem to not work quite right.

I will look at it some more later to see if I can see any pattern to the “inconsistency”.

OK, finally got back to checking this and I definitely see where it’s failing (and the workaround doesn’t seem to help, either)

Here’s the code:

from direct.gui.DirectGui import *
from direct.showbase.ShowBase import ShowBase

class MyApp(ShowBase):
	def __init__(self):
		ShowBase.__init__(self) 
		v=-2
		self.x=DirectSlider(range=(-5,5), 
			pageSize=1, command=self.showx,
			thumb_frameSize=(0,.1,.03,-.03), thumb_frameColor=(1,1,0,1), 
			relief=DGG.RAISED, frameSize=(0,.8,.06,-.06), frameColor= (1,0,0,1))
		
		self.x["value"] = v  #<_ workaround line

	def showx(self):
		print int(self.x["value"])

app = MyApp()
app.run()

Note that I added the line
self.x[“value”] = v #<_ workaround line

Even with this line, values of -1 and -2 return the wrong values -> 0 and -1 respectively.

I tried it 2 ways when setting “value=v” in the initial call to DirectSlider and without setting it (like in the code above) - no difference - same problem.

BTW- other negative values seem fine.

The above code works fine for me. It prints -2. When I drag the bar left and right, it prints a range from -5 to 5.

David

Fine for me too. P3D 1.7.

Bizarre.
Let me reinstall Panda 1.7 , maybe I screwed up my install somehow.

OK, just for the heck of it I added a line here:

	def showx(self):
		print self.x["value"]
		print int(self.x["value"])

and now I see
-1.99999988079
-1

so, it seems like a rounding problem that I’m having?

Yep, adding :

print self.x["value"]
	        print self.x["value"]
		print int(self.x["value"])
		print int(round(self.x["value"]))

gives me:
-1.99999988079
-1
-2

so I can get -2 as I wanted.
Could there be some setting (python or panda) that I have that made the original code not work for me, though it worked for others?

Oh, that’s a different problem. Sure, that’s just mathematical precision. The slider value is stored as a floating-point value, and floating-point values being what they are, you can never be sure you’ll get back precisely the same value that you put in.

This is especially likely when you are using DirectX on windows, which puts the FPU into single-precision mode.

If you’re expecting an integer value, you can use int(x + 0.5) to round to the nearest integer value, instead of truncating to the lower value.

David