Python updates:
[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/BL DUAL 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. The Blender
12 // Foundation also sells licenses for use in proprietary software under
13 // the Blender License.  See http://www.blender.org/BL/ for information
14 // about this.
15 //
16 // This program is distributed in the hope that it will be useful,
17 // but WITHOUT ANY WARRANTY; without even the implied warranty of
18 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 // GNU General Public License for more details.
20 //
21 // You should have received a copy of the GNU General Public License
22 // along with this program; if not, write to the Free Software Foundation,
23 // Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24 //
25 // The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26 // All rights reserved.
27 //
28 // The Original Code is: all of this file.
29 //
30 // Contributor(s): none yet.
31 //
32 // ***** END GPL/BL DUAL LICENSE BLOCK *****
33
34 //
35 // Previously existed as:
36
37 // \source\gameengine\GameLogic\SCA_ReplaceMeshActuator.cpp
38
39 // Please look here for revision history.
40
41 #include "KX_SCA_ReplaceMeshActuator.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::
56
57 Type = {
58         PyObject_HEAD_INIT(&PyType_Type)
59         0,
60         "KX_SCA_ReplaceMeshActuator",
61         sizeof(KX_SCA_ReplaceMeshActuator),
62         0,
63         PyDestructor,
64         0,
65         __getattr,
66         __setattr,
67         0, 
68         __repr,
69         0, 
70         0,
71         0,
72         0,
73         0
74 };
75
76 PyParentObject KX_SCA_ReplaceMeshActuator::Parents[] = {
77         &SCA_IActuator::Type,
78         &SCA_ILogicBrick::Type,
79         &CValue::Type,
80         NULL
81 };
82
83
84
85 PyMethodDef KX_SCA_ReplaceMeshActuator::Methods[] = {
86   {"setMesh", (PyCFunction) KX_SCA_ReplaceMeshActuator::sPySetMesh, METH_VARARGS, SetMesh_doc},
87   KX_PYMETHODTABLE(KX_SCA_ReplaceMeshActuator, getMesh),
88   {NULL,NULL} //Sentinel
89 };
90
91
92
93 PyObject* KX_SCA_ReplaceMeshActuator::_getattr(const STR_String& attr)
94 {
95   _getattr_up(SCA_IActuator);
96 }
97
98
99
100 /* 1. setMesh */
101 char KX_SCA_ReplaceMeshActuator::SetMesh_doc[] = 
102         "setMesh(name)\n"
103         "\t- name: string\n"
104         "\tSet the mesh that will be substituted for the current one.\n";
105
106 PyObject* KX_SCA_ReplaceMeshActuator::PySetMesh(PyObject* self,
107                                                                           PyObject* args, 
108                                                                           PyObject* kwds)
109 {
110         char* meshname;
111         
112         if (!PyArg_ParseTuple(args, "s", &meshname))
113         {
114                 return NULL;    
115         }
116
117         void* mesh = SCA_ILogicBrick::m_sCurrentLogicManager->GetMeshByName(STR_String(meshname));
118         
119         if (m_mesh) {
120                 m_mesh= (class RAS_MeshObject*)mesh;
121                 Py_Return;
122         }
123         
124         return NULL;
125 }
126
127 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, getMesh,
128 "getMesh() -> string\n"
129 "Returns the name of the mesh to be substituted.\n"
130 )
131 {
132         return PyString_FromString(const_cast<char *>(m_mesh->GetName().ReadPtr()));
133 }
134
135 /* ------------------------------------------------------------------------- */
136 /* Native functions                                                          */
137 /* ------------------------------------------------------------------------- */
138
139 KX_SCA_ReplaceMeshActuator::KX_SCA_ReplaceMeshActuator(SCA_IObject *gameobj,
140                                                                                                            class RAS_MeshObject *mesh,
141                                                                                                            SCA_IScene* scene,
142                                                                                                            PyTypeObject* T) : 
143
144         SCA_IActuator(gameobj, T),
145         m_mesh(mesh),
146         m_scene(scene)
147 {
148 } /* End of constructor */
149
150
151
152 KX_SCA_ReplaceMeshActuator::~KX_SCA_ReplaceMeshActuator()
153
154         // there's nothing to be done here, really....
155 } /* end of destructor */
156
157
158
159 bool KX_SCA_ReplaceMeshActuator::Update(double curtime,
160                                                                                 double deltatime)
161 {
162         bool result = false;    
163         bool bNegativeEvent = IsNegativeEvent();
164         RemoveAllEvents();
165
166         if (bNegativeEvent)
167                 return false; // do nothing on negative events
168
169         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh);
170
171         return false;
172 }
173
174
175
176 CValue* KX_SCA_ReplaceMeshActuator::GetReplica()
177 {
178         KX_SCA_ReplaceMeshActuator* replica = 
179                 new KX_SCA_ReplaceMeshActuator(*this);
180
181         if (replica == NULL)
182                 return NULL;
183
184         replica->ProcessReplica();
185
186         // this will copy properties and so on...
187         CValue::AddDataToReplica(replica);
188
189         return replica;
190 };
191
192 /* eof */