8b379bcd44fbe67098e41862250932c5ca18f35e
[blender.git] / source / gameengine / Ketsji / KX_ParentActuator.cpp
1 /**
2  * Set or remove an objects parent
3  *
4  * $Id: SCA_ParentActuator.cpp 13932 2008-03-01 19:05:41Z ben2610 $
5  *
6  * ***** BEGIN GPL LICENSE BLOCK *****
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version. The Blender
12  * Foundation also sells licenses for use in proprietary software under
13  * the Blender License.  See http://www.blender.org/BL/ for information
14  * about this.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  *
25  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): none yet.
31  *
32  * ***** END GPL LICENSE BLOCK *****
33  */
34
35 #include "KX_ParentActuator.h"
36 #include "KX_GameObject.h"
37 #include "KX_PythonInit.h"
38
39 #ifdef HAVE_CONFIG_H
40 #include <config.h>
41 #endif
42
43 /* ------------------------------------------------------------------------- */
44 /* Native functions                                                          */
45 /* ------------------------------------------------------------------------- */
46
47 KX_ParentActuator::KX_ParentActuator(SCA_IObject *gameobj, 
48                                                                          int mode,
49                                                                          CValue *ob,
50                                                                          PyTypeObject* T)
51         : SCA_IActuator(gameobj, T),
52           m_mode(mode),
53           m_ob(ob)
54 {
55
56
57
58
59 KX_ParentActuator::~KX_ParentActuator()
60 {
61         /* intentionally empty */ 
62
63
64
65
66 CValue* KX_ParentActuator::GetReplica()
67 {
68         KX_ParentActuator* replica = new KX_ParentActuator(*this);
69         // replication just copy the m_base pointer => common random generator
70         replica->ProcessReplica();
71         CValue::AddDataToReplica(replica);
72
73         return replica;
74 }
75
76
77
78 bool KX_ParentActuator::Update()
79 {
80         bool bNegativeEvent = IsNegativeEvent();
81         RemoveAllEvents();
82
83         if (bNegativeEvent)
84                 return false; // do nothing on negative events
85
86         KX_GameObject *obj = (KX_GameObject*) GetParent();
87         KX_Scene *scene = PHY_GetActiveScene();
88         switch (m_mode) {
89                 case KX_PARENT_SET:
90                         obj->SetParent(scene, (KX_GameObject*)m_ob);
91                         break;
92                 case KX_PARENT_REMOVE:
93                         obj->RemoveParent(scene);
94                         break;
95         };
96         
97         return false;
98 }
99
100 /* ------------------------------------------------------------------------- */
101 /* Python functions                                                          */
102 /* ------------------------------------------------------------------------- */
103
104 /* Integration hooks ------------------------------------------------------- */
105 PyTypeObject KX_ParentActuator::Type = {
106         PyObject_HEAD_INIT(&PyType_Type)
107         0,
108         "KX_ParentActuator",
109         sizeof(KX_ParentActuator),
110         0,
111         PyDestructor,
112         0,
113         __getattr,
114         __setattr,
115         0, //&MyPyCompare,
116         __repr,
117         0, //&cvalue_as_number,
118         0,
119         0,
120         0,
121         0
122 };
123
124 PyParentObject KX_ParentActuator::Parents[] = {
125         &KX_ParentActuator::Type,
126         &SCA_IActuator::Type,
127         &SCA_ILogicBrick::Type,
128         &CValue::Type,
129         NULL
130 };
131
132 PyMethodDef KX_ParentActuator::Methods[] = {
133         {"setObject",         (PyCFunction) KX_ParentActuator::sPySetObject, METH_VARARGS, SetObject_doc},
134         {"getObject",         (PyCFunction) KX_ParentActuator::sPyGetObject, METH_VARARGS, GetObject_doc},
135         {NULL,NULL} //Sentinel
136 };
137
138 PyObject* KX_ParentActuator::_getattr(const STR_String& attr) {
139         _getattr_up(SCA_IActuator);
140 }
141
142 /* 1. setObject                                                            */
143 char KX_ParentActuator::SetObject_doc[] = 
144 "setObject(object)\n"
145 "\tSet the object to set as parent.\n"
146 "\tCan be an object name or an object\n";
147 PyObject* KX_ParentActuator::PySetObject(PyObject* self, PyObject* args, PyObject* kwds) {
148         PyObject* gameobj;
149         if (PyArg_ParseTuple(args, "O!", &KX_GameObject::Type, &gameobj))
150         {
151                 m_ob = (CValue*)gameobj;
152                 Py_Return;
153         }
154         PyErr_Clear();
155         
156         char* objectname;
157         if (PyArg_ParseTuple(args, "s", &objectname))
158         {
159                 CValue *object = (CValue*)SCA_ILogicBrick::m_sCurrentLogicManager->GetGameObjectByName(STR_String(objectname));
160                 if(object)
161                 {
162                         m_ob = object;
163                         Py_Return;
164                 }
165         }
166         
167         return NULL;
168 }
169
170 /* 2. getObject                                                            */
171 char KX_ParentActuator::GetObject_doc[] =
172 "getObject()\n"
173 "\tReturns the object that is set to.\n";
174 PyObject* KX_ParentActuator::PyGetObject(PyObject* self, PyObject* args, PyObject* kwds) {
175         return PyString_FromString(m_ob->GetName());
176 }
177         
178 /* eof */