joystick update

* use SDL events to trigger the sensor, trigger was being forced every tick. removed workaround for this problem.
* added "All Events" option, similar to all keys in the keyboard sensor.
  This means every event from the joystick will trigger the sensor, however only events from the selected type (axis/button/hat) is used to set the positive state of the sensor.
* Added python function sens_joy.GetButtonValues(), returns a list of pressed button indicies.
* Removed pressed/released option for joystick buttons, it was the same as the invert option.
This commit is contained in:
2008-10-07 05:09:24 +00:00
parent 8d73ea72a7
commit 77484d28d5
9 changed files with 198 additions and 174 deletions

View File

@@ -41,7 +41,9 @@
#include "SND_SDLCDDevice.h"
#include "SoundDefines.h"
#ifndef DISABLE_SDL
#include <SDL.h>
#endif
SND_SDLCDDevice::SND_SDLCDDevice() :
m_cdrom(NULL),

View File

@@ -163,12 +163,12 @@ typedef struct bSensor {
typedef struct bJoystickSensor {
char name[32];
short type;
short joyindex;
char type;
char joyindex;
short flag;
int axis;
int axisf;
int button;
int buttonf;
int hat;
int hatf;
int precision;
@@ -252,6 +252,8 @@ typedef struct bJoystickSensor {
#define BL_SENS_MOUSE_MOUSEOVER 16
#define BL_SENS_MOUSE_MOUSEOVER_ANY 32
#define SENS_JOY_ANY_EVENT 1
#define SENS_JOY_BUTTON 0
#define SENS_JOY_BUTTON_PRESSED 0
#define SENS_JOY_BUTTON_RELEASED 1
@@ -266,6 +268,7 @@ typedef struct bJoystickSensor {
#define SENS_JOY_HAT 2
#define SENS_JOY_HAT_DIR 0
#define SENS_DELAY_REPEAT 1
// should match JOYINDEX_MAX in SCA_JoystickDefines.h */
#define SENS_JOY_MAXINDEX 8

View File

@@ -1447,29 +1447,33 @@ static short draw_sensorbuttons(bSensor *sens, uiBlock *block, short xco, short
joy= sens->data;
uiDefButS(block, NUM, 1, "Index:", xco+10, yco-44, 0.6 * (width-120), 19,
uiDefButC(block, NUM, 1, "Index:", xco+10, yco-44, 0.33 * (width-20), 19,
&joy->joyindex, 0, SENS_JOY_MAXINDEX-1, 100, 0,
"Specify which joystick to use");
str= "Type %t|Button %x0|Axis %x1|Hat%x2";
uiDefButS(block, MENU, B_REDR, str, xco+87, yco-44, 0.6 * (width-150), 19,
uiDefButC(block, MENU, B_REDR, str, xco+87, yco-44, 0.26 * (width-20), 19,
&joy->type, 0, 31, 0, 0,
"The type of event this joystick sensor is triggered on.");
uiDefButBitS(block, TOG, SENS_JOY_ANY_EVENT, B_REDR,
(joy->flag & SENS_JOY_ANY_EVENT) ? "All Events" : "All",
xco+10 + 0.5 * (width-20), yco-68, ((joy->flag & SENS_JOY_ANY_EVENT) ? 0.5 : 0.098) * (width-20), 19,
&joy->flag, 0, 0, 0, 0,
"Trigger from all events of the current (axis/button/hat)");
if(joy->type == SENS_JOY_BUTTON)
{
uiDefButI(block, NUM, 1, "Number:", xco+10, yco-68, 0.6 * (width-20), 19,
&joy->button, 0, 18, 100, 0,
"Specify which button to use");
str = "Type %t|Pressed %x0|Released %x1";
uiDefButI(block, MENU, B_REDR, str, xco+10 + 0.6 * (width-20), yco-68, 0.4 * (width-20), 19,
&joy->buttonf, 2.0, 31, 0, 0,
"Button pressed or released.");
if ((joy->flag & SENS_JOY_ANY_EVENT)==0) {
uiDefButI(block, NUM, 1, "Number:", xco+10 + 0.6 * (width-20), yco-68, 0.4 * (width-20), 19,
&joy->button, 0, 18, 100, 0,
"Specify which button to use");
}
}
else if(joy->type == SENS_JOY_AXIS)
{
uiDefButI(block, NUM, 1, "Number:", xco+10, yco-68, 0.6 * (width-20), 19,
uiDefButI(block, NUM, 1, "Number:", xco+10, yco-68, 0.46 * (width-20), 19,
&joy->axis, 1, 2.0, 100, 0,
"Specify which axis to use");
@@ -1477,20 +1481,24 @@ static short draw_sensorbuttons(bSensor *sens, uiBlock *block, short xco, short
&joy->precision, 0, 32768.0, 100, 0,
"Specify the precision of the axis");
str = "Type %t|Up Axis %x1 |Down Axis %x3|Left Axis %x2|Right Axis %x0";
uiDefButI(block, MENU, B_REDR, str, xco+10 + 0.6 * (width-20), yco-68, 0.4 * (width-20), 19,
&joy->axisf, 2.0, 31, 0, 0,
"The direction of the axis");
if ((joy->flag & SENS_JOY_ANY_EVENT)==0) {
str = "Type %t|Up Axis %x1 |Down Axis %x3|Left Axis %x2|Right Axis %x0";
uiDefButI(block, MENU, B_REDR, str, xco+10 + 0.6 * (width-20), yco-68, 0.4 * (width-20), 19,
&joy->axisf, 2.0, 31, 0, 0,
"The direction of the axis");
}
}
else
{
uiDefButI(block, NUM, 1, "Number:", xco+10, yco-68, 0.6 * (width-20), 19,
uiDefButI(block, NUM, 1, "Number:", xco+10, yco-68, 0.46 * (width-20), 19,
&joy->hat, 1, 2.0, 100, 0,
"Specify which hat to use");
uiDefButI(block, NUM, 1, "Direction:", xco+10 + 0.6 * (width-20), yco-68, 0.4 * (width-20), 19,
&joy->hatf, 0, 12, 100, 0,
"Specify hat direction");
if ((joy->flag & SENS_JOY_ANY_EVENT)==0) {
uiDefButI(block, NUM, 1, "Direction:", xco+10 + 0.6 * (width-20), yco-68, 0.4 * (width-20), 19,
&joy->hatf, 0, 12, 100, 0,
"Specify hat direction");
}
}
yco-= ysize;
break;

View File

@@ -685,7 +685,6 @@ void BL_ConvertSensors(struct Object* blenderobject,
int axis =0;
int axisf =0;
int button =0;
int buttonf =0;
int hat =0;
int hatf =0;
int prec =0;
@@ -700,7 +699,6 @@ void BL_ConvertSensors(struct Object* blenderobject,
break;
case SENS_JOY_BUTTON:
button = bjoy->button;
buttonf = bjoy->buttonf;
joysticktype = SCA_JoystickSensor::KX_JOYSENSORMODE_BUTTON;
break;
case SENS_JOY_HAT:
@@ -719,8 +717,9 @@ void BL_ConvertSensors(struct Object* blenderobject,
joysticktype,
axis,axisf,
prec,
button,buttonf,
hat,hatf);
button,
hat,hatf,
(bjoy->flag & SENS_JOY_ANY_EVENT));
}
else
{

View File

@@ -24,7 +24,9 @@
*
* ***** END GPL LICENSE BLOCK *****
*/
#ifndef DISABLE_SDL
#include <SDL.h>
#endif
#include "SCA_Joystick.h"
#include "SCA_JoystickPrivate.h"
@@ -40,7 +42,10 @@ SCA_Joystick::SCA_Joystick(short int index)
m_buttonnum(-2),
m_hatdir(-2),
m_isinit(0),
m_istrig(0)
m_istrig(0),
m_axismax(-1),
m_buttonmax(-1),
m_hatmax(-1)
{
#ifndef DISABLE_SDL
m_private = new PrivateData();
@@ -116,12 +121,19 @@ void SCA_Joystick::cSetPrecision(int val)
}
bool SCA_Joystick::aAnyAxisIsPositive(int axis)
{
bool result;
int res = pAxisTest(axis);
res > m_prec? result = true: result = false;
return result;
}
bool SCA_Joystick::aRightAxisIsPositive(int axis)
{
bool result;
int res = pGetAxis(axis,1);
res > m_prec? result = true: result = false;
m_istrig = result;
return result;
}
@@ -131,7 +143,6 @@ bool SCA_Joystick::aUpAxisIsPositive(int axis)
bool result;
int res = pGetAxis(axis,0);
res < -m_prec? result = true : result = false;
m_istrig = result;
return result;
}
@@ -141,7 +152,6 @@ bool SCA_Joystick::aLeftAxisIsPositive(int axis)
bool result;
int res = pGetAxis(axis,1);
res < -m_prec ? result = true : result = false;
m_istrig = result;
return result;
}
@@ -151,10 +161,18 @@ bool SCA_Joystick::aDownAxisIsPositive(int axis)
bool result;
int res = pGetAxis(axis,0);
res > m_prec ? result = true:result = false;
m_istrig = result;
return result;
}
bool SCA_Joystick::aAnyButtonPressIsPositive(void)
{
return (m_buttonnum==-2) ? false : true;
}
bool SCA_Joystick::aAnyButtonReleaseIsPositive(void)
{
return (m_buttonnum==-2) ? true : false;
}
bool SCA_Joystick::aButtonPressIsPositive(int button)
{
@@ -163,7 +181,6 @@ bool SCA_Joystick::aButtonPressIsPositive(int button)
#else
bool result;
SDL_JoystickGetButton(m_private->m_joystick, button)? result = true:result = false;
m_istrig = result;
return result;
#endif
}
@@ -176,7 +193,6 @@ bool SCA_Joystick::aButtonReleaseIsPositive(int button)
#else
bool result;
SDL_JoystickGetButton(m_private->m_joystick, button)? result = false : result = true;
m_istrig = result;
return result;
#endif
}
@@ -187,27 +203,9 @@ bool SCA_Joystick::aHatIsPositive(int dir)
bool result;
int res = pGetHat(dir);
res == dir? result = true : result = false;
m_istrig = result;
return result;
}
int SCA_Joystick::pGetButtonPress(int button)
{
if(button == m_buttonnum)
return m_buttonnum;
return -2;
}
int SCA_Joystick::pGetButtonRelease(int button)
{
if(button == m_buttonnum)
return m_buttonnum;
return -2;
}
int SCA_Joystick::pGetHat(int direction)
{
if(direction == m_hatdir){
@@ -218,52 +216,19 @@ int SCA_Joystick::pGetHat(int direction)
int SCA_Joystick::GetNumberOfAxes()
{
#ifdef DISABLE_SDL
return -1;
#else
int number;
if(m_isinit){
if(m_private->m_joystick){
number = SDL_JoystickNumAxes(m_private->m_joystick);
return number;
}
}
return -1;
#endif
return m_axismax;
}
int SCA_Joystick::GetNumberOfButtons()
{
#ifdef DISABLE_SDL
return -1;
#else
int number;
if(m_isinit){
if(m_private->m_joystick){
number = SDL_JoystickNumButtons(m_private->m_joystick);
return number;
}
}
return -1;
#endif
return m_buttonmax;
}
int SCA_Joystick::GetNumberOfHats()
{
#ifdef DISABLE_SDL
return -1;
#else
int number;
if(m_isinit){
if(m_private->m_joystick){
number = SDL_JoystickNumHats(m_private->m_joystick);
return number;
}
}
return -1;
#endif
return m_hatmax;
}
bool SCA_Joystick::CreateJoystickDevice(void)
@@ -280,9 +245,14 @@ bool SCA_Joystick::CreateJoystickDevice(void)
m_private->m_joystick = SDL_JoystickOpen(m_joyindex);
SDL_JoystickEventState(SDL_ENABLE);
echo("Joystick " << m_joyindex << " initialized");
m_isinit = true;
echo("Joystick " << m_joyindex << " initialized");
/* must run after being initialized */
m_axismax = SDL_JoystickNumAxes(m_private->m_joystick);
m_buttonmax = SDL_JoystickNumButtons(m_private->m_joystick);
m_hatmax = SDL_JoystickNumHats(m_private->m_joystick);
}
return true;
#endif
@@ -314,10 +284,10 @@ int SCA_Joystick::Connected(void)
void SCA_Joystick::pFillAxes()
{
#ifndef DISABLE_SDL
if(GetNumberOfAxes() == 1){
if(m_axismax == 1){
m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
}else if(GetNumberOfAxes() > 1){
}else if(m_axismax > 1){
m_axis10 = SDL_JoystickGetAxis(m_private->m_joystick, 0);
m_axis11 = SDL_JoystickGetAxis(m_private->m_joystick, 1);
m_axis20 = SDL_JoystickGetAxis(m_private->m_joystick, 2);
@@ -340,3 +310,13 @@ int SCA_Joystick::pGetAxis(int axisnum, int udlr)
return 0;
}
#define MAX2(x,y) ( (x)>(y) ? (x) : (y) )
int SCA_Joystick::pAxisTest(int axisnum)
{
#ifndef DISABLE_SDL
if(axisnum == 1)return MAX2(abs(m_axis10), abs(m_axis11));
if(axisnum == 2)return MAX2(abs(m_axis20), abs(m_axis21));
#endif
return 0;
}

View File

@@ -29,7 +29,9 @@
#define _SCA_JOYSTICK_H_
#include "SCA_JoystickDefines.h"
#ifndef DISABLE_SDL
#include "SDL.h"
#endif
/*
* Basic Joystick class
@@ -83,8 +85,11 @@ class SCA_Joystick
/*
* max # of buttons avail
*/
int m_axismax;
int m_buttonmax;
int m_hatmax;
/*
* hat values stored here
*/
@@ -131,21 +136,15 @@ class SCA_Joystick
void pFillAxes(void);
void pFillButtons(void);
/*
* returns m_axis10,m_axis11...
*/
int pAxisTest(int axisnum);
/*
* returns m_axis10,m_axis11...
*/
int pGetAxis(int axisnum, int udlr);
/*
* gets the current button
*/
int pGetButtonPress(int button);
/*
* returns if no button is pressed
*/
int pGetButtonRelease(int button);
/*
* gets the current hat direction
@@ -165,11 +164,14 @@ public:
/*
*/
bool aAnyAxisIsPositive(int axis);
bool aUpAxisIsPositive(int axis);
bool aDownAxisIsPositive(int axis);
bool aLeftAxisIsPositive(int axis);
bool aRightAxisIsPositive(int axis);
bool aAnyButtonPressIsPositive(void);
bool aAnyButtonReleaseIsPositive(void);
bool aButtonPressIsPositive(int button);
bool aButtonReleaseIsPositive(int button);
bool aHatIsPositive(int dir);

View File

@@ -24,7 +24,10 @@
*
* ***** END GPL LICENSE BLOCK *****
*/
#ifndef DISABLE_SDL
#include <SDL.h>
#endif
#include "SCA_Joystick.h"
#include "SCA_JoystickPrivate.h"
@@ -46,17 +49,26 @@ void SCA_Joystick::OnHatMotion(SDL_Event* sdl_event)
m_istrig = 1;
}
void SCA_Joystick::OnButtonUp(SDL_Event* sdl_event)
{
m_istrig = 1;
/* this is needed for the "all events" option
* so we know if there are no buttons pressed */
int i;
for (i=0; i<m_buttonmax; i++) {
if (SDL_JoystickGetButton(m_private->m_joystick, i)) {
m_buttonnum = i;
return;
}
}
m_buttonnum = -2;
}
void SCA_Joystick::OnButtonDown(SDL_Event* sdl_event)
{
m_buttonmax = GetNumberOfButtons();
if(sdl_event->jbutton.button >= 1 || sdl_event->jbutton.button <= m_buttonmax)
if(sdl_event->jbutton.button >= 0 || sdl_event->jbutton.button <= m_buttonmax)
{
m_istrig = 1;
m_buttonnum = sdl_event->jbutton.button;
@@ -74,7 +86,12 @@ void SCA_Joystick::OnNothing(SDL_Event* sdl_event)
void SCA_Joystick::HandleEvents(void)
{
SDL_Event sdl_event;
int i;
for (i=0; i<JOYINDEX_MAX; i++) {
SCA_Joystick::m_instance[i]->OnNothing(&sdl_event);
}
if(SDL_PollEvent(&sdl_event))
{
/* Note! m_instance[sdl_event.jaxis.which]

View File

@@ -45,27 +45,26 @@ SCA_JoystickSensor::SCA_JoystickSensor(class SCA_JoystickManager* eventmgr,
short int joyindex,
short int joymode,
int axis, int axisf,int prec,
int button, int buttonf,
int hat, int hatf,
int button,
int hat, int hatf, bool allevents,
PyTypeObject* T )
:SCA_ISensor(gameobj,eventmgr,T),
m_pJoystickMgr(eventmgr),
m_axis(axis),
m_axisf(axisf),
m_button(button),
m_buttonf(buttonf),
m_hat(hat),
m_hatf(hatf),
m_precision(prec),
m_joymode(joymode),
m_joyindex(joyindex)
m_joyindex(joyindex),
m_bAllEvents(allevents)
{
/*
std::cout << " axis " << m_axis << std::endl;
std::cout << " axis flag " << m_axisf << std::endl;
std::cout << " precision " << m_precision << std::endl;
std::cout << " button " << m_button << std::endl;
std::cout << " button flag "<< m_buttonf << std::endl;
std::cout << " hat " << m_hat << std::endl;
std::cout << " hat flag " << m_hatf << std::endl;
*/
@@ -108,9 +107,12 @@ bool SCA_JoystickSensor::Evaluate(CValue* event)
bool result = false;
bool reset = m_reset && m_level;
if(js==NULL)
if(js==NULL) /* no joystick - dont do anything */
return false;
if (!js->IsTrig()) /* No events from SDL? - dont bother */
return reset ? true : false;
m_reset = false;
switch(m_joymode)
{
@@ -124,7 +126,18 @@ bool SCA_JoystickSensor::Evaluate(CValue* event)
numberof== m_axis -- max 2
*/
js->cSetPrecision(m_precision);
if(m_axisf == 1){
if (m_bAllEvents) {
if(js->aAnyAxisIsPositive(m_axis)){
m_istrig = 1;
result = true;
}else{
if(m_istrig){
m_istrig = 0;
result = true;
}
}
}
else if(m_axisf == 1){
if(js->aUpAxisIsPositive(m_axis)){
m_istrig = 1;
result = true;
@@ -135,7 +148,7 @@ bool SCA_JoystickSensor::Evaluate(CValue* event)
}
}
}
if(m_axisf == 3){
else if(m_axisf == 3){
if(js->aDownAxisIsPositive(m_axis)){
m_istrig = 1;
result = true;
@@ -146,7 +159,7 @@ bool SCA_JoystickSensor::Evaluate(CValue* event)
}
}
}
if(m_axisf == 2){
else if(m_axisf == 2){
if(js->aLeftAxisIsPositive(m_axis)){
m_istrig = 1;
result = true;
@@ -157,7 +170,7 @@ bool SCA_JoystickSensor::Evaluate(CValue* event)
}
}
}
if(m_axisf == 0){
else if(m_axisf == 0){
if(js->aRightAxisIsPositive(m_axis)){
m_istrig = 1;
result = true;
@@ -173,32 +186,17 @@ bool SCA_JoystickSensor::Evaluate(CValue* event)
case KX_JOYSENSORMODE_BUTTON:
{
/* what is what!
pressed = m_buttonf == 0
released = m_buttonf == 1
m_button = the actual button in question
*/
if(m_buttonf == 0){
if(js->aButtonPressIsPositive(m_button)){
m_istrig = 1;
if(( m_bAllEvents && js->aAnyButtonPressIsPositive()) || (!m_bAllEvents && js->aButtonPressIsPositive(m_button))) {
m_istrig = 1;
result = true;
}else {
if(m_istrig){
m_istrig = 0;
result = true;
}else {
if(m_istrig){
m_istrig = 0;
result = true;
}
}
}
if(m_buttonf == 1){
if(js->aButtonReleaseIsPositive(m_button)){
m_istrig = 1;
result = true;
}else {
if(m_istrig){
m_istrig = 0;
result = true;
}
}
}
break;
}
case KX_JOYSENSORMODE_HAT:
@@ -250,25 +248,14 @@ bool SCA_JoystickSensor::Evaluate(CValue* event)
break;
}
if (js->IsTrig()) {
/* The if below detects changes with the joystick trigger state.
* js->IsTrig() will stay true as long as the key is held.
* even though the event from SDL will only be sent once.
* (js->IsTrig() && m_istrig_lastjs) - when true it means this sensor
* had the same joystick trigger state last time,
* Setting the result false this time means it wont run the sensors
* controller every time (like a pulse sensor)
*
* This is not done with the joystick its self incase other sensors use
* it or become active.
*/
if (m_istrig_lastjs) {
/* if not all events are enabled, only send a positive pulse when
* the button state changes */
if (!m_bAllEvents) {
if (m_istrig_prev == m_istrig) {
result = false;
} else {
m_istrig_prev = m_istrig;
}
m_istrig_lastjs = true;
} else {
m_istrig = 0;
m_istrig_lastjs = false;
}
if (reset)
@@ -325,11 +312,12 @@ PyMethodDef SCA_JoystickSensor::Methods[] = {
{"setIndex", (PyCFunction) SCA_JoystickSensor::sPySetIndex, METH_O, (PY_METHODCHAR)SetIndex_doc},
{"getAxis", (PyCFunction) SCA_JoystickSensor::sPyGetAxis, METH_NOARGS, (PY_METHODCHAR)GetAxis_doc},
{"setAxis", (PyCFunction) SCA_JoystickSensor::sPySetAxis, METH_VARARGS, (PY_METHODCHAR)SetAxis_doc},
{"getAxisValue", (PyCFunction) SCA_JoystickSensor::sPyGetRealAxis, METH_NOARGS, (PY_METHODCHAR)GetRealAxis_doc},
{"getAxisValue", (PyCFunction) SCA_JoystickSensor::sPyGetAxisValue, METH_NOARGS, (PY_METHODCHAR)GetAxisValue_doc},
{"getThreshold", (PyCFunction) SCA_JoystickSensor::sPyGetThreshold, METH_NOARGS, (PY_METHODCHAR)GetThreshold_doc},
{"setThreshold", (PyCFunction) SCA_JoystickSensor::sPySetThreshold, METH_VARARGS, (PY_METHODCHAR)SetThreshold_doc},
{"getButton", (PyCFunction) SCA_JoystickSensor::sPyGetButton, METH_NOARGS, (PY_METHODCHAR)GetButton_doc},
{"setButton", (PyCFunction) SCA_JoystickSensor::sPySetButton, METH_VARARGS, (PY_METHODCHAR)SetButton_doc},
{"setButton", (PyCFunction) SCA_JoystickSensor::sPySetButton, METH_O, (PY_METHODCHAR)SetButton_doc},
{"getButtonValue",(PyCFunction) SCA_JoystickSensor::sPyGetButtonValue, METH_NOARGS,(PY_METHODCHAR)GetButtonValue_doc},
{"getHat", (PyCFunction) SCA_JoystickSensor::sPyGetHat, METH_NOARGS, (PY_METHODCHAR)GetHat_doc},
{"setHat", (PyCFunction) SCA_JoystickSensor::sPySetHat, METH_VARARGS, (PY_METHODCHAR)SetHat_doc},
{"getNumAxes", (PyCFunction) SCA_JoystickSensor::sPyNumberOfAxes, METH_NOARGS, (PY_METHODCHAR)NumberOfAxes_doc},
@@ -374,7 +362,7 @@ const char SCA_JoystickSensor::GetAxis_doc[] =
"getAxis\n"
"\tReturns the current state of the axis.\n";
PyObject* SCA_JoystickSensor::PyGetAxis( PyObject* self) {
return PyInt_FromLong(m_joyindex);
return Py_BuildValue("[ii]",m_axis, m_axisf);
}
@@ -395,10 +383,10 @@ PyObject* SCA_JoystickSensor::PySetAxis( PyObject* self, PyObject* args ) {
/* get axis value ----------------------------------------------------- */
const char SCA_JoystickSensor::GetRealAxis_doc[] =
const char SCA_JoystickSensor::GetAxisValue_doc[] =
"getAxisValue\n"
"\tReturns a list of the values for each axis .\n";
PyObject* SCA_JoystickSensor::PyGetRealAxis( PyObject* self) {
PyObject* SCA_JoystickSensor::PyGetAxisValue( PyObject* self) {
SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
if(joy)
return Py_BuildValue("[iiii]", joy->GetAxis10(), joy->GetAxis11(), joy->GetAxis20(), joy->GetAxis21());
@@ -429,30 +417,49 @@ PyObject* SCA_JoystickSensor::PySetThreshold( PyObject* self, PyObject* args ) {
Py_RETURN_NONE;
}
/* get button -------------------------------------------------------- */
const char SCA_JoystickSensor::GetButton_doc[] =
"getButton\n"
"\tReturns the currently pressed button.\n";
"\tReturns the current button this sensor is checking.\n";
PyObject* SCA_JoystickSensor::PyGetButton( PyObject* self) {
return Py_BuildValue("[ii]",m_button, m_buttonf);
return PyInt_FromLong(m_button);
}
/* set button -------------------------------------------------------- */
const char SCA_JoystickSensor::SetButton_doc[] =
"setButton\n"
"\tSets the button the sensor reacts to.\n";
PyObject* SCA_JoystickSensor::PySetButton( PyObject* self, PyObject* args ) {
int button,buttonflag;
if(!PyArg_ParseTuple(args, "ii", &button, &buttonflag)){
PyObject* SCA_JoystickSensor::PySetButton( PyObject* self, PyObject* value ) {
int button = PyInt_AsLong(value);
if(button==-1 && PyErr_Occurred()) {
PyErr_SetString(PyExc_ValueError, "expected an int");
return NULL;
}
m_button = button;
m_buttonf = buttonflag;
Py_RETURN_NONE;
}
/* get button value -------------------------------------------------- */
const char SCA_JoystickSensor::GetButtonValue_doc[] =
"getButtonValue\n"
"\tReturns a list containing the indicies of the currently pressed buttons.\n";
PyObject* SCA_JoystickSensor::PyGetButtonValue( PyObject* self) {
SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
PyObject *ls = PyList_New(0);
PyObject *value;
int i;
if(joy) {
for (i=0; i < joy->GetNumberOfButtons(); i++) {
if (joy->aButtonPressIsPositive(i)) {
value = PyInt_FromLong(i);
PyList_Append(ls, value);
Py_DECREF(value);
}
}
}
return ls;
}
/* get hat ----------------------------------------------------------- */
const char SCA_JoystickSensor::GetHat_doc[] =

View File

@@ -73,7 +73,7 @@ class SCA_JoystickSensor :public SCA_ISensor
* Otherwise it will trigger all the time
* this is used to see if the trigger state changes.
*/
bool m_istrig_lastjs;
bool m_istrig_prev;
/**
* The mode to determine axis,button or hat
*/
@@ -83,6 +83,11 @@ class SCA_JoystickSensor :public SCA_ISensor
*/
short int m_joyindex;
/**
* Detect all events for the currently selected type
*/
bool m_bAllEvents;
enum KX_JOYSENSORMODE {
KX_JOYSENSORMODE_NODEF = 0,
KX_JOYSENSORMODE_AXIS,
@@ -98,8 +103,8 @@ public:
short int joyindex,
short int joymode,
int axis, int axisf,int prec,
int button, int buttonf,
int hat, int hatf,
int button,
int hat, int hatf, bool allevents,
PyTypeObject* T=&Type );
virtual ~SCA_JoystickSensor();
virtual CValue* GetReplica();
@@ -124,12 +129,13 @@ public:
/* Axes*/
KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetAxis);
KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,SetAxis);
KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetRealAxis);
KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetAxisValue);
KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetThreshold);
KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,SetThreshold);
/* Buttons */
KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetButton);
KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,SetButton);
KX_PYMETHOD_DOC_O(SCA_JoystickSensor,SetButton);
KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetButtonValue);
/* Hats */
KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,GetHat);
KX_PYMETHOD_DOC_VARARGS(SCA_JoystickSensor,SetHat);