Updates for the Joystick demo to allow it to funciton on Linux where

the min position values are negative.


git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@27266 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Robin Dunn 2004-05-14 02:53:19 +00:00
parent a800dc50d4
commit 3ba55e8ab7

View File

@ -114,8 +114,21 @@ class JoyGauge(wx.Panel):
joyy = float(self.stick.GetPosition().y) joyy = float(self.stick.GetPosition().y)
# Get the joystick range of motion # Get the joystick range of motion
xrange = self.stick.GetXMax() - self.stick.GetXMin() xmin = self.stick.GetXMin()
yrange = self.stick.GetYMax() - self.stick.GetYMin() xmax = self.stick.GetXMax()
if xmin < 0:
xmax += abs(xmin)
joyx += abs(xmin)
xmin = 0
xrange = max(xmax - xmin, 1)
ymin = self.stick.GetYMin()
ymax = self.stick.GetYMax()
if ymin < 0:
ymax += abs(ymin)
joyy += abs(ymin)
ymin = 0
yrange = max(ymax - ymin, 1)
# calc a ratio of our range versus the joystick range # calc a ratio of our range versus the joystick range
xratio = float(edgeSize) / xrange xratio = float(edgeSize) / xrange
@ -123,7 +136,7 @@ class JoyGauge(wx.Panel):
# calc the displayable value based on position times ratio # calc the displayable value based on position times ratio
xval = int(joyx * xratio) xval = int(joyx * xratio)
yval = int(joyy * xratio) yval = int(joyy * yratio)
# and normalize the value from our brush's origin # and normalize the value from our brush's origin
x = xval + xorigin x = xval + xorigin
@ -131,7 +144,7 @@ class JoyGauge(wx.Panel):
# Now to draw it. # Now to draw it.
dc.SetPen(wx.Pen(wx.RED, 2)) dc.SetPen(wx.Pen(wx.RED, 2))
dc.CrossHair((x, y)) dc.CrossHair(x, y)
# Turn off drawing optimization # Turn off drawing optimization
dc.EndDrawing() dc.EndDrawing()
@ -758,10 +771,8 @@ class Axis(wx.Panel):
self.GetMax = eval('stick.Get%sMax' % token) self.GetMax = eval('stick.Get%sMax' % token)
# Create our displays and set them up. # Create our displays and set them up.
self.Min = wx.StaticText(self, -1, str(self.GetMin()), self.Min = wx.StaticText(self, -1, str(self.GetMin()), style=wx.ALIGN_RIGHT)
size=(40,-1), style=wx.ALIGN_RIGHT | wx.ST_NO_AUTORESIZE) self.Max = wx.StaticText(self, -1, str(self.GetMax()), style=wx.ALIGN_LEFT)
self.Max = wx.StaticText(self, -1, str(self.GetMax()),
size=(40,-1), style=wx.ALIGN_LEFT | wx.ST_NO_AUTORESIZE)
self.bar = AxisBar(self) self.bar = AxisBar(self)
sizer.Add(self.Min, 0, wx.ALL | wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL, 1) sizer.Add(self.Min, 0, wx.ALL | wx.ALIGN_RIGHT | wx.ALIGN_CENTER_VERTICAL, 1)
@ -813,20 +824,25 @@ class Axis(wx.Panel):
else: else:
val = eval('self.stick.Get%sPosition()' % self.token) val = eval('self.stick.Get%sPosition()' % self.token)
# #
# While we might be able to rely on a range of 0-FFFFFF on Win, that might # While we might be able to rely on a range of 0-FFFFFF on Win, that might
# not be true of all drivers on all platforms. Thus, calc the actual full # not be true of all drivers on all platforms. Thus, calc the actual full
# range first. # range first.
# #
if min < 0:
max += abs(min)
val += abs(min)
min = 0
range = float(max - min) range = float(max - min)
# #
# The relative value is used by the derived wx.Gauge since it is a # The relative value is used by the derived wx.Gauge since it is a
# positive-only control. # positive-only control.
# #
relative = 0 relative = 0
if range: if range:
relative = int(val / range * 1000) relative = int( val / range * 1000)
# #
# Pass both the raw and relative values to the derived Gauge # Pass both the raw and relative values to the derived Gauge
@ -941,9 +957,9 @@ class JoystickDemoPanel(wx.Panel):
# Capture Joystick events (if they happen) # Capture Joystick events (if they happen)
self.Bind(wx.EVT_JOYSTICK_EVENTS, self.OnJoystick) self.Bind(wx.EVT_JOYSTICK_EVENTS, self.OnJoystick)
self.stick.SetMovementThreshold(10) self.stick.SetMovementThreshold(10)
def Calibrate(self, evt=None): def Calibrate(self, evt=None):
# Do not try this without a stick # Do not try this without a stick
if not self.stick: if not self.stick:
@ -954,6 +970,7 @@ class JoystickDemoPanel(wx.Panel):
self.pov.Calibrate() self.pov.Calibrate()
self.buttons.Calibrate() self.buttons.Calibrate()
def OnJoystick(self, evt=None): def OnJoystick(self, evt=None):
if not self.stick: if not self.stick:
return return
@ -961,9 +978,15 @@ class JoystickDemoPanel(wx.Panel):
self.axes.Update() self.axes.Update()
self.joy.Update() self.joy.Update()
self.pov.Update() self.pov.Update()
self.buttons.Update() if evt is not None and evt.IsButton():
self.buttons.Update()
def ShutdownDemo(self):
if self.stick:
self.stick.ReleaseCapture()
self.stick = None
#---------------------------------------------------------------------------- #----------------------------------------------------------------------------
def runTest(frame, nb, log): def runTest(frame, nb, log):