remove all python api functions deprecated in 2.49
[blender.git] / source / gameengine / Ketsji / KX_MouseFocusSensor.cpp
index c2e4430d1bc7b444eae9eaee20c975a0655c0357..8abc4f6b897d3440f5779bf2b5337ebc760ba60c 100644 (file)
@@ -42,6 +42,7 @@
 #include "KX_Scene.h"
 #include "KX_Camera.h"
 #include "KX_MouseFocusSensor.h"
+#include "KX_PyMath.h"
 
 #include "KX_RayCast.h"
 #include "KX_IPhysicsController.h"
@@ -60,14 +61,13 @@ KX_MouseFocusSensor::KX_MouseFocusSensor(SCA_MouseManager* eventmgr,
                                                                                 int starty,
                                                                                 short int mousemode,
                                                                                 int focusmode,
-                                                                                RAS_ICanvas* canvas,
+                                                                                bool bTouchPulse,
                                                                                 KX_Scene* kxscene,
                                                                                 KX_KetsjiEngine *kxengine,
-                                                                                SCA_IObject* gameobj, 
-                                                                                PyTypeObject* T)
-    : SCA_MouseSensor(eventmgr, startx, starty, mousemode, gameobj, T),
+                                                                                SCA_IObject* gameobj)
+       : SCA_MouseSensor(eventmgr, startx, starty, mousemode, gameobj),
          m_focusmode(focusmode),
-         m_gp_canvas(canvas),
+         m_bTouchPulse(bTouchPulse),
          m_kxscene(kxscene),
          m_kxengine(kxengine)
 {
@@ -79,10 +79,16 @@ void KX_MouseFocusSensor::Init()
        m_mouse_over_in_previous_frame = (m_invert)?true:false;
        m_positive_event = false;
        m_hitObject = 0;
+       m_hitObject_Last = NULL;
        m_reset = true;
+       
+       m_hitPosition.setValue(0,0,0);
+       m_prevTargetPoint.setValue(0,0,0);
+       m_prevSourcePoint.setValue(0,0,0);
+       m_hitNormal.setValue(0,0,1);
 }
 
-bool KX_MouseFocusSensor::Evaluate(CValue* event)
+bool KX_MouseFocusSensor::Evaluate()
 {
        bool result = false;
        bool obHasFocus = false;
@@ -104,7 +110,10 @@ bool KX_MouseFocusSensor::Evaluate(CValue* event)
                        m_positive_event = true;
                        if (!m_mouse_over_in_previous_frame) {
                                result = true;
-                       } 
+                       }
+                       else if(m_bTouchPulse && (m_hitObject != m_hitObject_Last)) {
+                               result = true;
+                       }
                } 
                if (reset) {
                        // force an event 
@@ -115,12 +124,13 @@ bool KX_MouseFocusSensor::Evaluate(CValue* event)
          * mode is never used, because the converter never makes this
          * sensor for a mouse-key event. It is here for
          * completeness. */
-               result = SCA_MouseSensor::Evaluate(event);
+               result = SCA_MouseSensor::Evaluate();
                m_positive_event = (m_val!=0);
        }
 
        m_mouse_over_in_previous_frame = obHasFocus;
-
+       m_hitObject_Last = (void *)m_hitObject;
+                                          
        return result;
 }
 
@@ -149,12 +159,8 @@ bool KX_MouseFocusSensor::RayHit(KX_ClientObjectInfo* client_info, KX_RayCast* r
 
 
 
-bool KX_MouseFocusSensor::ParentObjectHasFocus(void)
+bool KX_MouseFocusSensor::ParentObjectHasFocusCamera(KX_Camera *cam)
 {
-       m_hitObject = 0;
-       m_hitPosition = MT_Vector3(0,0,0);
-       m_hitNormal =   MT_Vector3(1,0,0);
-
        /* All screen handling in the gameengine is done by GL,
         * specifically the model/view and projection parts. The viewport
         * part is in the creator. 
@@ -186,6 +192,7 @@ bool KX_MouseFocusSensor::ParentObjectHasFocus(void)
         *    = 1.0 - 2(y_blender - y_lb)/height
         *
         * */
+        
        
        /* Because we don't want to worry about resize events, camera
         * changes and all that crap, we just determine this over and
@@ -194,15 +201,20 @@ bool KX_MouseFocusSensor::ParentObjectHasFocus(void)
         * canvas, the test is irrelevant. The 1.0 makes sure the
         * calculations don't bomb. Maybe we should explicitly guard for
         * division by 0.0...*/
-
-       KX_Camera* cam = m_kxscene->GetActiveCamera();
-
-       /* get the scenes current viewport. we recompute it because there
-        * may be multiple cameras and m_kxscene->GetSceneViewport() only
-        * has the one that was last drawn */
-
+       
        RAS_Rect area, viewport;
+       short m_y_inv = m_kxengine->GetCanvas()->GetHeight()-m_y;
+       
        m_kxengine->GetSceneViewport(m_kxscene, cam, area, viewport);
+       
+       /* Check if the mouse is in the viewport */
+       if ((   m_x < viewport.m_x2 &&  // less then right
+                       m_x > viewport.m_x1 &&  // more then then left
+                       m_y_inv < viewport.m_y2 &&      // below top
+                       m_y_inv > viewport.m_y1) == 0)  // above bottom
+       {
+               return false;
+       }
 
        float height = float(viewport.m_y2 - viewport.m_y1 + 1);
        float width  = float(viewport.m_x2 - viewport.m_x1 + 1);
@@ -210,14 +222,17 @@ bool KX_MouseFocusSensor::ParentObjectHasFocus(void)
        float x_lb = float(viewport.m_x1);
        float y_lb = float(viewport.m_y1);
 
+       MT_Vector4 frompoint;
+       MT_Vector4 topoint;
+       
+       /* m_y_inv - inverting for a bounds check is only part of it, now make relative to view bounds */
+       m_y_inv = (viewport.m_y2 - m_y_inv) + viewport.m_y1;
+       
+       
        /* There's some strangeness I don't fully get here... These values
-        * _should_ be wrong! */
+        * _should_ be wrong! - see from point Z values */
+       
        
-
-       /* old: */
-       float nearclip = 0.0;
-       float farclip = 1.0;
-
        /*      build the from and to point in normalized device coordinates 
         *      Looks like normailized device coordinates are [-1,1] in x [-1,1] in y
         *      [0,-1] in z 
@@ -225,23 +240,19 @@ bool KX_MouseFocusSensor::ParentObjectHasFocus(void)
         *      The actual z coordinates used don't have to be exact just infront and 
         *      behind of the near and far clip planes.
         */ 
-       MT_Vector4 frompoint = MT_Vector4( 
-               (2 * (m_x-x_lb) / width) - 1.0,
-               1.0 - (2 * (m_y - y_lb) / height),
-               nearclip,
-               1.0
-       );
-       MT_Vector4 topoint = MT_Vector4( 
-               (2 * (m_x-x_lb) / width) - 1.0,
-               1.0 - (2 * (m_y-y_lb) / height),
-               farclip,
-               1.0
-       );
+       frompoint.setValue(     (2 * (m_x-x_lb) / width) - 1.0,
+                                               1.0 - (2 * (m_y_inv - y_lb) / height),
+                                               /*cam->GetCameraData()->m_perspective ? 0.0:cdata->m_clipstart,*/ /* real clipstart is scaled in ortho for some reason, zero is ok */
+                                               0.0, /* nearclip, see above comments */
+                                               1.0 );
+       
+       topoint.setValue(       (2 * (m_x-x_lb) / width) - 1.0,
+                                               1.0 - (2 * (m_y_inv-y_lb) / height),
+                                               cam->GetCameraData()->m_perspective ? 1.0:cam->GetCameraData()->m_clipend, /* farclip, see above comments */
+                                               1.0 );
 
        /* camera to world  */
        MT_Transform wcs_camcs_tranform = cam->GetWorldToCamera();
-       if (!cam->GetCameraData()->m_perspective)
-               wcs_camcs_tranform.getOrigin()[2] *= 100.0;
        MT_Transform cams_wcs_transform;
        cams_wcs_transform.invert(wcs_camcs_tranform);
        
@@ -259,31 +270,74 @@ bool KX_MouseFocusSensor::ParentObjectHasFocus(void)
        topoint   = camcs_wcs_matrix * topoint;
        
        /* from hom wcs to 3d wcs: */
-       MT_Point3 frompoint3 = MT_Point3(frompoint[0]/frompoint[3], 
-                                                                        frompoint[1]/frompoint[3], 
-                                                                        frompoint[2]/frompoint[3]); 
-       MT_Point3 topoint3 = MT_Point3(topoint[0]/topoint[3], 
-                                                                  topoint[1]/topoint[3], 
-                                                                  topoint[2]/topoint[3]); 
-       m_prevTargetPoint = topoint3;
-       m_prevSourcePoint = frompoint3;
+       m_prevSourcePoint.setValue(     frompoint[0]/frompoint[3],
+                                                               frompoint[1]/frompoint[3],
+                                                               frompoint[2]/frompoint[3]); 
+       
+       m_prevTargetPoint.setValue(     topoint[0]/topoint[3],
+                                                               topoint[1]/topoint[3],
+                                                               topoint[2]/topoint[3]); 
        
        /* 2. Get the object from PhysicsEnvironment */
        /* Shoot! Beware that the first argument here is an
         * ignore-object. We don't ignore anything... */
-       
        KX_IPhysicsController* physics_controller = cam->GetPhysicsController();
        PHY_IPhysicsEnvironment* physics_environment = m_kxscene->GetPhysicsEnvironment();
 
-       bool result = false;
-
        KX_RayCast::Callback<KX_MouseFocusSensor> callback(this,physics_controller);
-       KX_RayCast::RayTest(physics_environment, frompoint3, topoint3, callback);
+        
+       KX_RayCast::RayTest(physics_environment, m_prevSourcePoint, m_prevTargetPoint, callback);
+       
+       if (m_hitObject)
+               return true;
        
-       result = (m_hitObject!=0);
+       return false;
+}
 
-       return result;
+bool KX_MouseFocusSensor::ParentObjectHasFocus()
+{
+       m_hitObject = 0;
+       m_hitPosition.setValue(0,0,0);
+       m_hitNormal.setValue(1,0,0);
+       
+       KX_Camera *cam= m_kxscene->GetActiveCamera();
+       
+       if(ParentObjectHasFocusCamera(cam))
+               return true;
+
+       list<class KX_Camera*>* cameras = m_kxscene->GetCameras();
+       list<KX_Camera*>::iterator it = cameras->begin();
+       
+       while(it != cameras->end())
+       {
+               if(((*it) != cam) && (*it)->GetViewport())
+                       if (ParentObjectHasFocusCamera(*it))
+                               return true;
+               
+               it++;
+       }
+       
+       return false;
+}
+
+const MT_Point3& KX_MouseFocusSensor::RaySource() const
+{
+       return m_prevSourcePoint;
+}
 
+const MT_Point3& KX_MouseFocusSensor::RayTarget() const
+{
+       return m_prevTargetPoint;
+}
+
+const MT_Point3& KX_MouseFocusSensor::HitPosition() const
+{
+       return m_hitPosition;
+}
+
+const MT_Vector3& KX_MouseFocusSensor::HitNormal() const
+{
+       return m_hitNormal;
 }
 
 /* ------------------------------------------------------------------------- */
@@ -292,161 +346,87 @@ bool KX_MouseFocusSensor::ParentObjectHasFocus(void)
 
 /* Integration hooks ------------------------------------------------------- */
 PyTypeObject KX_MouseFocusSensor::Type = {
-       PyObject_HEAD_INIT(&PyType_Type)
-       0,
+       PyVarObject_HEAD_INIT(NULL, 0)
        "KX_MouseFocusSensor",
-       sizeof(KX_MouseFocusSensor),
+       sizeof(PyObjectPlus_Proxy),
        0,
-       PyDestructor,
+       py_base_dealloc,
        0,
-       __getattr,
-       __setattr,
-       0, //&MyPyCompare,
-       __repr,
-       0, //&cvalue_as_number,
        0,
        0,
        0,
-       0
-};
-
-PyParentObject KX_MouseFocusSensor::Parents[] = {
-       &KX_MouseFocusSensor::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_MouseSensor::Type,
-       &SCA_ISensor::Type,
-       &SCA_ILogicBrick::Type,
-       &CValue::Type,
-       NULL
+       0,0,0,0,0,0,
+       py_base_new
 };
 
 PyMethodDef KX_MouseFocusSensor::Methods[] = {
-       {"getRayTarget", (PyCFunction) KX_MouseFocusSensor::sPyGetRayTarget, 
-        METH_VARARGS, GetRayTarget_doc},
-       {"getRaySource", (PyCFunction) KX_MouseFocusSensor::sPyGetRaySource, 
-        METH_VARARGS, GetRaySource_doc},
-       {"getHitObject",(PyCFunction) KX_MouseFocusSensor::sPyGetHitObject,METH_VARARGS, GetHitObject_doc},
-       {"getHitPosition",(PyCFunction) KX_MouseFocusSensor::sPyGetHitPosition,METH_VARARGS, GetHitPosition_doc},
-       {"getHitNormal",(PyCFunction) KX_MouseFocusSensor::sPyGetHitNormal,METH_VARARGS, GetHitNormal_doc},
-       {"getRayDirection",(PyCFunction) KX_MouseFocusSensor::sPyGetRayDirection,METH_VARARGS, GetRayDirection_doc},
-
-
        {NULL,NULL} //Sentinel
 };
 
-PyObject* KX_MouseFocusSensor::_getattr(const STR_String& attr) {
-       _getattr_up(SCA_MouseSensor);
-}
-
+PyAttributeDef KX_MouseFocusSensor::Attributes[] = {
+       KX_PYATTRIBUTE_RO_FUNCTION("raySource",         KX_MouseFocusSensor, pyattr_get_ray_source),
+       KX_PYATTRIBUTE_RO_FUNCTION("rayTarget",         KX_MouseFocusSensor, pyattr_get_ray_target),
+       KX_PYATTRIBUTE_RO_FUNCTION("rayDirection",      KX_MouseFocusSensor, pyattr_get_ray_direction),
+       KX_PYATTRIBUTE_RO_FUNCTION("hitObject",         KX_MouseFocusSensor, pyattr_get_hit_object),
+       KX_PYATTRIBUTE_RO_FUNCTION("hitPosition",       KX_MouseFocusSensor, pyattr_get_hit_position),
+       KX_PYATTRIBUTE_RO_FUNCTION("hitNormal",         KX_MouseFocusSensor, pyattr_get_hit_normal),
+       KX_PYATTRIBUTE_BOOL_RW("usePulseFocus", KX_MouseFocusSensor,m_bTouchPulse),
+       { NULL }        //Sentinel
+};
 
-const char KX_MouseFocusSensor::GetHitObject_doc[] = 
-"getHitObject()\n"
-"\tReturns the name of the object that was hit by this ray.\n";
-PyObject* KX_MouseFocusSensor::PyGetHitObject(PyObject* self, 
-                                                                                  PyObject* args, 
-                                                                                  PyObject* kwds)
+/* Attributes */
+PyObject* KX_MouseFocusSensor::pyattr_get_ray_source(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
-       if (m_hitObject)
-       {
-               return m_hitObject->AddRef();
-       }
-       Py_Return;
+       KX_MouseFocusSensor* self= static_cast<KX_MouseFocusSensor*>(self_v);
+       return PyObjectFrom(self->RaySource());
 }
 
-
-const char KX_MouseFocusSensor::GetHitPosition_doc[] = 
-"getHitPosition()\n"
-"\tReturns the position (in worldcoordinates) where the object was hit by this ray.\n";
-PyObject* KX_MouseFocusSensor::PyGetHitPosition(PyObject* self, 
-                              PyObject* args, 
-                              PyObject* kwds)
+PyObject* KX_MouseFocusSensor::pyattr_get_ray_target(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
-
-       MT_Point3 pos = m_hitPosition;
-
-       PyObject* resultlist = PyList_New(3);
-       int index;
-       for (index=0;index<3;index++)
-       {
-               PyList_SetItem(resultlist,index,PyFloat_FromDouble(pos[index]));
-       }
-       return resultlist;
-
+       KX_MouseFocusSensor* self= static_cast<KX_MouseFocusSensor*>(self_v);
+       return PyObjectFrom(self->RayTarget());
 }
 
-const char KX_MouseFocusSensor::GetRayDirection_doc[] = 
-"getRayDirection()\n"
-"\tReturns the direction from the ray (in worldcoordinates) .\n";
-PyObject* KX_MouseFocusSensor::PyGetRayDirection(PyObject* self, 
-                              PyObject* args, 
-                              PyObject* kwds)
+PyObject* KX_MouseFocusSensor::pyattr_get_ray_direction(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
-
-       MT_Vector3 dir = m_prevTargetPoint - m_prevSourcePoint;
-       dir.normalize();
-
-       PyObject* resultlist = PyList_New(3);
-       int index;
-       for (index=0;index<3;index++)
-       {
-               PyList_SetItem(resultlist,index,PyFloat_FromDouble(dir[index]));
-       }
-       return resultlist;
-
+       KX_MouseFocusSensor* self= static_cast<KX_MouseFocusSensor*>(self_v);
+       MT_Vector3 dir = self->RayTarget() - self->RaySource();
+       if(MT_fuzzyZero(dir))   dir.setValue(0,0,0);
+       else                                    dir.normalize();
+       return PyObjectFrom(dir);
 }
 
-const char KX_MouseFocusSensor::GetHitNormal_doc[] = 
-"getHitNormal()\n"
-"\tReturns the normal (in worldcoordinates) of the object at the location where the object was hit by this ray.\n";
-PyObject* KX_MouseFocusSensor::PyGetHitNormal(PyObject* self, 
-                              PyObject* args, 
-                              PyObject* kwds)
+PyObject* KX_MouseFocusSensor::pyattr_get_hit_object(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
 {
-       MT_Vector3 pos = m_hitNormal;
-
-       PyObject* resultlist = PyList_New(3);
-       int index;
-       for (index=0;index<3;index++)
-       {
-               PyList_SetItem(resultlist,index,PyFloat_FromDouble(pos[index]));
-       }
-       return resultlist;
-
+       KX_MouseFocusSensor* self= static_cast<KX_MouseFocusSensor*>(self_v);
+       
+       if(self->m_hitObject)
+               return self->m_hitObject->GetProxy();
+       
+       Py_RETURN_NONE;
 }
 
-
-/*  getRayTarget                                                */
-const char KX_MouseFocusSensor::GetRayTarget_doc[] = 
-"getRayTarget()\n"
-"\tReturns the target of the ray that seeks the focus object,\n"
-"\tin worldcoordinates.";
-PyObject* KX_MouseFocusSensor::PyGetRayTarget(PyObject* self, 
-                                                                                         PyObject* args, 
-                                                                                         PyObject* kwds) {
-       PyObject *retVal = PyList_New(3);
-
-       PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_prevTargetPoint[0]));
-       PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_prevTargetPoint[1]));
-       PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_prevTargetPoint[2]));
-       
-       return retVal;
+PyObject* KX_MouseFocusSensor::pyattr_get_hit_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_MouseFocusSensor* self= static_cast<KX_MouseFocusSensor*>(self_v);
+       return PyObjectFrom(self->HitPosition());
 }
 
-/*  getRayTarget                                                */
-const char KX_MouseFocusSensor::GetRaySource_doc[] = 
-"getRaySource()\n"
-"\tReturns the source of the ray that seeks the focus object,\n"
-"\tin worldcoordinates.";
-PyObject* KX_MouseFocusSensor::PyGetRaySource(PyObject* self, 
-                                                                                         PyObject* args, 
-                                                                                         PyObject* kwds) {
-       PyObject *retVal = PyList_New(3);
-
-       PyList_SetItem(retVal, 0, PyFloat_FromDouble(m_prevSourcePoint[0]));
-       PyList_SetItem(retVal, 1, PyFloat_FromDouble(m_prevSourcePoint[1]));
-       PyList_SetItem(retVal, 2, PyFloat_FromDouble(m_prevSourcePoint[2]));
-       
-       return retVal;
+PyObject* KX_MouseFocusSensor::pyattr_get_hit_normal(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
+{
+       KX_MouseFocusSensor* self= static_cast<KX_MouseFocusSensor*>(self_v);
+       return PyObjectFrom(self->HitNormal());
 }
 
+
+
 /* eof */