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