BGE Python API cleanup - no functionality changes
[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         // this will copy properties and so on...
66         CValue::AddDataToReplica(replica);
67         return replica;
68 }
69
70 bool
71 KX_StateActuator::Update()
72 {
73         bool bNegativeEvent = IsNegativeEvent();
74         unsigned int objMask;
75         
76         RemoveAllEvents();
77         if (bNegativeEvent) return false;
78
79         KX_GameObject *obj = (KX_GameObject*) GetParent();
80         
81         objMask = obj->GetState();
82         switch (m_operation) 
83         {
84         case OP_CPY:
85                 objMask = m_mask;
86                 break;
87         case OP_SET:
88                 objMask |= m_mask;
89                 break;
90         case OP_CLR:
91                 objMask &= ~m_mask;
92                 break;
93         case OP_NEG:
94                 objMask ^= m_mask;
95                 break;
96         default:
97                 // unsupported operation, no  nothing
98                 return false;
99         }
100         obj->SetState(objMask);
101         return false;
102 }
103
104 /* ------------------------------------------------------------------------- */
105 /* Python functions                                                          */
106 /* ------------------------------------------------------------------------- */
107
108
109
110 /* Integration hooks ------------------------------------------------------- */
111 PyTypeObject KX_StateActuator::Type = {
112         PyObject_HEAD_INIT(NULL)
113         0,
114         "KX_StateActuator",
115         sizeof(PyObjectPlus_Proxy),
116         0,
117         py_base_dealloc,
118         0,
119         0,
120         0,
121         0,
122         py_base_repr,
123         0,0,0,0,0,0,
124         py_base_getattro,
125         py_base_setattro,
126         0,0,0,0,0,0,0,0,0,
127         Methods
128 };
129
130 PyParentObject 
131 KX_StateActuator::Parents[] = {
132         &KX_StateActuator::Type,
133         &SCA_IActuator::Type,
134         &SCA_ILogicBrick::Type,
135         &CValue::Type,
136         NULL
137 };
138
139 PyMethodDef 
140 KX_StateActuator::Methods[] = {
141         // deprecated -->
142         {"setOperation", (PyCFunction) KX_StateActuator::sPySetOperation, 
143          METH_VARARGS, (PY_METHODCHAR)SetOperation_doc},
144         {"setMask", (PyCFunction) KX_StateActuator::sPySetMask, 
145          METH_VARARGS, (PY_METHODCHAR)SetMask_doc},
146          // <--
147         {NULL,NULL} //Sentinel
148 };
149
150 PyAttributeDef KX_StateActuator::Attributes[] = {
151         KX_PYATTRIBUTE_INT_RW("operation",KX_StateActuator::OP_NOP+1,KX_StateActuator::OP_COUNT-1,false,KX_StateActuator,m_operation),
152         KX_PYATTRIBUTE_INT_RW("mask",0,0x3FFFFFFF,false,KX_StateActuator,m_mask),
153         { NULL }        //Sentinel
154 };
155
156 PyObject* KX_StateActuator::py_getattro(PyObject *attr)
157 {
158         py_getattro_up(SCA_IActuator);
159 };
160
161 int KX_StateActuator::py_setattro(PyObject *attr, PyObject* value)
162 {
163         py_setattro_up(SCA_IActuator);
164 }
165
166
167 /* set operation ---------------------------------------------------------- */
168 const char 
169 KX_StateActuator::SetOperation_doc[] = 
170 "setOperation(op)\n"
171 "\t - op : bit operation (0=Copy, 1=Set, 2=Clear, 3=Negate)"
172 "\tSet the type of bit operation to be applied on object state mask.\n"
173 "\tUse setMask() to specify the bits that will be modified.\n";
174 PyObject* 
175
176 KX_StateActuator::PySetOperation(PyObject* self, 
177                                     PyObject* args, 
178                                     PyObject* kwds) {
179         ShowDeprecationWarning("setOperation()", "the operation property");
180         int oper;
181
182         if(!PyArg_ParseTuple(args, "i:setOperation", &oper)) {
183                 return NULL;
184         }
185
186         m_operation = oper;
187
188         Py_RETURN_NONE;
189 }
190
191 /* set mask ---------------------------------------------------------- */
192 const char 
193 KX_StateActuator::SetMask_doc[] = 
194 "setMask(mask)\n"
195 "\t - mask : bits that will be modified"
196 "\tSet the value that defines the bits that will be modified by the operation.\n"
197 "\tThe bits that are 1 in the value will be updated in the object state,\n"
198 "\tthe bits that are 0 are will be left unmodified expect for the Copy operation\n"
199 "\twhich copies the value to the object state.\n";
200 PyObject* 
201
202 KX_StateActuator::PySetMask(PyObject* self, 
203                                     PyObject* args, 
204                                     PyObject* kwds) {
205         ShowDeprecationWarning("setMask()", "the mask property");
206         int mask;
207
208         if(!PyArg_ParseTuple(args, "i:setMask", &mask)) {
209                 return NULL;
210         }
211
212         m_mask = mask;
213
214         Py_RETURN_NONE;
215 }
216
217