fix for bug #18898: GE perspective 3D View not working properly (missing LENS)
[blender.git] / source / gameengine / Ketsji / KX_SCA_DynamicActuator.cpp
1 //
2 // Adjust dynamics settins for this object
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_DynamicActuator.cpp
35
36 // Please look here for revision history.
37
38 #include "KX_SCA_DynamicActuator.h"
39 #include "blendef.h"
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 /* ------------------------------------------------------------------------- */
46 /* Python functions                                                          */
47 /* ------------------------------------------------------------------------- */
48
49 /* Integration hooks ------------------------------------------------------- */
50
51         PyTypeObject 
52
53 KX_SCA_DynamicActuator::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_DynamicActuator",
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,
71         py_base_getattro,
72         py_base_setattro,
73         0,0,0,0,0,0,0,0,0,
74         Methods
75 };
76
77 PyParentObject KX_SCA_DynamicActuator::Parents[] = {
78         &KX_SCA_DynamicActuator::Type,
79         &SCA_IActuator::Type,
80         &SCA_ILogicBrick::Type,
81         &CValue::Type,
82         NULL
83 };
84
85
86 PyMethodDef KX_SCA_DynamicActuator::Methods[] = {
87         // ---> deprecated
88         KX_PYMETHODTABLE(KX_SCA_DynamicActuator, setOperation),
89         KX_PYMETHODTABLE(KX_SCA_DynamicActuator, getOperation),
90         {NULL,NULL} //Sentinel
91 };
92
93 PyAttributeDef KX_SCA_DynamicActuator::Attributes[] = {
94         KX_PYATTRIBUTE_SHORT_RW("mode",0,4,false,KX_SCA_DynamicActuator,m_dyn_operation),
95         KX_PYATTRIBUTE_FLOAT_RW("mass",0.0,MAXFLOAT,KX_SCA_DynamicActuator,m_setmass),
96         { NULL }        //Sentinel
97 };
98
99
100 PyObject* KX_SCA_DynamicActuator::py_getattro(PyObject *attr)
101 {
102         py_getattro_up(SCA_IActuator);
103 }
104
105 PyObject* KX_SCA_DynamicActuator::py_getattro_dict() {
106         py_getattro_dict_up(SCA_IActuator);
107 }
108
109 int KX_SCA_DynamicActuator::py_setattro(PyObject *attr, PyObject* value)
110 {
111         py_setattro_up(SCA_IActuator);
112 }
113
114
115 /* 1. setOperation */
116 KX_PYMETHODDEF_DOC(KX_SCA_DynamicActuator, setOperation,
117 "setOperation(operation?)\n"
118 "\t - operation? : type of dynamic operation\n"
119 "\t                0 = restore dynamics\n"
120 "\t                1 = disable dynamics\n"
121 "\t                2 = enable rigid body\n"
122 "\t                3 = disable rigid body\n"
123 "Change the dynamic status of the parent object.\n")
124 {
125         ShowDeprecationWarning("setOperation()", "the mode property");
126         int dyn_operation;
127         
128         if (!PyArg_ParseTuple(args, "i:setOperation", &dyn_operation))
129         {
130                 return NULL;    
131         }
132         if (dyn_operation <0 || dyn_operation>3) {
133                 PyErr_SetString(PyExc_IndexError, "Dynamic Actuator's setOperation() range must be between 0 and 3");
134                 return NULL;
135         }
136         m_dyn_operation= dyn_operation;
137         Py_RETURN_NONE;
138 }
139
140 KX_PYMETHODDEF_DOC(KX_SCA_DynamicActuator, getOperation,
141 "getOperation() -> integer\n"
142 "Returns the operation type of this actuator.\n"
143 )
144 {
145         ShowDeprecationWarning("getOperation()", "the mode property");
146         return PyInt_FromLong((long)m_dyn_operation);
147 }
148
149
150 /* ------------------------------------------------------------------------- */
151 /* Native functions                                                          */
152 /* ------------------------------------------------------------------------- */
153
154 KX_SCA_DynamicActuator::KX_SCA_DynamicActuator(SCA_IObject *gameobj,
155                                                                                                            short dyn_operation,
156                                                                                                            float setmass,
157                                                                                                            PyTypeObject* T) : 
158
159         SCA_IActuator(gameobj, T),
160         m_dyn_operation(dyn_operation),
161         m_setmass(setmass)
162 {
163 } /* End of constructor */
164
165
166 KX_SCA_DynamicActuator::~KX_SCA_DynamicActuator()
167
168         // there's nothing to be done here, really....
169 } /* end of destructor */
170
171
172
173 bool KX_SCA_DynamicActuator::Update()
174 {
175         // bool result = false; /*unused*/
176         KX_GameObject *obj = (KX_GameObject*) GetParent();
177         bool bNegativeEvent = IsNegativeEvent();
178         KX_IPhysicsController* controller;
179         RemoveAllEvents();
180
181         if (bNegativeEvent)
182                 return false; // do nothing on negative events
183         
184         if (!obj)
185                 return false; // object not accessible, shouldnt happen
186         controller = obj->GetPhysicsController();
187         if (!controller)
188                 return false;   // no physic object
189
190         switch (m_dyn_operation)
191         {
192                 case 0:                 
193                         obj->RestoreDynamics(); 
194                         break;
195                 case 1:
196                         obj->SuspendDynamics();
197                         break;
198                 case 2:
199                         controller->setRigidBody(true); 
200                         break;
201                 case 3:
202                         controller->setRigidBody(false);
203                         break;
204                 case 4:
205                         controller->SetMass(m_setmass);
206                         break;
207         }
208
209         return false;
210 }
211
212
213
214 CValue* KX_SCA_DynamicActuator::GetReplica()
215 {
216         KX_SCA_DynamicActuator* replica = 
217                 new KX_SCA_DynamicActuator(*this);
218
219         if (replica == NULL)
220                 return NULL;
221
222         replica->ProcessReplica();
223         return replica;
224 };
225
226
227 /* eof */