svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[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 KX_SCA_ReplaceMeshActuator::Type = {
54 #if (PY_VERSION_HEX >= 0x02060000)
55         PyVarObject_HEAD_INIT(NULL, 0)
56 #else
57         /* python 2.5 and below */
58         PyObject_HEAD_INIT( NULL )  /* required py macro */
59         0,                          /* ob_size */
60 #endif
61         "KX_SCA_ReplaceMeshActuator",
62         sizeof(PyObjectPlus_Proxy),
63         0,
64         py_base_dealloc,
65         0,
66         0,
67         0,
68         0,
69         py_base_repr,
70         0,0,0,0,0,0,0,0,0,
71         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
72         0,0,0,0,0,0,0,
73         Methods,
74         0,
75         0,
76         &SCA_IActuator::Type,
77         0,0,0,0,0,0,
78         py_base_new
79 };
80
81 PyMethodDef KX_SCA_ReplaceMeshActuator::Methods[] = {
82         KX_PYMETHODTABLE(KX_SCA_ReplaceMeshActuator, instantReplaceMesh),
83         // Deprecated ----->
84         {"setMesh", (PyCFunction) KX_SCA_ReplaceMeshActuator::sPySetMesh, METH_O, (PY_METHODCHAR)SetMesh_doc},
85         KX_PYMETHODTABLE(KX_SCA_ReplaceMeshActuator, getMesh),
86         {NULL,NULL} //Sentinel
87 };
88
89 PyAttributeDef KX_SCA_ReplaceMeshActuator::Attributes[] = {
90         KX_PYATTRIBUTE_RW_FUNCTION("mesh", KX_SCA_ReplaceMeshActuator, pyattr_get_mesh, pyattr_set_mesh),
91         { NULL }        //Sentinel
92 };
93
94 PyObject* KX_SCA_ReplaceMeshActuator::pyattr_get_mesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
95 {
96         KX_SCA_ReplaceMeshActuator* actuator = static_cast<KX_SCA_ReplaceMeshActuator*>(self);
97         if (!actuator->m_mesh)
98                 Py_RETURN_NONE;
99         KX_MeshProxy* meshproxy = new KX_MeshProxy(actuator->m_mesh);
100         return meshproxy->NewProxy(true);
101 }
102
103 int KX_SCA_ReplaceMeshActuator::pyattr_set_mesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
104 {
105         KX_SCA_ReplaceMeshActuator* actuator = static_cast<KX_SCA_ReplaceMeshActuator*>(self);
106         RAS_MeshObject* new_mesh;
107         
108         if (!ConvertPythonToMesh(value, &new_mesh, true, "actuator.mesh = value: KX_SCA_ReplaceMeshActuator"))
109                 return PY_SET_ATTR_FAIL;
110         
111         actuator->m_mesh = new_mesh;
112         return PY_SET_ATTR_SUCCESS;
113 }
114
115 /* 1. setMesh */
116 const char KX_SCA_ReplaceMeshActuator::SetMesh_doc[] = 
117         "setMesh(name)\n"
118         "\t- name: string or None\n"
119         "\tSet the mesh that will be substituted for the current one.\n";
120
121 PyObject* KX_SCA_ReplaceMeshActuator::PySetMesh(PyObject* value)
122 {
123         ShowDeprecationWarning("setMesh()", "the mesh property");
124         RAS_MeshObject* new_mesh;
125         
126         if (!ConvertPythonToMesh(value, &new_mesh, true, "actuator.mesh = value: KX_SCA_ReplaceMeshActuator"))
127                 return NULL;
128         
129         m_mesh = new_mesh;
130         Py_RETURN_NONE;
131 }
132
133 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, getMesh,
134 "getMesh() -> string\n"
135 "Returns the name of the mesh to be substituted.\n"
136 )
137 {
138         ShowDeprecationWarning("getMesh()", "the mesh property");
139         if (!m_mesh)
140                 Py_RETURN_NONE;
141
142         return PyUnicode_FromString(const_cast<char *>(m_mesh->GetName().ReadPtr()));
143 }
144
145
146 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, instantReplaceMesh,
147 "instantReplaceMesh() : immediately replace mesh without delay\n")
148 {
149         InstantReplaceMesh();
150         Py_RETURN_NONE;
151 }
152
153 /* ------------------------------------------------------------------------- */
154 /* Native functions                                                          */
155 /* ------------------------------------------------------------------------- */
156
157 KX_SCA_ReplaceMeshActuator::KX_SCA_ReplaceMeshActuator(SCA_IObject *gameobj,
158                                                                                                            class RAS_MeshObject *mesh,
159                                                                                                            SCA_IScene* scene) :
160
161         SCA_IActuator(gameobj),
162         m_mesh(mesh),
163         m_scene(scene)
164 {
165 } /* End of constructor */
166
167
168
169 KX_SCA_ReplaceMeshActuator::~KX_SCA_ReplaceMeshActuator()
170
171         // there's nothing to be done here, really....
172 } /* end of destructor */
173
174
175
176 bool KX_SCA_ReplaceMeshActuator::Update()
177 {
178         // bool result = false; /*unused*/
179         bool bNegativeEvent = IsNegativeEvent();
180         RemoveAllEvents();
181
182         if (bNegativeEvent)
183                 return false; // do nothing on negative events
184
185         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh);
186
187         return false;
188 }
189
190
191
192 CValue* KX_SCA_ReplaceMeshActuator::GetReplica()
193 {
194         KX_SCA_ReplaceMeshActuator* replica = 
195                 new KX_SCA_ReplaceMeshActuator(*this);
196
197         if (replica == NULL)
198                 return NULL;
199
200         replica->ProcessReplica();
201
202         return replica;
203 };
204
205 void KX_SCA_ReplaceMeshActuator::InstantReplaceMesh()
206 {
207         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh);
208 }
209
210 /* eof */