rename and negate DISABLE_PYTHON --> WITH_PYTHON
[blender.git] / source / gameengine / GameLogic / SCA_MouseSensor.cpp
index 8281eed47bcb3b2285443ab03997a181d2900040..af11353c93e0672f5b7e37789e60e4d54d61f215 100644 (file)
@@ -18,7 +18,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software Foundation,
- * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  *
  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
  * All rights reserved.
 #include "ConstExpr.h"
 #include <iostream>
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
 /* ------------------------------------------------------------------------- */
 /* Native functions                                                          */
 /* ------------------------------------------------------------------------- */
 SCA_MouseSensor::SCA_MouseSensor(SCA_MouseManager* eventmgr, 
                                                                 int startx,int starty,
                                                                 short int mousemode,
-                                                                SCA_IObject* gameobj, 
-                                                                PyTypeObject* T)
-    : SCA_ISensor(gameobj,eventmgr, T),
-       m_pMouseMgr(eventmgr),
+                                                                SCA_IObject* gameobj)
+    : SCA_ISensor(gameobj,eventmgr),
        m_x(startx),
        m_y(starty)
 {
        m_mousemode   = mousemode;
        m_triggermode = true;
 
-       UpdateHotkey();
+       UpdateHotkey(this);
        Init();
 }
 
@@ -74,29 +68,30 @@ SCA_MouseSensor::~SCA_MouseSensor()
     /* Nothing to be done here. */
 }
 
-void SCA_MouseSensor::UpdateHotkey()
+void 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 */
@@ -107,7 +102,7 @@ CValue* SCA_MouseSensor::GetReplica()
 {
        SCA_MouseSensor* replica = new SCA_MouseSensor(*this);
        // this will copy properties and so on...
-       CValue::AddDataToReplica(replica);
+       replica->ProcessReplica();
        replica->Init();
 
        return replica;
@@ -140,11 +135,11 @@ SCA_IInputDevice::KX_EnumInputs SCA_MouseSensor::GetHotKey()
 
 
 
-bool SCA_MouseSensor::Evaluate(CValue* event)
+bool SCA_MouseSensor::Evaluate()
 {
        bool result = false;
        bool reset = m_reset && m_level;
-       SCA_IInputDevice* mousedev = m_pMouseMgr->GetInputDevice();
+       SCA_IInputDevice* mousedev = ((SCA_MouseManager *)m_eventmgr)->GetInputDevice();
 
        m_reset = false;
        switch (m_mousemode) {
@@ -154,8 +149,8 @@ bool SCA_MouseSensor::Evaluate(CValue* event)
        case KX_MOUSESENSORMODE_WHEELUP:
        case KX_MOUSESENSORMODE_WHEELDOWN:
                {
-                       const SCA_InputEvent& event = mousedev->GetEventValue(m_hotkey);
-                       switch (event.m_status){        
+                       const SCA_InputEvent& mevent = mousedev->GetEventValue(m_hotkey);
+                       switch (mevent.m_status){       
                        case SCA_InputEvent::KX_JUSTACTIVATED:
                                m_val = 1;
                                result = true;
@@ -237,58 +232,32 @@ bool SCA_MouseSensor::isValid(SCA_MouseSensor::KX_MOUSESENSORMODE m)
        return ((m > KX_MOUSESENSORMODE_NODEF) && (m < KX_MOUSESENSORMODE_MAX));
 }
 
+#ifdef WITH_PYTHON
+
 /* ------------------------------------------------------------------------- */
 /* Python functions                                                          */
 /* ------------------------------------------------------------------------- */
 
-//Deprecated functions ------>
-/* get x position ---------------------------------------------------------- */
-const char SCA_MouseSensor::GetXPosition_doc[] = 
-"getXPosition\n"
-"\tReturns the x-coordinate of the mouse sensor, in frame coordinates.\n"
-"\tThe lower-left corner is the origin. The coordinate is given in\n"
-"\tpixels\n";
-PyObject* SCA_MouseSensor::PyGetXPosition(PyObject* self, 
-                                                                                PyObject* args, 
-                                                                                PyObject* kwds) {
-       ShowDeprecationWarning("getXPosition()", "the position property");
-       return PyInt_FromLong(m_x);
-}
-
-/* get y position ---------------------------------------------------------- */
-const char SCA_MouseSensor::GetYPosition_doc[] = 
-"getYPosition\n"
-"\tReturns the y-coordinate of the mouse sensor, in frame coordinates.\n"
-"\tThe lower-left corner is the origin. The coordinate is given in\n"
-"\tpixels\n";
-PyObject* SCA_MouseSensor::PyGetYPosition(PyObject* self, 
-                                                                                PyObject* args, 
-                                                                                PyObject* kwds) {
-       ShowDeprecationWarning("getYPosition()", "the position property");
-       return PyInt_FromLong(m_y);
-}
-//<----- Deprecated
-
 KX_PYMETHODDEF_DOC_O(SCA_MouseSensor, getButtonStatus,
 "getButtonStatus(button)\n"
-"\tGet the given button's status (KX_NO_INPUTSTATUS, KX_JUSTACTIVATED, KX_ACTIVE or KX_JUSTRELEASED).\n")
+"\tGet the given button's status (KX_INPUT_NONE, KX_INPUT_NONE, KX_INPUT_JUST_ACTIVATED, KX_INPUT_ACTIVE, KX_INPUT_JUST_RELEASED).\n")
 {
-       if (PyInt_Check(value))
+       if (PyLong_Check(value))
        {
-               int button = PyInt_AsLong(value);
+               int button = PyLong_AsSsize_t(value);
                
                if ((button < SCA_IInputDevice::KX_LEFTMOUSE)
-                       || (button > SCA_IInputDevice::KX_MIDDLEMOUSE)){
-                       PyErr_SetString(PyExc_ValueError, "invalid button specified!");
+                       || (button > SCA_IInputDevice::KX_RIGHTMOUSE)){
+                       PyErr_SetString(PyExc_ValueError, "sensor.getButtonStatus(int): Mouse Sensor, invalid button specified!");
                        return NULL;
                }
                
-               SCA_IInputDevice* mousedev = m_pMouseMgr->GetInputDevice();
+               SCA_IInputDevice* mousedev = ((SCA_MouseManager *)m_eventmgr)->GetInputDevice();
                const SCA_InputEvent& event = mousedev->GetEventValue((SCA_IInputDevice::KX_EnumInputs) button);
-               return PyInt_FromLong(event.m_status);
+               return PyLong_FromSsize_t(event.m_status);
        }
        
-       Py_Return;
+       Py_RETURN_NONE;
 }
 
 /* ------------------------------------------------------------------------- */
@@ -296,80 +265,46 @@ KX_PYMETHODDEF_DOC_O(SCA_MouseSensor, getButtonStatus,
 /* ------------------------------------------------------------------------- */
 
 PyTypeObject SCA_MouseSensor::Type = {
-       PyObject_HEAD_INIT(&PyType_Type)
-       0,
+       PyVarObject_HEAD_INIT(NULL, 0)
        "SCA_MouseSensor",
-       sizeof(SCA_MouseSensor),
+       sizeof(PyObjectPlus_Proxy),
        0,
-       PyDestructor,
+       py_base_dealloc,
        0,
-       __getattr,
-       __setattr,
-       0, //&MyPyCompare,
-       __repr,
-       0, //&cvalue_as_number,
        0,
        0,
        0,
-       0
-};
-
-PyParentObject SCA_MouseSensor::Parents[] = {
-       &SCA_MouseSensor::Type,
+       py_base_repr,
+       0,0,0,0,0,0,0,0,0,
+       Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
+       0,0,0,0,0,0,0,
+       Methods,
+       0,
+       0,
        &SCA_ISensor::Type,
-       &SCA_ILogicBrick::Type,
-       &CValue::Type,
-       NULL
+       0,0,0,0,0,0,
+       py_base_new
 };
 
 PyMethodDef SCA_MouseSensor::Methods[] = {
-       //Deprecated functions ------>
-       {"getXPosition", (PyCFunction) SCA_MouseSensor::sPyGetXPosition, METH_VARARGS, (PY_METHODCHAR)GetXPosition_doc},
-       {"getYPosition", (PyCFunction) SCA_MouseSensor::sPyGetYPosition, METH_VARARGS, (PY_METHODCHAR)GetYPosition_doc},
-       //<----- Deprecated
        KX_PYMETHODTABLE_O(SCA_MouseSensor, getButtonStatus),
        {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);
-       
-       _getattr_up(SCA_ISensor);
-}
-
-int SCA_MouseSensor::_setattr(const STR_String& attr, PyObject *value)
+int SCA_MouseSensor::UpdateHotkeyPy(void *self, const PyAttributeDef*)
 {
-       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;               
-       }
-       
-       return SCA_ISensor::_setattr(attr, value);
+       UpdateHotkey(self);
+       // return value is used in py_setattro(),
+       // 0=attribute checked ok (see Attributes array definition)
+       return 0;
 }
 
+PyAttributeDef SCA_MouseSensor::Attributes[] = {
+       KX_PYATTRIBUTE_SHORT_RW_CHECK("mode",KX_MOUSESENSORMODE_NODEF,KX_MOUSESENSORMODE_MAX-1,true,SCA_MouseSensor,m_mousemode,UpdateHotkeyPy),
+       KX_PYATTRIBUTE_SHORT_LIST_RO("position",SCA_MouseSensor,m_x,2),
+       { NULL }        //Sentinel
+};
+
+#endif // WITH_PYTHON
+
 /* eof */