remove python2.x support
[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         PyVarObject_HEAD_INIT(NULL, 0)
55         "KX_SCA_ReplaceMeshActuator",
56         sizeof(PyObjectPlus_Proxy),
57         0,
58         py_base_dealloc,
59         0,
60         0,
61         0,
62         0,
63         py_base_repr,
64         0,0,0,0,0,0,0,0,0,
65         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
66         0,0,0,0,0,0,0,
67         Methods,
68         0,
69         0,
70         &SCA_IActuator::Type,
71         0,0,0,0,0,0,
72         py_base_new
73 };
74
75 PyMethodDef KX_SCA_ReplaceMeshActuator::Methods[] = {
76         KX_PYMETHODTABLE(KX_SCA_ReplaceMeshActuator, instantReplaceMesh),
77         // Deprecated ----->
78         {"setMesh", (PyCFunction) KX_SCA_ReplaceMeshActuator::sPySetMesh, METH_O, (const char *)SetMesh_doc},
79         KX_PYMETHODTABLE(KX_SCA_ReplaceMeshActuator, getMesh),
80         {NULL,NULL} //Sentinel
81 };
82
83 PyAttributeDef KX_SCA_ReplaceMeshActuator::Attributes[] = {
84         KX_PYATTRIBUTE_RW_FUNCTION("mesh", KX_SCA_ReplaceMeshActuator, pyattr_get_mesh, pyattr_set_mesh),
85         KX_PYATTRIBUTE_BOOL_RW    ("useDisplayMesh", KX_SCA_ReplaceMeshActuator, m_use_gfx),
86         KX_PYATTRIBUTE_BOOL_RW    ("usePhysicsMesh", KX_SCA_ReplaceMeshActuator, m_use_phys),
87         { NULL }        //Sentinel
88 };
89
90 PyObject* KX_SCA_ReplaceMeshActuator::pyattr_get_mesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
91 {
92         KX_SCA_ReplaceMeshActuator* actuator = static_cast<KX_SCA_ReplaceMeshActuator*>(self);
93         if (!actuator->m_mesh)
94                 Py_RETURN_NONE;
95         KX_MeshProxy* meshproxy = new KX_MeshProxy(actuator->m_mesh);
96         return meshproxy->NewProxy(true);
97 }
98
99 int KX_SCA_ReplaceMeshActuator::pyattr_set_mesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
100 {
101         KX_SCA_ReplaceMeshActuator* actuator = static_cast<KX_SCA_ReplaceMeshActuator*>(self);
102         RAS_MeshObject* new_mesh;
103         
104         if (!ConvertPythonToMesh(value, &new_mesh, true, "actuator.mesh = value: KX_SCA_ReplaceMeshActuator"))
105                 return PY_SET_ATTR_FAIL;
106         
107         actuator->m_mesh = new_mesh;
108         return PY_SET_ATTR_SUCCESS;
109 }
110
111 /* 1. setMesh */
112 const char KX_SCA_ReplaceMeshActuator::SetMesh_doc[] = 
113         "setMesh(name)\n"
114         "\t- name: string or None\n"
115         "\tSet the mesh that will be substituted for the current one.\n";
116
117 PyObject* KX_SCA_ReplaceMeshActuator::PySetMesh(PyObject* value)
118 {
119         ShowDeprecationWarning("setMesh()", "the mesh property");
120         RAS_MeshObject* new_mesh;
121         
122         if (!ConvertPythonToMesh(value, &new_mesh, true, "actuator.mesh = value: KX_SCA_ReplaceMeshActuator"))
123                 return NULL;
124         
125         m_mesh = new_mesh;
126         Py_RETURN_NONE;
127 }
128
129 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, getMesh,
130 "getMesh() -> string\n"
131 "Returns the name of the mesh to be substituted.\n"
132 )
133 {
134         ShowDeprecationWarning("getMesh()", "the mesh property");
135         if (!m_mesh)
136                 Py_RETURN_NONE;
137
138         return PyUnicode_FromString(const_cast<char *>(m_mesh->GetName().ReadPtr()));
139 }
140
141
142 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, instantReplaceMesh,
143 "instantReplaceMesh() : immediately replace mesh without delay\n")
144 {
145         InstantReplaceMesh();
146         Py_RETURN_NONE;
147 }
148
149 /* ------------------------------------------------------------------------- */
150 /* Native functions                                                          */
151 /* ------------------------------------------------------------------------- */
152
153 KX_SCA_ReplaceMeshActuator::KX_SCA_ReplaceMeshActuator(SCA_IObject *gameobj,
154                                                                                                            class RAS_MeshObject *mesh,
155                                                                                                            SCA_IScene* scene,
156                                                                                                            bool use_gfx,
157                                                                                                            bool use_phys) :
158
159         SCA_IActuator(gameobj),
160         m_mesh(mesh),
161         m_scene(scene),
162         m_use_gfx(use_gfx),
163         m_use_phys(use_phys)
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_use_phys) /* NULL mesh is ok if were updating physics */
186                 m_scene->ReplaceMesh(GetParent(),m_mesh, m_use_gfx, m_use_phys);
187
188         return false;
189 }
190
191
192
193 CValue* KX_SCA_ReplaceMeshActuator::GetReplica()
194 {
195         KX_SCA_ReplaceMeshActuator* replica = 
196                 new KX_SCA_ReplaceMeshActuator(*this);
197
198         if (replica == NULL)
199                 return NULL;
200
201         replica->ProcessReplica();
202
203         return replica;
204 };
205
206 void KX_SCA_ReplaceMeshActuator::InstantReplaceMesh()
207 {
208         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh, m_use_gfx, m_use_phys);
209 }
210
211 /* eof */