Changeset View
Changeset View
Standalone View
Standalone View
source/gameengine/GameLogic/SCA_MouseFocusSensor.cpp
- This file was moved from source/gameengine/Ketsji/KX_MouseFocusSensor.cpp.
| Show All 17 Lines | |||||
| * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. | * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV. | ||||
| * All rights reserved. | * All rights reserved. | ||||
| * | * | ||||
| * The Original Code is: all of this file. | * The Original Code is: all of this file. | ||||
| * | * | ||||
| * Contributor(s): none yet. | * Contributor(s): none yet. | ||||
| * | * | ||||
| * ***** END GPL LICENSE BLOCK ***** | * ***** END GPL LICENSE BLOCK ***** | ||||
| * KX_MouseFocusSensor determines mouse in/out/over events. | * SCA_MouseFocusSensor determines mouse in/out/over events. | ||||
| */ | */ | ||||
| /** \file gameengine/Ketsji/KX_MouseFocusSensor.cpp | /** \file gameengine/GameLogic/SCA_MouseFocusSensor.cpp | ||||
| * \ingroup ketsji | * \ingroup gamelogic | ||||
| */ | */ | ||||
| #ifdef _MSC_VER | #ifdef _MSC_VER | ||||
| /* This warning tells us about truncation of __long__ stl-generated names. | /* This warning tells us about truncation of __long__ stl-generated names. | ||||
| * It can occasionally cause DevStudio to have internal compiler warnings. */ | * It can occasionally cause DevStudio to have internal compiler warnings. */ | ||||
| # pragma warning(disable:4786) | # pragma warning(disable:4786) | ||||
| #endif | #endif | ||||
| #include "MT_Point3.h" | #include "MT_Point3.h" | ||||
| #include "RAS_FramingManager.h" | #include "RAS_FramingManager.h" | ||||
| #include "RAS_ICanvas.h" | #include "RAS_ICanvas.h" | ||||
| #include "RAS_IRasterizer.h" | #include "RAS_IRasterizer.h" | ||||
| #include "SCA_IScene.h" | #include "SCA_IScene.h" | ||||
| #include "KX_Scene.h" | #include "KX_Scene.h" | ||||
| #include "KX_Camera.h" | #include "KX_Camera.h" | ||||
| #include "KX_MouseFocusSensor.h" | #include "SCA_MouseFocusSensor.h" | ||||
| #include "KX_PyMath.h" | #include "KX_PyMath.h" | ||||
| #include "KX_RayCast.h" | #include "KX_RayCast.h" | ||||
| #include "PHY_IPhysicsController.h" | #include "PHY_IPhysicsController.h" | ||||
| #include "PHY_IPhysicsEnvironment.h" | #include "PHY_IPhysicsEnvironment.h" | ||||
| #include "KX_ClientObjectInfo.h" | #include "KX_ClientObjectInfo.h" | ||||
| /* ------------------------------------------------------------------------- */ | /* ------------------------------------------------------------------------- */ | ||||
| /* Native functions */ | /* Native functions */ | ||||
| /* ------------------------------------------------------------------------- */ | /* ------------------------------------------------------------------------- */ | ||||
| KX_MouseFocusSensor::KX_MouseFocusSensor(SCA_MouseManager* eventmgr, | SCA_MouseFocusSensor::SCA_MouseFocusSensor(SCA_MouseManager* eventmgr, | ||||
| int startx, | int startx, | ||||
| int starty, | int starty, | ||||
| short int mousemode, | short int mousemode, | ||||
| int focusmode, | int focusmode, | ||||
| bool bTouchPulse, | bool bTouchPulse, | ||||
| KX_Scene* kxscene, | KX_Scene* kxscene, | ||||
| KX_KetsjiEngine *kxengine, | KX_KetsjiEngine *kxengine, | ||||
| SCA_IObject* gameobj) | SCA_IObject* gameobj) | ||||
| : SCA_MouseSensor(eventmgr, startx, starty, mousemode, gameobj), | : SCA_MouseSensor(eventmgr, startx, starty, mousemode, gameobj), | ||||
| m_focusmode(focusmode), | m_focusmode(focusmode), | ||||
| m_bTouchPulse(bTouchPulse), | m_bTouchPulse(bTouchPulse), | ||||
| m_kxscene(kxscene), | m_kxscene(kxscene), | ||||
| m_kxengine(kxengine) | m_kxengine(kxengine) | ||||
| { | { | ||||
| Init(); | Init(); | ||||
| } | } | ||||
| void KX_MouseFocusSensor::Init() | void SCA_MouseFocusSensor::Init() | ||||
| { | { | ||||
| m_mouse_over_in_previous_frame = (m_invert)?true:false; | m_mouse_over_in_previous_frame = (m_invert)?true:false; | ||||
| m_positive_event = false; | m_positive_event = false; | ||||
| m_hitObject = 0; | m_hitObject = 0; | ||||
| m_hitObject_Last = NULL; | m_hitObject_Last = NULL; | ||||
| m_reset = true; | m_reset = true; | ||||
| m_hitPosition.setValue(0,0,0); | m_hitPosition.setValue(0,0,0); | ||||
| m_prevTargetPoint.setValue(0,0,0); | m_prevTargetPoint.setValue(0,0,0); | ||||
| m_prevSourcePoint.setValue(0,0,0); | m_prevSourcePoint.setValue(0,0,0); | ||||
| m_hitNormal.setValue(0,0,1); | m_hitNormal.setValue(0,0,1); | ||||
| } | } | ||||
| bool KX_MouseFocusSensor::Evaluate() | bool SCA_MouseFocusSensor::Evaluate() | ||||
| { | { | ||||
| bool result = false; | bool result = false; | ||||
| bool obHasFocus = false; | bool obHasFocus = false; | ||||
| bool reset = m_reset && m_level; | bool reset = m_reset && m_level; | ||||
| // cout << "evaluate focus mouse sensor "<<endl; | // cout << "evaluate focus mouse sensor "<<endl; | ||||
| m_reset = false; | m_reset = false; | ||||
| if (m_focusmode) { | if (m_focusmode) { | ||||
| Show All 29 Lines | // cout << "evaluate focus mouse sensor "<<endl; | ||||
| } | } | ||||
| m_mouse_over_in_previous_frame = obHasFocus; | m_mouse_over_in_previous_frame = obHasFocus; | ||||
| m_hitObject_Last = (void *)m_hitObject; | m_hitObject_Last = (void *)m_hitObject; | ||||
| return result; | return result; | ||||
| } | } | ||||
| bool KX_MouseFocusSensor::RayHit(KX_ClientObjectInfo *client_info, KX_RayCast *result, void * const data) | bool SCA_MouseFocusSensor::RayHit(KX_ClientObjectInfo *client_info, KX_RayCast *result, void * const data) | ||||
| { | { | ||||
| KX_GameObject* hitKXObj = client_info->m_gameobject; | KX_GameObject* hitKXObj = client_info->m_gameobject; | ||||
| /* Is this me? In the ray test, there are a lot of extra checks | /* Is this me? In the ray test, there are a lot of extra checks | ||||
| * for aliasing artifacts from self-hits. That doesn't happen | * for aliasing artifacts from self-hits. That doesn't happen | ||||
| * here, so a simple test suffices. Or does the camera also get | * here, so a simple test suffices. Or does the camera also get | ||||
| * self-hits? (No, and the raysensor shouldn't do it either, since | * self-hits? (No, and the raysensor shouldn't do it either, since | ||||
| * self-hits are excluded by setting the correct ignore-object.) | * self-hits are excluded by setting the correct ignore-object.) | ||||
| Show All 9 Lines | bool SCA_MouseFocusSensor::RayHit(KX_ClientObjectInfo *client_info, KX_RayCast *result, void * const data) | ||||
| } | } | ||||
| return true; // object must be visible to trigger | return true; // object must be visible to trigger | ||||
| //return false; // occluded objects can trigger | //return false; // occluded objects can trigger | ||||
| } | } | ||||
| bool KX_MouseFocusSensor::ParentObjectHasFocusCamera(KX_Camera *cam) | bool SCA_MouseFocusSensor::ParentObjectHasFocusCamera(KX_Camera *cam) | ||||
| { | { | ||||
| /* All screen handling in the gameengine is done by GL, | /* All screen handling in the gameengine is done by GL, | ||||
| * specifically the model/view and projection parts. The viewport | * specifically the model/view and projection parts. The viewport | ||||
| * part is in the creator. | * part is in the creator. | ||||
| * | * | ||||
| * The theory is this: | * The theory is this: | ||||
| * WCS - world coordinates | * WCS - world coordinates | ||||
| * -> wcs_camcs_trafo -> | * -> wcs_camcs_trafo -> | ||||
| ▲ Show 20 Lines • Show All 105 Lines • ▼ Show 20 Lines | bool SCA_MouseFocusSensor::ParentObjectHasFocusCamera(KX_Camera *cam) | ||||
| /* 2. Get the object from PhysicsEnvironment */ | /* 2. Get the object from PhysicsEnvironment */ | ||||
| /* Shoot! Beware that the first argument here is an | /* Shoot! Beware that the first argument here is an | ||||
| * ignore-object. We don't ignore anything... */ | * ignore-object. We don't ignore anything... */ | ||||
| PHY_IPhysicsController* physics_controller = cam->GetPhysicsController(); | PHY_IPhysicsController* physics_controller = cam->GetPhysicsController(); | ||||
| PHY_IPhysicsEnvironment* physics_environment = m_kxscene->GetPhysicsEnvironment(); | PHY_IPhysicsEnvironment* physics_environment = m_kxscene->GetPhysicsEnvironment(); | ||||
| // get UV mapping | // get UV mapping | ||||
| KX_RayCast::Callback<KX_MouseFocusSensor> callback(this,physics_controller,NULL,false,true); | KX_RayCast::Callback<SCA_MouseFocusSensor> callback(this,physics_controller,NULL,false,true); | ||||
| KX_RayCast::RayTest(physics_environment, m_prevSourcePoint, m_prevTargetPoint, callback); | KX_RayCast::RayTest(physics_environment, m_prevSourcePoint, m_prevTargetPoint, callback); | ||||
| if (m_hitObject) | if (m_hitObject) | ||||
| return true; | return true; | ||||
| return false; | return false; | ||||
| } | } | ||||
| bool KX_MouseFocusSensor::ParentObjectHasFocus() | bool SCA_MouseFocusSensor::ParentObjectHasFocus() | ||||
| { | { | ||||
| m_hitObject = 0; | m_hitObject = 0; | ||||
| m_hitPosition.setValue(0,0,0); | m_hitPosition.setValue(0,0,0); | ||||
| m_hitNormal.setValue(1,0,0); | m_hitNormal.setValue(1,0,0); | ||||
| KX_Camera *cam= m_kxscene->GetActiveCamera(); | KX_Camera *cam= m_kxscene->GetActiveCamera(); | ||||
| if (ParentObjectHasFocusCamera(cam)) | if (ParentObjectHasFocusCamera(cam)) | ||||
| return true; | return true; | ||||
| list<class KX_Camera*>* cameras = m_kxscene->GetCameras(); | list<class KX_Camera*>* cameras = m_kxscene->GetCameras(); | ||||
| list<KX_Camera*>::iterator it = cameras->begin(); | list<KX_Camera*>::iterator it = cameras->begin(); | ||||
| while (it != cameras->end()) { | while (it != cameras->end()) { | ||||
| if (((*it) != cam) && (*it)->GetViewport()) | if (((*it) != cam) && (*it)->GetViewport()) | ||||
| if (ParentObjectHasFocusCamera(*it)) | if (ParentObjectHasFocusCamera(*it)) | ||||
| return true; | return true; | ||||
| it++; | it++; | ||||
| } | } | ||||
| return false; | return false; | ||||
| } | } | ||||
| const MT_Point3& KX_MouseFocusSensor::RaySource() const | const MT_Point3& SCA_MouseFocusSensor::RaySource() const | ||||
| { | { | ||||
| return m_prevSourcePoint; | return m_prevSourcePoint; | ||||
| } | } | ||||
| const MT_Point3& KX_MouseFocusSensor::RayTarget() const | const MT_Point3& SCA_MouseFocusSensor::RayTarget() const | ||||
| { | { | ||||
| return m_prevTargetPoint; | return m_prevTargetPoint; | ||||
| } | } | ||||
| const MT_Point3& KX_MouseFocusSensor::HitPosition() const | const MT_Point3& SCA_MouseFocusSensor::HitPosition() const | ||||
| { | { | ||||
| return m_hitPosition; | return m_hitPosition; | ||||
| } | } | ||||
| const MT_Vector3& KX_MouseFocusSensor::HitNormal() const | const MT_Vector3& SCA_MouseFocusSensor::HitNormal() const | ||||
| { | { | ||||
| return m_hitNormal; | return m_hitNormal; | ||||
| } | } | ||||
| const MT_Vector2& KX_MouseFocusSensor::HitUV() const | const MT_Vector2& SCA_MouseFocusSensor::HitUV() const | ||||
| { | { | ||||
| return m_hitUV; | return m_hitUV; | ||||
| } | } | ||||
| #ifdef WITH_PYTHON | #ifdef WITH_PYTHON | ||||
| /* ------------------------------------------------------------------------- */ | /* ------------------------------------------------------------------------- */ | ||||
| /* Python functions */ | /* Python functions */ | ||||
| /* ------------------------------------------------------------------------- */ | /* ------------------------------------------------------------------------- */ | ||||
| /* Integration hooks ------------------------------------------------------- */ | /* Integration hooks ------------------------------------------------------- */ | ||||
| PyTypeObject KX_MouseFocusSensor::Type = { | PyTypeObject SCA_MouseFocusSensor::Type = { | ||||
| PyVarObject_HEAD_INIT(NULL, 0) | PyVarObject_HEAD_INIT(NULL, 0) | ||||
| "KX_MouseFocusSensor", | "SCA_MouseFocusSensor", | ||||
| sizeof(PyObjectPlus_Proxy), | sizeof(PyObjectPlus_Proxy), | ||||
| 0, | 0, | ||||
| py_base_dealloc, | py_base_dealloc, | ||||
| 0, | 0, | ||||
| 0, | 0, | ||||
| 0, | 0, | ||||
| 0, | 0, | ||||
| py_base_repr, | py_base_repr, | ||||
| 0,0,0,0,0,0,0,0,0, | 0,0,0,0,0,0,0,0,0, | ||||
| Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, | ||||
| 0,0,0,0,0,0,0, | 0,0,0,0,0,0,0, | ||||
| Methods, | Methods, | ||||
| 0, | 0, | ||||
| 0, | 0, | ||||
| &SCA_MouseSensor::Type, | &SCA_MouseSensor::Type, | ||||
| 0,0,0,0,0,0, | 0,0,0,0,0,0, | ||||
| py_base_new | py_base_new | ||||
| }; | }; | ||||
| PyMethodDef KX_MouseFocusSensor::Methods[] = { | PyMethodDef SCA_MouseFocusSensor::Methods[] = { | ||||
| {NULL,NULL} //Sentinel | {NULL,NULL} //Sentinel | ||||
| }; | }; | ||||
| PyAttributeDef KX_MouseFocusSensor::Attributes[] = { | PyAttributeDef SCA_MouseFocusSensor::Attributes[] = { | ||||
| KX_PYATTRIBUTE_RO_FUNCTION("raySource", KX_MouseFocusSensor, pyattr_get_ray_source), | KX_PYATTRIBUTE_RO_FUNCTION("raySource", SCA_MouseFocusSensor, pyattr_get_ray_source), | ||||
| KX_PYATTRIBUTE_RO_FUNCTION("rayTarget", KX_MouseFocusSensor, pyattr_get_ray_target), | KX_PYATTRIBUTE_RO_FUNCTION("rayTarget", SCA_MouseFocusSensor, pyattr_get_ray_target), | ||||
| KX_PYATTRIBUTE_RO_FUNCTION("rayDirection", KX_MouseFocusSensor, pyattr_get_ray_direction), | KX_PYATTRIBUTE_RO_FUNCTION("rayDirection", SCA_MouseFocusSensor, pyattr_get_ray_direction), | ||||
| KX_PYATTRIBUTE_RO_FUNCTION("hitObject", KX_MouseFocusSensor, pyattr_get_hit_object), | KX_PYATTRIBUTE_RO_FUNCTION("hitObject", SCA_MouseFocusSensor, pyattr_get_hit_object), | ||||
| KX_PYATTRIBUTE_RO_FUNCTION("hitPosition", KX_MouseFocusSensor, pyattr_get_hit_position), | KX_PYATTRIBUTE_RO_FUNCTION("hitPosition", SCA_MouseFocusSensor, pyattr_get_hit_position), | ||||
| KX_PYATTRIBUTE_RO_FUNCTION("hitNormal", KX_MouseFocusSensor, pyattr_get_hit_normal), | KX_PYATTRIBUTE_RO_FUNCTION("hitNormal", SCA_MouseFocusSensor, pyattr_get_hit_normal), | ||||
| KX_PYATTRIBUTE_RO_FUNCTION("hitUV", KX_MouseFocusSensor, pyattr_get_hit_uv), | KX_PYATTRIBUTE_RO_FUNCTION("hitUV", SCA_MouseFocusSensor, pyattr_get_hit_uv), | ||||
| KX_PYATTRIBUTE_BOOL_RW("usePulseFocus", KX_MouseFocusSensor,m_bTouchPulse), | KX_PYATTRIBUTE_BOOL_RW("usePulseFocus", SCA_MouseFocusSensor,m_bTouchPulse), | ||||
| { NULL } //Sentinel | { NULL } //Sentinel | ||||
| }; | }; | ||||
| /* Attributes */ | /* Attributes */ | ||||
| PyObject *KX_MouseFocusSensor::pyattr_get_ray_source(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | PyObject *SCA_MouseFocusSensor::pyattr_get_ray_source(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | ||||
| { | { | ||||
| KX_MouseFocusSensor* self = static_cast<KX_MouseFocusSensor*>(self_v); | SCA_MouseFocusSensor* self = static_cast<SCA_MouseFocusSensor*>(self_v); | ||||
| return PyObjectFrom(self->RaySource()); | return PyObjectFrom(self->RaySource()); | ||||
| } | } | ||||
| PyObject *KX_MouseFocusSensor::pyattr_get_ray_target(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | PyObject *SCA_MouseFocusSensor::pyattr_get_ray_target(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | ||||
| { | { | ||||
| KX_MouseFocusSensor* self = static_cast<KX_MouseFocusSensor*>(self_v); | SCA_MouseFocusSensor* self = static_cast<SCA_MouseFocusSensor*>(self_v); | ||||
| return PyObjectFrom(self->RayTarget()); | return PyObjectFrom(self->RayTarget()); | ||||
| } | } | ||||
| PyObject *KX_MouseFocusSensor::pyattr_get_ray_direction(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | PyObject *SCA_MouseFocusSensor::pyattr_get_ray_direction(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | ||||
| { | { | ||||
| KX_MouseFocusSensor* self = static_cast<KX_MouseFocusSensor*>(self_v); | SCA_MouseFocusSensor* self = static_cast<SCA_MouseFocusSensor*>(self_v); | ||||
| MT_Vector3 dir = self->RayTarget() - self->RaySource(); | MT_Vector3 dir = self->RayTarget() - self->RaySource(); | ||||
| if (MT_fuzzyZero(dir)) dir.setValue(0,0,0); | if (MT_fuzzyZero(dir)) dir.setValue(0,0,0); | ||||
| else dir.normalize(); | else dir.normalize(); | ||||
| return PyObjectFrom(dir); | return PyObjectFrom(dir); | ||||
| } | } | ||||
| PyObject *KX_MouseFocusSensor::pyattr_get_hit_object(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | PyObject *SCA_MouseFocusSensor::pyattr_get_hit_object(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | ||||
| { | { | ||||
| KX_MouseFocusSensor* self = static_cast<KX_MouseFocusSensor*>(self_v); | SCA_MouseFocusSensor* self = static_cast<SCA_MouseFocusSensor*>(self_v); | ||||
| if (self->m_hitObject) | if (self->m_hitObject) | ||||
| return self->m_hitObject->GetProxy(); | return self->m_hitObject->GetProxy(); | ||||
| Py_RETURN_NONE; | Py_RETURN_NONE; | ||||
| } | } | ||||
| PyObject *KX_MouseFocusSensor::pyattr_get_hit_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | PyObject *SCA_MouseFocusSensor::pyattr_get_hit_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | ||||
| { | { | ||||
| KX_MouseFocusSensor* self = static_cast<KX_MouseFocusSensor*>(self_v); | SCA_MouseFocusSensor* self = static_cast<SCA_MouseFocusSensor*>(self_v); | ||||
| return PyObjectFrom(self->HitPosition()); | return PyObjectFrom(self->HitPosition()); | ||||
| } | } | ||||
| PyObject *KX_MouseFocusSensor::pyattr_get_hit_normal(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | PyObject *SCA_MouseFocusSensor::pyattr_get_hit_normal(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | ||||
| { | { | ||||
| KX_MouseFocusSensor* self = static_cast<KX_MouseFocusSensor*>(self_v); | SCA_MouseFocusSensor* self = static_cast<SCA_MouseFocusSensor*>(self_v); | ||||
| return PyObjectFrom(self->HitNormal()); | return PyObjectFrom(self->HitNormal()); | ||||
| } | } | ||||
| PyObject *KX_MouseFocusSensor::pyattr_get_hit_uv(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | PyObject *SCA_MouseFocusSensor::pyattr_get_hit_uv(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef) | ||||
| { | { | ||||
| KX_MouseFocusSensor* self = static_cast<KX_MouseFocusSensor*>(self_v); | SCA_MouseFocusSensor* self = static_cast<SCA_MouseFocusSensor*>(self_v); | ||||
| return PyObjectFrom(self->HitUV()); | return PyObjectFrom(self->HitUV()); | ||||
| } | } | ||||
| #endif // WITH_PYTHON | #endif // WITH_PYTHON | ||||
| /* eof */ | /* eof */ | ||||