2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r19323...
[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 #include "KX_MeshProxy.h"
40
41 #include "PyObjectPlus.h" 
42
43 #ifdef HAVE_CONFIG_H
44 #include <config.h>
45 #endif
46
47 /* ------------------------------------------------------------------------- */
48 /* Python functions                                                          */
49 /* ------------------------------------------------------------------------- */
50
51 /* Integration hooks ------------------------------------------------------- */
52
53         PyTypeObject 
54
55 KX_SCA_ReplaceMeshActuator::Type = {
56         PyObject_HEAD_INIT(NULL)
57         0,
58         "KX_SCA_ReplaceMeshActuator",
59         sizeof(PyObjectPlus_Proxy),
60         0,
61         py_base_dealloc,
62         0,
63         0,
64         0,
65         0,
66         py_base_repr,
67         0,0,0,0,0,0,
68         py_base_getattro,
69         py_base_setattro,
70         0,0,0,0,0,0,0,0,0,
71         Methods
72 };
73
74 PyParentObject KX_SCA_ReplaceMeshActuator::Parents[] = {
75         &KX_SCA_ReplaceMeshActuator::Type,
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::py_getattro(PyObject *attr)
98 {
99         py_getattro_up(SCA_IActuator);
100 }
101
102 int KX_SCA_ReplaceMeshActuator::py_setattro(PyObject *attr, PyObject* value) 
103 {
104         py_setattro_up(SCA_IActuator);
105 }
106
107 PyObject* KX_SCA_ReplaceMeshActuator::pyattr_get_mesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
108 {
109         KX_SCA_ReplaceMeshActuator* actuator = static_cast<KX_SCA_ReplaceMeshActuator*>(self);
110         if (!actuator->m_mesh)
111                 Py_RETURN_NONE;
112         KX_MeshProxy* meshproxy = new KX_MeshProxy(actuator->m_mesh);
113         return meshproxy->NewProxy(true);
114 }
115
116 int KX_SCA_ReplaceMeshActuator::pyattr_set_mesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
117 {
118         KX_SCA_ReplaceMeshActuator* actuator = static_cast<KX_SCA_ReplaceMeshActuator*>(self);
119         RAS_MeshObject* new_mesh;
120         
121         if (!ConvertPythonToMesh(value, &new_mesh, true, "actuator.mesh = value: KX_SCA_ReplaceMeshActuator"))
122                 return 1;
123         
124         actuator->m_mesh = new_mesh;
125         return 0;
126 }
127
128 /* 1. setMesh */
129 const char KX_SCA_ReplaceMeshActuator::SetMesh_doc[] = 
130         "setMesh(name)\n"
131         "\t- name: string or None\n"
132         "\tSet the mesh that will be substituted for the current one.\n";
133
134 PyObject* KX_SCA_ReplaceMeshActuator::PySetMesh(PyObject* value)
135 {
136         ShowDeprecationWarning("setMesh()", "the mesh property");
137         RAS_MeshObject* new_mesh;
138         
139         if (!ConvertPythonToMesh(value, &new_mesh, true, "actuator.mesh = value: KX_SCA_ReplaceMeshActuator"))
140                 return NULL;
141         
142         m_mesh = new_mesh;
143         Py_RETURN_NONE;
144 }
145
146 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, getMesh,
147 "getMesh() -> string\n"
148 "Returns the name of the mesh to be substituted.\n"
149 )
150 {
151         ShowDeprecationWarning("getMesh()", "the mesh property");
152         if (!m_mesh)
153                 Py_RETURN_NONE;
154
155         return PyString_FromString(const_cast<char *>(m_mesh->GetName().ReadPtr()));
156 }
157
158
159 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, instantReplaceMesh,
160 "instantReplaceMesh() : immediately replace mesh without delay\n")
161 {
162         InstantReplaceMesh();
163         Py_RETURN_NONE;
164 }
165
166 /* ------------------------------------------------------------------------- */
167 /* Native functions                                                          */
168 /* ------------------------------------------------------------------------- */
169
170 KX_SCA_ReplaceMeshActuator::KX_SCA_ReplaceMeshActuator(SCA_IObject *gameobj,
171                                                                                                            class RAS_MeshObject *mesh,
172                                                                                                            SCA_IScene* scene,
173                                                                                                            PyTypeObject* T) : 
174
175         SCA_IActuator(gameobj, T),
176         m_mesh(mesh),
177         m_scene(scene)
178 {
179 } /* End of constructor */
180
181
182
183 KX_SCA_ReplaceMeshActuator::~KX_SCA_ReplaceMeshActuator()
184
185         // there's nothing to be done here, really....
186 } /* end of destructor */
187
188
189
190 bool KX_SCA_ReplaceMeshActuator::Update()
191 {
192         // bool result = false; /*unused*/
193         bool bNegativeEvent = IsNegativeEvent();
194         RemoveAllEvents();
195
196         if (bNegativeEvent)
197                 return false; // do nothing on negative events
198
199         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh);
200
201         return false;
202 }
203
204
205
206 CValue* KX_SCA_ReplaceMeshActuator::GetReplica()
207 {
208         KX_SCA_ReplaceMeshActuator* replica = 
209                 new KX_SCA_ReplaceMeshActuator(*this);
210
211         if (replica == NULL)
212                 return NULL;
213
214         replica->ProcessReplica();
215
216         // this will copy properties and so on...
217         CValue::AddDataToReplica(replica);
218
219         return replica;
220 };
221
222 void KX_SCA_ReplaceMeshActuator::InstantReplaceMesh()
223 {
224         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh);
225 }
226
227 /* eof */