doxygen: prevent GPL license block from being parsed as doxygen comment.
[blender.git] / source / gameengine / GameLogic / SCA_ActuatorSensor.cpp
1 /*
2  * Actuator sensor
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #include <stddef.h>
33
34 #include <iostream>
35 #include "SCA_ActuatorSensor.h"
36 #include "SCA_EventManager.h"
37 #include "SCA_LogicManager.h"
38
39 SCA_ActuatorSensor::SCA_ActuatorSensor(SCA_EventManager* eventmgr,
40                                                                          SCA_IObject* gameobj,
41                                                                          const STR_String& actname)
42         : SCA_ISensor(gameobj,eventmgr),
43           m_checkactname(actname)
44 {
45         m_actuator = GetParent()->FindActuator(m_checkactname);
46         Init();
47 }
48
49 void SCA_ActuatorSensor::Init()
50 {
51         m_lastresult = m_invert?true:false;
52         m_midresult = m_lastresult;
53         m_reset = true;
54 }
55
56 CValue* SCA_ActuatorSensor::GetReplica()
57 {
58         SCA_ActuatorSensor* replica = new SCA_ActuatorSensor(*this);
59         // m_range_expr must be recalculated on replica!
60         replica->ProcessReplica();
61         replica->Init();
62
63         return replica;
64 }
65
66 void SCA_ActuatorSensor::ReParent(SCA_IObject* parent)
67 {
68         m_actuator = parent->FindActuator(m_checkactname);
69         SCA_ISensor::ReParent(parent);
70 }
71
72 bool SCA_ActuatorSensor::IsPositiveTrigger()
73 {
74         bool result = m_lastresult;
75         if (m_invert)
76                 result = !result;
77
78         return result;
79 }
80
81
82
83 SCA_ActuatorSensor::~SCA_ActuatorSensor()
84 {
85 }
86
87
88
89 bool SCA_ActuatorSensor::Evaluate()
90 {
91         if (m_actuator)
92         {
93                 bool result = m_actuator->IsActive();
94                 bool reset = m_reset && m_level;
95                 
96                 m_reset = false;
97                 if (m_lastresult != result || m_midresult != result)
98                 {
99                         m_lastresult = m_midresult = result;
100                         return true;
101                 }
102                 return (reset) ? true : false;
103         }
104         return false;
105 }
106
107 void SCA_ActuatorSensor::Update()
108 {
109         if (m_actuator)
110         {
111                 m_midresult = m_actuator->IsActive() && !m_actuator->IsNegativeEvent();
112         }
113 }
114
115 #ifdef WITH_PYTHON
116
117 /* ------------------------------------------------------------------------- */
118 /* Python functions                                                          */
119 /* ------------------------------------------------------------------------- */
120
121 /* Integration hooks ------------------------------------------------------- */
122 PyTypeObject SCA_ActuatorSensor::Type = {
123         PyVarObject_HEAD_INIT(NULL, 0)
124         "SCA_ActuatorSensor",
125         sizeof(PyObjectPlus_Proxy),
126         0,
127         py_base_dealloc,
128         0,
129         0,
130         0,
131         0,
132         py_base_repr,
133         0,0,0,0,0,0,0,0,0,
134         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
135         0,0,0,0,0,0,0,
136         Methods,
137         0,
138         0,
139         &SCA_ISensor::Type,
140         0,0,0,0,0,0,
141         py_base_new
142 };
143
144 PyMethodDef SCA_ActuatorSensor::Methods[] = {
145         {NULL,NULL} //Sentinel
146 };
147
148 PyAttributeDef SCA_ActuatorSensor::Attributes[] = {
149         KX_PYATTRIBUTE_STRING_RW_CHECK("actuator",0,100,false,SCA_ActuatorSensor,m_checkactname,CheckActuator),
150         { NULL }        //Sentinel
151 };
152
153 int SCA_ActuatorSensor::CheckActuator(void *self, const PyAttributeDef*)
154 {
155         SCA_ActuatorSensor* sensor = reinterpret_cast<SCA_ActuatorSensor*>(self);
156         SCA_IActuator* act = sensor->GetParent()->FindActuator(sensor->m_checkactname);
157         if (act) {
158                 sensor->m_actuator = act;
159                 return 0;
160         }
161         PyErr_SetString(PyExc_AttributeError, "string does not correspond to an actuator");
162         return 1;
163 }
164
165 #endif // WITH_PYTHON
166
167 /* eof */