svn merge -r 21041:21301 https://svn.blender.org/svnroot/bf-blender/branches/blender2...
[blender.git] / source / gameengine / GameLogic / SCA_PythonController.h
1 /**
2  * Execute Python scripts
3  *
4  * $Id$
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.
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 LICENSE BLOCK *****
30  */
31
32 #ifndef KX_PYTHONCONTROLLER_H
33 #define KX_PYTHONCONTROLLER_H
34               
35 #include "SCA_IController.h"
36 #include "SCA_LogicManager.h"
37 #include "BoolValue.h"
38
39 #include <vector>
40
41 class SCA_IObject;
42 class SCA_PythonController : public SCA_IController
43 {
44         Py_Header;
45         struct _object *                m_bytecode; /* SCA_PYEXEC_SCRIPT only */
46         PyObject*                               m_function; /* SCA_PYEXEC_MODULE only */
47         int                                             m_function_argc;
48         bool                                    m_bModified;
49         bool                                    m_debug;        /* use with SCA_PYEXEC_MODULE for reloading every logic run */
50         int                                             m_mode;
51
52         
53  protected:
54         STR_String                              m_scriptText;
55         STR_String                              m_scriptName;
56         PyObject*                               m_pythondictionary;     /* for SCA_PYEXEC_SCRIPT only */
57         PyObject*                               m_pythonfunction;       /* for SCA_PYEXEC_MODULE only */
58  
59         std::vector<class SCA_ISensor*>         m_triggeredSensors;
60  
61  public:
62         enum SCA_PyExecMode
63         {
64                 SCA_PYEXEC_SCRIPT = 0,
65                 SCA_PYEXEC_MODULE,
66                 SCA_PYEXEC_MAX
67         };
68
69         static SCA_PythonController* m_sCurrentController; // protected !!!
70
71         //for debugging
72         //virtual       CValue*         AddRef();
73         //virtual int                   Release();                                                                                              // Release a reference to this value (when reference count reaches 0, the value is removed from the heap)
74
75         SCA_PythonController(SCA_IObject* gameobj, int mode);
76         virtual ~SCA_PythonController();
77
78         virtual CValue* GetReplica();
79         virtual void  Trigger(class SCA_LogicManager* logicmgr);
80   
81         void    SetScriptText(const STR_String& text);
82         void    SetScriptName(const STR_String& name);
83         void    SetDictionary(PyObject* pythondictionary);
84         void    SetDebug(bool debug) { m_debug = debug; }
85         void    AddTriggeredSensor(class SCA_ISensor* sensor)
86                 { m_triggeredSensors.push_back(sensor); }
87         int             IsTriggered(class SCA_ISensor* sensor);
88         bool    Compile();
89         bool    Import();
90         void    ErrorPrint(const char *error_msg);
91         
92
93         static const char* sPyGetCurrentController__doc__;
94         static PyObject* sPyGetCurrentController(PyObject* self);
95         static const char* sPyAddActiveActuator__doc__;
96         static PyObject* sPyAddActiveActuator(PyObject* self, 
97                                                                                   PyObject* args);
98         static SCA_IActuator* LinkedActuatorFromPy(PyObject *value);
99
100                 
101         KX_PYMETHOD_O(SCA_PythonController,Activate);
102         KX_PYMETHOD_O(SCA_PythonController,DeActivate);
103         KX_PYMETHOD_O(SCA_PythonController,SetScript);
104         KX_PYMETHOD_NOARGS(SCA_PythonController,GetScript);
105
106         
107         static PyObject*        pyattr_get_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
108         static int                      pyattr_set_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
109         
110
111 };
112
113 #endif //KX_PYTHONCONTROLLER_H
114