Name attributes added since 2.48a more consistently.
[blender.git] / source / gameengine / GameLogic / SCA_DelaySensor.cpp
1 /**
2  * Delay trigger
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 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #ifdef WIN32
37 // This warning tells us about truncation of __long__ stl-generated names.
38 // It can occasionally cause DevStudio to have internal compiler warnings.
39 #pragma warning( disable : 4786 )     
40 #endif
41
42 #include "SCA_DelaySensor.h"
43 #include "SCA_LogicManager.h"
44 #include "SCA_EventManager.h"
45
46 /* ------------------------------------------------------------------------- */
47 /* Native functions                                                          */
48 /* ------------------------------------------------------------------------- */
49
50 SCA_DelaySensor::SCA_DelaySensor(class SCA_EventManager* eventmgr,
51                                                                  SCA_IObject* gameobj,
52                                                                  int delay,
53                                                                  int duration,
54                                                                  bool repeat,
55                                                                  PyTypeObject* T)
56         : SCA_ISensor(gameobj,eventmgr, T),
57         m_repeat(repeat),
58         m_delay(delay),
59         m_duration(duration)
60 {
61         Init();
62 }
63
64 void SCA_DelaySensor::Init()
65 {
66         m_lastResult = false;
67         m_frameCount = -1;
68         m_reset = true;
69 }
70
71 SCA_DelaySensor::~SCA_DelaySensor()
72 {
73         /* intentionally empty */
74 }
75
76 CValue* SCA_DelaySensor::GetReplica()
77 {
78         CValue* replica = new SCA_DelaySensor(*this);
79         // this will copy properties and so on...
80         replica->ProcessReplica();
81
82         return replica;
83 }
84
85
86
87 bool SCA_DelaySensor::IsPositiveTrigger()
88
89         return (m_invert ? !m_lastResult : m_lastResult);
90 }
91
92 bool SCA_DelaySensor::Evaluate()
93 {
94         bool trigger = false;
95         bool result;
96
97         if (m_frameCount==-1) {
98                 // this is needed to ensure ON trigger in case delay==0
99                 // and avoid spurious OFF trigger when duration==0
100                 m_lastResult = false;
101                 m_frameCount = 0;
102         }
103
104         if (m_frameCount<m_delay) {
105                 m_frameCount++;
106                 result = false;
107         } else if (m_duration > 0) {
108                 if (m_frameCount < m_delay+m_duration) {
109                         m_frameCount++;
110                         result = true;
111                 } else {
112                         result = false;
113                         if (m_repeat)
114                                 m_frameCount = -1;
115                 }
116         } else {
117                 result = true;
118                 if (m_repeat)
119                         m_frameCount = -1;
120         }
121         if ((m_reset && m_level) || result != m_lastResult)
122                 trigger = true;
123         m_reset = false;
124         m_lastResult = result;
125         return trigger;
126 }
127
128 /* ------------------------------------------------------------------------- */
129 /* Python functions                                                          */
130 /* ------------------------------------------------------------------------- */
131
132 /* Integration hooks ------------------------------------------------------- */
133 PyTypeObject SCA_DelaySensor::Type = {
134 #if (PY_VERSION_HEX >= 0x02060000)
135         PyVarObject_HEAD_INIT(NULL, 0)
136 #else
137         /* python 2.5 and below */
138         PyObject_HEAD_INIT( NULL )  /* required py macro */
139         0,                          /* ob_size */
140 #endif
141         "SCA_DelaySensor",
142         sizeof(PyObjectPlus_Proxy),
143         0,
144         py_base_dealloc,
145         0,
146         0,
147         0,
148         0,
149         py_base_repr,
150         0,0,0,0,0,0,
151         py_base_getattro,
152         py_base_setattro,
153         0,0,0,0,0,0,0,0,0,
154         Methods
155 };
156
157 PyParentObject SCA_DelaySensor::Parents[] = {
158         &SCA_DelaySensor::Type,
159         &SCA_ISensor::Type,
160         &SCA_ILogicBrick::Type,
161         &CValue::Type,
162         NULL
163 };
164
165 PyMethodDef SCA_DelaySensor::Methods[] = {
166         //Deprecated functions ------>
167         /* setProperty */
168         {"setDelay", (PyCFunction) SCA_DelaySensor::sPySetDelay, METH_VARARGS, (PY_METHODCHAR)SetDelay_doc},
169         {"setDuration", (PyCFunction) SCA_DelaySensor::sPySetDuration, METH_VARARGS, (PY_METHODCHAR)SetDuration_doc},
170         {"setRepeat", (PyCFunction) SCA_DelaySensor::sPySetRepeat, METH_VARARGS, (PY_METHODCHAR)SetRepeat_doc},
171         /* getProperty */
172         {"getDelay", (PyCFunction) SCA_DelaySensor::sPyGetDelay, METH_NOARGS, (PY_METHODCHAR)GetDelay_doc},
173         {"getDuration", (PyCFunction) SCA_DelaySensor::sPyGetDuration, METH_NOARGS, (PY_METHODCHAR)GetDuration_doc},
174         {"getRepeat", (PyCFunction) SCA_DelaySensor::sPyGetRepeat, METH_NOARGS, (PY_METHODCHAR)GetRepeat_doc},
175         //<----- Deprecated
176         {NULL,NULL} //Sentinel
177 };
178
179 PyAttributeDef SCA_DelaySensor::Attributes[] = {
180         KX_PYATTRIBUTE_INT_RW("delay",0,100000,true,SCA_DelaySensor,m_delay),
181         KX_PYATTRIBUTE_INT_RW("duration",0,100000,true,SCA_DelaySensor,m_duration),
182         KX_PYATTRIBUTE_BOOL_RW("repeat",SCA_DelaySensor,m_repeat),
183         { NULL }        //Sentinel
184 };
185
186 PyObject* SCA_DelaySensor::py_getattro(PyObject *attr) {
187         py_getattro_up(SCA_ISensor);
188 }
189
190 PyObject* SCA_DelaySensor::py_getattro_dict() {
191         py_getattro_dict_up(SCA_ISensor);
192 }
193
194 int SCA_DelaySensor::py_setattro(PyObject *attr, PyObject *value) {
195         py_setattro_up(SCA_ISensor);
196 }
197
198
199 const char SCA_DelaySensor::SetDelay_doc[] = 
200 "setDelay(delay)\n"
201 "\t- delay: length of the initial OFF period as number of frame\n"
202 "\t         0 for immediate trigger\n"
203 "\tSet the initial delay before the positive trigger\n";
204 PyObject* SCA_DelaySensor::PySetDelay(PyObject* args)
205 {
206         ShowDeprecationWarning("setDelay()", "the delay property");
207         int delay;
208         
209         if(!PyArg_ParseTuple(args, "i:setDelay", &delay)) {
210                 return NULL;
211         }
212         if (delay < 0) {
213                 PyErr_SetString(PyExc_ValueError, "Delay cannot be negative");
214                 return NULL;
215         }
216         m_delay = delay;
217         Py_RETURN_NONE;
218 }
219
220 const char SCA_DelaySensor::SetDuration_doc[] = 
221 "setDuration(duration)\n"
222 "\t- duration: length of the ON period in number of frame after the initial off period\n"
223 "\t            0 for no ON period\n"
224 "\tSet the duration of the ON pulse after initial delay.\n"
225 "\tIf > 0, a negative trigger is fired at the end of the ON pulse.\n";
226 PyObject* SCA_DelaySensor::PySetDuration(PyObject* args)
227 {
228         ShowDeprecationWarning("setDuration()", "the duration property");
229         int duration;
230         
231         if(!PyArg_ParseTuple(args, "i:setDuration", &duration)) {
232                 return NULL;
233         }
234         if (duration < 0) {
235                 PyErr_SetString(PyExc_ValueError, "Duration cannot be negative");
236                 return NULL;
237         }
238         m_duration = duration;
239         Py_RETURN_NONE;
240 }
241
242 const char SCA_DelaySensor::SetRepeat_doc[] = 
243 "setRepeat(repeat)\n"
244 "\t- repeat: 1 if the initial OFF-ON cycle should be repeated indefinately\n"
245 "\t          0 if the initial OFF-ON cycle should run only once\n"
246 "\tSet the sensor repeat mode\n";
247 PyObject* SCA_DelaySensor::PySetRepeat(PyObject* args)
248 {
249         ShowDeprecationWarning("setRepeat()", "the repeat property");
250         int repeat;
251         
252         if(!PyArg_ParseTuple(args, "i:setRepeat", &repeat)) {
253                 return NULL;
254         }
255         m_repeat = (repeat != 0);
256         Py_RETURN_NONE;
257 }
258
259 const char SCA_DelaySensor::GetDelay_doc[] = 
260 "getDelay()\n"
261 "\tReturn the delay parameter value\n";
262 PyObject* SCA_DelaySensor::PyGetDelay()
263 {
264         ShowDeprecationWarning("getDelay()", "the delay property");
265         return PyInt_FromLong(m_delay);
266 }
267
268 const char SCA_DelaySensor::GetDuration_doc[] = 
269 "getDuration()\n"
270 "\tReturn the duration parameter value\n";
271 PyObject* SCA_DelaySensor::PyGetDuration()
272 {
273         ShowDeprecationWarning("getDuration()", "the duration property");
274         return PyInt_FromLong(m_duration);
275 }
276
277 const char SCA_DelaySensor::GetRepeat_doc[] = 
278 "getRepeat()\n"
279 "\tReturn the repeat parameter value\n";
280 PyObject* SCA_DelaySensor::PyGetRepeat()
281 {
282         ShowDeprecationWarning("getRepeat()", "the repeat property");
283         return BoolToPyArg(m_repeat);
284 }
285
286 /* eof */