BGE API cleanup: DynamicActuator, ReplaceMeshActuator, TrackToActuator.
[blender.git] / source / gameengine / Ketsji / KX_SCA_ReplaceMeshActuator.cpp
1 //
2 // Replace the mesh for this actuator's parent
3 //
4 // $Id$
5 //
6 // ***** BEGIN GPL LICENSE BLOCK *****
7 //
8 // This program is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU General Public License
10 // as published by the Free Software Foundation; either version 2
11 // of the License, or (at your option) any later version.
12 //
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software Foundation,
20 // Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21 //
22 // The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
23 // All rights reserved.
24 //
25 // The Original Code is: all of this file.
26 //
27 // Contributor(s): none yet.
28 //
29 // ***** END GPL LICENSE BLOCK *****
30
31 //
32 // Previously existed as:
33
34 // \source\gameengine\GameLogic\SCA_ReplaceMeshActuator.cpp
35
36 // Please look here for revision history.
37
38 #include "KX_SCA_ReplaceMeshActuator.h"
39
40 #include "PyObjectPlus.h" 
41
42 #ifdef HAVE_CONFIG_H
43 #include <config.h>
44 #endif
45
46 /* ------------------------------------------------------------------------- */
47 /* Python functions                                                          */
48 /* ------------------------------------------------------------------------- */
49
50 /* Integration hooks ------------------------------------------------------- */
51
52         PyTypeObject 
53
54 KX_SCA_ReplaceMeshActuator::
55
56 Type = {
57         PyObject_HEAD_INIT(&PyType_Type)
58         0,
59         "KX_SCA_ReplaceMeshActuator",
60         sizeof(KX_SCA_ReplaceMeshActuator),
61         0,
62         PyDestructor,
63         0,
64         __getattr,
65         __setattr,
66         0, 
67         __repr,
68         0, 
69         0,
70         0,
71         0,
72         0
73 };
74
75 PyParentObject KX_SCA_ReplaceMeshActuator::Parents[] = {
76         &SCA_IActuator::Type,
77         &SCA_ILogicBrick::Type,
78         &CValue::Type,
79         NULL
80 };
81
82
83
84 PyMethodDef KX_SCA_ReplaceMeshActuator::Methods[] = {
85         KX_PYMETHODTABLE(KX_SCA_ReplaceMeshActuator, instantReplaceMesh),
86         // Deprecated ----->
87         {"setMesh", (PyCFunction) KX_SCA_ReplaceMeshActuator::sPySetMesh, METH_O, (PY_METHODCHAR)SetMesh_doc},
88         KX_PYMETHODTABLE(KX_SCA_ReplaceMeshActuator, getMesh),
89         {NULL,NULL} //Sentinel
90 };
91
92 PyAttributeDef KX_SCA_ReplaceMeshActuator::Attributes[] = {
93         KX_PYATTRIBUTE_RW_FUNCTION("mesh", KX_SCA_ReplaceMeshActuator, pyattr_get_mesh, pyattr_set_mesh),
94         { NULL }        //Sentinel
95 };
96
97 PyObject* KX_SCA_ReplaceMeshActuator::_getattr(const char *attr)
98 {
99         PyObject* object = _getattr_self(Attributes, this, attr);
100         if (object != NULL)
101                 return object;
102         _getattr_up(SCA_IActuator);
103 }
104
105 int KX_SCA_ReplaceMeshActuator::_setattr(const char *attr, PyObject* value) 
106 {
107         int ret = _setattr_self(Attributes, this, attr, value);
108         if (ret >= 0)
109                 return ret;
110         return SCA_IActuator::_setattr(attr, value);
111 }
112
113 PyObject* KX_SCA_ReplaceMeshActuator::pyattr_get_mesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
114 {
115         KX_SCA_ReplaceMeshActuator* actuator = static_cast<KX_SCA_ReplaceMeshActuator*>(self);
116         if (!actuator->m_mesh)
117                 Py_RETURN_NONE;
118         return PyString_FromString(const_cast<char *>(actuator->m_mesh->GetName().ReadPtr()));
119 }
120
121 int KX_SCA_ReplaceMeshActuator::pyattr_set_mesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
122 {
123         KX_SCA_ReplaceMeshActuator* actuator = static_cast<KX_SCA_ReplaceMeshActuator*>(self);
124         if (value == Py_None) {
125                 actuator->m_mesh = NULL;
126         } else {
127                 char* meshname = PyString_AsString(value);
128                 if (!meshname) {
129                         PyErr_SetString(PyExc_ValueError, "Expected the name of a mesh or None");
130                         return 1;
131                 }
132                 void* mesh = SCA_ILogicBrick::m_sCurrentLogicManager->GetMeshByName(STR_String(meshname));
133                 if (mesh==NULL) {
134                         PyErr_SetString(PyExc_ValueError, "The mesh name given does not exist");
135                         return 1;
136                 }
137                 actuator->m_mesh= (class RAS_MeshObject*)mesh;
138         }
139         return 0;
140 }
141
142 /* 1. setMesh */
143 const char KX_SCA_ReplaceMeshActuator::SetMesh_doc[] = 
144         "setMesh(name)\n"
145         "\t- name: string or None\n"
146         "\tSet the mesh that will be substituted for the current one.\n";
147
148 PyObject* KX_SCA_ReplaceMeshActuator::PySetMesh(PyObject* self, PyObject* value)
149 {
150         ShowDeprecationWarning("setMesh()", "the mesh property");
151         if (value == Py_None) {
152                 m_mesh = NULL;
153         } else {
154                 char* meshname = PyString_AsString(value);
155                 if (!meshname) {
156                         PyErr_SetString(PyExc_ValueError, "Expected the name of a mesh or None");
157                         return NULL;
158                 }
159                 void* mesh = SCA_ILogicBrick::m_sCurrentLogicManager->GetMeshByName(STR_String(meshname));
160                 
161                 if (mesh==NULL) {
162                         PyErr_SetString(PyExc_ValueError, "The mesh name given does not exist");
163                         return NULL;
164                 }
165                 m_mesh= (class RAS_MeshObject*)mesh;
166         }
167         
168         Py_RETURN_NONE;
169 }
170
171 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, getMesh,
172 "getMesh() -> string\n"
173 "Returns the name of the mesh to be substituted.\n"
174 )
175 {
176         ShowDeprecationWarning("getMesh()", "the mesh property");
177         if (!m_mesh)
178                 Py_RETURN_NONE;
179
180         return PyString_FromString(const_cast<char *>(m_mesh->GetName().ReadPtr()));
181 }
182
183
184 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, instantReplaceMesh,
185 "instantReplaceMesh() : immediately replace mesh without delay\n")
186 {
187         InstantReplaceMesh();
188         Py_RETURN_NONE;
189 }
190
191 /* ------------------------------------------------------------------------- */
192 /* Native functions                                                          */
193 /* ------------------------------------------------------------------------- */
194
195 KX_SCA_ReplaceMeshActuator::KX_SCA_ReplaceMeshActuator(SCA_IObject *gameobj,
196                                                                                                            class RAS_MeshObject *mesh,
197                                                                                                            SCA_IScene* scene,
198                                                                                                            PyTypeObject* T) : 
199
200         SCA_IActuator(gameobj, T),
201         m_mesh(mesh),
202         m_scene(scene)
203 {
204 } /* End of constructor */
205
206
207
208 KX_SCA_ReplaceMeshActuator::~KX_SCA_ReplaceMeshActuator()
209
210         // there's nothing to be done here, really....
211 } /* end of destructor */
212
213
214
215 bool KX_SCA_ReplaceMeshActuator::Update()
216 {
217         // bool result = false; /*unused*/
218         bool bNegativeEvent = IsNegativeEvent();
219         RemoveAllEvents();
220
221         if (bNegativeEvent)
222                 return false; // do nothing on negative events
223
224         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh);
225
226         return false;
227 }
228
229
230
231 CValue* KX_SCA_ReplaceMeshActuator::GetReplica()
232 {
233         KX_SCA_ReplaceMeshActuator* replica = 
234                 new KX_SCA_ReplaceMeshActuator(*this);
235
236         if (replica == NULL)
237                 return NULL;
238
239         replica->ProcessReplica();
240
241         // this will copy properties and so on...
242         CValue::AddDataToReplica(replica);
243
244         return replica;
245 };
246
247 void KX_SCA_ReplaceMeshActuator::InstantReplaceMesh()
248 {
249         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh);
250 }
251
252 /* eof */