Minor Fixes:
[blender.git] / source / gameengine / GameLogic / SCA_ILogicBrick.cpp
1 /**
2  * $Id$
3  * ***** BEGIN GPL/BL DUAL 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. The Blender
9  * Foundation also sells licenses for use in proprietary software under
10  * the Blender License.  See http://www.blender.org/BL/ for information
11  * about this.
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, 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/BL DUAL LICENSE BLOCK *****
30  */
31
32 #include "SCA_ILogicBrick.h"
33
34 #ifdef HAVE_CONFIG_H
35 #include <config.h>
36 #endif
37
38 SCA_LogicManager* SCA_ILogicBrick::m_sCurrentLogicManager = NULL;
39
40 SCA_ILogicBrick::SCA_ILogicBrick(SCA_IObject* gameobj,
41                                                                  PyTypeObject* T)
42         :
43         CValue(T),
44         m_gameobj(gameobj),
45         m_Execute_Priority(0),
46         m_Execute_Ueber_Priority(0),
47         m_bActive(false),
48         m_eventval(0)
49 {
50         m_text = "KX_LogicBrick";
51 }
52
53
54
55 SCA_ILogicBrick::~SCA_ILogicBrick()
56 {
57         RemoveEvent();
58 }
59
60
61
62 void SCA_ILogicBrick::SetExecutePriority(int execute_Priority)
63 {
64         m_Execute_Priority = execute_Priority;
65 }
66
67
68
69 void SCA_ILogicBrick::SetUeberExecutePriority(int execute_Priority)
70 {
71         m_Execute_Ueber_Priority = execute_Priority;
72 }
73
74
75
76 SCA_IObject* SCA_ILogicBrick::GetParent()
77 {
78         return m_gameobj;
79 }
80
81
82
83 void SCA_ILogicBrick::ReParent(SCA_IObject* parent)
84 {
85         m_gameobj = parent;
86 }
87
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_VARARGS},
249   {"getExecutePriority", (PyCFunction) SCA_ILogicBrick::sPySetExecutePriority, METH_VARARGS},
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                                PyObject* args, 
266                                PyObject* kwds)
267 {
268         CValue* parent = GetParent();
269         if (parent)
270         {
271                 parent->AddRef();
272                 return parent;
273         }
274
275         printf("ERROR: Python scriptblock without owner\n");
276         Py_INCREF(Py_None);
277         return Py_None;//Int_FromLong(IsPositiveTrigger());
278 }
279
280
281
282 PyObject* SCA_ILogicBrick::PySetExecutePriority(PyObject* self, 
283                                PyObject* args, 
284                                PyObject* kwds)
285 {
286
287         int priority=0;
288
289     if (!PyArg_ParseTuple(args, "i", &priority)) {
290                 return NULL;
291     }
292         
293         m_Execute_Ueber_Priority = priority;
294
295         Py_Return;
296 }
297
298
299
300 PyObject* SCA_ILogicBrick::PyGetExecutePriority(PyObject* self, 
301                                PyObject* args, 
302                                PyObject* kwds)
303 {
304         return PyInt_FromLong(m_Execute_Ueber_Priority);
305 }
306
307
308
309 /* Conversions for making life better. */
310 bool SCA_ILogicBrick::PyArgToBool(int boolArg)
311 {
312         if (boolArg) {
313                 return true;
314         } else {
315                 return false;
316         }
317 }
318
319
320
321 PyObject* SCA_ILogicBrick::BoolToPyArg(bool boolarg)
322 {
323         return PyInt_FromLong(boolarg? KX_TRUE: KX_FALSE);      
324 }