- for BGE mouse and keyboard events use tuples rather then lists
[blender.git] / source / gameengine / GameLogic / SCA_PythonMouse.cpp
1 /**
2  * Python Mouse Object
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 #ifdef HAVE_CONFIG_H
33 #include <config.h>
34 #endif
35
36 #include "SCA_PythonMouse.h"
37 #include "SCA_IInputDevice.h"
38 #include "RAS_ICanvas.h"
39
40 /* ------------------------------------------------------------------------- */
41 /* Native functions                                                          */
42 /* ------------------------------------------------------------------------- */
43
44 SCA_PythonMouse::SCA_PythonMouse(SCA_IInputDevice* mouse, RAS_ICanvas* canvas)
45 : PyObjectPlus(),
46 m_canvas(canvas),
47 m_mouse(mouse)
48 {
49 }
50
51 SCA_PythonMouse::~SCA_PythonMouse()
52 {
53         /* intentionally empty */
54 }
55
56 #ifndef DISABLE_PYTHON
57
58 /* ------------------------------------------------------------------------- */
59 /* Python functions                                                          */
60 /* ------------------------------------------------------------------------- */
61
62 /* Integration hooks ------------------------------------------------------- */
63 PyTypeObject SCA_PythonMouse::Type = {
64         PyVarObject_HEAD_INIT(NULL, 0)
65         "SCA_PythonMouse",
66         sizeof(PyObjectPlus_Proxy),
67         0,
68         py_base_dealloc,
69         0,
70         0,
71         0,
72         0,
73         py_base_repr,
74         0,0,0,0,0,0,0,0,0,
75         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
76         0,0,0,0,0,0,0,
77         Methods,
78         0,
79         0,
80         &PyObjectPlus::Type,
81         0,0,0,0,0,0,
82         py_base_new
83 };
84
85 PyMethodDef SCA_PythonMouse::Methods[] = {
86 //      KX_PYMETHODTABLE(SCA_PythonMouse, show),
87         {NULL,NULL} //Sentinel
88 };
89
90 PyAttributeDef SCA_PythonMouse::Attributes[] = {
91         KX_PYATTRIBUTE_RO_FUNCTION("events", SCA_PythonMouse, pyattr_get_events),
92         KX_PYATTRIBUTE_RW_FUNCTION("position", SCA_PythonMouse, pyattr_get_position, pyattr_set_position),
93         KX_PYATTRIBUTE_RW_FUNCTION("visible", SCA_PythonMouse, pyattr_get_visible, pyattr_set_visible),
94         { NULL }        //Sentinel
95 };      
96
97 PyObject* SCA_PythonMouse::pyattr_get_events(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
98 {
99         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
100
101         PyObject* resultlist = PyList_New(0);
102
103         for (int i=SCA_IInputDevice::KX_BEGINMOUSE; i<=SCA_IInputDevice::KX_ENDMOUSE; i++)
104         {
105                 const SCA_InputEvent & inevent = self->m_mouse->GetEventValue((SCA_IInputDevice::KX_EnumInputs)i);
106                 
107                 
108                 if (inevent.m_status != SCA_InputEvent::KX_NO_INPUTSTATUS)
109                 {
110                         PyObject* keypair = PyTuple_New(2);
111                         PyTuple_SET_ITEM(keypair, 0, PyLong_FromSsize_t(i));
112                         PyTuple_SET_ITEM(keypair, 1, PyLong_FromSsize_t(inevent.m_status));
113                         PyList_Append(resultlist, keypair);
114                 }
115         }
116
117         return resultlist;
118 }
119
120 PyObject* SCA_PythonMouse::pyattr_get_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
121 {
122         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
123         const SCA_InputEvent & xevent = self->m_mouse->GetEventValue(SCA_IInputDevice::KX_MOUSEX);
124         const SCA_InputEvent & yevent = self->m_mouse->GetEventValue(SCA_IInputDevice::KX_MOUSEY);
125
126         PyObject* ret = PyTuple_New(2);
127
128         PyTuple_SET_ITEM(ret, 0, PyFloat_FromDouble(float(xevent.m_eventval)/self->m_canvas->GetWidth()));
129         PyTuple_SET_ITEM(ret, 1, PyFloat_FromDouble(float(yevent.m_eventval)/self->m_canvas->GetHeight()));
130
131         return ret;
132 }
133
134 int SCA_PythonMouse::pyattr_set_position(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
135 {
136         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
137         int x, y;
138         float pyx, pyy;
139         if (!PyArg_ParseTuple(value, "ff:position", &pyx, &pyy))
140                 return PY_SET_ATTR_FAIL;
141
142         x = (int)(pyx*self->m_canvas->GetWidth());
143         y = (int)(pyy*self->m_canvas->GetHeight());
144
145         self->m_canvas->SetMousePosition(x, y);
146
147         return PY_SET_ATTR_SUCCESS;
148 }
149
150 PyObject* SCA_PythonMouse::pyattr_get_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
151 {
152         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
153
154         int visible;
155
156         if (self->m_canvas->GetMouseState() == RAS_ICanvas::MOUSE_INVISIBLE)
157                 visible = 0;
158         else
159                 visible = 1;
160
161         return PyBool_FromLong(visible);
162 }
163
164 int SCA_PythonMouse::pyattr_set_visible(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef, PyObject *value)
165 {
166         SCA_PythonMouse* self = static_cast<SCA_PythonMouse*>(self_v);
167
168         int visible = PyObject_IsTrue(value);
169
170         if (visible == -1)
171         {
172                 PyErr_SetString(PyExc_AttributeError, "SCA_PythonMouse.visible = bool: SCA_PythonMouse, expected True or False");
173                 return PY_SET_ATTR_FAIL;
174         }
175
176         if (visible)
177                 self->m_canvas->SetMouseState(RAS_ICanvas::MOUSE_NORMAL);
178         else
179                 self->m_canvas->SetMouseState(RAS_ICanvas::MOUSE_INVISIBLE);
180
181         return PY_SET_ATTR_SUCCESS;
182 }
183
184 #endif