Merge of itasc branch. Project files, scons and cmake should be working. Makefile...
[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         {NULL,NULL} //Sentinel
78 };
79
80 PyAttributeDef KX_SCA_ReplaceMeshActuator::Attributes[] = {
81         KX_PYATTRIBUTE_RW_FUNCTION("mesh", KX_SCA_ReplaceMeshActuator, pyattr_get_mesh, pyattr_set_mesh),
82         KX_PYATTRIBUTE_BOOL_RW    ("useDisplayMesh", KX_SCA_ReplaceMeshActuator, m_use_gfx),
83         KX_PYATTRIBUTE_BOOL_RW    ("usePhysicsMesh", KX_SCA_ReplaceMeshActuator, m_use_phys),
84         { NULL }        //Sentinel
85 };
86
87 PyObject* KX_SCA_ReplaceMeshActuator::pyattr_get_mesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef)
88 {
89         KX_SCA_ReplaceMeshActuator* actuator = static_cast<KX_SCA_ReplaceMeshActuator*>(self);
90         if (!actuator->m_mesh)
91                 Py_RETURN_NONE;
92         KX_MeshProxy* meshproxy = new KX_MeshProxy(actuator->m_mesh);
93         return meshproxy->NewProxy(true);
94 }
95
96 int KX_SCA_ReplaceMeshActuator::pyattr_set_mesh(void *self, const struct KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
97 {
98         KX_SCA_ReplaceMeshActuator* actuator = static_cast<KX_SCA_ReplaceMeshActuator*>(self);
99         RAS_MeshObject* new_mesh;
100         
101         if (!ConvertPythonToMesh(value, &new_mesh, true, "actuator.mesh = value: KX_SCA_ReplaceMeshActuator"))
102                 return PY_SET_ATTR_FAIL;
103         
104         actuator->m_mesh = new_mesh;
105         return PY_SET_ATTR_SUCCESS;
106 }
107
108 KX_PYMETHODDEF_DOC(KX_SCA_ReplaceMeshActuator, instantReplaceMesh,
109 "instantReplaceMesh() : immediately replace mesh without delay\n")
110 {
111         InstantReplaceMesh();
112         Py_RETURN_NONE;
113 }
114
115 /* ------------------------------------------------------------------------- */
116 /* Native functions                                                          */
117 /* ------------------------------------------------------------------------- */
118
119 KX_SCA_ReplaceMeshActuator::KX_SCA_ReplaceMeshActuator(SCA_IObject *gameobj,
120                                                                                                            class RAS_MeshObject *mesh,
121                                                                                                            SCA_IScene* scene,
122                                                                                                            bool use_gfx,
123                                                                                                            bool use_phys) :
124
125         SCA_IActuator(gameobj, KX_ACT_REPLACE_MESH),
126         m_mesh(mesh),
127         m_scene(scene),
128         m_use_gfx(use_gfx),
129         m_use_phys(use_phys)
130 {
131 } /* End of constructor */
132
133
134
135 KX_SCA_ReplaceMeshActuator::~KX_SCA_ReplaceMeshActuator()
136
137         // there's nothing to be done here, really....
138 } /* end of destructor */
139
140
141
142 bool KX_SCA_ReplaceMeshActuator::Update()
143 {
144         // bool result = false; /*unused*/
145         bool bNegativeEvent = IsNegativeEvent();
146         RemoveAllEvents();
147
148         if (bNegativeEvent)
149                 return false; // do nothing on negative events
150
151         if (m_mesh || m_use_phys) /* NULL mesh is ok if were updating physics */
152                 m_scene->ReplaceMesh(GetParent(),m_mesh, m_use_gfx, m_use_phys);
153
154         return false;
155 }
156
157
158
159 CValue* KX_SCA_ReplaceMeshActuator::GetReplica()
160 {
161         KX_SCA_ReplaceMeshActuator* replica = 
162                 new KX_SCA_ReplaceMeshActuator(*this);
163
164         if (replica == NULL)
165                 return NULL;
166
167         replica->ProcessReplica();
168
169         return replica;
170 };
171
172 void KX_SCA_ReplaceMeshActuator::InstantReplaceMesh()
173 {
174         if (m_mesh) m_scene->ReplaceMesh(GetParent(),m_mesh, m_use_gfx, m_use_phys);
175 }
176
177 /* eof */