Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenoit Bolsee <benoit.bolsee@online.be>2008-12-31 23:35:20 +0300
committerBenoit Bolsee <benoit.bolsee@online.be>2008-12-31 23:35:20 +0300
commit2dfd34994fb2a5d7d8ca57a7ce1fa18e7047463e (patch)
tree1e441a4511791a211a6efb61d2f4e08036c5fa8d /source/gameengine/GameLogic
parenteee013d9b918a9582951fa71919fa17e07e6e775 (diff)
BGE API cleanup: introduction of a generic framework to link Python attributes to logic brick class member. See KX_PYATTRIBUTE macros in PyObjectPlus.h.
Diffstat (limited to 'source/gameengine/GameLogic')
-rw-r--r--source/gameengine/GameLogic/SCA_JoystickSensor.cpp114
-rw-r--r--source/gameengine/GameLogic/SCA_JoystickSensor.h30
-rw-r--r--source/gameengine/GameLogic/SCA_KeyboardSensor.cpp94
-rw-r--r--source/gameengine/GameLogic/SCA_MouseSensor.cpp69
-rw-r--r--source/gameengine/GameLogic/SCA_MouseSensor.h4
5 files changed, 94 insertions, 217 deletions
diff --git a/source/gameengine/GameLogic/SCA_JoystickSensor.cpp b/source/gameengine/GameLogic/SCA_JoystickSensor.cpp
index ec5921bad85..ce9058448ac 100644
--- a/source/gameengine/GameLogic/SCA_JoystickSensor.cpp
+++ b/source/gameengine/GameLogic/SCA_JoystickSensor.cpp
@@ -327,6 +327,21 @@ PyMethodDef SCA_JoystickSensor::Methods[] = {
{NULL,NULL} //Sentinel
};
+PyAttributeDef SCA_JoystickSensor::Attributes[] = {
+ KX_PYATTRIBUTE_SHORT_RW("index",0,JOYINDEX_MAX-1,SCA_JoystickSensor,m_joyindex),
+ KX_PYATTRIBUTE_INT_RW("threshold",0,32768,SCA_JoystickSensor,m_precision),
+ KX_PYATTRIBUTE_INT_RW("button",0,100,SCA_JoystickSensor,m_button),
+ KX_PYATTRIBUTE_INT_ARRAY_RW_CHECK("axis",0,3,SCA_JoystickSensor,m_axis,2,CheckAxis),
+ KX_PYATTRIBUTE_INT_ARRAY_RW_CHECK("hat",0,12,SCA_JoystickSensor,m_hat,2,CheckHat),
+ // dummy attributes will just be read-only in _setattr
+ // you still need to defined them in _getattr
+ KX_PYATTRIBUTE_DUMMY("axisPosition"),
+ KX_PYATTRIBUTE_DUMMY("numAxis"),
+ KX_PYATTRIBUTE_DUMMY("numButtons"),
+ KX_PYATTRIBUTE_DUMMY("numHats"),
+ KX_PYATTRIBUTE_DUMMY("connected"),
+ { NULL } //Sentinel
+};
PyObject* SCA_JoystickSensor::_getattr(const STR_String& attr) {
SCA_Joystick *joy = m_pJoystickMgr->GetJoystickDevice(m_joyindex);
@@ -348,100 +363,17 @@ PyObject* SCA_JoystickSensor::_getattr(const STR_String& attr) {
if (attr == "connected") {
return PyBool_FromLong( joy ? joy->Connected() : 0 );
}
- if (attr == "index") {
- return PyInt_FromLong(m_joyindex);
- }
- if (attr == "threshold") {
- return PyInt_FromLong(m_precision);
- }
- if (attr == "button") {
- return PyInt_FromLong(m_button);
- }
- if (attr == "axis") {
- return Py_BuildValue("[ii]",m_axis, m_axisf);
- }
- if (attr == "hat") {
- return Py_BuildValue("[ii]",m_hat, m_hatf);
- }
+ PyObject* object = _getattr_self(Attributes, this, attr);
+ if (object != NULL)
+ return object;
_getattr_up(SCA_ISensor);
}
-int SCA_JoystickSensor::_setattr(const STR_String& attr, PyObject *value) {
- if (attr == "axisPosition") {
- PyErr_SetString(PyExc_AttributeError, "axisPosition is read only");
- return 1;
- }
- if (attr == "numAxis") {
- PyErr_SetString(PyExc_AttributeError, "numaxis is read only");
- return 1;
- }
- if (attr == "numButtons") {
- PyErr_SetString(PyExc_AttributeError, "numbuttons is read only");
- return 1;
- }
- if (attr == "numHats") {
- PyErr_SetString(PyExc_AttributeError, "numhats is read only");
- return 1;
- }
- if (attr == "connected") {
- PyErr_SetString(PyExc_AttributeError, "connected is read only");
- return 1;
- }
- if (PyInt_Check(value)) {
- int ival = PyInt_AsLong(value);
- if (attr == "index") {
- if (ival < 0 || ival >= JOYINDEX_MAX) {
- PyErr_SetString(PyExc_ValueError, "joystick index out of range");
- return 1;
- }
- m_joyindex = ival;
- } else if (attr == "threshold") {
- m_precision = ival;
- } else if (attr == "button") {
- if (ival < 0) {
- PyErr_SetString(PyExc_ValueError, "button out of range");
- return 1;
- }
- m_button = ival;
- }
- return 0;
- }
- if (PySequence_Check(value)) {
- if (attr == "axis" || attr == "hat") {
- if (PySequence_Size(value) != 2) {
- PyErr_SetString(PyExc_ValueError, "value must be sequence of 2 integers");
- return 1;
- }
- int ival = -1;
- int ivalf = -1;
- PyObject *item = PySequence_GetItem(value, 0); /* new ref */
- ival = PyInt_AsLong(item);
- Py_DECREF(item);
- item = PySequence_GetItem(value, 1); /* new ref */
- ivalf = PyInt_AsLong(item);
- Py_DECREF(item);
- if (PyErr_Occurred()) {
- PyErr_SetString(PyExc_ValueError, "one or more of the items in the sequence was not an integer");
- return 1;
- }
- if (attr == "axis") {
- if (ival < 1 || ival > 2 || ivalf < 0 || ivalf > 3) {
- PyErr_SetString(PyExc_ValueError, "items in the sequence are out of range");
- return 1;
- }
- m_axis = ival;
- m_axisf = ivalf;
- } else {
- if (ival < 1 || ival > 2) {
- PyErr_SetString(PyExc_ValueError, "items in the sequence are out of range");
- return 1;
- }
- m_hat = ival;
- m_hatf = ivalf;
- }
- }
- return 0;
- }
+int SCA_JoystickSensor::_setattr(const STR_String& attr, PyObject *value)
+{
+ int ret = _setattr_self(Attributes, this, attr, value);
+ if (ret >= 0)
+ return ret;
return SCA_ISensor::_setattr(attr, value);
}
diff --git a/source/gameengine/GameLogic/SCA_JoystickSensor.h b/source/gameengine/GameLogic/SCA_JoystickSensor.h
index 03653070db7..25103b3c6bc 100644
--- a/source/gameengine/GameLogic/SCA_JoystickSensor.h
+++ b/source/gameengine/GameLogic/SCA_JoystickSensor.h
@@ -37,11 +37,11 @@ class SCA_JoystickSensor :public SCA_ISensor
class SCA_JoystickManager* m_pJoystickMgr;
/**
- * Axis 1-or-2
+ * Axis 1-or-2, MUST be followed by m_axisf
*/
int m_axis;
/**
- * Axis flag to find direction
+ * Axis flag to find direction, MUST be an int
*/
int m_axisf;
/**
@@ -53,11 +53,11 @@ class SCA_JoystickSensor :public SCA_ISensor
*/
int m_buttonf;
/**
- * The actual hat
+ * The actual hat. MUST be followed by m_hatf
*/
int m_hat;
/**
- * Flag to find direction 1-12
+ * Flag to find direction 0-11, MUST be an int
*/
int m_hatf;
/**
@@ -147,6 +147,28 @@ public:
KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,NumberOfButtons);
KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,NumberOfHats);
KX_PYMETHOD_DOC_NOARGS(SCA_JoystickSensor,Connected);
+
+ /* attribute check */
+ static int CheckAxis(void *self)
+ {
+ SCA_JoystickSensor* sensor = reinterpret_cast<SCA_JoystickSensor*>(self);
+ if (sensor->m_axis < 1 || sensor->m_axis > 2)
+ {
+ PyErr_SetString(PyExc_ValueError, "axis number must be 1 or 2");
+ return 1;
+ }
+ return 0;
+ }
+ static int CheckHat(void *self)
+ {
+ SCA_JoystickSensor* sensor = reinterpret_cast<SCA_JoystickSensor*>(self);
+ if (sensor->m_hat < 1 || sensor->m_hat > 2)
+ {
+ PyErr_SetString(PyExc_ValueError, "hat number must be 1 or 2");
+ return 1;
+ }
+ return 0;
+ }
};
diff --git a/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp b/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp
index 0e014628ac7..a05f9ae9879 100644
--- a/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp
+++ b/source/gameengine/GameLogic/SCA_KeyboardSensor.cpp
@@ -820,91 +820,29 @@ PyMethodDef SCA_KeyboardSensor::Methods[] = {
{NULL,NULL} //Sentinel
};
+PyAttributeDef SCA_KeyboardSensor::Attributes[] = {
+ KX_PYATTRIBUTE_BOOL_RW("useAllKeys",SCA_KeyboardSensor,m_bAllKeys),
+ KX_PYATTRIBUTE_INT_RW("key",0,1000,SCA_KeyboardSensor,m_hotkey),
+ KX_PYATTRIBUTE_SHORT_RW("hold1",0,1000,SCA_KeyboardSensor,m_qual),
+ KX_PYATTRIBUTE_SHORT_RW("hold2",0,1000,SCA_KeyboardSensor,m_qual2),
+ KX_PYATTRIBUTE_STRING_RW("toggleProperty",0,100,SCA_KeyboardSensor,m_toggleprop),
+ KX_PYATTRIBUTE_STRING_RW("targetProperty",0,100,SCA_KeyboardSensor,m_targetprop),
+ { NULL } //Sentinel
+};
+
PyObject*
SCA_KeyboardSensor::_getattr(const STR_String& attr)
{
- if (attr == "key")
- return PyInt_FromLong(m_hotkey);
-
- if (attr == "hold1")
- return PyInt_FromLong(m_qual);
-
- if (attr == "hold2")
- return PyInt_FromLong(m_qual2);
-
- if (attr == "toggleProperty")
- return PyString_FromString(m_toggleprop);
-
- if (attr == "targetProperty")
- return PyString_FromString(m_targetprop);
-
- if (attr == "useAllKeys")
- return PyInt_FromLong(m_bAllKeys);
-
+ PyObject* object = _getattr_self(Attributes, this, attr);
+ if (object != NULL)
+ return object;
_getattr_up(SCA_ISensor);
}
int SCA_KeyboardSensor::_setattr(const STR_String& attr, PyObject *value)
{
- if (attr == "key")
- {
- if (!PyInt_Check(value)){
- PyErr_SetString(PyExc_TypeError, "expected an integer");
- return 1;
- }
- m_hotkey = PyInt_AsLong(value);
- return 0;
- }
-
- if (attr == "hold1")
- {
- if (!PyInt_Check(value)){
- PyErr_SetString(PyExc_TypeError, "expected an integer");
- return 1;
- }
- m_qual = PyInt_AsLong(value);
- return 0;
- }
-
- if (attr == "hold2")
- {
- if (!PyInt_Check(value)){
- PyErr_SetString(PyExc_TypeError, "expected an integer");
- return 1;
- }
- m_qual2 = PyInt_AsLong(value);
- return 0;
- }
-
- if (attr == "useAllKeys")
- {
- if (!PyInt_Check(value)){
- PyErr_SetString(PyExc_TypeError, "expected an integer");
- return 1;
- }
- m_bAllKeys = (PyInt_AsLong(value) != 0);
- return 0;
- }
-
- if (attr == "logToggleProperty")
- {
- if (!PyString_Check(value)){
- PyErr_SetString(PyExc_TypeError, "expected a string");
- return 1;
- }
- m_toggleprop = PyString_AsString(value);
- return 0;
- }
-
- if (attr == "logTargetProperty")
- {
- if (!PyString_Check(value)){
- PyErr_SetString(PyExc_TypeError, "expected a string");
- return 1;
- }
- m_targetprop = PyString_AsString(value);
- return 0;
- }
-
+ int ret = _setattr_self(Attributes, this, attr, value);
+ if (ret >= 0)
+ return ret;
return SCA_ISensor::_setattr(attr, value);
}
diff --git a/source/gameengine/GameLogic/SCA_MouseSensor.cpp b/source/gameengine/GameLogic/SCA_MouseSensor.cpp
index 8281eed47bc..14d9c898980 100644
--- a/source/gameengine/GameLogic/SCA_MouseSensor.cpp
+++ b/source/gameengine/GameLogic/SCA_MouseSensor.cpp
@@ -59,7 +59,7 @@ SCA_MouseSensor::SCA_MouseSensor(SCA_MouseManager* eventmgr,
m_mousemode = mousemode;
m_triggermode = true;
- UpdateHotkey();
+ UpdateHotkey(this);
Init();
}
@@ -74,33 +74,37 @@ SCA_MouseSensor::~SCA_MouseSensor()
/* Nothing to be done here. */
}
-void SCA_MouseSensor::UpdateHotkey()
+int SCA_MouseSensor::UpdateHotkey(void *self)
{
// gosh, this function is so damn stupid
// its here because of a design mistake in the mouse sensor, it should only
// have 3 trigger modes (button, wheel, move), and let the user set the
// hotkey separately, like the other sensors. but instead it has a mode for
// each friggin key and i have to update the hotkey based on it... genius!
-
- switch (m_mousemode) {
+ SCA_MouseSensor* sensor = reinterpret_cast<SCA_MouseSensor*>(self);
+
+ switch (sensor->m_mousemode) {
case KX_MOUSESENSORMODE_LEFTBUTTON:
- m_hotkey = SCA_IInputDevice::KX_LEFTMOUSE;
+ sensor->m_hotkey = SCA_IInputDevice::KX_LEFTMOUSE;
break;
case KX_MOUSESENSORMODE_MIDDLEBUTTON:
- m_hotkey = SCA_IInputDevice::KX_MIDDLEMOUSE;
+ sensor->m_hotkey = SCA_IInputDevice::KX_MIDDLEMOUSE;
break;
case KX_MOUSESENSORMODE_RIGHTBUTTON:
- m_hotkey = SCA_IInputDevice::KX_RIGHTMOUSE;
+ sensor->m_hotkey = SCA_IInputDevice::KX_RIGHTMOUSE;
break;
case KX_MOUSESENSORMODE_WHEELUP:
- m_hotkey = SCA_IInputDevice::KX_WHEELUPMOUSE;
+ sensor->m_hotkey = SCA_IInputDevice::KX_WHEELUPMOUSE;
break;
case KX_MOUSESENSORMODE_WHEELDOWN:
- m_hotkey = SCA_IInputDevice::KX_WHEELDOWNMOUSE;
+ sensor->m_hotkey = SCA_IInputDevice::KX_WHEELDOWNMOUSE;
break;
default:
; /* ignore, no hotkey */
}
+ // return value is used in _setattr(),
+ // 0=attribute checked ok (see Attributes array definition)
+ return 0;
}
CValue* SCA_MouseSensor::GetReplica()
@@ -331,44 +335,25 @@ PyMethodDef SCA_MouseSensor::Methods[] = {
{NULL,NULL} //Sentinel
};
-PyObject* SCA_MouseSensor::_getattr(const STR_String& attr) {
- if (attr == "position")
- return Py_BuildValue("[ii]", m_x, m_y);
-
- if (attr == "mode")
- return PyInt_FromLong(m_mousemode);
-
+PyAttributeDef SCA_MouseSensor::Attributes[] = {
+ KX_PYATTRIBUTE_SHORT_RW_CHECK("mode",KX_MOUSESENSORMODE_NODEF,KX_MOUSESENSORMODE_MAX-1,SCA_MouseSensor,m_mousemode,UpdateHotkey),
+ KX_PYATTRIBUTE_SHORT_ARRAY_RO("position",SCA_MouseSensor,m_x,2),
+ { NULL } //Sentinel
+};
+
+PyObject* SCA_MouseSensor::_getattr(const STR_String& attr)
+{
+ PyObject* object = _getattr_self(Attributes, this, attr);
+ if (object != NULL)
+ return object;
_getattr_up(SCA_ISensor);
}
int SCA_MouseSensor::_setattr(const STR_String& attr, PyObject *value)
{
- if (attr == "mode")
- {
- if (!PyInt_Check(value)){
- PyErr_SetString(PyExc_TypeError, "expected an integer");
- return 1;
- }
-
- int val = PyInt_AsLong(value);
-
- if ((val < KX_MOUSESENSORMODE_NODEF)
- || (val > KX_MOUSESENSORMODE_MAX)){
- PyErr_SetString(PyExc_ValueError, "invalid mode specified!");
- return 1;
- }
-
- m_mousemode = val;
- UpdateHotkey();
- return 0;
- }
-
- if (attr == "position")
- {
- PyErr_SetString(PyExc_AttributeError, "'position' is a read-only property!");
- return 1;
- }
-
+ int ret = _setattr_self(Attributes, this, attr, value);
+ if (ret >= 0)
+ return ret;
return SCA_ISensor::_setattr(attr, value);
}
diff --git a/source/gameengine/GameLogic/SCA_MouseSensor.h b/source/gameengine/GameLogic/SCA_MouseSensor.h
index 6d6e641c798..58ee96c8856 100644
--- a/source/gameengine/GameLogic/SCA_MouseSensor.h
+++ b/source/gameengine/GameLogic/SCA_MouseSensor.h
@@ -58,7 +58,7 @@ class SCA_MouseSensor : public SCA_ISensor
SCA_IInputDevice::KX_EnumInputs m_hotkey;
/**
- * valid x coordinate
+ * valid x coordinate, MUST be followed by y coordinate
*/
short m_x;
@@ -87,7 +87,7 @@ class SCA_MouseSensor : public SCA_ISensor
bool isValid(KX_MOUSESENSORMODE);
- void UpdateHotkey();
+ static int UpdateHotkey(void *self);
SCA_MouseSensor(class SCA_MouseManager* keybdmgr,
int startx,int starty,