Blender V2.61 - r43446

SCA_ILogicBrick.cpp

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