rename and negate DISABLE_PYTHON --> WITH_PYTHON
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 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 #ifdef WITH_PYTHON
46         struct _object *                m_bytecode; /* SCA_PYEXEC_SCRIPT only */
47         PyObject*                               m_function; /* SCA_PYEXEC_MODULE only */
48 #endif
49         int                                             m_function_argc;
50         bool                                    m_bModified;
51         bool                                    m_debug;        /* use with SCA_PYEXEC_MODULE for reloading every logic run */
52         int                                             m_mode;
53
54         
55  protected:
56         STR_String                              m_scriptText;
57         STR_String                              m_scriptName;
58 #ifdef WITH_PYTHON
59         PyObject*                               m_pythondictionary;     /* for SCA_PYEXEC_SCRIPT only */
60         PyObject*                               m_pythonfunction;       /* for SCA_PYEXEC_MODULE only */
61 #endif
62         std::vector<class SCA_ISensor*>         m_triggeredSensors;
63  
64  public:
65         enum SCA_PyExecMode
66         {
67                 SCA_PYEXEC_SCRIPT = 0,
68                 SCA_PYEXEC_MODULE,
69                 SCA_PYEXEC_MAX
70         };
71
72         static SCA_PythonController* m_sCurrentController; // protected !!!
73
74         //for debugging
75         //virtual       CValue*         AddRef();
76         //virtual int                   Release();                                                                                              // Release a reference to this value (when reference count reaches 0, the value is removed from the heap)
77
78         SCA_PythonController(SCA_IObject* gameobj, int mode);
79         virtual ~SCA_PythonController();
80
81         virtual CValue* GetReplica();
82         virtual void  Trigger(class SCA_LogicManager* logicmgr);
83   
84         void    SetScriptText(const STR_String& text);
85         void    SetScriptName(const STR_String& name);
86 #ifdef WITH_PYTHON
87         void    SetNamespace(PyObject*  pythondictionary);
88 #endif
89         void    SetDebug(bool debug) { m_debug = debug; }
90         void    AddTriggeredSensor(class SCA_ISensor* sensor)
91                 { m_triggeredSensors.push_back(sensor); }
92         int             IsTriggered(class SCA_ISensor* sensor);
93         bool    Compile();
94         bool    Import();
95         void    ErrorPrint(const char *error_msg);
96         
97 #ifdef WITH_PYTHON
98         static const char* sPyGetCurrentController__doc__;
99         static PyObject* sPyGetCurrentController(PyObject* self);
100         static const char* sPyAddActiveActuator__doc__;
101         static PyObject* sPyAddActiveActuator(PyObject* self, 
102                                                                                   PyObject* args);
103         static SCA_IActuator* LinkedActuatorFromPy(PyObject *value);
104
105                 
106         KX_PYMETHOD_O(SCA_PythonController,Activate);
107         KX_PYMETHOD_O(SCA_PythonController,DeActivate);
108         KX_PYMETHOD_O(SCA_PythonController,SetScript);
109         KX_PYMETHOD_NOARGS(SCA_PythonController,GetScript);
110
111         
112         static PyObject*        pyattr_get_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef);
113         static int                      pyattr_set_script(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value);
114 #endif
115 };
116
117 #endif //KX_PYTHONCONTROLLER_H
118