7fa55cfb1eef28e736c98ff7f9605cc729202e20
[blender.git] / source / gameengine / GameLogic / SCA_ILogicBrick.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28
29 #include "SCA_ILogicBrick.h"
30 #include "PyObjectPlus.h"
31
32 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 SCA_LogicManager* SCA_ILogicBrick::m_sCurrentLogicManager = NULL;
37
38 SCA_ILogicBrick::SCA_ILogicBrick(SCA_IObject* gameobj,
39                                                                  PyTypeObject* T)
40         :
41         CValue(T),
42         m_gameobj(gameobj),
43         m_Execute_Priority(0),
44         m_Execute_Ueber_Priority(0),
45         m_bActive(false),
46         m_eventval(0)
47 {
48         m_text = "KX_LogicBrick";
49 }
50
51
52
53 SCA_ILogicBrick::~SCA_ILogicBrick()
54 {
55         RemoveEvent();
56 }
57
58
59
60 void SCA_ILogicBrick::SetExecutePriority(int execute_Priority)
61 {
62         m_Execute_Priority = execute_Priority;
63 }
64
65
66
67 void SCA_ILogicBrick::SetUeberExecutePriority(int execute_Priority)
68 {
69         m_Execute_Ueber_Priority = execute_Priority;
70 }
71
72
73
74 SCA_IObject* SCA_ILogicBrick::GetParent()
75 {
76         return m_gameobj;
77 }
78
79
80
81 void SCA_ILogicBrick::ReParent(SCA_IObject* parent)
82 {
83         m_gameobj = parent;
84 }
85
86 void SCA_ILogicBrick::Relink(GEN_Map<GEN_HashedPtr, void*> *obj_map)
87 {
88         // nothing to do
89 }
90
91 CValue* SCA_ILogicBrick::Calc(VALUE_OPERATOR op, CValue *val)
92 {
93         CValue* temp = new CBoolValue(false,"");
94         CValue* result = temp->Calc(op,val);
95         temp->Release();
96
97         return result;
98
99
100
101
102 CValue* SCA_ILogicBrick::CalcFinal(VALUE_DATA_TYPE dtype,
103                                                                    VALUE_OPERATOR op,
104                                                                    CValue *val)
105 {
106         // same as bool implementation, so...
107         CValue* temp = new CBoolValue(false,"");
108         CValue* result = temp->CalcFinal(dtype,op,val);
109         temp->Release();
110
111         return result;
112 }
113
114
115
116 const STR_String& SCA_ILogicBrick::GetText()
117
118         if (m_name.Length())
119                 return m_name;
120
121         return m_text;
122 }
123
124
125
126 double SCA_ILogicBrick::GetNumber()
127 {
128         return -1;
129 }
130
131
132
133 STR_String SCA_ILogicBrick::GetName()
134 {
135         return m_name;
136 }
137
138
139
140 void SCA_ILogicBrick::SetName(STR_String name)
141 {
142         m_name = name;
143 }
144
145
146
147 void SCA_ILogicBrick::ReplicaSetName(STR_String name)
148 {
149         m_name = name;
150 }
151                 
152
153
154 bool SCA_ILogicBrick::IsActive()
155 {
156         return m_bActive;
157 }
158
159
160
161 bool SCA_ILogicBrick::LessComparedTo(SCA_ILogicBrick* other)
162 {
163         return (this->m_Execute_Ueber_Priority < other->m_Execute_Ueber_Priority) 
164                 || ((this->m_Execute_Ueber_Priority == other->m_Execute_Ueber_Priority) && 
165                 (this->m_Execute_Priority < other->m_Execute_Priority));
166 }
167
168
169
170 void SCA_ILogicBrick::SetActive(bool active)
171 {
172         m_bActive=active;
173         if (active)
174         {
175                 //m_gameobj->SetDebugColor(GetDrawColor());
176         } else
177         {
178                 //m_gameobj->ResetDebugColor();
179         }
180 }
181
182
183
184 void SCA_ILogicBrick::RegisterEvent(CValue* eventval)
185 {
186         if (m_eventval)
187                 m_eventval->Release();
188
189         m_eventval = eventval->AddRef();
190 }
191
192
193 void SCA_ILogicBrick::RemoveEvent()
194 {
195         if (m_eventval)
196         {
197                 m_eventval->Release();
198                 m_eventval = NULL;
199         }
200 }
201
202
203
204 CValue* SCA_ILogicBrick::GetEvent()
205 {
206         if (m_eventval)
207         {
208                 return m_eventval->AddRef();
209         }
210         
211         return NULL;
212 }
213
214
215
216
217 /* python stuff */
218
219 PyTypeObject SCA_ILogicBrick::Type = {
220         PyObject_HEAD_INIT(NULL)
221         0,
222         "SCA_ILogicBrick",
223         sizeof(SCA_ILogicBrick),
224         0,
225         PyDestructor,
226         0,
227         0,
228         0,
229         0,
230         py_base_repr,
231         0,0,0,0,0,0,
232         py_base_getattro,
233         py_base_setattro,
234         0,0,0,0,0,0,0,0,0,
235         Methods
236 };
237
238
239
240 PyParentObject SCA_ILogicBrick::Parents[] = {
241         &SCA_ILogicBrick::Type,
242         &CValue::Type,
243         NULL
244 };
245
246
247
248 PyMethodDef SCA_ILogicBrick::Methods[] = {
249   {"getOwner", (PyCFunction) SCA_ILogicBrick::sPyGetOwner, METH_NOARGS},
250   // --> Deprecated
251   {"getExecutePriority", (PyCFunction) SCA_ILogicBrick::sPyGetExecutePriority, METH_NOARGS},
252   {"setExecutePriority", (PyCFunction) SCA_ILogicBrick::sPySetExecutePriority, METH_VARARGS},
253   // <-- Deprecated
254   {NULL,NULL} //Sentinel
255 };
256
257 PyAttributeDef SCA_ILogicBrick::Attributes[] = {
258         KX_PYATTRIBUTE_INT_RW("executePriority",0,100000,false,SCA_ILogicBrick,m_Execute_Ueber_Priority),
259         {NULL} //Sentinel
260 };
261
262 int SCA_ILogicBrick::CheckProperty(void *self, const PyAttributeDef *attrdef)
263 {
264         if (attrdef->m_type != KX_PYATTRIBUTE_TYPE_STRING || attrdef->m_length != 1) {
265                 PyErr_SetString(PyExc_AttributeError, "inconsistent check function for attribute type, report to blender.org");
266                 return 1;
267         }
268         SCA_ILogicBrick* brick = reinterpret_cast<SCA_ILogicBrick*>(self);
269         STR_String* var = reinterpret_cast<STR_String*>((char*)self+attrdef->m_offset);
270         CValue* prop = brick->GetParent()->FindIdentifier(*var);
271         bool error = prop->IsError();
272         prop->Release();
273         if (error) {
274                 PyErr_SetString(PyExc_ValueError, "string does not correspond to a property");
275                 return 1;
276         }
277         return 0;
278 }
279
280 PyObject*
281 SCA_ILogicBrick::py_getattro(PyObject *attr)
282 {
283   py_getattro_up(CValue);
284 }
285
286 int SCA_ILogicBrick::py_setattro(PyObject *attr, PyObject *value)
287 {
288         py_setattro_up(CValue);
289 }
290
291
292 PyObject* SCA_ILogicBrick::PyGetOwner(PyObject* self)
293 {
294         CValue* parent = GetParent();
295         if (parent)
296         {
297                 return parent->GetProxy();
298         }
299
300         printf("ERROR: Python scriptblock without owner\n");
301         Py_RETURN_NONE; //Int_FromLong(IsPositiveTrigger());
302 }
303
304
305
306 PyObject* SCA_ILogicBrick::PySetExecutePriority(PyObject* self, 
307                                PyObject* args, 
308                                PyObject* kwds)
309 {
310         ShowDeprecationWarning("setExecutePriority()", "the executePriority property");
311
312         int priority=0;
313
314     if (!PyArg_ParseTuple(args, "i:setExecutePriority", &priority)) {
315                 return NULL;
316     }
317         
318         m_Execute_Ueber_Priority = priority;
319
320         Py_RETURN_NONE;
321 }
322
323
324
325 PyObject* SCA_ILogicBrick::PyGetExecutePriority(PyObject* self)
326 {
327         ShowDeprecationWarning("getExecutePriority()", "the executePriority property");
328         return PyInt_FromLong(m_Execute_Ueber_Priority);
329 }
330
331
332
333 /* Conversions for making life better. */
334 bool SCA_ILogicBrick::PyArgToBool(int boolArg)
335 {
336         if (boolArg) {
337                 return true;
338         } else {
339                 return false;
340         }
341 }
342
343
344
345 PyObject* SCA_ILogicBrick::BoolToPyArg(bool boolarg)
346 {
347         return PyInt_FromLong(boolarg? KX_TRUE: KX_FALSE);      
348 }