ifdef's for future py3 support, after this adding py3 can mostly be done with defines...
[blender.git] / source / gameengine / Ketsji / KX_StateActuator.cpp
1 /*
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  * Actuator to toggle visibility/invisibility of objects
29  */
30
31 #include "KX_StateActuator.h"
32 #include "KX_GameObject.h"
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 KX_StateActuator::KX_StateActuator(
39         SCA_IObject* gameobj,
40         int operation,
41         unsigned int mask,
42         PyTypeObject* T
43         ) 
44         : SCA_IActuator(gameobj,T),
45           m_operation(operation),
46           m_mask(mask)
47 {
48         // intentionally empty
49 }
50
51 KX_StateActuator::~KX_StateActuator(
52         void
53         )
54 {
55         // intentionally empty
56 }
57
58 CValue*
59 KX_StateActuator::GetReplica(
60         void
61         )
62 {
63         KX_StateActuator* replica = new KX_StateActuator(*this);
64         replica->ProcessReplica();
65         return replica;
66 }
67
68 bool
69 KX_StateActuator::Update()
70 {
71         bool bNegativeEvent = IsNegativeEvent();
72         unsigned int objMask;
73         
74         RemoveAllEvents();
75         if (bNegativeEvent) return false;
76
77         KX_GameObject *obj = (KX_GameObject*) GetParent();
78         
79         objMask = obj->GetState();
80         switch (m_operation) 
81         {
82         case OP_CPY:
83                 objMask = m_mask;
84                 break;
85         case OP_SET:
86                 objMask |= m_mask;
87                 break;
88         case OP_CLR:
89                 objMask &= ~m_mask;
90                 break;
91         case OP_NEG:
92                 objMask ^= m_mask;
93                 break;
94         default:
95                 // unsupported operation, no  nothing
96                 return false;
97         }
98         obj->SetState(objMask);
99         return false;
100 }
101
102 /* ------------------------------------------------------------------------- */
103 /* Python functions                                                          */
104 /* ------------------------------------------------------------------------- */
105
106
107
108 /* Integration hooks ------------------------------------------------------- */
109 PyTypeObject KX_StateActuator::Type = {
110 #if (PY_VERSION_HEX >= 0x02060000)
111         PyVarObject_HEAD_INIT(NULL, 0)
112 #else
113         /* python 2.5 and below */
114         PyObject_HEAD_INIT( NULL )  /* required py macro */
115         0,                          /* ob_size */
116 #endif
117         "KX_StateActuator",
118         sizeof(PyObjectPlus_Proxy),
119         0,
120         py_base_dealloc,
121         0,
122         0,
123         0,
124         0,
125         py_base_repr,
126         0,0,0,0,0,0,
127         py_base_getattro,
128         py_base_setattro,
129         0,0,0,0,0,0,0,0,0,
130         Methods
131 };
132
133 PyParentObject 
134 KX_StateActuator::Parents[] = {
135         &KX_StateActuator::Type,
136         &SCA_IActuator::Type,
137         &SCA_ILogicBrick::Type,
138         &CValue::Type,
139         NULL
140 };
141
142 PyMethodDef 
143 KX_StateActuator::Methods[] = {
144         // deprecated -->
145         {"setOperation", (PyCFunction) KX_StateActuator::sPySetOperation, 
146          METH_VARARGS, (PY_METHODCHAR)SetOperation_doc},
147         {"setMask", (PyCFunction) KX_StateActuator::sPySetMask, 
148          METH_VARARGS, (PY_METHODCHAR)SetMask_doc},
149          // <--
150         {NULL,NULL} //Sentinel
151 };
152
153 PyAttributeDef KX_StateActuator::Attributes[] = {
154         KX_PYATTRIBUTE_INT_RW("operation",KX_StateActuator::OP_NOP+1,KX_StateActuator::OP_COUNT-1,false,KX_StateActuator,m_operation),
155         KX_PYATTRIBUTE_INT_RW("mask",0,0x3FFFFFFF,false,KX_StateActuator,m_mask),
156         { NULL }        //Sentinel
157 };
158
159 PyObject* KX_StateActuator::py_getattro(PyObject *attr)
160 {
161         py_getattro_up(SCA_IActuator);
162 }
163
164 PyObject* KX_StateActuator::py_getattro_dict() {
165         py_getattro_dict_up(SCA_IActuator);
166 }
167
168 int KX_StateActuator::py_setattro(PyObject *attr, PyObject* value)
169 {
170         py_setattro_up(SCA_IActuator);
171 }
172
173
174 /* set operation ---------------------------------------------------------- */
175 const char 
176 KX_StateActuator::SetOperation_doc[] = 
177 "setOperation(op)\n"
178 "\t - op : bit operation (0=Copy, 1=Set, 2=Clear, 3=Negate)"
179 "\tSet the type of bit operation to be applied on object state mask.\n"
180 "\tUse setMask() to specify the bits that will be modified.\n";
181 PyObject* 
182
183 KX_StateActuator::PySetOperation(PyObject* args) {
184         ShowDeprecationWarning("setOperation()", "the operation property");
185         int oper;
186
187         if(!PyArg_ParseTuple(args, "i:setOperation", &oper)) {
188                 return NULL;
189         }
190
191         m_operation = oper;
192
193         Py_RETURN_NONE;
194 }
195
196 /* set mask ---------------------------------------------------------- */
197 const char 
198 KX_StateActuator::SetMask_doc[] = 
199 "setMask(mask)\n"
200 "\t - mask : bits that will be modified"
201 "\tSet the value that defines the bits that will be modified by the operation.\n"
202 "\tThe bits that are 1 in the value will be updated in the object state,\n"
203 "\tthe bits that are 0 are will be left unmodified expect for the Copy operation\n"
204 "\twhich copies the value to the object state.\n";
205 PyObject* 
206
207 KX_StateActuator::PySetMask(PyObject* args) {
208         ShowDeprecationWarning("setMask()", "the mask property");
209         int mask;
210
211         if(!PyArg_ParseTuple(args, "i:setMask", &mask)) {
212                 return NULL;
213         }
214
215         m_mask = mask;
216
217         Py_RETURN_NONE;
218 }
219
220