Merged 15170:15635 from trunk (no conflicts or even merges)
[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
86
87 CValue* SCA_ILogicBrick::Calc(VALUE_OPERATOR op, CValue *val)
88 {
89         CValue* temp = new CBoolValue(false,"");
90         CValue* result = temp->Calc(op,val);
91         temp->Release();
92
93         return result;
94
95
96
97
98 CValue* SCA_ILogicBrick::CalcFinal(VALUE_DATA_TYPE dtype,
99                                                                    VALUE_OPERATOR op,
100                                                                    CValue *val)
101 {
102         // same as bool implementation, so...
103         CValue* temp = new CBoolValue(false,"");
104         CValue* result = temp->CalcFinal(dtype,op,val);
105         temp->Release();
106
107         return result;
108 }
109
110
111
112 const STR_String& SCA_ILogicBrick::GetText()
113
114         if (m_name.Length())
115                 return m_name;
116
117         return m_text;
118 }
119
120
121
122 float SCA_ILogicBrick::GetNumber()
123 {
124         return -1;
125 }
126
127
128
129 STR_String SCA_ILogicBrick::GetName()
130 {
131         return m_name;
132 }
133
134
135
136 void SCA_ILogicBrick::SetName(STR_String name)
137 {
138         m_name = name;
139 }
140
141
142
143 void SCA_ILogicBrick::ReplicaSetName(STR_String name)
144 {
145         m_name = name;
146 }
147                 
148
149
150 bool SCA_ILogicBrick::IsActive()
151 {
152         return m_bActive;
153 }
154
155
156
157 bool SCA_ILogicBrick::LessComparedTo(SCA_ILogicBrick* other)
158 {
159         return (this->m_Execute_Ueber_Priority < other->m_Execute_Ueber_Priority) 
160                 || ((this->m_Execute_Ueber_Priority == other->m_Execute_Ueber_Priority) && 
161                 (this->m_Execute_Priority < other->m_Execute_Priority));
162 }
163
164
165
166 void SCA_ILogicBrick::SetActive(bool active)
167 {
168         m_bActive=active;
169         if (active)
170         {
171                 //m_gameobj->SetDebugColor(GetDrawColor());
172         } else
173         {
174                 //m_gameobj->ResetDebugColor();
175         }
176 }
177
178
179
180 void SCA_ILogicBrick::RegisterEvent(CValue* eventval)
181 {
182         if (m_eventval)
183                 m_eventval->Release();
184
185         m_eventval = eventval->AddRef();
186 }
187
188
189 void SCA_ILogicBrick::RemoveEvent()
190 {
191         if (m_eventval)
192         {
193                 m_eventval->Release();
194                 m_eventval = NULL;
195         }
196 }
197
198
199
200 CValue* SCA_ILogicBrick::GetEvent()
201 {
202         if (m_eventval)
203         {
204                 return m_eventval->AddRef();
205         }
206         
207         return NULL;
208 }
209
210
211
212
213 /* python stuff */
214
215 PyTypeObject SCA_ILogicBrick::Type = {
216         PyObject_HEAD_INIT(&PyType_Type)
217         0,
218         "SCA_ILogicBrick",
219         sizeof(SCA_ILogicBrick),
220         0,
221         PyDestructor,
222         0,
223         __getattr,
224         __setattr,
225         0, //&MyPyCompare,
226         __repr,
227         0, //&cvalue_as_number,
228         0,
229         0,
230         0,
231         0
232 };
233
234
235
236 PyParentObject SCA_ILogicBrick::Parents[] = {
237         &SCA_ILogicBrick::Type,
238         &CValue::Type,
239         NULL
240 };
241
242
243
244 PyMethodDef SCA_ILogicBrick::Methods[] = {
245   {"getOwner", (PyCFunction) SCA_ILogicBrick::sPyGetOwner, METH_NOARGS},
246   {"getExecutePriority", (PyCFunction) SCA_ILogicBrick::sPySetExecutePriority, METH_NOARGS},
247   {"setExecutePriority", (PyCFunction) SCA_ILogicBrick::sPySetExecutePriority, METH_VARARGS},
248   {NULL,NULL} //Sentinel
249 };
250
251
252
253 PyObject*
254 SCA_ILogicBrick::_getattr(const STR_String& attr)
255 {
256   _getattr_up(CValue);
257 }
258
259
260
261 PyObject* SCA_ILogicBrick::PyGetOwner(PyObject* self)
262 {
263         CValue* parent = GetParent();
264         if (parent)
265         {
266                 parent->AddRef();
267                 return parent;
268         }
269
270         printf("ERROR: Python scriptblock without owner\n");
271         Py_INCREF(Py_None);
272         return Py_None;//Int_FromLong(IsPositiveTrigger());
273 }
274
275
276
277 PyObject* SCA_ILogicBrick::PySetExecutePriority(PyObject* self, 
278                                PyObject* args, 
279                                PyObject* kwds)
280 {
281
282         int priority=0;
283
284     if (!PyArg_ParseTuple(args, "i", &priority)) {
285                 return NULL;
286     }
287         
288         m_Execute_Ueber_Priority = priority;
289
290         Py_Return;
291 }
292
293
294
295 PyObject* SCA_ILogicBrick::PyGetExecutePriority(PyObject* self)
296 {
297         return PyInt_FromLong(m_Execute_Ueber_Priority);
298 }
299
300
301
302 /* Conversions for making life better. */
303 bool SCA_ILogicBrick::PyArgToBool(int boolArg)
304 {
305         if (boolArg) {
306                 return true;
307         } else {
308                 return false;
309         }
310 }
311
312
313
314 PyObject* SCA_ILogicBrick::BoolToPyArg(bool boolarg)
315 {
316         return PyInt_FromLong(boolarg? KX_TRUE: KX_FALSE);      
317 }