BGE: Adding a Python interface for handling joysticks without needing logic bricks...
[blender.git] / source / gameengine / Ketsji / KX_PythonInitTypes.cpp
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): Campbell Barton
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file gameengine/Ketsji/KX_PythonInitTypes.cpp
29  *  \ingroup ketsji
30  */
31
32 #ifdef WITH_PYTHON
33
34 #include "KX_PythonInitTypes.h"
35
36 /* Only for Class::Parents */
37 #include "BL_BlenderShader.h"
38 #include "BL_ShapeActionActuator.h"
39 #include "BL_ArmatureActuator.h"
40 #include "BL_ArmatureConstraint.h"
41 #include "BL_ArmatureObject.h"
42 #include "BL_ArmatureChannel.h"
43 #include "KX_ArmatureSensor.h"
44 #include "KX_BlenderMaterial.h"
45 #include "KX_CameraActuator.h"
46 #include "KX_CharacterWrapper.h"
47 #include "KX_ConstraintActuator.h"
48 #include "KX_ConstraintWrapper.h"
49 #include "KX_GameActuator.h"
50 #include "KX_Light.h"
51 #include "KX_FontObject.h"
52 #include "KX_MeshProxy.h"
53 #include "KX_MouseFocusSensor.h"
54 #include "KX_NetworkMessageActuator.h"
55 #include "KX_NetworkMessageSensor.h"
56 #include "KX_ObjectActuator.h"
57 #include "KX_ParentActuator.h"
58 #include "KX_PolyProxy.h"
59 #include "KX_PolygonMaterial.h"
60 #include "KX_PythonSeq.h"
61 #include "KX_SCA_AddObjectActuator.h"
62 #include "KX_SCA_EndObjectActuator.h"
63 #include "KX_SCA_ReplaceMeshActuator.h"
64 #include "KX_SceneActuator.h"
65 #include "KX_StateActuator.h"
66 #include "KX_SteeringActuator.h"
67 #include "KX_TrackToActuator.h"
68 #include "KX_VehicleWrapper.h"
69 #include "KX_VertexProxy.h"
70 #include "SCA_2DFilterActuator.h"
71 #include "SCA_ANDController.h"
72 #include "SCA_ActuatorSensor.h"
73 #include "SCA_AlwaysSensor.h"
74 #include "SCA_DelaySensor.h"
75 #include "SCA_JoystickSensor.h"
76 #include "SCA_KeyboardSensor.h"
77 #include "SCA_MouseSensor.h"
78 #include "SCA_NANDController.h"
79 #include "SCA_NORController.h"
80 #include "SCA_ORController.h"
81 #include "SCA_RandomSensor.h"
82 #include "SCA_XNORController.h"
83 #include "SCA_XORController.h"
84 #include "SCA_PythonJoystick.h"
85 #include "SCA_PythonKeyboard.h"
86 #include "SCA_PythonMouse.h"
87 #include "KX_IpoActuator.h"
88 #include "KX_NearSensor.h"
89 #include "KX_RadarSensor.h"
90 #include "KX_RaySensor.h"
91 #include "KX_SCA_DynamicActuator.h"
92 #include "KX_SoundActuator.h"
93 #include "KX_TouchSensor.h"
94 #include "KX_VisibilityActuator.h"
95 #include "SCA_PropertySensor.h"
96 #include "SCA_PythonController.h"
97 #include "SCA_RandomActuator.h"
98 #include "SCA_IController.h"
99 #include "KX_NavMeshObject.h"
100
101 static void PyType_Attr_Set(PyGetSetDef *attr_getset, PyAttributeDef *attr)
102 {
103         attr_getset->name= (char *)attr->m_name;
104         attr_getset->doc= NULL;
105
106         attr_getset->get= reinterpret_cast<getter>(PyObjectPlus::py_get_attrdef);
107
108         if (attr->m_access==KX_PYATTRIBUTE_RO)
109                 attr_getset->set= NULL;
110         else
111                 attr_getset->set= reinterpret_cast<setter>(PyObjectPlus::py_set_attrdef);
112
113         attr_getset->closure= reinterpret_cast<void *>(attr);
114 }
115
116 static void PyType_Ready_ADD(PyObject *dict, PyTypeObject *tp, PyAttributeDef *attributes, PyAttributeDef *attributesPtr, int init_getset)
117 {
118         PyAttributeDef *attr;
119
120         if (init_getset) {
121                 /* we need to do this for all types before calling PyType_Ready
122                  * since they will call the parents PyType_Ready and those might not have initialized vars yet */
123
124                 //if (tp->tp_base==NULL)
125                 //      printf("Debug: No Parents - '%s'\n" , tp->tp_name);
126
127                 if (tp->tp_getset==NULL && ((attributes && attributes->m_name) || (attributesPtr && attributesPtr->m_name))) {
128                         PyGetSetDef *attr_getset;
129                         int attr_tot= 0;
130
131                         if (attributes) {
132                                 for (attr= attributes; attr->m_name; attr++, attr_tot++)
133                                         attr->m_usePtr = false;
134                         }
135                         if (attributesPtr) {
136                                 for (attr= attributesPtr; attr->m_name; attr++, attr_tot++)
137                                         attr->m_usePtr = true;
138                         }
139
140                         tp->tp_getset = attr_getset = reinterpret_cast<PyGetSetDef *>(PyMem_Malloc((attr_tot+1) * sizeof(PyGetSetDef))); // XXX - Todo, free
141
142                         if (attributes) {
143                                 for (attr= attributes; attr->m_name; attr++, attr_getset++) {
144                                         PyType_Attr_Set(attr_getset, attr);
145                                 }
146                         }
147                         if (attributesPtr) {
148                                 for (attr= attributesPtr; attr->m_name; attr++, attr_getset++) {
149                                         PyType_Attr_Set(attr_getset, attr);
150                                 }
151                         }
152                         memset(attr_getset, 0, sizeof(PyGetSetDef));
153                 }
154         } else {
155                 PyType_Ready(tp);
156                 PyDict_SetItemString(dict, tp->tp_name, reinterpret_cast<PyObject *>(tp));
157         }
158         
159 }
160
161
162 #define PyType_Ready_Attr(d, n, i)   PyType_Ready_ADD(d, &n::Type, n::Attributes, NULL, i)
163 #define PyType_Ready_AttrPtr(d, n, i)   PyType_Ready_ADD(d, &n::Type, n::Attributes, n::AttributesPtr, i)
164
165 void initPyTypes(void)
166 {
167
168 /*
169  * initPyObjectPlusType(BL_ActionActuator::Parents);
170  * .....
171  */
172
173         /* For now just do PyType_Ready */
174         PyObject *mod= PyModule_New("GameTypes");
175         PyObject *dict= PyModule_GetDict(mod);
176         PyDict_SetItemString(PySys_GetObject((char *)"modules"), (char *)"GameTypes", mod);
177         Py_DECREF(mod);
178         
179         
180         for (int init_getset= 1; init_getset > -1; init_getset--) { /* run twice, once to init the getsets another to run PyType_Ready */
181                 PyType_Ready_Attr(dict, BL_ActionActuator, init_getset);
182                 PyType_Ready_Attr(dict, BL_Shader, init_getset);
183                 PyType_Ready_Attr(dict, BL_ShapeActionActuator, init_getset);
184                 PyType_Ready_Attr(dict, BL_ArmatureObject, init_getset);
185                 PyType_Ready_Attr(dict, BL_ArmatureActuator, init_getset);
186                 PyType_Ready_Attr(dict, BL_ArmatureConstraint, init_getset);
187                 PyType_Ready_AttrPtr(dict, BL_ArmatureBone, init_getset);
188                 PyType_Ready_AttrPtr(dict, BL_ArmatureChannel, init_getset);
189                 // PyType_Ready_Attr(dict, CPropValue, init_getset);  // doesn't use Py_Header
190                 PyType_Ready_Attr(dict, CListValue, init_getset);
191                 PyType_Ready_Attr(dict, CValue, init_getset);
192                 PyType_Ready_Attr(dict, KX_ArmatureSensor, init_getset);
193                 PyType_Ready_Attr(dict, KX_BlenderMaterial, init_getset);
194                 PyType_Ready_Attr(dict, KX_Camera, init_getset);
195                 PyType_Ready_Attr(dict, KX_CameraActuator, init_getset);
196                 PyType_Ready_Attr(dict, KX_CharacterWrapper, init_getset);
197                 PyType_Ready_Attr(dict, KX_ConstraintActuator, init_getset);
198                 PyType_Ready_Attr(dict, KX_ConstraintWrapper, init_getset);
199                 PyType_Ready_Attr(dict, KX_GameActuator, init_getset);
200                 PyType_Ready_Attr(dict, KX_GameObject, init_getset);
201                 PyType_Ready_Attr(dict, KX_IpoActuator, init_getset);
202                 PyType_Ready_Attr(dict, KX_LightObject, init_getset);
203                 PyType_Ready_Attr(dict, KX_FontObject, init_getset);
204                 PyType_Ready_Attr(dict, KX_MeshProxy, init_getset);
205                 PyType_Ready_Attr(dict, KX_MouseFocusSensor, init_getset);
206                 PyType_Ready_Attr(dict, KX_NearSensor, init_getset);
207                 PyType_Ready_Attr(dict, KX_NetworkMessageActuator, init_getset);
208                 PyType_Ready_Attr(dict, KX_NetworkMessageSensor, init_getset);
209                 PyType_Ready_Attr(dict, KX_ObjectActuator, init_getset);
210                 PyType_Ready_Attr(dict, KX_ParentActuator, init_getset);
211                 PyType_Ready_Attr(dict, KX_PolyProxy, init_getset);
212                 PyType_Ready_Attr(dict, KX_PolygonMaterial, init_getset);
213                 PyType_Ready_Attr(dict, KX_RadarSensor, init_getset);
214                 PyType_Ready_Attr(dict, KX_RaySensor, init_getset);
215                 PyType_Ready_Attr(dict, KX_SCA_AddObjectActuator, init_getset);
216                 PyType_Ready_Attr(dict, KX_SCA_DynamicActuator, init_getset);
217                 PyType_Ready_Attr(dict, KX_SCA_EndObjectActuator, init_getset);
218                 PyType_Ready_Attr(dict, KX_SCA_ReplaceMeshActuator, init_getset);
219                 PyType_Ready_Attr(dict, KX_Scene, init_getset);
220                 PyType_Ready_Attr(dict, KX_NavMeshObject, init_getset);
221                 PyType_Ready_Attr(dict, KX_SceneActuator, init_getset);
222                 PyType_Ready_Attr(dict, KX_SoundActuator, init_getset);
223                 PyType_Ready_Attr(dict, KX_StateActuator, init_getset);
224                 PyType_Ready_Attr(dict, KX_SteeringActuator, init_getset);
225                 PyType_Ready_Attr(dict, KX_TouchSensor, init_getset);
226                 PyType_Ready_Attr(dict, KX_TrackToActuator, init_getset);
227                 PyType_Ready_Attr(dict, KX_VehicleWrapper, init_getset);
228                 PyType_Ready_Attr(dict, KX_VertexProxy, init_getset);
229                 PyType_Ready_Attr(dict, KX_VisibilityActuator, init_getset);
230                 PyType_Ready_Attr(dict, PyObjectPlus, init_getset);
231                 PyType_Ready_Attr(dict, SCA_2DFilterActuator, init_getset);
232                 PyType_Ready_Attr(dict, SCA_ANDController, init_getset);
233                 // PyType_Ready_Attr(dict, SCA_Actuator, init_getset);  // doesn't use Py_Header
234                 PyType_Ready_Attr(dict, SCA_ActuatorSensor, init_getset);
235                 PyType_Ready_Attr(dict, SCA_AlwaysSensor, init_getset);
236                 PyType_Ready_Attr(dict, SCA_DelaySensor, init_getset);
237                 PyType_Ready_Attr(dict, SCA_ILogicBrick, init_getset);
238                 PyType_Ready_Attr(dict, SCA_IObject, init_getset);
239                 PyType_Ready_Attr(dict, SCA_ISensor, init_getset);
240                 PyType_Ready_Attr(dict, SCA_JoystickSensor, init_getset);
241                 PyType_Ready_Attr(dict, SCA_KeyboardSensor, init_getset);
242                 PyType_Ready_Attr(dict, SCA_MouseSensor, init_getset);
243                 PyType_Ready_Attr(dict, SCA_NANDController, init_getset);
244                 PyType_Ready_Attr(dict, SCA_NORController, init_getset);
245                 PyType_Ready_Attr(dict, SCA_ORController, init_getset);
246                 PyType_Ready_Attr(dict, SCA_PropertyActuator, init_getset);
247                 PyType_Ready_Attr(dict, SCA_PropertySensor, init_getset);
248                 PyType_Ready_Attr(dict, SCA_PythonController, init_getset);
249                 PyType_Ready_Attr(dict, SCA_RandomActuator, init_getset);
250                 PyType_Ready_Attr(dict, SCA_RandomSensor, init_getset);
251                 PyType_Ready_Attr(dict, SCA_XNORController, init_getset);
252                 PyType_Ready_Attr(dict, SCA_XORController, init_getset);
253                 PyType_Ready_Attr(dict, SCA_IController, init_getset);
254                 PyType_Ready_Attr(dict, SCA_PythonJoystick, init_getset);
255                 PyType_Ready_Attr(dict, SCA_PythonKeyboard, init_getset);
256                 PyType_Ready_Attr(dict, SCA_PythonMouse, init_getset);
257         }
258
259
260         /* Normal python type */
261         PyType_Ready(&KX_PythonSeq_Type);
262
263 #ifdef USE_MATHUTILS
264         /* Init mathutils callbacks */
265         KX_GameObject_Mathutils_Callback_Init();
266         KX_ObjectActuator_Mathutils_Callback_Init();
267 #endif
268 }
269
270 #endif // WITH_PYTHON