95164a09a7
I made this patch to declared a python list without converting all elements in python object (too slow) or use a CListValue which required CValue items (too expensive in memory). In the case of a big list of points like a collision contacts points list, to use a CListValue we must implement a new class based on CValue for 3D vector to create a python proxy even if mathutils do it perfectly, we must also convert all points (frequently ~100 points) when fill the CListValue even if the list is not used (in the case of the collision callback). The easy way is to use callback (it doesn't worth to do an inheritance) which convert the item in PyObject only during an acces. 5 callbacks are used : - Check if the list is valid = allow acces (like PyObjectPlus.invalid) - Get the list size - Get an item in the list by index. - Get an item name in the list by index (used for operator `list["name"]`) - Set an item in the list at the index position. All of these callback take as first argument the client instance. Why do we use a void * for the client instance ? : In KX_PythonInitTypes.cpp we have to initialize each python inherited class, if we use a template (the only other way) we must add this class each time we use a new type with in KX_PythonInitTypes.cpp To check if the list can be accessed from python by the user, we check if the python proxy, which is the `m_base` member, is still a valid proxy like in PyObjectPlus. But we can use a callback for more control of user access (e.g a list of collision point invalidate a frame later, in this case no real python owner). This python list is easily defined with : ``` CPythonCallBackList( void *client, // The client instance PyObject *base, // The python instance which owned this list, used to know if the list is valid (like in KX_PythonSeq) bool (*checkValid)(void *), // A callback to check if this list is till valid (optional) int (*getSize)(void *), // A callback to get size PyObject *(*getItem)(void *, int), // A callback to get an item const char *(*getItemName)(void *, int), // A callback to get an item name (optional) use for acces by string key bool (*setItem)(void *, int, PyObject *) // A callback to set an item (optional) ) ``` To show its usecase i replaced the odd KX_PythonSeq, it modify KX_Gameobject.sensors/controllers/actuators, SCA_IController.sensors/actuators and BL_ArmatureObject.constraints/channels. Example : {F245193}, See message in console, press R to erase the object and see invalid proxy error message. Reviewers: brita_, #game_python, youle, campbellbarton, moguri, agoose77, sergey Reviewed By: campbellbarton, moguri, agoose77, sergey Subscribers: sergey Projects: #game_engine Differential Revision: https://developer.blender.org/D1363
299 lines
7.2 KiB
C++
299 lines
7.2 KiB
C++
/*
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* 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., 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.
|
|
*
|
|
* The Original Code is: all of this file.
|
|
*
|
|
* Contributor(s): none yet.
|
|
*
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
*/
|
|
|
|
/** \file gameengine/GameLogic/SCA_IController.cpp
|
|
* \ingroup gamelogic
|
|
*/
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include "SCA_IController.h"
|
|
#include "SCA_LogicManager.h"
|
|
#include "SCA_IActuator.h"
|
|
#include "SCA_ISensor.h"
|
|
#include "EXP_PyObjectPlus.h"
|
|
#include "EXP_ListWrapper.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
SCA_IController::SCA_IController(SCA_IObject* gameobj)
|
|
:
|
|
SCA_ILogicBrick(gameobj),
|
|
m_statemask(0),
|
|
m_justActivated(false)
|
|
{
|
|
}
|
|
|
|
|
|
|
|
SCA_IController::~SCA_IController()
|
|
{
|
|
//UnlinkAllActuators();
|
|
}
|
|
|
|
|
|
|
|
std::vector<class SCA_ISensor*>& SCA_IController::GetLinkedSensors()
|
|
{
|
|
return m_linkedsensors;
|
|
}
|
|
|
|
|
|
|
|
std::vector<class SCA_IActuator*>& SCA_IController::GetLinkedActuators()
|
|
{
|
|
return m_linkedactuators;
|
|
}
|
|
|
|
|
|
|
|
void SCA_IController::UnlinkAllSensors()
|
|
{
|
|
std::vector<class SCA_ISensor*>::iterator sensit;
|
|
for (sensit = m_linkedsensors.begin();!(sensit==m_linkedsensors.end());++sensit)
|
|
{
|
|
if (IsActive())
|
|
{
|
|
(*sensit)->DecLink();
|
|
}
|
|
(*sensit)->UnlinkController(this);
|
|
}
|
|
m_linkedsensors.clear();
|
|
}
|
|
|
|
|
|
|
|
void SCA_IController::UnlinkAllActuators()
|
|
{
|
|
std::vector<class SCA_IActuator*>::iterator actit;
|
|
for (actit = m_linkedactuators.begin();!(actit==m_linkedactuators.end());++actit)
|
|
{
|
|
if (IsActive())
|
|
{
|
|
(*actit)->DecLink();
|
|
}
|
|
(*actit)->UnlinkController(this);
|
|
}
|
|
m_linkedactuators.clear();
|
|
}
|
|
|
|
void SCA_IController::LinkToActuator(SCA_IActuator* actua)
|
|
{
|
|
m_linkedactuators.push_back(actua);
|
|
if (IsActive())
|
|
{
|
|
actua->IncLink();
|
|
}
|
|
}
|
|
|
|
void SCA_IController::UnlinkActuator(class SCA_IActuator* actua)
|
|
{
|
|
std::vector<class SCA_IActuator*>::iterator actit;
|
|
for (actit = m_linkedactuators.begin();!(actit==m_linkedactuators.end());++actit)
|
|
{
|
|
if ((*actit) == actua)
|
|
{
|
|
if (IsActive())
|
|
{
|
|
(*actit)->DecLink();
|
|
}
|
|
*actit = m_linkedactuators.back();
|
|
m_linkedactuators.pop_back();
|
|
return;
|
|
}
|
|
}
|
|
printf("Missing link from controller %s:%s to actuator %s:%s\n",
|
|
m_gameobj->GetName().ReadPtr(), GetName().ReadPtr(),
|
|
actua->GetParent()->GetName().ReadPtr(), actua->GetName().ReadPtr());
|
|
}
|
|
|
|
void SCA_IController::LinkToSensor(SCA_ISensor* sensor)
|
|
{
|
|
m_linkedsensors.push_back(sensor);
|
|
if (IsActive())
|
|
{
|
|
sensor->IncLink();
|
|
}
|
|
}
|
|
|
|
void SCA_IController::UnlinkSensor(class SCA_ISensor* sensor)
|
|
{
|
|
std::vector<class SCA_ISensor*>::iterator sensit;
|
|
for (sensit = m_linkedsensors.begin();!(sensit==m_linkedsensors.end());++sensit)
|
|
{
|
|
if ((*sensit) == sensor)
|
|
{
|
|
if (IsActive())
|
|
{
|
|
sensor->DecLink();
|
|
}
|
|
*sensit = m_linkedsensors.back();
|
|
m_linkedsensors.pop_back();
|
|
return;
|
|
}
|
|
}
|
|
printf("Missing link from controller %s:%s to sensor %s:%s\n",
|
|
m_gameobj->GetName().ReadPtr(), GetName().ReadPtr(),
|
|
sensor->GetParent()->GetName().ReadPtr(), sensor->GetName().ReadPtr());
|
|
}
|
|
|
|
|
|
void SCA_IController::ApplyState(unsigned int state)
|
|
{
|
|
std::vector<class SCA_IActuator*>::iterator actit;
|
|
std::vector<class SCA_ISensor*>::iterator sensit;
|
|
|
|
if (m_statemask & state)
|
|
{
|
|
if (!IsActive())
|
|
{
|
|
// reactive the controller, all the links to actuator are valid again
|
|
for (actit = m_linkedactuators.begin();!(actit==m_linkedactuators.end());++actit)
|
|
{
|
|
(*actit)->IncLink();
|
|
}
|
|
|
|
for (sensit = m_linkedsensors.begin();!(sensit==m_linkedsensors.end());++sensit)
|
|
{
|
|
(*sensit)->IncLink();
|
|
}
|
|
SetActive(true);
|
|
m_justActivated = true;
|
|
}
|
|
} else if (IsActive())
|
|
{
|
|
for (actit = m_linkedactuators.begin();!(actit==m_linkedactuators.end());++actit)
|
|
{
|
|
(*actit)->DecLink();
|
|
}
|
|
for (sensit = m_linkedsensors.begin();!(sensit==m_linkedsensors.end());++sensit)
|
|
{
|
|
(*sensit)->DecLink();
|
|
}
|
|
SetActive(false);
|
|
m_justActivated = false;
|
|
}
|
|
}
|
|
|
|
#ifdef WITH_PYTHON
|
|
|
|
/* Python api */
|
|
|
|
PyTypeObject SCA_IController::Type = {
|
|
PyVarObject_HEAD_INIT(NULL, 0)
|
|
"SCA_IController",
|
|
sizeof(PyObjectPlus_Proxy),
|
|
0,
|
|
py_base_dealloc,
|
|
0,
|
|
0,
|
|
0,
|
|
0,
|
|
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_ILogicBrick::Type,
|
|
0,0,0,0,0,0,
|
|
py_base_new
|
|
};
|
|
|
|
PyMethodDef SCA_IController::Methods[] = {
|
|
{NULL,NULL} //Sentinel
|
|
};
|
|
|
|
PyAttributeDef SCA_IController::Attributes[] = {
|
|
KX_PYATTRIBUTE_RO_FUNCTION("state", SCA_IController, pyattr_get_state),
|
|
KX_PYATTRIBUTE_RO_FUNCTION("sensors", SCA_IController, pyattr_get_sensors),
|
|
KX_PYATTRIBUTE_RO_FUNCTION("actuators", SCA_IController, pyattr_get_actuators),
|
|
KX_PYATTRIBUTE_BOOL_RW("useHighPriority",SCA_IController,m_bookmark),
|
|
{ NULL } //Sentinel
|
|
};
|
|
|
|
PyObject *SCA_IController::pyattr_get_state(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
|
|
{
|
|
SCA_IController* self = static_cast<SCA_IController*>(self_v);
|
|
return PyLong_FromLong(self->m_statemask);
|
|
}
|
|
|
|
static int sca_icontroller_get_sensors_size_cb(void *self_v)
|
|
{
|
|
return ((SCA_IController *)self_v)->GetLinkedSensors().size();
|
|
}
|
|
|
|
static PyObject *sca_icontroller_get_sensors_item_cb(void *self_v, int index)
|
|
{
|
|
return ((SCA_IController *)self_v)->GetLinkedSensors()[index]->GetProxy();
|
|
}
|
|
|
|
static const char *sca_icontroller_get_sensors_item_name_cb(void *self_v, int index)
|
|
{
|
|
return ((SCA_IController *)self_v)->GetLinkedSensors()[index]->GetName().ReadPtr();
|
|
}
|
|
|
|
PyObject *SCA_IController::pyattr_get_sensors(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
|
|
{
|
|
return (new CListWrapper(self_v,
|
|
((SCA_IController *)self_v)->GetProxy(),
|
|
NULL,
|
|
sca_icontroller_get_sensors_size_cb,
|
|
sca_icontroller_get_sensors_item_cb,
|
|
sca_icontroller_get_sensors_item_name_cb,
|
|
NULL))->NewProxy(true);
|
|
}
|
|
|
|
static int sca_icontroller_get_actuators_size_cb(void *self_v)
|
|
{
|
|
return ((SCA_IController *)self_v)->GetLinkedActuators().size();
|
|
}
|
|
|
|
static PyObject *sca_icontroller_get_actuators_item_cb(void *self_v, int index)
|
|
{
|
|
return ((SCA_IController *)self_v)->GetLinkedActuators()[index]->GetProxy();
|
|
}
|
|
|
|
static const char *sca_icontroller_get_actuators_item_name_cb(void *self_v, int index)
|
|
{
|
|
return ((SCA_IController *)self_v)->GetLinkedActuators()[index]->GetName().ReadPtr();
|
|
}
|
|
|
|
PyObject *SCA_IController::pyattr_get_actuators(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
|
|
{
|
|
return (new CListWrapper(self_v,
|
|
((SCA_IController *)self_v)->GetProxy(),
|
|
NULL,
|
|
sca_icontroller_get_actuators_size_cb,
|
|
sca_icontroller_get_actuators_item_cb,
|
|
sca_icontroller_get_actuators_item_name_cb,
|
|
NULL))->NewProxy(true);
|
|
}
|
|
#endif // WITH_PYTHON
|