BGE Py API
[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 #ifdef HAVE_CONFIG_H
41 #include <config.h>
42 #endif
43
44 /* ------------------------------------------------------------------------- */
45 /* Python functions                                                          */
46 /* ------------------------------------------------------------------------- */
47
48 /* Integration hooks ------------------------------------------------------- */
49
50         PyTypeObject 
51
52 KX_SCA_ReplaceMeshActuator::
53
54 Type = {
55         PyObject_HEAD_INIT(&PyType_Type)
56         0,
57         "KX_SCA_ReplaceMeshActuator",
58         sizeof(KX_SCA_ReplaceMeshActuator),
59         0,
60         PyDestructor,
61         0,
62         __getattr,
63         __setattr,
64         0, 
65         __repr,
66         0, 
67         0,
68         0,
69         0,
70         0
71 };
72
73 PyParentObject KX_SCA_ReplaceMeshActuator::Parents[] = {
74         &SCA_IActuator::Type,
75         &SCA_ILogicBrick::Type,
76         &CValue::Type,
77         NULL
78 };
79
80
81
82 PyMethodDef KX_SCA_ReplaceMeshActuator::Methods[] = {
83         {"setMesh", (PyCFunction) KX_SCA_ReplaceMeshActuator::sPySetMesh, METH_O, SetMesh_doc},
84         
85         KX_PYMETHODTABLE(KX_SCA_ReplaceMeshActuator, instantReplaceMesh),
86         KX_PYMETHODTABLE(KX_SCA_ReplaceMeshActuator, getMesh),
87         {NULL,NULL} //Sentinel
88 };
89
90
91
92 PyObject* KX_SCA_ReplaceMeshActuator::_getattr(const STR_String& attr)
93 {
94   _getattr_up(SCA_IActuator);
95 }
96
97
98
99 /* 1. setMesh */
100 char KX_SCA_ReplaceMeshActuator::SetMesh_doc[] = 
101         "setMesh(name)\n"
102         "\t- name: string or None\n"
103         "\tSet the mesh that will be substituted for the current one.\n";
104
105 PyObject* KX_SCA_ReplaceMeshActuator::PySetMesh(PyObject* self, PyObject* value)
106 {
107         if (value == Py_None) {
108                 m_mesh = NULL;
109         } else {
110                 char* meshname = PyString_AsString(value);
111                 if (!meshname) {
112                         PyErr_SetString(PyExc_ValueError, "Expected the name of a mesh or None");
113                         return NULL;
114                 }
115                 void* mesh = SCA_ILogicBrick::m_sCurrentLogicManager->GetMeshByName(STR_String(meshname));
116                 
117                 if (mesh==NULL) {
118                         PyErr_SetString(PyExc_ValueError, "The mesh name given does not exist");
119                         return NULL;
120                 }
121                 m_mesh= (class RAS_MeshObject*)mesh;
122         }
123         
124         Py_RETURN_NONE;
125 }
126
127 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, getMesh,
128 "getMesh() -> string\n"
129 "Returns the name of the mesh to be substituted.\n"
130 )
131 {
132         if (!m_mesh)
133                 Py_RETURN_NONE;
134
135         return PyString_FromString(const_cast<char *>(m_mesh->GetName().ReadPtr()));
136 }
137
138
139 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, instantReplaceMesh,
140 "instantReplaceMesh() : immediately replace mesh without delay\n")
141 {
142         InstantReplaceMesh();
143         Py_RETURN_NONE;
144 }
145
146 /* ------------------------------------------------------------------------- */
147 /* Native functions                                                          */
148 /* ------------------------------------------------------------------------- */
149
150 KX_SCA_ReplaceMeshActuator::KX_SCA_ReplaceMeshActuator(SCA_IObject *gameobj,
151                                                                                                            class RAS_MeshObject *mesh,
152                                                                                                            SCA_IScene* scene,
153                                                                                                            PyTypeObject* T) : 
154
155         SCA_IActuator(gameobj, T),
156         m_mesh(mesh),
157         m_scene(scene)
158 {
159 } /* End of constructor */
160
161
162
163 KX_SCA_ReplaceMeshActuator::~KX_SCA_ReplaceMeshActuator()
164
165         // there's nothing to be done here, really....
166 } /* end of destructor */
167
168
169
170 bool KX_SCA_ReplaceMeshActuator::Update()
171 {
172         // bool result = false; /*unused*/
173         bool bNegativeEvent = IsNegativeEvent();
174         RemoveAllEvents();
175
176         if (bNegativeEvent)
177                 return false; // do nothing on negative events
178
179         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh);
180
181         return false;
182 }
183
184
185
186 CValue* KX_SCA_ReplaceMeshActuator::GetReplica()
187 {
188         KX_SCA_ReplaceMeshActuator* replica = 
189                 new KX_SCA_ReplaceMeshActuator(*this);
190
191         if (replica == NULL)
192                 return NULL;
193
194         replica->ProcessReplica();
195
196         // this will copy properties and so on...
197         CValue::AddDataToReplica(replica);
198
199         return replica;
200 };
201
202 void KX_SCA_ReplaceMeshActuator::InstantReplaceMesh()
203 {
204         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh);
205 }
206
207 /* eof */