Merge from trunk 16122-16307
[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_VARARGS, 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\n"
103         "\tSet the mesh that will be substituted for the current one.\n";
104
105 PyObject* KX_SCA_ReplaceMeshActuator::PySetMesh(PyObject* self,
106                                                                           PyObject* args, 
107                                                                           PyObject* kwds)
108 {
109         char* meshname;
110         
111         if (!PyArg_ParseTuple(args, "s", &meshname))
112         {
113                 return NULL;    
114         }
115
116         void* mesh = SCA_ILogicBrick::m_sCurrentLogicManager->GetMeshByName(STR_String(meshname));
117         
118         if (mesh) {
119                 m_mesh= (class RAS_MeshObject*)mesh;
120                 Py_Return;
121         }
122         
123         return NULL;
124 }
125
126 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, getMesh,
127 "getMesh() -> string\n"
128 "Returns the name of the mesh to be substituted.\n"
129 )
130 {
131         if (!m_mesh)
132                 Py_Return;
133
134         return PyString_FromString(const_cast<char *>(m_mesh->GetName().ReadPtr()));
135 }
136
137
138 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, instantReplaceMesh,
139 "instantReplaceMesh() : immediately replace mesh without delay\n")
140 {
141         InstantReplaceMesh();
142         Py_Return;
143 }
144
145 /* ------------------------------------------------------------------------- */
146 /* Native functions                                                          */
147 /* ------------------------------------------------------------------------- */
148
149 KX_SCA_ReplaceMeshActuator::KX_SCA_ReplaceMeshActuator(SCA_IObject *gameobj,
150                                                                                                            class RAS_MeshObject *mesh,
151                                                                                                            SCA_IScene* scene,
152                                                                                                            PyTypeObject* T) : 
153
154         SCA_IActuator(gameobj, T),
155         m_mesh(mesh),
156         m_scene(scene)
157 {
158 } /* End of constructor */
159
160
161
162 KX_SCA_ReplaceMeshActuator::~KX_SCA_ReplaceMeshActuator()
163
164         // there's nothing to be done here, really....
165 } /* end of destructor */
166
167
168
169 bool KX_SCA_ReplaceMeshActuator::Update()
170 {
171         // bool result = false; /*unused*/
172         bool bNegativeEvent = IsNegativeEvent();
173         RemoveAllEvents();
174
175         if (bNegativeEvent)
176                 return false; // do nothing on negative events
177
178         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh);
179
180         return false;
181 }
182
183
184
185 CValue* KX_SCA_ReplaceMeshActuator::GetReplica()
186 {
187         KX_SCA_ReplaceMeshActuator* replica = 
188                 new KX_SCA_ReplaceMeshActuator(*this);
189
190         if (replica == NULL)
191                 return NULL;
192
193         replica->ProcessReplica();
194
195         // this will copy properties and so on...
196         CValue::AddDataToReplica(replica);
197
198         return replica;
199 };
200
201 void KX_SCA_ReplaceMeshActuator::InstantReplaceMesh()
202 {
203         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh);
204 }
205
206 /* eof */