soc-2008-mxcurioni: merged changes to revision 15705
[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
31 #ifdef HAVE_CONFIG_H
32 #include <config.h>
33 #endif
34
35 SCA_LogicManager* SCA_ILogicBrick::m_sCurrentLogicManager = NULL;
36
37 SCA_ILogicBrick::SCA_ILogicBrick(SCA_IObject* gameobj,
38                                                                  PyTypeObject* T)
39         :
40         CValue(T),
41         m_gameobj(gameobj),
42         m_Execute_Priority(0),
43         m_Execute_Ueber_Priority(0),
44         m_bActive(false),
45         m_eventval(0)
46 {
47         m_text = "KX_LogicBrick";
48 }
49
50
51
52 SCA_ILogicBrick::~SCA_ILogicBrick()
53 {
54         RemoveEvent();
55 }
56
57
58
59 void SCA_ILogicBrick::SetExecutePriority(int execute_Priority)
60 {
61         m_Execute_Priority = execute_Priority;
62 }
63
64
65
66 void SCA_ILogicBrick::SetUeberExecutePriority(int execute_Priority)
67 {
68         m_Execute_Ueber_Priority = execute_Priority;
69 }
70
71
72
73 SCA_IObject* SCA_ILogicBrick::GetParent()
74 {
75         return m_gameobj;
76 }
77
78
79
80 void SCA_ILogicBrick::ReParent(SCA_IObject* parent)
81 {
82         m_gameobj = parent;
83 }
84
85 void SCA_ILogicBrick::Relink(GEN_Map<GEN_HashedPtr, void*> *obj_map)
86 {
87         // nothing to do
88 }
89
90 CValue* SCA_ILogicBrick::Calc(VALUE_OPERATOR op, CValue *val)
91 {
92         CValue* temp = new CBoolValue(false,"");
93         CValue* result = temp->Calc(op,val);
94         temp->Release();
95
96         return result;
97
98
99
100
101 CValue* SCA_ILogicBrick::CalcFinal(VALUE_DATA_TYPE dtype,
102                                                                    VALUE_OPERATOR op,
103                                                                    CValue *val)
104 {
105         // same as bool implementation, so...
106         CValue* temp = new CBoolValue(false,"");
107         CValue* result = temp->CalcFinal(dtype,op,val);
108         temp->Release();
109
110         return result;
111 }
112
113
114
115 const STR_String& SCA_ILogicBrick::GetText()
116
117         if (m_name.Length())
118                 return m_name;
119
120         return m_text;
121 }
122
123
124
125 float SCA_ILogicBrick::GetNumber()
126 {
127         return -1;
128 }
129
130
131
132 STR_String SCA_ILogicBrick::GetName()
133 {
134         return m_name;
135 }
136
137
138
139 void SCA_ILogicBrick::SetName(STR_String name)
140 {
141         m_name = name;
142 }
143
144
145
146 void SCA_ILogicBrick::ReplicaSetName(STR_String name)
147 {
148         m_name = name;
149 }
150                 
151
152
153 bool SCA_ILogicBrick::IsActive()
154 {
155         return m_bActive;
156 }
157
158
159
160 bool SCA_ILogicBrick::LessComparedTo(SCA_ILogicBrick* other)
161 {
162         return (this->m_Execute_Ueber_Priority < other->m_Execute_Ueber_Priority) 
163                 || ((this->m_Execute_Ueber_Priority == other->m_Execute_Ueber_Priority) && 
164                 (this->m_Execute_Priority < other->m_Execute_Priority));
165 }
166
167
168
169 void SCA_ILogicBrick::SetActive(bool active)
170 {
171         m_bActive=active;
172         if (active)
173         {
174                 //m_gameobj->SetDebugColor(GetDrawColor());
175         } else
176         {
177                 //m_gameobj->ResetDebugColor();
178         }
179 }
180
181
182
183 void SCA_ILogicBrick::RegisterEvent(CValue* eventval)
184 {
185         if (m_eventval)
186                 m_eventval->Release();
187
188         m_eventval = eventval->AddRef();
189 }
190
191
192 void SCA_ILogicBrick::RemoveEvent()
193 {
194         if (m_eventval)
195         {
196                 m_eventval->Release();
197                 m_eventval = NULL;
198         }
199 }
200
201
202
203 CValue* SCA_ILogicBrick::GetEvent()
204 {
205         if (m_eventval)
206         {
207                 return m_eventval->AddRef();
208         }
209         
210         return NULL;
211 }
212
213
214
215
216 /* python stuff */
217
218 PyTypeObject SCA_ILogicBrick::Type = {
219         PyObject_HEAD_INIT(&PyType_Type)
220         0,
221         "SCA_ILogicBrick",
222         sizeof(SCA_ILogicBrick),
223         0,
224         PyDestructor,
225         0,
226         __getattr,
227         __setattr,
228         0, //&MyPyCompare,
229         __repr,
230         0, //&cvalue_as_number,
231         0,
232         0,
233         0,
234         0
235 };
236
237
238
239 PyParentObject SCA_ILogicBrick::Parents[] = {
240         &SCA_ILogicBrick::Type,
241         &CValue::Type,
242         NULL
243 };
244
245
246
247 PyMethodDef SCA_ILogicBrick::Methods[] = {
248   {"getOwner", (PyCFunction) SCA_ILogicBrick::sPyGetOwner, METH_NOARGS},
249   {"getExecutePriority", (PyCFunction) SCA_ILogicBrick::sPySetExecutePriority, METH_NOARGS},
250   {"setExecutePriority", (PyCFunction) SCA_ILogicBrick::sPySetExecutePriority, METH_VARARGS},
251   {NULL,NULL} //Sentinel
252 };
253
254
255
256 PyObject*
257 SCA_ILogicBrick::_getattr(const STR_String& attr)
258 {
259   _getattr_up(CValue);
260 }
261
262
263
264 PyObject* SCA_ILogicBrick::PyGetOwner(PyObject* self)
265 {
266         CValue* parent = GetParent();
267         if (parent)
268         {
269                 parent->AddRef();
270                 return parent;
271         }
272
273         printf("ERROR: Python scriptblock without owner\n");
274         Py_INCREF(Py_None);
275         return Py_None;//Int_FromLong(IsPositiveTrigger());
276 }
277
278
279
280 PyObject* SCA_ILogicBrick::PySetExecutePriority(PyObject* self, 
281                                PyObject* args, 
282                                PyObject* kwds)
283 {
284
285         int priority=0;
286
287     if (!PyArg_ParseTuple(args, "i", &priority)) {
288                 return NULL;
289     }
290         
291         m_Execute_Ueber_Priority = priority;
292
293         Py_Return;
294 }
295
296
297
298 PyObject* SCA_ILogicBrick::PyGetExecutePriority(PyObject* self)
299 {
300         return PyInt_FromLong(m_Execute_Ueber_Priority);
301 }
302
303
304
305 /* Conversions for making life better. */
306 bool SCA_ILogicBrick::PyArgToBool(int boolArg)
307 {
308         if (boolArg) {
309                 return true;
310         } else {
311                 return false;
312         }
313 }
314
315
316
317 PyObject* SCA_ILogicBrick::BoolToPyArg(bool boolarg)
318 {
319         return PyInt_FromLong(boolarg? KX_TRUE: KX_FALSE);      
320 }