diff options
Diffstat (limited to 'source/gameengine/GameLogic/SCA_JoystickSensor.cpp')
-rw-r--r-- | source/gameengine/GameLogic/SCA_JoystickSensor.cpp | 157 |
1 files changed, 80 insertions, 77 deletions
diff --git a/source/gameengine/GameLogic/SCA_JoystickSensor.cpp b/source/gameengine/GameLogic/SCA_JoystickSensor.cpp index 58818240009..8b96840b149 100644 --- a/source/gameengine/GameLogic/SCA_JoystickSensor.cpp +++ b/source/gameengine/GameLogic/SCA_JoystickSensor.cpp @@ -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; */ @@ -75,6 +74,7 @@ std::cout << " hat flag " << m_hatf << std::endl; void SCA_JoystickSensor::Init() { m_istrig=(m_invert)?1:0; + m_istrig_prev=0; m_reset = true; } @@ -108,7 +108,7 @@ 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; m_reset = false; @@ -123,8 +123,23 @@ bool SCA_JoystickSensor::Evaluate(CValue* event) m_axisf == 3 == down numberof== m_axis -- max 2 */ + + if (!js->IsTrigAxis() && !reset) /* No events from SDL? - dont bother */ + return false; + 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 +150,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 +161,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 +172,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 +188,20 @@ 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 (!js->IsTrigButton() && !reset) /* No events from SDL? - dont bother */ + return false; + + 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: @@ -207,6 +210,10 @@ bool SCA_JoystickSensor::Evaluate(CValue* event) numberof = m_hat -- max 2 direction= m_hatf -- max 12 */ + + if (!js->IsTrigHat() && !reset) /* No events from SDL? - dont bother */ + return false; + if(m_hat == 1){ if(js->aHatIsPositive(m_hatf)){ m_istrig = 1; @@ -229,19 +236,6 @@ bool SCA_JoystickSensor::Evaluate(CValue* event) } } } - /* - if(m_hat == 3){ - if(js->aHatIsPositive(m_hatf)){ - m_istrig = 1; - result = true; - }else{ - if(m_istrig){ - m_istrig = 0; - result = true; - } - } - } - */ break; } /* test for ball anyone ?*/ @@ -250,25 +244,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 +308,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}, @@ -372,16 +356,16 @@ PyObject* SCA_JoystickSensor::PySetIndex( PyObject* self, PyObject* value ) { /* get axis ---------------------------------------------------------- */ const char SCA_JoystickSensor::GetAxis_doc[] = "getAxis\n" -"\tReturns the current state of the axis.\n"; +"\tReturns the current axis this sensor reacts to.\n"; PyObject* SCA_JoystickSensor::PyGetAxis( PyObject* self) { - return PyInt_FromLong(m_joyindex); + return Py_BuildValue("[ii]",m_axis, m_axisf); } /* set axis ---------------------------------------------------------- */ const char SCA_JoystickSensor::SetAxis_doc[] = "setAxis\n" -"\tSets the current state of the axis.\n"; +"\tSets the current axis this sensor reacts to.\n"; PyObject* SCA_JoystickSensor::PySetAxis( PyObject* self, PyObject* args ) { int axis,axisflag; @@ -395,10 +379,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) { +"\tReturns a list of the values for the current state of each axis.\n"; +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 +413,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 current pressed state of each button.\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[] = |