remove config.h references, was added for automake build system rev around 124-126...
[blender.git] / source / gameengine / GameLogic / SCA_PythonMouse.cpp
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * Contributor(s): none yet.
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 #include "SCA_PythonMouse.h"
26 #include "SCA_IInputDevice.h"
27 #include "RAS_ICanvas.h"
28
29 /* ------------------------------------------------------------------------- */
30 /* Native functions                                                          */
31 /* ------------------------------------------------------------------------- */
32
33 SCA_PythonMouse::SCA_PythonMouse(SCA_IInputDevice* mouse, RAS_ICanvas* canvas)
34 : PyObjectPlus(),
35 m_canvas(canvas),
36 m_mouse(mouse)
37 {
38 }
39
40 SCA_PythonMouse::~SCA_PythonMouse()
41 {
42         /* intentionally empty */
43 }
44
45 #ifndef DISABLE_PYTHON
46
47 /* ------------------------------------------------------------------------- */
48 /* Python functions                                                          */
49 /* ------------------------------------------------------------------------- */
50
51 /* Integration hooks ------------------------------------------------------- */
52 PyTypeObject SCA_PythonMouse::Type = {
53         PyVarObject_HEAD_INIT(NULL, 0)
54         "SCA_PythonMouse",
55         sizeof(PyObjectPlus_Proxy),
56         0,
57         py_base_dealloc,
58         0,
59         0,
60         0,
61         0,
62         py_base_repr,
63         0,0,0,0,0,0,0,0,0,
64         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
65         0,0,0,0,0,0,0,
66         Methods,
67         0,
68         0,
69         &PyObjectPlus::Type,
70         0,0,0,0,0,0,
71         py_base_new
72 };
73
74 PyMethodDef SCA_PythonMouse::Methods[] = {
75 //      KX_PYMETHODTABLE(SCA_PythonMouse, show),
76         {NULL,NULL} //Sentinel
77 };
78
79 PyAttributeDef SCA_PythonMouse::Attributes[] = {
80         KX_PYATTRIBUTE_RO_FUNCTION("events", SCA_PythonMouse, pyattr_get_events),
81         KX_PYATTRIBUTE_RW_FUNCTION("position", SCA_PythonMouse, pyattr_get_position, pyattr_set_position),
82         KX_PYATTRIBUTE_RW_FUNCTION("visible", SCA_PythonMouse, pyattr_get_visible, pyattr_set_visible),
83         { NULL }        //Sentinel
84 };      
85
86 PyObject* SCA_PythonMouse::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
87 {
88         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
89
90         PyObject* resultlist = PyList_New(0);
91
92         for (int i=SCA_IInputDevice::KX_BEGINMOUSE; i<=SCA_IInputDevice::KX_ENDMOUSE; i++)
93         {
94                 const SCA_InputEvent & inevent = self->m_mouse->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i);
95                 
96                 
97                 if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS)
98                 {
99                         PyObject* keypair = PyTuple_New(2);
100                         PyTuple_SET_ITEM(keypair, 0, PyLong_FromSsize_t(i));
101                         PyTuple_SET_ITEM(keypair, 1, PyLong_FromSsize_t(inevent.m_status));
102                         PyList_Append(resultlist, keypair);
103                 }
104         }
105
106         return resultlist;
107 }
108
109 PyObject* SCA_PythonMouse::pyattr_get_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
110 {
111         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
112         const SCA_InputEvent & xevent = self->m_mouse->GetEventValue(SCA_IInputDevice::KX_MOUSEX);
113         const SCA_InputEvent & yevent = self->m_mouse->GetEventValue(SCA_IInputDevice::KX_MOUSEY);
114
115         PyObject* ret = PyTuple_New(2);
116
117         PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(float(xevent.m_eventval)/self->m_canvas->GetWidth()));
118         PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(float(yevent.m_eventval)/self->m_canvas->GetHeight()));
119
120         return ret;
121 }
122
123 int SCA_PythonMouse::pyattr_set_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
124 {
125         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
126         int x, y;
127         float pyx, pyy;
128         if (!PyArg_ParseTuple(value, "ff:position", &pyx, &pyy))
129                 return PY_SET_ATTR_FAIL;
130
131         x = (int)(pyx*self->m_canvas->GetWidth());
132         y = (int)(pyy*self->m_canvas->GetHeight());
133
134         self->m_canvas->SetMousePosition(x, y);
135
136         return PY_SET_ATTR_SUCCESS;
137 }
138
139 PyObject* SCA_PythonMouse::pyattr_get_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
140 {
141         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
142
143         int visible;
144
145         if (self->m_canvas->GetMouseState() == RAS_ICanvas::MOUSE_INVISIBLE)
146                 visible = 0;
147         else
148                 visible = 1;
149
150         return PyBool_FromLong(visible);
151 }
152
153 int SCA_PythonMouse::pyattr_set_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
154 {
155         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
156
157         int visible = PyObject_IsTrue(value);
158
159         if (visible == -1)
160         {
161                 PyErr_SetString(PyExc_AttributeError, "SCA_PythonMouse.visible = bool: SCA_PythonMouse, expected True or False");
162                 return PY_SET_ATTR_FAIL;
163         }
164
165         if (visible)
166                 self->m_canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
167         else
168                 self->m_canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
169
170         return PY_SET_ATTR_SUCCESS;
171 }
172
173 #endif