Blender  V2.59
SCA_ILogicBrick.cpp
Go to the documentation of this file.
00001 /*
00002  * $Id: SCA_ILogicBrick.cpp 36523 2011-05-06 20:18:42Z blendix $
00003  * ***** BEGIN GPL LICENSE BLOCK *****
00004  *
00005  * This program is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public License
00007  * as published by the Free Software Foundation; either version 2
00008  * of the License, or (at your option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013  * GNU General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License
00016  * along with this program; if not, write to the Free Software Foundation,
00017  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00018  *
00019  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
00020  * All rights reserved.
00021  *
00022  * The Original Code is: all of this file.
00023  *
00024  * Contributor(s): none yet.
00025  *
00026  * ***** END GPL LICENSE BLOCK *****
00027  */
00028 
00034 #include <stddef.h>
00035 
00036 #include "SCA_ILogicBrick.h"
00037 #include "PyObjectPlus.h"
00038 
00039 SCA_LogicManager* SCA_ILogicBrick::m_sCurrentLogicManager = NULL;
00040 
00041 SCA_ILogicBrick::SCA_ILogicBrick(SCA_IObject* gameobj)
00042         :
00043         CValue(),
00044         m_gameobj(gameobj),
00045         m_Execute_Priority(0),
00046         m_Execute_Ueber_Priority(0),
00047         m_bActive(false),
00048         m_eventval(0)
00049 {
00050         m_text = "KX_LogicBrick";
00051 }
00052 
00053 
00054 
00055 SCA_ILogicBrick::~SCA_ILogicBrick()
00056 {
00057         RemoveEvent();
00058 }
00059 
00060 
00061 
00062 void SCA_ILogicBrick::SetExecutePriority(int execute_Priority)
00063 {
00064         m_Execute_Priority = execute_Priority;
00065 }
00066 
00067 
00068 
00069 void SCA_ILogicBrick::SetUeberExecutePriority(int execute_Priority)
00070 {
00071         m_Execute_Ueber_Priority = execute_Priority;
00072 }
00073 
00074 
00075 
00076 void SCA_ILogicBrick::ReParent(SCA_IObject* parent)
00077 {
00078         m_gameobj = parent;
00079 }
00080 
00081 void SCA_ILogicBrick::Relink(CTR_Map<CTR_HashedPtr, void*> *obj_map)
00082 {
00083         // nothing to do
00084 }
00085 
00086 CValue* SCA_ILogicBrick::Calc(VALUE_OPERATOR op, CValue *val)
00087 {
00088         CValue* temp = new CBoolValue(false,"");
00089         CValue* result = temp->Calc(op,val);
00090         temp->Release();
00091 
00092         return result;
00093 } 
00094 
00095 
00096 
00097 CValue* SCA_ILogicBrick::CalcFinal(VALUE_DATA_TYPE dtype,
00098                                                                    VALUE_OPERATOR op,
00099                                                                    CValue *val)
00100 {
00101         // same as bool implementation, so...
00102         CValue* temp = new CBoolValue(false,"");
00103         CValue* result = temp->CalcFinal(dtype,op,val);
00104         temp->Release();
00105 
00106         return result;
00107 }
00108 
00109 
00110 
00111 const STR_String& SCA_ILogicBrick::GetText()
00112 { 
00113         if (m_name.Length())
00114                 return m_name;
00115 
00116         return m_text;
00117 }
00118 
00119 
00120 
00121 double SCA_ILogicBrick::GetNumber()
00122 {
00123         return -1;
00124 }
00125 
00126 
00127 
00128 STR_String& SCA_ILogicBrick::GetName()
00129 {
00130         return m_name;
00131 }
00132 
00133 
00134 
00135 void SCA_ILogicBrick::SetName(const char *name)
00136 {
00137         m_name = name;
00138 }
00139 
00140 bool SCA_ILogicBrick::LessComparedTo(SCA_ILogicBrick* other)
00141 {
00142         return (this->m_Execute_Ueber_Priority < other->m_Execute_Ueber_Priority) 
00143                 || ((this->m_Execute_Ueber_Priority == other->m_Execute_Ueber_Priority) && 
00144                 (this->m_Execute_Priority < other->m_Execute_Priority));
00145 }
00146 
00147 void SCA_ILogicBrick::RegisterEvent(CValue* eventval)
00148 {
00149         if (m_eventval)
00150                 m_eventval->Release();
00151 
00152         m_eventval = eventval->AddRef();
00153 }
00154 
00155 
00156 void SCA_ILogicBrick::RemoveEvent()
00157 {
00158         if (m_eventval)
00159         {
00160                 m_eventval->Release();
00161                 m_eventval = NULL;
00162         }
00163 }
00164 
00165 
00166 
00167 CValue* SCA_ILogicBrick::GetEvent()
00168 {
00169         if (m_eventval)
00170         {
00171                 return m_eventval->AddRef();
00172         }
00173         
00174         return NULL;
00175 }
00176 
00177 
00178 
00179 #ifdef WITH_PYTHON
00180 
00181 /* python stuff */
00182 
00183 PyTypeObject SCA_ILogicBrick::Type = {
00184         PyVarObject_HEAD_INIT(NULL, 0)
00185         "SCA_ILogicBrick",
00186         sizeof(PyObjectPlus_Proxy),
00187         0,
00188         py_base_dealloc,
00189         0,
00190         0,
00191         0,
00192         0,
00193         py_base_repr,
00194         0,0,0,0,0,0,0,0,0,
00195         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
00196         0,0,0,0,0,0,0,
00197         Methods,
00198         0,
00199         0,
00200         &CValue::Type,
00201         0,0,0,0,0,0,
00202         py_base_new
00203 };
00204 
00205 PyMethodDef SCA_ILogicBrick::Methods[] = {
00206   {NULL,NULL} //Sentinel
00207 };
00208 
00209 PyAttributeDef SCA_ILogicBrick::Attributes[] = {
00210         KX_PYATTRIBUTE_RO_FUNCTION("owner",     SCA_ILogicBrick, pyattr_get_owner),
00211         KX_PYATTRIBUTE_INT_RW("executePriority",0,100000,false,SCA_ILogicBrick,m_Execute_Priority),
00212         KX_PYATTRIBUTE_STRING_RO("name", SCA_ILogicBrick, m_name),
00213         {NULL} //Sentinel
00214 };
00215 
00216 int SCA_ILogicBrick::CheckProperty(void *self, const PyAttributeDef *attrdef)
00217 {
00218         if (attrdef->m_type != KX_PYATTRIBUTE_TYPE_STRING || attrdef->m_length != 1) {
00219                 PyErr_SetString(PyExc_AttributeError, "inconsistent check function for attribute type, report to blender.org");
00220                 return 1;
00221         }
00222         SCA_ILogicBrick* brick = reinterpret_cast<SCA_ILogicBrick*>(self);
00223         STR_String* var = reinterpret_cast<STR_String*>((char*)self+attrdef->m_offset);
00224         CValue* prop = brick->GetParent()->FindIdentifier(*var);
00225         bool error = prop->IsError();
00226         prop->Release();
00227         if (error) {
00228                 PyErr_SetString(PyExc_ValueError, "string does not correspond to a property");
00229                 return 1;
00230         }
00231         return 0;
00232 }
00233 
00234 /*Attribute functions */
00235 PyObject* SCA_ILogicBrick::pyattr_get_owner(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
00236 {
00237         SCA_ILogicBrick* self= static_cast<SCA_ILogicBrick*>(self_v);
00238         CValue* parent = self->GetParent();
00239         
00240         if (parent)
00241                 return parent->GetProxy();
00242         
00243         Py_RETURN_NONE;
00244 }
00245 
00246 
00247 
00248 /* Conversions for making life better. */
00249 bool SCA_ILogicBrick::PyArgToBool(int boolArg)
00250 {
00251         if (boolArg) {
00252                 return true;
00253         } else {
00254                 return false;
00255         }
00256 }
00257 
00258 PyObject* SCA_ILogicBrick::BoolToPyArg(bool boolarg)
00259 {
00260         return PyLong_FromSsize_t(boolarg? KX_TRUE: KX_FALSE);  
00261 }
00262 
00263 #endif // WITH_PYTHON